1 | <?php |
||
2 | |||
3 | /** |
||
4 | * TransactionStoreRequest.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\Requests; |
||
26 | |||
27 | use FireflyIII\Rules\BelongsUser; |
||
28 | use FireflyIII\Rules\IsBoolean; |
||
29 | use FireflyIII\Rules\IsDateOrTime; |
||
30 | use FireflyIII\Support\NullArrayObject; |
||
31 | use FireflyIII\Validation\CurrencyValidation; |
||
32 | use FireflyIII\Validation\GroupValidation; |
||
33 | use FireflyIII\Validation\TransactionValidation; |
||
34 | use Illuminate\Validation\Validator; |
||
35 | use Log; |
||
36 | |||
37 | /** |
||
38 | * Class TransactionStoreRequest |
||
39 | */ |
||
40 | class TransactionStoreRequest extends Request |
||
41 | { |
||
42 | use TransactionValidation, GroupValidation, CurrencyValidation; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
43 | |||
44 | /** |
||
45 | * Authorize logged in users. |
||
46 | * |
||
47 | * @return bool |
||
48 | */ |
||
49 | public function authorize(): bool |
||
50 | { |
||
51 | Log::debug('Authorize TransactionStoreRequest'); |
||
52 | |||
53 | // Only allow authenticated users |
||
54 | return auth()->check(); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Get all data. Is pretty complex because of all the ??-statements. |
||
59 | * |
||
60 | * @return array |
||
61 | */ |
||
62 | public function getAll(): array |
||
63 | { |
||
64 | Log::debug('get all data in TransactionStoreRequest'); |
||
65 | |||
66 | return [ |
||
67 | 'group_title' => $this->string('group_title'), |
||
68 | 'error_if_duplicate_hash' => $this->boolean('error_if_duplicate_hash'), |
||
69 | 'apply_rules' => $this->boolean('apply_rules', true), |
||
70 | 'transactions' => $this->getTransactionData(), |
||
71 | ]; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * The rules that the incoming request must be matched against. |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | public function rules(): array |
||
80 | { |
||
81 | Log::debug('Collect rules of TransactionStoreRequest'); |
||
82 | |||
83 | return [ |
||
84 | // basic fields for group: |
||
85 | 'group_title' => 'between:1,1000|nullable', |
||
86 | 'error_if_duplicate_hash' => [new IsBoolean], |
||
87 | 'apply_rules' => [new IsBoolean], |
||
88 | |||
89 | // transaction rules (in array for splits): |
||
90 | 'transactions.*.type' => 'required|in:withdrawal,deposit,transfer,opening-balance,reconciliation', |
||
91 | 'transactions.*.date' => ['required', new IsDateOrTime], |
||
92 | 'transactions.*.order' => 'numeric|min:0', |
||
93 | |||
94 | // currency info |
||
95 | 'transactions.*.currency_id' => 'numeric|exists:transaction_currencies,id|nullable', |
||
96 | 'transactions.*.currency_code' => 'min:3|max:3|exists:transaction_currencies,code|nullable', |
||
97 | 'transactions.*.foreign_currency_id' => 'numeric|exists:transaction_currencies,id|nullable', |
||
98 | 'transactions.*.foreign_currency_code' => 'min:3|max:3|exists:transaction_currencies,code|nullable', |
||
99 | |||
100 | // amount |
||
101 | 'transactions.*.amount' => 'required|numeric|more:0', |
||
102 | 'transactions.*.foreign_amount' => 'numeric', |
||
103 | |||
104 | // description |
||
105 | 'transactions.*.description' => 'nullable|between:1,1000', |
||
106 | |||
107 | // source of transaction |
||
108 | 'transactions.*.source_id' => ['numeric', 'nullable', new BelongsUser], |
||
109 | 'transactions.*.source_name' => 'between:1,255|nullable', |
||
110 | 'transactions.*.source_iban' => 'between:1,255|nullable|iban', |
||
111 | 'transactions.*.source_number' => 'between:1,255|nullable', |
||
112 | 'transactions.*.source_bic' => 'between:1,255|nullable|bic', |
||
113 | |||
114 | // destination of transaction |
||
115 | 'transactions.*.destination_id' => ['numeric', 'nullable', new BelongsUser], |
||
116 | 'transactions.*.destination_name' => 'between:1,255|nullable', |
||
117 | 'transactions.*.destination_iban' => 'between:1,255|nullable|iban', |
||
118 | 'transactions.*.destination_number' => 'between:1,255|nullable', |
||
119 | 'transactions.*.destination_bic' => 'between:1,255|nullable|bic', |
||
120 | |||
121 | // budget, category, bill and piggy |
||
122 | 'transactions.*.budget_id' => ['mustExist:budgets,id', new BelongsUser], |
||
123 | 'transactions.*.budget_name' => ['between:1,255', 'nullable', new BelongsUser], |
||
124 | 'transactions.*.category_id' => ['mustExist:categories,id', new BelongsUser], |
||
125 | 'transactions.*.category_name' => 'between:1,255|nullable', |
||
126 | 'transactions.*.bill_id' => ['numeric', 'nullable', 'mustExist:bills,id', new BelongsUser], |
||
127 | 'transactions.*.bill_name' => ['between:1,255', 'nullable', new BelongsUser], |
||
128 | 'transactions.*.piggy_bank_id' => ['numeric', 'nullable', 'mustExist:piggy_banks,id', new BelongsUser], |
||
129 | 'transactions.*.piggy_bank_name' => ['between:1,255', 'nullable', new BelongsUser], |
||
130 | |||
131 | // other interesting fields |
||
132 | 'transactions.*.reconciled' => [new IsBoolean], |
||
133 | 'transactions.*.notes' => 'min:1,max:50000|nullable', |
||
134 | 'transactions.*.tags' => 'between:0,255', |
||
135 | |||
136 | // meta info fields |
||
137 | 'transactions.*.internal_reference' => 'min:1,max:255|nullable', |
||
138 | 'transactions.*.external_id' => 'min:1,max:255|nullable', |
||
139 | 'transactions.*.recurrence_id' => 'min:1,max:255|nullable', |
||
140 | 'transactions.*.bunq_payment_id' => 'min:1,max:255|nullable', |
||
141 | |||
142 | // SEPA fields: |
||
143 | 'transactions.*.sepa_cc' => 'min:1,max:255|nullable', |
||
144 | 'transactions.*.sepa_ct_op' => 'min:1,max:255|nullable', |
||
145 | 'transactions.*.sepa_ct_id' => 'min:1,max:255|nullable', |
||
146 | 'transactions.*.sepa_db' => 'min:1,max:255|nullable', |
||
147 | 'transactions.*.sepa_country' => 'min:1,max:255|nullable', |
||
148 | 'transactions.*.sepa_ep' => 'min:1,max:255|nullable', |
||
149 | 'transactions.*.sepa_ci' => 'min:1,max:255|nullable', |
||
150 | 'transactions.*.sepa_batch_id' => 'min:1,max:255|nullable', |
||
151 | |||
152 | // dates |
||
153 | 'transactions.*.interest_date' => 'date|nullable', |
||
154 | 'transactions.*.book_date' => 'date|nullable', |
||
155 | 'transactions.*.process_date' => 'date|nullable', |
||
156 | 'transactions.*.due_date' => 'date|nullable', |
||
157 | 'transactions.*.payment_date' => 'date|nullable', |
||
158 | 'transactions.*.invoice_date' => 'date|nullable', |
||
159 | ]; |
||
160 | |||
161 | |||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Configure the validator instance. |
||
166 | * |
||
167 | * @param Validator $validator |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | public function withValidator(Validator $validator): void |
||
172 | { |
||
173 | $validator->after( |
||
174 | function (Validator $validator) { |
||
175 | // must submit at least one transaction. |
||
176 | $this->validateOneTransaction($validator); |
||
177 | |||
178 | // all journals must have a description |
||
179 | $this->validateDescriptions($validator); |
||
180 | |||
181 | // all transaction types must be equal: |
||
182 | $this->validateTransactionTypes($validator); |
||
183 | |||
184 | // validate foreign currency info |
||
185 | $this->validateForeignCurrencyInformation($validator); |
||
186 | |||
187 | // validate all account info |
||
188 | $this->validateAccountInformation($validator); |
||
189 | |||
190 | // validate source/destination is equal, depending on the transaction journal type. |
||
191 | $this->validateEqualAccounts($validator); |
||
192 | |||
193 | // the group must have a description if > 1 journal. |
||
194 | $this->validateGroupDescription($validator); |
||
195 | |||
196 | } |
||
197 | ); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Get transaction data. |
||
202 | * |
||
203 | * @return array |
||
204 | */ |
||
205 | private function getTransactionData(): array |
||
206 | { |
||
207 | $return = []; |
||
208 | /** |
||
209 | * @var int $index |
||
210 | * @var array $transaction |
||
211 | */ |
||
212 | foreach ($this->get('transactions') as $index => $transaction) { |
||
213 | $object = new NullArrayObject($transaction); |
||
214 | $return[] = [ |
||
215 | 'type' => $this->stringFromValue($object['type']), |
||
216 | 'date' => $this->dateFromValue($object['date']), |
||
217 | 'order' => $this->integerFromValue((string) $object['order']), |
||
218 | |||
219 | 'currency_id' => $this->integerFromValue((string) $object['currency_id']), |
||
220 | 'currency_code' => $this->stringFromValue($object['currency_code']), |
||
221 | |||
222 | // foreign currency info: |
||
223 | 'foreign_currency_id' => $this->integerFromValue((string) $object['foreign_currency_id']), |
||
224 | 'foreign_currency_code' => $this->stringFromValue($object['foreign_currency_code']), |
||
225 | |||
226 | // amount and foreign amount. Cannot be 0. |
||
227 | 'amount' => $this->stringFromValue((string) $object['amount']), |
||
228 | 'foreign_amount' => $this->stringFromValue((string) $object['foreign_amount']), |
||
229 | |||
230 | // description. |
||
231 | 'description' => $this->stringFromValue($object['description']), |
||
232 | |||
233 | // source of transaction. If everything is null, assume cash account. |
||
234 | 'source_id' => $this->integerFromValue((string) $object['source_id']), |
||
235 | 'source_name' => $this->stringFromValue($object['source_name']), |
||
236 | 'source_iban' => $this->stringFromValue($object['source_iban']), |
||
237 | 'source_number' => $this->stringFromValue($object['source_number']), |
||
238 | 'source_bic' => $this->stringFromValue($object['source_bic']), |
||
239 | |||
240 | // destination of transaction. If everything is null, assume cash account. |
||
241 | 'destination_id' => $this->integerFromValue((string) $object['destination_id']), |
||
242 | 'destination_name' => $this->stringFromValue($object['destination_name']), |
||
243 | 'destination_iban' => $this->stringFromValue($object['destination_iban']), |
||
244 | 'destination_number' => $this->stringFromValue($object['destination_number']), |
||
245 | 'destination_bic' => $this->stringFromValue($object['destination_bic']), |
||
246 | |||
247 | // budget info |
||
248 | 'budget_id' => $this->integerFromValue((string) $object['budget_id']), |
||
249 | 'budget_name' => $this->stringFromValue($object['budget_name']), |
||
250 | |||
251 | // category info |
||
252 | 'category_id' => $this->integerFromValue((string) $object['category_id']), |
||
253 | 'category_name' => $this->stringFromValue($object['category_name']), |
||
254 | |||
255 | // journal bill reference. Optional. Will only work for withdrawals |
||
256 | 'bill_id' => $this->integerFromValue((string) $object['bill_id']), |
||
257 | 'bill_name' => $this->stringFromValue($object['bill_name']), |
||
258 | |||
259 | // piggy bank reference. Optional. Will only work for transfers |
||
260 | 'piggy_bank_id' => $this->integerFromValue((string) $object['piggy_bank_id']), |
||
261 | 'piggy_bank_name' => $this->stringFromValue($object['piggy_bank_name']), |
||
262 | |||
263 | // some other interesting properties |
||
264 | 'reconciled' => $this->convertBoolean((string) $object['reconciled']), |
||
265 | 'notes' => $this->nlStringFromValue($object['notes']), |
||
266 | 'tags' => $this->arrayFromValue($object['tags']), |
||
267 | |||
268 | // all custom fields: |
||
269 | 'internal_reference' => $this->stringFromValue((string) $object['internal_reference']), |
||
270 | 'external_id' => $this->stringFromValue((string) $object['external_id']), |
||
271 | 'original_source' => sprintf('ff3-v%s|api-v%s', config('firefly.version'), config('firefly.api_version')), |
||
272 | 'recurrence_id' => $this->integerFromValue($object['recurrence_id']), |
||
273 | 'bunq_payment_id' => $this->stringFromValue((string) $object['bunq_payment_id']), |
||
274 | |||
275 | 'sepa_cc' => $this->stringFromValue($object['sepa_cc']), |
||
276 | 'sepa_ct_op' => $this->stringFromValue($object['sepa_ct_op']), |
||
277 | 'sepa_ct_id' => $this->stringFromValue($object['sepa_ct_id']), |
||
278 | 'sepa_db' => $this->stringFromValue($object['sepa_db']), |
||
279 | 'sepa_country' => $this->stringFromValue($object['sepa_country']), |
||
280 | 'sepa_ep' => $this->stringFromValue($object['sepa_ep']), |
||
281 | 'sepa_ci' => $this->stringFromValue($object['sepa_ci']), |
||
282 | 'sepa_batch_id' => $this->stringFromValue($object['sepa_batch_id']), |
||
283 | |||
284 | |||
285 | // custom date fields. Must be Carbon objects. Presence is optional. |
||
286 | 'interest_date' => $this->dateFromValue($object['interest_date']), |
||
287 | 'book_date' => $this->dateFromValue($object['book_date']), |
||
288 | 'process_date' => $this->dateFromValue($object['process_date']), |
||
289 | 'due_date' => $this->dateFromValue($object['due_date']), |
||
290 | 'payment_date' => $this->dateFromValue($object['payment_date']), |
||
291 | 'invoice_date' => $this->dateFromValue($object['invoice_date']), |
||
292 | |||
293 | ]; |
||
294 | } |
||
295 | |||
296 | return $return; |
||
297 | } |
||
298 | } |
||
299 |