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

126 lines
3.8 KiB
PHP
Raw Normal View History

2015-05-16 02:09:52 -05:00
<?php
2015-05-16 02:41:14 -05:00
namespace FireflyIII\Http\Controllers\Chart;
2015-05-16 02:09:52 -05:00
use Carbon\Carbon;
use FireflyIII\Helpers\Report\ReportQueryInterface;
2015-05-16 02:41:14 -05:00
use FireflyIII\Http\Controllers\Controller;
2015-06-27 04:44:18 -05:00
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
2015-05-16 02:09:52 -05:00
use Response;
/**
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
{
2015-06-27 04:44:18 -05:00
/** @var \FireflyIII\Generator\Chart\Report\ReportChartGenerator */
protected $generator;
/**
2015-06-28 03:03:34 -05:00
* @codeCoverageIgnore
2015-06-27 04:44:18 -05:00
*/
public function __construct()
{
parent::__construct();
// create chart generator:
2015-07-07 12:09:45 -05:00
$this->generator = app('FireflyIII\Generator\Chart\Report\ReportChartGenerator');
2015-06-27 04:44:18 -05:00
}
2015-05-16 02:09:52 -05:00
/**
* Summarizes all income and expenses, per month, for a given year.
*
* @param ReportQueryInterface $query
* @param $report_type
* @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
*/
public function yearInOut(ReportQueryInterface $query, $report_type, 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');
$cache->addProperty($start);
$cache->addProperty($end);
2015-06-27 04:44:18 -05:00
if ($cache->has()) {
2015-06-27 15:22:27 -05:00
return Response::json($cache->get()); // @codeCoverageIgnore
2015-06-27 04:44:18 -05:00
}
2015-05-16 02:09:52 -05:00
2015-06-27 04:44:18 -05:00
$entries = new Collection;
2015-05-16 02:09:52 -05:00
while ($start < $end) {
$month = clone $start;
$month->endOfMonth();
// total income and total expenses:
$incomeSum = $query->incomeInPeriodCorrectedForList($start, $month, $accounts)->sum('amount_positive');
$expenseSum = $query->expenseInPeriodCorrectedForList($start, $month, $accounts)->sum('amount_positive');
2015-05-16 02:09:52 -05:00
2015-06-27 04:44:18 -05:00
$entries->push([clone $start, $incomeSum, $expenseSum]);
2015-05-16 02:09:52 -05:00
$start->addMonth();
}
2015-06-27 04:44:18 -05:00
$data = $this->generator->yearInOut($entries);
$cache->store($data);
return Response::json($data);
2015-05-16 02:09:52 -05:00
}
/**
* Summarizes all income and expenses for a given year. Gives a total and an average.
*
* @param ReportQueryInterface $query
* @param $report_type
* @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
*/
public function yearInOutSummarized(ReportQueryInterface $query, $report_type, 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');
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($accounts);
2015-06-27 04:44:18 -05:00
if ($cache->has()) {
2015-06-27 15:22:27 -05:00
return Response::json($cache->get()); // @codeCoverageIgnore
2015-06-27 04:44:18 -05:00
}
$income = '0';
$expense = '0';
2015-05-16 02:09:52 -05:00
$count = 0;
2015-06-27 04:44:18 -05:00
bcscale(2);
2015-05-16 02:09:52 -05:00
while ($start < $end) {
$month = clone $start;
$month->endOfMonth();
// total income and total expenses:
$currentIncome = $query->incomeInPeriodCorrectedForList($start, $month, $accounts)->sum('amount_positive');
$currentExpense = $query->expenseInPeriodCorrectedForList($start, $month, $accounts)->sum('amount_positive');
$income = bcadd($income, $currentIncome);
$expense = bcadd($expense, $currentExpense);
2015-05-16 02:09:52 -05:00
$count++;
$start->addMonth();
}
2015-06-27 04:44:18 -05:00
$data = $this->generator->yearInOutSummarized($income, $expense, $count);
$cache->store($data);
2015-05-16 02:09:52 -05:00
2015-06-27 04:44:18 -05:00
return Response::json($data);
2015-05-16 02:09:52 -05:00
}
2015-05-20 12:56:14 -05:00
}