Files
firefly-iii/app/Api/V1/Controllers/Chart/AvailableBudgetController.php

121 lines
4.2 KiB
PHP
Raw Normal View History

2019-01-26 12:10:35 +01:00
<?php
2019-02-09 10:36:59 +01:00
/**
* AvailableBudgetController.php
* Copyright (c) 2019 james@firefly-iii.org
2019-02-09 10:36:59 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-02-09 10:36:59 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2019-02-09 10:36:59 +01:00
*
* This program is distributed in the hope that it will be useful,
2019-02-09 10:36:59 +01:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2019-02-09 10:36:59 +01:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-02-09 10:36:59 +01:00
*/
2019-01-26 12:10:35 +01:00
declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers\Chart;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Models\AvailableBudget;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2019-08-30 08:19:55 +02:00
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
2019-01-26 12:10:35 +01:00
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
/**
* Class AvailableBudgetController
*/
class AvailableBudgetController extends Controller
{
2019-08-30 08:19:55 +02:00
/** @var OperationsRepositoryInterface */
private $opsRepository;
2020-07-31 09:42:00 +02:00
2019-01-26 12:10:35 +01:00
/** @var BudgetRepositoryInterface */
private $repository;
2020-07-31 09:24:08 +02:00
2019-01-26 12:10:35 +01:00
/**
* AvailableBudgetController constructor.
2019-08-30 08:19:55 +02:00
*
2019-06-09 15:28:54 +02:00
* @codeCoverageIgnore
2019-01-26 12:10:35 +01:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
/** @var User $user */
2019-08-30 08:19:55 +02:00
$user = auth()->user();
$this->repository = app(BudgetRepositoryInterface::class);
$this->opsRepository = app(OperationsRepositoryInterface::class);
2019-01-26 12:10:35 +01:00
$this->repository->setUser($user);
2019-08-30 08:19:55 +02:00
$this->opsRepository->setUser($user);
2019-01-26 12:10:35 +01:00
return $next($request);
}
);
}
/**
* @param AvailableBudget $availableBudget
*
* @return JsonResponse
*/
2019-02-13 17:38:41 +01:00
public function overview(AvailableBudget $availableBudget): JsonResponse
2019-01-26 12:10:35 +01:00
{
$currency = $availableBudget->transactionCurrency;
$budgets = $this->repository->getActiveBudgets();
2019-08-30 08:19:55 +02:00
$budgetInformation = $this->opsRepository->spentInPeriodMc($budgets, new Collection, $availableBudget->start_date, $availableBudget->end_date);
2019-01-26 12:10:35 +01:00
$spent = 0.0;
// get for current currency
foreach ($budgetInformation as $spentInfo) {
if ($spentInfo['currency_id'] === $availableBudget->transaction_currency_id) {
$spent = $spentInfo['amount'];
}
}
$left = bcadd($availableBudget->amount, (string) $spent);
2019-01-26 12:10:35 +01:00
// left less than zero? Set to zero.
2020-03-14 07:30:55 +01:00
if (-1 === bccomp($left, '0')) {
2019-01-26 12:10:35 +01:00
$left = '0';
}
$chartData = [
[
'label' => trans('firefly.spent'),
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'type' => 'pie',
'yAxisID' => 0, // 0, 1, 2
'entries' => [$spent * -1],
],
[
'label' => trans('firefly.left'),
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'type' => 'line', // line, area or bar
'yAxisID' => 0, // 0, 1, 2
'entries' => [round($left, $currency->decimal_places)],
],
];
return response()->json($chartData);
}
2019-02-09 10:36:59 +01:00
}