From e19c44efbde2a48055e5514a321173350394ba86 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 25 May 2015 07:14:04 +0200 Subject: [PATCH] Some code cleanup. --- app/Http/Controllers/TagController.php | 68 +++-------------- app/Providers/ConfigServiceProvider.php | 2 +- app/Repositories/Tag/TagRepository.php | 73 +++++++++++++++++++ .../Tag/TagRepositoryInterface.php | 52 ++++++++----- app/Support/ExpandedForm.php | 6 +- 5 files changed, 122 insertions(+), 79 deletions(-) diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php index 37d1d3c840..95e3fbdacd 100644 --- a/app/Http/Controllers/TagController.php +++ b/app/Http/Controllers/TagController.php @@ -31,6 +31,9 @@ use View; */ class TagController extends Controller { + + public $tagOptions = []; + /** * @codeCoverageIgnore */ @@ -40,12 +43,12 @@ class TagController extends Controller View::share('title', 'Tags'); View::share('mainTitleIcon', 'fa-tags'); View::share('hideTags', true); - $tagOptions = [ + $this->tagOptions = [ 'nothing' => 'Just a regular tag.', 'balancingAct' => 'The tag takes at most two transactions; an expense and a transfer. They\'ll balance each other out.', 'advancePayment' => 'The tag accepts one expense and any number of deposits aimed to repay the original expense.', ]; - View::share('tagOptions', $tagOptions); + View::share('tagOptions', $this->tagOptions); } /** @@ -108,7 +111,7 @@ class TagController extends Controller * * @return View */ - public function edit(Tag $tag) + public function edit(Tag $tag, TagRepositoryInterface $repository) { $subTitle = 'Edit tag "' . e($tag->tag) . '"'; $subTitleIcon = 'fa-tag'; @@ -116,67 +119,16 @@ class TagController extends Controller /* * Default tag options (again) */ - $tagOptions = [ - 'nothing' => 'Just a regular tag.', - 'balancingAct' => 'The tag takes at most two transactions; an expense and a transfer. They\'ll balance each other out.', - 'advancePayment' => 'The tag accepts one expense and any number of deposits aimed to repay the original expense.', - ]; + $tagOptions = $this->tagOptions; /* * Can this tag become another type? */ - $allowToAdvancePayment = true; - $allowToBalancingAct = true; - - /* - * If this tag is a balancing act, and it contains transfers, it cannot be - * changes to an advancePayment. - */ - - if ($tag->tagMode == 'balancingAct' || $tag->tagMode == 'nothing') { - foreach ($tag->transactionjournals as $journal) { - if ($journal->transactionType->type == 'Transfer') { - $allowToAdvancePayment = false; - } - } - } - - /* - * If this tag contains more than one expenses, it cannot become an advance payment. - */ - $count = 0; - foreach ($tag->transactionjournals as $journal) { - if ($journal->transactionType->type == 'Withdrawal') { - $count++; - } - } - if ($count > 1) { - $allowToAdvancePayment = false; - } - - /* - * If has more than two transactions already, cannot become a balancing act: - */ - if ($tag->transactionjournals->count() > 2) { - $allowToBalancingAct = false; - } - - /* - * If any transaction is a deposit, cannot become a balancing act. - */ - $count = 0; - foreach ($tag->transactionjournals as $journal) { - if ($journal->transactionType->type == 'Deposit') { - $count++; - } - } - if ($count > 0) { - $allowToBalancingAct = false; - } - + $allowAdvance = $repository->tagAllowAdvance($tag); + $allowToBalancingAct = $repository->tagAllowBalancing($tag); // edit tag options: - if ($allowToAdvancePayment === false) { + if ($allowAdvance === false) { unset($tagOptions['advancePayment']); } if ($allowToBalancingAct === false) { diff --git a/app/Providers/ConfigServiceProvider.php b/app/Providers/ConfigServiceProvider.php index 7b8e25a927..df8e7269d0 100644 --- a/app/Providers/ConfigServiceProvider.php +++ b/app/Providers/ConfigServiceProvider.php @@ -23,7 +23,7 @@ class ConfigServiceProvider extends ServiceProvider */ public function register() { - + } } diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index f1b42f628e..0c48669c7d 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -143,6 +143,79 @@ class TagRepository implements TagRepositoryInterface } + /** + * Can a tag become an advance payment? + * + * @param Tag $tag + * + * @return bool + */ + public function tagAllowAdvance(Tag $tag) + { + /* + * If this tag is a balancing act, and it contains transfers, it cannot be + * changes to an advancePayment. + */ + + if ($tag->tagMode == 'balancingAct' || $tag->tagMode == 'nothing') { + foreach ($tag->transactionjournals as $journal) { + if ($journal->transactionType->type == 'Transfer') { + return false; + } + } + } + + /* + * If this tag contains more than one expenses, it cannot become an advance payment. + */ + $count = 0; + foreach ($tag->transactionjournals as $journal) { + if ($journal->transactionType->type == 'Withdrawal') { + $count++; + } + } + if ($count > 1) { + return false; + } + + return true; + + } + + /** + * Can a tag become a balancing act? + * + * @param Tag $tag + * + * @return bool + */ + public function tagAllowBalancing(Tag $tag) + { + /* + * If has more than two transactions already, cannot become a balancing act: + */ + if ($tag->transactionjournals->count() > 2) { + return false; + } + + /* + * If any transaction is a deposit, cannot become a balancing act. + */ + $count = 0; + foreach ($tag->transactionjournals as $journal) { + if ($journal->transactionType->type == 'Deposit') { + $count++; + } + } + if ($count > 0) { + return false; + } + + + return true; + } + + /** * @param Tag $tag * @param array $data diff --git a/app/Repositories/Tag/TagRepositoryInterface.php b/app/Repositories/Tag/TagRepositoryInterface.php index 95cbe558e5..463e0979d2 100644 --- a/app/Repositories/Tag/TagRepositoryInterface.php +++ b/app/Repositories/Tag/TagRepositoryInterface.php @@ -18,6 +18,14 @@ interface TagRepositoryInterface { + /** + * @param TransactionJournal $journal + * @param Tag $tag + * + * @return boolean + */ + public function connect(TransactionJournal $journal, Tag $tag); + /** * This method scans the transaction journals from or to the given asset account * and checks if these are part of a balancing act. If so, it will sum up the amounts @@ -34,6 +42,18 @@ interface TagRepositoryInterface */ public function coveredByBalancingActs(Account $account, Carbon $start, Carbon $end); + /** + * @param Tag $tag + * + * @return boolean + */ + public function destroy(Tag $tag); + + /** + * @return Collection + */ + public function get(); + /** * @param array $data * @@ -42,9 +62,22 @@ interface TagRepositoryInterface public function store(array $data); /** - * @return Collection + * Can a tag become an advance payment? + * + * @param Tag $tag + * + * @return bool */ - public function get(); + public function tagAllowAdvance(Tag $tag); + + /** + * Can a tag become a balancing act? + * + * @param Tag $tag + * + * @return bool + */ + public function tagAllowBalancing(Tag $tag); /** * @param Tag $tag @@ -53,19 +86,4 @@ interface TagRepositoryInterface * @return Tag */ public function update(Tag $tag, array $data); - - /** - * @param Tag $tag - * - * @return boolean - */ - public function destroy(Tag $tag); - - /** - * @param TransactionJournal $journal - * @param Tag $tag - * - * @return boolean - */ - public function connect(TransactionJournal $journal, Tag $tag); } diff --git a/app/Support/ExpandedForm.php b/app/Support/ExpandedForm.php index e117e7ba1b..8e5d4fe7a4 100644 --- a/app/Support/ExpandedForm.php +++ b/app/Support/ExpandedForm.php @@ -232,15 +232,15 @@ class ExpandedForm $fields = ['title', 'name', 'description']; /** @var Eloquent $entry */ foreach ($set as $entry) { - $id = intval($entry->id); - $title = null; + $entryId = intval($entry->id); + $title = null; foreach ($fields as $field) { if (isset($entry->$field)) { $title = $entry->$field; } } - $selectList[$id] = $title; + $selectList[$entryId] = $title; } return $selectList;