diff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.php index 84da0e1cd7..dc0648ba89 100644 --- a/app/controllers/TransactionController.php +++ b/app/controllers/TransactionController.php @@ -165,8 +165,8 @@ class TransactionController extends BaseController */ public function doRelate() { - $brother = intval(Input::get('id')); - $sister = intval(Input::get('relateTo')); + $brother = intval(Input::get('id')); + $sister = intval(Input::get('relateTo')); $journal = $this->_repository->find($brother); $sis = $this->_repository->find($sister); @@ -358,9 +358,14 @@ class TransactionController extends BaseController */ public function store($what) { - $data = Input::except('_token'); - $data['what'] = $what; - $data['currency'] = 'EUR'; // TODO allow custom currency + $data = Input::except('_token'); + $transactionType = $this->_repository->getJournalType($what); + $transactionCurrency = $this->_repository->getJournalCurrency('EUR'); + $data['transaction_type_id'] = $transactionType->id; + $data['transaction_currency_id'] = $transactionCurrency->id; + $data['completed'] = 0; + $data['what'] = $what; + $data['currency'] = 'EUR'; // TODO allow custom currency // always validate: $messages = $this->_repository->validate($data); @@ -434,10 +439,13 @@ class TransactionController extends BaseController */ public function update(TransactionJournal $journal) { - $data = Input::except('_token'); - $data['currency'] = 'EUR'; - $data['what'] = strtolower($journal->transactionType->type); - $messages = $this->_repository->validate($data); + $data = Input::except('_token'); + $data['currency'] = 'EUR'; + $data['what'] = strtolower($journal->transactionType->type); + $data['transaction_type_id'] = $journal->transaction_type_id; + $data['transaction_currency_id'] = $journal->transaction_currency_id; + $data['completed'] = 1; + $messages = $this->_repository->validate($data); Session::flash('warnings', $messages['warnings']); Session::flash('successes', $messages['successes']); diff --git a/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php b/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php index 8a6641d93f..0daa0aa27b 100644 --- a/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php +++ b/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php @@ -63,11 +63,12 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData */ public function store(array $data) { - $journalType = $this->getJournalType($data['what']); - $currency = $this->getJournalCurrency($data['currency']); - $journal = new \TransactionJournal( - ['transaction_type_id' => $journalType->id, 'transaction_currency_id' => $currency->id, 'user_id' => $this->getUser()->id, - 'description' => $data['description'], 'date' => $data['date'], 'completed' => 0] + $currency = $this->getJournalCurrency($data['currency']); + $journal = new \TransactionJournal( + [ + 'transaction_type_id' => $data['transaction_type_id'], + 'transaction_currency_id' => $currency->id, 'user_id' => $this->getUser()->id, + 'description' => $data['description'], 'date' => $data['date'], 'completed' => 0] ); $journal->save(); @@ -147,39 +148,14 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData $warnings = new MessageBag; $successes = new MessageBag; - $errors = new MessageBag; + $journal = new \TransactionJournal($model); + $journal->isValid(); + $errors = $journal->getErrors(); if (!isset($model['what'])) { $errors->add('description', 'Internal error: need to know type of transaction!'); } - if (isset($model['bill_id']) && intval($model['bill_id']) < 0) { - $errors->add('bill_id', 'Bill is invalid.'); - } - if (!isset($model['description'])) { - $errors->add('description', 'This field is mandatory.'); - } - if (isset($model['description']) && strlen($model['description']) == 0) { - $errors->add('description', 'This field is mandatory.'); - } - if (isset($model['description']) && strlen($model['description']) > 255) { - $errors->add('description', 'Description is too long.'); - } - - if (!isset($model['currency'])) { - $errors->add('description', 'Internal error: currency is mandatory!'); - } - if (isset($model['date']) && !($model['date'] instanceof Carbon) && strlen($model['date']) > 0) { - try { - new Carbon($model['date']); - } catch (\Exception $e) { - $errors->add('date', 'This date is invalid.'); - } - } - if (!isset($model['date'])) { - $errors->add('date', 'This date is invalid.'); - } - /* * Amount: */ @@ -254,12 +230,6 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData } - $validator = \Validator::make([$model], \TransactionJournal::$rules); - if ($validator->invalid()) { - $errors->merge($errors); - } - - /* * Add "OK" */ @@ -275,20 +245,6 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData } - /** - * @param $type - * - * @return \AccountType|null - * @throws FireflyException - */ - public function getJournalType($type) - { - /** @var \FireflyIII\Database\TransactionType\TransactionType $typeRepository */ - $typeRepository = \App::make('FireflyIII\Database\TransactionType\TransactionType'); - - return $typeRepository->findByWhat($type); - } - /** * @param $currency * @@ -397,6 +353,20 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData return; } + /** + * @param $type + * + * @return \TransactionType|null + * @throws FireflyException + */ + public function getJournalType($type) + { + /** @var \FireflyIII\Database\TransactionType\TransactionType $typeRepository */ + $typeRepository = \App::make('FireflyIII\Database\TransactionType\TransactionType'); + + return $typeRepository->findByWhat($type); + } + /** * Returns an object with id $id. * diff --git a/app/lib/FireflyIII/Database/TransactionType/TransactionType.php b/app/lib/FireflyIII/Database/TransactionType/TransactionType.php index d5bf703404..e2bb47c1ab 100644 --- a/app/lib/FireflyIII/Database/TransactionType/TransactionType.php +++ b/app/lib/FireflyIII/Database/TransactionType/TransactionType.php @@ -95,25 +95,16 @@ class TransactionType implements CUD, CommonDatabaseCalls */ public function findByWhat($what) { - switch ($what) { - case 'opening': - return \TransactionType::whereType('Opening balance')->first(); - break; - case 'transfer': - return \TransactionType::whereType('Transfer')->first(); - break; - case 'withdrawal': - return \TransactionType::whereType('Withdrawal')->first(); - break; - case 'deposit': - return \TransactionType::whereType('Deposit')->first(); - break; - default: - throw new FireflyException('Cannot find transaction type described as "' . e($what) . '".'); - break; - - + $translation = [ + 'opening' => 'Opening balance', + 'transfer' => 'Transfer', + 'withdrawal' => 'Withdrawal', + 'deposit' => 'Deposit', + ]; + if(!isset($translation[$what])) { + throw new FireflyException('Cannot find transaction type described as "' . e($what) . '".'); } + return \TransactionType::whereType($translation[$what])->first(); } /** diff --git a/app/lib/FireflyIII/Helper/TransactionJournal/Helper.php b/app/lib/FireflyIII/Helper/TransactionJournal/Helper.php index 8b909c7493..b0a9496cdf 100644 --- a/app/lib/FireflyIII/Helper/TransactionJournal/Helper.php +++ b/app/lib/FireflyIII/Helper/TransactionJournal/Helper.php @@ -11,6 +11,16 @@ use Illuminate\Support\Collection; */ class Helper implements HelperInterface { + + /** + * @param $what + * + * @return int + */ + public function getTransactionTypeIdByWhat($what) { + + } + /** * * Get the account_id, which is the asset account that paid for the transaction. diff --git a/app/lib/FireflyIII/Helper/TransactionJournal/HelperInterface.php b/app/lib/FireflyIII/Helper/TransactionJournal/HelperInterface.php index dc5703dc22..b500d206a1 100644 --- a/app/lib/FireflyIII/Helper/TransactionJournal/HelperInterface.php +++ b/app/lib/FireflyIII/Helper/TransactionJournal/HelperInterface.php @@ -22,6 +22,13 @@ interface HelperInterface */ public function getAssetAccount($what, Collection $transactions); + /** + * @param $what + * + * @return int + */ + public function getTransactionTypeIdByWhat($what); + /** * @return Collection */ diff --git a/app/lib/FireflyIII/Shared/Toolkit/Date.php b/app/lib/FireflyIII/Shared/Toolkit/Date.php index 9f563d3706..3f7b321216 100644 --- a/app/lib/FireflyIII/Shared/Toolkit/Date.php +++ b/app/lib/FireflyIII/Shared/Toolkit/Date.php @@ -2,6 +2,7 @@ namespace FireflyIII\Shared\Toolkit; +use Carbon\Carbon; use FireflyIII\Exception\FireflyException; /** @@ -12,14 +13,14 @@ use FireflyIII\Exception\FireflyException; class Date { /** - * @param \Carbon\Carbon $theDate + * @param Carbon $theDate * @param $repeatFreq * @param $skip * * @return \Carbon\Carbon * @throws FireflyException */ - public function addPeriod(\Carbon\Carbon $theDate, $repeatFreq, $skip) + public function addPeriod(Carbon $theDate, $repeatFreq, $skip) { $date = clone $theDate; $add = ($skip + 1); @@ -58,13 +59,13 @@ class Date } /** - * @param \Carbon\Carbon $theCurrentEnd + * @param Carbon $theCurrentEnd * @param $repeatFreq * - * @return \Carbon\Carbon + * @return Carbon * @throws FireflyException */ - public function endOfPeriod(\Carbon\Carbon $theCurrentEnd, $repeatFreq) + public function endOfPeriod(Carbon $theCurrentEnd, $repeatFreq) { $currentEnd = clone $theCurrentEnd; switch ($repeatFreq) { @@ -99,14 +100,14 @@ class Date } /** - * @param \Carbon\Carbon $theCurrentEnd + * @param Carbon $theCurrentEnd * @param $repeatFreq - * @param \Carbon\Carbon $maxDate + * @param Carbon $maxDate * - * @return \Carbon\Carbon + * @return Carbon * @throws FireflyException */ - public function endOfX(\Carbon\Carbon $theCurrentEnd, $repeatFreq, \Carbon\Carbon $maxDate) + public function endOfX(Carbon $theCurrentEnd, $repeatFreq, Carbon $maxDate) { $currentEnd = clone $theCurrentEnd; switch ($repeatFreq) { @@ -148,13 +149,13 @@ class Date } /** - * @param \Carbon\Carbon $date + * @param Carbon $date * @param $repeatFrequency * * @return string * @throws FireflyException */ - public function periodShow(\Carbon\Carbon $date, $repeatFrequency) + public function periodShow(Carbon $date, $repeatFrequency) { switch ($repeatFrequency) { default: @@ -182,13 +183,13 @@ class Date } /** - * @param \Carbon\Carbon $theDate + * @param Carbon $theDate * @param $repeatFreq * - * @return \Carbon\Carbon + * @return Carbon * @throws FireflyException */ - public function startOfPeriod(\Carbon\Carbon $theDate, $repeatFreq) + public function startOfPeriod(Carbon $theDate, $repeatFreq) { $date = clone $theDate; switch ($repeatFreq) { @@ -227,14 +228,14 @@ class Date } /** - * @param \Carbon\Carbon $theDate + * @param Carbon $theDate * @param $repeatFreq * @param int $subtract * - * @return \Carbon\Carbon + * @return Carbon * @throws FireflyException */ - public function subtractPeriod(\Carbon\Carbon $theDate, $repeatFreq, $subtract = 1) + public function subtractPeriod(Carbon $theDate, $repeatFreq, $subtract = 1) { $date = clone $theDate; switch ($repeatFreq) { diff --git a/app/lib/FireflyIII/Shared/Toolkit/Form.php b/app/lib/FireflyIII/Shared/Toolkit/Form.php index 42d6270009..65310b1be4 100644 --- a/app/lib/FireflyIII/Shared/Toolkit/Form.php +++ b/app/lib/FireflyIII/Shared/Toolkit/Form.php @@ -1,6 +1,7 @@ 'required|exists:transaction_types,id', 'transaction_currency_id' => 'required|exists:transaction_currencies,id', 'description' => 'required|between:1,255', diff --git a/tests/functional/TransactionControllerCest.php b/tests/functional/TransactionControllerCest.php index ab85e29be6..dcaf3727fd 100644 --- a/tests/functional/TransactionControllerCest.php +++ b/tests/functional/TransactionControllerCest.php @@ -155,7 +155,7 @@ class TransactionControllerCest 'post_submit_action' => 'store' ] ); - $I->see('Could not store transaction: This field is mandatory.'); + $I->see('Could not store transaction: The description field is required.'); } public function update(FunctionalTester $I) @@ -195,7 +195,7 @@ class TransactionControllerCest 'post_submit_action' => 'update' ] ); - $I->see('Could not update transaction: This field is mandatory.'); + $I->see('Could not update transaction: The description field is required.'); } public function updateAndReturn(FunctionalTester $I) diff --git a/tests/functional/UserControllerCest.php b/tests/functional/UserControllerCest.php index 7ee760c943..d04dfcccd1 100644 --- a/tests/functional/UserControllerCest.php +++ b/tests/functional/UserControllerCest.php @@ -134,7 +134,7 @@ class UserControllerCest { $I->wantTo('reset my password and fail'); $I->amOnPage('/reset/123'); - $I->see('Yo no hablo reset code!'); + $I->see('No reset code found!'); } /**