| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2020-11-08 13:31:54 +01:00
										 |  |  |  * PiggyBankUpdateRequest.php | 
					
						
							| 
									
										
										
										
											2020-01-23 19:44:52 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +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 06:43:44 +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 06:43:44 +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 06:43:44 +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 06:43:44 +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 06:43:44 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-06 12:45:49 +01:00
										 |  |  | namespace FireflyIII\Api\V1\Requests\Models\PiggyBank; | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 14:44:49 +01:00
										 |  |  | use FireflyIII\Models\PiggyBank; | 
					
						
							| 
									
										
										
										
											2024-01-02 14:33:17 +01:00
										 |  |  | use FireflyIII\Rules\IsValidPositiveAmount; | 
					
						
							| 
									
										
										
										
											2025-02-22 08:03:42 +01:00
										 |  |  | use FireflyIII\Rules\IsValidZeroOrMoreAmount; | 
					
						
							| 
									
										
										
										
											2019-08-21 05:15:47 +02:00
										 |  |  | use FireflyIII\Rules\LessThanPiggyTarget; | 
					
						
							| 
									
										
										
										
											2020-11-08 13:31:54 +01:00
										 |  |  | use FireflyIII\Support\Request\ChecksLogin; | 
					
						
							| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  | use FireflyIII\Support\Request\ConvertsDataTypes; | 
					
						
							| 
									
										
										
										
											2020-07-18 08:25:25 +02:00
										 |  |  | use Illuminate\Foundation\Http\FormRequest; | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2021-03-06 12:45:49 +01:00
										 |  |  |  * Class UpdateRequest | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-03-06 12:45:49 +01:00
										 |  |  | class UpdateRequest extends FormRequest | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:23:00 +01:00
										 |  |  |     use ChecksLogin; | 
					
						
							| 
									
										
										
										
											2023-11-04 14:18:49 +01:00
										 |  |  |     use ConvertsDataTypes; | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * Get all data from the request. | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function getAll(): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-02-22 08:18:27 +01:00
										 |  |  |         $fields             = [ | 
					
						
							| 
									
										
										
										
											2025-02-22 08:03:42 +01:00
										 |  |  |             'name'                      => ['name', 'convertString'], | 
					
						
							|  |  |  |             'target_amount'             => ['target_amount', 'convertString'], | 
					
						
							|  |  |  |             'start_date'                => ['start_date', 'convertDateTime'], | 
					
						
							|  |  |  |             'target_date'               => ['target_date', 'convertDateTime'], | 
					
						
							|  |  |  |             'notes'                     => ['notes', 'stringWithNewlines'], | 
					
						
							|  |  |  |             'order'                     => ['order', 'convertInteger'], | 
					
						
							|  |  |  |             'object_group_title'        => ['object_group_title', 'convertString'], | 
					
						
							|  |  |  |             'object_group_id'           => ['object_group_id', 'convertInteger'], | 
					
						
							|  |  |  |             'transaction_currency_code' => ['transaction_currency_code', 'convertString'], | 
					
						
							|  |  |  |             'transaction_currency_id'   => ['transaction_currency_id', 'convertInteger'], | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2021-03-14 06:20:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-22 08:03:42 +01:00
										 |  |  |         $result             = $this->getAllData($fields); | 
					
						
							|  |  |  |         $result['accounts'] = $this->parseAccounts($this->get('accounts')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * The rules that the incoming request must be matched against. | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function rules(): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:44:49 +01:00
										 |  |  |         /** @var PiggyBank $piggyBank */ | 
					
						
							| 
									
										
										
										
											2020-12-02 06:41:42 +01:00
										 |  |  |         $piggyBank = $this->route()->parameter('piggyBank'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-08 13:31:54 +01:00
										 |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2025-02-22 08:18:27 +01:00
										 |  |  |             'name'                      => 'min:1|max:255|uniquePiggyBankForUser:'.$piggyBank->id, | 
					
						
							| 
									
										
										
										
											2025-02-22 08:03:42 +01:00
										 |  |  |             'current_amount'            => ['nullable', new LessThanPiggyTarget(), new IsValidPositiveAmount()], | 
					
						
							|  |  |  |             'target_amount'             => ['nullable', new IsValidZeroOrMoreAmount()], | 
					
						
							|  |  |  |             'start_date'                => 'date|nullable', | 
					
						
							|  |  |  |             'target_date'               => 'date|nullable|after:start_date', | 
					
						
							|  |  |  |             'notes'                     => 'max:65000', | 
					
						
							|  |  |  |             'accounts'                  => 'required', | 
					
						
							|  |  |  |             'accounts.*'                => 'array|required', | 
					
						
							|  |  |  |             'accounts.*.account_id'     => ['required', 'numeric', 'belongsToUser:accounts,id'], | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |             'accounts.*.current_amount' => ['numeric', 'nullable', new IsValidZeroOrMoreAmount(true)], | 
					
						
							| 
									
										
										
										
											2025-02-22 08:03:42 +01:00
										 |  |  |             'object_group_id'           => 'numeric|belongsToUser:object_groups,id', | 
					
						
							|  |  |  |             'object_group_title'        => ['min:1', 'max:255'], | 
					
						
							|  |  |  |             'transaction_currency_id'   => 'exists:transaction_currencies,id|nullable', | 
					
						
							|  |  |  |             'transaction_currency_code' => 'exists:transaction_currencies,code|nullable', | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |