diff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.php index ba2035f3ad..3a8ccf0fe4 100644 --- a/app/controllers/TransactionController.php +++ b/app/controllers/TransactionController.php @@ -1,9 +1,10 @@ _repository = $repository; + $this->_helper = $helper; View::share('title', 'Transactions'); View::share('mainTitleIcon', 'fa-repeat'); } @@ -37,19 +47,17 @@ class TransactionController extends BaseController $ids = []; /** @var TransactionGroup $group */ foreach ($journal->transactiongroups()->get() as $group) { - /** @var TransactionJournal $jrnl */ - foreach ($group->transactionjournals()->get() as $jrnl) { - if ($jrnl->id != $journal->id) { - $ids[] = $jrnl->id; + /** @var TransactionJournal $loopJournal */ + foreach ($group->transactionjournals()->get() as $loopJournal) { + if ($loopJournal->id != $journal->id) { + $ids[] = $loopJournal->id; } } } $unique = array_unique($ids); if (count($unique) > 0) { - /** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repository */ - $repository = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal'); - $set = $repository->getByIds($unique); + $set = $this->_repository->getByIds($unique); $set->each( function (TransactionJournal $journal) { $journal->amount = mf($journal->getAmount()); @@ -71,33 +79,18 @@ class TransactionController extends BaseController */ public function create($what = 'deposit') { - /** @var \FireflyIII\Database\Account\Account $accountRepository */ - $accountRepository = App::make('FireflyIII\Database\Account\Account'); + $accounts = FFForm::makeSelectList($this->_helper->getAssetAccounts()); + $budgets = FFForm::makeSelectList($this->_helper->getBudgets()); + $budgets[0] = '(no budget)'; + $piggyBanks = $this->_helper->getPiggyBanks(); + $repeatedExpenses = $this->_helper->getRepeatedExpenses(); + $list = $piggyBanks->merge($repeatedExpenses); + $piggies = FFForm::makeSelectList($list); + $piggies[0] = '(no piggy bank)'; + $preFilled = Session::has('preFilled') ? Session::get('preFilled') : []; + $respondTo = ['account_id', 'account_from_id']; + $subTitle = 'Add a new ' . $what; - /** @var \FireflyIII\Database\Budget\Budget $budgetRepository */ - $budgetRepository = App::make('FireflyIII\Database\Budget\Budget'); - - /** @var \FireflyIII\Database\PiggyBank\PiggyBank $piggyRepository */ - $piggyRepository = App::make('FireflyIII\Database\PiggyBank\PiggyBank'); - - /** @var \FireflyIII\Database\PiggyBank\RepeatedExpense $repRepository */ - $repRepository = App::make('FireflyIII\Database\PiggyBank\RepeatedExpense'); - - // get asset accounts with names and id's . - $assetAccounts = FFForm::makeSelectList($accountRepository->getAssetAccounts()); - - // get budgets as a select list. - $budgets = FFForm::makeSelectList($budgetRepository->get()); - $budgets[0] = '(no budget)'; - - // get the piggy banks. - $list = $piggyRepository->get()->merge($repRepository->get()); - $piggies = FFForm::makeSelectList($list); - $piggies[0] = '(no piggy bank)'; - asort($piggies); - - $preFilled = Session::has('preFilled') ? Session::get('preFilled') : []; - $respondTo = ['account_id', 'account_from_id']; foreach ($respondTo as $r) { if (!is_null(Input::get($r))) { $preFilled[$r] = Input::get($r); @@ -105,24 +98,25 @@ class TransactionController extends BaseController } Session::put('preFilled', $preFilled); - return View::make('transactions.create')->with('accounts', $assetAccounts)->with('budgets', $budgets)->with('what', $what)->with('piggies', $piggies) - ->with('subTitle', 'Add a new ' . $what); + asort($piggies); + + + return View::make('transactions.create', compact('accounts', 'budgets', 'what', 'piggies', 'subTitle')); } /** * Shows the form that allows a user to delete a transaction journal. * - * @param TransactionJournal $transactionJournal + * @param TransactionJournal $journal * * @return $this */ - public function delete(TransactionJournal $transactionJournal) + public function delete(TransactionJournal $journal) { - $type = strtolower($transactionJournal->transactionType->type); + $type = strtolower($journal->transactionType->type); + $subTitle = 'Delete ' . $type . ' "' . $journal->description . '"'; - return View::make('transactions.delete')->with('journal', $transactionJournal)->with( - 'subTitle', 'Delete ' . $type . ' "' . $transactionJournal->description . '"' - ); + return View::make('transactions.delete', compact('journal', 'subTitle')); } @@ -134,13 +128,9 @@ class TransactionController extends BaseController */ public function destroy(TransactionJournal $transactionJournal) { - $type = $transactionJournal->transactionType->type; - - /** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repository */ - $repository = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal'); - $repository->destroy($transactionJournal); - + $type = $transactionJournal->transactionType->type; $return = 'withdrawal'; + $this->_repository->destroy($transactionJournal); switch ($type) { @@ -165,16 +155,13 @@ class TransactionController extends BaseController $id = intval(Input::get('id')); $sister = intval(Input::get('relateTo')); - /** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repository */ - $repository = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal'); - - $journal = $repository->find($id); - $sis = $repository->find($sister); + $journal = $this->_repository->find($id); + $sis = $this->_repository->find($sister); if ($journal && $sis) { $group = new TransactionGroup; $group->relation = 'balance'; - $group->user_id = $repository->getUser()->id; + $group->user_id = $this->_repository->getUser()->id; $group->save(); $group->transactionjournals()->save($journal); $group->transactionjournals()->save($sis); @@ -196,114 +183,42 @@ class TransactionController extends BaseController */ public function edit(TransactionJournal $journal) { - /* - * All the repositories we need: - */ - - /** @var \FireflyIII\Database\Account\Account $accountRepository */ - $accountRepository = App::make('FireflyIII\Database\Account\Account'); - - /** @var \FireflyIII\Database\Budget\Budget $budgetRepository */ - $budgetRepository = App::make('FireflyIII\Database\Budget\Budget'); - - /** @var \FireflyIII\Database\PiggyBank\PiggyBank $piggyRepository */ - $piggyRepository = App::make('FireflyIII\Database\PiggyBank\PiggyBank'); - - - // type is useful for display: - $what = strtolower($journal->transactiontype->type); - - // get asset accounts with names and id's. - - $budgets = FFForm::makeSelectList($budgetRepository->get(), true); - $accounts = FFForm::makeSelectList($accountRepository->getAssetAccounts()); - $piggies = FFForm::makeSelectList($piggyRepository->get(), true); - - - /* - * Data to properly display the edit form. - */ - $preFilled = [ - 'date' => $journal->date->format('Y-m-d'), - 'category' => '', - 'budget_id' => 0, + $what = strtolower($journal->transactiontype->type); + $subTitle = 'Edit ' . $what . ' "' . $journal->description . '"'; + $budgets = FFForm::makeSelectList($this->_helper->getBudgets(), true); + $accounts = FFForm::makeSelectList($this->_helper->getAssetAccounts()); + $piggies = FFForm::makeSelectList($this->_helper->getPiggyBanks(), true); + $transactions = $journal->transactions()->orderBy('amount', 'DESC')->get(); + $preFilled = [ + 'date' => $journal->date->format('Y-m-d'), + 'category' => '', + 'budget_id' => 0, 'piggy_bank_id' => 0 ]; - /* - * Fill in the category. - */ $category = $journal->categories()->first(); if (!is_null($category)) { $preFilled['category'] = $category->name; } - /* - * Switch on the type of transaction edited by the user and fill in other - * relevant fields: - */ - switch ($what) { - case 'withdrawal': - if (floatval($journal->transactions[0]->amount) < 0) { - // transactions[0] is the asset account that paid for the withdrawal. - $preFilled['account_id'] = $journal->transactions[0]->account->id; - $preFilled['expense_account'] = $journal->transactions[1]->account->name; - $preFilled['amount'] = floatval($journal->transactions[1]->amount); - } else { - // transactions[1] is the asset account that paid for the withdrawal. - $preFilled['account_id'] = $journal->transactions[1]->account->id; - $preFilled['expense_account'] = $journal->transactions[0]->account->name; - $preFilled['amount'] = floatval($journal->transactions[0]->amount); - } - - - $budget = $journal->budgets()->first(); - if (!is_null($budget)) { - $preFilled['budget_id'] = $budget->id; - } - break; - case 'deposit': - if (floatval($journal->transactions[0]->amount) < 0) { - // transactions[0] contains the account the money came from. - $preFilled['account_id'] = $journal->transactions[1]->account->id; - $preFilled['revenue_account'] = $journal->transactions[0]->account->name; - $preFilled['amount'] = floatval($journal->transactions[1]->amount); - } else { - // transactions[1] contains the account the money came from. - $preFilled['account_id'] = $journal->transactions[0]->account->id; - $preFilled['revenue_account'] = $journal->transactions[1]->account->name; - $preFilled['amount'] = floatval($journal->transactions[0]->amount); - - } - - break; - case 'transfer': - if (floatval($journal->transactions[0]->amount) < 0) { - // zero = from account. - $preFilled['account_from_id'] = $journal->transactions[0]->account->id; - $preFilled['account_to_id'] = $journal->transactions[1]->account->id; - $preFilled['amount'] = floatval($journal->transactions[1]->amount); - } else { - // one = from account - $preFilled['account_from_id'] = $journal->transactions[1]->account->id; - $preFilled['account_to_id'] = $journal->transactions[0]->account->id; - $preFilled['amount'] = floatval($journal->transactions[0]->amount); - } - if ($journal->piggyBankEvents()->count() > 0) { - $preFilled['piggy_bank_id'] = $journal->piggyBankEvents()->first()->piggy_bank_id; - } - break; + $budget = $journal->budgets()->first(); + if (!is_null($budget)) { + $preFilled['budget_id'] = $budget->id; } - /* - * Show the view. - */ + if ($journal->piggyBankEvents()->count() > 0) { + $preFilled['piggy_bank_id'] = $journal->piggyBankEvents()->first()->piggy_bank_id; + } - return View::make('transactions.edit')->with('journal', $journal)->with('accounts', $accounts)->with( - 'what', $what - )->with('budgets', $budgets)->with('data', $preFilled)->with('piggies', $piggies)->with( - 'subTitle', 'Edit ' . $what . ' "' . $journal->description . '"' - ); + $preFilled['amount'] = $journal->getAmount(); + $preFilled['account_id'] = $this->_helper->getAssetAccount($what, $transactions); + $preFilled['expense_account'] = $transactions[0]->account->name; + $preFilled['revenue_account'] = $transactions[1]->account->name; + $preFilled['account_from_id'] = $transactions[1]->account->id; + $preFilled['account_to_id'] = $transactions[0]->account->id; + + + return View::make('transactions.edit', compact('journal', 'accounts', 'what', 'budgets', 'piggies', 'subTitle'))->with('data', $preFilled); } /** @@ -313,28 +228,24 @@ class TransactionController extends BaseController */ public function index($what) { - - /** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repository */ - $repository = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal'); - switch ($what) { case 'expenses': case 'withdrawal': $subTitleIcon = 'fa-long-arrow-left'; $subTitle = 'Expenses'; - $journals = $repository->getWithdrawalsPaginated(50); + $journals = $this->_repository->getWithdrawalsPaginated(50); break; case 'revenue': case 'deposit': $subTitleIcon = 'fa-long-arrow-right'; $subTitle = 'Revenue, income and deposits'; - $journals = $repository->getDepositsPaginated(50); + $journals = $this->_repository->getDepositsPaginated(50); break; case 'transfer': case 'transfers': $subTitleIcon = 'fa-arrows-h'; $subTitle = 'Transfers'; - $journals = $repository->getTransfersPaginated(50); + $journals = $this->_repository->getTransfersPaginated(50); break; } @@ -343,6 +254,8 @@ class TransactionController extends BaseController } /** + * TODO refactor relate stuff into another controller. + * * @param TransactionJournal $journal * * @return \Illuminate\View\View @@ -353,10 +266,10 @@ class TransactionController extends BaseController $members = new Collection; /** @var TransactionGroup $group */ foreach ($groups as $group) { - /** @var TransactionJournal $jrnl */ - foreach ($group->transactionjournals()->get() as $jrnl) { - if ($jrnl->id != $journal->id) { - $members->push($jrnl); + /** @var TransactionJournal $loopJournal */ + foreach ($group->transactionjournals()->get() as $loopJournal) { + if ($loopJournal->id != $journal->id) { + $members->push($loopJournal); } } } @@ -375,10 +288,7 @@ class TransactionController extends BaseController { $search = e(trim(Input::get('searchValue'))); - /** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repository */ - $repository = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal'); - - $result = $repository->searchRelated($search, $journal); + $result = $this->_repository->searchRelated($search, $journal); $result->each( function (TransactionJournal $j) { $j->amount = mf($j->getAmount()); @@ -410,10 +320,10 @@ class TransactionController extends BaseController $members = new Collection; /** @var TransactionGroup $group */ foreach ($journal->transactiongroups()->get() as $group) { - /** @var TransactionJournal $jrnl */ - foreach ($group->transactionjournals()->get() as $jrnl) { - if ($jrnl->id != $journal->id) { - $members->push($jrnl); + /** @var TransactionJournal $loopJournal */ + foreach ($group->transactionjournals()->get() as $loopJournal) { + if ($loopJournal->id != $journal->id) { + $members->push($loopJournal); } } } @@ -433,58 +343,41 @@ class TransactionController extends BaseController { $data = Input::except('_token'); $data['what'] = $what; - $data['currency'] = 'EUR'; + $data['currency'] = 'EUR'; // TODO allow custom currency - /** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repository */ - $repository = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal'); + // always validate: + $messages = $this->_repository->validate($data); - switch ($data['post_submit_action']) { - default: - throw new FireflyException('Cannot handle post_submit_action "' . e($data['post_submit_action']) . '"'); - break; - case 'create_another': - case 'store': - $messages = $repository->validate($data); - /** @var MessageBag $messages ['errors'] */ - if ($messages['errors']->count() > 0) { - Session::flash('warnings', $messages['warnings']); - Session::flash('successes', $messages['successes']); - Session::flash('error', 'Could not save transaction: ' . $messages['errors']->first()); - - return Redirect::route('transactions.create', $what)->withInput()->withErrors($messages['errors']); - } - // store! - $journal = $repository->store($data); - Session::flash('success', 'New transaction stored!'); - - /* - * Trigger a search for the related (if selected) - * piggy bank and store an event. - */ - Event::fire('transactionJournal.store', [$journal, Input::get('piggy_bank_id')]); // new and used. - /* - * Also trigger on both transactions. - */ - /** @var Transaction $transaction */ - foreach ($journal->transactions as $transaction) { - Event::fire('transaction.store', [$transaction]); - } - - if ($data['post_submit_action'] == 'create_another') { - return Redirect::route('transactions.create', $what)->withInput(); - } else { - return Redirect::route('transactions.index', $what); - } - break; - case 'validate_only': - $messageBags = $repository->validate($data); - Session::flash('warnings', $messageBags['warnings']); - Session::flash('successes', $messageBags['successes']); - Session::flash('errors', $messageBags['errors']); - - return Redirect::route('transactions.create', $what)->withInput(); - break; + // flash messages: + Session::flash('warnings', $messages['warnings']); + Session::flash('successes', $messages['successes']); + Session::flash('errors', $messages['errors']); + if ($messages['errors']->count() > 0) { + Session::flash('error', 'Could not store transaction: ' . $messages['errors']->first()); } + + // return to create screen: + if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) { + return Redirect::route('transactions.create', $data['what'])->withInput(); + } + + // store: + $journal = $this->_repository->store($data); + Event::fire('transactionJournal.store', [$journal, Input::get('piggy_bank_id')]); // new and used. + /* + * Also trigger on both transactions. + */ + /** @var Transaction $transaction */ + foreach ($journal->transactions as $transaction) { + Event::fire('transaction.store', [$transaction]); + } + + Session::flash('success', 'Transaction "' . e($data['description']) . '" stored.'); + if ($data['post_submit_action'] == 'store') { + return Redirect::route('transactions.index', $data['what']); + } + + return Redirect::route('transactions.create', $data['what'])->withInput(); } /** @@ -501,8 +394,8 @@ class TransactionController extends BaseController $relatedTo = intval(Input::get('relation')); /** @var TransactionGroup $group */ foreach ($groups as $group) { - foreach ($group->transactionjournals()->get() as $jrnl) { - if ($jrnl->id == $relatedTo) { + foreach ($group->transactionjournals()->get() as $loopJournal) { + if ($loopJournal->id == $relatedTo) { // remove from group: $group->transactionjournals()->detach($relatedTo); } @@ -524,60 +417,50 @@ class TransactionController extends BaseController */ public function update(TransactionJournal $journal) { - /** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repos */ - $repos = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal'); - $data = Input::except('_token'); $data['currency'] = 'EUR'; $data['what'] = strtolower($journal->transactionType->type); + // always validate: + $messages = $this->_repository->validate($data); - switch (Input::get('post_submit_action')) { - case 'update': - case 'return_to_edit': - $messageBag = $repos->update($journal, $data); - if ($messageBag->count() == 0) { - // has been saved, return to index: - Session::flash('success', 'Transaction updated!'); - Event::fire('transactionJournal.update', [$journal]); // new and used. - - /* - * Also trigger on both transactions. - */ - /** @var Transaction $transaction */ - foreach ($journal->transactions as $transaction) { - Event::fire('transaction.update', [$transaction]); - } - - if (Input::get('post_submit_action') == 'return_to_edit') { - return Redirect::route('transactions.edit', $journal->id)->withInput(); - } else { - return Redirect::route('transactions.index', $data['what']); - } - } else { - Session::flash('error', 'Could not update transaction: ' . $journal->getErrors()->first()); - - return Redirect::route('transactions.edit', $journal->id)->withInput()->withErrors( - $journal->getErrors() - ); - } - - break; - case 'validate_only': - $messageBags = $repos->validate($data); - - Session::flash('warnings', $messageBags['warnings']); - Session::flash('successes', $messageBags['successes']); - Session::flash('errors', $messageBags['errors']); - - return Redirect::route('transactions.edit', $journal->id)->withInput(); - break; - default: - throw new FireflyException('Method ' . Input::get('post_submit_action') . ' not implemented yet.'); - break; + // flash messages: + Session::flash('warnings', $messages['warnings']); + Session::flash('successes', $messages['successes']); + Session::flash('errors', $messages['errors']); + if ($messages['errors']->count() > 0) { + Session::flash('error', 'Could not update transaction: ' . $messages['errors']->first()); } + // return to update screen: + if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) { + return Redirect::route('transactions.edit', $journal->id)->withInput(); + } + + // update + $this->_repository->update($journal, $data); + Session::flash('success', 'Transaction "' . e($data['description']) . '" updated.'); + + Event::fire('transactionJournal.update', [$journal]); // new and used. + + /* + * Also trigger on both transactions. + */ + /** @var Transaction $transaction */ + foreach ($journal->transactions()->get() as $transaction) { + Event::fire('transaction.update', [$transaction]); + } + + // go back to list + if ($data['post_submit_action'] == 'update') { + return Redirect::route('transactions.index', $data['what']); + } + + // go back to update screen. + return Redirect::route('transactions.edit', $journal->id)->withInput(['post_submit_action' => 'return_to_edit']); + + } } \ No newline at end of file diff --git a/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php b/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php index 1c160059e2..8a6641d93f 100644 --- a/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php +++ b/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php @@ -128,7 +128,7 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData $transaction->save(); } - return new MessageBag; + return true; } /** diff --git a/app/lib/FireflyIII/FF3ServiceProvider.php b/app/lib/FireflyIII/FF3ServiceProvider.php index cf85ed6380..37daab3d37 100644 --- a/app/lib/FireflyIII/FF3ServiceProvider.php +++ b/app/lib/FireflyIII/FF3ServiceProvider.php @@ -98,6 +98,7 @@ class FF3ServiceProvider extends ServiceProvider $this->app->bind('FireflyIII\Report\ReportInterface', 'FireflyIII\Report\Report'); $this->app->bind('FireflyIII\Report\ReportQueryInterface', 'FireflyIII\Report\ReportQuery'); $this->app->bind('FireflyIII\Report\ReportHelperInterface', 'FireflyIII\Report\ReportHelper'); + $this->app->bind('FireflyIII\Helper\TransactionJournal\HelperInterface', 'FireflyIII\Helper\TransactionJournal\Helper'); // chart $this->app->bind('FireflyIII\Chart\ChartInterface', 'FireflyIII\Chart\Chart'); diff --git a/app/lib/FireflyIII/Helper/TransactionJournal/Helper.php b/app/lib/FireflyIII/Helper/TransactionJournal/Helper.php new file mode 100644 index 0000000000..8b909c7493 --- /dev/null +++ b/app/lib/FireflyIII/Helper/TransactionJournal/Helper.php @@ -0,0 +1,83 @@ +account->id); + } + + // otherwise (its a deposit), it's been paid into account #0. + return intval($transactions[0]->account->id); + } + + /** + * @return Collection + */ + public function getAssetAccounts() + { + /** @var \FireflyIII\Database\Account\Account $accountRepository */ + $accountRepository = \App::make('FireflyIII\Database\Account\Account'); + + return $accountRepository->getAssetAccounts(); + } + + /** + * @return Collection + */ + public function getBudgets() + { + /** @var \FireflyIII\Database\Budget\Budget $budgetRepository */ + $budgetRepository = \App::make('FireflyIII\Database\Budget\Budget'); + + return $budgetRepository->get(); + + } + + /** + * @return Collection + */ + public function getPiggyBanks() + { + /** @var \FireflyIII\Database\PiggyBank\PiggyBank $piggyRepository */ + $piggyRepository = \App::make('FireflyIII\Database\PiggyBank\PiggyBank'); + + return $piggyRepository->get(); + + + } + + /** + * @return Collection + */ + public function getRepeatedExpenses() + { + /** @var \FireflyIII\Database\PiggyBank\RepeatedExpense $repRepository */ + $repRepository = \App::make('FireflyIII\Database\PiggyBank\RepeatedExpense'); + + return $repRepository->get(); + + + } + +} \ No newline at end of file diff --git a/app/lib/FireflyIII/Helper/TransactionJournal/HelperInterface.php b/app/lib/FireflyIII/Helper/TransactionJournal/HelperInterface.php new file mode 100644 index 0000000000..dc5703dc22 --- /dev/null +++ b/app/lib/FireflyIII/Helper/TransactionJournal/HelperInterface.php @@ -0,0 +1,45 @@ +