| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * BillUpdateService.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:35 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +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-22 20:07:14 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +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-22 20:07:14 +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-22 20:07:14 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Services\Internal\Update; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2018-12-21 15:42:40 +01:00
										 |  |  | use FireflyIII\Factory\TransactionCurrencyFactory; | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  | use FireflyIII\Models\Bill; | 
					
						
							| 
									
										
										
										
											2020-03-20 08:41:20 +01:00
										 |  |  | use FireflyIII\Models\Rule; | 
					
						
							|  |  |  | use FireflyIII\Models\RuleTrigger; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Bill\BillRepositoryInterface; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2020-07-01 15:33:06 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2020-03-20 08:41:20 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2019-02-12 21:49:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Class BillUpdateService | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class BillUpdateService | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |     use BillServiceTrait; | 
					
						
							|  |  |  |     use CreatesObjectGroups; | 
					
						
							| 
									
										
										
										
											2020-06-30 19:06:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-24 17:27:36 +02:00
										 |  |  |     protected User $user; | 
					
						
							| 
									
										
										
										
											2018-09-06 12:29:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  |     public function update(Bill $bill, array $data): Bill | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-06-30 19:06:05 +02:00
										 |  |  |         $this->user = $bill->user; | 
					
						
							| 
									
										
										
										
											2018-12-21 15:42:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-13 17:16:38 +01:00
										 |  |  |         if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $factory                       = app(TransactionCurrencyFactory::class); | 
					
						
							| 
									
										
										
										
											2025-02-24 04:13:39 +01:00
										 |  |  |             $currency                      = $factory->find((int) ($data['currency_id'] ?? null), $data['currency_code'] ?? null) | 
					
						
							|  |  |  |                         ?? app('amount')->getNativeCurrencyByUserGroup($bill->user->userGroup); | 
					
						
							| 
									
										
										
										
											2018-12-21 15:42:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-13 17:16:38 +01:00
										 |  |  |             // enable the currency if it isn't.
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $currency->enabled             = true; | 
					
						
							| 
									
										
										
										
											2021-03-13 17:16:38 +01:00
										 |  |  |             $currency->save(); | 
					
						
							|  |  |  |             $bill->transaction_currency_id = $currency->id; | 
					
						
							|  |  |  |             $bill->save(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // update bill properties:
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $bill       = $this->updateBillProperties($bill, $data); | 
					
						
							| 
									
										
										
										
											2020-10-27 06:53:33 +01:00
										 |  |  |         $bill->save(); | 
					
						
							| 
									
										
										
										
											2021-03-13 17:16:38 +01:00
										 |  |  |         $bill->refresh(); | 
					
						
							| 
									
										
										
										
											2020-03-20 08:41:20 +01:00
										 |  |  |         // old values
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $oldData    = [ | 
					
						
							| 
									
										
										
										
											2020-10-27 06:53:33 +01:00
										 |  |  |             'name'                      => $bill->name, | 
					
						
							|  |  |  |             'amount_min'                => $bill->amount_min, | 
					
						
							|  |  |  |             'amount_max'                => $bill->amount_max, | 
					
						
							| 
									
										
										
										
											2020-03-20 08:41:20 +01:00
										 |  |  |             'transaction_currency_name' => $bill->transactionCurrency->name, | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  |         // update note:
 | 
					
						
							| 
									
										
										
										
											2021-03-13 17:16:38 +01:00
										 |  |  |         if (array_key_exists('notes', $data)) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $this->updateNote($bill, (string) $data['notes']); | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 15:33:06 +02:00
										 |  |  |         // update order.
 | 
					
						
							| 
									
										
										
										
											2021-03-13 17:16:38 +01:00
										 |  |  |         if (array_key_exists('order', $data)) { | 
					
						
							|  |  |  |             // update the order of the piggy bank:
 | 
					
						
							| 
									
										
										
										
											2023-11-05 19:55:39 +01:00
										 |  |  |             $oldOrder = $bill->order; | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $newOrder = (int) ($data['order'] ?? $oldOrder); | 
					
						
							| 
									
										
										
										
											2021-03-13 17:16:38 +01:00
										 |  |  |             if ($oldOrder !== $newOrder) { | 
					
						
							|  |  |  |                 $this->updateOrder($bill, $oldOrder, $newOrder); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-07-01 15:33:06 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-14 20:31:31 +02:00
										 |  |  |         // update rule actions.
 | 
					
						
							| 
									
										
										
										
											2021-03-13 17:16:38 +01:00
										 |  |  |         if (array_key_exists('name', $data)) { | 
					
						
							|  |  |  |             $this->updateBillActions($bill, $oldData['name'], $data['name']); | 
					
						
							|  |  |  |             $this->updateBillTriggers($bill, $oldData, $data); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-14 20:31:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-30 19:06:05 +02:00
										 |  |  |         // update using name:
 | 
					
						
							| 
									
										
										
										
											2021-03-13 17:16:38 +01:00
										 |  |  |         if (array_key_exists('object_group_title', $data)) { | 
					
						
							|  |  |  |             $objectGroupTitle = $data['object_group_title'] ?? ''; | 
					
						
							|  |  |  |             if ('' !== $objectGroupTitle) { | 
					
						
							|  |  |  |                 $objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle); | 
					
						
							|  |  |  |                 if (null !== $objectGroup) { | 
					
						
							|  |  |  |                     $bill->objectGroups()->sync([$objectGroup->id]); | 
					
						
							|  |  |  |                     $bill->save(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $bill; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             // remove if name is empty. Should be overruled by ID.
 | 
					
						
							| 
									
										
										
										
											2023-11-05 16:55:16 +01:00
										 |  |  |             $bill->objectGroups()->sync([]); | 
					
						
							|  |  |  |             $bill->save(); | 
					
						
							| 
									
										
										
										
											2020-06-30 19:06:05 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-13 17:16:38 +01:00
										 |  |  |         if (array_key_exists('object_group_id', $data)) { | 
					
						
							|  |  |  |             // try also with ID:
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $objectGroupId = (int) ($data['object_group_id'] ?? 0); | 
					
						
							| 
									
										
										
										
											2021-03-13 17:16:38 +01:00
										 |  |  |             if (0 !== $objectGroupId) { | 
					
						
							|  |  |  |                 $objectGroup = $this->findObjectGroupById($objectGroupId); | 
					
						
							|  |  |  |                 if (null !== $objectGroup) { | 
					
						
							|  |  |  |                     $bill->objectGroups()->sync([$objectGroup->id]); | 
					
						
							|  |  |  |                     $bill->save(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $bill; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-11-05 16:55:16 +01:00
										 |  |  |             $bill->objectGroups()->sync([]); | 
					
						
							|  |  |  |             $bill->save(); | 
					
						
							| 
									
										
										
										
											2020-06-30 19:06:05 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-22 20:07:14 +01:00
										 |  |  |         return $bill; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.NPathComplexity") | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |     private function updateBillProperties(Bill $bill, array $data): Bill | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         if (array_key_exists('name', $data) && '' !== (string) $data['name']) { | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |             $bill->name = $data['name']; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         if (array_key_exists('amount_min', $data) && '' !== (string) $data['amount_min']) { | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |             $bill->amount_min = $data['amount_min']; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         if (array_key_exists('amount_max', $data) && '' !== (string) $data['amount_max']) { | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |             $bill->amount_max = $data['amount_max']; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         if (array_key_exists('date', $data) && '' !== (string) $data['date']) { | 
					
						
							| 
									
										
										
										
											2024-11-06 11:57:12 +01:00
										 |  |  |             $bill->date    = $data['date']; | 
					
						
							|  |  |  |             $bill->date_tz = $data['date']->format('e'); | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         if (array_key_exists('repeat_freq', $data) && '' !== (string) $data['repeat_freq']) { | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |             $bill->repeat_freq = $data['repeat_freq']; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-13 17:16:38 +01:00
										 |  |  |         if (array_key_exists('skip', $data)) { | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |             $bill->skip = $data['skip']; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-13 17:16:38 +01:00
										 |  |  |         if (array_key_exists('active', $data)) { | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |             $bill->active = $data['active']; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-03-29 14:59:58 +02:00
										 |  |  |         if (array_key_exists('end_date', $data)) { | 
					
						
							| 
									
										
										
										
											2024-11-06 11:57:12 +01:00
										 |  |  |             $bill->end_date    = $data['end_date']; | 
					
						
							| 
									
										
										
										
											2024-11-08 09:30:24 +01:00
										 |  |  |             $bill->end_date_tz = $data['end_date']?->format('e'); | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-03-29 14:59:58 +02:00
										 |  |  |         if (array_key_exists('extension_date', $data)) { | 
					
						
							| 
									
										
										
										
											2024-11-06 11:57:12 +01:00
										 |  |  |             $bill->extension_date    = $data['extension_date']; | 
					
						
							| 
									
										
										
										
											2024-11-08 09:30:24 +01:00
										 |  |  |             $bill->extension_date_tz = $data['extension_date']?->format('e'); | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $bill->match     = 'EMPTY'; | 
					
						
							|  |  |  |         $bill->automatch = true; | 
					
						
							|  |  |  |         $bill->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $bill; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function updateOrder(Bill $bill, int $oldOrder, int $newOrder): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($newOrder > $oldOrder) { | 
					
						
							|  |  |  |             $this->user->bills()->where('order', '<=', $newOrder)->where('order', '>', $oldOrder) | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |                 ->where('bills.id', '!=', $bill->id) | 
					
						
							|  |  |  |                 ->decrement('bills.order') | 
					
						
							|  |  |  |             ; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             $bill->order = $newOrder; | 
					
						
							|  |  |  |             $bill->save(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($newOrder < $oldOrder) { | 
					
						
							|  |  |  |             $this->user->bills()->where('order', '>=', $newOrder)->where('order', '<', $oldOrder) | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |                 ->where('bills.id', '!=', $bill->id) | 
					
						
							|  |  |  |                 ->increment('bills.order') | 
					
						
							|  |  |  |             ; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             $bill->order = $newOrder; | 
					
						
							|  |  |  |             $bill->save(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 08:41:20 +01:00
										 |  |  |     private function updateBillTriggers(Bill $bill, array $oldData, array $newData): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Now in updateBillTriggers(%d, "%s")', $bill->id, $bill->name)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 08:41:20 +01:00
										 |  |  |         /** @var BillRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(BillRepositoryInterface::class); | 
					
						
							|  |  |  |         $repository->setUser($bill->user); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $rules      = $repository->getRulesForBill($bill); | 
					
						
							| 
									
										
										
										
											2020-03-20 08:41:20 +01:00
										 |  |  |         if (0 === $rules->count()) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug('Found no rules.'); | 
					
						
							| 
									
										
										
										
											2020-03-20 08:41:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Found %d rules', $rules->count())); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $fields     = [ | 
					
						
							| 
									
										
										
										
											2020-10-27 06:53:33 +01:00
										 |  |  |             'name'                      => 'description_contains', | 
					
						
							|  |  |  |             'amount_min'                => 'amount_more', | 
					
						
							|  |  |  |             'amount_max'                => 'amount_less', | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             'transaction_currency_name' => 'currency_is', | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2020-03-20 08:41:20 +01:00
										 |  |  |         foreach ($fields as $field => $ruleTriggerKey) { | 
					
						
							| 
									
										
										
										
											2021-04-01 06:19:01 +02:00
										 |  |  |             if (!array_key_exists($field, $newData)) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-03-20 08:41:20 +01:00
										 |  |  |             if ($oldData[$field] === $newData[$field]) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('Field %s is unchanged ("%s"), continue.', $field, $oldData[$field])); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 08:41:20 +01:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $this->updateRules($rules, $ruleTriggerKey, $oldData[$field], $newData[$field]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function updateRules(Collection $rules, string $key, string $oldValue, string $newValue): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Rule $rule */ | 
					
						
							|  |  |  |         foreach ($rules as $rule) { | 
					
						
							|  |  |  |             $trigger = $this->getRuleTrigger($rule, $key); | 
					
						
							|  |  |  |             if (null !== $trigger && $trigger->trigger_value === $oldValue) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('Updated rule trigger #%d from value "%s" to value "%s"', $trigger->id, $oldValue, $newValue)); | 
					
						
							| 
									
										
										
										
											2020-03-20 08:41:20 +01:00
										 |  |  |                 $trigger->trigger_value = $newValue; | 
					
						
							|  |  |  |                 $trigger->save(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 08:41:20 +01:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (null !== $trigger && $trigger->trigger_value !== $oldValue && in_array($key, ['amount_more', 'amount_less'], true) | 
					
						
							|  |  |  |                 && 0 === bccomp($trigger->trigger_value, $oldValue)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('Updated rule trigger #%d from value "%s" to value "%s"', $trigger->id, $oldValue, $newValue)); | 
					
						
							| 
									
										
										
										
											2020-03-20 08:41:20 +01:00
										 |  |  |                 $trigger->trigger_value = $newValue; | 
					
						
							|  |  |  |                 $trigger->save(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private function getRuleTrigger(Rule $rule, string $key): ?RuleTrigger | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-01-04 07:39:16 +01:00
										 |  |  |         /** @var null|RuleTrigger */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         return $rule->ruleTriggers()->where('trigger_type', $key)->first(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |