From 590591f6bd4c349cc3aeb70a4b550a28b7f65ecd Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 19 Oct 2020 18:44:52 +0200 Subject: [PATCH] Fix #3968 --- app/Http/Controllers/Bill/IndexController.php | 46 +++++++++++++++++-- resources/lang/en_US/firefly.php | 2 + resources/views/v1/list/bills.twig | 39 ++++++++++++++++ 3 files changed, 84 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Bill/IndexController.php b/app/Http/Controllers/Bill/IndexController.php index c084dfa90d..4d9bc7dc1c 100644 --- a/app/Http/Controllers/Bill/IndexController.php +++ b/app/Http/Controllers/Bill/IndexController.php @@ -43,6 +43,7 @@ use Symfony\Component\HttpFoundation\ParameterBag; class IndexController extends Controller { use OrganisesObjectGroups; + private BillRepositoryInterface $repository; /** @@ -139,9 +140,10 @@ class IndexController extends Controller ksort($bills); // summarise per currency / per group. - $sums = $this->getSums($bills); + $sums = $this->getSums($bills); + $totals = $this->getTotals($sums); - return view('bills.index', compact('bills', 'sums', 'total')); + return view('bills.index', compact('bills', 'sums', 'total', 'totals')); } @@ -238,7 +240,7 @@ class IndexController extends Controller */ public function setOrder(Request $request, Bill $bill): JsonResponse { - $objectGroupTitle = (string)$request->get('objectGroupTitle'); + $objectGroupTitle = (string) $request->get('objectGroupTitle'); $newOrder = (int) $request->get('order'); $this->repository->setOrder($bill, $newOrder); if ('' !== $objectGroupTitle) { @@ -250,4 +252,42 @@ class IndexController extends Controller return response()->json(['data' => 'OK']); } + + /** + * @param array $sums + * @return array + */ + private function getTotals(array $sums): array + { + $totals = []; + if (count($sums) < 2) { + return []; + } + /** + * @var int $objectGroupId + * @var array $array + */ + foreach ($sums as $objectGroupId => $array) { + /** + * @var int $currencyId + * @var array $entry + */ + foreach ($array as $currencyId => $entry) { + $totals[$currencyId] = $totals[$currencyId] ?? [ + 'currency_id' => $currencyId, + 'currency_code' => $entry['currency_code'], + 'currency_name' => $entry['currency_name'], + 'currency_symbol' => $entry['currency_symbol'], + 'currency_decimal_places' => $entry['currency_decimal_places'], + 'avg' => '0', + 'period' => $entry['period'], + 'per_period' => '0', + ]; + $totals[$currencyId]['avg'] = bcadd($totals[$currencyId]['avg'], $entry['avg']); + $totals[$currencyId]['per_period'] = bcadd($totals[$currencyId]['per_period'], $entry['per_period']); + } + } + + return $totals; + } } diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 830e4a6b68..e735000695 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -204,7 +204,9 @@ return [ 'button_register' => 'Register', 'authorization' => 'Authorization', 'active_bills_only' => 'active bills only', + 'active_bills_only_total' => 'all active bills', 'active_exp_bills_only' => 'active and expected bills only', + 'active_exp_bills_only_total' => 'all active expected bills only', 'per_period_sum_1D' => 'Expected daily costs', 'per_period_sum_1W' => 'Expected weekly costs', 'per_period_sum_1M' => 'Expected monthly costs', diff --git a/resources/views/v1/list/bills.twig b/resources/views/v1/list/bills.twig index 9be7dc45fa..2f59864bd0 100644 --- a/resources/views/v1/list/bills.twig +++ b/resources/views/v1/list/bills.twig @@ -164,6 +164,45 @@ {% endif %} {% endfor %} + {% if totals|length > 0 %} + + + + + {% for sum in totals %} + {% if '0' != sum.avg %} + +   +   + + {{ 'sum'|_ }} ({{ sum.currency_name }}) ({{ 'active_exp_bills_only_total'|_ }}) + + + {{ formatAmountBySymbol(sum.avg, sum.currency_symbol, sum.currency_decimal_places) }} + +   +   +   + + {% endif %} + {% if '0' != sum.per_period %} + +   +   + + {{ ('per_period_sum_'~sum.period)|_ }} ({{ sum.currency_name }}) ({{ 'active_bills_only_total'|_ }}) + + + {{ formatAmountBySymbol(sum.per_period, sum.currency_symbol, sum.currency_decimal_places) }} + +   +   +   + + {% endif %} + {% endfor %} + + {% endif %}