2018-12-31 07:58:13 +01:00
|
|
|
<?php
|
2024-11-25 04:18:55 +01:00
|
|
|
|
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
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-12-31 07:58:13 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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
|
|
|
*
|
2019-10-02 06:37:26 +02: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
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-12-31 07:58:13 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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;
|
2021-09-18 10:26:12 +02:00
|
|
|
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;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait ChartGeneration
|
|
|
|
*/
|
|
|
|
trait ChartGeneration
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Shows an overview of the account balances for a set of accounts.
|
|
|
|
*
|
2021-09-18 10:26:12 +02:00
|
|
|
* @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-01-01 14:43:56 +01:00
|
|
|
$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-22 16:41:55 +01:00
|
|
|
$convertToNative =app('preferences')->get('convert_to_native', false)->data;
|
2018-12-31 07:58:13 +01:00
|
|
|
if ($cache->has()) {
|
2024-12-22 16:41:55 +01:00
|
|
|
// 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.');
|
2024-01-01 14:43:56 +01:00
|
|
|
$locale = app('steam')->getLocale();
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2018-12-31 07:58:13 +01:00
|
|
|
/** @var GeneratorInterface $generator */
|
2024-01-01 14:43:56 +01:00
|
|
|
$generator = app(GeneratorInterface::class);
|
2018-12-31 07:58:13 +01:00
|
|
|
|
|
|
|
/** @var AccountRepositoryInterface $accountRepos */
|
|
|
|
$accountRepos = app(AccountRepositoryInterface::class);
|
|
|
|
|
2024-01-01 14:43:56 +01:00
|
|
|
$default = app('amount')->getDefaultCurrency();
|
|
|
|
$chartData = [];
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2018-12-31 07:58:13 +01:00
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($accounts as $account) {
|
2022-10-30 11:43:17 +01:00
|
|
|
// TODO we can use getAccountCurrency instead.
|
2024-01-01 14:43:56 +01:00
|
|
|
$currency = $accountRepos->getAccountCurrency($account);
|
2018-12-31 07:58:13 +01:00
|
|
|
if (null === $currency) {
|
|
|
|
$currency = $default;
|
|
|
|
}
|
2024-12-22 16:41:55 +01:00
|
|
|
// if the user prefers the native currency, overrule the currency of the account.
|
|
|
|
if($currency->id !== $default->id && $convertToNative) {
|
|
|
|
$currency = $default;
|
|
|
|
}
|
|
|
|
|
2024-01-01 14:43:56 +01:00
|
|
|
$currentSet = [
|
2018-12-31 07:58:13 +01:00
|
|
|
'label' => $account->name,
|
|
|
|
'currency_symbol' => $currency->symbol,
|
|
|
|
'entries' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
$currentStart = clone $start;
|
2024-12-22 16:41:55 +01:00
|
|
|
$range = $convertToNative ? app('steam')->balanceInRangeNative($account, $start, clone $end) : app('steam')->balanceInRange($account, $start, clone $end);
|
2018-12-31 07:58:13 +01:00
|
|
|
$previous = array_values($range)[0];
|
|
|
|
while ($currentStart <= $end) {
|
2024-01-01 14:43:56 +01:00
|
|
|
$format = $currentStart->format('Y-m-d');
|
2024-12-22 08:43:12 +01:00
|
|
|
$label = trim($currentStart->isoFormat((string) trans('config.month_and_day_js', [], $locale)));
|
2024-01-01 14:43:56 +01:00
|
|
|
$balance = $range[$format] ?? $previous;
|
|
|
|
$previous = $balance;
|
2018-12-31 07:58:13 +01:00
|
|
|
$currentStart->addDay();
|
|
|
|
$currentSet['entries'][$label] = $balance;
|
|
|
|
}
|
2024-01-01 14:43:56 +01:00
|
|
|
$chartData[] = $currentSet;
|
2018-12-31 07:58:13 +01:00
|
|
|
}
|
2024-01-01 14:43:56 +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
|
|
|
}
|