1 | <?php |
||
2 | |||
3 | /** |
||
4 | * AvailableBudgetController.php |
||
5 | * Copyright (c) 2019 [email protected] |
||
6 | * |
||
7 | * This file is part of Firefly III (https://github.com/firefly-iii). |
||
8 | * |
||
9 | * This program is free software: you can redistribute it and/or modify |
||
10 | * it under the terms of the GNU Affero General Public License as |
||
11 | * published by the Free Software Foundation, either version 3 of the |
||
12 | * License, or (at your option) any later version. |
||
13 | * |
||
14 | * This program 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 Affero General Public License for more details. |
||
18 | * |
||
19 | * You should have received a copy of the GNU Affero General Public License |
||
20 | * along with this program. If not, see <https://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\Repositories\Budget\OperationsRepositoryInterface; |
||
32 | use FireflyIII\User; |
||
33 | use Illuminate\Http\JsonResponse; |
||
34 | use Illuminate\Support\Collection; |
||
35 | |||
36 | /** |
||
37 | * Class AvailableBudgetController |
||
38 | */ |
||
39 | class AvailableBudgetController extends Controller |
||
40 | { |
||
41 | /** @var OperationsRepositoryInterface */ |
||
42 | private $opsRepository; |
||
43 | /** @var BudgetRepositoryInterface */ |
||
44 | private $repository; |
||
45 | |||
46 | /** |
||
47 | * AvailableBudgetController constructor. |
||
48 | * |
||
49 | * @codeCoverageIgnore |
||
50 | */ |
||
51 | public function __construct() |
||
52 | { |
||
53 | parent::__construct(); |
||
54 | $this->middleware( |
||
55 | function ($request, $next) { |
||
56 | /** @var User $user */ |
||
57 | $user = auth()->user(); |
||
58 | $this->repository = app(BudgetRepositoryInterface::class); |
||
59 | $this->opsRepository = app(OperationsRepositoryInterface::class); |
||
60 | $this->repository->setUser($user); |
||
61 | $this->opsRepository->setUser($user); |
||
62 | |||
63 | return $next($request); |
||
64 | } |
||
65 | ); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param AvailableBudget $availableBudget |
||
70 | * |
||
71 | * @return JsonResponse |
||
72 | */ |
||
73 | public function overview(AvailableBudget $availableBudget): JsonResponse |
||
74 | { |
||
75 | $currency = $availableBudget->transactionCurrency; |
||
76 | $budgets = $this->repository->getActiveBudgets(); |
||
77 | $budgetInformation = $this->opsRepository->spentInPeriodMc($budgets, new Collection, $availableBudget->start_date, $availableBudget->end_date); |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
78 | $spent = 0.0; |
||
79 | |||
80 | // get for current currency |
||
81 | foreach ($budgetInformation as $spentInfo) { |
||
82 | if ($spentInfo['currency_id'] === $availableBudget->transaction_currency_id) { |
||
83 | $spent = $spentInfo['amount']; |
||
84 | } |
||
85 | } |
||
86 | $left = bcadd($availableBudget->amount, (string) $spent); |
||
87 | // left less than zero? Set to zero. |
||
88 | if (-1 === bccomp($left, '0')) { |
||
89 | $left = '0'; |
||
90 | } |
||
91 | |||
92 | $chartData = [ |
||
93 | [ |
||
94 | 'label' => trans('firefly.spent'), |
||
95 | 'currency_id' => $currency->id, |
||
96 | 'currency_code' => $currency->code, |
||
97 | 'currency_symbol' => $currency->symbol, |
||
98 | 'currency_decimal_places' => $currency->decimal_places, |
||
99 | 'type' => 'pie', |
||
100 | 'yAxisID' => 0, // 0, 1, 2 |
||
101 | 'entries' => [$spent * -1], |
||
102 | ], |
||
103 | [ |
||
104 | 'label' => trans('firefly.left'), |
||
105 | 'currency_id' => $currency->id, |
||
106 | 'currency_code' => $currency->code, |
||
107 | 'currency_symbol' => $currency->symbol, |
||
108 | 'currency_decimal_places' => $currency->decimal_places, |
||
109 | 'type' => 'line', // line, area or bar |
||
110 | 'yAxisID' => 0, // 0, 1, 2 |
||
111 | 'entries' => [round($left, $currency->decimal_places)], |
||
112 | ], |
||
113 | ]; |
||
114 | |||
115 | return response()->json($chartData); |
||
116 | } |
||
117 | |||
118 | } |
||
119 |