From 50837af607ff89cc44a913b148423fcf17996b0d Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 26 Aug 2017 14:50:08 +0200 Subject: [PATCH] Expand tag view. --- app/Http/Controllers/TagController.php | 7 +++-- app/Repositories/Tag/TagRepository.php | 28 +++++++++++++++++++ .../Tag/TagRepositoryInterface.php | 9 ++++++ resources/lang/en_US/firefly.php | 1 + resources/views/tags/show.twig | 2 ++ 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php index 3a7f9dd1c4..f4ecb7f429 100644 --- a/app/Http/Controllers/TagController.php +++ b/app/Http/Controllers/TagController.php @@ -171,7 +171,6 @@ class TagController extends Controller } $count = $repository->count(); - return view('tags.index', compact('clouds', 'count')); } @@ -205,6 +204,7 @@ class TagController extends Controller $start = $repository->firstUseDate($tag); $end = new Carbon; $sum = $repository->sumOfTag($tag, null, null); + $result = $repository->resultOfTag($tag, null, null); $path = route('tags.show', [$tag->id, 'all']); } @@ -219,6 +219,7 @@ class TagController extends Controller ); $periods = $this->getPeriodOverview($tag); $sum = $repository->sumOfTag($tag, $start, $end); + $result = $repository->resultOfTag($tag, $start, $end); $path = route('tags.show', [$tag->id, $moment]); } @@ -227,6 +228,8 @@ class TagController extends Controller $start = clone session('start', Navigation::startOfPeriod(new Carbon, $range)); $end = clone session('end', Navigation::endOfPeriod(new Carbon, $range)); $periods = $this->getPeriodOverview($tag); + $sum = $repository->sumOfTag($tag, $start, $end); + $result = $repository->resultOfTag($tag, $start, $end); $subTitle = trans( 'firefly.journals_in_period_for_tag', ['tag' => $tag->tag, 'start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)] @@ -241,7 +244,7 @@ class TagController extends Controller $journals->setPath($path); - return view('tags.show', compact('apiKey', 'tag', 'periods', 'subTitle', 'subTitleIcon', 'journals', 'sum', 'start', 'end', 'moment')); + return view('tags.show', compact('apiKey', 'tag', 'result', 'periods', 'subTitle', 'subTitleIcon', 'journals', 'sum', 'start', 'end', 'moment')); } /** diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index 4a5b350a27..3f3218da19 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -262,6 +262,34 @@ class TagRepository implements TagRepositoryInterface return strval($sum); } + /** + * Same as sum of tag but substracts income instead of adding it as well. + * + * @param Tag $tag + * @param Carbon|null $start + * @param Carbon|null $end + * + * @return string + */ + public function resultOfTag(Tag $tag, ?Carbon $start, ?Carbon $end): string + { + /** @var JournalCollectorInterface $collector */ + $collector = app(JournalCollectorInterface::class); + + if (!is_null($start) && !is_null($end)) { + $collector->setRange($start, $end); + } + + $collector->setAllAssetAccounts()->setTag($tag); + $journals = $collector->getJournals(); + $sum = '0'; + foreach ($journals as $journal) { + $sum = bcadd($sum, strval($journal->transaction_amount)); + } + + return strval($sum); + } + /** * Generates a tag cloud. * diff --git a/app/Repositories/Tag/TagRepositoryInterface.php b/app/Repositories/Tag/TagRepositoryInterface.php index 6ed8ec4edd..85e54f58c7 100644 --- a/app/Repositories/Tag/TagRepositoryInterface.php +++ b/app/Repositories/Tag/TagRepositoryInterface.php @@ -108,6 +108,15 @@ interface TagRepositoryInterface */ public function setUser(User $user); + /** + * @param Tag $tag + * @param Carbon|null $start + * @param Carbon|null $end + * + * @return string + */ + public function resultOfTag(Tag $tag, ?Carbon $start, ?Carbon $end): string; + /** * @param Tag $tag * @param Carbon $start diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 4c4cc20289..73f0f9434e 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -311,6 +311,7 @@ return [ 'meta_data' => 'Meta data', 'location' => 'Location', 'without_date' => 'Without date', + 'result' => 'Result', // preferences 'pref_home_screen_accounts' => 'Home screen accounts', diff --git a/resources/views/tags/show.twig b/resources/views/tags/show.twig index acd4a376ad..dd99386bdd 100644 --- a/resources/views/tags/show.twig +++ b/resources/views/tags/show.twig @@ -40,10 +40,12 @@ {% if moment == 'all' %}

{{ 'total_sum'|_ }}: {{ sum|formatAmount }}
+ {{ 'total_result'|_ }}: {{ result|formatAmount }}

{% else %}

{{ 'sum'|_ }}: {{ sum|formatAmount }}
+ {{ 'result'|_ }}: {{ result|formatAmount }}

{% endif %}