Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Firefly is now capable of adding a transaction. Untested [skip-ci]
James Colea1fb4bdb6015
diff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.phpindex 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();- return View::make('transactions.withdrawal')->with('accounts',$accounts);+ $budgets[0] = '(no budget)';+++ 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 filediff --git a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.phpindex 0ba7e745c3..e4460525cd 100644--- a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php+++ b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php@@ -15,6 +15,8 @@ interface AccountRepositoryInterfacepublic function find($id);+ public function findByName($name);+public function getByIds($ids);public function getDefault();@@ -27,4 +29,8 @@ interface AccountRepositoryInterfacepublic function storeWithInitialBalance($data, \Carbon\Carbon $date, $amount = 0);+ public function createOrFindBeneficiary($name);++ public function createOrFind($name, \AccountType $type);+}\ No newline at end of filediff --git a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.phpindex 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 AccountRepositoryInterfacereturn \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 AccountRepositoryInterfacereturn $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 filediff --git a/app/lib/Firefly/Storage/Budget/BudgetRepositoryInterface.php b/app/lib/Firefly/Storage/Budget/BudgetRepositoryInterface.phpindex 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 filediff --git a/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php b/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.phpindex 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 filediff --git a/app/lib/Firefly/Storage/Category/CategoryRepositoryInterface.php b/app/lib/Firefly/Storage/Category/CategoryRepositoryInterface.phpindex 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 filediff --git a/app/lib/Firefly/Storage/Category/EloquentCategoryRepository.php b/app/lib/Firefly/Storage/Category/EloquentCategoryRepository.phpindex 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 filediff --git a/app/routes.php b/app/routes.phpindex 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.phpindex bd6a719b57..a20666c4e2 100644--- a/app/views/transactions/withdrawal.blade.php+++ b/app/views/transactions/withdrawal.blade.php@@ -26,21 +26,21 @@<h4>Mandatory fields</h4><div class="form-group">- <label for="account" class="col-sm-4 control-label">Description</label>+ <label for="description" class="col-sm-4 control-label">Description</label><div class="col-sm-8"><input type="text" name="description" value="{{{Input::old('description')}}}" autocomplete="off" class="form-control" placeholder="Description" /></div></div><div class="form-group">- <label for="account" class="col-sm-4 control-label">Account</label>+ <label for="account_id" class="col-sm-4 control-label">Account</label><div class="col-sm-8">{{Form::select('account_id',$accounts,Input::old('account_id'),['class' => 'form-control'])}}</div></div><div class="form-group">- <label for="account" class="col-sm-4 control-label">Beneficiary</label>+ <label for="beneficiary" class="col-sm-4 control-label">Beneficiary</label><div class="col-sm-8"><input type="text" name="beneficiary" value="{{{Input::old('beneficiary')}}}" autocomplete="off" class="form-control" placeholder="Beneficiary" /><span class="help-block">This field will auto-complete your existing beneficiaries (if any), but you can type freely to create new ones.</span>@@ -50,21 +50,21 @@<div class="form-group">- <label for="account" class="col-sm-4 control-label">Amount spent</label>+ <label for="amount" class="col-sm-4 control-label">Amount spent</label><div class="col-sm-8">- <input type="number" min="0.01" step="any" class="form-control" />+ <input type="number" name="amount" min="0.01" step="any" class="form-control" /></div></div><div class="form-group">- <label for="account" class="col-sm-4 control-label">Date</label>+ <label for="date" class="col-sm-4 control-label">Date</label><div class="col-sm-8">- <input type="date" class="form-control" />+ <input type="date" name="date" value="{{Input::old('date') ?: date('Y-m-d')}}" class="form-control" /></div></div><div class="form-group">- <label for="account" class="col-sm-4 control-label"> </label>+ <label for="submit" class="col-sm-4 control-label"> </label><div class="col-sm-8"><input type="submit" name="submit" value="Create withdrawal" class="btn btn-info" /></div>@@ -75,17 +75,15 @@<h4>Optional fields</h4><div class="form-group">- <label for="account" class="col-sm-4 control-label">Budget</label>+ <label for="budget_id" class="col-sm-4 control-label">Budget</label><div class="col-sm-8">- <select class="form-control">- <option>1</option>- </select>+ {{Form::select('budget_id',$budgets,Input::old('budget_id') ?: 0,['class' => 'form-control'])}}<span class="help-block">Select one of your budgets to make this transaction a part of it.</span></div></div><div class="form-group">- <label for="account" class="col-sm-4 control-label">Category</label>+ <label for="category" class="col-sm-4 control-label">Category</label><div class="col-sm-8"><input type="text" name="category" value="" autocomplete="off" class="form-control" placeholder="Category" /><span class="help-block">Add more fine-grained information to this transaction by entering a category.