| 
									
										
										
										
											2018-06-28 17:02:13 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-12-14 18:33:07 +01:00
										 |  |  |  * TransactionLinkRequest.php | 
					
						
							| 
									
										
										
										
											2018-06-28 17:02:13 +02:00
										 |  |  |  * 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Api\V1\Requests; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-11 18:27:45 +02:00
										 |  |  | use FireflyIII\Repositories\Journal\JournalRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2019-02-13 17:38:41 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2018-08-11 18:27:45 +02:00
										 |  |  | use Illuminate\Validation\Validator; | 
					
						
							| 
									
										
										
										
											2018-06-28 17:02:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-14 18:33:07 +01:00
										 |  |  |  * Class TransactionLinkRequest | 
					
						
							| 
									
										
										
										
											2018-06-28 17:02:13 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-12-14 18:33:07 +01:00
										 |  |  | class TransactionLinkRequest extends Request | 
					
						
							| 
									
										
										
										
											2018-06-28 17:02:13 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * Authorize logged in users. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-06-28 17:02:13 +02:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function authorize(): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Only allow authenticated users
 | 
					
						
							|  |  |  |         return auth()->check(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * Get all data from the request. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-06-28 17:02:13 +02:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAll(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2018-08-11 18:27:45 +02:00
										 |  |  |             'link_type_id'   => $this->integer('link_type_id'), | 
					
						
							|  |  |  |             'link_type_name' => $this->string('link_type_name'), | 
					
						
							|  |  |  |             'inward_id'      => $this->integer('inward_id'), | 
					
						
							|  |  |  |             'outward_id'     => $this->integer('outward_id'), | 
					
						
							|  |  |  |             'notes'          => $this->string('notes'), | 
					
						
							| 
									
										
										
										
											2018-06-28 17:02:13 +02:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-07 21:17:46 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * The rules that the incoming request must be matched against. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-06-28 17:02:13 +02:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function rules(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2018-08-11 18:27:45 +02:00
										 |  |  |             'link_type_id'   => 'exists:link_types,id|required_without:link_type_name', | 
					
						
							|  |  |  |             'link_type_name' => 'exists:link_types,name|required_without:link_type_id', | 
					
						
							| 
									
										
										
										
											2018-12-08 07:57:29 +01:00
										 |  |  |             'inward_id'      => 'required|belongsToUser:transaction_journals,id|different:outward_id', | 
					
						
							|  |  |  |             'outward_id'     => 'required|belongsToUser:transaction_journals,id|different:inward_id', | 
					
						
							| 
									
										
										
										
											2018-08-11 18:27:45 +02:00
										 |  |  |             'notes'          => 'between:0,65000', | 
					
						
							| 
									
										
										
										
											2018-06-28 17:02:13 +02:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-11 18:27:45 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Configure the validator instance. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-05-30 12:39:06 +02:00
										 |  |  |      * @param Validator $validator | 
					
						
							| 
									
										
										
										
											2018-08-11 18:27:45 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function withValidator(Validator $validator): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $validator->after( | 
					
						
							|  |  |  |             function (Validator $validator) { | 
					
						
							|  |  |  |                 $this->validateExistingLink($validator); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Validator $validator | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function validateExistingLink(Validator $validator): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-02-13 17:38:41 +01:00
										 |  |  |         /** @var User $user */ | 
					
						
							|  |  |  |         $user = auth()->user(); | 
					
						
							| 
									
										
										
										
											2018-08-11 18:27:45 +02:00
										 |  |  |         /** @var LinkTypeRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(LinkTypeRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2019-02-13 17:38:41 +01:00
										 |  |  |         $repository->setUser($user); | 
					
						
							| 
									
										
										
										
											2018-08-11 18:27:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var JournalRepositoryInterface $journalRepos */ | 
					
						
							|  |  |  |         $journalRepos = app(JournalRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2019-02-13 17:38:41 +01:00
										 |  |  |         $journalRepos->setUser($user); | 
					
						
							| 
									
										
										
										
											2018-08-11 18:27:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $data      = $validator->getData(); | 
					
						
							|  |  |  |         $inwardId  = (int)($data['inward_id'] ?? 0); | 
					
						
							|  |  |  |         $outwardId = (int)($data['outward_id'] ?? 0); | 
					
						
							|  |  |  |         $inward    = $journalRepos->findNull($inwardId); | 
					
						
							|  |  |  |         $outward   = $journalRepos->findNull($outwardId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (null === $inward) { | 
					
						
							|  |  |  |             $validator->errors()->add('inward_id', 'Invalid inward ID.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (null === $outward) { | 
					
						
							|  |  |  |             $validator->errors()->add('outward_id', 'Invalid outward ID.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($repository->findLink($inward, $outward)) { | 
					
						
							| 
									
										
										
										
											2018-12-08 07:57:29 +01:00
										 |  |  |             // only if not updating:
 | 
					
						
							|  |  |  |             $link = $this->route()->parameter('journalLink'); | 
					
						
							|  |  |  |             if (null === $link) { | 
					
						
							|  |  |  |                 $validator->errors()->add('outward_id', 'Already have a link between inward and outward.'); | 
					
						
							|  |  |  |                 $validator->errors()->add('inward_id', 'Already have a link between inward and outward.'); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-08-11 18:27:45 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |