1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AvailableBudgetController.php |
5
|
|
|
* Copyright (c) 2019 [email protected] |
6
|
|
|
* |
7
|
|
|
* This file is part of Firefly III. |
8
|
|
|
* |
9
|
|
|
* Firefly III is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU General Public License as published by |
11
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
12
|
|
|
* (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* Firefly III is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
declare(strict_types=1); |
24
|
|
|
|
25
|
|
|
namespace FireflyIII\Api\V1\Controllers\Chart; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
use FireflyIII\Api\V1\Controllers\Controller; |
29
|
|
|
use FireflyIII\Models\AvailableBudget; |
30
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; |
31
|
|
|
use FireflyIII\User; |
32
|
|
|
use Illuminate\Http\JsonResponse; |
33
|
|
|
use Illuminate\Support\Collection; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class AvailableBudgetController |
37
|
|
|
*/ |
38
|
|
|
class AvailableBudgetController extends Controller |
39
|
|
|
{ |
40
|
|
|
/** @var BudgetRepositoryInterface */ |
41
|
|
|
private $repository; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* AvailableBudgetController constructor. |
45
|
|
|
*/ |
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
parent::__construct(); |
49
|
|
|
$this->middleware( |
50
|
|
|
function ($request, $next) { |
51
|
|
|
/** @var User $user */ |
52
|
|
|
$user = auth()->user(); |
53
|
|
|
$this->repository = app(BudgetRepositoryInterface::class); |
54
|
|
|
$this->repository->setUser($user); |
55
|
|
|
|
56
|
|
|
return $next($request); |
57
|
|
|
} |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param AvailableBudget $availableBudget |
63
|
|
|
* |
64
|
|
|
* @return JsonResponse |
65
|
|
|
*/ |
66
|
|
|
public function overview(AvailableBudget $availableBudget): JsonResponse |
67
|
|
|
{ |
68
|
|
|
$currency = $availableBudget->transactionCurrency; |
69
|
|
|
$budgets = $this->repository->getActiveBudgets(); |
70
|
|
|
$budgetInformation = $this->repository->spentInPeriodMc($budgets, new Collection, $availableBudget->start_date, $availableBudget->end_date); |
|
|
|
|
71
|
|
|
$spent = 0.0; |
72
|
|
|
|
73
|
|
|
// get for current currency |
74
|
|
|
foreach ($budgetInformation as $spentInfo) { |
75
|
|
|
if ($spentInfo['currency_id'] === $availableBudget->transaction_currency_id) { |
76
|
|
|
$spent = $spentInfo['amount']; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
$left = bcadd($availableBudget->amount, (string)$spent); |
80
|
|
|
// left less than zero? Set to zero. |
81
|
|
|
if (bccomp($left, '0') === -1) { |
82
|
|
|
$left = '0'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$chartData = [ |
86
|
|
|
[ |
87
|
|
|
'label' => trans('firefly.spent'), |
88
|
|
|
'currency_id' => $currency->id, |
89
|
|
|
'currency_code' => $currency->code, |
90
|
|
|
'currency_symbol' => $currency->symbol, |
91
|
|
|
'currency_decimal_places' => $currency->decimal_places, |
92
|
|
|
'type' => 'pie', |
93
|
|
|
'yAxisID' => 0, // 0, 1, 2 |
94
|
|
|
'entries' => [$spent * -1], |
95
|
|
|
], |
96
|
|
|
[ |
97
|
|
|
'label' => trans('firefly.left'), |
98
|
|
|
'currency_id' => $currency->id, |
99
|
|
|
'currency_code' => $currency->code, |
100
|
|
|
'currency_symbol' => $currency->symbol, |
101
|
|
|
'currency_decimal_places' => $currency->decimal_places, |
102
|
|
|
'type' => 'line', // line, area or bar |
103
|
|
|
'yAxisID' => 0, // 0, 1, 2 |
104
|
|
|
'entries' => [round($left, $currency->decimal_places)], |
|
|
|
|
105
|
|
|
], |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
return response()->json($chartData); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|