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

103 lines
3.9 KiB
PHP
Raw Normal View History

2018-12-31 07:58:13 +01:00
<?php
2018-12-31 07:58:13 +01:00
/**
* ChartGeneration.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-12-31 07:58:13 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-12-31 07:58:13 +01:00
*
* 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.
2018-12-31 07:58:13 +01:00
*
* This program is distributed in the hope that it will be useful,
2018-12-31 07:58:13 +01:00
* 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.
2018-12-31 07:58:13 +01:00
*
* 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/>.
2018-12-31 07:58:13 +01:00
*/
declare(strict_types=1);
namespace FireflyIII\Support\Http\Controllers;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
2018-12-31 07:58:13 +01:00
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\CacheProperties;
2024-12-28 07:35:20 +01:00
use FireflyIII\Support\Facades\Amount;
2024-12-22 20:32:58 +01:00
use FireflyIII\Support\Facades\Steam;
2018-12-31 07:58:13 +01:00
use Illuminate\Support\Collection;
/**
* Trait ChartGeneration
*/
trait ChartGeneration
{
/**
* Shows an overview of the account balances for a set of accounts.
*
* @throws FireflyException
2023-12-22 20:12:38 +01:00
*/
2018-12-31 07:58:13 +01:00
protected function accountBalanceChart(Collection $accounts, Carbon $start, Carbon $end): array // chart helper method.
{
// chart properties for cache:
2024-12-28 07:35:20 +01:00
$convertToNative = Amount::convertToNative();
$cache = new CacheProperties();
2018-12-31 07:58:13 +01:00
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('chart.account.account-balance-chart');
$cache->addProperty($accounts);
2024-12-24 10:29:07 +01:00
$cache->addProperty($convertToNative);
2018-12-31 07:58:13 +01:00
if ($cache->has()) {
return $cache->get();
2018-12-31 07:58:13 +01:00
}
2023-10-29 06:33:43 +01:00
app('log')->debug('Regenerate chart.account.account-balance-chart from scratch.');
$locale = app('steam')->getLocale();
2023-12-20 19:35:52 +01:00
2018-12-31 07:58:13 +01:00
/** @var GeneratorInterface $generator */
$generator = app(GeneratorInterface::class);
2018-12-31 07:58:13 +01:00
/** @var AccountRepositoryInterface $accountRepos */
$accountRepos = app(AccountRepositoryInterface::class);
2018-12-31 07:58:13 +01:00
2025-01-19 19:07:19 +01:00
$default = app('amount')->getNativeCurrency();
$chartData = [];
2023-12-20 19:35:52 +01:00
2018-12-31 07:58:13 +01:00
/** @var Account $account */
foreach ($accounts as $account) {
$currency = $accountRepos->getAccountCurrency($account) ?? $default;
$useNative = $convertToNative && $default->id !== $currency->id;
$field = $useNative ? 'native_balance' : 'balance';
$currency = $useNative ? $default : $currency;
$currentSet = [
2018-12-31 07:58:13 +01:00
'label' => $account->name,
'currency_symbol' => $currency->symbol,
'entries' => [],
];
$currentStart = clone $start;
2024-12-26 05:21:28 +01:00
$range = Steam::finalAccountBalanceInRange($account, $start, clone $end, $this->convertToNative);
2018-12-31 07:58:13 +01:00
$previous = array_values($range)[0];
while ($currentStart <= $end) {
$format = $currentStart->format('Y-m-d');
$label = trim($currentStart->isoFormat((string) trans('config.month_and_day_js', [], $locale)));
$balance = $range[$format] ?? $previous;
$previous = $balance;
2018-12-31 07:58:13 +01:00
$currentStart->addDay();
2024-12-24 10:29:07 +01:00
$currentSet['entries'][$label] = $balance[$field];
2018-12-31 07:58:13 +01:00
}
$chartData[] = $currentSet;
2018-12-31 07:58:13 +01:00
}
$data = $generator->multiSet($chartData);
2018-12-31 07:58:13 +01:00
$cache->store($data);
return $data;
}
2019-02-09 10:36:59 +01:00
}