Some code cleanup.

This commit is contained in:
James Cole
2015-05-25 07:14:04 +02:00
parent 4b687b9bdc
commit e19c44efbd
5 changed files with 122 additions and 79 deletions

View File

@@ -31,6 +31,9 @@ use View;
*/ */
class TagController extends Controller class TagController extends Controller
{ {
public $tagOptions = [];
/** /**
* @codeCoverageIgnore * @codeCoverageIgnore
*/ */
@@ -40,12 +43,12 @@ class TagController extends Controller
View::share('title', 'Tags'); View::share('title', 'Tags');
View::share('mainTitleIcon', 'fa-tags'); View::share('mainTitleIcon', 'fa-tags');
View::share('hideTags', true); View::share('hideTags', true);
$tagOptions = [ $this->tagOptions = [
'nothing' => 'Just a regular tag.', 'nothing' => 'Just a regular tag.',
'balancingAct' => 'The tag takes at most two transactions; an expense and a transfer. They\'ll balance each other out.', '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.', '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 * @return View
*/ */
public function edit(Tag $tag) public function edit(Tag $tag, TagRepositoryInterface $repository)
{ {
$subTitle = 'Edit tag "' . e($tag->tag) . '"'; $subTitle = 'Edit tag "' . e($tag->tag) . '"';
$subTitleIcon = 'fa-tag'; $subTitleIcon = 'fa-tag';
@@ -116,67 +119,16 @@ class TagController extends Controller
/* /*
* Default tag options (again) * Default tag options (again)
*/ */
$tagOptions = [ $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.',
];
/* /*
* Can this tag become another type? * Can this tag become another type?
*/ */
$allowToAdvancePayment = true; $allowAdvance = $repository->tagAllowAdvance($tag);
$allowToBalancingAct = true; $allowToBalancingAct = $repository->tagAllowBalancing($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') {
$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;
}
// edit tag options: // edit tag options:
if ($allowToAdvancePayment === false) { if ($allowAdvance === false) {
unset($tagOptions['advancePayment']); unset($tagOptions['advancePayment']);
} }
if ($allowToBalancingAct === false) { if ($allowToBalancingAct === false) {

View File

@@ -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 Tag $tag
* @param array $data * @param array $data

View File

@@ -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 * 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 * 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); 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 * @param array $data
* *
@@ -42,9 +62,22 @@ interface TagRepositoryInterface
public function store(array $data); 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 * @param Tag $tag
@@ -53,19 +86,4 @@ interface TagRepositoryInterface
* @return Tag * @return Tag
*/ */
public function update(Tag $tag, array $data); 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);
} }

View File

@@ -232,15 +232,15 @@ class ExpandedForm
$fields = ['title', 'name', 'description']; $fields = ['title', 'name', 'description'];
/** @var Eloquent $entry */ /** @var Eloquent $entry */
foreach ($set as $entry) { foreach ($set as $entry) {
$id = intval($entry->id); $entryId = intval($entry->id);
$title = null; $title = null;
foreach ($fields as $field) { foreach ($fields as $field) {
if (isset($entry->$field)) { if (isset($entry->$field)) {
$title = $entry->$field; $title = $entry->$field;
} }
} }
$selectList[$id] = $title; $selectList[$entryId] = $title;
} }
return $selectList; return $selectList;