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

127 lines
4.0 KiB
PHP
Raw Normal View History

2016-10-30 12:29:26 -05:00
<?php
/**
* BudgetController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-10-30 12:29:26 -05:00
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
2016-10-30 12:29:26 -05:00
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-10-30 12:29:26 -05:00
*/
declare(strict_types=1);
2016-10-30 12:29:26 -05:00
namespace FireflyIII\Http\Controllers\Report;
use Carbon\Carbon;
use FireflyIII\Helpers\Report\BudgetReportHelperInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2016-10-30 12:29:26 -05:00
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
/**
2017-11-15 05:25:49 -06:00
* Class BudgetController.
2016-10-30 12:29:26 -05:00
*/
class BudgetController extends Controller
{
/**
2016-12-06 00:06:20 -06:00
* @param BudgetReportHelperInterface $helper
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
2016-11-19 00:27:54 -06:00
*
2016-12-06 00:06:20 -06:00
* @return mixed|string
2018-04-27 23:23:13 -05:00
* @throws \Throwable
*/
2016-12-06 00:06:20 -06:00
public function general(BudgetReportHelperInterface $helper, Collection $accounts, Carbon $start, Carbon $end)
{
2016-12-06 00:06:20 -06:00
// chart properties for cache:
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-06 00:06:20 -06:00
$cache->addProperty('budget-report');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
2017-03-04 04:19:44 -06:00
return $cache->get(); // @codeCoverageIgnore
}
2016-12-06 00:06:20 -06:00
$budgets = $helper->getBudgetReport($start, $end, $accounts);
2016-12-06 00:06:20 -06:00
$result = view('reports.partials.budgets', compact('budgets'))->render();
$cache->store($result);
return $result;
}
2016-10-30 12:29:26 -05:00
/**
2016-12-06 00:06:20 -06:00
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
2016-10-30 12:29:26 -05:00
*
2016-12-06 00:06:20 -06:00
* @return mixed|string
2018-04-27 23:23:13 -05:00
* @throws \Throwable
2016-10-30 12:29:26 -05:00
*/
2016-12-06 00:06:20 -06:00
public function period(Collection $accounts, Carbon $start, Carbon $end)
2016-10-30 12:29:26 -05:00
{
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-06 00:06:20 -06:00
$cache->addProperty('budget-period-report');
2016-10-30 12:29:26 -05:00
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
2017-03-04 04:19:44 -06:00
return $cache->get(); // @codeCoverageIgnore
2016-10-30 12:29:26 -05:00
}
2016-12-06 00:06:20 -06:00
// generate budget report right here.
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$budgets = $repository->getBudgets();
$data = $repository->getBudgetPeriodReport($budgets, $accounts, $start, $end);
$data[0] = $repository->getNoBudgetPeriodReport($accounts, $start, $end); // append report data for "no budget"
$report = $this->filterBudgetPeriodReport($data);
$periods = app('navigation')->listOfPeriods($start, $end);
2016-10-30 12:29:26 -05:00
2016-12-06 00:06:20 -06:00
$result = view('reports.partials.budget-period', compact('report', 'periods'))->render();
2016-10-30 12:29:26 -05:00
$cache->store($result);
return $result;
}
/**
2017-11-15 05:25:49 -06:00
* Filters empty results from getBudgetPeriodReport.
*
* @param array $data
*
* @return array
*/
private function filterBudgetPeriodReport(array $data): array
{
/**
2017-11-15 05:25:49 -06:00
* @var int
* @var array $set
*/
foreach ($data as $budgetId => $set) {
$sum = '0';
foreach ($set['entries'] as $amount) {
$sum = bcadd($amount, $sum);
}
$data[$budgetId]['sum'] = $sum;
2017-11-15 05:25:49 -06:00
if (0 === bccomp('0', $sum)) {
unset($data[$budgetId]);
}
}
return $data;
}
}