Skip to content

Firefly III: a personal finances manager

Add cached periods for tags.

James Cole0f0a28c3d9d0
diff --git a/app/Models/Tag.php b/app/Models/Tag.php
index c9fc2f134b..f348bea48a 100644
--- a/app/Models/Tag.php
+++ b/app/Models/Tag.php
@@ -104,4 +104,9 @@ class Tag extends Model
'user_group_id' => 'integer',
];
}
+
+ public function primaryPeriodStatistics(): MorphMany
+ {
+ return $this->morphMany(PeriodStatistic::class, 'primary_statable');
+ }
}
diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php
index 58c998e760..06a41da184 100644
--- a/app/Repositories/Tag/TagRepository.php
+++ b/app/Repositories/Tag/TagRepository.php
@@ -379,4 +379,44 @@ class TagRepository implements TagRepositoryInterface, UserGroupInterface
/** @var null|Location */
return $tag->locations()->first();
}
+
+ #[\Override]
+ public function periodCollection(Tag $tag, Carbon $start, Carbon $end): array
+ {
+ Log::debug(sprintf('periodCollection(#%d, %s, %s)', $tag->id, $start->format('Y-m-d'), $end->format('Y-m-d')));
+
+ return $tag->transactionJournals()
+ ->leftJoin('transactions','transactions.transaction_journal_id', '=', 'transaction_journals.id')
+ ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
+ ->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transactions.transaction_currency_id')
+ ->leftJoin('transaction_currencies as foreign_currencies', 'foreign_currencies.id', '=', 'transactions.foreign_currency_id')
+ ->where('transaction_journals.date', '>=', $start)
+ ->where('transaction_journals.date', '<=', $end)
+ ->where('transactions.amount', '>', 0)
+ ->get([
+ // currencies
+ 'transaction_currencies.id as currency_id',
+ 'transaction_currencies.code as currency_code',
+ 'transaction_currencies.name as currency_name',
+ 'transaction_currencies.symbol as currency_symbol',
+ 'transaction_currencies.decimal_places as currency_decimal_places',
+
+ // foreign
+ 'foreign_currencies.id as foreign_currency_id',
+ 'foreign_currencies.code as foreign_currency_code',
+ 'foreign_currencies.name as foreign_currency_name',
+ 'foreign_currencies.symbol as foreign_currency_symbol',
+ 'foreign_currencies.decimal_places as foreign_currency_decimal_places',
+
+ // fields
+ 'transaction_journals.date',
+ 'transaction_types.type',
+ 'transaction_journals.transaction_currency_id',
+ 'transactions.amount',
+ 'transactions.native_amount as pc_amount',
+ 'transactions.foreign_amount',
+ ])
+ ->toArray()
+ ;
+ }
}
diff --git a/app/Repositories/Tag/TagRepositoryInterface.php b/app/Repositories/Tag/TagRepositoryInterface.php
index 2052dff88c..18e45fb9ce 100644
--- a/app/Repositories/Tag/TagRepositoryInterface.php
+++ b/app/Repositories/Tag/TagRepositoryInterface.php
@@ -50,6 +50,7 @@ interface TagRepositoryInterface
* This method destroys a tag.
*/
public function destroy(Tag $tag): bool;
+ public function periodCollection(Tag $tag, Carbon $start, Carbon $end): array;
/**
* Destroy all tags.
diff --git a/app/Support/Http/Controllers/PeriodOverview.php b/app/Support/Http/Controllers/PeriodOverview.php
index 9cdf54d6e3..3b9a708d88 100644
--- a/app/Support/Http/Controllers/PeriodOverview.php
+++ b/app/Support/Http/Controllers/PeriodOverview.php
@@ -27,7 +27,6 @@ namespace FireflyIII\Support\Http\Controllers;
use Carbon\Carbon;
use FireflyIII\Enums\TransactionTypeEnum;
use FireflyIII\Exceptions\FireflyException;
-use FireflyIII\Generator\Report\Category\YearReportGenerator;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\Category;
@@ -37,9 +36,11 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\PeriodStatistic\PeriodStatisticRepositoryInterface;
+use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Facades\Navigation;
+use FireflyIII\Support\Facades\Steam;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
@@ -74,7 +75,8 @@ use Illuminate\Support\Facades\Log;
trait PeriodOverview
{
protected AccountRepositoryInterface $accountRepository;
- protected CategoryRepositoryInterface $categoryRepository;
+ protected CategoryRepositoryInterface $categoryRepository;
+ protected TagRepositoryInterface $tagRepository;
protected JournalRepositoryInterface $journalRepos;
protected PeriodStatisticRepositoryInterface $periodStatisticRepo;
private Collection $statistics; // temp data holder
@@ -90,16 +92,16 @@ trait PeriodOverview
protected function getAccountPeriodOverview(Account $account, Carbon $start, Carbon $end): array
{
Log::debug(sprintf('Now in getAccountPeriodOverview(#%d, %s %s)', $account->id, $start->format('Y-m-d H:i:s.u'), $end->format('Y-m-d H:i:s.u')));
- $this->accountRepository = app(AccountRepositoryInterface::class);
+ $this->accountRepository = app(AccountRepositoryInterface::class);
$this->accountRepository->setUser($account->user);
$this->periodStatisticRepo = app(PeriodStatisticRepositoryInterface::class);
$range = Navigation::getViewRange(true);
- [$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
+ [$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
/** @var array $dates */
- $dates = Navigation::blockPeriods($start, $end, $range);
- [$start, $end] = $this->getPeriodFromBlocks($dates, $start, $end);
- $this->statistics = $this->periodStatisticRepo->allInRangeForModel($account, $start, $end);
+ $dates = Navigation::blockPeriods($start, $end, $range);
+ [$start, $end] = $this->getPeriodFromBlocks($dates, $start, $end);
+ $this->statistics = $this->periodStatisticRepo->allInRangeForModel($account, $start, $end);
// TODO needs to be re-arranged:
// get all period stats for entire range.
@@ -107,7 +109,7 @@ trait PeriodOverview
// create new ones, or use collected.
- $entries = [];
+ $entries = [];
Log::debug(sprintf('Count of loops: %d', count($dates)));
foreach ($dates as $currentDate) {
$entries[] = $this->getSingleAccountPeriod($account, $currentDate['period'], $currentDate['start'], $currentDate['end']);
@@ -143,7 +145,7 @@ trait PeriodOverview
*/
protected function getCategoryPeriodOverview(Category $category, Carbon $start, Carbon $end): array
{
- $this->categoryRepository = app(CategoryRepositoryInterface::class);
+ $this->categoryRepository = app(CategoryRepositoryInterface::class);
$this->categoryRepository->setUser($category->user);
$this->periodStatisticRepo = app(PeriodStatisticRepositoryInterface::class);
@@ -163,51 +165,6 @@ trait PeriodOverview
}
return $entries;
-
- // collect all expenses in this period:
- /** @var GroupCollectorInterface $collector */
- $collector = app(GroupCollectorInterface::class);
- $collector->setCategory($category);
- $collector->setRange($start, $end);
- $collector->setTypes([TransactionTypeEnum::DEPOSIT->value]);
- $earnedSet = $collector->getExtractedJournals();
-
- // collect all income in this period:
- /** @var GroupCollectorInterface $collector */
- $collector = app(GroupCollectorInterface::class);
- $collector->setCategory($category);
- $collector->setRange($start, $end);
- $collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value]);
- $spentSet = $collector->getExtractedJournals();
-
- // collect all transfers in this period:
- /** @var GroupCollectorInterface $collector */
- $collector = app(GroupCollectorInterface::class);
- $collector->setCategory($category);
- $collector->setRange($start, $end);
- $collector->setTypes([TransactionTypeEnum::TRANSFER->value]);
- $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 = Navigation::periodShow($currentDate['end'], $currentDate['period']);
- $entries[]
- = [
- '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),
- ];
- }
-
- return $entries;
}
/**
@@ -219,11 +176,11 @@ trait PeriodOverview
*/
protected function getNoBudgetPeriodOverview(Carbon $start, Carbon $end): array
{
- $range = Navigation::getViewRange(true);
+ $range = Navigation::getViewRange(true);
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
- $cache = new CacheProperties();
+ $cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($this->convertToPrimary);
@@ -234,28 +191,28 @@ trait PeriodOverview
}
/** @var array $dates */
- $dates = Navigation::blockPeriods($start, $end, $range);
- $entries = [];
+ $dates = Navigation::blockPeriods($start, $end, $range);
+ $entries = [];
// get all expenses without a budget.
/** @var GroupCollectorInterface $collector */
- $collector = app(GroupCollectorInterface::class);
+ $collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end)->withoutBudget()->withAccountInformation()->setTypes([TransactionTypeEnum::WITHDRAWAL->value]);
- $journals = $collector->getExtractedJournals();
+ $journals = $collector->getExtractedJournals();
foreach ($dates as $currentDate) {
$set = $this->filterJournalsByDate($journals, $currentDate['start'], $currentDate['end']);
$title = Navigation::periodShow($currentDate['end'], $currentDate['period']);
$entries[]
= [
- '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' => [],
- ];
+ '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);
@@ -272,38 +229,38 @@ trait PeriodOverview
protected function getNoCategoryPeriodOverview(Carbon $theDate): array
{
Log::debug(sprintf('Now in getNoCategoryPeriodOverview(%s)', $theDate->format('Y-m-d')));
- $range = Navigation::getViewRange(true);
- $first = $this->journalRepos->firstNull();
- $start = null === $first ? new Carbon() : $first->date;
- $end = clone $theDate;
- $end = Navigation::endOfPeriod($end, $range);
+ $range = Navigation::getViewRange(true);
+ $first = $this->journalRepos->firstNull();
+ $start = null === $first ? new Carbon() : $first->date;
+ $end = clone $theDate;
+ $end = Navigation::endOfPeriod($end, $range);
Log::debug(sprintf('Start for getNoCategoryPeriodOverview() is %s', $start->format('Y-m-d')));
Log::debug(sprintf('End for getNoCategoryPeriodOverview() is %s', $end->format('Y-m-d')));
// properties for cache
- $dates = Navigation::blockPeriods($start, $end, $range);
- $entries = [];
+ $dates = Navigation::blockPeriods($start, $end, $range);
+ $entries = [];
// collect all expenses in this period:
/** @var GroupCollectorInterface $collector */
- $collector = app(GroupCollectorInterface::class);
+ $collector = app(GroupCollectorInterface::class);
$collector->withoutCategory();
$collector->setRange($start, $end);
$collector->setTypes([TransactionTypeEnum::DEPOSIT->value]);
- $earnedSet = $collector->getExtractedJournals();
+ $earnedSet = $collector->getExtractedJournals();
// collect all income in this period:
/** @var GroupCollectorInterface $collector */
- $collector = app(GroupCollectorInterface::class);
+ $collector = app(GroupCollectorInterface::class);
$collector->withoutCategory();
$collector->setRange($start, $end);
$collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value]);
- $spentSet = $collector->getExtractedJournals();
+ $spentSet = $collector->getExtractedJournals();
// collect all transfers in this period:
/** @var GroupCollectorInterface $collector */
- $collector = app(GroupCollectorInterface::class);
+ $collector = app(GroupCollectorInterface::class);
$collector->withoutCategory();
$collector->setRange($start, $end);
$collector->setTypes([TransactionTypeEnum::TRANSFER->value]);
@@ -317,13 +274,13 @@ trait PeriodOverview
$title = Navigation::periodShow($currentDate['end'], $currentDate['period']);
$entries[]
= [
- '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),
- ];
+ '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),
+ ];
}
Log::debug('End of loops');
@@ -342,7 +299,7 @@ trait PeriodOverview
];
$this->transactions = [];
foreach ($types as $type) {
- $set = $this->getSingleAccountPeriodByType($account, $start, $end, $type);
+ $set = $this->getSingleAccountPeriodByType($account, $start, $end, $type);
$return['total_transactions'] += $set['count'];
unset($set['count']);
$return[$type] = $set;
@@ -371,6 +328,26 @@ trait PeriodOverview
return $return;
}
+ protected function getSingleTagPeriod(Tag $tag, string $period, Carbon $start, Carbon $end): array
+ {
+ Log::debug(sprintf('Now in getSingleTagPeriod(#%d, %s %s)', $tag->id, $start->format('Y-m-d'), $end->format('Y-m-d')));
+ $types = ['spent', 'earned', 'transferred_in', 'transferred_away'];
+ $return = [
+ 'title' => Navigation::periodShow($start, $period),
+ 'route' => route('tags.show', [$tag->id, $start->format('Y-m-d'), $end->format('Y-m-d')]),
+ 'total_transactions' => 0,
+ ];
+ $this->transactions = [];
+ foreach ($types as $type) {
+ $set = $this->getSingleTagPeriodByType($tag, $start, $end, $type);
+ $return['total_transactions'] += $set['count'];
+ unset($set['count']);
+ $return[$type] = $set;
+ }
+
+ return $return;
+ }
+
protected function filterStatistics(Carbon $start, Carbon $end, string $type): Collection
{
return $this->statistics->filter(
@@ -436,15 +413,15 @@ trait PeriodOverview
return $grouped;
}
- $grouped = [
+ $grouped = [
'count' => 0,
];
/** @var PeriodStatistic $statistic */
foreach ($statistics as $statistic) {
- $id = (int)$statistic->transaction_currency_id;
- $currency = Amount::getTransactionCurrencyById($id);
- $grouped[$id] = [
+ $id = (int)$statistic->transaction_currency_id;
+ $currency = Amount::getTransactionCurrencyById($id);
+ $grouped[$id] = [
'amount' => (string)$statistic->amount,
'count' => (int)$statistic->count,
'currency_id' => $currency->id,
@@ -476,6 +453,7 @@ trait PeriodOverview
throw new FireflyException(sprintf('Cannot deal with category period type %s', $type));
case 'spent':
+
$result = $this->filterTransactionsByType(TransactionTypeEnum::WITHDRAWAL, $start, $end);
break;
@@ -526,6 +504,74 @@ trait PeriodOverview
return $grouped;
}
+ protected function getSingleTagPeriodByType(Tag $tag, Carbon $start, Carbon $end, string $type): array
+ {
+ Log::debug(sprintf('Now in getSingleTagPeriodByType(#%d, %s %s, %s)', $tag->id, $start->format('Y-m-d'), $end->format('Y-m-d'), $type));
+ $statistics = $this->filterStatistics($start, $end, $type);
+
+ // nothing found, regenerate them.
+ if (0 === $statistics->count()) {
+ Log::debug(sprintf('Found nothing in this period for type "%s"', $type));
+ if (0 === count($this->transactions)) {
+ $this->transactions = $this->tagRepository->periodCollection($tag, $start, $end);
+ }
+
+ switch ($type) {
+ default:
+ throw new FireflyException(sprintf('Cannot deal with tag period type %s', $type));
+
+ case 'spent':
+
+ $result = $this->filterTransactionsByType(TransactionTypeEnum::WITHDRAWAL, $start, $end);
+
+ break;
+
+ case 'earned':
+ $result = $this->filterTransactionsByType(TransactionTypeEnum::DEPOSIT, $start, $end);
+
+ break;
+
+ case 'transferred_in':
+ $result = $this->filterTransfers('in', $start, $end);
+
+ break;
+
+ case 'transferred_away':
+ $result = $this->filterTransfers('away', $start, $end);
+
+ break;
+ }
+ // each result must be grouped by currency, then saved as period statistic.
+ Log::debug(sprintf('Going to group %d found journal(s)', count($result)));
+ $grouped = $this->groupByCurrency($result);
+
+ $this->saveGroupedAsStatistics($tag, $start, $end, $type, $grouped);
+
+ return $grouped;
+ }
+ $grouped = [
+ 'count' => 0,
+ ];
+
+ /** @var PeriodStatistic $statistic */
+ foreach ($statistics as $statistic) {
+ $id = (int)$statistic->transaction_currency_id;
+ $currency = Amount::getTransactionCurrencyById($id);
+ $grouped[$id] = [
+ 'amount' => (string)$statistic->amount,
+ 'count' => (int)$statistic->count,
+ 'currency_id' => $currency->id,
+ 'currency_name' => $currency->name,
+ 'currency_code' => $currency->code,
+ 'currency_symbol' => $currency->symbol,
+ 'currency_decimal_places' => $currency->decimal_places,
+ ];
+ $grouped['count'] += (int)$statistic->count;
+ }
+
+ return $grouped;
+ }
+
/**
* This shows a period overview for a tag. It goes back in time and lists all relevant transactions and sums.
*
@@ -533,51 +579,63 @@ trait PeriodOverview
*/
protected function getTagPeriodOverview(Tag $tag, Carbon $start, Carbon $end): array // period overview for tags.
{
- $range = Navigation::getViewRange(true);
+ $this->tagRepository = app(TagRepositoryInterface::class);
+ $this->tagRepository->setUser($tag->user);
+ $this->periodStatisticRepo = app(PeriodStatisticRepositoryInterface::class);
+
+ $range = Navigation::getViewRange(true);
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
- // properties for cache
- $cache = new CacheProperties();
- $cache->addProperty($start);
- $cache->addProperty($end);
- $cache->addProperty('tag-period-entries');
- $cache->addProperty($tag->id);
- if ($cache->has()) {
- return $cache->get();
+ /** @var array $dates */
+ $dates = Navigation::blockPeriods($start, $end, $range);
+ $entries = [];
+ [$start, $end] = $this->getPeriodFromBlocks($dates, $start, $end);
+ $this->statistics = $this->periodStatisticRepo->allInRangeForModel($tag, $start, $end);
+
+
+ Log::debug(sprintf('Count of loops: %d', count($dates)));
+ foreach ($dates as $currentDate) {
+ $entries[] = $this->getSingleTagPeriod($tag, $currentDate['period'], $currentDate['start'], $currentDate['end']);
}
+ return $entries;
+
+
+ $range = Navigation::getViewRange(true);
+ [$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
+
/** @var array $dates */
- $dates = Navigation::blockPeriods($start, $end, $range);
- $entries = [];
+ $dates = Navigation::blockPeriods($start, $end, $range);
+ $entries = [];
// collect all expenses in this period:
/** @var GroupCollectorInterface $collector */
- $collector = app(GroupCollectorInterface::class);
+ $collector = app(GroupCollectorInterface::class);
$collector->setTag($tag);
$collector->setRange($start, $end);
$collector->setTypes([TransactionTypeEnum::DEPOSIT->value]);
- $earnedSet = $collector->getExtractedJournals();
+ $earnedSet = $collector->getExtractedJournals();
// collect all income in this period:
/** @var GroupCollectorInterface $collector */
- $collector = app(GroupCollectorInterface::class);
+ $collector = app(GroupCollectorInterface::class);
$collector->setTag($tag);
$collector->setRange($start, $end);
$collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value]);
- $spentSet = $collector->getExtractedJournals();
+ $spentSet = $collector->getExtractedJournals();
// collect all transfers in this period:
/** @var GroupCollectorInterface $collector */
- $collector = app(GroupCollectorInterface::class);
+ $collector = app(GroupCollectorInterface::class);
$collector->setTag($tag);
$collector->setRange($start, $end);
$collector->setTypes([TransactionTypeEnum::TRANSFER->value]);
- $transferSet = $collector->getExtractedJournals();
+ $transferSet = $collector->getExtractedJournals();
// filer all of them:
- $earnedSet = $this->filterJournalsByTag($earnedSet, $tag);
- $spentSet = $this->filterJournalsByTag($spentSet, $tag);
- $transferSet = $this->filterJournalsByTag($transferSet, $tag);
+ $earnedSet = $this->filterJournalsByTag($earnedSet, $tag);
+ $spentSet = $this->filterJournalsByTag($spentSet, $tag);
+ $transferSet = $this->filterJournalsByTag($transferSet, $tag);
foreach ($dates as $currentDate) {
$spent = $this->filterJournalsByDate($spentSet, $currentDate['start'], $currentDate['end']);
@@ -586,17 +644,17 @@ trait PeriodOverview
$title = Navigation::periodShow($currentDate['end'], $currentDate['period']);
$entries[]
= [
- '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),
- ];
+ '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),
+ ];
}
return $entries;
@@ -607,12 +665,12 @@ trait PeriodOverview
*/
protected function getTransactionPeriodOverview(string $transactionType, Carbon $start, Carbon $end): array
{
- $range = Navigation::getViewRange(true);
- $types = config(sprintf('firefly.transactionTypesByType.%s', $transactionType));
+ $range = Navigation::getViewRange(true);
+ $types = config(sprintf('firefly.transactionTypesByType.%s', $transactionType));
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
// properties for cache
- $cache = new CacheProperties();
+ $cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('transactions-period-entries');
@@ -622,16 +680,16 @@ trait PeriodOverview
}
/** @var array $dates */
- $dates = Navigation::blockPeriods($start, $end, $range);
- $entries = [];
- $spent = [];
- $earned = [];
- $transferred = [];
+ $dates = Navigation::blockPeriods($start, $end, $range);
+ $entries = [];
+ $spent = [];
+ $earned = [];
+ $transferred = [];
// collect all journals in this period (regardless of type)
- $collector = app(GroupCollectorInterface::class);
+ $collector = app(GroupCollectorInterface::class);
$collector->setTypes($types)->setRange($start, $end);
- $genericSet = $collector->getExtractedJournals();
- $loops = 0;
+ $genericSet = $collector->getExtractedJournals();
+ $loops = 0;
foreach ($dates as $currentDate) {
$title = Navigation::periodShow($currentDate['end'], $currentDate['period']);
@@ -649,14 +707,14 @@ trait PeriodOverview
}
}
$entries[]
- = [
- '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),
- ];
+ = [
+ '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),
+ ];
++$loops;
}
@@ -666,7 +724,7 @@ trait PeriodOverview
protected function saveGroupedAsStatistics(Model $model, Carbon $start, Carbon $end, string $type, array $array): void
{
unset($array['count']);
- Log::debug(sprintf('saveGroupedAsStatistics(%s #%d, %s, %s, "%s", array(%d))',get_class($model), $model->id, $start->format('Y-m-d'), $end->format('Y-m-d'), $type, count($array)));
+ Log::debug(sprintf('saveGroupedAsStatistics(%s #%d, %s, %s, "%s", array(%d))', get_class($model), $model->id, $start->format('Y-m-d'), $end->format('Y-m-d'), $type, count($array)));
foreach ($array as $entry) {
$this->periodStatisticRepo->saveStatistic($model, $entry['currency_id'], $start, $end, $type, $entry['count'], $entry['amount']);
}
@@ -697,7 +755,7 @@ trait PeriodOverview
{
$return = [];
foreach ($set as $entry) {
- $found = false;
+ $found = false;
/** @var array $localTag */
foreach ($entry['tags'] as $localTag) {
@@ -726,6 +784,11 @@ trait PeriodOverview
$date = Carbon::parse($item['date']);
$fits = $item['type'] === $type->value && $date >= $start && $date <= $end;
if ($fits) {
+
+ // if type is withdrawal, negative amount:
+ if (TransactionTypeEnum::WITHDRAWAL === $type && 1 === bccomp((string)$item['amount'], '0')) {
+ $item['amount'] = Steam::negative($item['amount']);
+ }
$result[] = $item;
}
}
@@ -801,21 +864,13 @@ trait PeriodOverview
/** @var array $journal */
foreach ($journals as $journal) {
-
- if (!array_key_exists('currency_id', $journal)) {
- Log::debug('very strange!');
- var_dump($journals);
-
- exit;
- }
-
- $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';
+ $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';
if ($this->convertToPrimary && $currencyId !== $this->primaryCurrency->id && $foreignCurrencyId !== $this->primaryCurrency->id) {
$amount = $journal['pc_amount'] ?? '0';