| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2020-02-16 14:00:57 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01: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-02-19 19:44:46 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01: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-02-19 19:44:46 +01: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-02-19 19:44:46 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Factory; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | use FireflyIII\Models\Bill; | 
					
						
							| 
									
										
										
										
											2020-06-30 19:06:05 +02:00
										 |  |  | use FireflyIII\Repositories\ObjectGroup\CreatesObjectGroups; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  | use Illuminate\Database\QueryException; | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class BillFactory | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class BillFactory | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:10 +01:00
										 |  |  |     use BillServiceTrait; | 
					
						
							|  |  |  |     use CreatesObjectGroups; | 
					
						
							| 
									
										
										
										
											2018-12-21 15:42:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-23 19:11:25 +02:00
										 |  |  |     private User $user; | 
					
						
							| 
									
										
										
										
											2018-09-06 12:29:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-08-01 17:51:52 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |     public function create(array $data): ?Bill | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Now in %s', __METHOD__), $data); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $factory          = app(TransactionCurrencyFactory::class); | 
					
						
							|  |  |  |         $currency         = $factory->find((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null)) ?? | 
					
						
							| 
									
										
										
										
											2023-10-28 14:59:16 +02:00
										 |  |  |                     app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup); | 
					
						
							| 
									
										
										
										
											2018-12-21 15:42:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2021-03-13 16:47:29 +01:00
										 |  |  |             $skip   = array_key_exists('skip', $data) ? $data['skip'] : 0; | 
					
						
							|  |  |  |             $active = array_key_exists('active', $data) ? $data['active'] : 0; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  |             /** @var Bill $bill */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $bill   = Bill::create( | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  |                 [ | 
					
						
							|  |  |  |                     'name'                    => $data['name'], | 
					
						
							|  |  |  |                     'match'                   => 'MIGRATED_TO_RULES', | 
					
						
							|  |  |  |                     'amount_min'              => $data['amount_min'], | 
					
						
							|  |  |  |                     'user_id'                 => $this->user->id, | 
					
						
							| 
									
										
										
										
											2023-08-11 19:37:28 +02:00
										 |  |  |                     'user_group_id'           => $this->user->user_group_id, | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  |                     'transaction_currency_id' => $currency->id, | 
					
						
							|  |  |  |                     'amount_max'              => $data['amount_max'], | 
					
						
							|  |  |  |                     'date'                    => $data['date'], | 
					
						
							| 
									
										
										
										
											2021-07-25 19:39:35 +02:00
										 |  |  |                     'end_date'                => $data['end_date'] ?? null, | 
					
						
							|  |  |  |                     'extension_date'          => $data['extension_date'] ?? null, | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  |                     'repeat_freq'             => $data['repeat_freq'], | 
					
						
							| 
									
										
										
										
											2021-03-13 16:47:29 +01:00
										 |  |  |                     'skip'                    => $skip, | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  |                     'automatch'               => true, | 
					
						
							| 
									
										
										
										
											2021-03-13 16:47:29 +01:00
										 |  |  |                     'active'                  => $active, | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  |                 ] | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2020-03-17 14:57:04 +01:00
										 |  |  |         } catch (QueryException $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error($e->getMessage()); | 
					
						
							|  |  |  |             app('log')->error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  |             throw new FireflyException('400000: Could not store bill.', 0, $e); | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-12-21 15:42:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 17:51:52 +02:00
										 |  |  |         if (array_key_exists('notes', $data)) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |             $this->updateNote($bill, (string)$data['notes']); | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-13 16:47:29 +01:00
										 |  |  |         $objectGroupTitle = $data['object_group_title'] ?? ''; | 
					
						
							| 
									
										
										
										
											2020-06-30 19:06:05 +02:00
										 |  |  |         if ('' !== $objectGroupTitle) { | 
					
						
							|  |  |  |             $objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle); | 
					
						
							|  |  |  |             if (null !== $objectGroup) { | 
					
						
							|  |  |  |                 $bill->objectGroups()->sync([$objectGroup->id]); | 
					
						
							|  |  |  |                 $bill->save(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // try also with ID:
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $objectGroupId    = (int)($data['object_group_id'] ?? 0); | 
					
						
							| 
									
										
										
										
											2020-06-30 19:06:05 +02:00
										 |  |  |         if (0 !== $objectGroupId) { | 
					
						
							|  |  |  |             $objectGroup = $this->findObjectGroupById($objectGroupId); | 
					
						
							|  |  |  |             if (null !== $objectGroup) { | 
					
						
							|  |  |  |                 $bill->objectGroups()->sync([$objectGroup->id]); | 
					
						
							|  |  |  |                 $bill->save(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |         return $bill; | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function find(?int $billId, ?string $billName): ?Bill | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |         $billId   = (int)$billId; | 
					
						
							|  |  |  |         $billName = (string)$billName; | 
					
						
							| 
									
										
										
										
											2018-07-06 19:06:08 +02:00
										 |  |  |         $bill     = null; | 
					
						
							| 
									
										
										
										
											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-02-19 19:44:46 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // then find by name:
 | 
					
						
							| 
									
										
										
										
											2019-02-13 17:38:41 +01:00
										 |  |  |         if (null === $bill && '' !== $billName) { | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |             $bill = $this->findByName($billName); | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-06 19:06:08 +02:00
										 |  |  |         return $bill; | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-23 19:11:25 +02:00
										 |  |  |         return $this->user->bills()->where('name', 'LIKE', sprintf('%%%s%%', $name))->first(); | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-06 19:06:08 +02:00
										 |  |  |     public function setUser(User $user): void | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } |