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

240 lines
7.7 KiB
PHP
Raw Normal View History

2015-05-16 02:41:14 -05:00
<?php
namespace FireflyIII\Http\Controllers\Chart;
2015-06-27 04:44:18 -05:00
use App;
2015-05-16 02:41:14 -05:00
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition;
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:
$this->generator = App::make('FireflyIII\Generator\Chart\Budget\BudgetChartGenerator');
}
/**
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
2015-05-26 01:17:58 -05:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-06-27 04:44:18 -05:00
public function budget(BudgetRepositoryInterface $repository, 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);
$last = Navigation::endOfX($last, $range, $final);
// 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->spentInPeriodCorrected($budget, $first, $end);
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;
// 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:
*/
$sum = $repository->expensesOnDayCorrected($budget, $start);
2015-05-16 06:06:38 -05:00
$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-06-27 04:44:18 -05:00
public function frontpage(BudgetRepositoryInterface $repository)
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;
// 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->spentInPeriodCorrected($budget, $start, $end, true);
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) {
2015-06-27 04:44:18 -05:00
$expenses = $repository->spentInPeriodCorrected($budget, $repetition->startdate, $repetition->enddate, true);
// $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-06-27 04:44:18 -05:00
public function year(BudgetRepositoryInterface $repository, $year, $shared = false)
2015-05-16 02:41:14 -05:00
{
$start = new Carbon($year . '-01-01');
$end = new Carbon($year . '-12-31');
$shared = $shared == 'shared' ? true : false;
$budgets = $repository->getBudgets();
// 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('year');
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;
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:
foreach ($budgets as $budget) {
$spent = $repository->spentInPeriodCorrected($budget, $start, $month, $shared);
2015-05-16 02:41:14 -05:00
$row[] = $spent;
}
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-06-27 04:44:18 -05: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
}