Files
firefly-iii/app/Http/Controllers/Chart/ReportController.php

240 lines
10 KiB
PHP
Raw Normal View History

2015-05-16 09:09:52 +02:00
<?php
2022-12-29 19:41:57 +01:00
/**
* ReportController.php
2020-01-31 07:32:04 +01: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 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02: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 08:40:00 +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/>.
*/
declare(strict_types=1);
2015-05-16 09:09:52 +02:00
2015-05-16 09:41:14 +02:00
namespace FireflyIII\Http\Controllers\Chart;
2015-05-16 09:09:52 +02:00
use Carbon\Carbon;
2016-12-15 14:38:05 +01:00
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Helpers\Report\NetWorthInterface;
2015-05-16 09:41:14 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2015-06-27 11:44:18 +02:00
use FireflyIII\Support\CacheProperties;
2018-08-09 16:16:27 +02:00
use FireflyIII\Support\Http\Controllers\BasicDataSupport;
2018-12-31 08:11:57 +01:00
use FireflyIII\Support\Http\Controllers\ChartGeneration;
2018-07-01 09:27:22 +02:00
use Illuminate\Http\JsonResponse;
2015-06-27 11:44:18 +02:00
use Illuminate\Support\Collection;
2015-05-16 09:09:52 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class ReportController.
2015-05-16 09:09:52 +02:00
*/
2015-05-16 09:41:14 +02:00
class ReportController extends Controller
2015-05-16 09:09:52 +02:00
{
2022-10-30 14:24:28 +01:00
use BasicDataSupport;
use ChartGeneration;
2021-03-28 11:46:23 +02:00
2023-12-22 17:28:42 +01:00
protected GeneratorInterface $generator;
2015-06-27 11:44:18 +02:00
/**
2018-07-21 08:06:24 +02:00
* ReportController constructor.
2015-06-27 11:44:18 +02:00
*/
public function __construct()
{
parent::__construct();
// create chart generator:
2016-12-15 14:38:05 +01:00
$this->generator = app(GeneratorInterface::class);
2015-06-27 11:44:18 +02:00
}
2016-02-18 10:04:53 +01:00
/**
* This chart, by default, is shown on the multi-year and year report pages,
* which means that giving it a 2 week "period" should be enough granularity.
*/
2018-07-01 09:27:22 +02:00
public function netWorth(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
2016-02-18 10:04:53 +01:00
{
// chart properties for cache:
2024-01-14 05:10:05 +01:00
$cache = new CacheProperties();
2016-12-15 14:38:05 +01:00
$cache->addProperty('chart.report.net-worth');
2016-02-18 10:04:53 +01:00
$cache->addProperty($start);
$cache->addProperty(implode(',', $accounts->pluck('id')->toArray()));
2016-02-18 10:04:53 +01:00
$cache->addProperty($end);
if ($cache->has()) {
return response()->json($cache->get());
2016-02-18 10:04:53 +01:00
}
2024-01-14 05:10:05 +01:00
$locale = app('steam')->getLocale();
$current = clone $start;
$chartData = [];
2023-12-20 19:35:52 +01:00
/** @var NetWorthInterface $helper */
2024-01-14 05:10:05 +01:00
$helper = app(NetWorthInterface::class);
$helper->setUser(auth()->user());
// filter accounts on having the preference for being included.
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
2018-12-31 08:11:57 +01:00
$filtered = $accounts->filter(
2019-08-17 08:00:27 +02:00
static function (Account $account) use ($accountRepository) {
$includeNetWorth = $accountRepository->getMetaValue($account, 'include_net_worth');
$result = null === $includeNetWorth ? true : '1' === $includeNetWorth;
if (false === $result) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Will not include "%s" in net worth charts.', $account->name));
}
return $result;
}
);
2022-10-30 11:43:17 +01:00
// TODO get liabilities and include those as well?
2016-02-18 10:04:53 +01:00
while ($current < $end) {
// get balances by date, grouped by currency.
2023-10-29 05:54:01 +01:00
$result = $helper->byAccounts($filtered, $current);
// loop result, add to array.
/** @var array $netWorthItem */
2023-10-29 05:54:01 +01:00
foreach ($result as $key => $netWorthItem) {
2023-10-29 06:36:37 +01:00
if ('native' === $key) {
2023-10-29 05:54:01 +01:00
continue;
}
2024-01-14 05:10:05 +01:00
$currencyId = $netWorthItem['currency_id'];
$label = $current->isoFormat((string)trans('config.month_and_day_js', [], $locale));
2021-04-07 07:28:43 +02:00
if (!array_key_exists($currencyId, $chartData)) {
$chartData[$currencyId] = [
2024-01-14 05:10:05 +01:00
'label' => 'Net worth in '.$netWorthItem['currency_name'],
'type' => 'line',
2023-10-29 05:54:01 +01:00
'currency_symbol' => $netWorthItem['currency_symbol'],
'currency_code' => $netWorthItem['currency_code'],
'entries' => [],
];
}
$chartData[$currencyId]['entries'][$label] = $netWorthItem['balance'];
}
2016-02-18 10:04:53 +01:00
$current->addDays(7);
}
2024-01-14 05:10:05 +01:00
$data = $this->generator->multiSet($chartData);
$cache->store($data);
2016-02-18 10:04:53 +01:00
2018-03-10 20:30:09 +01:00
return response()->json($data);
2016-02-18 10:04:53 +01:00
}
2015-05-16 09:09:52 +02:00
/**
2017-12-19 18:53:50 +01:00
* Shows income and expense, debit/credit: operations.
2016-12-06 08:15:53 +01:00
*
2023-12-22 17:28:42 +01:00
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
2018-07-08 12:28:42 +02:00
public function operations(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
2015-05-16 09:09:52 +02:00
{
2015-06-27 11:44:18 +02:00
// chart properties for cache:
2024-01-14 05:10:05 +01:00
$cache = new CacheProperties();
2016-12-15 14:38:05 +01:00
$cache->addProperty('chart.report.operations');
$cache->addProperty($start);
2015-12-12 21:20:20 +01:00
$cache->addProperty($accounts);
$cache->addProperty($end);
2015-06-27 11:44:18 +02:00
if ($cache->has()) {
return response()->json($cache->get());
2015-06-27 11:44:18 +02:00
}
2023-10-29 06:33:43 +01:00
app('log')->debug('Going to do operations for accounts ', $accounts->pluck('id')->toArray());
$format = app('navigation')->preferredCarbonFormat($start, $end);
$titleFormat = app('navigation')->preferredCarbonLocalizedFormat($start, $end);
$preferredRange = app('navigation')->preferredRangeFormat($start, $end);
$ids = $accounts->pluck('id')->toArray();
2023-12-22 17:28:42 +01:00
$data = [];
$chartData = [];
2023-12-20 19:35:52 +01:00
/** @var GroupCollectorInterface $collector */
2024-01-14 05:10:05 +01:00
$collector = app(GroupCollectorInterface::class);
2019-09-06 17:18:55 +02:00
$collector->setRange($start, $end)->withAccountInformation();
$collector->setXorAccounts($accounts);
$collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::RECONCILIATION, TransactionType::TRANSFER]);
2024-01-14 05:10:05 +01:00
$journals = $collector->getExtractedJournals();
// loop. group by currency and by period.
/** @var array $journal */
foreach ($journals as $journal) {
2024-01-14 05:10:05 +01:00
$period = $journal['date']->format($format);
$currencyId = (int)$journal['currency_id'];
2023-12-10 06:51:59 +01:00
$data[$currencyId] ??= [
'currency_id' => $currencyId,
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_decimal_places' => (int)$journal['currency_decimal_places'],
];
2023-12-10 06:45:59 +01:00
$data[$currencyId][$period] ??= [
'period' => $period,
'spent' => '0',
'earned' => '0',
];
// in our outgoing?
2024-01-14 05:10:05 +01:00
$key = 'spent';
$amount = app('steam')->positive($journal['amount']);
2020-01-01 19:55:02 +01:00
// deposit = incoming
// transfer or reconcile or opening balance, and these accounts are the destination.
2023-12-22 17:28:42 +01:00
if (TransactionType::DEPOSIT === $journal['transaction_type_type'] || ((TransactionType::TRANSFER === $journal['transaction_type_type'] || TransactionType::RECONCILIATION === $journal['transaction_type_type'] || TransactionType::OPENING_BALANCE === $journal['transaction_type_type']) && in_array($journal['destination_account_id'], $ids, true))) {
$key = 'earned';
}
$data[$currencyId][$period][$key] = bcadd($data[$currencyId][$period][$key], $amount);
}
// loop this data, make chart bars for each currency:
/** @var array $currency */
foreach ($data as $currency) {
2024-01-14 05:10:05 +01:00
$income = [
'label' => (string)trans('firefly.box_earned_in_currency', ['currency' => $currency['currency_name']]),
2018-07-01 09:43:44 +02:00
'type' => 'bar',
'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
2020-07-28 06:25:14 +02:00
'currency_code' => $currency['currency_code'],
2018-07-01 09:43:44 +02:00
'entries' => [],
];
2024-01-14 05:10:05 +01:00
$expense = [
'label' => (string)trans('firefly.box_spent_in_currency', ['currency' => $currency['currency_name']]),
2018-07-01 09:43:44 +02:00
'type' => 'bar',
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
2020-07-28 06:25:14 +02:00
'currency_code' => $currency['currency_code'],
2018-07-01 09:43:44 +02:00
'entries' => [],
];
// loop all possible periods between $start and $end
$currentStart = clone $start;
$currentEnd = clone $end;
// #8374. Sloppy fix for yearly charts. Not really interested in a better fix with v2 layout and all.
if ('1Y' === $preferredRange) {
2024-01-14 05:10:05 +01:00
$currentEnd = app('navigation')->endOfPeriod($currentEnd, $preferredRange);
}
while ($currentStart <= $currentEnd) {
$key = $currentStart->format($format);
2022-03-27 20:24:13 +02:00
$title = $currentStart->isoFormat($titleFormat);
2023-12-20 19:35:52 +01:00
$income['entries'][$title] = app('steam')->bcround($currency[$key]['earned'] ?? '0', $currency['currency_decimal_places']);
$expense['entries'][$title] = app('steam')->bcround($currency[$key]['spent'] ?? '0', $currency['currency_decimal_places']);
$currentStart = app('navigation')->addPeriod($currentStart, $preferredRange, 0);
2024-01-14 05:10:05 +01:00
}
2017-04-17 08:31:42 +02:00
2024-01-14 05:10:05 +01:00
$chartData[] = $income;
$chartData[] = $expense;
2016-01-02 16:57:31 +01:00
}
2024-01-14 05:10:05 +01:00
$data = $this->generator->multiSet($chartData);
2016-12-15 14:38:05 +01:00
$cache->store($data);
2015-12-31 08:26:04 +01:00
2018-03-10 20:30:09 +01:00
return response()->json($data);
2015-12-31 08:26:04 +01:00
}
2015-05-20 19:56:14 +02:00
}