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

325 lines
11 KiB
PHP
Raw Normal View History

2015-05-16 02:41:14 -05:00
<?php
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition;
2015-12-15 05:46:40 -06:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2015-05-16 02:41:14 -05:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2015-06-03 11:22:47 -05:00
use FireflyIII\Support\CacheProperties;
2015-05-16 02:41:14 -05:00
use Illuminate\Support\Collection;
use Navigation;
use Preferences;
2015-05-16 02:41:14 -05:00
use Response;
use Session;
/**
* Class BudgetController
*
* @package FireflyIII\Http\Controllers\Chart
*/
class BudgetController extends Controller
{
2015-06-27 04:44:18 -05:00
/** @var \FireflyIII\Generator\Chart\Budget\BudgetChartGenerator */
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\Budget\BudgetChartGenerator');
2015-06-27 04:44:18 -05:00
}
/**
* @param BudgetRepositoryInterface $repository
* @param $report_type
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Collection $budgets
*/
public function multiYear(BudgetRepositoryInterface $repository, $report_type, Carbon $start, Carbon $end, Collection $accounts, Collection $budgets)
{
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($report_type);
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($accounts);
$cache->addProperty($budgets);
$cache->addProperty('multiYearBudget');
if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore
}
2015-12-16 03:54:56 -06:00
/**
* budget
* year:
* spent: x
* budgeted: x
* year
* spent: x
* budgeted: x
*/
$entries = new Collection;
// go by budget, not by year.
foreach ($budgets as $budget) {
$entry = ['name' => '', 'spent' => [], 'budgeted' => []];
$currentStart = clone $start;
while ($currentStart < $end) {
// fix the date:
$currentEnd = clone $currentStart;
$currentEnd->endOfYear();
// get data:
if (is_null($budget->id)) {
2015-12-16 03:54:56 -06:00
$name = trans('firefly.noBudget');
$sum = $repository->getWithoutBudgetSum($currentStart, $currentEnd);
$budgeted = 0;
} else {
2015-12-16 03:54:56 -06:00
$name = $budget->name;
$sum = $repository->balanceInPeriod($budget, $currentStart, $currentEnd, $accounts);
2015-12-16 03:54:56 -06:00
$budgeted = $repository->getBudgetLimitRepetitions($budget, $currentStart, $currentEnd)->sum('amount');
}
2015-12-16 03:54:56 -06:00
// save to array:
$year = $currentStart->year;
$entry['name'] = $name;
$entry['spent'][$year] = ($sum * -1);
$entry['budgeted'][$year] = $budgeted;
2015-12-16 03:54:56 -06:00
// jump to next year.
$currentStart = clone $currentEnd;
$currentStart->addDay();
}
$entries->push($entry);
}
2015-12-16 03:54:56 -06:00
// generate chart with data:
$data = $this->generator->multiYear($entries);
return Response::json($data);
}
/**
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
2015-05-26 01:17:58 -05:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-12-15 05:46:40 -06:00
public function budget(BudgetRepositoryInterface $repository, AccountRepositoryInterface $accountRepository, Budget $budget)
{
2015-06-27 04:44:18 -05:00
// dates and times
$first = $repository->getFirstBudgetLimitDate($budget);
2015-05-24 08:03:45 -05:00
$range = Preferences::get('viewRange', '1M')->data;
$last = Session::get('end', new Carbon);
$final = clone $last;
$final->addYears(2);
2015-12-15 05:46:40 -06:00
$last = Navigation::endOfX($last, $range, $final);
$accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']);
// chart properties for cache:
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties();
$cache->addProperty($first);
$cache->addProperty($last);
$cache->addProperty('budget');
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
$entries = new Collection;
while ($first < $last) {
$end = Navigation::addPeriod($first, $range, 0);
2015-06-05 09:49:16 -05:00
$end->subDay();
$chartDate = clone $end;
$chartDate->startOfMonth();
$spent = $repository->balanceInPeriod($budget, $first, $end, $accounts) * -1;
2015-06-27 04:44:18 -05:00
$entries->push([$chartDate, $spent]);
$first = Navigation::addPeriod($first, $range, 0);
}
2015-06-27 04:44:18 -05:00
$data = $this->generator->budget($entries);
2015-06-03 14:25:11 -05:00
$cache->store($data);
return Response::json($data);
}
2015-05-16 06:06:38 -05: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 04:44:18 -05:00
public function budgetLimit(BudgetRepositoryInterface $repository, Budget $budget, LimitRepetition $repetition)
2015-05-16 06:06:38 -05:00
{
$start = clone $repetition->startdate;
$end = $repetition->enddate;
2015-07-06 11:04:13 -05:00
bcscale(2);
2015-05-16 06:06:38 -05:00
// chart properties for cache:
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('budget');
$cache->addProperty('limit');
$cache->addProperty($budget->id);
$cache->addProperty($repetition->id);
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
$entries = new Collection;
$amount = $repetition->amount;
2015-05-16 06:06:38 -05:00
while ($start <= $end) {
/*
* Sum of expenses on this day:
*/
2015-12-13 03:05:13 -06:00
$sum = $repository->expensesOnDay($budget, $start);
2015-07-06 11:04:13 -05:00
$amount = bcadd($amount, $sum);
2015-06-27 04:44:18 -05:00
$entries->push([clone $start, $amount]);
2015-05-16 06:06:38 -05:00
$start->addDay();
}
2015-06-27 04:44:18 -05:00
$data = $this->generator->budgetLimit($entries);
2015-06-03 14:25:11 -05:00
$cache->store($data);
return Response::json($data);
2015-05-16 06:06:38 -05:00
}
2015-05-16 02:41:14 -05:00
/**
* Shows a budget list with spent/left/overspent.
*
* @param BudgetRepositoryInterface $repository
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-12-15 05:46:40 -06:00
public function frontpage(BudgetRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
2015-05-16 02:41:14 -05:00
{
$budgets = $repository->getBudgets();
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
$allEntries = new Collection;
2015-12-15 05:46:40 -06:00
$accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']);
2015-05-16 02:41:14 -05:00
// chart properties for cache:
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('budget');
$cache->addProperty('all');
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
bcscale(2);
2015-06-02 10:14:03 -05:00
/** @var Budget $budget */
2015-05-16 02:41:14 -05:00
foreach ($budgets as $budget) {
$repetitions = $repository->getBudgetLimitRepetitions($budget, $start, $end);
if ($repetitions->count() == 0) {
$expenses = $repository->balanceInPeriod($budget, $start, $end, $accounts) * -1;
2015-06-27 10:05:39 -05:00
$allEntries->push([$budget->name, 0, 0, $expenses, 0, 0]);
2015-05-16 02:41:14 -05:00
continue;
}
/** @var LimitRepetition $repetition */
foreach ($repetitions as $repetition) {
$expenses = $repository->balanceInPeriod($budget, $repetition->startdate, $repetition->enddate, $accounts) * -1;
2015-06-27 04:44:18 -05:00
// $left can be less than zero.
// $overspent can be more than zero ( = overspending)
2015-05-16 02:41:14 -05:00
2015-06-27 04:44:18 -05:00
$left = max(bcsub($repetition->amount, $expenses), 0); // limited at zero.
$overspent = max(bcsub($expenses, $repetition->amount), 0); // limited at zero.
2015-06-28 01:33:23 -05:00
$name = $budget->name;
2015-05-16 02:41:14 -05:00
2015-06-27 04:44:18 -05:00
// $spent is maxed to the repetition amount:
$spent = $expenses > $repetition->amount ? $repetition->amount : $expenses;
2015-06-27 10:05:39 -05:00
$allEntries->push([$name, $left, $spent, $overspent, $repetition->amount, $expenses]);
2015-05-16 02:41:14 -05:00
}
}
2015-06-27 04:44:18 -05:00
$noBudgetExpenses = $repository->getWithoutBudgetSum($start, $end) * -1;
2015-06-27 10:05:39 -05:00
$allEntries->push([trans('firefly.noBudget'), 0, 0, $noBudgetExpenses, 0, 0]);
2015-05-16 02:41:14 -05:00
2015-06-27 04:44:18 -05:00
$data = $this->generator->frontpage($allEntries);
2015-06-03 14:25:11 -05:00
$cache->store($data);
return Response::json($data);
2015-05-16 02:41:14 -05:00
}
/**
* Show a yearly overview for a budget.
*
* @param BudgetRepositoryInterface $repository
* @param $year
* @param bool $shared
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-12-12 13:56:07 -06:00
public function year(BudgetRepositoryInterface $repository, $report_type, Carbon $start, Carbon $end, Collection $accounts)
2015-05-16 02:41:14 -05:00
{
2015-07-31 11:18:54 -05:00
$allBudgets = $repository->getBudgets();
$budgets = new Collection;
2015-05-16 02:41:14 -05:00
// chart properties for cache:
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
2015-12-24 03:14:01 -06:00
$cache->addProperty($report_type);
$cache->addProperty($accounts);
2015-06-03 14:25:11 -05:00
$cache->addProperty('budget');
$cache->addProperty('year');
if ($cache->has()) {
2015-06-27 15:22:27 -05:00
return Response::json($cache->get()); // @codeCoverageIgnore
}
2015-12-24 02:50:28 -06:00
// filter empty budgets:
foreach ($allBudgets as $budget) {
$spent = $repository->balanceInPeriod($budget, $start, $end, $accounts);
2015-12-24 02:50:28 -06:00
if ($spent != 0) {
$budgets->push($budget);
}
}
2015-07-31 11:18:54 -05:00
2015-06-27 04:44:18 -05:00
$entries = new Collection;
2015-05-16 02:41:14 -05:00
while ($start < $end) {
// month is the current end of the period:
$month = clone $start;
$month->endOfMonth();
$row = [clone $start];
// each budget, fill the row:
2015-12-24 02:50:28 -06:00
foreach ($budgets as $budget) {
$spent = $repository->balanceInPeriod($budget, $start, $month, $accounts);
2015-07-31 11:18:54 -05:00
$row[] = $spent * -1;
2015-05-16 02:41:14 -05:00
}
2015-06-27 04:44:18 -05:00
$entries->push($row);
2015-06-05 09:49:16 -05:00
$start->endOfMonth()->addDay();
2015-05-16 02:41:14 -05:00
}
2015-12-24 02:50:28 -06:00
$data = $this->generator->year($budgets, $entries);
2015-06-03 14:25:11 -05:00
$cache->store($data);
return Response::json($data);
2015-05-16 02:41:14 -05:00
}
2015-05-20 12:56:14 -05:00
}