From 260ef1a07e78026ca807b1871d4622f820475b72 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 24 Sep 2017 21:18:43 +0200 Subject: [PATCH] Tag view update. --- app/Http/Controllers/TagController.php | 13 +- app/Repositories/Tag/TagRepository.php | 65 ++++---- .../Tag/TagRepositoryInterface.php | 20 +-- resources/lang/en_US/firefly.php | 1 + resources/lang/en_US/list.php | 148 +++++++++--------- resources/views/tags/show.twig | 76 +++++---- 6 files changed, 177 insertions(+), 146 deletions(-) diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php index 2ba789f0c8..018fa5a428 100644 --- a/app/Http/Controllers/TagController.php +++ b/app/Http/Controllers/TagController.php @@ -194,17 +194,13 @@ class TagController extends Controller $end = null; $periods = new Collection; $apiKey = env('GOOGLE_MAPS_API_KEY', ''); - $sum = '0'; $path = route('tags.show', [$tag->id]); - // prep for "all" view. if ($moment === 'all') { $subTitle = trans('firefly.all_journals_for_tag', ['tag' => $tag->tag]); $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']); } @@ -218,20 +214,16 @@ class TagController extends Controller 'start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)] ); $periods = $this->getPeriodOverview($tag); - $sum = $repository->sumOfTag($tag, $start, $end); - $result = $repository->resultOfTag($tag, $start, $end); $path = route('tags.show', [$tag->id, $moment]); } // prep for current period if (strlen($moment) === 0) { /** @var Carbon $start */ - $start = clone session('start', Navigation::startOfPeriod(new Carbon, $range)); + $start = clone session('start', Navigation::startOfPeriod(new Carbon, $range)); /** @var Carbon $end */ $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)] @@ -245,8 +237,9 @@ class TagController extends Controller $journals = $collector->getPaginatedJournals(); $journals->setPath($path); + $sums = $repository->sumsOfTag($tag, $start, $end); - return view('tags.show', compact('apiKey', 'tag', 'result', 'periods', 'subTitle', 'subTitleIcon', 'journals', 'sum', 'start', 'end', 'moment')); + return view('tags.show', compact('apiKey', 'tag', 'sums', 'periods', 'subTitle', 'subTitleIcon', 'journals', 'start', 'end', 'moment')); } /** diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index bc0b780065..9593851b99 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -186,34 +186,6 @@ class TagRepository implements TagRepositoryInterface return new Carbon; } - /** - * 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); - } - /** * @param User $user */ @@ -290,6 +262,43 @@ class TagRepository implements TagRepositoryInterface return strval($sum); } + /** + * @param Tag $tag + * @param Carbon|null $start + * @param Carbon|null $end + * + * @return string + */ + public function sumsOfTag(Tag $tag, ?Carbon $start, ?Carbon $end): array + { + /** @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(); + + $sums = [ + TransactionType::WITHDRAWAL => '0', + TransactionType::DEPOSIT => '0', + TransactionType::TRANSFER => '0', + ]; + + foreach ($journals as $journal) { + $amount = app('steam')->positive(strval($journal->transaction_amount)); + $type = $journal->transaction_type_type; + if ($type === TransactionType::WITHDRAWAL) { + $amount = bcmul($amount, '-1'); + } + $sums[$type] = bcadd($sums[$type], $amount); + } + + return $sums; + } + /** * Generates a tag cloud. * diff --git a/app/Repositories/Tag/TagRepositoryInterface.php b/app/Repositories/Tag/TagRepositoryInterface.php index 688c71dd90..0dc6772e9c 100644 --- a/app/Repositories/Tag/TagRepositoryInterface.php +++ b/app/Repositories/Tag/TagRepositoryInterface.php @@ -103,15 +103,6 @@ interface TagRepositoryInterface */ public function lastUseDate(Tag $tag): Carbon; - /** - * @param Tag $tag - * @param Carbon|null $start - * @param Carbon|null $end - * - * @return string - */ - public function resultOfTag(Tag $tag, ?Carbon $start, ?Carbon $end): string; - /** * @param User $user */ @@ -126,6 +117,17 @@ interface TagRepositoryInterface */ public function spentInPeriod(Tag $tag, Carbon $start, Carbon $end): string; + /** + * Calculates various amounts in tag. + * + * @param Tag $tag + * @param Carbon|null $start + * @param Carbon|null $end + * + * @return array + */ + public function sumsOfTag(Tag $tag, ?Carbon $start, ?Carbon $end): array; + /** * This method stores a tag. * diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 35cf398ba9..923a81740c 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -329,6 +329,7 @@ return [ 'without_date' => 'Without date', 'result' => 'Result', 'total_result' => 'Total result', + 'sums_apply_to_range' => 'All sums apply to the selected range', // preferences 'pref_home_screen_accounts' => 'Home screen accounts', diff --git a/resources/lang/en_US/list.php b/resources/lang/en_US/list.php index a9434fe3ec..e23dfbc609 100644 --- a/resources/lang/en_US/list.php +++ b/resources/lang/en_US/list.php @@ -12,76 +12,80 @@ declare(strict_types=1); */ return [ - 'buttons' => 'Buttons', - 'icon' => 'Icon', - 'id' => 'ID', - 'create_date' => 'Created at', - 'update_date' => 'Updated at', - 'balance_before' => 'Balance before', - 'balance_after' => 'Balance after', - 'name' => 'Name', - 'role' => 'Role', - 'currentBalance' => 'Current balance', - 'active' => 'Is active?', - 'lastActivity' => 'Last activity', - 'balanceDiff' => 'Balance difference between :start and :end', - 'matchesOn' => 'Matched on', - 'account_type' => 'Account type', - 'created_at' => 'Created at', - 'account' => 'Account', - 'matchingAmount' => 'Amount', - 'split_number' => 'Split #', - 'destination' => 'Destination', - 'source' => 'Source', - 'next_expected_match' => 'Next expected match', - 'automatch' => 'Auto match?', - 'repeat_freq' => 'Repeats', - 'description' => 'Description', - 'amount' => 'Amount', - 'internal_reference' => 'Internal reference', - 'date' => 'Date', - 'interest_date' => 'Interest date', - 'book_date' => 'Book date', - 'process_date' => 'Processing date', - 'due_date' => 'Due date', - 'payment_date' => 'Payment date', - 'invoice_date' => 'Invoice date', - 'interal_reference' => 'Internal reference', - 'notes' => 'Notes', - 'from' => 'From', - 'piggy_bank' => 'Piggy bank', - 'to' => 'To', - 'budget' => 'Budget', - 'category' => 'Category', - 'bill' => 'Bill', - 'withdrawal' => 'Withdrawal', - 'deposit' => 'Deposit', - 'transfer' => 'Transfer', - 'type' => 'Type', - 'completed' => 'Completed', - 'iban' => 'IBAN', - 'paid_current_period' => 'Paid this period', - 'email' => 'Email', - 'registered_at' => 'Registered at', - 'is_blocked' => 'Is blocked', - 'is_admin' => 'Is admin', - 'has_two_factor' => 'Has 2FA', - 'blocked_code' => 'Block code', - 'source_account' => 'Source account', - 'destination_account' => 'Destination account', - 'accounts_count' => 'Number of accounts', - 'journals_count' => 'Number of transactions', - 'attachments_count' => 'Number of attachments', - 'bills_count' => 'Number of bills', - 'categories_count' => 'Number of categories', - 'export_jobs_count' => 'Number of export jobs', - 'import_jobs_count' => 'Number of import jobs', - 'budget_count' => 'Number of budgets', - 'rule_and_groups_count' => 'Number of rules and rule groups', - 'tags_count' => 'Number of tags', - 'inward' => 'Inward description', - 'outward' => 'Outward description', - 'number_of_transactions' => 'Number of transactions', - 'total_amount' => 'Total amount', - + 'buttons' => 'Buttons', + 'icon' => 'Icon', + 'id' => 'ID', + 'create_date' => 'Created at', + 'update_date' => 'Updated at', + 'balance_before' => 'Balance before', + 'balance_after' => 'Balance after', + 'name' => 'Name', + 'role' => 'Role', + 'currentBalance' => 'Current balance', + 'active' => 'Is active?', + 'lastActivity' => 'Last activity', + 'balanceDiff' => 'Balance difference between :start and :end', + 'matchesOn' => 'Matched on', + 'account_type' => 'Account type', + 'created_at' => 'Created at', + 'account' => 'Account', + 'matchingAmount' => 'Amount', + 'split_number' => 'Split #', + 'destination' => 'Destination', + 'source' => 'Source', + 'next_expected_match' => 'Next expected match', + 'automatch' => 'Auto match?', + 'repeat_freq' => 'Repeats', + 'description' => 'Description', + 'amount' => 'Amount', + 'internal_reference' => 'Internal reference', + 'date' => 'Date', + 'interest_date' => 'Interest date', + 'book_date' => 'Book date', + 'process_date' => 'Processing date', + 'due_date' => 'Due date', + 'payment_date' => 'Payment date', + 'invoice_date' => 'Invoice date', + 'interal_reference' => 'Internal reference', + 'notes' => 'Notes', + 'from' => 'From', + 'piggy_bank' => 'Piggy bank', + 'to' => 'To', + 'budget' => 'Budget', + 'category' => 'Category', + 'bill' => 'Bill', + 'withdrawal' => 'Withdrawal', + 'deposit' => 'Deposit', + 'transfer' => 'Transfer', + 'type' => 'Type', + 'completed' => 'Completed', + 'iban' => 'IBAN', + 'paid_current_period' => 'Paid this period', + 'email' => 'Email', + 'registered_at' => 'Registered at', + 'is_blocked' => 'Is blocked', + 'is_admin' => 'Is admin', + 'has_two_factor' => 'Has 2FA', + 'blocked_code' => 'Block code', + 'source_account' => 'Source account', + 'destination_account' => 'Destination account', + 'accounts_count' => 'Number of accounts', + 'journals_count' => 'Number of transactions', + 'attachments_count' => 'Number of attachments', + 'bills_count' => 'Number of bills', + 'categories_count' => 'Number of categories', + 'export_jobs_count' => 'Number of export jobs', + 'import_jobs_count' => 'Number of import jobs', + 'budget_count' => 'Number of budgets', + 'rule_and_groups_count' => 'Number of rules and rule groups', + 'tags_count' => 'Number of tags', + 'inward' => 'Inward description', + 'outward' => 'Outward description', + 'number_of_transactions' => 'Number of transactions', + 'total_amount' => 'Total amount', + 'sum' => 'Sum', + 'sum_excluding_transfers' => 'Sum (excluding transfers)', + 'sum_withdrawals' => 'Sum of withdrawals', + 'sum_deposits' => 'Sum of deposits', + 'sum_transfers' => 'Sum of transfers', ]; diff --git a/resources/views/tags/show.twig b/resources/views/tags/show.twig index 2f2c8f0e93..c2a22597a4 100644 --- a/resources/views/tags/show.twig +++ b/resources/views/tags/show.twig @@ -26,34 +26,56 @@ -
- - {% if tag.description %} -

- {{ tag.description }} -

- {% endif %} - - {% if tag.date %} -

{{ 'date'|_ }}: {{ tag.date.formatLocalized(monthAndDayFormat) }}

- {% endif %} - {% if moment == 'all' %} -

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

- {% else %} -

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

- {% endif %} -
- - {{ trans('firefly.edit_tag',{tag: tag.tag}) }} - {{ trans('firefly.delete_tag',{tag: tag.tag}) }} +
+ + {% if tag.description %} + + + + + {% endif %} + {% if tag.date %} + + + + + {% endif %} + + + + + + + + + + + + + + + + + + + + +
+ {{ trans('list.description') }} + {{ tag.description }}
+ {{ trans('list.date') }} + + {{ tag.date.formatLocalized(monthAndDayFormat) }} +
{{ trans('list.sum') }} {{ (sums.Withdrawal + sums.Transfer + sums.Deposit)|formatAmount }}
{{ trans('list.sum_excluding_transfers') }} {{ (sums.Withdrawal + sums.Deposit)|formatAmount }}
{{ trans('list.sum_withdrawals') }} {{ sums.Withdrawal|formatAmount }}
{{ trans('list.sum_deposits') }} {{ sums.Deposit|formatAmount }}
{{ trans('list.sum_transfers') }} {{ sums.Transfer|formatAmount }}
+
+