From a1fb4bdb60151e28ffb9ed65e2466be4d432a601 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 15 Jul 2014 11:04:53 +0200 Subject: [PATCH] Firefly is now capable of adding a transaction. Untested [skip-ci] --- app/controllers/TransactionController.php | 62 +++++++++++++++++-- .../Account/AccountRepositoryInterface.php | 6 ++ .../Account/EloquentAccountRepository.php | 27 +++++++- .../Budget/BudgetRepositoryInterface.php | 5 +- .../Budget/EloquentBudgetRepository.php | 19 +++++- .../Category/CategoryRepositoryInterface.php | 9 ++- .../Category/EloquentCategoryRepository.php | 32 +++++++++- app/routes.php | 5 ++ app/views/transactions/withdrawal.blade.php | 24 ++++--- 9 files changed, 164 insertions(+), 25 deletions(-) diff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.php index f81868f868..1647ca25f8 100644 --- a/app/controllers/TransactionController.php +++ b/app/controllers/TransactionController.php @@ -2,25 +2,75 @@ use Firefly\Storage\Account\AccountRepositoryInterface as ARI; +use Firefly\Storage\Budget\BudgetRepositoryInterface as Bud; +use Firefly\Storage\Category\CategoryRepositoryInterface as Cat; +use Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface as TJRI; -class TransactionController extends BaseController { +class TransactionController extends BaseController +{ protected $accounts; + protected $budgets; + protected $categories; + protected $tj; - public function __construct(ARI $accounts) { + public function __construct(ARI $accounts, Bud $budgets, Cat $categories, TJRI $tj) + { $this->accounts = $accounts; + $this->budgets = $budgets; + $this->categories = $categories; + $this->tj = $tj; - View::share('menu','home'); + View::share('menu', 'home'); } - public function createWithdrawal() { + public function createWithdrawal() + { // get accounts with names and id's. - $accounts =$this->accounts->getActiveDefaultAsSelectList(); + $accounts = $this->accounts->getActiveDefaultAsSelectList(); + + $budgets = $this->budgets->getAsSelectList(); + + $budgets[0] = '(no budget)'; - return View::make('transactions.withdrawal')->with('accounts',$accounts); + return View::make('transactions.withdrawal')->with('accounts', $accounts)->with('budgets', $budgets); + } + + public function postCreateWithdrawal() + { + + // create or find beneficiary: + $beneficiary = $this->accounts->createOrFindBeneficiary(Input::get('beneficiary')); + + // create or find category: + $category = $this->categories->createOrFind(Input::get('category')); + + // find budget: + $budget = $this->budgets->find(intval(Input::get('budget_id'))); + + // find account: + $account = $this->accounts->find(intval(Input::get('account_id'))); + + // find amount & description: + $description = trim(Input::get('description')); + $amount = floatval(Input::get('amount')); + $date = new \Carbon\Carbon(Input::get('date')); + + // create journal + /** @var \TransactionJournal $journal */ + $journal = $this->tj->createSimpleJournal($account,$beneficiary,$description,$amount,$date); + + var_dump($journal); + + // attach bud/cat (?) + $journal->budgets()->save($budget); + $journal->categories()->save($category); + + Session::flash('success','Transaction saved'); + return Redirect::route('index'); } } \ No newline at end of file diff --git a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php index 0ba7e745c3..e4460525cd 100644 --- a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php +++ b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php @@ -15,6 +15,8 @@ interface AccountRepositoryInterface public function find($id); + public function findByName($name); + public function getByIds($ids); public function getDefault(); @@ -27,4 +29,8 @@ interface AccountRepositoryInterface public function storeWithInitialBalance($data, \Carbon\Carbon $date, $amount = 0); + public function createOrFindBeneficiary($name); + + public function createOrFind($name, \AccountType $type); + } \ No newline at end of file diff --git a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php index a9a1fa1a9e..c7f72161a6 100644 --- a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php +++ b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php @@ -16,7 +16,8 @@ class EloquentAccountRepository implements AccountRepositoryInterface return \Auth::user()->accounts()->with('accounttype')->get(); } - public function getBeneficiaries() { + public function getBeneficiaries() + { $list = \Auth::user()->accounts()->leftJoin( 'account_types', 'account_types.id', '=', 'accounts.account_type_id' ) @@ -126,4 +127,28 @@ class EloquentAccountRepository implements AccountRepositoryInterface return $account; } + public function createOrFindBeneficiary($name) + { + $type = \AccountType::where('description', 'Beneficiary account')->first(); + return $this->createOrFind($name, $type); + } + + public function createOrFind($name, \AccountType $type) + { + $beneficiary = $this->findByName($name); + if (!$beneficiary) { + $data = [ + 'name' => $name, + 'account_type' => $type + ]; + return $this->store($data); + } + return $beneficiary; + } + + public function findByName($name) + { + return \Auth::user()->accounts()->where('name', 'like', '%' . $name . '%')->first(); + } + } \ No newline at end of file diff --git a/app/lib/Firefly/Storage/Budget/BudgetRepositoryInterface.php b/app/lib/Firefly/Storage/Budget/BudgetRepositoryInterface.php index 6148ca4705..89577d8eed 100644 --- a/app/lib/Firefly/Storage/Budget/BudgetRepositoryInterface.php +++ b/app/lib/Firefly/Storage/Budget/BudgetRepositoryInterface.php @@ -3,6 +3,9 @@ namespace Firefly\Storage\Budget; -interface BudgetRepositoryInterface { +interface BudgetRepositoryInterface +{ + public function getAsSelectList(); + public function find($id); } \ No newline at end of file diff --git a/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php b/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php index 397c261b80..daafdddfb6 100644 --- a/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php +++ b/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php @@ -3,6 +3,23 @@ namespace Firefly\Storage\Budget; -class EloquentBudgetRepository implements BudgetRepositoryInterface { +class EloquentBudgetRepository implements BudgetRepositoryInterface +{ + + public function getAsSelectList() + { + $list = \Auth::user()->budgets()->get(); + $return = []; + foreach ($list as $entry) { + $return[intval($entry->id)] = $entry->name; + } + return $return; + } + + public function find($id) + { + + return \Auth::user()->budgets()->find($id); + } } \ No newline at end of file diff --git a/app/lib/Firefly/Storage/Category/CategoryRepositoryInterface.php b/app/lib/Firefly/Storage/Category/CategoryRepositoryInterface.php index 5b38755d76..b64a7531de 100644 --- a/app/lib/Firefly/Storage/Category/CategoryRepositoryInterface.php +++ b/app/lib/Firefly/Storage/Category/CategoryRepositoryInterface.php @@ -3,8 +3,15 @@ namespace Firefly\Storage\Category; -interface CategoryRepositoryInterface { +interface CategoryRepositoryInterface +{ public function get(); + public function createOrFind($name); + + public function findByName($name); + + public function store($name); + } \ No newline at end of file diff --git a/app/lib/Firefly/Storage/Category/EloquentCategoryRepository.php b/app/lib/Firefly/Storage/Category/EloquentCategoryRepository.php index 0dc3b22842..6d1950a46a 100644 --- a/app/lib/Firefly/Storage/Category/EloquentCategoryRepository.php +++ b/app/lib/Firefly/Storage/Category/EloquentCategoryRepository.php @@ -3,9 +3,37 @@ namespace Firefly\Storage\Category; -class EloquentCategoryRepository implements CategoryRepositoryInterface { - public function get() { +class EloquentCategoryRepository implements CategoryRepositoryInterface +{ + public function get() + { return \Auth::user()->categories()->get(); } + public function createOrFind($name) + { + $category = $this->findByName($name); + if (!$category) { + return $this->store($name); + } + return $category; + + + } + + public function findByName($name) + { + return \Auth::user()->categories()->where('name', 'LIKE', '%' . $name . '%')->first(); + + } + + public function store($name) + { + $category = new \Category(); + $category->name = $name; + $category->user()->associate(\Auth::user()); + $category->save(); + return $category; + } + } \ No newline at end of file diff --git a/app/routes.php b/app/routes.php index 1cba80ad8d..f3b7ed34cb 100644 --- a/app/routes.php +++ b/app/routes.php @@ -55,6 +55,11 @@ Route::group(['before' => 'csrf|auth'], function () { // account controller: Route::get('/accounts/store', ['uses' => 'AccountController@store', 'as' => 'accounts.store']); + // transaction controller: + Route::post('/transactions/add/withdrawal', ['uses' => 'TransactionController@postCreateWithdrawal']); + Route::post('/transactions/add/deposit', ['uses' => 'TransactionController@postCreateDeposit']); + Route::post('/transactions/add/transfer', ['uses' => 'TransactionController@postCreateTransfer']); + } ); diff --git a/app/views/transactions/withdrawal.blade.php b/app/views/transactions/withdrawal.blade.php index bd6a719b57..a20666c4e2 100644 --- a/app/views/transactions/withdrawal.blade.php +++ b/app/views/transactions/withdrawal.blade.php @@ -26,21 +26,21 @@

Mandatory fields

- +
- +
{{Form::select('account_id',$accounts,Input::old('account_id'),['class' => 'form-control'])}}
- +
This field will auto-complete your existing beneficiaries (if any), but you can type freely to create new ones. @@ -50,21 +50,21 @@
- +
- +
- +
- +
- +
@@ -75,17 +75,15 @@

Optional fields

- +
- + {{Form::select('budget_id',$budgets,Input::old('budget_id') ?: 0,['class' => 'form-control'])}} Select one of your budgets to make this transaction a part of it.
- +
Add more fine-grained information to this transaction by entering a category.