mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-06 12:45:30 +00:00
Simplify chart code with external helper.
This commit is contained in:
@@ -36,6 +36,7 @@ use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use FireflyIII\Repositories\Budget\NoBudgetRepositoryInterface;
|
||||
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use FireflyIII\Support\Chart\Budget\FrontpageChartGenerator;
|
||||
use FireflyIII\Support\Http\Controllers\AugumentData;
|
||||
use FireflyIII\Support\Http\Controllers\DateCalculation;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -411,6 +412,7 @@ class BudgetController extends Controller
|
||||
{
|
||||
$start = session('start', Carbon::now()->startOfMonth());
|
||||
$end = session('end', Carbon::now()->endOfMonth());
|
||||
|
||||
// chart properties for cache:
|
||||
$cache = new CacheProperties();
|
||||
$cache->addProperty($start);
|
||||
@@ -419,59 +421,14 @@ class BudgetController extends Controller
|
||||
if ($cache->has()) {
|
||||
return response()->json($cache->get()); // @codeCoverageIgnore
|
||||
}
|
||||
$budgets = $this->repository->getActiveBudgets();
|
||||
$chartData = [
|
||||
['label' => (string) trans('firefly.spent_in_budget'), 'entries' => [], 'type' => 'bar'],
|
||||
['label' => (string) trans('firefly.left_to_spend'), 'entries' => [], 'type' => 'bar'],
|
||||
['label' => (string) trans('firefly.overspent'), 'entries' => [], 'type' => 'bar'],
|
||||
];
|
||||
|
||||
/** @var Budget $budget */
|
||||
foreach ($budgets as $budget) {
|
||||
$limits = $this->blRepository->getBudgetLimits($budget, $start, $end);
|
||||
if (0 === $limits->count()) {
|
||||
$spent = $this->opsRepository->sumExpenses($start, $end, null, new Collection([$budget]), null);
|
||||
/** @var array $entry */
|
||||
foreach ($spent as $entry) {
|
||||
$title = sprintf('%s (%s)', $budget->name, $entry['currency_name']);
|
||||
$chartData[0]['entries'][$title] = bcmul($entry['sum'], '-1'); // spent
|
||||
$chartData[1]['entries'][$title] = 0; // left to spend
|
||||
$chartData[2]['entries'][$title] = 0; // overspent
|
||||
}
|
||||
}
|
||||
if (0 !== $limits->count()) {
|
||||
/** @var BudgetLimit $limit */
|
||||
foreach ($limits as $limit) {
|
||||
$spent = $this->opsRepository->sumExpenses(
|
||||
$limit->start_date,
|
||||
$limit->end_date,
|
||||
null,
|
||||
new Collection([$budget]),
|
||||
$limit->transactionCurrency
|
||||
);
|
||||
/** @var array $entry */
|
||||
foreach ($spent as $entry) {
|
||||
$title = sprintf('%s (%s)', $budget->name, $entry['currency_name']);
|
||||
if ($limit->start_date->startOfDay()->ne($start->startOfDay()) || $limit->end_date->startOfDay()->ne($end->startOfDay())) {
|
||||
$title = sprintf(
|
||||
'%s (%s) (%s - %s)',
|
||||
$budget->name,
|
||||
$entry['currency_name'],
|
||||
$limit->start_date->formatLocalized($this->monthAndDayFormat),
|
||||
$limit->end_date->formatLocalized($this->monthAndDayFormat)
|
||||
);
|
||||
}
|
||||
$sumSpent = bcmul($entry['sum'], '-1'); // spent
|
||||
$chartData[0]['entries'][$title] = 1 === bccomp($sumSpent, $limit->amount) ? $limit->amount : $sumSpent;
|
||||
$chartData[1]['entries'][$title] = 1 === bccomp($limit->amount, $sumSpent) ? bcadd($entry['sum'], $limit->amount)
|
||||
: '0';
|
||||
$chartData[2]['entries'][$title] = 1 === bccomp($limit->amount, $sumSpent) ?
|
||||
'0' : bcmul(bcadd($entry['sum'], $limit->amount), '-1');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$data = $this->generator->multiSet($chartData);
|
||||
$generator = app(FrontpageChartGenerator::class);
|
||||
$generator->setUser(auth()->user());
|
||||
$generator->setStart($start);
|
||||
$generator->setEnd($end);
|
||||
|
||||
$chartData = $generator->generate();
|
||||
$data = $this->generator->multiSet($chartData);
|
||||
$cache->store($data);
|
||||
|
||||
return response()->json($data);
|
||||
|
Reference in New Issue
Block a user