Some cleanup.

This commit is contained in:
James Cole 2014-09-21 15:51:07 +02:00
parent c119a42d70
commit 5dfc04e777

View File

@ -2,6 +2,11 @@
namespace Firefly\Helper\Controllers; namespace Firefly\Helper\Controllers;
use Firefly\Storage\Account\AccountRepositoryInterface as ARI;
use Firefly\Storage\Budget\BudgetRepositoryInterface as BRI;
use Firefly\Storage\Category\CategoryRepositoryInterface as CRI;
use Firefly\Storage\Piggybank\PiggybankRepositoryInterface as PRI;
use Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface as TJRI;
use Illuminate\Support\MessageBag; use Illuminate\Support\MessageBag;
/** /**
@ -13,12 +18,37 @@ class Transaction implements TransactionInterface
{ {
protected $_user = null; protected $_user = null;
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $_journals */
protected $_journals;
/** @var \Firefly\Storage\Category\CategoryRepositoryInterface $_categories */
protected $_categories;
/** @var \Firefly\Storage\Budget\BudgetRepositoryInterface $_budgets */
protected $_budgets;
/** @var \Firefly\Storage\Piggybank\PiggybankRepositoryInterface $_piggybanks */
protected $_piggybanks;
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $_accounts */
protected $_accounts;
/** /**
* * @param TJRI $journals
* @param CRI $categories
* @param BRI $budgets
* @param PRI $piggybanks
* @param ARI $accounts
*/ */
public function __construct() public function __construct(TJRI $journals, CRI $categories, BRI $budgets, PRI $piggybanks, ARI $accounts)
{ {
$this->_user = \Auth::user(); $this->_journals = $journals;
$this->_categories = $categories;
$this->_budgets = $budgets;
$this->_piggybanks = $piggybanks;
$this->_accounts = $accounts;
$this->overruleUser(\Auth::user());
} }
/** /**
@ -29,6 +59,11 @@ class Transaction implements TransactionInterface
public function overruleUser(\User $user) public function overruleUser(\User $user)
{ {
$this->_user = $user; $this->_user = $user;
$this->_journals->overruleUser($user);
$this->_categories->overruleUser($user);
$this->_budgets->overruleUser($user);
$this->_piggybanks->overruleUser($user);
$this->_accounts->overruleUser($user);
return true; return true;
} }
@ -38,30 +73,12 @@ class Transaction implements TransactionInterface
* *
* @return MessageBag|\TransactionJournal * @return MessageBag|\TransactionJournal
*/ */
public function update(\TransactionJournal $journal, array $data) { public function update(\TransactionJournal $journal, array $data)
/* {
* All the repositories we need:
*/
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $journals */
$journals = \App::make('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$journals->overruleUser($this->_user);
/** @var \Firefly\Storage\Category\CategoryRepositoryInterface $categories */
$categories = \App::make('Firefly\Storage\Category\CategoryRepositoryInterface');
$categories->overruleUser($this->_user);
/** @var \Firefly\Storage\Budget\BudgetRepositoryInterface $budgets */
$budgets = \App::make('Firefly\Storage\Budget\BudgetRepositoryInterface');
$budgets->overruleUser($this->_user);
/** @var \Firefly\Storage\Piggybank\PiggybankRepositoryInterface $piggybanks */
$piggybanks = \App::make('Firefly\Storage\Piggybank\PiggybankRepositoryInterface');
$piggybanks->overruleUser($this->_user);
/* /*
* Update the journal using the repository. * Update the journal using the repository.
*/ */
$journal = $journals->update($journal, $data); $journal = $this->_journals->update($journal, $data);
/* /*
* If invalid, return the message bag: * If invalid, return the message bag:
@ -74,76 +91,74 @@ class Transaction implements TransactionInterface
* find budget using repository * find budget using repository
*/ */
if (isset($data['budget_id'])) { if (isset($data['budget_id'])) {
$budget = $budgets->find($data['budget_id']); $budget = $this->_budgets->find($data['budget_id']);
} }
/* /*
* find category using repository * find category using repository
*/ */
$category = $categories->firstOrCreate($data['category']); $category = $this->_categories->firstOrCreate($data['category']);
/* /*
* Find piggy bank using repository: * Find piggy bank using repository:
*/ */
$piggybank = null; $piggybank = null;
if(isset($data['piggybank_id'])) { if (isset($data['piggybank_id'])) {
$piggybank = $piggybanks->find($data['piggybank_id']); $piggybank = $this->_piggybanks->find($data['piggybank_id']);
} }
/* /*
* save accounts using repositories * save accounts using repositories
* this depends on the kind of transaction and i've yet to fix this. * this depends on the kind of transaction and i've yet to fix this.
*/ */
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
if (isset($data['account_id'])) { if (isset($data['account_id'])) {
$from = $accounts->findAssetAccountById($data['account_id']); $from = $this->_accounts->findAssetAccountById($data['account_id']);
} }
if (isset($data['expense_account'])) { if (isset($data['expense_account'])) {
$to = $accounts->findExpenseAccountByName($data['expense_account']); $to = $this->_accounts->findExpenseAccountByName($data['expense_account']);
} }
if (isset($data['revenue_account'])) { if (isset($data['revenue_account'])) {
$from = $accounts->findRevenueAccountByName($data['revenue_account']); $from = $this->_accounts->findRevenueAccountByName($data['revenue_account']);
$to = $accounts->findAssetAccountById($data['account_id']); $to = $this->_accounts->findAssetAccountById($data['account_id']);
} }
if (isset($data['account_from_id'])) { if (isset($data['account_from_id'])) {
$from = $accounts->findAssetAccountById($data['account_from_id']); $from = $this->_accounts->findAssetAccountById($data['account_from_id']);
} }
if (isset($data['account_to_id'])) { if (isset($data['account_to_id'])) {
$to = $accounts->findAssetAccountById($data['account_to_id']); $to = $this->_accounts->findAssetAccountById($data['account_to_id']);
} }
/* /*
* Add a custom error when they are the same. * Add a custom error when they are the same.
*/ */
if($to->id == $from->id) { if ($to->id == $from->id) {
$bag = new MessageBag; $bag = new MessageBag;
$bag->add('account_from_id','The account from cannot be the same as the account to.'); $bag->add('account_from_id', 'The account from cannot be the same as the account to.');
return $bag; return $bag;
} }
/* /*
* Check if the transactions need new data: * Check if the transactions need new data:
*/ */
$transactions = $journal->transactions()->orderBy('amount','ASC')->get(); $transactions = $journal->transactions()->orderBy('amount', 'ASC')->get();
/** @var \Transaction $transaction */ /** @var \Transaction $transaction */
foreach($transactions as $index => $transaction) { foreach ($transactions as $index => $transaction) {
switch(true) { switch (true) {
case ($index == 0): // FROM account case ($index == 0): // FROM account
$transaction->account()->associate($from); $transaction->account()->associate($from);
$transaction->amount = floatval($data['amount']) * -1; $transaction->amount = floatval($data['amount']) * -1;
break; break;
case ($index == 1): // TO account. case ($index == 1): // TO account.
$transaction->account()->associate($to); $transaction->account()->associate($to);
$transaction->amount = floatval($data['amount']); $transaction->amount = floatval($data['amount']);
break; break;
} }
$transaction->save(); $transaction->save();
// either way, try to attach the piggy bank: // either way, try to attach the piggy bank:
if(!is_null($piggybank)) { if (!is_null($piggybank)) {
if($piggybank->account_id == $transaction->account_id) { if ($piggybank->account_id == $transaction->account_id) {
$transaction->piggybank()->associate($piggybank); $transaction->piggybank()->associate($piggybank);
} }
} }
@ -153,11 +168,11 @@ class Transaction implements TransactionInterface
* Connect budget and category: * Connect budget and category:
*/ */
$budgetids = is_null($budget) ? [] : [$budget->id]; $budgetids = is_null($budget) ? [] : [$budget->id];
$catids = is_null($category) ? [] : [$category->id]; $catids = is_null($category) ? [] : [$category->id];
$journal->budgets()->sync($budgetids); $journal->budgets()->sync($budgetids);
$journal->categories()->sync($catids); $journal->categories()->sync($catids);
$journal->save(); $journal->save();
if(isset($data['return_journal']) && $data['return_journal'] == true) { if (isset($data['return_journal']) && $data['return_journal'] == true) {
return $journal; return $journal;
} }
return $journal->errors(); return $journal->errors();
@ -175,30 +190,10 @@ class Transaction implements TransactionInterface
*/ */
public function store(array $data) public function store(array $data)
{ {
/*
* All the repositories we need:
*/
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $journals */
$journals = \App::make('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$journals->overruleUser($this->_user);
/** @var \Firefly\Storage\Category\CategoryRepositoryInterface $categories */
$categories = \App::make('Firefly\Storage\Category\CategoryRepositoryInterface');
$categories->overruleUser($this->_user);
/** @var \Firefly\Storage\Budget\BudgetRepositoryInterface $budgets */
$budgets = \App::make('Firefly\Storage\Budget\BudgetRepositoryInterface');
$budgets->overruleUser($this->_user);
/** @var \Firefly\Storage\Piggybank\PiggybankRepositoryInterface $piggybanks */
$piggybanks = \App::make('Firefly\Storage\Piggybank\PiggybankRepositoryInterface');
$piggybanks->overruleUser($this->_user);
/* /*
* save journal using repository * save journal using repository
*/ */
$journal = $journals->store($data); $journal = $this->_journals->store($data);
/* /*
* If invalid, return the message bag: * If invalid, return the message bag:
@ -211,53 +206,50 @@ class Transaction implements TransactionInterface
* find budget using repository * find budget using repository
*/ */
if (isset($data['budget_id'])) { if (isset($data['budget_id'])) {
$budget = $budgets->find($data['budget_id']); $budget = $this->_budgets->find($data['budget_id']);
} }
/* /*
* find category using repository * find category using repository
*/ */
$category = $categories->firstOrCreate($data['category']); $category = $this->_categories->firstOrCreate($data['category']);
/* /*
* Find piggy bank using repository: * Find piggy bank using repository:
*/ */
$piggybank = null; $piggybank = null;
if(isset($data['piggybank_id'])) { if (isset($data['piggybank_id'])) {
$piggybank = $piggybanks->find($data['piggybank_id']); $piggybank = $this->_piggybanks->find($data['piggybank_id']);
} }
/* /*
* save accounts using repositories * save accounts using repositories
* this depends on the kind of transaction and i've yet to fix this. * this depends on the kind of transaction and i've yet to fix this.
*/ */
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
if (isset($data['account_id'])) { if (isset($data['account_id'])) {
$from = $accounts->findAssetAccountById($data['account_id']); $from = $this->_accounts->findAssetAccountById($data['account_id']);
} }
if (isset($data['expense_account'])) { if (isset($data['expense_account'])) {
$to = $accounts->findExpenseAccountByName($data['expense_account']); $to = $this->_accounts->findExpenseAccountByName($data['expense_account']);
} }
if (isset($data['revenue_account'])) { if (isset($data['revenue_account'])) {
$from = $accounts->findRevenueAccountByName($data['revenue_account']); $from = $this->_accounts->findRevenueAccountByName($data['revenue_account']);
$to = $accounts->findAssetAccountById($data['account_id']); $to = $this->_accounts->findAssetAccountById($data['account_id']);
} }
if (isset($data['account_from_id'])) { if (isset($data['account_from_id'])) {
$from = $accounts->findAssetAccountById($data['account_from_id']); $from = $this->_accounts->findAssetAccountById($data['account_from_id']);
} }
if (isset($data['account_to_id'])) { if (isset($data['account_to_id'])) {
$to = $accounts->findAssetAccountById($data['account_to_id']); $to = $this->_accounts->findAssetAccountById($data['account_to_id']);
} }
/* /*
* Add a custom error when they are the same. * Add a custom error when they are the same.
*/ */
if($to->id == $from->id) { if ($to->id == $from->id) {
$bag = new MessageBag; $bag = new MessageBag;
$bag->add('account_from_id','The account from cannot be the same as the account to.'); $bag->add('account_from_id', 'The account from cannot be the same as the account to.');
return $bag; return $bag;
} }
@ -266,9 +258,9 @@ class Transaction implements TransactionInterface
* piggy bank to either transaction, knowing it will only work with one of them. * piggy bank to either transaction, knowing it will only work with one of them.
*/ */
/** @var \Transaction $one */ /** @var \Transaction $one */
$one = $journals->saveTransaction($journal, $from, floatval($data['amount']) * -1); $one = $this->_journals->saveTransaction($journal, $from, floatval($data['amount']) * -1);
$one->connectPiggybank($piggybank); $one->connectPiggybank($piggybank);
$two = $journals->saveTransaction($journal, $to, floatval($data['amount'])); $two = $this->_journals->saveTransaction($journal, $to, floatval($data['amount']));
$two->connectPiggybank($piggybank); $two->connectPiggybank($piggybank);
/* /*
* Count for $journal is zero? Then there were errors! * Count for $journal is zero? Then there were errors!
@ -293,7 +285,7 @@ class Transaction implements TransactionInterface
} }
$journal->completed = true; $journal->completed = true;
$journal->save(); $journal->save();
if(isset($data['return_journal']) && $data['return_journal'] == true) { if (isset($data['return_journal']) && $data['return_journal'] == true) {
return $journal; return $journal;
} }
return $journal->errors(); return $journal->errors();