1 | <?php |
||||||
2 | |||||||
3 | /** |
||||||
4 | * CategoryController.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 | use Carbon\Carbon; |
||||||
28 | use FireflyIII\Api\V1\Controllers\Controller; |
||||||
29 | use FireflyIII\Exceptions\FireflyException; |
||||||
30 | use FireflyIII\Repositories\Category\CategoryRepositoryInterface; |
||||||
31 | use FireflyIII\User; |
||||||
32 | use Illuminate\Http\JsonResponse; |
||||||
33 | use Illuminate\Http\Request; |
||||||
34 | use Illuminate\Support\Collection; |
||||||
35 | |||||||
36 | /** |
||||||
37 | * Class CategoryController |
||||||
38 | */ |
||||||
39 | class CategoryController extends Controller |
||||||
40 | { |
||||||
41 | /** @var CategoryRepositoryInterface */ |
||||||
42 | private $categoryRepository; |
||||||
43 | |||||||
44 | /** |
||||||
45 | * AccountController constructor. |
||||||
46 | */ |
||||||
47 | public function __construct() |
||||||
48 | { |
||||||
49 | parent::__construct(); |
||||||
50 | $this->middleware( |
||||||
51 | function ($request, $next) { |
||||||
52 | /** @var User $user */ |
||||||
53 | $user = auth()->user(); |
||||||
54 | $this->categoryRepository = app(CategoryRepositoryInterface::class); |
||||||
55 | $this->categoryRepository->setUser($user); |
||||||
56 | |||||||
57 | return $next($request); |
||||||
58 | } |
||||||
59 | ); |
||||||
60 | } |
||||||
61 | |||||||
62 | |||||||
63 | /** |
||||||
64 | * @param Request $request |
||||||
65 | * |
||||||
66 | * @return JsonResponse |
||||||
67 | * @throws FireflyException |
||||||
68 | */ |
||||||
69 | public function overview(Request $request): JsonResponse |
||||||
70 | { |
||||||
71 | // parameters for chart: |
||||||
72 | $start = (string)$request->get('start'); |
||||||
73 | $end = (string)$request->get('end'); |
||||||
74 | if ('' === $start || '' === $end) { |
||||||
75 | throw new FireflyException('Start and end are mandatory parameters.'); |
||||||
76 | } |
||||||
77 | $start = Carbon::createFromFormat('Y-m-d', $start); |
||||||
78 | $end = Carbon::createFromFormat('Y-m-d', $end); |
||||||
79 | $tempData = []; |
||||||
80 | $spent = $this->categoryRepository->spentInPeriodPerCurrency(new Collection, new Collection, $start, $end); |
||||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
It seems like
$end can also be of type false ; however, parameter $end of FireflyIII\Repositories\...ntInPeriodPerCurrency() does only seem to accept Carbon\Carbon , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
81 | $earned = $this->categoryRepository->earnedInPeriodPerCurrency(new Collection, new Collection, $start, $end); |
||||||
0 ignored issues
–
show
It seems like
$end can also be of type false ; however, parameter $end of FireflyIII\Repositories\...edInPeriodPerCurrency() does only seem to accept Carbon\Carbon , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$start can also be of type false ; however, parameter $start of FireflyIII\Repositories\...edInPeriodPerCurrency() does only seem to accept Carbon\Carbon , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
82 | $categories = []; |
||||||
83 | |||||||
84 | // earned: |
||||||
85 | foreach ($earned as $categoryId => $row) { |
||||||
86 | $categoryName = $row['name']; |
||||||
87 | foreach ($row['earned'] as $currencyId => $income) { |
||||||
88 | // find or make set for currency: |
||||||
89 | $key = sprintf('%s-e', $currencyId); |
||||||
90 | $decimalPlaces = $income['currency_decimal_places']; |
||||||
91 | if (!isset($tempData[$key])) { |
||||||
92 | $tempData[$key] = [ |
||||||
93 | 'label' => (string)trans('firefly.box_earned_in_currency', ['currency' => $income['currency_symbol']]), |
||||||
94 | 'currency_id' => $income['currency_id'], |
||||||
95 | 'currency_code' => $income['currency_code'], |
||||||
96 | 'currency_symbol' => $income['currency_symbol'], |
||||||
97 | 'currency_decimal_places' => $decimalPlaces, |
||||||
98 | 'type' => 'bar', // line, area or bar |
||||||
99 | 'yAxisID' => 0, // 0, 1, 2 |
||||||
100 | 'entries' => [], |
||||||
101 | ]; |
||||||
102 | } |
||||||
103 | $amount = round($income['earned'], $decimalPlaces); |
||||||
104 | $categories[$categoryName] = isset($categories[$categoryName]) ? $categories[$categoryName] + $amount : $amount; |
||||||
105 | $tempData[$key]['entries'][$categoryName] |
||||||
106 | = $amount; |
||||||
107 | |||||||
108 | } |
||||||
109 | } |
||||||
110 | |||||||
111 | // earned with no category: |
||||||
112 | $noCategory = $this->categoryRepository->earnedInPeriodPcWoCategory(new Collection, $start, $end); |
||||||
0 ignored issues
–
show
It seems like
$end can also be of type false ; however, parameter $end of FireflyIII\Repositories\...dInPeriodPcWoCategory() does only seem to accept Carbon\Carbon , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$start can also be of type false ; however, parameter $start of FireflyIII\Repositories\...dInPeriodPcWoCategory() does only seem to accept Carbon\Carbon , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
113 | foreach ($noCategory as $currencyId => $income) { |
||||||
114 | $categoryName = (string)trans('firefly.no_category'); |
||||||
115 | // find or make set for currency: |
||||||
116 | $key = sprintf('%s-e', $currencyId); |
||||||
117 | $decimalPlaces = $income['currency_decimal_places']; |
||||||
118 | if (!isset($tempData[$key])) { |
||||||
119 | $tempData[$key] = [ |
||||||
120 | 'label' => (string)trans('firefly.box_earned_in_currency', ['currency' => $income['currency_symbol']]), |
||||||
121 | 'currency_id' => $income['currency_id'], |
||||||
122 | 'currency_code' => $income['currency_code'], |
||||||
123 | 'currency_symbol' => $income['currency_symbol'], |
||||||
124 | 'currency_decimal_places' => $decimalPlaces, |
||||||
125 | 'type' => 'bar', // line, area or bar |
||||||
126 | 'yAxisID' => 0, // 0, 1, 2 |
||||||
127 | 'entries' => [], |
||||||
128 | ]; |
||||||
129 | } |
||||||
130 | $amount = round($income['spent'], $decimalPlaces); |
||||||
131 | $categories[$categoryName] = isset($categories[$categoryName]) ? $categories[$categoryName] + $amount : $amount; |
||||||
132 | $tempData[$key]['entries'][$categoryName] |
||||||
133 | = $amount; |
||||||
134 | } |
||||||
135 | |||||||
136 | |||||||
137 | // spent |
||||||
138 | foreach ($spent as $categoryId => $row) { |
||||||
139 | $categoryName = $row['name']; |
||||||
140 | // create a new set if necessary, "spent (EUR)": |
||||||
141 | foreach ($row['spent'] as $currencyId => $expense) { |
||||||
142 | // find or make set for currency: |
||||||
143 | $key = sprintf('%s-s', $currencyId); |
||||||
144 | $decimalPlaces = $expense['currency_decimal_places']; |
||||||
145 | if (!isset($tempData[$key])) { |
||||||
146 | $tempData[$key] = [ |
||||||
147 | 'label' => (string)trans('firefly.box_spent_in_currency', ['currency' => $expense['currency_symbol']]), |
||||||
148 | 'currency_id' => $expense['currency_id'], |
||||||
149 | 'currency_code' => $expense['currency_code'], |
||||||
150 | 'currency_symbol' => $expense['currency_symbol'], |
||||||
151 | 'currency_decimal_places' => $decimalPlaces, |
||||||
152 | 'type' => 'bar', // line, area or bar |
||||||
153 | 'yAxisID' => 0, // 0, 1, 2 |
||||||
154 | 'entries' => [], |
||||||
155 | ]; |
||||||
156 | } |
||||||
157 | $amount = round($expense['spent'], $decimalPlaces); |
||||||
158 | $categories[$categoryName] = isset($categories[$categoryName]) ? $categories[$categoryName] + $amount : $amount; |
||||||
159 | $tempData[$key]['entries'][$categoryName] |
||||||
160 | = $amount; |
||||||
161 | |||||||
162 | } |
||||||
163 | } |
||||||
164 | |||||||
165 | // spent with no category |
||||||
166 | $noCategory = $this->categoryRepository->spentInPeriodPcWoCategory(new Collection, $start, $end); |
||||||
0 ignored issues
–
show
It seems like
$start can also be of type false ; however, parameter $start of FireflyIII\Repositories\...tInPeriodPcWoCategory() does only seem to accept Carbon\Carbon , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$end can also be of type false ; however, parameter $end of FireflyIII\Repositories\...tInPeriodPcWoCategory() does only seem to accept Carbon\Carbon , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
167 | foreach ($noCategory as $currencyId => $expense) { |
||||||
168 | $categoryName = (string)trans('firefly.no_category'); |
||||||
169 | // find or make set for currency: |
||||||
170 | $key = sprintf('%s-s', $currencyId); |
||||||
171 | $decimalPlaces = $expense['currency_decimal_places']; |
||||||
172 | if (!isset($tempData[$key])) { |
||||||
173 | $tempData[$key] = [ |
||||||
174 | 'label' => (string)trans('firefly.box_spent_in_currency', ['currency' => $expense['currency_symbol']]), |
||||||
175 | 'currency_id' => $expense['currency_id'], |
||||||
176 | 'currency_code' => $expense['currency_code'], |
||||||
177 | 'currency_symbol' => $expense['currency_symbol'], |
||||||
178 | 'currency_decimal_places' => $decimalPlaces, |
||||||
179 | 'type' => 'bar', // line, area or bar |
||||||
180 | 'yAxisID' => 0, // 0, 1, 2 |
||||||
181 | 'entries' => [], |
||||||
182 | ]; |
||||||
183 | } |
||||||
184 | $amount = round($expense['spent'], $decimalPlaces); |
||||||
185 | $categories[$categoryName] = isset($categories[$categoryName]) ? $categories[$categoryName] + $amount : $amount; |
||||||
186 | $tempData[$key]['entries'][$categoryName] |
||||||
187 | = $amount; |
||||||
188 | } |
||||||
189 | |||||||
190 | |||||||
191 | asort($categories); |
||||||
192 | $keys = array_keys($categories); |
||||||
193 | |||||||
194 | // re-sort every spent array and add 0 for missing entries. |
||||||
195 | foreach ($tempData as $index => $set) { |
||||||
196 | $oldSet = $set['entries']; |
||||||
197 | $newSet = []; |
||||||
198 | foreach ($keys as $key) { |
||||||
199 | $value = $oldSet[$key] ?? 0; |
||||||
200 | $value = $value < 0 ? $value * -1 : $value; |
||||||
201 | $newSet[$key] = $value; |
||||||
202 | } |
||||||
203 | $tempData[$index]['entries'] = $newSet; |
||||||
204 | } |
||||||
205 | $chartData = array_values($tempData); |
||||||
206 | |||||||
207 | return response()->json($chartData); |
||||||
208 | } |
||||||
209 | } |
||||||
210 |