Skip to content

Firefly III: a personal finances manager

Some code cleanup.

James Colee19c44efbde2
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
@@ -35,11 +43,11 @@ interface TagRepositoryInterface
public function coveredByBalancingActs(Account $account, Carbon $start, Carbon $end);
/**
- * @param array $data
+ * @param Tag $tag
*
- * @return Tag
+ * @return boolean
*/
- public function store(array $data);
+ public function destroy(Tag $tag);
/**
* @return Collection
@@ -47,25 +55,35 @@ interface TagRepositoryInterface
public function get();
/**
- * @param Tag $tag
* @param array $data
*
* @return Tag
*/
- public function update(Tag $tag, array $data);
+ public function store(array $data);
/**
+ * Can a tag become an advance payment?
+ *
* @param Tag $tag
*
- * @return boolean
+ * @return bool
*/
- public function destroy(Tag $tag);
+ public function tagAllowAdvance(Tag $tag);
/**
- * @param TransactionJournal $journal
- * @param Tag $tag
+ * Can a tag become a balancing act?
*
- * @return boolean
+ * @param Tag $tag
+ *
+ * @return bool
*/
- public function connect(TransactionJournal $journal, Tag $tag);
+ public function tagAllowBalancing(Tag $tag);
+
+ /**
+ * @param Tag $tag
+ * @param array $data
+ *
+ * @return Tag
+ */
+ public function update(Tag $tag, array $data);
}
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;