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

310 lines
9.6 KiB
PHP
Raw Normal View History

2015-05-16 02:09:52 -05:00
<?php
/**
* ReportController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://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;
2016-12-15 07:38:05 -06:00
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
2015-05-16 02:41:14 -05:00
use FireflyIII\Http\Controllers\Controller;
2017-04-17 01:31:42 -05:00
use FireflyIII\Repositories\Account\AccountTaskerInterface;
2015-06-27 04:44:18 -05:00
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
use Log;
2016-05-14 07:02:12 -05:00
use Navigation;
2015-05-16 02:09:52 -05:00
use Response;
2016-02-18 03:04:53 -06:00
use Steam;
2015-05-16 02:09:52 -05:00
/**
2015-05-16 02:41:14 -05:00
* Class ReportController
2015-05-16 02:09:52 -05:00
*
2015-05-16 02:41:14 -05:00
* @package FireflyIII\Http\Controllers\Chart
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
{
2016-12-15 07:38:05 -06:00
/** @var GeneratorInterface */
2015-06-27 04:44:18 -05:00
protected $generator;
/**
2016-02-04 00:28:39 -06:00
*
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.
*
* @param Collection $accounts
2016-12-14 11:59:12 -06:00
* @param Carbon $start
2016-12-06 01:15:53 -06:00
* @param Carbon $end
2016-12-14 11:59:12 -06:00
*
2016-02-18 03:04:53 -06:00
* @return \Illuminate\Http\JsonResponse
*/
2016-12-06 01:15:53 -06:00
public function netWorth(Collection $accounts, Carbon $start, Carbon $end)
2016-02-18 03:04:53 -06:00
{
// chart properties for cache:
$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($accounts);
$cache->addProperty($end);
if ($cache->has()) {
2017-03-04 04:19:44 -06:00
return Response::json($cache->get()); // @codeCoverageIgnore
2016-02-18 03:04:53 -06:00
}
2016-12-15 07:38:05 -06:00
$current = clone $start;
$chartData = [];
2016-02-18 03:04:53 -06:00
while ($current < $end) {
$balances = Steam::balancesByAccounts($accounts, $current);
2016-12-15 07:38:05 -06:00
$sum = $this->arraySum($balances);
$label = $current->formatLocalized(strval(trans('config.month_and_day')));
$chartData[$label] = $sum;
2016-02-18 03:04:53 -06:00
$current->addDays(7);
}
2016-12-15 07:38:05 -06:00
$data = $this->generator->singleSet(strval(trans('firefly.net_worth')), $chartData);
$cache->store($data);
2016-02-18 03:04:53 -06:00
return Response::json($data);
}
2015-05-16 02:09:52 -05:00
/**
2016-12-06 01:15:53 -06:00
* Shows income and expense, debet/credit: operations
*
* @param Collection $accounts
2016-11-12 12:22:03 -06:00
* @param Carbon $start
* @param Carbon $end
2016-12-06 01:15:53 -06:00
*
*
* @return \Illuminate\Http\JsonResponse
2015-05-16 02:09:52 -05:00
*/
2016-12-06 01:15:53 -06:00
public function operations(Collection $accounts, Carbon $start, Carbon $end)
2015-05-16 02:09:52 -05:00
{
2015-06-27 04:44:18 -05:00
// chart properties for cache:
$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()) {
2017-06-05 04:12:50 -05:00
return Response::json($cache->get()); // @codeCoverageIgnore
2015-06-27 04:44:18 -05:00
}
Log::debug('Going to do operations for accounts ', $accounts->pluck('id')->toArray());
2016-12-15 07:38:05 -06:00
$format = Navigation::preferredCarbonLocalizedFormat($start, $end);
$source = $this->getChartData($accounts, $start, $end);
$chartData = [
[
'label' => trans('firefly.income'),
'type' => 'bar',
'entries' => [],
],
[
'label' => trans('firefly.expenses'),
'type' => 'bar',
'entries' => [],
],
];
2015-05-16 02:09:52 -05:00
2016-12-15 07:38:05 -06:00
foreach ($source['earned'] as $date => $amount) {
$carbon = new Carbon($date);
$label = $carbon->formatLocalized($format);
$earned = $chartData[0]['entries'][$label] ?? '0';
$chartData[0]['entries'][$label] = bcadd($earned, $amount);
}
foreach ($source['spent'] as $date => $amount) {
$carbon = new Carbon($date);
$label = $carbon->formatLocalized($format);
$spent = $chartData[1]['entries'][$label] ?? '0';
$chartData[1]['entries'][$label] = bcadd($spent, $amount);
2015-05-16 02:09:52 -05:00
}
2016-12-15 07:38:05 -06:00
$data = $this->generator->multiSet($chartData);
2016-01-02 09:57:31 -06:00
$cache->store($data);
2015-06-27 04:44:18 -05:00
return Response::json($data);
2015-05-16 02:09:52 -05:00
}
/**
2016-12-06 01:15:53 -06:00
* Shows sum income and expense, debet/credit: operations
2016-12-14 11:59:12 -06:00
*
2016-11-20 04:43:19 -06:00
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
2015-05-16 02:09:52 -05:00
*
* @return \Illuminate\Http\JsonResponse
2015-05-16 02:09:52 -05:00
*/
2016-12-06 01:15:53 -06:00
public function sum(Collection $accounts, Carbon $start, Carbon $end)
2015-05-16 02:09:52 -05:00
{
2015-06-27 04:44:18 -05:00
2016-12-15 07:38:05 -06:00
2015-06-27 04:44:18 -05:00
// chart properties for cache:
$cache = new CacheProperties;
2016-12-15 07:38:05 -06:00
$cache->addProperty('chart.report.sum');
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($accounts);
2015-06-27 04:44:18 -05:00
if ($cache->has()) {
2017-03-04 04:19:44 -06:00
return Response::json($cache->get()); // @codeCoverageIgnore
2015-06-27 04:44:18 -05:00
}
2017-04-17 01:31:42 -05:00
2016-12-15 07:38:05 -06:00
$source = $this->getChartData($accounts, $start, $end);
$numbers = [
'sum_earned' => '0',
'avg_earned' => '0',
'count_earned' => 0,
'sum_spent' => '0',
'avg_spent' => '0',
'count_spent' => 0,
];
foreach ($source['earned'] as $amount) {
$numbers['sum_earned'] = bcadd($amount, $numbers['sum_earned']);
$numbers['count_earned']++;
2016-01-02 09:57:31 -06:00
}
2016-12-15 07:38:05 -06:00
if ($numbers['count_earned'] > 0) {
$numbers['avg_earned'] = $numbers['sum_earned'] / $numbers['count_earned'];
2015-05-16 02:09:52 -05:00
}
2016-12-15 07:38:05 -06:00
foreach ($source['spent'] as $amount) {
$numbers['sum_spent'] = bcadd($amount, $numbers['sum_spent']);
$numbers['count_spent']++;
2016-01-02 09:57:31 -06:00
}
2016-12-15 07:38:05 -06:00
if ($numbers['count_spent'] > 0) {
$numbers['avg_spent'] = $numbers['sum_spent'] / $numbers['count_spent'];
2016-01-02 09:57:31 -06:00
}
2016-12-15 07:38:05 -06:00
$chartData = [
[
'label' => strval(trans('firefly.income')),
'type' => 'bar',
'entries' => [
strval(trans('firefly.sum_of_period')) => $numbers['sum_earned'],
strval(trans('firefly.average_in_period')) => $numbers['avg_earned'],
],
],
[
'label' => trans('firefly.expenses'),
'type' => 'bar',
'entries' => [
strval(trans('firefly.sum_of_period')) => $numbers['sum_spent'],
strval(trans('firefly.average_in_period')) => $numbers['avg_spent'],
],
],
];
2015-12-31 01:26:04 -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
2016-12-15 07:38:05 -06:00
return Response::json($data);
2015-12-31 01:26:04 -06:00
}
2016-02-18 03:04:53 -06:00
/**
* @param $array
*
* @return string
*/
2016-12-14 11:59:12 -06:00
private function arraySum($array): string
2016-02-18 03:04:53 -06:00
{
$sum = '0';
foreach ($array as $entry) {
$sum = bcadd($sum, $entry);
}
return $sum;
}
2016-11-12 12:22:03 -06:00
/**
2016-12-15 07:38:05 -06:00
* Collects the incomes and expenses for the given periods, grouped per month. Will cache its results
*
2016-11-12 12:22:03 -06:00
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
2016-12-15 07:38:05 -06:00
private function getChartData(Collection $accounts, Carbon $start, Carbon $end): array
2016-11-12 12:22:03 -06:00
{
2016-12-15 07:38:05 -06:00
$cache = new CacheProperties;
$cache->addProperty('chart.report.get-chart-data');
$cache->addProperty($start);
$cache->addProperty($accounts);
$cache->addProperty($end);
if ($cache->has()) {
2017-06-05 04:12:50 -05:00
return $cache->get(); // @codeCoverageIgnore
2016-12-15 07:38:05 -06:00
}
2016-11-12 12:22:03 -06:00
$currentStart = clone $start;
$spentArray = [];
$earnedArray = [];
2017-04-17 01:31:42 -05:00
/** @var AccountTaskerInterface $tasker */
$tasker = app(AccountTaskerInterface::class);
2016-11-12 12:22:03 -06:00
while ($currentStart <= $end) {
$currentEnd = Navigation::endOfPeriod($currentStart, '1M');
2017-04-17 01:31:42 -05:00
$earned = strval(
array_sum(
array_map(
function ($item) {
return $item['sum'];
}, $tasker->getIncomeReport($currentStart, $currentEnd, $accounts)
)
)
);
$spent = strval(
array_sum(
array_map(
function ($item) {
return $item['sum'];
}, $tasker->getExpenseReport($currentStart, $currentEnd, $accounts)
)
)
);
2016-12-15 07:38:05 -06:00
$label = $currentStart->format('Y-m') . '-01';
$spentArray[$label] = bcmul($spent, '-1');
$earnedArray[$label] = $earned;
$currentStart = Navigation::addPeriod($currentStart, '1M', 0);
2016-11-12 12:22:03 -06:00
}
2016-12-15 07:38:05 -06:00
$result = [
2016-11-12 12:22:03 -06:00
'spent' => $spentArray,
'earned' => $earnedArray,
];
2016-12-15 07:38:05 -06:00
$cache->store($result);
return $result;
2016-11-12 12:22:03 -06:00
}
2015-05-20 12:56:14 -05:00
}