Skip to content

Firefly III: a personal finances manager

Some tag related fine tuning.

James Cole7531134ad2f7
diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php
index 0e8503375d..c0e5e23eb8 100644
--- a/app/Http/Controllers/TagController.php
+++ b/app/Http/Controllers/TagController.php
@@ -22,6 +22,11 @@ use View;
* Remember: a balancingAct takes at most one expense and one transfer.
* an advancePayment takes at most one expense, infinite deposits and NO transfers.
*
+ * TODO transaction can only have one advancePayment OR balancingAct.
+ * TODO Other attempts to put in such a tag are blocked.
+ * TODO also show an error when editing a tag and it becomes either
+ * TODO of these two types. Or rather, block editing of the tag.
+ *
* @package FireflyIII\Http\Controllers
*/
class TagController extends Controller
@@ -34,7 +39,7 @@ class TagController extends Controller
parent::__construct();
View::share('title', 'Tags');
View::share('mainTitleIcon', 'fa-tags');
- View::share('hideTags',true);
+ View::share('hideTags', true);
$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.',
@@ -43,6 +48,29 @@ class TagController extends Controller
View::share('tagOptions', $tagOptions);
}
+ /**
+ * @return \Illuminate\View\View
+ */
+ public function create()
+ {
+ $subTitle = 'New tag';
+ $subTitleIcon = 'fa-tag';
+
+ $preFilled = [
+ 'tagMode' => 'nothing'
+ ];
+ if (!Input::old('tagMode')) {
+ Session::flash('preFilled', $preFilled);
+ }
+ // put previous url in session if not redirect from store (not "create another").
+ if (Session::get('tags.create.fromStore') !== true) {
+ Session::put('tags.create.url', URL::previous());
+ }
+ Session::forget('tags.create.fromStore');
+
+ return view('tags.create', compact('subTitle', 'subTitleIcon'));
+ }
+
/**
* @param Tag $tag
*
@@ -75,37 +103,85 @@ class TagController extends Controller
}
/**
- * @return \Illuminate\View\View
+ * @param Tag $tag
+ *
+ * @return View
*/
- public function create()
+ public function edit(Tag $tag)
{
- $subTitle = 'New tag';
+ $subTitle = 'Edit tag "' . e($tag->tag) . '"';
$subTitleIcon = 'fa-tag';
- $preFilled = [
- 'tagMode' => 'nothing'
+ /*
+ * 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.',
];
- if (!Input::old('tagMode')) {
- Session::flash('preFilled', $preFilled);
+
+ /*
+ * 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') {
+ foreach ($tag->transactionjournals as $journal) {
+ if ($journal->transactionType->type == 'Transfer') {
+ $allowToAdvancePayment = false;
+ }
+ }
}
- // put previous url in session if not redirect from store (not "create another").
- if (Session::get('tags.create.fromStore') !== true) {
- Session::put('tags.create.url', URL::previous());
+
+ /*
+ * 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;
}
- Session::forget('tags.create.fromStore');
- return view('tags.create', compact('subTitle', 'subTitleIcon'));
- }
+ /*
+ * 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 tagoptions:
+ if ($allowToAdvancePayment === false) {
+ unset($tagOptions['advancePayment']);
+ }
+ if ($allowToBalancingAct === false) {
+ unset($tagOptions['balancingAct']);
+ }
- /**
- * @param Tag $tag
- *
- * @return View
- */
- public function edit(Tag $tag)
- {
- $subTitle = 'Edit tag "' . e($tag->tag) . '"';
- $subTitleIcon = 'fa-tag';
// put previous url in session if not redirect from store (not "return_to_edit").
if (Session::get('tags.edit.fromUpdate') !== true) {
@@ -113,8 +189,7 @@ class TagController extends Controller
}
Session::forget('tags.edit.fromUpdate');
-
- return view('tags.edit', compact('tag', 'subTitle', 'subTitleIcon'));
+ return view('tags.edit', compact('tag', 'subTitle', 'subTitleIcon', 'tagOptions'));
}
/**
diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php
index 935a6b977d..301fedb267 100644
--- a/app/Repositories/Tag/TagRepository.php
+++ b/app/Repositories/Tag/TagRepository.php
@@ -71,7 +71,6 @@ class TagRepository implements TagRepositoryInterface
// ignore expense
return false;
}
-
if ($tag->tagMode == 'advancePayment') {