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

263 lines
11 KiB
PHP
Raw Normal View History

2015-05-16 02:09:52 -05:00
<?php
2022-12-29 12:41:57 -06:00
/**
* ReportController.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:09:52 -05:00
2015-05-16 02:41:14 -05:00
namespace FireflyIII\Http\Controllers\Chart;
2015-05-16 02:09:52 -05:00
use Carbon\Carbon;
2022-12-29 12:41:57 -06:00
use FireflyIII\Exceptions\FireflyException;
2016-12-15 07:38:05 -06:00
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Helpers\Report\NetWorthInterface;
2015-05-16 02:41:14 -05:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2015-06-27 04:44:18 -05:00
use FireflyIII\Support\CacheProperties;
2018-08-09 09:16:27 -05:00
use FireflyIII\Support\Http\Controllers\BasicDataSupport;
2018-12-31 01:11:57 -06:00
use FireflyIII\Support\Http\Controllers\ChartGeneration;
2018-07-01 02:27:22 -05:00
use Illuminate\Http\JsonResponse;
2015-06-27 04:44:18 -05:00
use Illuminate\Support\Collection;
use JsonException;
2023-04-01 00:04:42 -05:00
use Illuminate\Support\Facades\Log;
2015-05-16 02:09:52 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class ReportController.
2015-05-16 02:09:52 -05:00
*/
2015-05-16 02:41:14 -05:00
class ReportController extends Controller
2015-05-16 02:09:52 -05:00
{
2022-10-30 08:24:28 -05:00
use BasicDataSupport;
use ChartGeneration;
2021-03-28 04:46:23 -05:00
2018-07-21 01:06:24 -05:00
/** @var GeneratorInterface Chart generation methods. */
2015-06-27 04:44:18 -05:00
protected $generator;
/**
2018-07-21 01:06:24 -05:00
* ReportController constructor.
*
2015-06-27 04:44:18 -05:00
*/
public function __construct()
{
parent::__construct();
// create chart generator:
2016-12-15 07:38:05 -06:00
$this->generator = app(GeneratorInterface::class);
2015-06-27 04:44:18 -05:00
}
2016-02-18 03:04:53 -06: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.
*
2022-12-29 12:41:57 -06:00
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
2016-12-14 11:59:12 -06:00
*
2018-07-01 02:27:22 -05:00
* @return JsonResponse
2022-12-29 12:41:57 -06:00
* @throws FireflyException
2016-02-18 03:04:53 -06:00
*/
2018-07-01 02:27:22 -05:00
public function netWorth(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
2016-02-18 03:04:53 -06:00
{
// chart properties for cache:
2022-10-30 08:24:28 -05:00
$cache = new CacheProperties();
2016-12-15 07:38:05 -06:00
$cache->addProperty('chart.report.net-worth');
2016-02-18 03:04:53 -06:00
$cache->addProperty($start);
$cache->addProperty(implode(',', $accounts->pluck('id')->toArray()));
2016-02-18 03:04:53 -06:00
$cache->addProperty($end);
if ($cache->has()) {
return response()->json($cache->get());
2016-02-18 03:04:53 -06:00
}
2021-03-28 04:46:23 -05:00
$locale = app('steam')->getLocale();
2016-12-15 07:38:05 -06:00
$current = clone $start;
$chartData = [];
/** @var NetWorthInterface $helper */
$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 01:11:57 -06:00
$filtered = $accounts->filter(
2019-08-17 01:00:27 -05:00
static function (Account $account) use ($accountRepository) {
$includeNetWorth = $accountRepository->getMetaValue($account, 'include_net_worth');
$result = null === $includeNetWorth ? true : '1' === $includeNetWorth;
if (false === $result) {
Log::debug(sprintf('Will not include "%s" in net worth charts.', $account->name));
}
return $result;
}
);
2022-10-30 05:43:17 -05:00
// TODO get liabilities and include those as well?
2016-02-18 03:04:53 -06:00
while ($current < $end) {
// get balances by date, grouped by currency.
$result = $helper->getNetWorthByCurrency($filtered, $current);
// loop result, add to array.
/** @var array $netWorthItem */
foreach ($result as $netWorthItem) {
$currencyId = $netWorthItem['currency']->id;
2022-12-29 12:41:57 -06:00
$label = $current->isoFormat((string)trans('config.month_and_day_js', [], $locale));
2021-04-07 00:28:43 -05:00
if (!array_key_exists($currencyId, $chartData)) {
$chartData[$currencyId] = [
2022-12-29 12:41:57 -06:00
'label' => 'Net worth in '.$netWorthItem['currency']->name,
'type' => 'line',
'currency_symbol' => $netWorthItem['currency']->symbol,
2021-03-28 04:46:23 -05:00
'currency_code' => $netWorthItem['currency']->code,
'entries' => [],
];
}
$chartData[$currencyId]['entries'][$label] = $netWorthItem['balance'];
}
2016-02-18 03:04:53 -06:00
$current->addDays(7);
}
$data = $this->generator->multiSet($chartData);
$cache->store($data);
2016-02-18 03:04:53 -06:00
2018-03-10 13:30:09 -06:00
return response()->json($data);
2016-02-18 03:04:53 -06:00
}
2015-05-16 02:09:52 -05:00
/**
2017-12-19 11:53:50 -06:00
* Shows income and expense, debit/credit: operations.
2016-12-06 01:15:53 -06:00
*
2022-12-29 12:41:57 -06:00
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
2016-12-06 01:15:53 -06:00
*
* @return JsonResponse
* @throws JsonException
2015-05-16 02:09:52 -05:00
*/
2018-07-08 05:28:42 -05:00
public function operations(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
2015-05-16 02:09:52 -05:00
{
2015-06-27 04:44:18 -05:00
// chart properties for cache:
2022-10-30 08:24:28 -05:00
$cache = new CacheProperties();
2016-12-15 07:38:05 -06:00
$cache->addProperty('chart.report.operations');
$cache->addProperty($start);
2015-12-12 14:20:20 -06:00
$cache->addProperty($accounts);
$cache->addProperty($end);
2015-06-27 04:44:18 -05:00
if ($cache->has()) {
return response()->json($cache->get());
2015-06-27 04:44:18 -05:00
}
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();
// get journals for entire period:
$data = [];
$chartData = [
];
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2019-09-06 10:18:55 -05:00
$collector->setRange($start, $end)->withAccountInformation();
$collector->setXorAccounts($accounts);
$collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::RECONCILIATION, TransactionType::TRANSFER]);
$journals = $collector->getExtractedJournals();
// loop. group by currency and by period.
/** @var array $journal */
foreach ($journals as $journal) {
$period = $journal['date']->format($format);
2022-12-29 12:41:57 -06:00
$currencyId = (int)$journal['currency_id'];
$data[$currencyId] = $data[$currencyId] ?? [
'currency_id' => $currencyId,
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
2022-12-29 12:41:57 -06:00
'currency_decimal_places' => (int)$journal['currency_decimal_places'],
];
$data[$currencyId][$period] = $data[$currencyId][$period] ?? [
'period' => $period,
'spent' => '0',
'earned' => '0',
];
// in our outgoing?
$key = 'spent';
$amount = app('steam')->positive($journal['amount']);
2020-01-01 12:55:02 -06:00
// deposit = incoming
// transfer or reconcile or opening balance, and these accounts are the destination.
2020-01-01 12:55:02 -06:00
if (
TransactionType::DEPOSIT === $journal['transaction_type_type']
||
2020-01-01 12:55:02 -06:00
(
(
TransactionType::TRANSFER === $journal['transaction_type_type']
|| TransactionType::RECONCILIATION === $journal['transaction_type_type']
2020-01-01 12:55:02 -06:00
|| 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) {
$income = [
2022-12-29 12:41:57 -06:00
'label' => (string)trans('firefly.box_earned_in_currency', ['currency' => $currency['currency_name']]),
2018-07-01 02:43:44 -05:00
'type' => 'bar',
'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
2020-07-27 23:25:14 -05:00
'currency_code' => $currency['currency_code'],
2018-07-01 02:43:44 -05:00
'entries' => [],
];
$expense = [
2022-12-29 12:41:57 -06:00
'label' => (string)trans('firefly.box_spent_in_currency', ['currency' => $currency['currency_name']]),
2018-07-01 02:43:44 -05:00
'type' => 'bar',
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
2020-07-27 23:25:14 -05:00
'currency_code' => $currency['currency_code'],
2018-07-01 02:43:44 -05:00
'entries' => [],
2015-05-16 02:09:52 -05:00
];
// loop all possible periods between $start and $end
$currentStart = clone $start;
while ($currentStart <= $end) {
$key = $currentStart->format($format);
2022-03-27 13:24:13 -05:00
$title = $currentStart->isoFormat($titleFormat);
2022-12-23 22:06:39 -06: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);
}
2017-04-17 01:31:42 -05:00
$chartData[] = $income;
$chartData[] = $expense;
2016-01-02 09:57:31 -06:00
}
2016-12-15 07:38:05 -06:00
$data = $this->generator->multiSet($chartData);
$cache->store($data);
2015-12-31 01:26:04 -06:00
2018-03-10 13:30:09 -06:00
return response()->json($data);
2015-12-31 01:26:04 -06:00
}
2015-05-20 12:56:14 -05:00
}