firefly-iii/app/Http/Controllers/Chart/AccountController.php

570 lines
23 KiB
PHP
Raw Normal View History

2015-05-16 02:41:14 -05:00
<?php
2022-11-03 23:11:05 -05:00
/**
* AccountController.php
2020-01-31 00:32:04 -06: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.
2017-10-21 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05: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.
2017-10-21 01:40:00 -05: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/>.
*/
declare(strict_types=1);
2015-05-16 02:41:14 -05:00
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
2016-12-11 09:02:04 -06:00
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2015-05-16 02:41:14 -05:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
2016-05-13 10:22:24 -05:00
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionCurrency;
2016-11-20 11:31:29 -06:00
use FireflyIII\Models\TransactionType;
2016-10-10 00:25:27 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2023-10-27 23:58:33 -05:00
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
2016-05-13 10:22:24 -05:00
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Http\Controllers\AugumentData;
2018-12-31 00:58:13 -06:00
use FireflyIII\Support\Http\Controllers\ChartGeneration;
2018-07-14 16:22:08 -05:00
use FireflyIII\Support\Http\Controllers\DateCalculation;
2018-07-08 05:08:53 -05:00
use Illuminate\Http\JsonResponse;
2015-05-17 11:03:16 -05:00
use Illuminate\Support\Collection;
2015-05-16 02:41:14 -05:00
2018-07-14 16:22:08 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class AccountController.
2015-05-16 02:41:14 -05:00
*/
class AccountController extends Controller
{
2022-10-30 08:24:19 -05:00
use AugumentData;
use ChartGeneration;
2023-11-04 08:18:49 -05:00
use DateCalculation;
2018-07-14 16:22:08 -05:00
2022-03-28 05:24:16 -05:00
protected GeneratorInterface $generator;
private AccountRepositoryInterface $accountRepository;
private CurrencyRepositoryInterface $currencyRepository;
/**
2018-07-21 01:06:24 -05:00
* AccountController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->generator = app(GeneratorInterface::class);
$this->accountRepository = app(AccountRepositoryInterface::class);
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
return $next($request);
}
);
}
2021-03-28 04:46:23 -05:00
2015-12-12 03:41:51 -06:00
/**
* Shows the balances for all the user's expense accounts (on the front page).
2015-12-28 00:38:02 -06:00
*
* This chart is (multi) currency aware.
2015-12-12 03:41:51 -06:00
*
* */
public function expenseAccounts(): JsonResponse
2015-12-12 03:41:51 -06:00
{
2018-07-26 21:46:21 -05:00
/** @var Carbon $start */
2023-02-11 00:36:45 -06:00
$start = clone session('start', today(config('app.timezone'))->startOfMonth());
2023-12-20 12:35:52 -06:00
2018-07-26 21:46:21 -05:00
/** @var Carbon $end */
2023-02-11 00:36:45 -06:00
$end = clone session('end', today(config('app.timezone'))->endOfMonth());
2022-10-30 08:24:19 -05:00
$cache = new CacheProperties();
2015-12-12 03:41:51 -06:00
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-11 10:05:48 -06:00
$cache->addProperty('chart.account.expense-accounts');
2015-12-12 03:41:51 -06:00
if ($cache->has()) {
return response()->json($cache->get());
2015-12-12 03:41:51 -06:00
}
2016-05-13 10:22:24 -05:00
$start->subDay();
2016-12-11 10:05:48 -06:00
// prep some vars:
$currencies = [];
$chartData = [];
$tempData = [];
// grab all accounts and names
2019-01-27 03:51:00 -06:00
$accounts = $this->accountRepository->getAccountsByType([AccountType::EXPENSE]);
$accountNames = $this->extractNames($accounts);
// grab all balances
$startBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $start);
$endBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $end);
// loop the end balances. This is an array for each account ($expenses)
foreach ($endBalances as $accountId => $expenses) {
2022-11-03 23:11:05 -05:00
$accountId = (int)$accountId;
// loop each expense entry (each entry can be a different currency).
foreach ($expenses as $currencyId => $endAmount) {
2022-11-03 23:11:05 -05:00
$currencyId = (int)$currencyId;
// see if there is an accompanying start amount.
// grab the difference and find the currency.
2022-11-03 23:11:05 -05:00
$startAmount = (string)($startBalances[$accountId][$currencyId] ?? '0');
$diff = bcsub((string)$endAmount, $startAmount);
2023-12-09 23:45:59 -06:00
$currencies[$currencyId] ??= $this->currencyRepository->find($currencyId);
if (0 !== bccomp($diff, '0')) {
// store the values in a temporary array.
$tempData[] = [
'name' => $accountNames[$accountId],
'difference' => $diff,
2022-12-29 12:41:57 -06:00
'diff_float' => (float)$diff, // intentional float
'currency_id' => $currencyId,
];
}
2016-05-13 10:22:24 -05:00
}
2016-12-11 09:38:21 -06:00
}
// sort temp array by amount.
$amounts = array_column($tempData, 'diff_float');
array_multisort($amounts, SORT_DESC, $tempData);
// loop all found currencies and build the data array for the chart.
/**
2023-06-21 05:34:58 -05:00
* @var int $currencyId
* @var TransactionCurrency $currency
*/
foreach ($currencies as $currencyId => $currency) {
$dataSet
= [
2023-12-20 12:35:52 -06:00
'label' => (string)trans('firefly.spent'),
'type' => 'bar',
'currency_symbol' => $currency->symbol,
'currency_code' => $currency->code,
'entries' => $this->expandNames($tempData),
];
$chartData[$currencyId] = $dataSet;
}
// loop temp data and place data in correct array:
foreach ($tempData as $entry) {
$currencyId = $entry['currency_id'];
$name = $entry['name'];
$chartData[$currencyId]['entries'][$name] = $entry['difference'];
}
$data = $this->generator->multiSet($chartData);
2015-12-12 03:41:51 -06:00
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
2015-05-17 11:03:16 -05:00
}
2023-06-21 05:34:58 -05:00
/**
* Expenses per budget for all time, as shown on account overview.
*/
public function expenseBudgetAll(AccountRepositoryInterface $repository, Account $account): JsonResponse
{
$start = $repository->oldestJournalDate($account) ?? today(config('app.timezone'))->startOfMonth();
$end = today(config('app.timezone'));
return $this->expenseBudget($account, $start, $end);
}
2016-11-20 11:31:29 -06:00
/**
2018-07-21 01:06:24 -05:00
* Expenses per budget, as shown on account overview.
2016-11-20 11:31:29 -06:00
*/
2018-07-08 05:08:53 -05:00
public function expenseBudget(Account $account, Carbon $start, Carbon $end): JsonResponse
2016-11-20 11:31:29 -06:00
{
2022-10-30 08:24:19 -05:00
$cache = new CacheProperties();
2016-11-20 11:31:29 -06:00
$cache->addProperty($account->id);
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-11 10:05:48 -06:00
$cache->addProperty('chart.account.expense-budget');
2016-11-20 11:31:29 -06:00
if ($cache->has()) {
return response()->json($cache->get());
2016-11-20 11:31:29 -06:00
}
2023-12-20 12:35:52 -06:00
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2017-02-25 06:19:42 -06:00
$collector->setAccounts(new Collection([$account]))->setRange($start, $end)->withBudgetInformation()->setTypes([TransactionType::WITHDRAWAL]);
$journals = $collector->getExtractedJournals();
$chartData = [];
$result = [];
$budgetIds = [];
2023-12-20 12:35:52 -06:00
/** @var array $journal */
foreach ($journals as $journal) {
2022-11-03 23:11:05 -05:00
$budgetId = (int)$journal['budget_id'];
2020-01-02 01:34:34 -06:00
$key = sprintf('%d-%d', $budgetId, $journal['currency_id']);
$budgetIds[] = $budgetId;
2021-04-07 00:28:43 -05:00
if (!array_key_exists($key, $result)) {
2020-01-02 01:34:34 -06:00
$result[$key] = [
2018-09-10 10:57:20 -05:00
'total' => '0',
'budget_id' => $budgetId,
2020-01-02 01:34:34 -06:00
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
2020-07-27 23:25:14 -05:00
'currency_code' => $journal['currency_code'],
];
}
2020-01-02 01:34:34 -06:00
$result[$key]['total'] = bcadd($journal['amount'], $result[$key]['total']);
2016-11-20 11:31:29 -06:00
}
2016-12-11 10:05:48 -06:00
$names = $this->getBudgetNames($budgetIds);
2018-09-10 10:57:20 -05:00
foreach ($result as $row) {
$budgetId = $row['budget_id'];
$name = $names[$budgetId];
2022-11-03 23:11:05 -05:00
$label = (string)trans('firefly.name_in_currency', ['name' => $name, 'currency' => $row['currency_name']]);
2020-07-27 23:25:14 -05:00
$chartData[$label] = ['amount' => $row['total'], 'currency_symbol' => $row['currency_symbol'], 'currency_code' => $row['currency_code']];
2016-12-11 10:05:48 -06:00
}
2018-09-10 10:57:20 -05:00
$data = $this->generator->multiCurrencyPieChart($chartData);
2016-11-20 11:31:29 -06:00
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
2016-11-20 11:31:29 -06:00
}
/**
2023-06-21 05:34:58 -05:00
* Expenses grouped by category for account.
2016-11-20 11:31:29 -06:00
*/
2023-06-21 05:34:58 -05:00
public function expenseCategoryAll(AccountRepositoryInterface $repository, Account $account): JsonResponse
2017-02-25 06:19:42 -06:00
{
2023-02-11 00:36:45 -06:00
$start = $repository->oldestJournalDate($account) ?? today(config('app.timezone'))->startOfMonth();
$end = today(config('app.timezone'));
2017-02-25 06:19:42 -06:00
2023-06-21 05:34:58 -05:00
return $this->expenseCategory($account, $start, $end);
2017-02-25 06:19:42 -06:00
}
/**
2018-07-21 01:06:24 -05:00
* Expenses per category for one single account.
2017-02-25 06:19:42 -06:00
*/
2018-07-08 05:08:53 -05:00
public function expenseCategory(Account $account, Carbon $start, Carbon $end): JsonResponse
2016-11-20 11:31:29 -06:00
{
2022-10-30 08:24:19 -05:00
$cache = new CacheProperties();
2016-11-20 11:31:29 -06:00
$cache->addProperty($account->id);
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-11 10:05:48 -06:00
$cache->addProperty('chart.account.expense-category');
2016-11-20 11:31:29 -06:00
if ($cache->has()) {
return response()->json($cache->get());
2016-11-20 11:31:29 -06:00
}
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2016-11-20 11:31:29 -06:00
$collector->setAccounts(new Collection([$account]))->setRange($start, $end)->withCategoryInformation()->setTypes([TransactionType::WITHDRAWAL]);
$journals = $collector->getExtractedJournals();
$result = [];
$chartData = [];
/** @var array $journal */
foreach ($journals as $journal) {
2020-01-02 01:34:34 -06:00
$key = sprintf('%d-%d', $journal['category_id'], $journal['currency_id']);
2021-04-07 00:28:43 -05:00
if (!array_key_exists($key, $result)) {
2020-01-02 01:34:34 -06:00
$result[$key] = [
2018-09-10 10:57:20 -05:00
'total' => '0',
2022-11-03 23:11:05 -05:00
'category_id' => (int)$journal['category_id'],
2020-01-02 01:34:34 -06:00
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
2020-07-27 23:25:14 -05:00
'currency_code' => $journal['currency_code'],
];
}
2020-01-02 01:34:34 -06:00
$result[$key]['total'] = bcadd($journal['amount'], $result[$key]['total']);
2016-11-20 11:31:29 -06:00
}
2020-01-02 01:34:34 -06:00
$names = $this->getCategoryNames(array_keys($result));
foreach ($result as $row) {
$categoryId = $row['category_id'];
2018-08-28 07:20:04 -05:00
$name = $names[$categoryId] ?? '(unknown)';
2022-11-03 23:11:05 -05:00
$label = (string)trans('firefly.name_in_currency', ['name' => $name, 'currency' => $row['currency_name']]);
2020-07-27 23:25:14 -05:00
$chartData[$label] = ['amount' => $row['total'], 'currency_symbol' => $row['currency_symbol'], 'currency_code' => $row['currency_code']];
2016-12-11 10:05:48 -06:00
}
2018-09-10 10:57:20 -05:00
$data = $this->generator->multiCurrencyPieChart($chartData);
2016-11-20 11:31:29 -06:00
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
2016-11-20 11:31:29 -06:00
}
2015-08-01 00:04:41 -05:00
/**
2016-05-13 08:53:39 -05:00
* Shows the balances for all the user's frontpage accounts.
2015-08-01 00:04:41 -05:00
*
* @throws FireflyException
2023-12-22 00:58:35 -06:00
* */
2018-07-08 05:08:53 -05:00
public function frontpage(AccountRepositoryInterface $repository): JsonResponse
2015-08-01 00:04:41 -05:00
{
2023-02-11 00:36:45 -06:00
$start = clone session('start', today(config('app.timezone'))->startOfMonth());
$end = clone session('end', today(config('app.timezone'))->endOfMonth());
$defaultSet = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])->pluck('id')->toArray();
2023-10-29 00:33:43 -05:00
app('log')->debug('Default set is ', $defaultSet);
2023-11-26 05:10:42 -06:00
$frontPage = app('preferences')->get('frontPageAccounts', $defaultSet);
$frontPageArray = !is_array($frontPage->data) ? [] : $frontPage->data;
app('log')->debug('Frontpage preference set is ', $frontPageArray);
if (0 === count($frontPageArray)) {
app('preferences')->set('frontPageAccounts', $defaultSet);
2023-10-29 00:33:43 -05:00
app('log')->debug('frontpage set is empty!');
}
2023-11-26 05:10:42 -06:00
$accounts = $repository->getAccountsById($frontPageArray);
2016-05-13 10:22:24 -05:00
2018-03-10 13:30:09 -06:00
return response()->json($this->accountBalanceChart($accounts, $start, $end));
2015-08-01 00:04:41 -05:00
}
2023-06-21 05:34:58 -05:00
/**
* Shows the income grouped by category for an account, in all time.
*/
public function incomeCategoryAll(AccountRepositoryInterface $repository, Account $account): JsonResponse
{
$start = $repository->oldestJournalDate($account) ?? today(config('app.timezone'))->startOfMonth();
$end = today(config('app.timezone'));
return $this->incomeCategory($account, $start, $end);
}
2016-11-20 11:31:29 -06:00
/**
2018-07-21 01:06:24 -05:00
* Shows all income per account for each category.
2016-11-20 11:31:29 -06:00
*/
2018-07-08 05:08:53 -05:00
public function incomeCategory(Account $account, Carbon $start, Carbon $end): JsonResponse
2016-11-20 11:31:29 -06:00
{
2022-10-30 08:24:19 -05:00
$cache = new CacheProperties();
2016-11-20 11:31:29 -06:00
$cache->addProperty($account->id);
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-11 10:05:48 -06:00
$cache->addProperty('chart.account.income-category');
2016-11-20 11:31:29 -06:00
if ($cache->has()) {
return response()->json($cache->get());
2016-11-20 11:31:29 -06:00
}
// grab all journals:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2016-11-20 11:31:29 -06:00
$collector->setAccounts(new Collection([$account]))->setRange($start, $end)->withCategoryInformation()->setTypes([TransactionType::DEPOSIT]);
$journals = $collector->getExtractedJournals();
$result = [];
$chartData = [];
2023-12-20 12:35:52 -06:00
/** @var array $journal */
foreach ($journals as $journal) {
2020-01-02 01:34:34 -06:00
$key = sprintf('%d-%d', $journal['category_id'], $journal['currency_id']);
2021-04-07 00:28:43 -05:00
if (!array_key_exists($key, $result)) {
2020-01-02 01:34:34 -06:00
$result[$key] = [
2018-09-10 10:57:20 -05:00
'total' => '0',
2020-01-02 01:34:34 -06:00
'category_id' => $journal['category_id'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
2021-03-28 04:46:23 -05:00
'currency_code' => $journal['currency_code'],
];
}
2020-01-02 01:34:34 -06:00
$result[$key]['total'] = bcadd($journal['amount'], $result[$key]['total']);
2016-11-20 11:31:29 -06:00
}
2016-12-11 10:05:48 -06:00
2020-01-02 01:34:34 -06:00
$names = $this->getCategoryNames(array_keys($result));
foreach ($result as $row) {
$categoryId = $row['category_id'];
$name = $names[$categoryId] ?? '(unknown)';
2022-11-03 23:11:05 -05:00
$label = (string)trans('firefly.name_in_currency', ['name' => $name, 'currency' => $row['currency_name']]);
2020-07-27 23:25:14 -05:00
$chartData[$label] = ['amount' => $row['total'], 'currency_symbol' => $row['currency_symbol'], 'currency_code' => $row['currency_code']];
2016-12-11 10:05:48 -06:00
}
2018-09-10 10:57:20 -05:00
$data = $this->generator->multiCurrencyPieChart($chartData);
2016-11-20 11:31:29 -06:00
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
2016-11-20 11:31:29 -06:00
}
2016-12-11 04:15:19 -06:00
/**
2018-07-21 01:06:24 -05:00
* Shows overview of account during a single period.
*
* @throws FireflyException
* */
2018-07-08 05:08:53 -05:00
public function period(Account $account, Carbon $start, Carbon $end): JsonResponse
2016-12-11 04:15:19 -06:00
{
2020-03-20 11:59:56 -05:00
$chartData = [];
2022-10-30 08:24:19 -05:00
$cache = new CacheProperties();
2018-02-09 09:47:01 -06:00
$cache->addProperty('chart.account.period');
2016-12-11 04:15:19 -06:00
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($account->id);
if ($cache->has()) {
return response()->json($cache->get());
2016-12-11 04:15:19 -06:00
}
2020-03-19 22:37:45 -05:00
$currencies = $this->accountRepository->getUsedCurrencies($account);
// if the account is not expense or revenue, just use the account's default currency.
if (!in_array($account->accountType->type, [AccountType::REVENUE, AccountType::EXPENSE], true)) {
2020-04-11 23:24:35 -05:00
$currencies = [$this->accountRepository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency()];
}
2020-03-19 22:37:45 -05:00
/** @var TransactionCurrency $currency */
foreach ($currencies as $currency) {
$chartData[] = $this->periodByCurrency($start, $end, $account, $currency);
2016-12-11 04:15:19 -06:00
}
2020-03-19 22:37:45 -05:00
$data = $this->generator->multiSet($chartData);
2016-12-11 04:15:19 -06:00
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
2016-12-11 04:15:19 -06:00
}
2015-05-16 02:41:14 -05:00
/**
2016-05-13 08:53:39 -05:00
* Shows the balances for a given set of dates and accounts.
2015-05-16 02:41:14 -05:00
*
2022-10-30 05:43:17 -05:00
* TODO this chart is not multi currency aware.
*
2022-03-29 08:10:05 -05:00
* @throws FireflyException
* */
2018-07-08 05:08:53 -05:00
public function report(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
2015-05-16 02:41:14 -05:00
{
2018-03-10 13:30:09 -06:00
return response()->json($this->accountBalanceChart($accounts, $start, $end));
2015-05-16 02:41:14 -05:00
}
2021-03-28 04:46:23 -05:00
2016-10-14 12:59:10 -05:00
/**
* Shows the balances for all the user's revenue accounts.
*
* This chart is multi-currency aware.
2016-10-14 12:59:10 -05:00
*
* */
public function revenueAccounts(): JsonResponse
2016-10-14 12:59:10 -05:00
{
/** @var Carbon $start */
2023-02-11 00:36:45 -06:00
$start = clone session('start', today(config('app.timezone'))->startOfMonth());
2023-12-20 12:35:52 -06:00
/** @var Carbon $end */
2023-02-11 00:36:45 -06:00
$end = clone session('end', today(config('app.timezone'))->endOfMonth());
2022-10-30 08:24:19 -05:00
$cache = new CacheProperties();
2016-10-14 12:59:10 -05:00
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-11 10:05:48 -06:00
$cache->addProperty('chart.account.revenue-accounts');
2016-10-14 12:59:10 -05:00
if ($cache->has()) {
return response()->json($cache->get());
2016-10-14 12:59:10 -05:00
}
$start->subDay();
// prep some vars:
$currencies = [];
$chartData = [];
$tempData = [];
// grab all accounts and names
2019-01-27 03:51:00 -06:00
$accounts = $this->accountRepository->getAccountsByType([AccountType::REVENUE]);
$accountNames = $this->extractNames($accounts);
// grab all balances
$startBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $start);
$endBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $end);
// loop the end balances. This is an array for each account ($expenses)
foreach ($endBalances as $accountId => $expenses) {
2022-11-03 23:11:05 -05:00
$accountId = (int)$accountId;
// loop each expense entry (each entry can be a different currency).
foreach ($expenses as $currencyId => $endAmount) {
2022-11-03 23:11:05 -05:00
$currencyId = (int)$currencyId;
// see if there is an accompanying start amount.
// grab the difference and find the currency.
$startAmount = (string)($startBalances[$accountId][$currencyId] ?? '0');
2022-11-03 23:11:05 -05:00
$diff = bcsub((string)$endAmount, $startAmount);
2023-12-09 23:45:59 -06:00
$currencies[$currencyId] ??= $this->currencyRepository->find($currencyId);
if (0 !== bccomp($diff, '0')) {
// store the values in a temporary array.
$tempData[] = [
'name' => $accountNames[$accountId],
'difference' => $diff,
2022-12-29 12:41:57 -06:00
'diff_float' => (float)$diff, // intentional float
'currency_id' => $currencyId,
];
}
2016-10-14 12:59:10 -05:00
}
2016-12-11 09:38:21 -06:00
}
2016-10-14 12:59:10 -05:00
// sort temp array by amount.
$amounts = array_column($tempData, 'diff_float');
2020-07-26 07:18:25 -05:00
array_multisort($amounts, SORT_ASC, $tempData);
// loop all found currencies and build the data array for the chart.
/**
2023-06-21 05:34:58 -05:00
* @var int $currencyId
* @var TransactionCurrency $currency
*/
foreach ($currencies as $currencyId => $currency) {
$dataSet
= [
2023-12-20 12:35:52 -06:00
'label' => (string)trans('firefly.earned'),
'type' => 'bar',
'currency_symbol' => $currency->symbol,
'currency_code' => $currency->code,
'entries' => $this->expandNames($tempData),
];
$chartData[$currencyId] = $dataSet;
}
// loop temp data and place data in correct array:
foreach ($tempData as $entry) {
$currencyId = $entry['currency_id'];
$name = $entry['name'];
$chartData[$currencyId]['entries'][$name] = bcmul($entry['difference'], '-1');
}
$data = $this->generator->multiSet($chartData);
2016-10-14 12:59:10 -05:00
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
2016-10-14 12:59:10 -05:00
}
2023-12-20 12:35:52 -06:00
/**
* @throws FireflyException
* */
2023-12-20 12:35:52 -06:00
private function periodByCurrency(Carbon $start, Carbon $end, Account $account, TransactionCurrency $currency): array
{
app('log')->debug(sprintf('Now in periodByCurrency("%s", "%s", %s, "%s")', $start->format('Y-m-d'), $end->format('Y-m-d'), $account->id, $currency->code));
$locale = app('steam')->getLocale();
$step = $this->calculateStep($start, $end);
$result = [
'label' => sprintf('%s (%s)', $account->name, $currency->symbol),
'currency_symbol' => $currency->symbol,
'currency_code' => $currency->code,
];
$entries = [];
$current = clone $start;
app('log')->debug(sprintf('Step is %s', $step));
// fix for issue https://github.com/firefly-iii/firefly-iii/issues/8041
// have to make sure this chart is always based on the balance at the END of the period.
// This period depends on the size of the chart
$current = app('navigation')->endOfX($current, $step, null);
app('log')->debug(sprintf('$current date is %s', $current->format('Y-m-d')));
if ('1D' === $step) {
// per day the entire period, balance for every day.
$format = (string)trans('config.month_and_day_js', [], $locale);
$range = app('steam')->balanceInRange($account, $start, $end, $currency);
$previous = array_values($range)[0];
while ($end >= $current) {
$theDate = $current->format('Y-m-d');
$balance = $range[$theDate] ?? $previous;
$label = $current->isoFormat($format);
$entries[$label] = (float)$balance;
$previous = $balance;
$current->addDay();
}
}
if ('1W' === $step || '1M' === $step || '1Y' === $step) {
while ($end >= $current) {
app('log')->debug(sprintf('Current is: %s', $current->format('Y-m-d')));
$balance = (float)app('steam')->balance($account, $current, $currency);
$label = app('navigation')->periodShow($current, $step);
$entries[$label] = $balance;
$current = app('navigation')->addPeriod($current, $step, 0);
// here too, to fix #8041, the data is corrected to the end of the period.
$current = app('navigation')->endOfX($current, $step, null);
}
}
$result['entries'] = $entries;
return $result;
}
2015-05-20 12:56:14 -05:00
}