mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 02:36:28 +00:00 
			
		
		
		
	Fixed validation and made some new form elements.
This commit is contained in:
		| @@ -2,6 +2,9 @@ | ||||
|  | ||||
| namespace Firefly\Helper\Controllers; | ||||
|  | ||||
| use Carbon\Carbon; | ||||
| use Exception; | ||||
| use Firefly\Exception\FireflyException; | ||||
| use Firefly\Storage\Account\AccountRepositoryInterface as ARI; | ||||
| use Firefly\Storage\Budget\BudgetRepositoryInterface as BRI; | ||||
| use Firefly\Storage\Category\CategoryRepositoryInterface as CRI; | ||||
| @@ -36,18 +39,18 @@ class Transaction implements TransactionInterface | ||||
|  | ||||
|     /** | ||||
|      * @param TJRI $journals | ||||
|      * @param CRI  $categories | ||||
|      * @param BRI  $budgets | ||||
|      * @param PRI  $piggybanks | ||||
|      * @param ARI  $accounts | ||||
|      * @param CRI $categories | ||||
|      * @param BRI $budgets | ||||
|      * @param PRI $piggybanks | ||||
|      * @param ARI $accounts | ||||
|      */ | ||||
|     public function __construct(TJRI $journals, CRI $categories, BRI $budgets, PRI $piggybanks, ARI $accounts) | ||||
|     { | ||||
|         $this->_journals   = $journals; | ||||
|         $this->_journals = $journals; | ||||
|         $this->_categories = $categories; | ||||
|         $this->_budgets    = $budgets; | ||||
|         $this->_budgets = $budgets; | ||||
|         $this->_piggybanks = $piggybanks; | ||||
|         $this->_accounts   = $accounts; | ||||
|         $this->_accounts = $accounts; | ||||
|         $this->overruleUser(\Auth::user()); | ||||
|     } | ||||
|  | ||||
| @@ -69,7 +72,7 @@ class Transaction implements TransactionInterface | ||||
|  | ||||
|     /** | ||||
|      * @param \TransactionJournal $journal | ||||
|      * @param array               $data | ||||
|      * @param array $data | ||||
|      * | ||||
|      * @return MessageBag|\TransactionJournal | ||||
|      */ | ||||
| @@ -120,7 +123,7 @@ class Transaction implements TransactionInterface | ||||
|         } | ||||
|         if (isset($data['revenue_account'])) { | ||||
|             $from = $this->_accounts->findRevenueAccountByName($data['revenue_account']); | ||||
|             $to   = $this->_accounts->findAssetAccountById($data['account_id']); | ||||
|             $to = $this->_accounts->findAssetAccountById($data['account_id']); | ||||
|         } | ||||
|         if (isset($data['account_from_id'])) { | ||||
|             $from = $this->_accounts->findAssetAccountById($data['account_from_id']); | ||||
| @@ -168,7 +171,7 @@ class Transaction implements TransactionInterface | ||||
|          * Connect budget and category: | ||||
|          */ | ||||
|         $budgetids = !isset($budget) || (isset($budget) && is_null($budget)) ? [] : [$budget->id]; | ||||
|         $catids    = is_null($category) ? [] : [$category->id]; | ||||
|         $catids = is_null($category) ? [] : [$category->id]; | ||||
|         $journal->budgets()->sync($budgetids); | ||||
|         $journal->categories()->sync($catids); | ||||
|         $journal->save(); | ||||
| @@ -179,6 +182,186 @@ class Transaction implements TransactionInterface | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Returns messages about the validation. | ||||
|      * | ||||
|      * @param array $data | ||||
|      * @return array | ||||
|      * @throws FireflyException | ||||
|      */ | ||||
|     public function validate(array $data) | ||||
|     { | ||||
|         $errors = new MessageBag; | ||||
|         $warnings = new MessageBag; | ||||
|         $successes = new MessageBag; | ||||
|  | ||||
|         /* | ||||
|          * Description: | ||||
|          */ | ||||
|         if (strlen($data['description']) == 0) { | ||||
|             $errors->add('description', 'The description should not be this short.'); | ||||
|         } | ||||
|         if (strlen($data['description']) > 250) { | ||||
|             $errors->add('description', 'The description should not be this long.'); | ||||
|         } | ||||
|  | ||||
|         /* | ||||
|          * Amount | ||||
|          */ | ||||
|         if (floatval($data['amount']) <= 0) { | ||||
|             $errors->add('amount', 'The amount cannot be zero or less than zero.'); | ||||
|         } | ||||
|         if (floatval($data['amount']) > 10000) { | ||||
|             $warnings->add('amount', 'OK, but that\'s a lot of money dude.'); | ||||
|         } | ||||
|  | ||||
|         /* | ||||
|          * Date | ||||
|          */ | ||||
|         try { | ||||
|             $date = new Carbon($data['date']); | ||||
|         } catch (Exception $e) { | ||||
|             $errors->add('date', 'The date entered was invalid'); | ||||
|         } | ||||
|         if (strlen($data['date']) == 0) { | ||||
|             $errors->add('date', 'The date entered was invalid'); | ||||
|         } | ||||
|         if (!$errors->has('date')) { | ||||
|             $successes->add('date', 'OK!'); | ||||
|         } | ||||
|  | ||||
|         /* | ||||
|          * Category | ||||
|          */ | ||||
|         $category = $this->_categories->findByName($data['category']); | ||||
|         if (strlen($data['category']) == 0) { | ||||
|             $warnings->add('category', 'No category will be created.'); | ||||
|         } else { | ||||
|             if (is_null($category)) { | ||||
|                 $warnings->add('category', 'Will have to be created.'); | ||||
|             } else { | ||||
|                 $successes->add('category', 'OK!'); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         switch ($data['what']) { | ||||
|             default: | ||||
|                 throw new FireflyException('Cannot validate a ' . $data['what']); | ||||
|                 break; | ||||
|             case 'deposit': | ||||
|                 /* | ||||
|                  * Tests for deposit | ||||
|                  */ | ||||
|                 // asset account | ||||
|                 $accountId = isset($data['account_id']) ? intval($data['account_id']) : 0; | ||||
|                 $account = $this->_accounts->find($accountId); | ||||
|                 if (is_null($account)) { | ||||
|                     $errors->add('account_id', 'Cannot find this asset account.'); | ||||
|                 } else { | ||||
|                     $successes->add('account_id', 'OK!'); | ||||
|                 } | ||||
|  | ||||
|                 // revenue account: | ||||
|                 if (strlen($data['revenue_account']) == 0) { | ||||
|                     $warnings->add('revenue_account', 'Revenue account will be "cash".'); | ||||
|                 } else { | ||||
|                     $exp = $this->_accounts->findRevenueAccountByName($data['revenue_account'], false); | ||||
|                     if (is_null($exp)) { | ||||
|                         $warnings->add('revenue_account', 'Expense account will be created.'); | ||||
|                     } else { | ||||
|                         $successes->add('revenue_account', 'OK!'); | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|                 break; | ||||
|             case 'transfer': | ||||
|                 // account from | ||||
|                 $accountId = isset($data['account_from_id']) ? intval($data['account_from_id']) : 0; | ||||
|                 $account = $this->_accounts->find($accountId); | ||||
|                 if (is_null($account)) { | ||||
|                     $errors->add('account_from_id', 'Cannot find this asset account.'); | ||||
|                 } else { | ||||
|                     $successes->add('account_from_id', 'OK!'); | ||||
|                 } | ||||
|                 unset($accountId); | ||||
|                 // account to | ||||
|                 $accountId = isset($data['account_to_id']) ? intval($data['account_to_id']) : 0; | ||||
|                 $account = $this->_accounts->find($accountId); | ||||
|                 if (is_null($account)) { | ||||
|                     $errors->add('account_to_id', 'Cannot find this asset account.'); | ||||
|                 } else { | ||||
|                     $successes->add('account_to_id', 'OK!'); | ||||
|                 } | ||||
|                 unset($accountId); | ||||
|  | ||||
|                 // piggy bank | ||||
|                 $piggybankId = isset($data['piggybank_id']) ? intval($data['piggybank_id']) : 0; | ||||
|                 $piggybank = $this->_piggybanks->find($piggybankId); | ||||
|                 if (is_null($piggybank)) { | ||||
|                     $warnings->add('piggybank_id', 'No piggy bank will be modified.'); | ||||
|                 } else { | ||||
|                     $successes->add('piggybank_id', 'OK!'); | ||||
|                 } | ||||
|  | ||||
|                 break; | ||||
|             case 'withdrawal': | ||||
|                 /* | ||||
|                  * Tests for withdrawal | ||||
|                  */ | ||||
|                 // asset account | ||||
|                 $accountId = isset($data['account_id']) ? intval($data['account_id']) : 0; | ||||
|                 $account = $this->_accounts->find($accountId); | ||||
|                 if (is_null($account)) { | ||||
|                     $errors->add('account_id', 'Cannot find this asset account.'); | ||||
|                 } else { | ||||
|                     $successes->add('account_id', 'OK!'); | ||||
|                 } | ||||
|  | ||||
|                 // expense account | ||||
|                 if (strlen($data['expense_account']) == 0) { | ||||
|                     $warnings->add('expense_account', 'Expense account will be "cash".'); | ||||
|                 } else { | ||||
|                     $exp = $this->_accounts->findExpenseAccountByName($data['expense_account'], false); | ||||
|                     if (is_null($exp)) { | ||||
|                         $warnings->add('expense_account', 'Expense account will be created.'); | ||||
|                     } else { | ||||
|                         $successes->add('expense_account', 'OK!'); | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|                 // budget | ||||
|                 if (!isset($data['budget_id']) || (isset($data['budget_id']) && intval($data['budget_id']) == 0)) { | ||||
|                     $warnings->add('budget_id', 'No budget selected.'); | ||||
|                 } else { | ||||
|                     $budget = $this->_budgets->find(intval($data['budget_id'])); | ||||
|                     if (is_null($budget)) { | ||||
|                         $errors->add('budget_id', 'This budget does not exist'); | ||||
|                     } else { | ||||
|                         $successes->add('budget_id', 'OK!'); | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|                 break; | ||||
|         } | ||||
|  | ||||
|         if (count($errors->get('description')) == 0) { | ||||
|             $successes->add('description', 'OK!'); | ||||
|         } | ||||
|  | ||||
|         if (count($errors->get('amount')) == 0) { | ||||
|             $successes->add('amount', 'OK!'); | ||||
|         } | ||||
|  | ||||
|         return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes]; | ||||
|         /* | ||||
|          * Tests for deposit | ||||
|          */ | ||||
|         /* | ||||
|          * Tests for transfer | ||||
|          */ | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Store a full transaction journal and associated stuff | ||||
|      * | ||||
| @@ -235,7 +418,7 @@ class Transaction implements TransactionInterface | ||||
|         } | ||||
|         if (isset($data['revenue_account'])) { | ||||
|             $from = $this->_accounts->findRevenueAccountByName($data['revenue_account']); | ||||
|             $to   = $this->_accounts->findAssetAccountById($data['account_id']); | ||||
|             $to = $this->_accounts->findAssetAccountById($data['account_id']); | ||||
|         } | ||||
|         if (isset($data['account_from_id'])) { | ||||
|             $from = $this->_accounts->findAssetAccountById($data['account_from_id']); | ||||
| @@ -247,8 +430,7 @@ class Transaction implements TransactionInterface | ||||
|         /* | ||||
|          * Add a custom error when they are the same. | ||||
|          */ | ||||
|         if ($to->id == | ||||
|             $from->id) { | ||||
|         if ($to->id == $from->id) { | ||||
|             $bag = new MessageBag; | ||||
|             $bag->add('account_from_id', 'The account from cannot be the same as the account to.'); | ||||
|             return $bag; | ||||
|   | ||||
| @@ -19,6 +19,15 @@ interface TransactionInterface { | ||||
|      */ | ||||
|     public function store(array $data); | ||||
|  | ||||
|     /** | ||||
|      * Returns messages about the validation. | ||||
|      * | ||||
|      * @param array $data | ||||
|      * | ||||
|      * @return array | ||||
|      */ | ||||
|     public function validate(array $data); | ||||
|  | ||||
|     /** | ||||
|      * @param \TransactionJournal $journal | ||||
|      * @param array               $data | ||||
|   | ||||
		Reference in New Issue
	
	Block a user