2015-05-16 02:09:52 -05:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* ReportController.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
|
|
|
|
2016-02-05 05:08:25 -06:00
|
|
|
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-05-02 13:49:19 -05:00
|
|
|
use FireflyIII\Generator\Chart\Report\ReportChartGeneratorInterface;
|
2015-05-16 02:41:14 -05:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
2016-10-09 00:58:27 -05:00
|
|
|
use FireflyIII\Repositories\Account\AccountTaskerInterface;
|
2015-06-27 04:44:18 -05:00
|
|
|
use FireflyIII\Support\CacheProperties;
|
|
|
|
use Illuminate\Support\Collection;
|
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-05-02 13:49:19 -05:00
|
|
|
/** @var ReportChartGeneratorInterface */
|
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-05-02 13:49:19 -05:00
|
|
|
$this->generator = app(ReportChartGeneratorInterface::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.
|
|
|
|
*
|
2016-02-18 03:12:07 -06:00
|
|
|
* @param string $reportType
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
2016-02-18 03:04:53 -06:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2016-02-18 03:12:07 -06:00
|
|
|
public function netWorth(string $reportType, Carbon $start, Carbon $end, Collection $accounts)
|
2016-02-18 03:04:53 -06:00
|
|
|
{
|
|
|
|
// chart properties for cache:
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty('netWorth');
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($reportType);
|
|
|
|
$cache->addProperty($accounts);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
if ($cache->has()) {
|
2016-04-06 02:27:45 -05:00
|
|
|
return Response::json($cache->get());
|
2016-02-18 03:04:53 -06:00
|
|
|
}
|
|
|
|
$ids = $accounts->pluck('id')->toArray();
|
|
|
|
$current = clone $start;
|
|
|
|
$entries = new Collection;
|
|
|
|
while ($current < $end) {
|
|
|
|
$balances = Steam::balancesById($ids, $current);
|
2016-03-14 14:52:08 -05:00
|
|
|
$sum = $this->arraySum($balances);
|
2016-02-18 03:04:53 -06:00
|
|
|
$entries->push(
|
|
|
|
[
|
|
|
|
'date' => clone $current,
|
|
|
|
'net-worth' => $sum,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$current->addDays(7);
|
|
|
|
}
|
|
|
|
$data = $this->generator->netWorth($entries);
|
|
|
|
|
2016-02-18 03:12:07 -06:00
|
|
|
$cache->store($data);
|
2016-02-18 03:04:53 -06:00
|
|
|
|
|
|
|
return Response::json($data);
|
|
|
|
}
|
|
|
|
|
2015-05-16 02:09:52 -05:00
|
|
|
|
|
|
|
/**
|
2016-10-09 00:58:27 -05:00
|
|
|
* @param AccountTaskerInterface $accountTasker
|
|
|
|
* @param string $reportType
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
2016-01-15 11:21:59 -06:00
|
|
|
*
|
2015-12-12 12:04:30 -06:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2016-10-09 00:58:27 -05:00
|
|
|
* @internal param AccountRepositoryInterface $repository
|
2015-05-16 02:09:52 -05:00
|
|
|
*/
|
2016-10-09 00:58:27 -05:00
|
|
|
public function yearInOut(AccountTaskerInterface $accountTasker, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
|
2015-05-16 02:09:52 -05:00
|
|
|
{
|
2015-06-27 04:44:18 -05:00
|
|
|
// chart properties for cache:
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty('yearInOut');
|
2015-12-12 12:04:30 -06:00
|
|
|
$cache->addProperty($start);
|
2016-01-01 14:49:27 -06:00
|
|
|
$cache->addProperty($reportType);
|
2015-12-12 14:20:20 -06:00
|
|
|
$cache->addProperty($accounts);
|
2015-12-12 12:04:30 -06:00
|
|
|
$cache->addProperty($end);
|
2015-06-27 04:44:18 -05:00
|
|
|
if ($cache->has()) {
|
2016-05-15 11:36:40 -05:00
|
|
|
return Response::json($cache->get());
|
2015-06-27 04:44:18 -05:00
|
|
|
}
|
2015-05-16 02:09:52 -05:00
|
|
|
|
2016-05-14 07:02:12 -05:00
|
|
|
// always per month.
|
|
|
|
$currentStart = clone $start;
|
|
|
|
$spentArray = [];
|
|
|
|
$earnedArray = [];
|
|
|
|
while ($currentStart <= $end) {
|
|
|
|
$currentEnd = Navigation::endOfPeriod($currentStart, '1M');
|
|
|
|
$date = $currentStart->format('Y-m');
|
2016-10-09 00:58:27 -05:00
|
|
|
$spent = $accountTasker->amountOutInPeriod($accounts, $accounts, $currentStart, $currentEnd);
|
|
|
|
$earned = $accountTasker->amountInInPeriod($accounts, $accounts, $currentStart, $currentEnd);
|
2016-05-19 01:57:25 -05:00
|
|
|
$spentArray[$date] = bcmul($spent, '-1');
|
2016-05-14 07:02:12 -05:00
|
|
|
$earnedArray[$date] = $earned;
|
|
|
|
$currentStart = Navigation::addPeriod($currentStart, '1M', 0);
|
|
|
|
}
|
2015-12-14 14:11:57 -06:00
|
|
|
|
|
|
|
if ($start->diffInMonths($end) > 12) {
|
2016-01-02 09:57:31 -06:00
|
|
|
// data = method X
|
|
|
|
$data = $this->multiYearInOut($earnedArray, $spentArray, $start, $end);
|
2016-05-20 10:53:03 -05:00
|
|
|
$cache->store($data);
|
|
|
|
|
|
|
|
return Response::json($data);
|
2015-05-16 02:09:52 -05:00
|
|
|
}
|
|
|
|
|
2016-05-20 10:53:03 -05:00
|
|
|
// data = method Y
|
|
|
|
$data = $this->singleYearInOut($earnedArray, $spentArray, $start, $end);
|
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-05-20 10:53:03 -05:00
|
|
|
|
2015-05-16 02:09:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-09 00:58:27 -05:00
|
|
|
* @param AccountTaskerInterface $accountTasker
|
|
|
|
* @param string $reportType
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
2015-05-16 02:09:52 -05:00
|
|
|
*
|
2015-12-12 12:04:30 -06:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2016-10-09 00:58:27 -05:00
|
|
|
* @internal param AccountRepositoryInterface $repository
|
2015-05-16 02:09:52 -05:00
|
|
|
*/
|
2016-10-09 00:58:27 -05:00
|
|
|
public function yearInOutSummarized(AccountTaskerInterface $accountTasker, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
|
2015-05-16 02:09:52 -05:00
|
|
|
{
|
2015-06-27 04:44:18 -05:00
|
|
|
|
|
|
|
// chart properties for cache:
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty('yearInOutSummarized');
|
2015-12-12 12:04:30 -06:00
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
2016-01-01 14:49:27 -06:00
|
|
|
$cache->addProperty($reportType);
|
2015-12-12 12:04:30 -06:00
|
|
|
$cache->addProperty($accounts);
|
2015-06-27 04:44:18 -05:00
|
|
|
if ($cache->has()) {
|
2016-04-06 02:27:45 -05:00
|
|
|
return Response::json($cache->get());
|
2015-06-27 04:44:18 -05:00
|
|
|
}
|
2016-05-14 07:02:12 -05:00
|
|
|
|
|
|
|
// always per month.
|
|
|
|
$currentStart = clone $start;
|
|
|
|
$spentArray = [];
|
|
|
|
$earnedArray = [];
|
|
|
|
while ($currentStart <= $end) {
|
|
|
|
$currentEnd = Navigation::endOfPeriod($currentStart, '1M');
|
|
|
|
$date = $currentStart->format('Y-m');
|
2016-10-09 00:58:27 -05:00
|
|
|
$spent = $accountTasker->amountOutInPeriod($accounts, $accounts, $currentStart, $currentEnd);
|
|
|
|
$earned = $accountTasker->amountInInPeriod($accounts, $accounts, $currentStart, $currentEnd);
|
2016-05-19 01:57:25 -05:00
|
|
|
$spentArray[$date] = bcmul($spent, '-1');
|
2016-05-14 07:02:12 -05:00
|
|
|
$earnedArray[$date] = $earned;
|
|
|
|
$currentStart = Navigation::addPeriod($currentStart, '1M', 0);
|
|
|
|
}
|
|
|
|
|
2015-12-14 14:11:57 -06:00
|
|
|
if ($start->diffInMonths($end) > 12) {
|
|
|
|
// per year
|
2016-01-02 09:57:31 -06:00
|
|
|
$data = $this->multiYearInOutSummarized($earnedArray, $spentArray, $start, $end);
|
2016-05-20 10:53:03 -05:00
|
|
|
$cache->store($data);
|
|
|
|
|
|
|
|
return Response::json($data);
|
2016-01-02 09:57:31 -06:00
|
|
|
}
|
2016-05-20 10:53:03 -05:00
|
|
|
// per month!
|
|
|
|
$data = $this->singleYearInOutSummarized($earnedArray, $spentArray, $start, $end);
|
2016-01-02 09:57:31 -06:00
|
|
|
$cache->store($data);
|
2015-12-14 14:11:57 -06:00
|
|
|
|
2016-01-02 09:57:31 -06:00
|
|
|
return Response::json($data);
|
2016-05-20 10:53:03 -05:00
|
|
|
|
2016-01-02 09:57:31 -06:00
|
|
|
}
|
2015-12-14 14:11:57 -06:00
|
|
|
|
2016-01-02 09:57:31 -06:00
|
|
|
/**
|
|
|
|
* @param array $earned
|
|
|
|
* @param array $spent
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-02-05 05:28:05 -06:00
|
|
|
protected function multiYearInOut(array $earned, array $spent, Carbon $start, Carbon $end)
|
2016-01-02 09:57:31 -06:00
|
|
|
{
|
2016-02-05 05:28:05 -06:00
|
|
|
$entries = new Collection;
|
2016-01-02 09:57:31 -06:00
|
|
|
while ($start < $end) {
|
|
|
|
|
2016-02-05 05:28:05 -06:00
|
|
|
$incomeSum = $this->pluckFromArray($start->year, $earned);
|
2016-05-16 05:51:52 -05:00
|
|
|
$expenseSum = $this->pluckFromArray($start->year, $spent);
|
2016-02-05 05:28:05 -06:00
|
|
|
|
|
|
|
$entries->push([clone $start, $incomeSum, $expenseSum]);
|
|
|
|
$start->addYear();
|
2016-01-02 09:57:31 -06:00
|
|
|
}
|
2015-12-14 14:11:57 -06:00
|
|
|
|
2016-02-05 05:28:05 -06:00
|
|
|
$data = $this->generator->multiYearInOut($entries);
|
2016-01-02 09:57:31 -06:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2015-12-14 14:11:57 -06:00
|
|
|
|
2016-01-02 09:57:31 -06:00
|
|
|
/**
|
|
|
|
* @param array $earned
|
|
|
|
* @param array $spent
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function multiYearInOutSummarized(array $earned, array $spent, Carbon $start, Carbon $end)
|
|
|
|
{
|
|
|
|
$income = '0';
|
|
|
|
$expense = '0';
|
|
|
|
$count = 0;
|
|
|
|
while ($start < $end) {
|
|
|
|
|
|
|
|
$currentIncome = $this->pluckFromArray($start->year, $earned);
|
2016-05-16 05:51:52 -05:00
|
|
|
$currentExpense = $this->pluckFromArray($start->year, $spent);
|
2016-01-02 09:57:31 -06:00
|
|
|
$income = bcadd($income, $currentIncome);
|
|
|
|
$expense = bcadd($expense, $currentExpense);
|
|
|
|
|
|
|
|
$count++;
|
|
|
|
$start->addYear();
|
2015-05-16 02:09:52 -05:00
|
|
|
}
|
|
|
|
|
2016-01-02 09:57:31 -06:00
|
|
|
$data = $this->generator->multiYearInOutSummarized($income, $expense, $count);
|
2015-05-16 02:09:52 -05:00
|
|
|
|
2016-01-02 09:57:31 -06:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-05 05:28:05 -06:00
|
|
|
* @param int $year
|
|
|
|
* @param array $set
|
2016-01-02 09:57:31 -06:00
|
|
|
*
|
2016-02-05 05:28:05 -06:00
|
|
|
* @return string
|
2016-01-02 09:57:31 -06:00
|
|
|
*/
|
2016-02-05 05:28:05 -06:00
|
|
|
protected function pluckFromArray($year, array $set)
|
2016-01-02 09:57:31 -06:00
|
|
|
{
|
2016-02-05 05:28:05 -06:00
|
|
|
$sum = '0';
|
|
|
|
foreach ($set as $date => $amount) {
|
|
|
|
if (substr($date, 0, 4) == $year) {
|
|
|
|
$sum = bcadd($sum, $amount);
|
|
|
|
}
|
2016-01-02 09:57:31 -06:00
|
|
|
}
|
|
|
|
|
2016-02-05 05:28:05 -06:00
|
|
|
return $sum;
|
2016-01-02 09:57:31 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $earned
|
|
|
|
* @param array $spent
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function singleYearInOut(array $earned, array $spent, Carbon $start, Carbon $end)
|
|
|
|
{
|
|
|
|
// per month? simply use each month.
|
|
|
|
|
|
|
|
$entries = new Collection;
|
|
|
|
while ($start < $end) {
|
|
|
|
// total income and total expenses:
|
|
|
|
$date = $start->format('Y-m');
|
2016-05-16 02:05:06 -05:00
|
|
|
$incomeSum = isset($earned[$date]) ? $earned[$date] : 0;
|
|
|
|
$expenseSum = isset($spent[$date]) ? $spent[$date] : 0;
|
2016-01-02 09:57:31 -06:00
|
|
|
|
|
|
|
$entries->push([clone $start, $incomeSum, $expenseSum]);
|
|
|
|
$start->addMonth();
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $this->generator->yearInOut($entries);
|
2015-05-16 02:09:52 -05:00
|
|
|
|
2016-01-02 09:57:31 -06:00
|
|
|
return $data;
|
2015-05-16 02:09:52 -05:00
|
|
|
}
|
2015-12-31 01:26:04 -06:00
|
|
|
|
|
|
|
/**
|
2016-02-05 05:28:05 -06:00
|
|
|
* @param array $earned
|
|
|
|
* @param array $spent
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2015-12-31 01:26:04 -06:00
|
|
|
*
|
2016-02-05 05:28:05 -06:00
|
|
|
* @return array
|
2015-12-31 01:26:04 -06:00
|
|
|
*/
|
2016-02-05 05:28:05 -06:00
|
|
|
protected function singleYearInOutSummarized(array $earned, array $spent, Carbon $start, Carbon $end)
|
2015-12-31 01:26:04 -06:00
|
|
|
{
|
2016-02-05 05:28:05 -06:00
|
|
|
$income = '0';
|
|
|
|
$expense = '0';
|
|
|
|
$count = 0;
|
|
|
|
while ($start < $end) {
|
|
|
|
$date = $start->format('Y-m');
|
2016-05-16 02:05:06 -05:00
|
|
|
$currentIncome = isset($earned[$date]) ? $earned[$date] : 0;
|
|
|
|
$currentExpense = isset($spent[$date]) ? $spent[$date] : 0;
|
2016-02-05 05:28:05 -06:00
|
|
|
$income = bcadd($income, $currentIncome);
|
|
|
|
$expense = bcadd($expense, $currentExpense);
|
|
|
|
|
|
|
|
$count++;
|
|
|
|
$start->addMonth();
|
2015-12-31 01:26:04 -06:00
|
|
|
}
|
|
|
|
|
2016-02-05 05:28:05 -06:00
|
|
|
$data = $this->generator->yearInOutSummarized($income, $expense, $count);
|
2015-12-31 01:26:04 -06:00
|
|
|
|
2016-02-05 05:28:05 -06:00
|
|
|
return $data;
|
2015-12-31 01:26:04 -06:00
|
|
|
}
|
2016-02-18 03:04:53 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $array
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-14 14:52:08 -05: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;
|
|
|
|
}
|
2015-05-20 12:56:14 -05:00
|
|
|
}
|