| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * BillFactory.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-19 19:44:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Factory; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Models\Bill; | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  | use FireflyIII\Services\Internal\Support\BillServiceTrait; | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							|  |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class BillFactory | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class BillFactory | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  |     use BillServiceTrait; | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |     /** @var User */ | 
					
						
							|  |  |  |     private $user; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |      * @param array $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Bill|null | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |     public function create(array $data): ?Bill | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |         /** @var Bill $bill */ | 
					
						
							|  |  |  |         $bill = Bill::create( | 
					
						
							|  |  |  |             [ | 
					
						
							| 
									
										
										
										
											2018-04-08 16:27:52 +02:00
										 |  |  |                 'name'                    => $data['name'], | 
					
						
							|  |  |  |                 'match'                   => 'MIGRATED_TO_RULES', | 
					
						
							|  |  |  |                 'amount_min'              => $data['amount_min'], | 
					
						
							|  |  |  |                 'user_id'                 => $this->user->id, | 
					
						
							|  |  |  |                 'transaction_currency_id' => $data['transaction_currency_id'], | 
					
						
							|  |  |  |                 'amount_max'              => $data['amount_max'], | 
					
						
							|  |  |  |                 'date'                    => $data['date'], | 
					
						
							|  |  |  |                 'repeat_freq'             => $data['repeat_freq'], | 
					
						
							|  |  |  |                 'skip'                    => $data['skip'], | 
					
						
							|  |  |  |                 'automatch'               => true, | 
					
						
							|  |  |  |                 'active'                  => $data['active'], | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |             ] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // update note:
 | 
					
						
							|  |  |  |         if (isset($data['notes'])) { | 
					
						
							|  |  |  |             $this->updateNote($bill, $data['notes']); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $bill; | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param int|null    $billId | 
					
						
							|  |  |  |      * @param null|string $billName | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Bill|null | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function find(?int $billId, ?string $billName): ?Bill | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-04-02 14:42:07 +02:00
										 |  |  |         $billId   = (int)$billId; | 
					
						
							|  |  |  |         $billName = (string)$billName; | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |         // first find by ID:
 | 
					
						
							|  |  |  |         if ($billId > 0) { | 
					
						
							|  |  |  |             /** @var Bill $bill */ | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |             $bill = $this->user->bills()->find($billId); | 
					
						
							| 
									
										
										
										
											2018-04-02 14:42:07 +02:00
										 |  |  |             if (null !== $bill) { | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |                 return $bill; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // then find by name:
 | 
					
						
							| 
									
										
										
										
											2018-04-07 22:23:16 +02:00
										 |  |  |         if (\strlen($billName) > 0) { | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |             $bill = $this->findByName($billName); | 
					
						
							| 
									
										
										
										
											2018-04-02 14:42:07 +02:00
										 |  |  |             if (null !== $bill) { | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |                 return $bill; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |      * @param string $name | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Bill|null | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |     public function findByName(string $name): ?Bill | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |         /** @var Collection $collection */ | 
					
						
							|  |  |  |         $collection = $this->user->bills()->get(); | 
					
						
							|  |  |  |         /** @var Bill $bill */ | 
					
						
							|  |  |  |         foreach ($collection as $bill) { | 
					
						
							|  |  |  |             Log::debug(sprintf('"%s" vs. "%s"', $bill->name, $name)); | 
					
						
							|  |  |  |             if ($bill->name === $name) { | 
					
						
							|  |  |  |                 return $bill; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Log::debug(sprintf('Bill::Find by name returns NULL based on "%s"', $name)); | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |      * @param User $user | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |     public function setUser(User $user) | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |         $this->user = $user; | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |