Skip to content

Firefly III: a personal finances manager

Final code for tag report.

James Cole40c38af76642
diff --git a/app/Helpers/Chart/MetaPieChart.php b/app/Helpers/Chart/MetaPieChart.php
index 7d72637184..b93035c179 100644
--- a/app/Helpers/Chart/MetaPieChart.php
+++ b/app/Helpers/Chart/MetaPieChart.php
@@ -14,11 +14,13 @@ namespace FireflyIII\Helpers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Report\Support;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
+use FireflyIII\Models\Tag;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
+use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Steam;
@@ -46,6 +48,7 @@ class MetaPieChart implements MetaPieChartInterface
'account' => ['opposing_account_id'],
'budget' => ['transaction_journal_budget_id', 'transaction_budget_id'],
'category' => ['transaction_journal_category_id', 'transaction_category_id'],
+ 'tag' => [],
];
/** @var array */
protected $repositories
@@ -53,6 +56,7 @@ class MetaPieChart implements MetaPieChartInterface
'account' => AccountRepositoryInterface::class,
'budget' => BudgetRepositoryInterface::class,
'category' => CategoryRepositoryInterface::class,
+ 'tag' => TagRepositoryInterface::class,
];
/** @var Carbon */
protected $start;
@@ -249,8 +253,13 @@ class MetaPieChart implements MetaPieChartInterface
*
* @return array
*/
- protected function groupByFields(Collection $set, array $fields)
+ protected function groupByFields(Collection $set, array $fields): array
{
+ if (count($fields) === 0 && $this->tags->count() > 0) {
+ // do a special group on tags:
+ return $this->groupByTag($set);
+ }
+
$grouped = [];
/** @var Transaction $transaction */
foreach ($set as $transaction) {
@@ -280,7 +289,7 @@ class MetaPieChart implements MetaPieChartInterface
foreach ($array as $objectId => $amount) {
if (!isset($names[$objectId])) {
$object = $repository->find(intval($objectId));
- $names[$objectId] = $object->name;
+ $names[$objectId] = $object->name ?? $object->tag;
}
$amount = Steam::positive($amount);
$this->total = bcadd($this->total, $amount);
@@ -290,4 +299,22 @@ class MetaPieChart implements MetaPieChartInterface
return $chartData;
}
+
+ private function groupByTag(Collection $set): array
+ {
+ $grouped = [];
+ /** @var Transaction $transaction */
+ foreach ($set as $transaction) {
+ $journal = $transaction->transactionJournal;
+ $tags = $journal->tags;
+ /** @var Tag $tag */
+ foreach ($tags as $tag) {
+ $tagId = $tag->id;
+ $grouped[$tagId] = $grouped[$tagId] ?? '0';
+ $grouped[$tagId] = bcadd($transaction->transaction_amount, $grouped[$tagId]);
+ }
+ }
+
+ return $grouped;
+ }
}
diff --git a/app/Http/Controllers/Chart/TagReportController.php b/app/Http/Controllers/Chart/TagReportController.php
index c4a6eb3cba..0fd6f9d159 100644
--- a/app/Http/Controllers/Chart/TagReportController.php
+++ b/app/Http/Controllers/Chart/TagReportController.php
@@ -241,6 +241,55 @@ class TagReportController extends Controller
return Response::json($data);
}
+ /**
+ * @param Collection $accounts
+ * @param Collection $tags
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param string $others
+ *
+ * @return \Illuminate\Http\JsonResponse
+ */
+ public function tagExpense(Collection $accounts, Collection $tags, Carbon $start, Carbon $end, string $others)
+ {
+ /** @var MetaPieChartInterface $helper */
+ $helper = app(MetaPieChartInterface::class);
+ $helper->setAccounts($accounts);
+ $helper->setTags($tags);
+ $helper->setStart($start);
+ $helper->setEnd($end);
+ $helper->setCollectOtherObjects(intval($others) === 1);
+ $chartData = $helper->generate('expense', 'tag');
+ $data = $this->generator->pieChart($chartData);
+
+ return Response::json($data);
+ }
+
+ /**
+ * @param Collection $accounts
+ * @param Collection $tags
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param string $others
+ *
+ * @return \Illuminate\Http\JsonResponse
+ */
+ public function tagIncome(Collection $accounts, Collection $tags, Carbon $start, Carbon $end, string $others)
+ {
+
+ /** @var MetaPieChartInterface $helper */
+ $helper = app(MetaPieChartInterface::class);
+ $helper->setAccounts($accounts);
+ $helper->setTags($tags);
+ $helper->setStart($start);
+ $helper->setEnd($end);
+ $helper->setCollectOtherObjects(intval($others) === 1);
+ $chartData = $helper->generate('income', 'tag');
+ $data = $this->generator->pieChart($chartData);
+
+ return Response::json($data);
+ }
+
/**
* @param Collection $accounts
* @param Collection $tags
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index 3fe45e6dd0..d92a349172 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -747,6 +747,8 @@ return [
'include_expense_not_in_category' => 'Included expenses not in the selected category(ies)',
'include_income_not_in_category' => 'Included income not in the selected category(ies)',
'include_income_not_in_account' => 'Included income not in the selected account(s)',
+ 'include_income_not_in_tags' => 'Included income not in the selected tag(s)',
+ 'include_expense_not_in_tags' => 'Included expenses not in the selected tag(s)',
'everything_else' => 'Everything else',
'income_and_expenses' => 'Income and expenses',
'spent_average' => 'Spent (average)',
diff --git a/resources/views/reports/tag/month.twig b/resources/views/reports/tag/month.twig
index 4ba5515c30..9f27985c04 100644
--- a/resources/views/reports/tag/month.twig
+++ b/resources/views/reports/tag/month.twig
@@ -81,7 +81,6 @@
</div>
<div class="box-body">
<canvas id="budgets-out-pie-chart" style="margin:0 auto;" height="150" width="150"></canvas>
- Uitgaven per budget voor de gevonden transacties
</div>
</div>
</div>
@@ -160,7 +159,6 @@
</div>
<div class="box-body">
<canvas id="categories-out-pie-chart" style="margin:0 auto;" height="150" width="150"></canvas>
- Uitgaven per category voor de gevonden transacties
</div>
</div>
</div>