From d088b2c55821928b92fbb45d0d12392dc8561855 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 26 Jul 2014 08:05:02 +0200 Subject: [PATCH] Some more work done. Mainly accounts. [skip ci] --- app/controllers/AccountController.php | 129 ++++++----- app/controllers/ChartController.php | 7 +- app/controllers/TransactionController.php | 119 +++++++++- app/lib/Firefly/Helper/Form/FormHelper.php | 41 ++++ app/lib/Firefly/Helper/Form/FormTrigger.php | 37 +++ .../Account/AccountRepositoryInterface.php | 26 ++- .../Account/EloquentAccountRepository.php | 219 +++++++++++------- .../Category/EloquentCategoryRepository.php | 3 + .../EloquentTransactionJournalRepository.php | 4 +- app/routes.php | 6 +- app/views/accounts/create.blade.php | 137 +++++++---- app/views/accounts/delete.blade.php | 46 ++++ app/views/accounts/edit.blade.php | 94 ++++++++ app/views/accounts/index.blade.php | 6 +- app/views/accounts/list.blade.php | 7 + app/views/accounts/show.blade.php | 29 +++ app/views/budgets/index.blade.php | 29 --- app/views/index.blade.php | 20 +- app/views/partials/date_nav.blade.php | 9 +- app/views/transactions/create.blade.php | 1 + app/views/transactions/edit.blade.php | 110 +++++++-- app/views/transactions/index.blade.php | 10 +- bootstrap/start.php | 11 + public/assets/javascript/accounts.js | 95 ++++++++ public/assets/javascript/index.js | 19 +- 25 files changed, 932 insertions(+), 282 deletions(-) create mode 100644 app/lib/Firefly/Helper/Form/FormHelper.php create mode 100644 app/lib/Firefly/Helper/Form/FormTrigger.php create mode 100644 app/views/accounts/delete.blade.php create mode 100644 app/views/accounts/edit.blade.php create mode 100644 app/views/accounts/show.blade.php delete mode 100644 app/views/budgets/index.blade.php create mode 100644 public/assets/javascript/accounts.js diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php index c49c154736..caddcc6da0 100644 --- a/app/controllers/AccountController.php +++ b/app/controllers/AccountController.php @@ -8,12 +8,14 @@ use Firefly\Storage\Account\AccountRepositoryInterface as ARI; class AccountController extends \BaseController { + protected $_accounts; + /** * @param ARI $accounts */ public function __construct(ARI $accounts) { - $this->accounts = $accounts; + $this->_accounts = $accounts; View::share('menu', 'accounts'); } @@ -25,7 +27,7 @@ class AccountController extends \BaseController */ public function index() { - $all = $this->accounts->get(); + $all = $this->_accounts->get(); $list = [ @@ -68,23 +70,38 @@ class AccountController extends \BaseController { return View::make('accounts.create'); } -// -// -// /** -// * Store a newly created resource in storage. -// * -// * @return Response -// */ -// public function store() -// { -// $account = $this->accounts->store(); -// if($account === false) { -// Session::flash('error','Could not create account with provided information'); -// return Redirect::route('accounts.create')->withInput()->withErrors($this->accounts->validator); -// } -// } -// -// + + public function store() + { + + $account = $this->_accounts->store(Input::all()); + + if (!$account->id) { + // did not save, return with error: + Session::flash('error', 'Could not save the new account. Please check the form.'); + return View::make('accounts.create')->withErrors($account->errors()); + } else { + // saved! return to wherever. + Session::flash('success', 'Account "' . $account->name . '" created!'); + if (Input::get('create') == '1') { + return Redirect::route('accounts.create')->withInput(); + } else { + return Redirect::route('accounts.index'); + } + } + } + + public function edit($accountId) + { + $account = $this->_accounts->find($accountId); + + if ($account) { + // find the initial balance transaction, if any: + $openingBalance = $this->_accounts->findOpeningBalanceTransaction($account); + return View::make('accounts.edit')->with('account', $account)->with('openingBalance',$openingBalance); + } + } + /** * Display the specified resource. * @@ -94,44 +111,44 @@ class AccountController extends \BaseController */ public function show($accountId) { - return $accountId; + $account = $this->_accounts->find($accountId); + return View::make('accounts.show')->with('account',$account); } -// -// -// /** -// * Show the form for editing the specified resource. -// * -// * @param int $id -// * @return Response -// */ -// public function edit($id) -// { -// // -// } -// -// -// /** -// * Update the specified resource in storage. -// * -// * @param int $id -// * @return Response -// */ -// public function update($id) -// { -// // -// } -// -// -// /** -// * Remove the specified resource from storage. -// * -// * @param int $id -// * @return Response -// */ -// public function destroy($id) -// { -// // -// } + + /** + * Update the specified resource in storage. + * + * @return Response + */ + public function update() + { + $account = $this->_accounts->update(Input::all()); + Session::flash('success','Account "'.$account->name.'" updated.'); + return Redirect::route('accounts.index'); + } + + public function delete($accountId) { + $account = $this->_accounts->find($accountId); + if($account) { + return View::make('accounts.delete')->with('account',$account); + } + } + + /** + * @param $accountId + */ + public function destroy() + { + $result = $this->_accounts->destroy(Input::get('id')); + if($result === true) { + Session::flash('success','The account was deleted.'); + return Redirect::route('accounts.index'); + } else { + Session::flash('danger','Could not delete the account. Check the logs to be sure.'); + return Redirect::route('accounts.index'); + } + + } } diff --git a/app/controllers/ChartController.php b/app/controllers/ChartController.php index 42d8a33f39..8aebbad9b0 100644 --- a/app/controllers/ChartController.php +++ b/app/controllers/ChartController.php @@ -208,8 +208,11 @@ class ChartController extends BaseController foreach ($budget->limits as $limit) { foreach ($limit->limitrepetitions as $rep) { //0: envelope for period: - $data['series'][0]['data'][] = floatval($rep->amount); - $data['series'][1]['data'][] = $rep->spent; + $amount = floatval($rep->amount); + $spent = $rep->spent; + $color = $spent > $amount ? '#FF0000' : null; + $data['series'][0]['data'][] = $amount; + $data['series'][1]['data'][] = ['y' => $rep->spent, 'color' => $color]; } } diff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.php index 33ed5e1e32..9934858679 100644 --- a/app/controllers/TransactionController.php +++ b/app/controllers/TransactionController.php @@ -112,7 +112,7 @@ class TransactionController extends BaseController $journal->categories()->save($category); } - Session::flash('success', 'Transaction saved'); + Session::flash('success', 'Transaction "' . $description . '" saved'); if (Input::get('create') == '1') { return Redirect::route('transactions.create', $what)->withInput(); @@ -153,12 +153,127 @@ class TransactionController extends BaseController */ public function edit($journalId) { + // get journal: $journal = $this->_journal->find($journalId); + if ($journal) { + // type is useful for display: + $what = strtolower($journal->transactiontype->type); + + // some lists prefilled: + $budgets = $this->_budgets->getAsSelectList(); + $budgets[0] = '(no budget)'; $accounts = $this->_accounts->getActiveDefaultAsSelectList(); - return View::make('transactions.edit')->with('journal', $journal)->with('accounts', $accounts); + + // data to properly display form: + $data = [ + 'date' => $journal->date->format('Y-m-d'), + 'category' => '', + 'budget_id' => 0 + ]; + $category = $journal->categories()->first(); + if (!is_null($category)) { + $data['category'] = $category->name; + } + switch ($journal->transactiontype->type) { + case 'Withdrawal': + $data['account_id'] = $journal->transactions[0]->account->id; + $data['beneficiary'] = $journal->transactions[1]->account->name; + $data['amount'] = floatval($journal->transactions[1]->amount); + $budget = $journal->budgets()->first(); + if (!is_null($budget)) { + $data['budget_id'] = $budget->id; + } + break; + case 'Deposit': + $data['account_id'] = $journal->transactions[1]->account->id; + $data['beneficiary'] = $journal->transactions[0]->account->name; + $data['amount'] = floatval($journal->transactions[1]->amount); + break; + case 'Transfer': + $data['account_from_id'] = $journal->transactions[1]->account->id; + $data['account_to_id'] = $journal->transactions[0]->account->id; + $data['amount'] = floatval($journal->transactions[1]->amount); + break; + } + return View::make('transactions.edit')->with('journal', $journal)->with('accounts', $accounts)->with( + 'what', $what + )->with('budgets', $budgets)->with('data', $data); } return View::make('error')->with('message', 'Invalid journal'); } + public function update($journalId) + { + + // get journal: + $journal = $this->_journal->find($journalId); + + if ($journal) { + // update basics first: + $journal->description = Input::get('description'); + $journal->date = Input::get('date'); + $amount = floatval(Input::get('amount')); + + // remove previous category, if any: + if (!is_null($journal->categories()->first())) { + $journal->categories()->detach($journal->categories()->first()->id); + } + // remove previous budget, if any: + if (!is_null($journal->budgets()->first())) { + $journal->budgets()->detach($journal->budgets()->first()->id); + } + + // get the category: + $category = $this->_categories->findByName(Input::get('category')); + if (!is_null($category)) { + $journal->categories()->attach($category); + } + // update the amounts: + $journal->transactions[0]->amount = $amount * -1; + $journal->transactions[1]->amount = $amount; + + // switch on type to properly change things: + switch ($journal->transactiontype->type) { + case 'Withdrawal': + // means transaction[0] is the users account. + $account = $this->_accounts->find(Input::get('account_id')); + $beneficiary = $this->_accounts->findByName(Input::get('beneficiary')); + $journal->transactions[0]->account()->associate($account); + $journal->transactions[1]->account()->associate($beneficiary); + + + // do budget: + $budget = $this->_budgets->find(Input::get('budget_id')); + $journal->budgets()->attach($budget); + + break; + case 'Deposit': + // means transaction[0] is the beneficiary. + $account = $this->_accounts->find(Input::get('account_id')); + $beneficiary = $this->_accounts->findByName(Input::get('beneficiary')); + $journal->transactions[0]->account()->associate($beneficiary); + $journal->transactions[1]->account()->associate($account); + break; + case 'Transfer': + // means transaction[0] is account that sent the money (from). + $fromAccount = $this->_accounts->find(Input::get('account_from_id')); + $toAccount = $this->_accounts->find(Input::get('account_to_id')); + $journal->transactions[0]->account()->associate($fromAccount); + $journal->transactions[1]->account()->associate($toAccount); + break; + default: + throw new \Firefly\Exception\FireflyException('Cannot edit this!'); + break; + } + + $journal->transactions[0]->save(); + $journal->transactions[1]->save(); + $journal->save(); + return Redirect::route('transactions.edit', $journal->id); + } + + + } + } \ No newline at end of file diff --git a/app/lib/Firefly/Helper/Form/FormHelper.php b/app/lib/Firefly/Helper/Form/FormHelper.php new file mode 100644 index 0000000000..0618a3833d --- /dev/null +++ b/app/lib/Firefly/Helper/Form/FormHelper.php @@ -0,0 +1,41 @@ +'; + + $str .= '