| 
									
										
										
										
											2018-02-10 09:57:31 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 09:57:31 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * BillRequest.php | 
					
						
							|  |  |  |  * Copyright (c) 2018 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2018-02-10 09:57:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Api\V1\Requests; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 18:33:07 +01:00
										 |  |  | use FireflyIII\Rules\IsBoolean; | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  | use Illuminate\Validation\Validator; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 09:57:31 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Class BillRequest | 
					
						
							| 
									
										
										
										
											2019-04-12 04:53:18 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2018-02-10 09:57:31 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class BillRequest extends Request | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * Authorize logged in users. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-02-10 09:57:31 +01:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-16 16:42:23 +01:00
										 |  |  |     public function authorize(): bool | 
					
						
							| 
									
										
										
										
											2018-02-10 09:57:31 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         // Only allow authenticated users
 | 
					
						
							|  |  |  |         return auth()->check(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * Get all data from the request. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-02-10 09:57:31 +01:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAll(): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-12-21 15:42:57 +01:00
										 |  |  |         $active = true; | 
					
						
							| 
									
										
										
										
											2019-02-13 17:38:41 +01:00
										 |  |  |         if (null !== $this->get('active')) { | 
					
						
							| 
									
										
										
										
											2018-12-21 15:42:57 +01:00
										 |  |  |             $active = $this->boolean('active'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 09:57:31 +01:00
										 |  |  |         $data = [ | 
					
						
							| 
									
										
										
										
											2018-04-15 19:20:24 +02:00
										 |  |  |             'name'          => $this->string('name'), | 
					
						
							|  |  |  |             'amount_min'    => $this->string('amount_min'), | 
					
						
							|  |  |  |             'amount_max'    => $this->string('amount_max'), | 
					
						
							|  |  |  |             'currency_id'   => $this->integer('currency_id'), | 
					
						
							|  |  |  |             'currency_code' => $this->string('currency_code'), | 
					
						
							|  |  |  |             'date'          => $this->date('date'), | 
					
						
							|  |  |  |             'repeat_freq'   => $this->string('repeat_freq'), | 
					
						
							|  |  |  |             'skip'          => $this->integer('skip'), | 
					
						
							| 
									
										
										
										
											2018-12-21 15:42:57 +01:00
										 |  |  |             'active'        => $active, | 
					
						
							| 
									
										
										
										
											2018-04-15 19:20:24 +02:00
										 |  |  |             'notes'         => $this->string('notes'), | 
					
						
							| 
									
										
										
										
											2018-02-10 09:57:31 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * The rules that the incoming request must be matched against. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-02-10 09:57:31 +01:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-16 16:42:23 +01:00
										 |  |  |     public function rules(): array | 
					
						
							| 
									
										
										
										
											2018-02-10 09:57:31 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $rules = [ | 
					
						
							| 
									
										
										
										
											2018-04-15 19:20:24 +02:00
										 |  |  |             'name'          => 'required|between:1,255|uniqueObjectForUser:bills,name', | 
					
						
							|  |  |  |             'amount_min'    => 'required|numeric|more:0', | 
					
						
							|  |  |  |             'amount_max'    => 'required|numeric|more:0', | 
					
						
							| 
									
										
										
										
											2018-12-14 18:33:07 +01:00
										 |  |  |             'currency_id'   => 'numeric|exists:transaction_currencies,id', | 
					
						
							|  |  |  |             'currency_code' => 'min:3|max:3|exists:transaction_currencies,code', | 
					
						
							| 
									
										
										
										
											2018-04-15 19:20:24 +02:00
										 |  |  |             'date'          => 'required|date', | 
					
						
							|  |  |  |             'repeat_freq'   => 'required|in:weekly,monthly,quarterly,half-year,yearly', | 
					
						
							| 
									
										
										
										
											2018-12-14 18:33:07 +01:00
										 |  |  |             'skip'          => 'between:0,31', | 
					
						
							|  |  |  |             'automatch'     => [new IsBoolean], | 
					
						
							|  |  |  |             'active'        => [new IsBoolean], | 
					
						
							| 
									
										
										
										
											2018-04-15 19:20:24 +02:00
										 |  |  |             'notes'         => 'between:1,65536', | 
					
						
							| 
									
										
										
										
											2018-02-10 09:57:31 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  |         switch ($this->method()) { | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'PUT': | 
					
						
							|  |  |  |             case 'PATCH': | 
					
						
							| 
									
										
										
										
											2018-06-06 21:23:00 +02:00
										 |  |  |                 $bill          = $this->route()->parameter('bill'); | 
					
						
							|  |  |  |                 $rules['name'] .= ',' . $bill->id; | 
					
						
							| 
									
										
										
										
											2018-02-10 09:57:31 +01:00
										 |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $rules; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Configure the validator instance. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-04-12 04:53:18 +02:00
										 |  |  |      * @param Validator $validator | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function withValidator(Validator $validator): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $validator->after( | 
					
						
							|  |  |  |             function (Validator $validator) { | 
					
						
							|  |  |  |                 $data = $validator->getData(); | 
					
						
							| 
									
										
										
										
											2018-04-02 14:17:11 +02:00
										 |  |  |                 $min  = (float)($data['amount_min'] ?? 0); | 
					
						
							|  |  |  |                 $max  = (float)($data['amount_max'] ?? 0); | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  |                 if ($min > $max) { | 
					
						
							| 
									
										
										
										
											2018-07-15 09:38:49 +02:00
										 |  |  |                     $validator->errors()->add('amount_min', (string)trans('validation.amount_min_over_max')); | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |