| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * BudgetLimitHandler.php | 
					
						
							|  |  |  |  * Copyright (c) 2023 james@firefly-iii.org | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program 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 Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  | namespace FireflyIII\Handlers\Events\Model; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Events\Model\BudgetLimit\Created; | 
					
						
							|  |  |  | use FireflyIII\Events\Model\BudgetLimit\Deleted; | 
					
						
							|  |  |  | use FireflyIII\Events\Model\BudgetLimit\Updated; | 
					
						
							|  |  |  | use FireflyIII\Models\AvailableBudget; | 
					
						
							| 
									
										
										
										
											2023-05-13 06:17:22 +02:00
										 |  |  | use FireflyIII\Models\Budget; | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  | use FireflyIII\Models\BudgetLimit; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2023-11-01 18:45:15 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2023-04-30 06:45:25 +02:00
										 |  |  | use Psr\Container\ContainerExceptionInterface; | 
					
						
							|  |  |  | use Psr\Container\NotFoundExceptionInterface; | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  | use Spatie\Period\Boundaries; | 
					
						
							|  |  |  | use Spatie\Period\Period; | 
					
						
							|  |  |  | use Spatie\Period\Precision; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class BudgetLimitHandler | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class BudgetLimitHandler | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-17 20:33:26 +02:00
										 |  |  |     public function created(Created $event): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |         Log::debug(sprintf('BudgetLimitHandler::created(#%s)', $event->budgetLimit->id)); | 
					
						
							| 
									
										
										
										
											2025-08-18 19:34:29 +02:00
										 |  |  |         self::updateAvailableBudget($event->budgetLimit); | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-18 19:34:29 +02:00
										 |  |  |     public static function updateAvailableBudget(BudgetLimit $budgetLimit): void | 
					
						
							| 
									
										
										
										
											2023-07-17 20:33:26 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |         Log::debug(sprintf('Now in updateAvailableBudget(limit #%d)', $budgetLimit->id)); | 
					
						
							| 
									
										
										
										
											2025-01-05 07:31:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var null|Budget $budget */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $budget      = Budget::find($budgetLimit->budget_id); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (null === $budget) { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |             Log::warning('Budget is null, probably deleted, find deleted version.'); | 
					
						
							| 
									
										
										
										
											2025-01-05 07:31:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             /** @var null|Budget $budget */ | 
					
						
							| 
									
										
										
										
											2023-07-18 06:38:11 +02:00
										 |  |  |             $budget = Budget::withTrashed()->find($budgetLimit->budget_id); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (null === $budget) { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |             Log::warning('Budget is still null, cannot continue, will delete budget limit.'); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             $budgetLimit->forceDelete(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var null|User $user */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $user        = $budget->user; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // sanity check. It happens when the budget has been deleted so the original user is unknown.
 | 
					
						
							|  |  |  |         if (null === $user) { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |             Log::warning('User is null, cannot continue.'); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             $budgetLimit->forceDelete(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-25 07:12:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-06 05:45:35 +02:00
										 |  |  |         // based on the view range of the user (month week quarter etc) the budget limit could
 | 
					
						
							|  |  |  |         // either overlap multiple available budget periods or be contained in a single one.
 | 
					
						
							|  |  |  |         // all have to be created or updated.
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $viewRange = app('preferences')->getForUser($user, 'viewRange', '1M')->data; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         } catch (ContainerExceptionInterface|NotFoundExceptionInterface $e) { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |             Log::error($e->getMessage()); | 
					
						
							| 
									
										
										
										
											2023-09-06 05:45:35 +02:00
										 |  |  |             $viewRange = '1M'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-11-26 12:10:42 +01:00
										 |  |  |         // safety catch
 | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  |         if (null === $viewRange || is_array($viewRange)) { | 
					
						
							| 
									
										
										
										
											2023-11-26 12:10:42 +01:00
										 |  |  |             $viewRange = '1M'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $viewRange   = (string) $viewRange; | 
					
						
							| 
									
										
										
										
											2023-09-06 05:45:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $start       = app('navigation')->startOfPeriod($budgetLimit->start_date, $viewRange); | 
					
						
							|  |  |  |         $end         = app('navigation')->startOfPeriod($budgetLimit->end_date, $viewRange); | 
					
						
							|  |  |  |         $end         = app('navigation')->endOfPeriod($end, $viewRange); | 
					
						
							| 
									
										
										
										
											2023-09-09 15:12:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // limit period in total is:
 | 
					
						
							|  |  |  |         $limitPeriod = Period::make($start, $end, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE()); | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |         Log::debug(sprintf('Limit period is from %s to %s', $start->format('Y-m-d'), $end->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // from the start until the end of the budget limit, need to loop!
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $current     = clone $start; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         while ($current <= $end) { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $currentEnd      = app('navigation')->endOfPeriod($current, $viewRange); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // create or find AB for this particular period, and set the amount accordingly.
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             /** @var null|AvailableBudget $availableBudget */ | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |             $availableBudget = $user->availableBudgets()->where('start_date', $current->format('Y-m-d'))->where('end_date', $currentEnd->format('Y-m-d'))->where('transaction_currency_id', $budgetLimit->transaction_currency_id)->first(); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (null !== $availableBudget) { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |                 Log::debug('Found 1 AB, will update.'); | 
					
						
							| 
									
										
										
										
											2025-08-18 19:34:29 +02:00
										 |  |  |                 self::calculateAmount($availableBudget); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (null === $availableBudget) { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |                 Log::debug('No AB found, will create.'); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 // if not exists:
 | 
					
						
							|  |  |  |                 $currentPeriod = Period::make($current, $currentEnd, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE()); | 
					
						
							| 
									
										
										
										
											2025-08-18 19:34:29 +02:00
										 |  |  |                 $daily         = self::getDailyAmount($budgetLimit); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 $amount        = bcmul($daily, (string) $currentPeriod->length(), 12); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 // no need to calculate if period is equal.
 | 
					
						
							|  |  |  |                 if ($currentPeriod->equals($limitPeriod)) { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:41:37 +01:00
										 |  |  |                     $amount = 0 === $budgetLimit->id ? '0' : $budgetLimit->amount; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 if (0 === bccomp($amount, '0')) { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |                     Log::debug('Amount is zero, will not create AB.'); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 if (0 !== bccomp($amount, '0')) { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |                     Log::debug(sprintf('Will create AB for period %s to %s', $current->format('Y-m-d'), $currentEnd->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                     $availableBudget = new AvailableBudget( | 
					
						
							|  |  |  |                         [ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                             'user_id'                 => $user->id, | 
					
						
							|  |  |  |                             'user_group_id'           => $user->user_group_id, | 
					
						
							|  |  |  |                             'transaction_currency_id' => $budgetLimit->transaction_currency_id, | 
					
						
							|  |  |  |                             'start_date'              => $current, | 
					
						
							|  |  |  |                             'start_date_tz'           => $current->format('e'), | 
					
						
							|  |  |  |                             'end_date'                => $currentEnd, | 
					
						
							|  |  |  |                             'end_date_tz'             => $currentEnd->format('e'), | 
					
						
							|  |  |  |                             'amount'                  => $amount, | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                         ] | 
					
						
							|  |  |  |                     ); | 
					
						
							|  |  |  |                     $availableBudget->save(); | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |                     Log::debug(sprintf('ID of new AB is #%d', $availableBudget->id)); | 
					
						
							| 
									
										
										
										
											2025-08-18 19:34:29 +02:00
										 |  |  |                     self::calculateAmount($availableBudget); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // prep for next loop
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $current         = app('navigation')->addPeriod($current, $viewRange, 0); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-18 19:34:29 +02:00
										 |  |  |     private static function calculateAmount(AvailableBudget $availableBudget): void | 
					
						
							| 
									
										
										
										
											2023-07-17 20:33:26 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $repository              = app(BudgetLimitRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |         $repository->setUser($availableBudget->user); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $newAmount               = '0'; | 
					
						
							|  |  |  |         $abPeriod                = Period::make($availableBudget->start_date, $availableBudget->end_date, Precision::DAY()); | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |         Log::debug( | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |             sprintf( | 
					
						
							|  |  |  |                 'Now at AB #%d, ("%s" to "%s")', | 
					
						
							|  |  |  |                 $availableBudget->id, | 
					
						
							|  |  |  |                 $availableBudget->start_date->format('Y-m-d'), | 
					
						
							|  |  |  |                 $availableBudget->end_date->format('Y-m-d') | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2023-08-15 17:19:50 +02:00
										 |  |  |         // have to recalculate everything just in case.
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $set                     = $repository->getAllBudgetLimitsByCurrency($availableBudget->transactionCurrency, $availableBudget->start_date, $availableBudget->end_date); | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |         Log::debug(sprintf('Found %d interesting budget limit(s).', $set->count())); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |         /** @var BudgetLimit $budgetLimit */ | 
					
						
							|  |  |  |         foreach ($set as $budgetLimit) { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |             Log::debug( | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |                 sprintf( | 
					
						
							|  |  |  |                     'Found interesting budget limit #%d ("%s" to "%s")', | 
					
						
							|  |  |  |                     $budgetLimit->id, | 
					
						
							|  |  |  |                     $budgetLimit->start_date->format('Y-m-d'), | 
					
						
							|  |  |  |                     $budgetLimit->end_date->format('Y-m-d') | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |             // overlap in days:
 | 
					
						
							|  |  |  |             $limitPeriod = Period::make( | 
					
						
							| 
									
										
										
										
											2023-12-10 06:57:41 +01:00
										 |  |  |                 $budgetLimit->start_date, | 
					
						
							|  |  |  |                 $budgetLimit->end_date, | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 precision : Precision::DAY(), | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |                 boundaries: Boundaries::EXCLUDE_NONE() | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2023-08-15 17:19:50 +02:00
										 |  |  |             // if both equal each other, amount from this BL must be added to the AB
 | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |             if ($limitPeriod->equals($abPeriod)) { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |                 Log::debug('This budget limit is equal to the available budget period.'); | 
					
						
							| 
									
										
										
										
											2025-05-04 13:50:20 +02:00
										 |  |  |                 $newAmount = bcadd($newAmount, (string) $budgetLimit->amount); | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-08-15 17:19:50 +02:00
										 |  |  |             // if budget limit period is inside AB period, it can be added in full.
 | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |             if (!$limitPeriod->equals($abPeriod) && $abPeriod->contains($limitPeriod)) { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |                 Log::debug('This budget limit is smaller than the available budget period.'); | 
					
						
							| 
									
										
										
										
											2025-05-04 13:50:20 +02:00
										 |  |  |                 $newAmount = bcadd($newAmount, (string) $budgetLimit->amount); | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-08-15 17:19:50 +02:00
										 |  |  |             if (!$limitPeriod->equals($abPeriod) && !$abPeriod->contains($limitPeriod) && $abPeriod->overlapsWith($limitPeriod)) { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |                 Log::debug('This budget limit is something else entirely!'); | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |                 $overlap = $abPeriod->overlap($limitPeriod); | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |                 if ($overlap instanceof Period) { | 
					
						
							| 
									
										
										
										
											2023-04-26 12:12:23 +02:00
										 |  |  |                     $length    = $overlap->length(); | 
					
						
							| 
									
										
										
										
											2025-08-18 19:34:29 +02:00
										 |  |  |                     $daily     = bcmul(self::getDailyAmount($budgetLimit), (string) $length); | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |                     $newAmount = bcadd($newAmount, $daily); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-04-30 06:45:25 +02:00
										 |  |  |         if (0 === bccomp('0', $newAmount)) { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |             Log::debug('New amount is zero, deleting AB.'); | 
					
						
							| 
									
										
										
										
											2023-04-30 06:45:25 +02:00
										 |  |  |             $availableBudget->delete(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-30 06:45:25 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |         Log::debug(sprintf('Concluded new amount for this AB must be %s', $newAmount)); | 
					
						
							| 
									
										
										
										
											2023-08-15 17:19:50 +02:00
										 |  |  |         $availableBudget->amount = app('steam')->bcround($newAmount, $availableBudget->transactionCurrency->decimal_places); | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |         $availableBudget->save(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-18 19:34:29 +02:00
										 |  |  |     private static function getDailyAmount(BudgetLimit $budgetLimit): string | 
					
						
							| 
									
										
										
										
											2023-07-17 20:33:26 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:41:37 +01:00
										 |  |  |         if (0 === $budgetLimit->id) { | 
					
						
							| 
									
										
										
										
											2023-04-30 06:45:25 +02:00
										 |  |  |             return '0'; | 
					
						
							| 
									
										
										
										
											2023-05-07 20:17:29 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |         $limitPeriod = Period::make( | 
					
						
							| 
									
										
										
										
											2023-12-10 06:57:41 +01:00
										 |  |  |             $budgetLimit->start_date, | 
					
						
							|  |  |  |             $budgetLimit->end_date, | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             precision : Precision::DAY(), | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |             boundaries: Boundaries::EXCLUDE_NONE() | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $days        = $limitPeriod->length(); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $amount      = bcdiv($budgetLimit->amount, (string) $days, 12); | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |         Log::debug( | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |             sprintf('Total amount for budget limit #%d is %s. Nr. of days is %d. Amount per day is %s', $budgetLimit->id, $budgetLimit->amount, $days, $amount) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         return $amount; | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function deleted(Deleted $event): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-18 19:34:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function updated(Updated $event): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-28 13:36:25 +01:00
										 |  |  |         Log::debug(sprintf('BudgetLimitHandler::updated(#%s)', $event->budgetLimit->id)); | 
					
						
							| 
									
										
										
										
											2025-08-18 19:34:29 +02:00
										 |  |  |         self::updateAvailableBudget($event->budgetLimit); | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  | } |