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

105 lines
3.7 KiB
PHP
Raw Normal View History

2018-12-31 07:58:13 +01:00
<?php
/**
* 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;
use Illuminate\Support\Collection;
2023-04-01 07:04:42 +02:00
use Illuminate\Support\Facades\Log;
2023-05-29 13:56:55 +02:00
use JsonException;
2018-12-31 07:58:13 +01:00
/**
* Trait ChartGeneration
*/
trait ChartGeneration
{
/**
* Shows an overview of the account balances for a set of accounts.
*
2023-06-21 12:34:58 +02:00
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
2018-12-31 07:58:13 +01:00
*
* @return array
* @throws FireflyException
* @throws JsonException
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:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('chart.account.account-balance-chart');
$cache->addProperty($accounts);
if ($cache->has()) {
return $cache->get();
2018-12-31 07:58:13 +01:00
}
Log::debug('Regenerate chart.account.account-balance-chart from scratch.');
$locale = app('steam')->getLocale();
2018-12-31 07:58:13 +01:00
/** @var GeneratorInterface $generator */
$generator = app(GeneratorInterface::class);
/** @var AccountRepositoryInterface $accountRepos */
$accountRepos = app(AccountRepositoryInterface::class);
$default = app('amount')->getDefaultCurrency();
$chartData = [];
/** @var Account $account */
foreach ($accounts as $account) {
2022-10-30 11:43:17 +01:00
// TODO we can use getAccountCurrency instead.
2023-10-28 06:58:33 +02:00
$currency = $accountRepos->getAccountCurrency($account);
2018-12-31 07:58:13 +01:00
if (null === $currency) {
$currency = $default;
}
$currentSet = [
'label' => $account->name,
'currency_symbol' => $currency->symbol,
'entries' => [],
];
$currentStart = clone $start;
$range = app('steam')->balanceInRange($account, $start, clone $end);
$previous = array_values($range)[0];
while ($currentStart <= $end) {
$format = $currentStart->format('Y-m-d');
2022-12-29 19:42:26 +01:00
$label = trim($currentStart->isoFormat((string)trans('config.month_and_day_js', [], $locale)));
$balance = $range[$format] ?? $previous;
2018-12-31 07:58:13 +01:00
$previous = $balance;
$currentStart->addDay();
$currentSet['entries'][$label] = $balance;
}
$chartData[] = $currentSet;
}
$data = $generator->multiSet($chartData);
$cache->store($data);
return $data;
}
2019-02-09 10:36:59 +01:00
}