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

353 lines
12 KiB
PHP
Raw Normal View History

2015-05-16 09:41:14 +02:00
<?php
/**
* BudgetController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
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;
use FireflyIII\Helpers\Collector\JournalCollector;
2015-05-16 09:41:14 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
2015-05-16 09:41:14 +02:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Support\CacheProperties;
2015-05-16 09:41:14 +02:00
use Illuminate\Support\Collection;
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-07-30 16:29:04 +02:00
/** @var BudgetChartGeneratorInterface */
2015-06-27 11:44:18 +02:00
protected $generator;
/**
2016-09-16 09:09:54 +02:00
* BudgetController constructor.
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
/**
* 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)
{
$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()) {
2016-05-06 22:53:08 +02:00
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;
while ($first < $last) {
// periodspecific dates:
$currentStart = Navigation::startOfPeriod($first, $range);
$currentEnd = Navigation::endOfPeriod($first, $range);
// sub another day because reasons.
$currentEnd->subDay();
2016-05-06 22:53:08 +02:00
$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-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
{
$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()) {
2016-05-15 15:39:22 +02:00
return Response::json($cache->get());
}
$entries = new Collection;
$amount = $repetition->amount;
$budgetCollection = new Collection([$budget]);
while ($start <= $end) {
$spent = $repository->spentInPeriod($budgetCollection, new Collection, $start, $start);
$amount = bcadd($amount, $spent);
$entries->push([clone $start, round($amount, 2)]);
$start->addDay();
}
2016-05-22 15:48:34 +02:00
$data = $this->generator->budgetLimit($entries, 'month_and_day');
$cache->store($data);
2015-05-16 13:06:38 +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-10-09 07:58:27 +02:00
* @param BudgetRepositoryInterface $repository
*
2015-05-16 09:41:14 +02:00
* @return \Symfony\Component\HttpFoundation\Response
*/
2016-09-16 09:09:54 +02:00
public function frontpage(BudgetRepositoryInterface $repository)
2015-05-16 09:41:14 +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()) {
2016-05-13 15:53:39 +02:00
return Response::json($cache->get());
}
2016-09-16 09:09:54 +02:00
$budgets = $repository->getActiveBudgets();
$repetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
$allEntries = new Collection;
/** @var Budget $budget */
foreach ($budgets as $budget) {
// get relevant repetitions:
2016-07-30 16:29:04 +02:00
$reps = $this->filterRepetitions($repetitions, $budget, $start, $end);
if ($reps->count() === 0) {
2016-09-16 09:09:54 +02:00
$collection = $this->spentInPeriodSingle($repository, $budget, $start, $end);
2016-07-30 16:29:04 +02:00
$allEntries = $allEntries->merge($collection);
continue;
}
2016-09-16 09:09:54 +02:00
$collection = $this->spentInPeriodMulti($repository, $budget, $reps);
2016-07-30 16:29:04 +02:00
$allEntries = $allEntries->merge($collection);
}
$entry = $this->spentInPeriodWithout($start, $end);
2016-07-30 16:29:04 +02:00
$allEntries->push($entry);
$data = $this->generator->frontpage($allEntries);
$cache->store($data);
return Response::json($data);
2015-05-16 09:41:14 +02:00
}
2016-11-26 08:41:15 +01:00
2016-04-24 20:23:17 +02:00
/**
2016-05-06 22:53:08 +02:00
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
* @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
*/
2016-05-06 22:53:08 +02:00
public function period(BudgetRepositoryInterface $repository, Budget $budget, Carbon $start, Carbon $end, Collection $accounts)
2016-04-24 20:23:17 +02:00
{
2016-05-06 22:53:08 +02:00
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($accounts);
$cache->addProperty($budget->id);
$cache->addProperty('budget');
$cache->addProperty('period');
if ($cache->has()) {
// return Response::json($cache->get());
2016-05-06 22:53:08 +02:00
}
2016-11-19 13:37:44 +01:00
// the expenses:
$periods = Navigation::listOfPeriods($start, $end);
$entries = $repository->getBudgetPeriodReport(new Collection([$budget]), $accounts, $start, $end);
2016-11-19 13:37:44 +01:00
$budgeted = [];
2016-11-26 08:41:15 +01:00
$key = Navigation::preferredCarbonFormat($start, $end);
$range = Navigation::preferredRangeFormat($start, $end);
2016-11-19 13:37:44 +01:00
// get budgeted:
2016-11-19 13:37:44 +01:00
$repetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
$current = clone $start;
2016-05-06 22:53:08 +02:00
while ($current < $end) {
2016-11-19 13:37:44 +01:00
$currentStart = Navigation::startOfPeriod($current, $range);
$currentEnd = Navigation::endOfPeriod($current, $range);
$reps = $repetitions->filter(
function (LimitRepetition $repetition) use ($budget, $currentStart, $currentEnd) {
if ($repetition->budget_id === $budget->id && $repetition->startdate >= $currentStart && $repetition->enddate <= $currentEnd) {
return true;
2016-05-06 22:53:08 +02:00
}
2016-09-16 09:09:54 +02:00
return false;
2016-05-06 22:53:08 +02:00
}
);
2016-11-19 13:37:44 +01:00
$index = $currentStart->format($key);
$budgeted[$index] = $reps->sum('amount');
2016-05-06 22:53:08 +02:00
$currentEnd->addDay();
$current = clone $currentEnd;
2016-11-19 13:37:44 +01:00
}
2016-05-06 22:53:08 +02:00
2016-11-19 13:37:44 +01:00
// join them:
$result = [];
foreach (array_keys($periods) as $period) {
2016-11-20 11:43:19 +01:00
$nice = $periods[$period];
2016-11-19 13:37:44 +01:00
$result[$nice] = [
'spent' => isset($entries[$budget->id]['entries'][$period]) ? $entries[$budget->id]['entries'][$period] : '0',
2016-11-19 13:37:44 +01:00
'budgeted' => isset($entries[$period]) ? $budgeted[$period] : 0,
];
2016-05-06 22:53:08 +02:00
}
2016-11-19 13:37:44 +01:00
$data = $this->generator->period($result);
2016-05-06 22:53:08 +02:00
$cache->store($data);
return Response::json($data);
2015-05-16 09:41:14 +02:00
}
2016-07-30 16:29:04 +02:00
/**
* @param Collection $repetitions
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
private function filterRepetitions(Collection $repetitions, Budget $budget, Carbon $start, Carbon $end): Collection
{
return $repetitions->filter(
function (LimitRepetition $repetition) use ($budget, $start, $end) {
if ($repetition->startdate < $end && $repetition->enddate > $start && $repetition->budget_id === $budget->id) {
return true;
2016-07-30 16:29:04 +02:00
}
2016-09-16 09:09:54 +02:00
return false;
2016-07-30 16:29:04 +02:00
}
);
}
/**
2016-09-16 09:09:54 +02:00
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
* @param Collection $repetitions
2016-07-30 16:29:04 +02:00
*
* @return Collection
*/
2016-09-16 09:09:54 +02:00
private function spentInPeriodMulti(BudgetRepositoryInterface $repository, Budget $budget, Collection $repetitions): Collection
2016-07-30 16:29:04 +02:00
{
$format = strval(trans('config.month_and_day'));
$collection = new Collection;
$name = $budget->name;
/** @var LimitRepetition $repetition */
foreach ($repetitions as $repetition) {
2016-09-16 09:09:54 +02:00
$expenses = $repository->spentInPeriod(new Collection([$budget]), new Collection, $repetition->startdate, $repetition->enddate);
2016-07-30 16:29:04 +02:00
if ($repetitions->count() > 1) {
2016-09-16 09:09:54 +02:00
$name = $budget->name . ' ' . trans(
'firefly.between_dates',
2016-07-30 16:29:04 +02:00
['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';
$array = [$name, $left, $spent, $overspent, $amount, $spent];
$collection->push($array);
}
return $collection;
}
/**
2016-09-16 09:09:54 +02:00
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
2016-07-30 16:29:04 +02:00
*
* @return Collection
*/
2016-09-16 09:09:54 +02:00
private function spentInPeriodSingle(BudgetRepositoryInterface $repository, Budget $budget, Carbon $start, Carbon $end): Collection
2016-07-30 16:29:04 +02:00
{
$collection = new Collection;
$amount = '0';
$left = '0';
2016-09-16 09:09:54 +02:00
$spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
2016-07-30 16:29:04 +02:00
$overspent = '0';
$array = [$budget->name, $left, $spent, $overspent, $amount, $spent];
$collection->push($array);
return $collection;
}
/**
* @param Carbon $start
* @param Carbon $end
2016-07-30 16:29:04 +02:00
*
* @return array
*/
private function spentInPeriodWithout(Carbon $start, Carbon $end): array
2016-07-30 16:29:04 +02:00
{
// collector
$collector = new JournalCollector(auth()->user());
2016-11-19 13:37:44 +01:00
$types = [TransactionType::WITHDRAWAL];
$collector->setAllAssetAccounts()->setTypes($types)->setRange($start, $end)->withoutBudget();
$journals = $collector->getJournals();
$sum = '0';
/** @var Transaction $entry */
foreach ($journals as $entry) {
$sum = bcadd($entry->transaction_amount, $sum);
2016-07-30 16:29:04 +02:00
}
return [trans('firefly.no_budget'), '0', '0', $sum, '0', '0'];
}
2015-05-20 19:56:14 +02:00
}