2015-05-16 02:41:14 -05:00
|
|
|
<?php
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2015-05-16 02:41:14 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Chart;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
|
|
|
use FireflyIII\Models\Budget;
|
|
|
|
use FireflyIII\Models\LimitRepetition;
|
2015-12-30 01:00:52 -06:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
|
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;
|
2015-05-19 23:49:22 -05:00
|
|
|
use Navigation;
|
|
|
|
use Preferences;
|
2015-05-16 02:41:14 -05:00
|
|
|
use Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BudgetController
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Controllers\Chart
|
|
|
|
*/
|
|
|
|
class BudgetController extends Controller
|
|
|
|
{
|
2015-06-27 04:44:18 -05:00
|
|
|
|
2016-01-28 14:50:20 -06:00
|
|
|
/** @var \FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface */
|
2015-06-27 04:44:18 -05:00
|
|
|
protected $generator;
|
|
|
|
|
|
|
|
/**
|
2016-02-04 00:27:03 -06:00
|
|
|
*
|
2015-06-27 04:44:18 -05:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
// create chart generator:
|
2016-01-28 14:50:20 -06:00
|
|
|
$this->generator = app('FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface');
|
2015-06-27 04:44:18 -05:00
|
|
|
}
|
|
|
|
|
2015-12-15 05:37:55 -06:00
|
|
|
/**
|
2015-12-30 01:00:52 -06:00
|
|
|
* @param BudgetRepositoryInterface $repository
|
|
|
|
* @param Budget $budget
|
2015-05-26 01:17:58 -05:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
2015-05-19 23:49:22 -05:00
|
|
|
*/
|
2015-12-30 01:00:52 -06:00
|
|
|
public function budget(BudgetRepositoryInterface $repository, Budget $budget)
|
2015-05-19 23:49:22 -05:00
|
|
|
{
|
|
|
|
|
2015-06-27 04:44:18 -05:00
|
|
|
// dates and times
|
2015-05-19 23:49:22 -05:00
|
|
|
$first = $repository->getFirstBudgetLimitDate($budget);
|
2015-05-24 08:03:45 -05:00
|
|
|
$range = Preferences::get('viewRange', '1M')->data;
|
2016-02-04 00:27:03 -06:00
|
|
|
$last = session('end', new Carbon);
|
2015-05-19 23:49:22 -05:00
|
|
|
|
2015-06-02 10:44:50 -05:00
|
|
|
// 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-12-29 01:45:43 -06:00
|
|
|
|
2015-06-27 15:22:27 -05:00
|
|
|
return Response::json($cache->get()); // @codeCoverageIgnore
|
2015-06-02 10:44:50 -05:00
|
|
|
}
|
|
|
|
|
2015-12-29 01:45:43 -06:00
|
|
|
$final = clone $last;
|
|
|
|
$final->addYears(2);
|
|
|
|
$last = Navigation::endOfX($last, $range, $final);
|
2015-06-27 04:44:18 -05:00
|
|
|
$entries = new Collection;
|
2015-12-29 01:45:43 -06:00
|
|
|
// get all expenses:
|
2016-02-10 00:34:04 -06:00
|
|
|
$spentArray = $repository->spentPerDay($budget, $first, $last);
|
2015-06-02 10:44:50 -05:00
|
|
|
|
2015-05-19 23:49:22 -05:00
|
|
|
while ($first < $last) {
|
2015-12-29 01:45:43 -06:00
|
|
|
|
2016-02-10 00:34:04 -06:00
|
|
|
// periodspecific dates:
|
|
|
|
$currentStart = Navigation::startOfPeriod($first, $range);
|
|
|
|
$currentEnd = Navigation::endOfPeriod($first, $range);
|
|
|
|
$spent = $this->getSumOfRange($currentStart, $currentEnd, $spentArray);
|
|
|
|
$entry = [$first, ($spent * -1)];
|
2015-12-29 01:45:43 -06:00
|
|
|
|
2016-02-10 00:34:04 -06:00
|
|
|
$entries->push($entry);
|
2015-05-19 23:49:22 -05:00
|
|
|
$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);
|
2015-06-02 10:44:50 -05:00
|
|
|
|
|
|
|
return Response::json($data);
|
2015-05-19 23:49:22 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-06-02 10:44:50 -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-02 10:44:50 -05:00
|
|
|
}
|
|
|
|
|
2015-12-29 01:27:13 -06:00
|
|
|
$set = $repository->getExpensesPerDay($budget, $start, $end);
|
2015-06-27 04:44:18 -05:00
|
|
|
$entries = new Collection;
|
|
|
|
$amount = $repetition->amount;
|
2015-05-16 06:06:38 -05:00
|
|
|
|
2015-12-29 01:27:13 -06:00
|
|
|
// get sum (har har)!
|
2015-05-16 06:06:38 -05:00
|
|
|
while ($start <= $end) {
|
2015-12-29 01:27:13 -06:00
|
|
|
$formatted = $start->format('Y-m-d');
|
|
|
|
$filtered = $set->filter(
|
|
|
|
function (Budget $obj) use ($formatted) {
|
|
|
|
return $obj->date == $formatted;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$sum = is_null($filtered->first()) ? '0' : $filtered->first()->dailyAmount;
|
|
|
|
|
2015-05-16 06:06:38 -05:00
|
|
|
/*
|
|
|
|
* Sum of expenses on this day:
|
|
|
|
*/
|
2016-02-05 05:28:05 -06:00
|
|
|
$amount = round(bcadd(strval($amount), $sum), 2);
|
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);
|
2015-06-02 10:44:50 -05:00
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
2016-01-01 05:41:00 -06:00
|
|
|
* @param BudgetRepositoryInterface $repository
|
2015-12-28 00:55:09 -06:00
|
|
|
*
|
2016-01-01 05:41:00 -06:00
|
|
|
* @param ARI $accountRepository
|
2015-05-16 02:41:14 -05:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2015-12-30 01:00:52 -06:00
|
|
|
public function frontpage(BudgetRepositoryInterface $repository, ARI $accountRepository)
|
2015-05-16 02:41:14 -05:00
|
|
|
{
|
2016-02-04 00:27:03 -06:00
|
|
|
$start = session('start', Carbon::now()->startOfMonth());
|
|
|
|
$end = session('end', Carbon::now()->endOfMonth());
|
2015-05-16 02:41:14 -05:00
|
|
|
|
2015-06-02 10:44:50 -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-02 10:44:50 -05:00
|
|
|
}
|
|
|
|
|
2015-12-27 14:17:04 -06:00
|
|
|
$budgets = $repository->getBudgetsAndLimitsInRange($start, $end);
|
|
|
|
$allEntries = new Collection;
|
|
|
|
$accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']);
|
|
|
|
|
|
|
|
|
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) {
|
2015-12-27 14:17:04 -06:00
|
|
|
// 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.
|
2015-12-28 00:12:47 -06:00
|
|
|
$name = $budget->name;
|
2015-12-27 14:17:04 -06:00
|
|
|
if (is_null($budget->startdate) && is_null($budget->enddate)) {
|
|
|
|
$currentStart = clone $start;
|
|
|
|
$currentEnd = clone $end;
|
|
|
|
$expenses = $repository->balanceInPeriod($budget, $currentStart, $currentEnd, $accounts);
|
2016-02-05 06:09:18 -06:00
|
|
|
$amount = '0';
|
|
|
|
$left = '0';
|
2015-12-27 14:17:04 -06:00
|
|
|
$spent = $expenses;
|
2016-02-05 06:09:18 -06:00
|
|
|
$overspent = '0';
|
2015-12-27 14:17:04 -06:00
|
|
|
} else {
|
|
|
|
$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.
|
2016-02-05 06:09:18 -06:00
|
|
|
$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';
|
2015-05-16 02:41:14 -05:00
|
|
|
}
|
|
|
|
|
2015-12-27 14:17:04 -06:00
|
|
|
$allEntries->push([$name, $left, $spent, $overspent, $amount, $expenses]);
|
2015-05-16 02:41:14 -05:00
|
|
|
}
|
|
|
|
|
2015-12-27 14:26:44 -06:00
|
|
|
$noBudgetExpenses = $repository->getWithoutBudgetSum($start, $end);
|
2016-02-05 06:09:18 -06:00
|
|
|
$allEntries->push([trans('firefly.noBudget'), '0', '0', $noBudgetExpenses, '0', '0']);
|
2015-06-27 04:44:18 -05:00
|
|
|
$data = $this->generator->frontpage($allEntries);
|
2015-06-03 14:25:11 -05:00
|
|
|
$cache->store($data);
|
2015-06-02 10:44:50 -05:00
|
|
|
|
|
|
|
return Response::json($data);
|
2015-05-16 02:41:14 -05:00
|
|
|
}
|
|
|
|
|
2016-02-05 02:25:15 -06:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param BudgetRepositoryInterface $repository
|
|
|
|
* @param $reportType
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Collection $budgets
|
|
|
|
*
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveParameterList) // need all parameters
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function multiYear(BudgetRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $budgets)
|
|
|
|
{
|
|
|
|
// 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()); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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();
|
|
|
|
|
2016-02-24 10:37:08 -06:00
|
|
|
// basic information:
|
2016-02-05 02:25:15 -06:00
|
|
|
$year = $currentStart->year;
|
2016-02-24 10:37:08 -06:00
|
|
|
$entry['name'] = $budget->name ?? (string)trans('firefly.noBudget');
|
2016-02-05 02:25:15 -06:00
|
|
|
$spent = 0;
|
2016-02-24 10:37:08 -06:00
|
|
|
// 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($currentStart, $currentEnd) * -1;
|
|
|
|
} else {
|
|
|
|
if (isset($set[$id]['entries'][$year])) {
|
|
|
|
$spent = $set[$id]['entries'][$year] * -1;
|
|
|
|
}
|
2016-02-05 02:25:15 -06:00
|
|
|
}
|
|
|
|
|
2016-02-24 10:37:08 -06:00
|
|
|
$budgeted = $budgetedArray[$id][$year] ?? '0';
|
2016-02-05 02:25:15 -06:00
|
|
|
$entry['spent'][$year] = $spent;
|
2016-02-24 10:37:08 -06:00
|
|
|
$entry['budgeted'][$year] = round($budgeted, 2);
|
|
|
|
|
2016-02-05 02:25:15 -06:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-16 02:41:14 -05:00
|
|
|
/**
|
2015-12-28 12:56:28 -06:00
|
|
|
*
|
2015-05-16 02:41:14 -05:00
|
|
|
* @param BudgetRepositoryInterface $repository
|
2015-12-28 13:04:54 -06:00
|
|
|
* @param $reportType
|
2015-12-28 00:43:05 -06:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
2015-05-16 02:41:14 -05:00
|
|
|
*
|
2015-12-28 00:43:05 -06:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2015-05-16 02:41:14 -05:00
|
|
|
*/
|
2016-02-05 02:25:15 -06:00
|
|
|
public function year(BudgetRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
|
2015-05-16 02:41:14 -05:00
|
|
|
{
|
2015-06-02 10:44:50 -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-28 13:04:54 -06:00
|
|
|
$cache->addProperty($reportType);
|
2015-12-24 03:14:01 -06:00
|
|
|
$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-06-02 10:44:50 -05:00
|
|
|
}
|
|
|
|
|
2015-12-28 12:56:28 -06:00
|
|
|
$budgetInformation = $repository->getBudgetsAndExpensesPerMonth($accounts, $start, $end);
|
2015-12-28 10:57:03 -06:00
|
|
|
$budgets = new Collection;
|
|
|
|
$entries = new Collection;
|
2015-07-31 11:18:54 -05:00
|
|
|
|
2015-12-28 10:57:03 -06:00
|
|
|
/** @var array $row */
|
|
|
|
foreach ($budgetInformation as $row) {
|
|
|
|
$budgets->push($row['budget']);
|
|
|
|
}
|
2015-05-16 02:41:14 -05:00
|
|
|
|
|
|
|
while ($start < $end) {
|
|
|
|
// month is the current end of the period:
|
|
|
|
$month = clone $start;
|
|
|
|
$month->endOfMonth();
|
2015-12-28 10:57:03 -06:00
|
|
|
$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;
|
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);
|
2015-06-02 10:44:50 -05:00
|
|
|
|
|
|
|
return Response::json($data);
|
2015-05-16 02:41:14 -05:00
|
|
|
}
|
2015-05-20 12:56:14 -05:00
|
|
|
}
|