Files
firefly-iii/app/Support/Http/Controllers/PeriodOverview.php

615 lines
25 KiB
PHP
Raw Normal View History

<?php
/**
* PeriodOverview.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Http\Controllers;
use Carbon\Carbon;
2025-01-03 09:05:19 +01:00
use FireflyIII\Enums\TransactionTypeEnum;
2021-05-24 08:57:02 +02:00
use FireflyIII\Exceptions\FireflyException;
2019-05-29 18:28:28 +02:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\Category;
use FireflyIII\Models\Tag;
2025-03-09 09:54:06 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2021-04-06 17:00:16 +02:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Support\CacheProperties;
2025-03-09 09:54:06 +01:00
use FireflyIII\Support\Debug\Timer;
use Illuminate\Support\Collection;
2025-03-09 09:54:06 +01:00
use Illuminate\Support\Facades\Log;
/**
* Trait PeriodOverview.
*
2022-10-30 11:43:17 +01:00
* TODO verify this all works as expected.
2018-12-31 07:58:13 +01:00
*
* - Always request start date and end date.
* - Group expenses, income, etc. under this period.
* - Returns collection of arrays. Fields
2018-09-10 20:24:19 +02:00
* title (string),
* route (string)
* total_transactions (int)
* spent (array),
* earned (array),
* transferred_away (array)
* transferred_in (array)
* transferred (array)
*
* each array has the following format:
* currency_id => [
* currency_id : 1, (int)
* currency_symbol : X (str)
* currency_name: Euro (str)
* currency_code: EUR (str)
* amount: -1234 (str)
* count: 23
* ]
*/
trait PeriodOverview
{
2021-04-06 17:00:16 +02:00
protected JournalRepositoryInterface $journalRepos;
2025-03-09 09:54:06 +01:00
protected AccountRepositoryInterface $accountRepository;
2018-12-31 07:58:13 +01:00
/**
* This method returns "period entries", so nov-2015, dec-2015, etc etc (this depends on the users session range)
* and for each period, the amount of money spent and earned. This is a complex operation which is cached for
* performance reasons.
*
2021-05-24 08:57:02 +02:00
* @throws FireflyException
*/
protected function getAccountPeriodOverview(Account $account, Carbon $start, Carbon $end): array
{
2025-03-09 09:54:06 +01:00
Timer::start('account-period-total');
$this->accountRepository = app(AccountRepositoryInterface::class);
$range = app('navigation')->getViewRange(true);
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
// properties for cache
2025-03-09 09:54:06 +01:00
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('account-show-period-entries');
$cache->addProperty($account->id);
if ($cache->has()) {
2023-02-11 07:39:00 +01:00
return $cache->get();
}
2023-12-20 19:35:52 +01:00
/** @var array $dates */
2025-03-09 09:54:06 +01:00
$dates = app('navigation')->blockPeriods($start, $end, $range);
$entries = [];
2025-03-09 09:54:06 +01:00
// run a custom query because doing this with the collector is MEGA slow.
$transactions = $this->accountRepository->periodCollection($account, $start, $end);
// loop dates
foreach ($dates as $currentDate) {
$title = app('navigation')->periodShow($currentDate['start'], $currentDate['period']);
2025-03-09 09:54:06 +01:00
[$transactions, $spent] = $this->filterTransactionsByType(TransactionTypeEnum::WITHDRAWAL, $transactions, $currentDate['start'], $currentDate['end']);
[$transactions, $earned] = $this->filterTransactionsByType(TransactionTypeEnum::DEPOSIT, $transactions, $currentDate['start'], $currentDate['end']);
[$transactions, $transferredAway] = $this->filterTransfers('away',$transactions, $currentDate['start'], $currentDate['end']);
[$transactions, $transferredIn] = $this->filterTransfers('in',$transactions, $currentDate['start'], $currentDate['end']);
2021-03-21 09:15:40 +01:00
$entries[]
2025-03-09 09:54:06 +01:00
= [
'title' => $title,
'route' => route('accounts.show', [$account->id, $currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]),
'total_transactions' => count($spent) + count($earned) + count($transferredAway) + count($transferredIn),
'spent' => $this->groupByCurrency($spent),
'earned' => $this->groupByCurrency($earned),
'transferred_away' => $this->groupByCurrency($transferredAway),
'transferred_in' => $this->groupByCurrency($transferredIn),
];
}
$cache->store($entries);
2025-03-09 09:54:06 +01:00
Timer::stop('account-period-total');
return $entries;
}
2025-03-09 09:54:06 +01:00
private function filterTransfers(string $direction, array $transactions, Carbon $start, Carbon $end): array
{
$result = [];
/**
* @var int $index
* @var array $item
*/
foreach ($transactions as $index => $item) {
$date = Carbon::parse($item['date']);
if ($date >= $start && $date <= $end) {
if ($direction === 'away' && bccomp($item['amount'], '0') === -1) {
$result[] = $item;
unset($transactions[$index]);
}
if ($direction === 'in' && bccomp($item['amount'], '0') === 1) {
$result[] = $item;
unset($transactions[$index]);
}
}
}
return [$transactions, $result];
}
private function filterTransactionsByType(TransactionTypeEnum $type, array $transactions, Carbon $start, Carbon $end): array
{
$result = [];
/**
* @var int $index
* @var array $item
*/
foreach ($transactions as $index => $item) {
$date = Carbon::parse($item['date']);
if($item['type'] === $type->value && $date >= $start && $date <= $end) {
$result[] = $item;
unset($transactions[$index]);
}
}
return [$transactions, $result];
}
/**
* Filter a list of journals by a set of dates, and then group them by currency.
*/
private function filterJournalsByDate(array $array, Carbon $start, Carbon $end): array
{
$result = [];
/** @var array $journal */
foreach ($array as $journal) {
if ($journal['date'] <= $end && $journal['date'] >= $start) {
$result[] = $journal;
}
}
return $result;
}
/**
* Return only transactions where $account is the source.
*/
private function filterTransferredAway(Account $account, array $journals): array
{
$return = [];
/** @var array $journal */
foreach ($journals as $journal) {
2024-12-22 08:43:12 +01:00
if ($account->id === (int) $journal['source_account_id']) {
$return[] = $journal;
}
}
return $return;
}
/**
* Return only transactions where $account is the source.
*/
private function filterTransferredIn(Account $account, array $journals): array
{
$return = [];
/** @var array $journal */
foreach ($journals as $journal) {
2024-12-22 08:43:12 +01:00
if ($account->id === (int) $journal['destination_account_id']) {
$return[] = $journal;
}
}
return $return;
}
private function groupByCurrency(array $journals): array
{
$return = [];
/** @var array $journal */
foreach ($journals as $journal) {
2025-03-09 09:54:06 +01:00
$currencyId = (int) $journal['currency_id'];
$currencyCode = $journal['currency_code'];
$currencyName = $journal['currency_name'];
$currencySymbol = $journal['currency_symbol'];
$currencyDecimalPlaces = $journal['currency_decimal_places'];
$foreignCurrencyId = $journal['foreign_currency_id'];
$amount = $journal['amount'] ?? '0';
2024-12-27 16:36:01 +01:00
if ($this->convertToNative && $currencyId !== $this->defaultCurrency->id && $foreignCurrencyId !== $this->defaultCurrency->id) {
$amount = $journal['native_amount'] ?? '0';
2024-12-27 16:36:01 +01:00
$currencyId = $this->defaultCurrency->id;
$currencyCode = $this->defaultCurrency->code;
$currencyName = $this->defaultCurrency->name;
$currencySymbol = $this->defaultCurrency->symbol;
$currencyDecimalPlaces = $this->defaultCurrency->decimal_places;
}
2024-12-27 16:36:01 +01:00
if ($this->convertToNative && $currencyId !== $this->defaultCurrency->id && $foreignCurrencyId === $this->defaultCurrency->id) {
$currencyId = (int) $foreignCurrencyId;
2024-12-27 16:36:01 +01:00
$currencyCode = $journal['foreign_currency_code'];
$currencyName = $journal['foreign_currency_name'];
$currencySymbol = $journal['foreign_currency_symbol'];
$currencyDecimalPlaces = $journal['foreign_currency_decimal_places'];
$amount = $journal['foreign_amount'] ?? '0';
}
2024-12-27 16:36:01 +01:00
$return[$currencyId] ??= [
'amount' => '0',
'count' => 0,
'currency_id' => $currencyId,
'currency_name' => $currencyName,
'currency_code' => $currencyCode,
'currency_symbol' => $currencySymbol,
'currency_decimal_places' => $currencyDecimalPlaces,
];
$return[$currencyId]['amount'] = bcadd($return[$currencyId]['amount'], $amount);
++$return[$currencyId]['count'];
}
return $return;
}
2021-03-21 09:15:40 +01:00
/**
* Overview for single category. Has been refactored recently.
*
2021-05-24 08:57:02 +02:00
* @throws FireflyException
*/
protected function getCategoryPeriodOverview(Category $category, Carbon $start, Carbon $end): array
{
2025-03-09 09:54:06 +01:00
$range = app('navigation')->getViewRange(true);
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
2018-10-07 09:45:50 +02:00
// properties for entries with their amounts.
2025-03-09 09:54:06 +01:00
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
2018-10-07 09:45:50 +02:00
$cache->addProperty($range);
$cache->addProperty('category-show-period-entries');
$cache->addProperty($category->id);
if ($cache->has()) {
2021-07-04 19:19:31 +02:00
return $cache->get();
}
2023-12-20 19:35:52 +01:00
2018-09-10 20:24:19 +02:00
/** @var array $dates */
2025-03-09 09:54:06 +01:00
$dates = app('navigation')->blockPeriods($start, $end, $range);
$entries = [];
2018-10-07 09:45:50 +02:00
// collect all expenses in this period:
/** @var GroupCollectorInterface $collector */
2025-03-09 09:54:06 +01:00
$collector = app(GroupCollectorInterface::class);
$collector->setCategory($category);
$collector->setRange($start, $end);
2025-01-03 09:09:15 +01:00
$collector->setTypes([TransactionTypeEnum::DEPOSIT->value]);
2025-03-09 09:54:06 +01:00
$earnedSet = $collector->getExtractedJournals();
2018-10-07 09:45:50 +02:00
// collect all income in this period:
/** @var GroupCollectorInterface $collector */
2025-03-09 09:54:06 +01:00
$collector = app(GroupCollectorInterface::class);
$collector->setCategory($category);
$collector->setRange($start, $end);
2025-01-03 09:05:19 +01:00
$collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value]);
2025-03-09 09:54:06 +01:00
$spentSet = $collector->getExtractedJournals();
// collect all transfers in this period:
/** @var GroupCollectorInterface $collector */
2025-03-09 09:54:06 +01:00
$collector = app(GroupCollectorInterface::class);
$collector->setCategory($category);
$collector->setRange($start, $end);
2025-01-03 09:05:56 +01:00
$collector->setTypes([TransactionTypeEnum::TRANSFER->value]);
2025-03-09 09:54:06 +01:00
$transferSet = $collector->getExtractedJournals();
foreach ($dates as $currentDate) {
$spent = $this->filterJournalsByDate($spentSet, $currentDate['start'], $currentDate['end']);
$earned = $this->filterJournalsByDate($earnedSet, $currentDate['start'], $currentDate['end']);
$transferred = $this->filterJournalsByDate($transferSet, $currentDate['start'], $currentDate['end']);
$title = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
2021-03-21 09:15:40 +01:00
$entries[]
= [
2025-03-09 09:54:06 +01:00
'transactions' => 0,
'title' => $title,
'route' => route(
'categories.show',
[$category->id, $currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]
),
'total_transactions' => count($spent) + count($earned) + count($transferred),
'spent' => $this->groupByCurrency($spent),
'earned' => $this->groupByCurrency($earned),
'transferred' => $this->groupByCurrency($transferred),
];
}
$cache->store($entries);
return $entries;
}
/**
2018-10-07 09:45:50 +02:00
* Same as above, but for lists that involve transactions without a budget.
*
2018-10-07 09:45:50 +02:00
* This method has been refactored recently.
*
2021-05-24 08:57:02 +02:00
* @throws FireflyException
*/
protected function getNoBudgetPeriodOverview(Carbon $start, Carbon $end): array
{
2025-03-09 09:54:06 +01:00
$range = app('navigation')->getViewRange(true);
2018-09-10 20:24:19 +02:00
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
2025-03-09 09:54:06 +01:00
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
2024-12-27 16:36:01 +01:00
$cache->addProperty($this->convertToNative);
2018-10-07 09:45:50 +02:00
$cache->addProperty('no-budget-period-entries');
if ($cache->has()) {
return $cache->get();
}
2018-10-07 09:45:50 +02:00
/** @var array $dates */
2025-03-09 09:54:06 +01:00
$dates = app('navigation')->blockPeriods($start, $end, $range);
$entries = [];
2023-12-20 19:35:52 +01:00
// get all expenses without a budget.
/** @var GroupCollectorInterface $collector */
2025-03-09 09:54:06 +01:00
$collector = app(GroupCollectorInterface::class);
2025-01-03 09:05:19 +01:00
$collector->setRange($start, $end)->withoutBudget()->withAccountInformation()->setTypes([TransactionTypeEnum::WITHDRAWAL->value]);
2025-03-09 09:54:06 +01:00
$journals = $collector->getExtractedJournals();
foreach ($dates as $currentDate) {
2021-03-21 09:15:40 +01:00
$set = $this->filterJournalsByDate($journals, $currentDate['start'], $currentDate['end']);
$title = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
$entries[]
= [
2025-03-09 09:54:06 +01:00
'title' => $title,
'route' => route('budgets.no-budget', [$currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]),
'total_transactions' => count($set),
'spent' => $this->groupByCurrency($set),
'earned' => [],
'transferred_away' => [],
'transferred_in' => [],
];
}
$cache->store($entries);
return $entries;
}
2018-12-31 07:58:13 +01:00
/**
2022-10-30 11:43:17 +01:00
* TODO fix the date.
*
2018-12-31 07:58:13 +01:00
* Show period overview for no category view.
*
2021-05-24 08:57:02 +02:00
* @throws FireflyException
2018-12-31 07:58:13 +01:00
*/
protected function getNoCategoryPeriodOverview(Carbon $theDate): array
2018-12-31 07:58:13 +01:00
{
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Now in getNoCategoryPeriodOverview(%s)', $theDate->format('Y-m-d')));
2025-03-09 09:54:06 +01:00
$range = app('navigation')->getViewRange(true);
$first = $this->journalRepos->firstNull();
$start = null === $first ? new Carbon() : $first->date;
$end = clone $theDate;
$end = app('navigation')->endOfPeriod($end, $range);
2018-12-31 07:58:13 +01:00
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Start for getNoCategoryPeriodOverview() is %s', $start->format('Y-m-d')));
app('log')->debug(sprintf('End for getNoCategoryPeriodOverview() is %s', $end->format('Y-m-d')));
2018-12-31 07:58:13 +01:00
// properties for cache
2025-03-09 09:54:06 +01:00
$dates = app('navigation')->blockPeriods($start, $end, $range);
$entries = [];
// collect all expenses in this period:
/** @var GroupCollectorInterface $collector */
2025-03-09 09:54:06 +01:00
$collector = app(GroupCollectorInterface::class);
$collector->withoutCategory();
$collector->setRange($start, $end);
2025-01-03 09:09:15 +01:00
$collector->setTypes([TransactionTypeEnum::DEPOSIT->value]);
2025-03-09 09:54:06 +01:00
$earnedSet = $collector->getExtractedJournals();
// collect all income in this period:
/** @var GroupCollectorInterface $collector */
2025-03-09 09:54:06 +01:00
$collector = app(GroupCollectorInterface::class);
$collector->withoutCategory();
$collector->setRange($start, $end);
2025-01-03 09:05:19 +01:00
$collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value]);
2025-03-09 09:54:06 +01:00
$spentSet = $collector->getExtractedJournals();
// collect all transfers in this period:
/** @var GroupCollectorInterface $collector */
2025-03-09 09:54:06 +01:00
$collector = app(GroupCollectorInterface::class);
$collector->withoutCategory();
$collector->setRange($start, $end);
2025-01-03 09:05:56 +01:00
$collector->setTypes([TransactionTypeEnum::TRANSFER->value]);
$transferSet = $collector->getExtractedJournals();
/** @var array $currentDate */
foreach ($dates as $currentDate) {
$spent = $this->filterJournalsByDate($spentSet, $currentDate['start'], $currentDate['end']);
$earned = $this->filterJournalsByDate($earnedSet, $currentDate['start'], $currentDate['end']);
$transferred = $this->filterJournalsByDate($transferSet, $currentDate['start'], $currentDate['end']);
$title = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
2021-03-21 09:15:40 +01:00
$entries[]
= [
2025-03-09 09:54:06 +01:00
'title' => $title,
'route' => route('categories.no-category', [$currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]),
'total_transactions' => count($spent) + count($earned) + count($transferred),
'spent' => $this->groupByCurrency($spent),
'earned' => $this->groupByCurrency($earned),
'transferred' => $this->groupByCurrency($transferred),
];
2018-12-31 07:58:13 +01:00
}
2023-10-29 06:33:43 +01:00
app('log')->debug('End of loops');
2018-12-31 07:58:13 +01:00
return $entries;
}
/**
2018-10-07 09:45:50 +02:00
* This shows a period overview for a tag. It goes back in time and lists all relevant transactions and sums.
*
2021-05-24 08:57:02 +02:00
* @throws FireflyException
*/
2019-07-22 19:23:14 +02:00
protected function getTagPeriodOverview(Tag $tag, Carbon $start, Carbon $end): array // period overview for tags.
{
2025-03-09 09:54:06 +01:00
$range = app('navigation')->getViewRange(true);
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
2018-10-07 09:45:50 +02:00
2019-07-22 19:23:14 +02:00
// properties for cache
2025-03-09 09:54:06 +01:00
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
2018-10-07 09:45:50 +02:00
$cache->addProperty('tag-period-entries');
$cache->addProperty($tag->id);
if ($cache->has()) {
return $cache->get();
}
2023-12-20 19:35:52 +01:00
2018-10-07 09:45:50 +02:00
/** @var array $dates */
2025-03-09 09:54:06 +01:00
$dates = app('navigation')->blockPeriods($start, $end, $range);
$entries = [];
2018-10-07 09:45:50 +02:00
2019-07-22 19:23:14 +02:00
// collect all expenses in this period:
/** @var GroupCollectorInterface $collector */
2025-03-09 09:54:06 +01:00
$collector = app(GroupCollectorInterface::class);
2019-07-22 19:23:14 +02:00
$collector->setTag($tag);
$collector->setRange($start, $end);
2025-01-03 09:09:15 +01:00
$collector->setTypes([TransactionTypeEnum::DEPOSIT->value]);
2025-03-09 09:54:06 +01:00
$earnedSet = $collector->getExtractedJournals();
2018-10-07 09:45:50 +02:00
2019-07-22 19:23:14 +02:00
// collect all income in this period:
/** @var GroupCollectorInterface $collector */
2025-03-09 09:54:06 +01:00
$collector = app(GroupCollectorInterface::class);
2019-07-22 19:23:14 +02:00
$collector->setTag($tag);
$collector->setRange($start, $end);
2025-01-03 09:05:19 +01:00
$collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value]);
2025-03-09 09:54:06 +01:00
$spentSet = $collector->getExtractedJournals();
2018-10-07 09:45:50 +02:00
2019-07-22 19:23:14 +02:00
// collect all transfers in this period:
/** @var GroupCollectorInterface $collector */
2025-03-09 09:54:06 +01:00
$collector = app(GroupCollectorInterface::class);
2019-07-22 19:23:14 +02:00
$collector->setTag($tag);
$collector->setRange($start, $end);
2025-01-03 09:05:56 +01:00
$collector->setTypes([TransactionTypeEnum::TRANSFER->value]);
2025-03-09 09:54:06 +01:00
$transferSet = $collector->getExtractedJournals();
2019-07-22 19:23:14 +02:00
2024-01-01 08:17:15 +01:00
// filer all of them:
2025-03-09 09:54:06 +01:00
$earnedSet = $this->filterJournalsByTag($earnedSet, $tag);
$spentSet = $this->filterJournalsByTag($spentSet, $tag);
$transferSet = $this->filterJournalsByTag($transferSet, $tag);
2024-01-01 08:17:15 +01:00
2019-07-22 19:23:14 +02:00
foreach ($dates as $currentDate) {
$spent = $this->filterJournalsByDate($spentSet, $currentDate['start'], $currentDate['end']);
$earned = $this->filterJournalsByDate($earnedSet, $currentDate['start'], $currentDate['end']);
$transferred = $this->filterJournalsByDate($transferSet, $currentDate['start'], $currentDate['end']);
$title = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
2021-03-21 09:15:40 +01:00
$entries[]
= [
2025-03-09 09:54:06 +01:00
'transactions' => 0,
'title' => $title,
'route' => route(
'tags.show',
[$tag->id, $currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]
),
'total_transactions' => count($spent) + count($earned) + count($transferred),
'spent' => $this->groupByCurrency($spent),
'earned' => $this->groupByCurrency($earned),
'transferred' => $this->groupByCurrency($transferred),
];
}
2018-10-07 09:45:50 +02:00
return $entries;
}
private function filterJournalsByTag(array $set, Tag $tag): array
{
$return = [];
foreach ($set as $entry) {
2025-03-09 09:54:06 +01:00
$found = false;
/** @var array $localTag */
foreach ($entry['tags'] as $localTag) {
if ($localTag['id'] === $tag->id) {
$found = true;
}
}
if (false === $found) {
continue;
}
$return[] = $entry;
}
return $return;
}
/**
2021-05-24 08:57:02 +02:00
* @throws FireflyException
*/
protected function getTransactionPeriodOverview(string $transactionType, Carbon $start, Carbon $end): array
{
2025-03-09 09:54:06 +01:00
$range = app('navigation')->getViewRange(true);
$types = config(sprintf('firefly.transactionTypesByType.%s', $transactionType));
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
2018-10-07 09:45:50 +02:00
// properties for cache
2025-03-09 09:54:06 +01:00
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('transactions-period-entries');
$cache->addProperty($transactionType);
if ($cache->has()) {
2021-07-04 19:19:31 +02:00
return $cache->get();
}
2023-12-20 19:35:52 +01:00
/** @var array $dates */
2025-03-09 09:54:06 +01:00
$dates = app('navigation')->blockPeriods($start, $end, $range);
$entries = [];
// collect all journals in this period (regardless of type)
2025-03-09 09:54:06 +01:00
$collector = app(GroupCollectorInterface::class);
$collector->setTypes($types)->setRange($start, $end);
2025-03-09 09:54:06 +01:00
$genericSet = $collector->getExtractedJournals();
2019-06-21 19:10:02 +02:00
foreach ($dates as $currentDate) {
2019-04-19 07:00:19 +02:00
$spent = [];
$earned = [];
$transferred = [];
$title = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
2019-04-19 07:00:19 +02:00
// set to correct array
if ('expenses' === $transactionType || 'withdrawal' === $transactionType) {
$spent = $this->filterJournalsByDate($genericSet, $currentDate['start'], $currentDate['end']);
}
2019-04-19 07:00:19 +02:00
if ('revenue' === $transactionType || 'deposit' === $transactionType) {
$earned = $this->filterJournalsByDate($genericSet, $currentDate['start'], $currentDate['end']);
2018-10-07 09:45:50 +02:00
}
2019-04-19 07:00:19 +02:00
if ('transfer' === $transactionType || 'transfers' === $transactionType) {
$transferred = $this->filterJournalsByDate($genericSet, $currentDate['start'], $currentDate['end']);
2018-10-07 09:45:50 +02:00
}
2021-03-21 09:15:40 +01:00
$entries[]
2025-03-09 09:54:06 +01:00
= [
'title' => $title,
'route' => route('transactions.index', [$transactionType, $currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]),
'total_transactions' => count($spent) + count($earned) + count($transferred),
'spent' => $this->groupByCurrency($spent),
'earned' => $this->groupByCurrency($earned),
'transferred' => $this->groupByCurrency($transferred),
];
2024-01-01 08:17:15 +01:00
}
return $entries;
2024-01-01 08:17:15 +01:00
}
2018-12-31 07:48:23 +01:00
}