2015-05-16 09:41:14 +02:00
|
|
|
<?php
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
* BudgetController.php
|
2017-10-21 08:40:00 +02:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 12:41:23 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02: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 14:41:58 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 12:41:23 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2015-05-16 09:41:14 +02:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Chart;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2016-12-30 08:41:48 +01:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-12-15 09:49:35 +01:00
|
|
|
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
2019-05-30 12:31:19 +02:00
|
|
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
2015-05-16 09:41:14 +02:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
|
|
|
use FireflyIII\Models\Budget;
|
2016-12-30 08:41:48 +01:00
|
|
|
use FireflyIII\Models\BudgetLimit;
|
2016-11-05 18:08:44 +01:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2019-08-30 08:09:39 +02:00
|
|
|
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
|
2015-05-16 09:41:14 +02:00
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
2019-08-29 21:42:55 +02:00
|
|
|
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
|
2016-05-06 10:32:26 +02:00
|
|
|
use FireflyIII\Support\CacheProperties;
|
2018-08-11 06:39:29 +02:00
|
|
|
use FireflyIII\Support\Http\Controllers\AugumentData;
|
2018-07-14 23:22:08 +02:00
|
|
|
use FireflyIII\Support\Http\Controllers\DateCalculation;
|
2018-07-08 12:28:42 +02:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2015-05-16 09:41:14 +02:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class BudgetController.
|
2015-05-16 09:41:14 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
class BudgetController extends Controller
|
|
|
|
{
|
2018-08-11 06:39:29 +02:00
|
|
|
use DateCalculation, AugumentData;
|
2018-07-21 08:06:24 +02:00
|
|
|
/** @var GeneratorInterface Chart generation methods. */
|
2015-06-27 11:44:18 +02:00
|
|
|
protected $generator;
|
2019-08-29 21:42:55 +02:00
|
|
|
/** @var OperationsRepositoryInterface */
|
|
|
|
protected $opsRepository;
|
2018-07-21 08:06:24 +02:00
|
|
|
/** @var BudgetRepositoryInterface The budget repository */
|
2017-01-02 20:42:29 +01:00
|
|
|
protected $repository;
|
2019-08-30 08:09:39 +02:00
|
|
|
/** @var BudgetLimitRepositoryInterface */
|
|
|
|
private $blRepository;
|
2017-01-02 20:42:29 +01:00
|
|
|
|
2015-06-27 11:44:18 +02:00
|
|
|
/**
|
2016-09-16 09:09:54 +02:00
|
|
|
* BudgetController constructor.
|
2019-08-29 21:42:55 +02:00
|
|
|
*
|
2019-06-29 08:14:28 +02:00
|
|
|
* @codeCoverageIgnore
|
2015-06-27 11:44:18 +02:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
2017-01-02 20:42:29 +01:00
|
|
|
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
2019-08-29 21:42:55 +02:00
|
|
|
$this->generator = app(GeneratorInterface::class);
|
|
|
|
$this->repository = app(BudgetRepositoryInterface::class);
|
|
|
|
$this->opsRepository = app(OperationsRepositoryInterface::class);
|
2019-08-30 08:09:39 +02:00
|
|
|
$this->blRepository = app(BudgetLimitRepositoryInterface::class);
|
2017-01-02 20:42:29 +01:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2015-06-27 11:44:18 +02:00
|
|
|
}
|
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
|
2016-04-25 09:57:39 +02:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* Shows overview of a single budget.
|
|
|
|
*
|
2018-08-27 18:59:30 +02:00
|
|
|
* TODO this chart is not multi-currency aware.
|
|
|
|
*
|
2017-01-02 20:42:29 +01:00
|
|
|
* @param Budget $budget
|
2016-04-25 09:57:39 +02:00
|
|
|
*
|
2018-07-08 12:28:42 +02:00
|
|
|
* @return JsonResponse
|
2016-04-25 09:57:39 +02:00
|
|
|
*/
|
2018-07-08 12:28:42 +02:00
|
|
|
public function budget(Budget $budget): JsonResponse
|
2016-04-25 09:57:39 +02:00
|
|
|
{
|
2018-07-23 21:49:15 +02:00
|
|
|
/** @var Carbon $start */
|
|
|
|
$start = $this->repository->firstUseDate($budget) ?? session('start', new Carbon);
|
2018-07-14 23:32:03 +02:00
|
|
|
/** @var Carbon $end */
|
2018-01-14 16:32:26 +01:00
|
|
|
$end = session('end', new Carbon);
|
|
|
|
$cache = new CacheProperties();
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
2016-12-15 09:49:35 +01:00
|
|
|
$cache->addProperty('chart.budget.budget');
|
2018-02-25 15:09:57 +01:00
|
|
|
$cache->addProperty($budget->id);
|
2016-05-06 10:32:26 +02:00
|
|
|
|
|
|
|
if ($cache->has()) {
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
2016-05-06 10:32:26 +02:00
|
|
|
}
|
|
|
|
|
2018-07-15 19:17:26 +02:00
|
|
|
$step = $this->calculateStep($start, $end); // depending on diff, do something with range of chart.
|
2016-05-06 10:32:26 +02:00
|
|
|
$budgetCollection = new Collection([$budget]);
|
2018-01-14 16:32:26 +01:00
|
|
|
$chartData = [];
|
|
|
|
$current = clone $start;
|
|
|
|
$current = app('navigation')->startOfPeriod($current, $step);
|
|
|
|
while ($end >= $current) {
|
2018-07-09 19:24:08 +02:00
|
|
|
/** @var Carbon $currentEnd */
|
2018-01-17 10:17:49 +01:00
|
|
|
$currentEnd = app('navigation')->endOfPeriod($current, $step);
|
2018-07-08 07:59:58 +02:00
|
|
|
if ('1Y' === $step) {
|
2018-01-29 19:08:49 +01:00
|
|
|
$currentEnd->subDay(); // @codeCoverageIgnore
|
2018-01-17 10:17:49 +01:00
|
|
|
}
|
2019-08-29 21:42:55 +02:00
|
|
|
$spent = $this->opsRepository->spentInPeriod($budgetCollection, new Collection, $current, $currentEnd);
|
2018-01-14 16:32:26 +01:00
|
|
|
$label = app('navigation')->periodShow($current, $step);
|
2018-04-02 15:10:40 +02:00
|
|
|
$chartData[$label] = (float)bcmul($spent, '-1');
|
2018-01-17 10:17:49 +01:00
|
|
|
$current = clone $currentEnd;
|
|
|
|
$current->addDay();
|
2016-05-06 10:32:26 +02:00
|
|
|
}
|
|
|
|
|
2018-04-02 15:10:40 +02:00
|
|
|
$data = $this->generator->singleSet((string)trans('firefly.spent'), $chartData);
|
2016-12-15 09:49:35 +01:00
|
|
|
|
2016-05-06 10:32:26 +02:00
|
|
|
$cache->store($data);
|
|
|
|
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($data);
|
2016-04-25 09:57:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
|
2015-05-16 13:06:38 +02:00
|
|
|
/**
|
|
|
|
* Shows the amount left in a specific budget limit.
|
|
|
|
*
|
2018-08-27 18:59:30 +02:00
|
|
|
* TODO this chart is not multi-currency aware.
|
|
|
|
*
|
2019-08-29 21:42:55 +02:00
|
|
|
* @param Budget $budget
|
2017-01-02 20:42:29 +01:00
|
|
|
* @param BudgetLimit $budgetLimit
|
2015-05-16 13:06:38 +02:00
|
|
|
*
|
2018-07-08 12:28:42 +02:00
|
|
|
* @return JsonResponse
|
2017-11-15 12:25:49 +01:00
|
|
|
*
|
2017-01-02 20:42:29 +01:00
|
|
|
* @throws FireflyException
|
2015-05-16 13:06:38 +02:00
|
|
|
*/
|
2018-07-08 12:28:42 +02:00
|
|
|
public function budgetLimit(Budget $budget, BudgetLimit $budgetLimit): JsonResponse
|
2015-05-16 13:06:38 +02:00
|
|
|
{
|
2017-07-15 16:41:07 +02:00
|
|
|
if ($budgetLimit->budget->id !== $budget->id) {
|
2016-12-30 08:41:48 +01:00
|
|
|
throw new FireflyException('This budget limit is not part of this budget.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$start = clone $budgetLimit->start_date;
|
|
|
|
$end = clone $budgetLimit->end_date;
|
2016-05-06 10:32:26 +02:00
|
|
|
$cache = new CacheProperties();
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
2016-12-15 09:49:35 +01:00
|
|
|
$cache->addProperty('chart.budget.budget.limit');
|
2016-12-30 08:41:48 +01:00
|
|
|
$cache->addProperty($budgetLimit->id);
|
2018-02-25 15:09:57 +01:00
|
|
|
$cache->addProperty($budget->id);
|
2016-05-06 10:32:26 +02:00
|
|
|
|
|
|
|
if ($cache->has()) {
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
2016-05-06 10:32:26 +02:00
|
|
|
}
|
|
|
|
|
2016-12-15 09:49:35 +01:00
|
|
|
$entries = [];
|
2016-12-30 08:41:48 +01:00
|
|
|
$amount = $budgetLimit->amount;
|
2016-05-06 10:32:26 +02:00
|
|
|
$budgetCollection = new Collection([$budget]);
|
|
|
|
while ($start <= $end) {
|
2019-08-29 21:42:55 +02:00
|
|
|
$spent = $this->opsRepository->spentInPeriod($budgetCollection, new Collection, $start, $start);
|
2016-12-15 09:49:35 +01:00
|
|
|
$amount = bcadd($amount, $spent);
|
2018-04-02 15:10:40 +02:00
|
|
|
$format = $start->formatLocalized((string)trans('config.month_and_day'));
|
2016-12-15 09:49:35 +01:00
|
|
|
$entries[$format] = $amount;
|
2016-05-06 10:32:26 +02:00
|
|
|
|
|
|
|
$start->addDay();
|
|
|
|
}
|
2018-04-02 15:10:40 +02:00
|
|
|
$data = $this->generator->singleSet((string)trans('firefly.left'), $entries);
|
2016-05-06 10:32:26 +02:00
|
|
|
$cache->store($data);
|
2015-05-16 13:06:38 +02:00
|
|
|
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($data);
|
2015-05-16 13:06:38 +02:00
|
|
|
}
|
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
|
2017-04-22 07:04:39 +02:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* Shows how much is spent per asset account.
|
|
|
|
*
|
2018-08-27 18:59:30 +02:00
|
|
|
* TODO this chart is not multi-currency aware.
|
|
|
|
*
|
2019-08-29 21:42:55 +02:00
|
|
|
* @param Budget $budget
|
2017-04-22 07:04:39 +02:00
|
|
|
* @param BudgetLimit|null $budgetLimit
|
|
|
|
*
|
2018-07-08 12:28:42 +02:00
|
|
|
* @return JsonResponse
|
2017-04-22 07:04:39 +02:00
|
|
|
*/
|
2018-07-08 12:28:42 +02:00
|
|
|
public function expenseAsset(Budget $budget, ?BudgetLimit $budgetLimit): JsonResponse
|
2017-04-22 07:04:39 +02:00
|
|
|
{
|
2018-07-09 19:24:08 +02:00
|
|
|
$budgetLimitId = null === $budgetLimit ? 0 : $budgetLimit->id;
|
|
|
|
$cache = new CacheProperties;
|
2017-04-22 07:04:39 +02:00
|
|
|
$cache->addProperty($budget->id);
|
2018-07-09 19:24:08 +02:00
|
|
|
$cache->addProperty($budgetLimitId);
|
2017-04-22 07:04:39 +02:00
|
|
|
$cache->addProperty('chart.budget.expense-asset');
|
|
|
|
if ($cache->has()) {
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
2017-04-22 07:04:39 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 12:31:19 +02:00
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
|
|
|
$collector->setBudget($budget);
|
2018-07-09 19:24:08 +02:00
|
|
|
if (null !== $budgetLimit) {
|
2017-04-22 07:04:39 +02:00
|
|
|
$collector->setRange($budgetLimit->start_date, $budgetLimit->end_date);
|
|
|
|
}
|
|
|
|
|
2019-05-30 12:31:19 +02:00
|
|
|
$journals = $collector->getExtractedJournals();
|
|
|
|
$result = [];
|
|
|
|
$chartData = [];
|
|
|
|
foreach ($journals as $journal) {
|
|
|
|
$assetId = (int)$journal['destination_account_id'];
|
2017-04-22 07:04:39 +02:00
|
|
|
$result[$assetId] = $result[$assetId] ?? '0';
|
2019-05-30 12:31:19 +02:00
|
|
|
$result[$assetId] = bcadd($journal['amount'], $result[$assetId]);
|
2017-04-22 07:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$names = $this->getAccountNames(array_keys($result));
|
|
|
|
foreach ($result as $assetId => $amount) {
|
|
|
|
$chartData[$names[$assetId]] = $amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $this->generator->pieChart($chartData);
|
|
|
|
$cache->store($data);
|
|
|
|
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($data);
|
2017-04-22 07:04:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
|
2017-04-22 07:04:39 +02:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* Shows how much is spent per category.
|
|
|
|
*
|
2018-08-27 18:59:30 +02:00
|
|
|
* TODO this chart is not multi-currency aware.
|
|
|
|
*
|
2019-08-29 21:42:55 +02:00
|
|
|
* @param Budget $budget
|
2017-04-22 07:04:39 +02:00
|
|
|
* @param BudgetLimit|null $budgetLimit
|
|
|
|
*
|
2018-07-08 12:28:42 +02:00
|
|
|
* @return JsonResponse
|
2017-04-22 07:04:39 +02:00
|
|
|
*/
|
2018-07-08 12:28:42 +02:00
|
|
|
public function expenseCategory(Budget $budget, ?BudgetLimit $budgetLimit): JsonResponse
|
2017-04-22 07:04:39 +02:00
|
|
|
{
|
2018-07-09 19:24:08 +02:00
|
|
|
$budgetLimitId = null === $budgetLimit ? 0 : $budgetLimit->id;
|
2018-07-13 15:50:42 +02:00
|
|
|
$cache = new CacheProperties;
|
2017-04-22 07:04:39 +02:00
|
|
|
$cache->addProperty($budget->id);
|
2018-07-09 19:24:08 +02:00
|
|
|
$cache->addProperty($budgetLimitId);
|
2017-04-22 07:04:39 +02:00
|
|
|
$cache->addProperty('chart.budget.expense-category');
|
|
|
|
if ($cache->has()) {
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
2017-04-22 07:04:39 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 12:31:19 +02:00
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
|
|
|
$collector->setBudget($budget)->withCategoryInformation();
|
2018-07-09 19:24:08 +02:00
|
|
|
if (null !== $budgetLimit) {
|
2017-04-22 07:04:39 +02:00
|
|
|
$collector->setRange($budgetLimit->start_date, $budgetLimit->end_date);
|
|
|
|
}
|
|
|
|
|
2019-05-30 12:31:19 +02:00
|
|
|
$journals = $collector->getExtractedJournals();
|
|
|
|
$result = [];
|
|
|
|
$chartData = [];
|
|
|
|
foreach ($journals as $journal) {
|
|
|
|
$categoryId = (int)$journal['category_id'];
|
2017-04-22 07:04:39 +02:00
|
|
|
$result[$categoryId] = $result[$categoryId] ?? '0';
|
2019-05-30 12:31:19 +02:00
|
|
|
$result[$categoryId] = bcadd($journal['amount'], $result[$categoryId]);
|
2017-04-22 07:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$names = $this->getCategoryNames(array_keys($result));
|
|
|
|
foreach ($result as $categoryId => $amount) {
|
|
|
|
$chartData[$names[$categoryId]] = $amount;
|
|
|
|
}
|
|
|
|
$data = $this->generator->pieChart($chartData);
|
|
|
|
$cache->store($data);
|
2017-10-15 14:05:31 +02:00
|
|
|
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($data);
|
2017-04-22 07:04:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
|
2017-04-22 07:04:39 +02:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* Shows how much is spent per expense account.
|
|
|
|
*
|
2018-08-27 18:59:30 +02:00
|
|
|
* TODO this chart is not multi-currency aware.
|
|
|
|
*
|
2019-08-29 21:42:55 +02:00
|
|
|
* @param Budget $budget
|
2017-04-22 07:04:39 +02:00
|
|
|
* @param BudgetLimit|null $budgetLimit
|
|
|
|
*
|
2018-07-08 12:28:42 +02:00
|
|
|
* @return JsonResponse
|
2017-04-22 07:04:39 +02:00
|
|
|
*/
|
2018-07-08 12:28:42 +02:00
|
|
|
public function expenseExpense(Budget $budget, ?BudgetLimit $budgetLimit): JsonResponse
|
2017-04-22 07:04:39 +02:00
|
|
|
{
|
2018-07-09 19:24:08 +02:00
|
|
|
$budgetLimitId = null === $budgetLimit ? 0 : $budgetLimit->id;
|
2018-07-13 15:50:42 +02:00
|
|
|
$cache = new CacheProperties;
|
2017-04-22 07:04:39 +02:00
|
|
|
$cache->addProperty($budget->id);
|
2018-07-09 19:24:08 +02:00
|
|
|
$cache->addProperty($budgetLimitId);
|
2017-04-22 07:04:39 +02:00
|
|
|
$cache->addProperty('chart.budget.expense-expense');
|
|
|
|
if ($cache->has()) {
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
2017-04-22 07:04:39 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 12:31:19 +02:00
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
|
|
|
$collector->setTypes([TransactionType::WITHDRAWAL])->setBudget($budget)->withAccountInformation();
|
2018-07-09 19:24:08 +02:00
|
|
|
if (null !== $budgetLimit) {
|
2017-04-22 07:04:39 +02:00
|
|
|
$collector->setRange($budgetLimit->start_date, $budgetLimit->end_date);
|
|
|
|
}
|
|
|
|
|
2019-05-30 12:31:19 +02:00
|
|
|
$journals = $collector->getExtractedJournals();
|
|
|
|
$result = [];
|
|
|
|
$chartData = [];
|
|
|
|
/** @var array $journal */
|
|
|
|
foreach ($journals as $journal) {
|
|
|
|
$opposingId = (int)$journal['destination_account_id'];
|
2017-04-22 07:04:39 +02:00
|
|
|
$result[$opposingId] = $result[$opposingId] ?? '0';
|
2019-05-30 12:31:19 +02:00
|
|
|
$result[$opposingId] = bcadd($journal['amount'], $result[$opposingId]);
|
2017-04-22 07:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$names = $this->getAccountNames(array_keys($result));
|
|
|
|
foreach ($result as $opposingId => $amount) {
|
2017-04-23 18:53:00 +02:00
|
|
|
$name = $names[$opposingId] ?? 'no name';
|
|
|
|
$chartData[$name] = $amount;
|
2017-04-22 07:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = $this->generator->pieChart($chartData);
|
|
|
|
$cache->store($data);
|
|
|
|
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($data);
|
2017-04-22 07:04:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
|
2015-05-16 09:41:14 +02:00
|
|
|
/**
|
|
|
|
* Shows a budget list with spent/left/overspent.
|
2017-11-15 12:25:49 +01:00
|
|
|
*
|
2018-08-27 18:59:30 +02:00
|
|
|
* TODO this chart is not multi-currency aware.
|
|
|
|
*
|
2018-08-06 19:14:30 +02:00
|
|
|
* @return JsonResponse
|
2018-07-15 19:17:26 +02:00
|
|
|
*
|
2015-05-16 09:41:14 +02:00
|
|
|
*/
|
2018-08-06 19:14:30 +02:00
|
|
|
public function frontpage(): JsonResponse
|
2015-05-16 09:41:14 +02:00
|
|
|
{
|
2016-05-06 10:32:26 +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);
|
2016-12-15 09:49:35 +01:00
|
|
|
$cache->addProperty('chart.budget.frontpage');
|
2016-05-06 10:32:26 +02:00
|
|
|
if ($cache->has()) {
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
2016-05-06 10:32:26 +02:00
|
|
|
}
|
2017-01-02 20:42:29 +01:00
|
|
|
$budgets = $this->repository->getActiveBudgets();
|
2016-12-30 08:41:48 +01:00
|
|
|
$chartData = [
|
2018-04-02 15:10:40 +02:00
|
|
|
['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'],
|
2016-12-15 09:49:35 +01:00
|
|
|
];
|
|
|
|
|
2016-05-06 10:32:26 +02:00
|
|
|
/** @var Budget $budget */
|
|
|
|
foreach ($budgets as $budget) {
|
|
|
|
// get relevant repetitions:
|
2019-08-30 08:09:39 +02:00
|
|
|
$limits = $this->blRepository->getBudgetLimits($budget, $start, $end);
|
2016-12-30 08:41:48 +01:00
|
|
|
$expenses = $this->getExpensesForBudget($limits, $budget, $start, $end);
|
2017-06-02 06:45:38 +02:00
|
|
|
|
2016-12-29 17:42:46 +01:00
|
|
|
foreach ($expenses as $name => $row) {
|
|
|
|
$chartData[0]['entries'][$name] = $row['spent'];
|
|
|
|
$chartData[1]['entries'][$name] = $row['left'];
|
|
|
|
$chartData[2]['entries'][$name] = $row['overspent'];
|
2016-05-06 10:32:26 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-15 09:49:35 +01:00
|
|
|
// for no budget:
|
2016-12-29 17:42:46 +01:00
|
|
|
$spent = $this->spentInPeriodWithout($start, $end);
|
2018-04-02 15:10:40 +02:00
|
|
|
$name = (string)trans('firefly.no_budget');
|
2017-11-15 12:25:49 +01:00
|
|
|
if (0 !== bccomp($spent, '0')) {
|
2016-12-29 17:42:46 +01:00
|
|
|
$chartData[0]['entries'][$name] = bcmul($spent, '-1');
|
|
|
|
$chartData[1]['entries'][$name] = '0';
|
|
|
|
$chartData[2]['entries'][$name] = '0';
|
2016-12-15 09:49:35 +01:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:44:06 +01:00
|
|
|
$data = $this->generator->multiSet($chartData);
|
2016-05-06 10:32:26 +02:00
|
|
|
$cache->store($data);
|
|
|
|
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($data);
|
2015-05-16 09:41:14 +02:00
|
|
|
}
|
|
|
|
|
2019-08-16 21:38:35 +02:00
|
|
|
|
2016-04-24 20:23:17 +02:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* Shows a budget overview chart (spent and budgeted).
|
2017-01-02 20:42:29 +01:00
|
|
|
*
|
2018-08-27 18:59:30 +02:00
|
|
|
* TODO this chart is not multi-currency aware.
|
|
|
|
*
|
2019-08-29 21:42:55 +02:00
|
|
|
* @param Budget $budget
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2017-01-02 20:42:29 +01:00
|
|
|
* @param Collection $accounts
|
2016-04-26 09:21:57 +02:00
|
|
|
*
|
2018-07-08 12:28:42 +02:00
|
|
|
* @return JsonResponse
|
2016-04-24 20:23:17 +02:00
|
|
|
*/
|
2018-07-08 12:28:42 +02:00
|
|
|
public function period(Budget $budget, Collection $accounts, Carbon $start, Carbon $end): JsonResponse
|
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);
|
2016-12-15 10:41:10 +01:00
|
|
|
$cache->addProperty('chart.budget.period');
|
2016-05-06 22:53:08 +02:00
|
|
|
if ($cache->has()) {
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
2016-05-06 22:53:08 +02:00
|
|
|
}
|
2017-12-17 14:06:14 +01:00
|
|
|
$periods = app('navigation')->listOfPeriods($start, $end);
|
2019-08-30 08:12:15 +02:00
|
|
|
$entries = $this->opsRepository->getBudgetPeriodReport(new Collection([$budget]), $accounts, $start, $end); // get the expenses
|
2017-01-02 20:42:29 +01:00
|
|
|
$budgeted = $this->getBudgetedInPeriod($budget, $start, $end);
|
2016-05-06 22:53:08 +02:00
|
|
|
|
2016-12-15 10:41:10 +01:00
|
|
|
// join them into one set of data:
|
|
|
|
$chartData = [
|
2018-04-02 15:10:40 +02:00
|
|
|
['label' => (string)trans('firefly.spent'), 'type' => 'bar', 'entries' => []],
|
|
|
|
['label' => (string)trans('firefly.budgeted'), 'type' => 'bar', 'entries' => []],
|
2016-12-15 10:41:10 +01:00
|
|
|
];
|
2016-11-19 13:37:44 +01:00
|
|
|
|
2016-12-15 10:41:10 +01:00
|
|
|
foreach (array_keys($periods) as $period) {
|
|
|
|
$label = $periods[$period];
|
2018-04-02 15:10:40 +02:00
|
|
|
$spent = $entries[$budget->id]['entries'][$period] ?? '0';
|
|
|
|
$limit = (int)($budgeted[$period] ?? 0);
|
2016-12-30 13:45:02 +01:00
|
|
|
$chartData[0]['entries'][$label] = round(bcmul($spent, '-1'), 12);
|
2016-12-15 10:41:10 +01:00
|
|
|
$chartData[1]['entries'][$label] = $limit;
|
|
|
|
}
|
2016-12-15 10:44:06 +01:00
|
|
|
$data = $this->generator->multiSet($chartData);
|
2016-05-06 22:53:08 +02:00
|
|
|
$cache->store($data);
|
|
|
|
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($data);
|
2015-05-16 09:41:14 +02:00
|
|
|
}
|
2016-07-30 16:29:04 +02:00
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
|
2016-12-04 18:02:19 +01:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* Shows a chart for transactions without a budget.
|
|
|
|
*
|
2018-08-27 18:59:30 +02:00
|
|
|
* TODO this chart is not multi-currency aware.
|
|
|
|
*
|
2017-01-02 20:42:29 +01:00
|
|
|
* @param Collection $accounts
|
2019-08-29 21:42:55 +02:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2016-12-04 18:02:19 +01:00
|
|
|
*
|
2018-07-08 12:28:42 +02:00
|
|
|
* @return JsonResponse
|
2016-12-04 18:02:19 +01:00
|
|
|
*/
|
2018-07-08 12:28:42 +02:00
|
|
|
public function periodNoBudget(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
|
2016-12-04 18:02:19 +01:00
|
|
|
{
|
|
|
|
// chart properties for cache:
|
|
|
|
$cache = new CacheProperties();
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty($accounts);
|
2016-12-15 10:41:10 +01:00
|
|
|
$cache->addProperty('chart.budget.no-budget');
|
2016-12-04 18:02:19 +01:00
|
|
|
if ($cache->has()) {
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
2016-12-04 18:02:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// the expenses:
|
2017-12-17 14:06:14 +01:00
|
|
|
$periods = app('navigation')->listOfPeriods($start, $end);
|
2017-01-02 20:42:29 +01:00
|
|
|
$entries = $this->repository->getNoBudgetPeriodReport($accounts, $start, $end);
|
2016-12-15 10:41:10 +01:00
|
|
|
$chartData = [];
|
2016-12-04 18:02:19 +01:00
|
|
|
|
|
|
|
// join them:
|
|
|
|
foreach (array_keys($periods) as $period) {
|
2016-12-15 10:41:10 +01:00
|
|
|
$label = $periods[$period];
|
2018-04-02 15:10:40 +02:00
|
|
|
$spent = $entries['entries'][$period] ?? '0';
|
2016-12-15 10:41:56 +01:00
|
|
|
$chartData[$label] = bcmul($spent, '-1');
|
2016-12-04 18:02:19 +01:00
|
|
|
}
|
2018-04-02 15:10:40 +02:00
|
|
|
$data = $this->generator->singleSet((string)trans('firefly.spent'), $chartData);
|
2016-12-04 18:02:19 +01:00
|
|
|
$cache->store($data);
|
|
|
|
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($data);
|
2016-12-04 18:02:19 +01:00
|
|
|
}
|
|
|
|
|
2017-04-22 07:04:39 +02:00
|
|
|
|
2015-05-20 19:56:14 +02:00
|
|
|
}
|