1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CurrencyForm.php |
4
|
|
|
* Copyright (c) 2019 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III. |
7
|
|
|
* |
8
|
|
|
* Firefly III is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* Firefly III is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace FireflyIII\Support\Form; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
use Amount as Amt; |
26
|
|
|
use FireflyIII\Exceptions\FireflyException; |
27
|
|
|
use FireflyIII\Models\TransactionCurrency; |
28
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; |
29
|
|
|
use Illuminate\Support\Collection; |
30
|
|
|
use Log; |
31
|
|
|
use Throwable; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class CurrencyForm |
35
|
|
|
* |
36
|
|
|
* All currency related form methods. |
37
|
|
|
* |
38
|
|
|
* TODO cleanup and describe. |
39
|
|
|
*/ |
40
|
|
|
class CurrencyForm |
41
|
|
|
{ |
42
|
|
|
use FormSupport; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* TODO cleanup and describe. |
46
|
|
|
* @param string $name |
47
|
|
|
* @param mixed $value |
48
|
|
|
* @param array $options |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function currencyList(string $name, $value = null, array $options = null): string |
53
|
|
|
{ |
54
|
|
|
/** @var CurrencyRepositoryInterface $currencyRepos */ |
55
|
|
|
$currencyRepos = app(CurrencyRepositoryInterface::class); |
56
|
|
|
|
57
|
|
|
// get all currencies: |
58
|
|
|
$list = $currencyRepos->get(); |
59
|
|
|
$array = []; |
60
|
|
|
/** @var TransactionCurrency $currency */ |
61
|
|
|
foreach ($list as $currency) { |
62
|
|
|
$array[$currency->id] = $currency->name . ' (' . $currency->symbol . ')'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->select($name, $array, $value, $options); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* TODO cleanup and describe. |
70
|
|
|
* |
71
|
|
|
* @param string $name |
72
|
|
|
* @param mixed $value |
73
|
|
|
* @param array $options |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function currencyListEmpty(string $name, $value = null, array $options = null): string |
78
|
|
|
{ |
79
|
|
|
/** @var CurrencyRepositoryInterface $currencyRepos */ |
80
|
|
|
$currencyRepos = app(CurrencyRepositoryInterface::class); |
81
|
|
|
|
82
|
|
|
// get all currencies: |
83
|
|
|
$list = $currencyRepos->get(); |
84
|
|
|
$array = [ |
85
|
|
|
0 => (string)trans('firefly.no_currency'), |
86
|
|
|
]; |
87
|
|
|
/** @var TransactionCurrency $currency */ |
88
|
|
|
foreach ($list as $currency) { |
89
|
|
|
$array[$currency->id] = $currency->name . ' (' . $currency->symbol . ')'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->select($name, $array, $value, $options); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* TODO describe and cleanup. |
98
|
|
|
* |
99
|
|
|
* @param string $name |
100
|
|
|
* @param mixed $value |
101
|
|
|
* @param array $options |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
* @throws FireflyException |
105
|
|
|
*/ |
106
|
|
|
public function balanceAll(string $name, $value = null, array $options = null): string |
107
|
|
|
{ |
108
|
|
|
return $this->allCurrencyField($name, 'balance', $value, $options); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* TODO cleanup and describe better. |
113
|
|
|
* |
114
|
|
|
* @param string $name |
115
|
|
|
* @param string $view |
116
|
|
|
* @param mixed $value |
117
|
|
|
* @param array $options |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
121
|
|
|
*/ |
122
|
|
|
protected function allCurrencyField(string $name, string $view, $value = null, array $options = null): string |
123
|
|
|
{ |
124
|
|
|
$label = $this->label($name, $options); |
125
|
|
|
$options = $this->expandOptionArray($name, $label, $options); |
126
|
|
|
$classes = $this->getHolderClasses($name); |
127
|
|
|
$value = $this->fillFieldValue($name, $value); |
128
|
|
|
$options['step'] = 'any'; |
129
|
|
|
$defaultCurrency = $options['currency'] ?? Amt::getDefaultCurrency(); |
|
|
|
|
130
|
|
|
/** @var Collection $currencies */ |
131
|
|
|
$currencies = app('amount')->getAllCurrencies(); |
132
|
|
|
unset($options['currency'], $options['placeholder']); |
133
|
|
|
|
134
|
|
|
// perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount) |
135
|
|
|
$preFilled = session('preFilled'); |
136
|
|
|
$key = 'amount_currency_id_' . $name; |
137
|
|
|
$sentCurrencyId = isset($preFilled[$key]) ? (int)$preFilled[$key] : $defaultCurrency->id; |
138
|
|
|
|
139
|
|
|
Log::debug(sprintf('Sent currency ID is %d', $sentCurrencyId)); |
140
|
|
|
|
141
|
|
|
// find this currency in set of currencies: |
142
|
|
|
foreach ($currencies as $currency) { |
143
|
|
|
if ($currency->id === $sentCurrencyId) { |
144
|
|
|
$defaultCurrency = $currency; |
145
|
|
|
Log::debug(sprintf('default currency is now %s', $defaultCurrency->code)); |
146
|
|
|
break; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// make sure value is formatted nicely: |
151
|
|
|
if (null !== $value && '' !== $value) { |
152
|
|
|
$value = round($value, $defaultCurrency->decimal_places); |
153
|
|
|
} |
154
|
|
|
try { |
155
|
|
|
$html = view('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render(); |
156
|
|
|
} catch (Throwable $e) { |
157
|
|
|
Log::debug(sprintf('Could not render currencyField(): %s', $e->getMessage())); |
158
|
|
|
$html = 'Could not render currencyField.'; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $html; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $name |
166
|
|
|
* @param string $view |
167
|
|
|
* @param mixed $value |
168
|
|
|
* @param array $options |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
172
|
|
|
*/ |
173
|
|
|
protected function currencyField(string $name, string $view, $value = null, array $options = null): string |
174
|
|
|
{ |
175
|
|
|
$label = $this->label($name, $options); |
176
|
|
|
$options = $this->expandOptionArray($name, $label, $options); |
177
|
|
|
$classes = $this->getHolderClasses($name); |
178
|
|
|
$value = $this->fillFieldValue($name, $value); |
179
|
|
|
$options['step'] = 'any'; |
180
|
|
|
$defaultCurrency = $options['currency'] ?? Amt::getDefaultCurrency(); |
|
|
|
|
181
|
|
|
/** @var Collection $currencies */ |
182
|
|
|
$currencies = app('amount')->getCurrencies(); |
183
|
|
|
unset($options['currency'], $options['placeholder']); |
184
|
|
|
|
185
|
|
|
// perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount) |
186
|
|
|
$preFilled = session('preFilled'); |
187
|
|
|
$key = 'amount_currency_id_' . $name; |
188
|
|
|
$sentCurrencyId = isset($preFilled[$key]) ? (int)$preFilled[$key] : $defaultCurrency->id; |
189
|
|
|
|
190
|
|
|
Log::debug(sprintf('Sent currency ID is %d', $sentCurrencyId)); |
191
|
|
|
|
192
|
|
|
// find this currency in set of currencies: |
193
|
|
|
foreach ($currencies as $currency) { |
194
|
|
|
if ($currency->id === $sentCurrencyId) { |
195
|
|
|
$defaultCurrency = $currency; |
196
|
|
|
Log::debug(sprintf('default currency is now %s', $defaultCurrency->code)); |
197
|
|
|
break; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// make sure value is formatted nicely: |
202
|
|
|
if (null !== $value && '' !== $value) { |
203
|
|
|
$value = round($value, $defaultCurrency->decimal_places); |
204
|
|
|
} |
205
|
|
|
try { |
206
|
|
|
$html = view('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render(); |
207
|
|
|
} catch (Throwable $e) { |
208
|
|
|
Log::debug(sprintf('Could not render currencyField(): %s', $e->getMessage())); |
209
|
|
|
$html = 'Could not render currencyField.'; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $html; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param string $name |
217
|
|
|
* @param mixed $value |
218
|
|
|
* @param array $options |
219
|
|
|
* |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
|
|
public function amount(string $name, $value = null, array $options = null): string |
223
|
|
|
{ |
224
|
|
|
return $this->currencyField($name, 'amount', $value, $options); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
} |