| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2019-04-12 04:53:18 +02:00
										 |  |  |  * RecurrenceUpdateRequest.php | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * Copyright (c) 2019 thegrumpydictator@gmail.com | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Api\V1\Requests; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-22 17:56:48 +02:00
										 |  |  | use FireflyIII\Models\Recurrence; | 
					
						
							|  |  |  | use FireflyIII\Rules\BelongsUser; | 
					
						
							|  |  |  | use FireflyIII\Rules\IsBoolean; | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  | use FireflyIII\Validation\RecurrenceValidation; | 
					
						
							|  |  |  | use FireflyIII\Validation\TransactionValidation; | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  | use Illuminate\Validation\Validator; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2019-04-12 04:53:18 +02:00
										 |  |  |  * Class RecurrenceUpdateRequest | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-04-12 04:53:18 +02:00
										 |  |  | class RecurrenceUpdateRequest extends Request | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |     use RecurrenceValidation, TransactionValidation; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * Authorize logged in users. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function authorize(): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Only allow authenticated users
 | 
					
						
							|  |  |  |         return auth()->check(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-26 19:09:55 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get all data from the request. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAll(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $active     = null; | 
					
						
							|  |  |  |         $applyRules = null; | 
					
						
							|  |  |  |         if (null !== $this->get('active')) { | 
					
						
							|  |  |  |             $active = $this->boolean('active'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (null !== $this->get('apply_rules')) { | 
					
						
							|  |  |  |             $applyRules = $this->boolean('apply_rules'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $return = [ | 
					
						
							|  |  |  |             'recurrence'   => [ | 
					
						
							|  |  |  |                 'type'              => $this->nullableString('type'), | 
					
						
							|  |  |  |                 'title'             => $this->nullableString('title'), | 
					
						
							|  |  |  |                 'description'       => $this->nullableString('description'), | 
					
						
							|  |  |  |                 'first_date'        => $this->date('first_date'), | 
					
						
							| 
									
										
										
										
											2019-09-22 17:36:18 +02:00
										 |  |  |                 'notes'             => $this->nullableNlString('notes'), | 
					
						
							| 
									
										
										
										
											2019-08-26 19:09:55 +02:00
										 |  |  |                 'repeat_until'      => $this->date('repeat_until'), | 
					
						
							|  |  |  |                 'nr_of_repetitions' => $this->nullableInteger('nr_of_repetitions'), | 
					
						
							|  |  |  |                 'apply_rules'       => $applyRules, | 
					
						
							|  |  |  |                 'active'            => $active, | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             'transactions' => $this->getTransactionData(), | 
					
						
							|  |  |  |             'repetitions'  => $this->getRepetitionData(), | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * The rules that the incoming request must be matched against. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function rules(): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-22 17:56:48 +02:00
										 |  |  |         /** @var Recurrence $recurrence */ | 
					
						
							|  |  |  |         $recurrence = $this->route()->parameter('recurrence'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2019-09-04 17:39:39 +02:00
										 |  |  |             'type'                  => 'in:withdrawal,transfer,deposit', | 
					
						
							|  |  |  |             'title'                 => sprintf('between:1,255|uniqueObjectForUser:recurrences,title,%d', $recurrence->id), | 
					
						
							|  |  |  |             'description'           => 'between:1,65000', | 
					
						
							|  |  |  |             'first_date'            => 'date', | 
					
						
							|  |  |  |             'apply_rules'           => [new IsBoolean], | 
					
						
							|  |  |  |             'active'                => [new IsBoolean], | 
					
						
							|  |  |  |             'repeat_until'          => 'date', | 
					
						
							|  |  |  |             'nr_of_repetitions'     => 'numeric|between:1,31', | 
					
						
							|  |  |  |             'repetitions.*.type'    => 'in:daily,weekly,ndom,monthly,yearly', | 
					
						
							|  |  |  |             'repetitions.*.moment'  => 'between:0,10', | 
					
						
							|  |  |  |             'repetitions.*.skip'    => 'required|numeric|between:0,31', | 
					
						
							|  |  |  |             'repetitions.*.weekend' => 'required|numeric|min:1|max:4', | 
					
						
							| 
									
										
										
										
											2019-08-27 05:57:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-22 17:56:48 +02:00
										 |  |  |             'transactions.*.description'           => 'required|between:1,255', | 
					
						
							|  |  |  |             'transactions.*.amount'                => 'required|numeric|more:0', | 
					
						
							|  |  |  |             'transactions.*.foreign_amount'        => 'numeric|more:0', | 
					
						
							|  |  |  |             'transactions.*.currency_id'           => 'numeric|exists:transaction_currencies,id', | 
					
						
							|  |  |  |             'transactions.*.currency_code'         => 'min:3|max:3|exists:transaction_currencies,code', | 
					
						
							|  |  |  |             'transactions.*.foreign_currency_id'   => 'numeric|exists:transaction_currencies,id', | 
					
						
							|  |  |  |             'transactions.*.foreign_currency_code' => 'min:3|max:3|exists:transaction_currencies,code', | 
					
						
							|  |  |  |             'transactions.*.source_id'             => ['numeric', 'nullable', new BelongsUser], | 
					
						
							|  |  |  |             'transactions.*.source_name'           => 'between:1,255|nullable', | 
					
						
							|  |  |  |             'transactions.*.destination_id'        => ['numeric', 'nullable', new BelongsUser], | 
					
						
							|  |  |  |             'transactions.*.destination_name'      => 'between:1,255|nullable', | 
					
						
							| 
									
										
										
										
											2019-08-26 18:15:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // new and updated fields:
 | 
					
						
							|  |  |  |             'transactions.*.budget_id'             => ['mustExist:budgets,id', new BelongsUser], | 
					
						
							| 
									
										
										
										
											2019-08-26 19:09:55 +02:00
										 |  |  |             'transactions.*.budget_name'           => ['between:1,255', 'nullable', new BelongsUser], | 
					
						
							| 
									
										
										
										
											2019-08-26 18:15:21 +02:00
										 |  |  |             'transactions.*.category_id'           => ['mustExist:categories,id', new BelongsUser], | 
					
						
							|  |  |  |             'transactions.*.category_name'         => 'between:1,255|nullable', | 
					
						
							|  |  |  |             'transactions.*.piggy_bank_id'         => ['numeric', 'mustExist:piggy_banks,id', new BelongsUser], | 
					
						
							| 
									
										
										
										
											2019-08-26 19:09:55 +02:00
										 |  |  |             'transactions.*.piggy_bank_name'       => ['between:1,255', 'nullable', new BelongsUser], | 
					
						
							|  |  |  |             'transactions.*.tags'                  => 'between:1,64000', | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-22 17:56:48 +02:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Configure the validator instance. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-05-30 12:39:06 +02:00
										 |  |  |      * @param Validator $validator | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function withValidator(Validator $validator): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $validator->after( | 
					
						
							|  |  |  |             function (Validator $validator) { | 
					
						
							| 
									
										
										
										
											2019-08-26 19:09:55 +02:00
										 |  |  |                 $this->validateOneRecurrenceTransactionUpdate($validator); | 
					
						
							|  |  |  |                 $this->validateOneRepetitionUpdate($validator); | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |                 $this->validateRecurrenceRepetition($validator); | 
					
						
							|  |  |  |                 $this->validateRepetitionMoment($validator); | 
					
						
							|  |  |  |                 $this->validateForeignCurrencyInformation($validator); | 
					
						
							| 
									
										
										
										
											2019-08-27 05:57:09 +02:00
										 |  |  |                 $this->valUpdateAccountInfo($validator); | 
					
						
							| 
									
										
										
										
											2018-06-29 19:27:07 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-26 19:09:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns the repetition data as it is found in the submitted data. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array|null | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function getRepetitionData(): ?array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $return = []; | 
					
						
							|  |  |  |         // repetition data:
 | 
					
						
							|  |  |  |         /** @var array $repetitions */ | 
					
						
							|  |  |  |         $repetitions = $this->get('repetitions'); | 
					
						
							|  |  |  |         if (null === $repetitions) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         /** @var array $repetition */ | 
					
						
							|  |  |  |         foreach ($repetitions as $repetition) { | 
					
						
							|  |  |  |             $return[] = [ | 
					
						
							|  |  |  |                 'type'    => $repetition['type'], | 
					
						
							|  |  |  |                 'moment'  => $repetition['moment'], | 
					
						
							|  |  |  |                 'skip'    => (int)$repetition['skip'], | 
					
						
							|  |  |  |                 'weekend' => (int)$repetition['weekend'], | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns the transaction data as it is found in the submitted data. It's a complex method according to code | 
					
						
							|  |  |  |      * standards but it just has a lot of ??-statements because of the fields that may or may not exist. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array|null | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function getTransactionData(): ?array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $return = []; | 
					
						
							|  |  |  |         // transaction data:
 | 
					
						
							|  |  |  |         /** @var array $transactions */ | 
					
						
							|  |  |  |         $transactions = $this->get('transactions'); | 
					
						
							|  |  |  |         if (null === $transactions) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         /** @var array $transaction */ | 
					
						
							|  |  |  |         foreach ($transactions as $transaction) { | 
					
						
							|  |  |  |             $return[] = [ | 
					
						
							|  |  |  |                 'amount'                => $transaction['amount'], | 
					
						
							|  |  |  |                 'currency_id'           => isset($transaction['currency_id']) ? (int)$transaction['currency_id'] : null, | 
					
						
							|  |  |  |                 'currency_code'         => $transaction['currency_code'] ?? null, | 
					
						
							|  |  |  |                 'foreign_amount'        => $transaction['foreign_amount'] ?? null, | 
					
						
							|  |  |  |                 'foreign_currency_id'   => isset($transaction['foreign_currency_id']) ? (int)$transaction['foreign_currency_id'] : null, | 
					
						
							|  |  |  |                 'foreign_currency_code' => $transaction['foreign_currency_code'] ?? null, | 
					
						
							|  |  |  |                 'source_id'             => isset($transaction['source_id']) ? (int)$transaction['source_id'] : null, | 
					
						
							|  |  |  |                 'source_name'           => isset($transaction['source_name']) ? (string)$transaction['source_name'] : null, | 
					
						
							|  |  |  |                 'destination_id'        => isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : null, | 
					
						
							|  |  |  |                 'destination_name'      => isset($transaction['destination_name']) ? (string)$transaction['destination_name'] : null, | 
					
						
							|  |  |  |                 'description'           => $transaction['description'], | 
					
						
							|  |  |  |                 'type'                  => $this->string('type'), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // new and updated fields:
 | 
					
						
							|  |  |  |                 'piggy_bank_id'         => isset($transaction['piggy_bank_id']) ? (int)$transaction['piggy_bank_id'] : null, | 
					
						
							|  |  |  |                 'piggy_bank_name'       => $transaction['piggy_bank_name'] ?? null, | 
					
						
							| 
									
										
										
										
											2019-08-27 06:08:30 +02:00
										 |  |  |                 'tags'                  => $transaction['tags'] ?? [], | 
					
						
							| 
									
										
										
										
											2019-08-26 19:09:55 +02:00
										 |  |  |                 'budget_id'             => isset($transaction['budget_id']) ? (int)$transaction['budget_id'] : null, | 
					
						
							|  |  |  |                 'budget_name'           => $transaction['budget_name'] ?? null, | 
					
						
							|  |  |  |                 'category_id'           => isset($transaction['category_id']) ? (int)$transaction['category_id'] : null, | 
					
						
							|  |  |  |                 'category_name'         => $transaction['category_name'] ?? null, | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |