| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * RecurrenceFormRequest.php | 
					
						
							| 
									
										
										
										
											2020-01-31 07:32:04 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +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-16 21:47:51 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +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-16 21:47:51 +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-16 21:47:51 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Http\Requests; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2021-03-15 08:51:21 +01:00
										 |  |  | use FireflyIII\Factory\CategoryFactory; | 
					
						
							| 
									
										
										
										
											2018-06-21 18:57:51 +02:00
										 |  |  | use FireflyIII\Models\Recurrence; | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							|  |  |  | use FireflyIII\Rules\ValidRecurrenceRepetitionType; | 
					
						
							| 
									
										
										
										
											2018-06-17 15:14:34 +02:00
										 |  |  | use FireflyIII\Rules\ValidRecurrenceRepetitionValue; | 
					
						
							| 
									
										
										
										
											2020-10-24 07:55:09 +02:00
										 |  |  | use FireflyIII\Support\Request\ChecksLogin; | 
					
						
							| 
									
										
										
										
											2020-07-18 08:42:13 +02:00
										 |  |  | use FireflyIII\Support\Request\ConvertsDataTypes; | 
					
						
							| 
									
										
										
										
											2019-07-01 20:22:35 +02:00
										 |  |  | use FireflyIII\Validation\AccountValidator; | 
					
						
							| 
									
										
										
										
											2020-10-24 07:55:09 +02:00
										 |  |  | use Illuminate\Foundation\Http\FormRequest; | 
					
						
							| 
									
										
										
										
											2023-04-01 07:04:42 +02:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  | use Illuminate\Validation\Validator; | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class RecurrenceFormRequest | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-10-24 07:55:09 +02:00
										 |  |  | class RecurrenceFormRequest extends FormRequest | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:28 +01:00
										 |  |  |     use ConvertsDataTypes; | 
					
						
							|  |  |  |     use ChecksLogin; | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-22 08:10:16 +02:00
										 |  |  |      * Get the data required by the controller. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-10-24 07:55:09 +02:00
										 |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-07-20 14:34:56 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function getAll(): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-06-17 15:14:34 +02:00
										 |  |  |         $repetitionData = $this->parseRepetitionData(); | 
					
						
							|  |  |  |         $return         = [ | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |             'recurrence'   => [ | 
					
						
							| 
									
										
										
										
											2022-05-02 19:35:35 +02:00
										 |  |  |                 'type'              => $this->convertString('transaction_type'), | 
					
						
							|  |  |  |                 'title'             => $this->convertString('title'), | 
					
						
							|  |  |  |                 'description'       => $this->convertString('recurring_description'), | 
					
						
							| 
									
										
										
										
											2021-12-28 20:42:50 +01:00
										 |  |  |                 'first_date'        => $this->getCarbonDate('first_date'), | 
					
						
							|  |  |  |                 'repeat_until'      => $this->getCarbonDate('repeat_until'), | 
					
						
							| 
									
										
										
										
											2022-09-30 20:07:01 +02:00
										 |  |  |                 'nr_of_repetitions' => $this->convertInteger('repetitions'), | 
					
						
							| 
									
										
										
										
											2021-04-04 12:48:44 +02:00
										 |  |  |                 'apply_rules'       => $this->boolean('apply_rules'), | 
					
						
							|  |  |  |                 'active'            => $this->boolean('active'), | 
					
						
							| 
									
										
										
										
											2022-05-02 19:35:35 +02:00
										 |  |  |                 'repetition_end'    => $this->convertString('repetition_end'), | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |             ], | 
					
						
							|  |  |  |             'transactions' => [ | 
					
						
							|  |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2022-09-30 20:07:01 +02:00
										 |  |  |                     'currency_id'           => $this->convertInteger('transaction_currency_id'), | 
					
						
							| 
									
										
										
										
											2018-06-30 06:14:39 +02:00
										 |  |  |                     'currency_code'         => null, | 
					
						
							| 
									
										
										
										
											2022-05-02 19:35:35 +02:00
										 |  |  |                     'type'                  => $this->convertString('transaction_type'), | 
					
						
							|  |  |  |                     'description'           => $this->convertString('transaction_description'), | 
					
						
							|  |  |  |                     'amount'                => $this->convertString('amount'), | 
					
						
							| 
									
										
										
										
											2018-06-30 06:14:39 +02:00
										 |  |  |                     'foreign_amount'        => null, | 
					
						
							|  |  |  |                     'foreign_currency_id'   => null, | 
					
						
							|  |  |  |                     'foreign_currency_code' => null, | 
					
						
							| 
									
										
										
										
											2022-09-30 20:07:01 +02:00
										 |  |  |                     'budget_id'             => $this->convertInteger('budget_id'), | 
					
						
							| 
									
										
										
										
											2018-06-30 06:14:39 +02:00
										 |  |  |                     'budget_name'           => null, | 
					
						
							| 
									
										
										
										
											2022-09-30 20:07:01 +02:00
										 |  |  |                     'bill_id'               => $this->convertInteger('bill_id'), | 
					
						
							| 
									
										
										
										
											2021-07-18 14:51:30 +02:00
										 |  |  |                     'bill_name'             => null, | 
					
						
							| 
									
										
										
										
											2018-06-30 06:14:39 +02:00
										 |  |  |                     'category_id'           => null, | 
					
						
							| 
									
										
										
										
											2022-05-02 19:35:35 +02:00
										 |  |  |                     'category_name'         => $this->convertString('category'), | 
					
						
							|  |  |  |                     'tags'                  => '' !== $this->convertString('tags') ? explode(',', $this->convertString('tags')) : [], | 
					
						
							| 
									
										
										
										
											2022-09-30 20:07:01 +02:00
										 |  |  |                     'piggy_bank_id'         => $this->convertInteger('piggy_bank_id'), | 
					
						
							| 
									
										
										
										
											2019-08-27 06:36:16 +02:00
										 |  |  |                     'piggy_bank_name'       => null, | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             'repetitions'  => [ | 
					
						
							|  |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2018-06-27 05:37:56 +02:00
										 |  |  |                     'type'    => $repetitionData['type'], | 
					
						
							|  |  |  |                     'moment'  => $repetitionData['moment'], | 
					
						
							| 
									
										
										
										
											2022-09-30 20:07:01 +02:00
										 |  |  |                     'skip'    => $this->convertInteger('skip'), | 
					
						
							|  |  |  |                     'weekend' => $this->convertInteger('weekend'), | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // fill in foreign currency data
 | 
					
						
							| 
									
										
										
										
											2022-12-27 21:13:18 +01:00
										 |  |  |         if (null !== $this->convertFloat('foreign_amount')) { // intentional float, used because it defaults to null.
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:35:35 +02:00
										 |  |  |             $return['transactions'][0]['foreign_amount']      = $this->convertString('foreign_amount'); | 
					
						
							| 
									
										
										
										
											2022-09-30 20:07:01 +02:00
										 |  |  |             $return['transactions'][0]['foreign_currency_id'] = $this->convertInteger('foreign_currency_id'); | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-30 06:14:39 +02:00
										 |  |  |         // default values:
 | 
					
						
							|  |  |  |         $return['transactions'][0]['source_id']        = null; | 
					
						
							|  |  |  |         $return['transactions'][0]['source_name']      = null; | 
					
						
							|  |  |  |         $return['transactions'][0]['destination_id']   = null; | 
					
						
							|  |  |  |         $return['transactions'][0]['destination_name'] = null; | 
					
						
							| 
									
										
										
										
											2022-12-30 09:28:03 +01:00
										 |  |  |         $throwError                                    = true; | 
					
						
							|  |  |  |         $type                                          = $this->convertString('transaction_type'); | 
					
						
							|  |  |  |         if ('withdrawal' === $type) { | 
					
						
							|  |  |  |             $throwError                                  = false; | 
					
						
							|  |  |  |             $return['transactions'][0]['source_id']      = $this->convertInteger('source_id'); | 
					
						
							|  |  |  |             $return['transactions'][0]['destination_id'] = $this->convertInteger('withdrawal_destination_id'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ('deposit' === $type) { | 
					
						
							|  |  |  |             $throwError                                  = false; | 
					
						
							|  |  |  |             $return['transactions'][0]['source_id']      = $this->convertInteger('deposit_source_id'); | 
					
						
							|  |  |  |             $return['transactions'][0]['destination_id'] = $this->convertInteger('destination_id'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ('transfer' === $type) { | 
					
						
							|  |  |  |             $throwError                                  = false; | 
					
						
							|  |  |  |             $return['transactions'][0]['source_id']      = $this->convertInteger('source_id'); | 
					
						
							|  |  |  |             $return['transactions'][0]['destination_id'] = $this->convertInteger('destination_id'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (true === $throwError) { | 
					
						
							|  |  |  |             throw new FireflyException(sprintf('Cannot handle transaction type "%s"', $this->convertString('transaction_type'))); | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 08:51:21 +01:00
										 |  |  |         // replace category name with a new category:
 | 
					
						
							|  |  |  |         $factory = app(CategoryFactory::class); | 
					
						
							|  |  |  |         $factory->setUser(auth()->user()); | 
					
						
							| 
									
										
										
										
											2022-12-30 09:28:03 +01:00
										 |  |  |         /** | 
					
						
							|  |  |  |          * @var int $index | 
					
						
							|  |  |  |          * @var array $transaction | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         foreach ($return['transactions'] as $index => $transaction) { | 
					
						
							|  |  |  |             $categoryName = $transaction['category_name'] ?? null; | 
					
						
							|  |  |  |             if (null !== $categoryName) { | 
					
						
							| 
									
										
										
										
											2021-03-15 08:51:21 +01:00
										 |  |  |                 $category = $factory->findOrCreate(null, $categoryName); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |                 if (null !== $category) { | 
					
						
							| 
									
										
										
										
											2021-03-15 08:51:21 +01:00
										 |  |  |                     $return['transactions'][$index]['category_id'] = $category->id; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-22 08:10:16 +02:00
										 |  |  |      * The rules for this request. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-10-24 07:55:09 +02:00
										 |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2018-07-20 14:34:56 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function rules(): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-09-11 07:12:33 +02:00
										 |  |  |         $today    = today(config('app.timezone')); | 
					
						
							| 
									
										
										
										
											2023-02-11 07:36:45 +01:00
										 |  |  |         $tomorrow = today(config('app.timezone'))->addDay(); | 
					
						
							| 
									
										
										
										
											2018-08-06 19:14:30 +02:00
										 |  |  |         $rules    = [ | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |             // mandatory info for recurrence.
 | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |             'title'                   => 'required|between:1,255|uniqueObjectForUser:recurrences,title', | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             'first_date'              => 'required|date|after:'.$today->format('Y-m-d'), | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:28 +01:00
										 |  |  |             'repetition_type'         => ['required', new ValidRecurrenceRepetitionValue(), new ValidRecurrenceRepetitionType(), 'between:1,20'], | 
					
						
							| 
									
										
										
										
											2021-11-05 07:48:22 +01:00
										 |  |  |             'skip'                    => 'required|numeric|integer|gte:0|lte:31', | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // optional for recurrence:
 | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |             'recurring_description'   => 'between:0,65000', | 
					
						
							|  |  |  |             'active'                  => 'numeric|between:0,1', | 
					
						
							|  |  |  |             'apply_rules'             => 'numeric|between:0,1', | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // mandatory for transaction:
 | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |             'transaction_description' => 'required|between:1,255', | 
					
						
							|  |  |  |             'transaction_type'        => 'required|in:withdrawal,deposit,transfer', | 
					
						
							|  |  |  |             'transaction_currency_id' => 'required|exists:transaction_currencies,id', | 
					
						
							| 
									
										
										
										
											2020-07-06 06:55:27 +02:00
										 |  |  |             'amount'                  => 'numeric|required|gt:0|max:1000000000', | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |             // mandatory account info:
 | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |             'source_id'               => 'numeric|belongsToUser:accounts,id|nullable', | 
					
						
							|  |  |  |             'source_name'             => 'between:1,255|nullable', | 
					
						
							|  |  |  |             'destination_id'          => 'numeric|belongsToUser:accounts,id|nullable', | 
					
						
							|  |  |  |             'destination_name'        => 'between:1,255|nullable', | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // foreign amount data:
 | 
					
						
							| 
									
										
										
										
											2020-07-06 06:55:27 +02:00
										 |  |  |             'foreign_amount'          => 'nullable|gt:0|max:1000000000', | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // optional fields:
 | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |             'budget_id'               => 'mustExist:budgets,id|belongsToUser:budgets,id|nullable', | 
					
						
							| 
									
										
										
										
											2021-07-18 14:51:30 +02:00
										 |  |  |             'bill_id'                 => 'mustExist:bills,id|belongsToUser:bills,id|nullable', | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |             'category'                => 'between:1,255|nullable', | 
					
						
							|  |  |  |             'tags'                    => 'between:1,255|nullable', | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2022-09-30 20:07:01 +02:00
										 |  |  |         if ($this->convertInteger('foreign_currency_id') > 0) { | 
					
						
							| 
									
										
										
										
											2018-06-18 21:07:09 +02:00
										 |  |  |             $rules['foreign_currency_id'] = 'exists:transaction_currencies,id'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // if ends after X repetitions, set another rule
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:35:35 +02:00
										 |  |  |         if ('times' === $this->convertString('repetition_end')) { | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |             $rules['repetitions'] = 'required|numeric|between:0,254'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // if foreign amount, currency must be  different.
 | 
					
						
							| 
									
										
										
										
											2022-12-27 21:13:18 +01:00
										 |  |  |         if (null !== $this->convertFloat('foreign_amount')) { // intentional float, used because it defaults to null.
 | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |             $rules['foreign_currency_id'] = 'exists:transaction_currencies,id|different:transaction_currency_id'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // if ends at date X, set another rule.
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:35:35 +02:00
										 |  |  |         if ('until_date' === $this->convertString('repetition_end')) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             $rules['repeat_until'] = 'required|date|after:'.$tomorrow->format('Y-m-d'); | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-04 19:12:57 +02:00
										 |  |  |         // switch on type to expand rules for source and destination accounts:
 | 
					
						
							| 
									
										
										
										
											2022-12-30 09:28:03 +01:00
										 |  |  |         $type = strtolower($this->convertString('transaction_type')); | 
					
						
							|  |  |  |         if (strtolower(TransactionType::WITHDRAWAL) === $type) { | 
					
						
							|  |  |  |             $rules['source_id']        = 'required|exists:accounts,id|belongsToUser:accounts'; | 
					
						
							|  |  |  |             $rules['destination_name'] = 'between:1,255|nullable'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (strtolower(TransactionType::DEPOSIT) === $type) { | 
					
						
							|  |  |  |             $rules['source_name']    = 'between:1,255|nullable'; | 
					
						
							|  |  |  |             $rules['destination_id'] = 'required|exists:accounts,id|belongsToUser:accounts'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (strtolower(TransactionType::TRANSFER) === $type) { | 
					
						
							|  |  |  |             // this may not work:
 | 
					
						
							|  |  |  |             $rules['source_id']      = 'required|exists:accounts,id|belongsToUser:accounts|different:destination_id'; | 
					
						
							|  |  |  |             $rules['destination_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:source_id'; | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 18:57:51 +02:00
										 |  |  |         // update some rules in case the user is editing a post:
 | 
					
						
							|  |  |  |         /** @var Recurrence $recurrence */ | 
					
						
							|  |  |  |         $recurrence = $this->route()->parameter('recurrence'); | 
					
						
							|  |  |  |         if ($recurrence instanceof Recurrence) { | 
					
						
							|  |  |  |             $rules['id']         = 'required|numeric|exists:recurrences,id'; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             $rules['title']      = 'required|between:1,255|uniqueObjectForUser:recurrences,title,'.$recurrence->id; | 
					
						
							| 
									
										
										
										
											2018-06-21 18:57:51 +02:00
										 |  |  |             $rules['first_date'] = 'required|date'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-02-13 17:38:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-16 21:47:51 +02:00
										 |  |  |         return $rules; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-06-17 15:14:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-27 06:36:16 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Validates the given account information. Switches on given transaction type. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Validator  $validator | 
					
						
							| 
									
										
										
										
											2019-08-27 06:36:16 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function validateAccountInformation(Validator $validator): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |         Log::debug('Now in validateAccountInformation (RecurrenceFormRequest)()'); | 
					
						
							| 
									
										
										
										
											2019-08-27 06:36:16 +02:00
										 |  |  |         /** @var AccountValidator $accountValidator */ | 
					
						
							|  |  |  |         $accountValidator = app(AccountValidator::class); | 
					
						
							|  |  |  |         $data             = $validator->getData(); | 
					
						
							|  |  |  |         $transactionType  = $data['transaction_type'] ?? 'invalid'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $accountValidator->setTransactionType($transactionType); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // default values:
 | 
					
						
							|  |  |  |         $sourceId      = null; | 
					
						
							|  |  |  |         $destinationId = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 11:43:17 +01:00
										 |  |  |         // TODO typeOverrule: the account validator may have another opinion the transaction type.
 | 
					
						
							| 
									
										
										
										
											2022-12-30 09:28:03 +01:00
										 |  |  |         // TODO either use 'withdrawal' or the strtolower() variant, not both.
 | 
					
						
							| 
									
										
										
										
											2023-02-22 18:14:14 +01:00
										 |  |  |         $type       = $this->convertString('transaction_type'); | 
					
						
							| 
									
										
										
										
											2022-12-30 09:28:03 +01:00
										 |  |  |         $throwError = true; | 
					
						
							| 
									
										
										
										
											2022-12-30 20:38:54 +01:00
										 |  |  |         if ('withdrawal' === $type) { | 
					
						
							| 
									
										
										
										
											2023-02-22 18:14:14 +01:00
										 |  |  |             $throwError    = false; | 
					
						
							| 
									
										
										
										
											2022-12-30 09:28:03 +01:00
										 |  |  |             $sourceId      = (int)$data['source_id']; | 
					
						
							|  |  |  |             $destinationId = (int)$data['withdrawal_destination_id']; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-30 20:38:54 +01:00
										 |  |  |         if ('deposit' === $type) { | 
					
						
							| 
									
										
										
										
											2023-02-22 18:14:14 +01:00
										 |  |  |             $throwError    = false; | 
					
						
							| 
									
										
										
										
											2022-12-30 09:28:03 +01:00
										 |  |  |             $sourceId      = (int)$data['deposit_source_id']; | 
					
						
							|  |  |  |             $destinationId = (int)$data['destination_id']; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-30 20:38:54 +01:00
										 |  |  |         if ('transfer' === $type) { | 
					
						
							| 
									
										
										
										
											2023-02-22 18:14:14 +01:00
										 |  |  |             $throwError    = false; | 
					
						
							| 
									
										
										
										
											2022-12-30 09:28:03 +01:00
										 |  |  |             $sourceId      = (int)$data['source_id']; | 
					
						
							|  |  |  |             $destinationId = (int)$data['destination_id']; | 
					
						
							| 
									
										
										
										
											2019-08-27 06:36:16 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-30 20:38:54 +01:00
										 |  |  |         if (true === $throwError) { | 
					
						
							| 
									
										
										
										
											2022-12-30 09:28:03 +01:00
										 |  |  |             throw new FireflyException(sprintf('Cannot handle transaction type "%s"', $this->convertString('transaction_type'))); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-27 06:36:16 +02:00
										 |  |  |         // validate source account.
 | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |         $validSource = $accountValidator->validateSource(['id' => $sourceId,]); | 
					
						
							| 
									
										
										
										
											2019-08-27 06:36:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // do something with result:
 | 
					
						
							|  |  |  |         if (false === $validSource) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             $message = (string)trans('validation.generic_invalid_source'); | 
					
						
							| 
									
										
										
										
											2019-08-27 06:36:16 +02:00
										 |  |  |             $validator->errors()->add('source_id', $message); | 
					
						
							|  |  |  |             $validator->errors()->add('deposit_source_id', $message); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // validate destination account
 | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |         $validDestination = $accountValidator->validateDestination(['id' => $destinationId,]); | 
					
						
							| 
									
										
										
										
											2019-08-27 06:36:16 +02:00
										 |  |  |         // do something with result:
 | 
					
						
							|  |  |  |         if (false === $validDestination) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             $message = (string)trans('validation.generic_invalid_destination'); | 
					
						
							| 
									
										
										
										
											2019-08-27 06:36:16 +02:00
										 |  |  |             $validator->errors()->add('destination_id', $message); | 
					
						
							|  |  |  |             $validator->errors()->add('withdrawal_destination_id', $message); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Configure the validator instance with special rules for after the basic validation rules. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param  Validator  $validator | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function withValidator(Validator $validator): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $validator->after( | 
					
						
							|  |  |  |             function (Validator $validator) { | 
					
						
							|  |  |  |                 // validate all account info
 | 
					
						
							|  |  |  |                 $this->validateAccountInformation($validator); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Parses repetition data. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function parseRepetitionData(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $value  = $this->convertString('repetition_type'); | 
					
						
							|  |  |  |         $return = [ | 
					
						
							|  |  |  |             'type'   => '', | 
					
						
							|  |  |  |             'moment' => '', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ('daily' === $value) { | 
					
						
							|  |  |  |             $return['type'] = $value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         //monthly,17
 | 
					
						
							|  |  |  |         //ndom,3,7
 | 
					
						
							|  |  |  |         if (in_array(substr($value, 0, 6), ['yearly', 'weekly'], true)) { | 
					
						
							|  |  |  |             $return['type']   = substr($value, 0, 6); | 
					
						
							|  |  |  |             $return['moment'] = substr($value, 7); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (str_starts_with($value, 'monthly')) { | 
					
						
							|  |  |  |             $return['type']   = substr($value, 0, 7); | 
					
						
							|  |  |  |             $return['moment'] = substr($value, 8); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (str_starts_with($value, 'ndom')) { | 
					
						
							|  |  |  |             $return['type']   = substr($value, 0, 4); | 
					
						
							|  |  |  |             $return['moment'] = substr($value, 5); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |