2015-05-16 09:41:14 +02:00
|
|
|
<?php
|
2016-02-05 12:08:25 +01:00
|
|
|
declare(strict_types = 1);
|
2015-05-16 09:41:14 +02:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Chart;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2016-05-02 20:49:19 +02:00
|
|
|
use FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface;
|
2015-05-16 09:41:14 +02:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
|
|
|
use FireflyIII\Models\Budget;
|
|
|
|
use FireflyIII\Models\LimitRepetition;
|
2016-05-06 10:32:26 +02:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2015-12-30 08:00:52 +01:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
|
2015-05-16 09:41:14 +02:00
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
2016-05-06 10:32:26 +02:00
|
|
|
use FireflyIII\Support\CacheProperties;
|
2015-05-16 09:41:14 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2016-05-06 10:32:26 +02:00
|
|
|
use Log;
|
|
|
|
use Navigation;
|
|
|
|
use Preferences;
|
|
|
|
use Response;
|
2015-05-16 09:41:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BudgetController
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Controllers\Chart
|
|
|
|
*/
|
|
|
|
class BudgetController extends Controller
|
|
|
|
{
|
2015-06-27 11:44:18 +02:00
|
|
|
|
2016-01-28 21:50:20 +01:00
|
|
|
/** @var \FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface */
|
2015-06-27 11:44:18 +02:00
|
|
|
protected $generator;
|
|
|
|
|
|
|
|
/**
|
2016-02-04 07:27:03 +01:00
|
|
|
*
|
2015-06-27 11:44:18 +02:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
// create chart generator:
|
2016-05-02 20:49:19 +02:00
|
|
|
$this->generator = app(BudgetChartGeneratorInterface::class);
|
2015-06-27 11:44:18 +02:00
|
|
|
}
|
|
|
|
|
2016-04-25 09:57:39 +02:00
|
|
|
/**
|
2016-05-05 21:25:20 +02:00
|
|
|
* checked
|
|
|
|
*
|
2016-04-25 09:57:39 +02:00
|
|
|
* @param BudgetRepositoryInterface $repository
|
|
|
|
* @param Budget $budget
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
|
|
|
public function budget(BudgetRepositoryInterface $repository, Budget $budget)
|
|
|
|
{
|
2016-05-06 10:32:26 +02:00
|
|
|
$first = $repository->firstUseDate($budget);
|
|
|
|
$range = Preferences::get('viewRange', '1M')->data;
|
|
|
|
$last = session('end', new Carbon);
|
|
|
|
|
|
|
|
$cache = new CacheProperties();
|
|
|
|
$cache->addProperty($first);
|
|
|
|
$cache->addProperty($last);
|
|
|
|
$cache->addProperty('budget');
|
|
|
|
|
|
|
|
if ($cache->has()) {
|
|
|
|
//return Response::json($cache->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
$final = clone $last;
|
|
|
|
$final->addYears(2);
|
|
|
|
|
|
|
|
$budgetCollection = new Collection([$budget]);
|
|
|
|
$last = Navigation::endOfX($last, $range, $final); // not to overshoot.
|
|
|
|
$entries = new Collection;
|
|
|
|
Log::debug('---- now at chart');
|
|
|
|
while ($first < $last) {
|
|
|
|
|
|
|
|
// periodspecific dates:
|
|
|
|
$currentStart = Navigation::startOfPeriod($first, $range);
|
|
|
|
$currentEnd = Navigation::endOfPeriod($first, $range);
|
|
|
|
// sub another day because reasons.
|
|
|
|
$currentEnd->subDay();
|
|
|
|
$spent = $repository->spentInPeriod($budgetCollection, new Collection, $currentStart, $currentEnd);
|
|
|
|
$entry = [$first, ($spent * -1)];
|
|
|
|
|
|
|
|
$entries->push($entry);
|
|
|
|
$first = Navigation::addPeriod($first, $range, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $this->generator->budgetLimit($entries, 'month');
|
|
|
|
$cache->store($data);
|
|
|
|
|
|
|
|
return Response::json($data);
|
|
|
|
|
|
|
|
|
2016-05-05 22:03:35 +02:00
|
|
|
/**
|
|
|
|
* $final = clone $last;
|
|
|
|
* $final->addYears(2);
|
|
|
|
* $last = Navigation::endOfX($last, $range, $final);
|
|
|
|
* $entries = new Collection;
|
|
|
|
* // get all expenses:
|
|
|
|
* $spentArray = $repository->spentPerDay($budget, $first, $last, new Collection);
|
|
|
|
*
|
|
|
|
* while ($first < $last) {
|
|
|
|
*
|
|
|
|
* // periodspecific dates:
|
|
|
|
* $currentStart = Navigation::startOfPeriod($first, $range);
|
|
|
|
* $currentEnd = Navigation::endOfPeriod($first, $range);
|
|
|
|
* $spent = $this->getSumOfRange($currentStart, $currentEnd, $spentArray);
|
|
|
|
* $entry = [$first, ($spent * -1)];
|
|
|
|
*
|
|
|
|
* $entries->push($entry);
|
|
|
|
* $first = Navigation::addPeriod($first, $range, 0);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* $data = $this->generator->budgetLimit($entries, 'month');
|
|
|
|
* $cache->store($data);
|
|
|
|
*
|
|
|
|
* return Response::json($data);
|
|
|
|
* **/
|
2016-04-25 09:57:39 +02:00
|
|
|
}
|
|
|
|
|
2015-05-16 13:06:38 +02:00
|
|
|
/**
|
|
|
|
* Shows the amount left in a specific budget limit.
|
|
|
|
*
|
|
|
|
* @param BudgetRepositoryInterface $repository
|
|
|
|
* @param Budget $budget
|
|
|
|
* @param LimitRepetition $repetition
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2015-06-27 11:44:18 +02:00
|
|
|
public function budgetLimit(BudgetRepositoryInterface $repository, Budget $budget, LimitRepetition $repetition)
|
2015-05-16 13:06:38 +02:00
|
|
|
{
|
2016-05-06 10:32:26 +02:00
|
|
|
$start = clone $repetition->startdate;
|
|
|
|
$end = $repetition->enddate;
|
|
|
|
$cache = new CacheProperties();
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty('budget-limit');
|
|
|
|
$cache->addProperty($budget->id);
|
|
|
|
$cache->addProperty($repetition->id);
|
|
|
|
|
|
|
|
if ($cache->has()) {
|
|
|
|
// return Response::json($cache->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
$entries = new Collection;
|
|
|
|
$amount = $repetition->amount;
|
|
|
|
$budgetCollection = new Collection([$budget]);
|
|
|
|
Log::debug('amount starts ' . $amount);
|
|
|
|
while ($start <= $end) {
|
|
|
|
$spent = $repository->spentInPeriod($budgetCollection, new Collection, $start, $start);
|
|
|
|
$amount = bcadd($amount, $spent);
|
|
|
|
$entries->push([clone $start, round($amount, 2)]);
|
|
|
|
|
|
|
|
$start->addDay();
|
|
|
|
}
|
|
|
|
$data = $this->generator->budgetLimit($entries, 'monthAndDay');
|
|
|
|
$cache->store($data);
|
2015-05-16 13:06:38 +02:00
|
|
|
|
2016-05-06 10:32:26 +02:00
|
|
|
return Response::json($data);
|
2015-05-16 13:06:38 +02:00
|
|
|
}
|
|
|
|
|
2015-05-16 09:41:14 +02:00
|
|
|
/**
|
|
|
|
* Shows a budget list with spent/left/overspent.
|
|
|
|
*
|
2016-01-01 12:41:00 +01:00
|
|
|
* @param BudgetRepositoryInterface $repository
|
2015-12-28 07:55:09 +01:00
|
|
|
*
|
2016-01-01 12:41:00 +01:00
|
|
|
* @param ARI $accountRepository
|
2015-05-16 09:41:14 +02:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2015-12-30 08:00:52 +01:00
|
|
|
public function frontpage(BudgetRepositoryInterface $repository, ARI $accountRepository)
|
2015-05-16 09:41:14 +02:00
|
|
|
{
|
2016-05-06 10:32:26 +02:00
|
|
|
$start = session('start', Carbon::now()->startOfMonth());
|
|
|
|
$end = session('end', Carbon::now()->endOfMonth());
|
|
|
|
// chart properties for cache:
|
|
|
|
$cache = new CacheProperties();
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty('budget');
|
|
|
|
$cache->addProperty('all');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return Response::json($cache->get());
|
|
|
|
}
|
|
|
|
$budgets = $repository->getActiveBudgets();
|
|
|
|
$repetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
|
|
|
|
$allEntries = new Collection;
|
|
|
|
$format = strval(trans('config.month_and_day'));
|
|
|
|
|
|
|
|
/** @var Budget $budget */
|
|
|
|
foreach ($budgets as $budget) {
|
|
|
|
// get relevant repetitions:
|
|
|
|
$name = $budget->name;
|
|
|
|
$reps = $repetitions->filter(
|
|
|
|
function (LimitRepetition $repetition) use ($budget, $start, $end) {
|
|
|
|
if ($repetition->startdate < $end && $repetition->enddate > $start && $repetition->budget_id === $budget->id) {
|
|
|
|
return $repetition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if ($reps->count() === 0) {
|
|
|
|
$amount = '0';
|
|
|
|
$left = '0';
|
|
|
|
$spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
|
|
|
|
$overspent = '0';
|
|
|
|
$allEntries->push([$name, $left, $spent, $overspent, $amount, $spent]);
|
|
|
|
}
|
|
|
|
/** @var LimitRepetition $repetition */
|
|
|
|
foreach ($reps as $repetition) {
|
|
|
|
$expenses = $repository->spentInPeriod(new Collection([$budget]), new Collection, $repetition->startdate, $repetition->enddate);
|
|
|
|
if ($reps->count() > 1) {
|
|
|
|
$name = $budget->name . ' ' . trans(
|
|
|
|
'firefly.between_dates',
|
|
|
|
['start' => $repetition->startdate->formatLocalized($format), 'end' => $repetition->enddate->formatLocalized($format)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$amount = $repetition->amount;
|
|
|
|
$left = bccomp(bcadd($amount, $expenses), '0') < 1 ? '0' : bcadd($amount, $expenses);
|
|
|
|
$spent = bccomp(bcadd($amount, $expenses), '0') < 1 ? bcmul($amount, '-1') : $expenses;
|
|
|
|
$overspent = bccomp(bcadd($amount, $expenses), '0') < 1 ? bcadd($amount, $expenses) : '0';
|
|
|
|
$allEntries->push([$name, $left, $spent, $overspent, $amount, $spent]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = $repository->journalsInPeriodWithoutBudget(new Collection, $start, $end);
|
|
|
|
$sum = '0';
|
|
|
|
/** @var TransactionJournal $entry */
|
|
|
|
foreach ($list as $entry) {
|
|
|
|
$sum = bcadd(TransactionJournal::amount($entry), $sum);
|
|
|
|
}
|
|
|
|
$allEntries->push([trans('firefly.no_budget'), '0', '0', $sum, '0', '0']);
|
|
|
|
$data = $this->generator->frontpage($allEntries);
|
|
|
|
$cache->store($data);
|
|
|
|
|
|
|
|
return Response::json($data);
|
|
|
|
|
|
|
|
|
2016-05-05 22:03:35 +02:00
|
|
|
/**
|
|
|
|
*
|
2016-05-06 10:32:26 +02:00
|
|
|
*
|
|
|
|
*
|
2016-05-05 22:03:35 +02:00
|
|
|
*
|
|
|
|
* $budgets = $repository->getBudgetsAndLimitsInRange($start, $end);
|
|
|
|
* $allEntries = new Collection;
|
|
|
|
* $accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']);
|
|
|
|
* $format = strval(trans('config.month_and_day'));
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* // @var Budget $budget
|
|
|
|
* foreach ($budgets as $budget) {
|
|
|
|
* // we already have amount, startdate and enddate.
|
|
|
|
* // if this "is" a limit repetition (as opposed to a budget without one entirely)
|
|
|
|
* // depends on whether startdate and enddate are null.
|
|
|
|
* $name = $budget->name;
|
|
|
|
* if (is_null($budget->startdate) && is_null($budget->enddate)) {
|
|
|
|
* $currentStart = clone $start;
|
|
|
|
* $currentEnd = clone $end;
|
|
|
|
* $expenses = $repository->balanceInPeriod($budget, $currentStart, $currentEnd, $accounts);
|
|
|
|
* $amount = '0';
|
|
|
|
* $left = '0';
|
|
|
|
* $spent = $expenses;
|
|
|
|
* $overspent = '0';
|
|
|
|
* } else {
|
|
|
|
*
|
|
|
|
* // update the display name if the range
|
|
|
|
* // of the limit repetition does not match
|
|
|
|
* // the session's range (for clarity).
|
|
|
|
* if (
|
|
|
|
* ($start->format('Y-m-d') != $budget->startdate->format('Y-m-d'))
|
|
|
|
* || ($end->format('Y-m-d') != $budget->enddate->format('Y-m-d'))
|
|
|
|
* ) {
|
|
|
|
* $name .= ' ' . trans(
|
|
|
|
* 'firefly.between_dates',
|
|
|
|
* [
|
|
|
|
* 'start' => $budget->startdate->formatLocalized($format),
|
|
|
|
* 'end' => $budget->startdate->formatLocalized($format),
|
|
|
|
* ]
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
* $currentStart = clone $budget->startdate;
|
|
|
|
* $currentEnd = clone $budget->enddate;
|
|
|
|
* $expenses = $repository->balanceInPeriod($budget, $currentStart, $currentEnd, $accounts);
|
|
|
|
* $amount = $budget->amount;
|
|
|
|
* // smaller than 1 means spent MORE than budget allows.
|
|
|
|
* $left = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? '0' : bcadd($budget->amount, $expenses);
|
|
|
|
* $spent = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? bcmul($amount, '-1') : $expenses;
|
|
|
|
* $overspent = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? bcadd($budget->amount, $expenses) : '0';
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* $allEntries->push([$name, $left, $spent, $overspent, $amount, $expenses]);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* $noBudgetExpenses = $repository->getWithoutBudgetSum($accounts, $start, $end);
|
|
|
|
* $allEntries->push([trans('firefly.no_budget'), '0', '0', $noBudgetExpenses, '0', '0']);
|
|
|
|
* $data = $this->generator->frontpage($allEntries);
|
|
|
|
* $cache->store($data);
|
|
|
|
*
|
|
|
|
* return Response::json($data);
|
|
|
|
**/
|
2015-05-16 09:41:14 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 09:25:15 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param BudgetRepositoryInterface $repository
|
|
|
|
* @param $reportType
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Collection $budgets
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function multiYear(BudgetRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $budgets)
|
|
|
|
{
|
2016-05-05 22:03:35 +02:00
|
|
|
/**
|
|
|
|
* // chart properties for cache:
|
|
|
|
* $cache = new CacheProperties();
|
|
|
|
* $cache->addProperty($reportType);
|
|
|
|
* $cache->addProperty($start);
|
|
|
|
* $cache->addProperty($end);
|
|
|
|
* $cache->addProperty($accounts);
|
|
|
|
* $cache->addProperty($budgets);
|
|
|
|
* $cache->addProperty('multiYearBudget');
|
|
|
|
*
|
|
|
|
* if ($cache->has()) {
|
|
|
|
* return Response::json($cache->get());
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // Get the budgeted amounts for each budgets in each year.
|
|
|
|
* $budgetedSet = $repository->getBudgetedPerYear($budgets, $start, $end);
|
|
|
|
* $budgetedArray = [];
|
|
|
|
* // @var Budget $entry
|
|
|
|
* foreach ($budgetedSet as $entry) {
|
|
|
|
* $budgetedArray[$entry->id][$entry->dateFormatted] = $entry->budgeted;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* $set = $repository->getBudgetsAndExpensesPerYear($budgets, $accounts, $start, $end);
|
|
|
|
* $entries = new Collection;
|
|
|
|
* // go by budget, not by year.
|
|
|
|
* // @var Budget $budget
|
|
|
|
* foreach ($budgets as $budget) {
|
|
|
|
* $entry = ['name' => '', 'spent' => [], 'budgeted' => []];
|
|
|
|
* $id = $budget->id;
|
|
|
|
* $currentStart = clone $start;
|
|
|
|
* while ($currentStart < $end) {
|
|
|
|
* // fix the date:
|
|
|
|
* $currentEnd = clone $currentStart;
|
|
|
|
* $currentEnd->endOfYear();
|
|
|
|
*
|
|
|
|
* // basic information:
|
|
|
|
* $year = $currentStart->year;
|
|
|
|
* $entry['name'] = $budget->name ?? (string)trans('firefly.no_budget');
|
|
|
|
* $spent = 0;
|
|
|
|
* // this might be a good moment to collect no budget stuff.
|
|
|
|
* if (is_null($budget->id)) {
|
|
|
|
* // get without budget sum in range:
|
|
|
|
* $spent = $repository->getWithoutBudgetSum($accounts, $currentStart, $currentEnd) * -1;
|
|
|
|
* } else {
|
|
|
|
* if (isset($set[$id]['entries'][$year])) {
|
|
|
|
* $spent = $set[$id]['entries'][$year] * -1;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* $budgeted = $budgetedArray[$id][$year] ?? '0';
|
|
|
|
* $entry['spent'][$year] = $spent;
|
|
|
|
* $entry['budgeted'][$year] = round($budgeted, 2);
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* // jump to next year.
|
|
|
|
* $currentStart = clone $currentEnd;
|
|
|
|
* $currentStart->addDay();
|
|
|
|
* }
|
|
|
|
* $entries->push($entry);
|
|
|
|
* }
|
|
|
|
* // generate chart with data:
|
|
|
|
* $data = $this->generator->multiYear($entries);
|
|
|
|
* $cache->store($data);
|
|
|
|
*
|
|
|
|
* return Response::json($data);
|
|
|
|
**/
|
2016-02-05 09:25:15 +01:00
|
|
|
}
|
|
|
|
|
2016-04-24 20:23:17 +02:00
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
|
|
|
* @param string $reportType
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
2016-04-26 09:21:57 +02:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2016-04-24 20:23:17 +02:00
|
|
|
*/
|
|
|
|
public function period(Budget $budget, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
|
|
|
|
{
|
2016-05-05 22:03:35 +02:00
|
|
|
/**
|
|
|
|
* // chart properties for cache:
|
|
|
|
* $cache = new CacheProperties();
|
|
|
|
* $cache->addProperty($start);
|
|
|
|
* $cache->addProperty($end);
|
|
|
|
* $cache->addProperty($reportType);
|
|
|
|
* $cache->addProperty($accounts);
|
|
|
|
* $cache->addProperty($budget->id);
|
|
|
|
* $cache->addProperty('budget');
|
|
|
|
* $cache->addProperty('period');
|
|
|
|
* if ($cache->has()) {
|
|
|
|
* return Response::json($cache->get());
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // @var BudgetRepositoryInterface $repository
|
|
|
|
* $repository = app(BudgetRepositoryInterface::class);
|
|
|
|
* // loop over period, add by users range:
|
|
|
|
* $current = clone $start;
|
|
|
|
* $viewRange = Preferences::get('viewRange', '1M')->data;
|
|
|
|
* $set = new Collection;
|
|
|
|
* while ($current < $end) {
|
|
|
|
* $currentStart = clone $current;
|
|
|
|
* $currentEnd = Navigation::endOfPeriod($currentStart, $viewRange);
|
|
|
|
*
|
|
|
|
* // get all budget limits and their repetitions.
|
|
|
|
* $reps = $repository->getAllBudgetLimitRepetitions($currentStart, $currentEnd, $budget);
|
|
|
|
* $budgeted = $reps->sum('amount');
|
|
|
|
* $perBudget = $repository->spentPerBudgetPerAccount(new Collection([$budget]), $accounts, $currentStart, $currentEnd);
|
|
|
|
* // includes null, so filter!
|
|
|
|
* $perBudget = $perBudget->filter(
|
|
|
|
* function (TransactionJournal $journal) use ($budget) {
|
|
|
|
* if (intval($journal->budget_id) === $budget->id) {
|
|
|
|
* return $journal;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* );
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* $spent = $perBudget->sum('spent');
|
|
|
|
*
|
|
|
|
* $entry = [
|
|
|
|
* 'date' => clone $currentStart,
|
|
|
|
* 'budgeted' => $budgeted,
|
|
|
|
* 'spent' => $spent,
|
|
|
|
* ];
|
|
|
|
* $set->push($entry);
|
|
|
|
* $currentEnd->addDay();
|
|
|
|
* $current = clone $currentEnd;
|
|
|
|
* }
|
|
|
|
* $data = $this->generator->period($set, $viewRange);
|
|
|
|
* $cache->store($data);
|
|
|
|
*
|
|
|
|
* return Response::json($data);
|
|
|
|
*/
|
2016-04-24 20:23:17 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-16 09:41:14 +02:00
|
|
|
/**
|
2015-12-28 19:56:28 +01:00
|
|
|
*
|
2015-05-16 09:41:14 +02:00
|
|
|
* @param BudgetRepositoryInterface $repository
|
2015-12-28 20:04:54 +01:00
|
|
|
* @param $reportType
|
2015-12-28 07:43:05 +01:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
2015-05-16 09:41:14 +02:00
|
|
|
*
|
2015-12-28 07:43:05 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2015-05-16 09:41:14 +02:00
|
|
|
*/
|
2016-05-06 10:32:26 +02:00
|
|
|
public
|
|
|
|
function year(
|
|
|
|
BudgetRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts
|
|
|
|
) {
|
2016-05-05 22:03:35 +02:00
|
|
|
/**
|
|
|
|
* // chart properties for cache:
|
|
|
|
* $cache = new CacheProperties();
|
|
|
|
* $cache->addProperty($start);
|
|
|
|
* $cache->addProperty($end);
|
|
|
|
* $cache->addProperty($reportType);
|
|
|
|
* $cache->addProperty($accounts);
|
|
|
|
* $cache->addProperty('budget');
|
|
|
|
* $cache->addProperty('year');
|
|
|
|
* if ($cache->has()) {
|
|
|
|
* return Response::json($cache->get());
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* $budgetInformation = $repository->getBudgetsAndExpensesPerMonth($accounts, $start, $end);
|
|
|
|
* $budgets = new Collection;
|
|
|
|
* $entries = new Collection;
|
|
|
|
*
|
|
|
|
* // @var array $row
|
|
|
|
* foreach ($budgetInformation as $row) {
|
|
|
|
* $budgets->push($row['budget']);
|
|
|
|
* }
|
|
|
|
* while ($start < $end) {
|
|
|
|
* // month is the current end of the period:
|
|
|
|
* $month = clone $start;
|
|
|
|
* $month->endOfMonth();
|
|
|
|
* $row = [clone $start];
|
|
|
|
* $dateFormatted = $start->format('Y-m');
|
|
|
|
*
|
|
|
|
* // each budget, check if there is an entry for this month:
|
|
|
|
* // @var array $row
|
|
|
|
* foreach ($budgetInformation as $budgetRow) {
|
|
|
|
* $spent = 0; // nothing spent.
|
|
|
|
* if (isset($budgetRow['entries'][$dateFormatted])) {
|
|
|
|
* $spent = $budgetRow['entries'][$dateFormatted] * -1; // to fit array
|
|
|
|
* }
|
|
|
|
* $row[] = $spent;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // add "no budget" thing.
|
|
|
|
* $row[] = round(bcmul($repository->getWithoutBudgetSum($accounts, $start, $month), '-1'), 4);
|
|
|
|
*
|
|
|
|
* $entries->push($row);
|
|
|
|
* $start->endOfMonth()->addDay();
|
|
|
|
* }
|
|
|
|
* $data = $this->generator->year($budgets, $entries);
|
|
|
|
* $cache->store($data);
|
|
|
|
*
|
|
|
|
* return Response::json($data);
|
|
|
|
*/
|
2015-05-16 09:41:14 +02:00
|
|
|
}
|
2015-05-20 19:56:14 +02:00
|
|
|
}
|