From 8df6356abfdf58131143f21d7eb726a5efe48fdf Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 5 Aug 2020 18:51:17 +0200 Subject: [PATCH] Simplify frontpage. --- app/Http/Controllers/HomeController.php | 20 +++-- app/Support/Twig/TransactionGroupTwig.php | 92 ++++++++--------------- resources/views/v1/index.twig | 26 +++---- resources/views/v1/list/groups-tiny.twig | 64 ++++++++++++---- 4 files changed, 103 insertions(+), 99 deletions(-) diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 7c315ac633..60661c61c6 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -63,8 +63,8 @@ class HomeController extends Controller * * @param Request $request * - * @throws Exception * @return JsonResponse + * @throws Exception */ public function dateRange(Request $request): JsonResponse { @@ -105,8 +105,8 @@ class HomeController extends Controller * * @param AccountRepositoryInterface $repository * - * @throws Exception * @return Factory|RedirectResponse|Redirector|View + * @throws Exception */ public function index(AccountRepositoryInterface $repository) { @@ -119,10 +119,7 @@ class HomeController extends Controller } $subTitle = (string) trans('firefly.welcome_back'); $transactions = []; - $frontPage = app('preferences')->get( - 'frontPageAccounts', - $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])->pluck('id')->toArray() - ); + $frontPage = app('preferences')->get('frontPageAccounts', $repository->getAccountsByType([AccountType::ASSET])->pluck('id')->toArray()); /** @var Carbon $start */ $start = session('start', Carbon::now()->startOfMonth()); /** @var Carbon $end */ @@ -134,14 +131,15 @@ class HomeController extends Controller /** @var BillRepositoryInterface $billRepository */ $billRepository = app(BillRepositoryInterface::class); $billCount = $billRepository->getBills()->count(); + + + // collect groups for each transaction. foreach ($accounts as $account) { /** @var GroupCollectorInterface $collector */ $collector = app(GroupCollectorInterface::class); - $collector->setAccounts(new Collection([$account])) - ->withAccountInformation() - ->setRange($start, $end)->setLimit(10)->setPage(1); - $set = $collector->getGroups(); - $transactions[] = [$set, $account]; + $collector->setAccounts(new Collection([$account]))->withAccountInformation()->setRange($start, $end)->setLimit(10)->setPage(1); + $set = $collector->getExtractedJournals(); + $transactions[] = ['transactions' => $set, 'account' => $account]; } /** @var User $user */ diff --git a/app/Support/Twig/TransactionGroupTwig.php b/app/Support/Twig/TransactionGroupTwig.php index 6173121449..aaf6411e03 100644 --- a/app/Support/Twig/TransactionGroupTwig.php +++ b/app/Support/Twig/TransactionGroupTwig.php @@ -55,43 +55,6 @@ class TransactionGroupTwig extends AbstractExtension ]; } - /** - * @return TwigFunction - */ - public function groupAmount(): TwigFunction - { - return new TwigFunction( - 'groupAmount', - function (array $array): string { - $sums = $array['sums']; - $return = []; - $first = reset($array['transactions']); - $type = $first['transaction_type_type'] ?? TransactionType::WITHDRAWAL; - $colored = true; - if ($type === TransactionType::TRANSFER) { - $colored = false; - } - - - /** @var array $sum */ - foreach ($sums as $sum) { - $amount = $sum['amount']; - - $sourceType = $first['source_account_type'] ?? 'invalid'; - $amount = $this->signAmount($amount, $type, $sourceType); - - $return[] = app('amount')->formatFlat($sum['currency_symbol'], (int)$sum['currency_decimal_places'], $amount, $colored); - } - $result = implode(', ', $return); - if ($type === TransactionType::TRANSFER) { - $result = sprintf('%s', $result); - } - return $result; - }, - ['is_safe' => ['html']] - ); - } - /** * @return TwigFunction */ @@ -222,12 +185,12 @@ class TransactionGroupTwig extends AbstractExtension $colored = true; $sourceType = $array['source_account_type'] ?? 'invalid'; - $amount = $this->signAmount($amount, $type, $sourceType); - + $amount = $this->signAmount($amount, $type, $sourceType); + if ($type === TransactionType::TRANSFER) { $colored = false; } - $result = app('amount')->formatFlat($array['foreign_currency_symbol'], (int)$array['foreign_currency_decimal_places'], $amount, $colored); + $result = app('amount')->formatFlat($array['foreign_currency_symbol'], (int) $array['foreign_currency_decimal_places'], $amount, $colored); if ($type === TransactionType::TRANSFER) { $result = sprintf('%s', $result); } @@ -246,18 +209,18 @@ class TransactionGroupTwig extends AbstractExtension { $type = $journal->transactionType->type; /** @var Transaction $first */ - $first = $journal->transactions()->where('amount', '<', 0)->first(); - $currency = $first->foreignCurrency; - $amount = $first->foreign_amount ?? '0'; - $colored = true; + $first = $journal->transactions()->where('amount', '<', 0)->first(); + $currency = $first->foreignCurrency; + $amount = $first->foreign_amount ?? '0'; + $colored = true; $sourceType = $first->account()->first()->accountType()->first()->type; - + $amount = $this->signAmount($amount, $type, $sourceType); if ($type === TransactionType::TRANSFER) { $colored = false; } - $result = app('amount')->formatFlat($currency->symbol, (int)$currency->decimal_places, $amount, $colored); + $result = app('amount')->formatFlat($currency->symbol, (int) $currency->decimal_places, $amount, $colored); if ($type === TransactionType::TRANSFER) { $result = sprintf('%s', $result); } @@ -274,17 +237,17 @@ class TransactionGroupTwig extends AbstractExtension */ private function normalJournalArrayAmount(array $array): string { - $type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL; - $amount = $array['amount'] ?? '0'; - $colored = true; + $type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL; + $amount = $array['amount'] ?? '0'; + $colored = true; $sourceType = $array['source_account_type'] ?? 'invalid'; - $amount = $this->signAmount($amount, $type, $sourceType); - + $amount = $this->signAmount($amount, $type, $sourceType); + if ($type === TransactionType::TRANSFER) { $colored = false; } - $result = app('amount')->formatFlat($array['currency_symbol'], (int)$array['currency_decimal_places'], $amount, $colored); + $result = app('amount')->formatFlat($array['currency_symbol'], (int) $array['currency_decimal_places'], $amount, $colored); if ($type === TransactionType::TRANSFER) { $result = sprintf('%s', $result); } @@ -301,19 +264,19 @@ class TransactionGroupTwig extends AbstractExtension */ private function normalJournalObjectAmount(TransactionJournal $journal): string { - $type = $journal->transactionType->type; - $first = $journal->transactions()->where('amount', '<', 0)->first(); - $currency = $journal->transactionCurrency; - $amount = $first->amount ?? '0'; - $colored = true; + $type = $journal->transactionType->type; + $first = $journal->transactions()->where('amount', '<', 0)->first(); + $currency = $journal->transactionCurrency; + $amount = $first->amount ?? '0'; + $colored = true; $sourceType = $first->account()->first()->accountType()->first()->type; - + $amount = $this->signAmount($amount, $type, $sourceType); - + if ($type === TransactionType::TRANSFER) { $colored = false; } - $result = app('amount')->formatFlat($currency->symbol, (int)$currency->decimal_places, $amount, $colored); + $result = app('amount')->formatFlat($currency->symbol, (int) $currency->decimal_places, $amount, $colored); if ($type === TransactionType::TRANSFER) { $result = sprintf('%s', $result); } @@ -333,7 +296,14 @@ class TransactionGroupTwig extends AbstractExtension return null !== $first->foreign_amount; } - private function signAmount( string $amount, string $transactionType, string $sourceType ): string { + /** + * @param string $amount + * @param string $transactionType + * @param string $sourceType + * @return string + */ + private function signAmount(string $amount, string $transactionType, string $sourceType): string + { // withdrawals stay negative if ($transactionType !== TransactionType::WITHDRAWAL) { diff --git a/resources/views/v1/index.twig b/resources/views/v1/index.twig index 7672dd17b0..74ec0c20eb 100644 --- a/resources/views/v1/index.twig +++ b/resources/views/v1/index.twig @@ -54,23 +54,24 @@
+ {# TRANSACTIONS #}
{% for data in transactions %}
- {% if data[0].count > 0 %} + {% if data.transactions|length > 0 %}
- {% include 'list.groups-tiny' with {'groups': data[0],'account': data[1]} %} + {% include 'list.groups-tiny' with {'transactions': data.transactions,'account': data.account} %}
{% else %}

- {{ trans('firefly.no_transactions_account', {name: data[1].name}) }} + {{ trans('firefly.no_transactions_account', {name: data.account.name}) }}

@@ -84,23 +85,22 @@ {{ 'sidebar_frontpage_create'|_ }}
diff --git a/resources/views/v1/list/groups-tiny.twig b/resources/views/v1/list/groups-tiny.twig index 90d5b0d79b..e443ca039e 100644 --- a/resources/views/v1/list/groups-tiny.twig +++ b/resources/views/v1/list/groups-tiny.twig @@ -1,19 +1,55 @@
- {% for group in groups %} - - {% for transaction in group.transactions %} - {{ transaction.description }} - - {{ journalArrayAmount(transaction) }} - -
- {% endfor %} - {% if group.count > 1 %} -   - - {{ groupAmount(group) }} - + {% for transaction in transactions %} +
+ {% if transaction.transaction_group_title %} + {{ transaction.transaction_group_title }}: {% endif %} + {{ transaction.description }} + + + {% if transaction.transaction_type_type == 'Deposit' %} + {{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places) }} + {% if null != transaction.foreign_amount %} + ({{ formatAmountBySymbol(transaction.foreign_amount*-1, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }}) + {% endif %} + {% elseif transaction.transaction_type_type == 'Transfer' %} + + {{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places, false) }} + {% if null != transaction.foreign_amount %} + ({{ formatAmountBySymbol(transaction.foreign_amount*-1, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places, false) }}) + {% endif %} + + {% elseif transaction.transaction_type_type == 'Opening balance' %} + {% if transaction.source_account_type == 'Initial balance account' %} + {{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places) }} + {% if null != transaction.foreign_amount %} + ({{ formatAmountBySymbol(transaction.foreign_amount*-1, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }}) + {% endif %} + {% else %} + {{ formatAmountBySymbol(transaction.amount, transaction.currency_symbol, transaction.currency_decimal_places) }} + {% if null != transaction.foreign_amount %} + ({{ formatAmountBySymbol(transaction.foreign_amount, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }}) + {% endif %} + {% endif %} + {% elseif transaction.transaction_type_type == 'Reconciliation' %} + {% if transaction.source_account_type == 'Reconciliation account' %} + {{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places) }} + {% if null != transaction.foreign_amount %} + ({{ formatAmountBySymbol(transaction.foreign_amount*-1, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }}) + {% endif %} + {% else %} + {{ formatAmountBySymbol(transaction.amount, transaction.currency_symbol, transaction.currency_decimal_places) }} + {% if null != transaction.foreign_amount %} + ({{ formatAmountBySymbol(transaction.foreign_amount, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }}) + {% endif %} + {% endif %} + {% else %} + {{ formatAmountBySymbol(transaction.amount, transaction.currency_symbol, transaction.currency_decimal_places) }} + {% if null != transaction.foreign_amount %} + ({{ formatAmountBySymbol(transaction.foreign_amount, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }}) + {% endif %} + {% endif %} + {% endfor %}