| 
									
										
										
										
											2015-02-22 09:46:21 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * BudgetRepository.php | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * Copyright (c) 2017 thegrumpydictator@gmail.com | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * This file is part of Firefly III. | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * 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 | 
					
						
							| 
									
										
										
										
											2017-12-17 14:44:05 +01:00
										 |  |  |  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2015-02-22 09:46:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Repositories\Budget; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  | use Exception; | 
					
						
							|  |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2018-12-09 08:45:53 +01:00
										 |  |  | use FireflyIII\Factory\TransactionCurrencyFactory; | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  | use FireflyIII\Helpers\Collector\GroupCollectorInterface; | 
					
						
							| 
									
										
										
										
											2018-08-11 14:33:47 +02:00
										 |  |  | use FireflyIII\Helpers\Collector\TransactionCollectorInterface; | 
					
						
							| 
									
										
										
										
											2017-09-27 08:45:27 +02:00
										 |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2016-12-22 16:36:56 +01:00
										 |  |  | use FireflyIII\Models\AvailableBudget; | 
					
						
							| 
									
										
										
										
											2015-02-22 09:46:21 +01:00
										 |  |  | use FireflyIII\Models\Budget; | 
					
						
							|  |  |  | use FireflyIII\Models\BudgetLimit; | 
					
						
							| 
									
										
										
										
											2018-07-22 08:27:18 +02:00
										 |  |  | use FireflyIII\Models\RuleAction; | 
					
						
							|  |  |  | use FireflyIII\Models\RuleTrigger; | 
					
						
							| 
									
										
										
										
											2016-10-29 16:11:54 +02:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							| 
									
										
										
										
											2016-12-22 16:36:56 +01:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2015-12-09 22:39:50 -02:00
										 |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							| 
									
										
										
										
											2017-09-27 08:45:27 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2019-01-11 16:57:40 +01:00
										 |  |  | use FireflyIII\Services\Internal\Destroy\BudgetDestroyService; | 
					
						
							| 
									
										
										
										
											2016-03-03 08:44:20 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2016-10-06 21:18:43 +02:00
										 |  |  | use Illuminate\Database\Eloquent\Builder; | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2017-11-24 17:05:22 +01:00
										 |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2016-11-26 08:41:15 +01:00
										 |  |  | use Navigation; | 
					
						
							| 
									
										
										
										
											2015-02-22 09:46:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class BudgetRepository. | 
					
						
							| 
									
										
										
										
											2018-07-25 19:43:02 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @SuppressWarnings(PHPMD.CouplingBetweenObjects) | 
					
						
							|  |  |  |  * @SuppressWarnings(PHPMD.TooManyPublicMethods) | 
					
						
							|  |  |  |  * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) | 
					
						
							| 
									
										
										
										
											2015-02-22 09:46:21 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-05-06 06:15:46 +02:00
										 |  |  | class BudgetRepository implements BudgetRepositoryInterface | 
					
						
							| 
									
										
										
										
											2015-02-22 09:46:21 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-03 08:44:20 +01:00
										 |  |  |     /** @var User */ | 
					
						
							|  |  |  |     private $user; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-03 08:41:03 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-12-15 07:59:02 +01:00
										 |  |  |         if ('testing' === config('app.env')) { | 
					
						
							| 
									
										
										
										
											2018-09-03 08:41:03 +02:00
										 |  |  |             Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this))); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 06:46:37 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * A method that returns the amount of money budgeted per day for this budget, | 
					
						
							|  |  |  |      * on average. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function budgetedPerDay(Budget $budget): string | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |         Log::debug(sprintf('Now with budget #%d "%s"', $budget->id, $budget->name)); | 
					
						
							| 
									
										
										
										
											2018-03-24 06:46:37 +01:00
										 |  |  |         $total = '0'; | 
					
						
							|  |  |  |         $count = 0; | 
					
						
							|  |  |  |         foreach ($budget->budgetlimits as $limit) { | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |             $diff   = $limit->start_date->diffInDays($limit->end_date); | 
					
						
							|  |  |  |             $diff   = 0 === $diff ? 1 : $diff; | 
					
						
							| 
									
										
										
										
											2018-04-02 15:10:40 +02:00
										 |  |  |             $amount = (string)$limit->amount; | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |             $perDay = bcdiv($amount, (string)$diff); | 
					
						
							| 
									
										
										
										
											2018-03-24 06:46:37 +01:00
										 |  |  |             $total  = bcadd($total, $perDay); | 
					
						
							|  |  |  |             $count++; | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |             Log::debug(sprintf('Found %d budget limits. Per day is %s, total is %s', $count, $perDay, $total)); | 
					
						
							| 
									
										
										
										
											2018-03-24 06:46:37 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         $avg = $total; | 
					
						
							|  |  |  |         if ($count > 0) { | 
					
						
							| 
									
										
										
										
											2018-04-02 15:10:40 +02:00
										 |  |  |             $avg = bcdiv($total, (string)$count); | 
					
						
							| 
									
										
										
										
											2018-03-24 06:46:37 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |         Log::debug(sprintf('%s / %d = %s = average.', $total, $count, $avg)); | 
					
						
							| 
									
										
										
										
											2018-03-24 06:46:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $avg; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-05 11:49:06 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * This method collects various info on budgets, used on the budget page and on the index. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Collection $budgets | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							| 
									
										
										
										
											2017-10-05 11:49:06 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2018-07-23 21:49:15 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | 
					
						
							| 
									
										
										
										
											2017-10-05 11:49:06 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function collectBudgetInformation(Collection $budgets, Carbon $start, Carbon $end): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // get account information
 | 
					
						
							|  |  |  |         /** @var AccountRepositoryInterface $accountRepository */ | 
					
						
							|  |  |  |         $accountRepository = app(AccountRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2017-11-12 08:42:25 +01:00
										 |  |  |         $accounts          = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); | 
					
						
							| 
									
										
										
										
											2017-11-18 16:30:45 +01:00
										 |  |  |         $defaultCurrency   = app('amount')->getDefaultCurrency(); | 
					
						
							| 
									
										
										
										
											2017-10-05 11:49:06 +02:00
										 |  |  |         $return            = []; | 
					
						
							|  |  |  |         /** @var Budget $budget */ | 
					
						
							|  |  |  |         foreach ($budgets as $budget) { | 
					
						
							|  |  |  |             $budgetId          = $budget->id; | 
					
						
							|  |  |  |             $return[$budgetId] = [ | 
					
						
							| 
									
										
										
										
											2018-07-25 19:43:02 +02:00
										 |  |  |                 'spent'    => $this->spentInPeriod(new Collection([$budget]), $accounts, $start, $end), | 
					
						
							|  |  |  |                 'budgeted' => '0', | 
					
						
							| 
									
										
										
										
											2017-10-05 11:49:06 +02:00
										 |  |  |             ]; | 
					
						
							|  |  |  |             $budgetLimits      = $this->getBudgetLimits($budget, $start, $end); | 
					
						
							|  |  |  |             $otherLimits       = new Collection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // get all the budget limits relevant between start and end and examine them:
 | 
					
						
							|  |  |  |             /** @var BudgetLimit $limit */ | 
					
						
							|  |  |  |             foreach ($budgetLimits as $limit) { | 
					
						
							|  |  |  |                 if ($limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end) | 
					
						
							|  |  |  |                 ) { | 
					
						
							|  |  |  |                     $return[$budgetId]['currentLimit'] = $limit; | 
					
						
							| 
									
										
										
										
											2017-11-18 11:32:35 +01:00
										 |  |  |                     $return[$budgetId]['budgeted']     = round($limit->amount, $defaultCurrency->decimal_places); | 
					
						
							| 
									
										
										
										
											2017-10-05 11:49:06 +02:00
										 |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 // otherwise it's just one of the many relevant repetitions:
 | 
					
						
							|  |  |  |                 $otherLimits->push($limit); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $return[$budgetId]['otherLimits'] = $otherLimits; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Collection $budgets | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							|  |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         /** @var GroupCollectorInterface $collector */ | 
					
						
							|  |  |  |         $collector = app(GroupCollectorInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |         $collector->setUser($this->user); | 
					
						
							|  |  |  |         $collector->setRange($start, $end)->setBudgets($budgets)->withBudgetInformation(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($accounts->count() > 0) { | 
					
						
							|  |  |  |             $collector->setAccounts($accounts); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         return $collector->getSum(); | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							|  |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.ExcessiveMethodLength) | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (null === $end && null === $start) { | 
					
						
							|  |  |  |             return $budget->budgetlimits()->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (null === $end xor null === $start) { | 
					
						
							|  |  |  |             $query = $budget->budgetlimits()->orderBy('budget_limits.start_date', 'DESC'); | 
					
						
							|  |  |  |             // one of the two is null
 | 
					
						
							|  |  |  |             if (null !== $end) { | 
					
						
							|  |  |  |                 // end date must be before $end.
 | 
					
						
							|  |  |  |                 $query->where('end_date', '<=', $end->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (null !== $start) { | 
					
						
							|  |  |  |                 // start date must be after $start.
 | 
					
						
							|  |  |  |                 $query->where('start_date', '>=', $start->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $set = $query->get(['budget_limits.*']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $set; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // when both dates are set:
 | 
					
						
							|  |  |  |         $set = $budget->budgetlimits() | 
					
						
							|  |  |  |                       ->where( | 
					
						
							|  |  |  |                           function (Builder $q5) use ($start, $end) { | 
					
						
							|  |  |  |                               $q5->where( | 
					
						
							|  |  |  |                                   function (Builder $q1) use ($start, $end) { | 
					
						
							|  |  |  |                                       // budget limit ends within period
 | 
					
						
							|  |  |  |                                       $q1->where( | 
					
						
							|  |  |  |                                           function (Builder $q2) use ($start, $end) { | 
					
						
							|  |  |  |                                               $q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |                                               $q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |                                           } | 
					
						
							|  |  |  |                                       ) | 
					
						
							|  |  |  |                                           // budget limit start within period
 | 
					
						
							|  |  |  |                                          ->orWhere( | 
					
						
							|  |  |  |                                               function (Builder $q3) use ($start, $end) { | 
					
						
							|  |  |  |                                                   $q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |                                                   $q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |                                               } | 
					
						
							|  |  |  |                                           ); | 
					
						
							|  |  |  |                                   } | 
					
						
							|  |  |  |                               ) | 
					
						
							|  |  |  |                                  ->orWhere( | 
					
						
							|  |  |  |                                      function (Builder $q4) use ($start, $end) { | 
					
						
							|  |  |  |                                          // or start is before start AND end is after end.
 | 
					
						
							|  |  |  |                                          $q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |                                          $q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |                                      } | 
					
						
							|  |  |  |                                  ); | 
					
						
							|  |  |  |                           } | 
					
						
							|  |  |  |                       )->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $set; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-04-05 22:00:03 +02:00
										 |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-05 22:00:03 +02:00
										 |  |  |     public function destroy(Budget $budget): bool | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-01-11 16:57:40 +01:00
										 |  |  |         /** @var BudgetDestroyService $service */ | 
					
						
							|  |  |  |         $service = app(BudgetDestroyService::class); | 
					
						
							|  |  |  |         $service->destroy($budget); | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param AvailableBudget $availableBudget | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function destroyAvailableBudget(AvailableBudget $availableBudget): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $availableBudget->delete(); | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             Log::error(sprintf('Could not delete available budget: %s', $e->getMessage())); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 15:16:32 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Destroy a budget limit. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param BudgetLimit $budgetLimit | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function destroyBudgetLimit(BudgetLimit $budgetLimit): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $budgetLimit->delete(); | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             Log::info(sprintf('Could not delete budget limit: %s', $e->getMessage())); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param int|null $budgetId | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  |      * @param string|null $budgetName | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Budget|null | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  |     public function findBudget(?int $budgetId, ?string $budgetName): ?Budget | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         Log::debug('Now in findBudget()'); | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  |         Log::debug(sprintf('Searching for budget with ID #%d...', $budgetId)); | 
					
						
							|  |  |  |         $result = $this->findNull((int)$budgetId); | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |         if (null === $result && null !== $budgetName && '' !== $budgetName) { | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  |             Log::debug(sprintf('Searching for budget with name %s...', $budgetName)); | 
					
						
							|  |  |  |             $result = $this->findByName((string)$budgetName); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (null !== $result) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Found budget #%d: %s', $result->id, $result->name)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Log::debug(sprintf('Found result is null? %s', var_export(null === $result, true))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * Find a budget or return NULL | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param int $budgetId |null | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return Budget|null | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |     public function findNull(int $budgetId = null): ?Budget | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |         if (null === $budgetId) { | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |         return $this->user->budgets()->find($budgetId); | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 15:19:19 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * Find budget by name. | 
					
						
							| 
									
										
										
										
											2018-02-16 15:19:19 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param string|null $name | 
					
						
							| 
									
										
										
										
											2018-02-16 15:19:19 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return Budget|null | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |     public function findByName(?string $name): ?Budget | 
					
						
							| 
									
										
										
										
											2018-02-16 15:19:19 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |         if (null === $name) { | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |         $query = sprintf('%%%s%%', $name); | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |         return $this->user->budgets()->where('name', 'LIKE', $query)->first(); | 
					
						
							| 
									
										
										
										
											2016-07-24 18:47:55 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 10:32:26 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * This method returns the oldest journal or transaction date known to this budget. | 
					
						
							|  |  |  |      * Will cache result. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							| 
									
										
										
										
											2018-07-25 19:43:02 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-05-06 10:32:26 +02:00
										 |  |  |      * @return Carbon | 
					
						
							| 
									
										
										
										
											2018-07-25 19:43:02 +02:00
										 |  |  |      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.ExcessiveMethodLength) | 
					
						
							| 
									
										
										
										
											2016-05-06 10:32:26 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-07-23 21:49:15 +02:00
										 |  |  |     public function firstUseDate(Budget $budget): ?Carbon | 
					
						
							| 
									
										
										
										
											2016-05-06 10:32:26 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-07-23 21:49:15 +02:00
										 |  |  |         $oldest  = null; | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |         $journal = $budget->transactionJournals()->orderBy('date', 'ASC')->first(); | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (null !== $journal) { | 
					
						
							| 
									
										
										
										
											2016-05-06 10:32:26 +02:00
										 |  |  |             $oldest = $journal->date < $oldest ? $journal->date : $oldest; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $transaction = $budget | 
					
						
							|  |  |  |             ->transactions() | 
					
						
							|  |  |  |             ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.id') | 
					
						
							|  |  |  |             ->orderBy('transaction_journals.date', 'ASC')->first(['transactions.*', 'transaction_journals.date']); | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (null !== $transaction) { | 
					
						
							| 
									
										
										
										
											2016-05-11 17:17:43 +02:00
										 |  |  |             $carbon = new Carbon($transaction->date); | 
					
						
							|  |  |  |             $oldest = $carbon < $oldest ? $carbon : $oldest; | 
					
						
							| 
									
										
										
										
											2016-05-06 10:32:26 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $oldest; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-27 21:17:04 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-05 22:00:03 +02:00
										 |  |  |     public function getActiveBudgets(): Collection | 
					
						
							| 
									
										
										
										
											2015-12-27 21:17:04 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         /** @var Collection $set */ | 
					
						
							| 
									
										
										
										
											2018-10-17 15:18:09 +02:00
										 |  |  |         $set = $this->user->budgets()->where('active', 1) | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |                           ->orderBy('order', 'DESC') | 
					
						
							|  |  |  |                           ->orderBy('name', 'ASC') | 
					
						
							| 
									
										
										
										
											2018-10-17 15:18:09 +02:00
										 |  |  |                           ->get(); | 
					
						
							| 
									
										
										
										
											2015-12-27 21:17:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $set; | 
					
						
							| 
									
										
										
										
											2015-12-31 17:46:34 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-06 09:15:59 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-05-06 10:32:26 +02:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-04-06 09:15:59 +02:00
										 |  |  |      * @return Collection | 
					
						
							| 
									
										
										
										
											2018-07-25 19:43:02 +02:00
										 |  |  |      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.ExcessiveMethodLength) | 
					
						
							| 
									
										
										
										
											2015-04-06 09:15:59 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |     public function getAllBudgetLimits(Carbon $start = null, Carbon $end = null): Collection | 
					
						
							| 
									
										
										
										
											2015-04-06 09:15:59 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |         // both are NULL:
 | 
					
						
							|  |  |  |         if (null === $start && null === $end) { | 
					
						
							|  |  |  |             $set = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id') | 
					
						
							|  |  |  |                               ->with(['budget']) | 
					
						
							|  |  |  |                               ->where('budgets.user_id', $this->user->id) | 
					
						
							| 
									
										
										
										
											2018-09-26 20:42:07 +02:00
										 |  |  |                               ->whereNull('budgets.deleted_at') | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |                               ->get(['budget_limits.*']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $set; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // one of the two is NULL.
 | 
					
						
							|  |  |  |         if (null === $start xor null === $end) { | 
					
						
							|  |  |  |             $query = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id') | 
					
						
							|  |  |  |                                 ->with(['budget']) | 
					
						
							| 
									
										
										
										
											2018-09-26 20:42:07 +02:00
										 |  |  |                                 ->whereNull('budgets.deleted_at') | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |                                 ->where('budgets.user_id', $this->user->id); | 
					
						
							|  |  |  |             if (null !== $end) { | 
					
						
							|  |  |  |                 // end date must be before $end.
 | 
					
						
							|  |  |  |                 $query->where('end_date', '<=', $end->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (null !== $start) { | 
					
						
							|  |  |  |                 // start date must be after $start.
 | 
					
						
							|  |  |  |                 $query->where('start_date', '>=', $start->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $set = $query->get(['budget_limits.*']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $set; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // neither are NULL:
 | 
					
						
							| 
									
										
										
										
											2016-12-30 08:41:48 +01:00
										 |  |  |         $set = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id') | 
					
						
							|  |  |  |                           ->with(['budget']) | 
					
						
							|  |  |  |                           ->where('budgets.user_id', $this->user->id) | 
					
						
							| 
									
										
										
										
											2018-09-26 20:42:07 +02:00
										 |  |  |                           ->whereNull('budgets.deleted_at') | 
					
						
							| 
									
										
										
										
											2016-12-30 08:41:48 +01:00
										 |  |  |                           ->where( | 
					
						
							| 
									
										
										
										
											2016-12-30 09:02:48 +01:00
										 |  |  |                               function (Builder $q5) use ($start, $end) { | 
					
						
							|  |  |  |                                   $q5->where( | 
					
						
							|  |  |  |                                       function (Builder $q1) use ($start, $end) { | 
					
						
							|  |  |  |                                           $q1->where( | 
					
						
							|  |  |  |                                               function (Builder $q2) use ($start, $end) { | 
					
						
							|  |  |  |                                                   $q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |                                                   $q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |                                               } | 
					
						
							|  |  |  |                                           ) | 
					
						
							|  |  |  |                                              ->orWhere( | 
					
						
							|  |  |  |                                                  function (Builder $q3) use ($start, $end) { | 
					
						
							|  |  |  |                                                      $q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |                                                      $q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |                                                  } | 
					
						
							|  |  |  |                                              ); | 
					
						
							| 
									
										
										
										
											2016-12-30 08:41:48 +01:00
										 |  |  |                                       } | 
					
						
							|  |  |  |                                   ) | 
					
						
							|  |  |  |                                      ->orWhere( | 
					
						
							| 
									
										
										
										
											2016-12-30 09:02:48 +01:00
										 |  |  |                                          function (Builder $q4) use ($start, $end) { | 
					
						
							|  |  |  |                                              // or start is before start AND end is after end.
 | 
					
						
							|  |  |  |                                              $q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d 00:00:00')); | 
					
						
							|  |  |  |                                              $q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d 00:00:00')); | 
					
						
							| 
									
										
										
										
											2016-12-30 08:41:48 +01:00
										 |  |  |                                          } | 
					
						
							|  |  |  |                                      ); | 
					
						
							|  |  |  |                               } | 
					
						
							| 
									
										
										
										
											2016-12-30 09:02:48 +01:00
										 |  |  |                           )->get(['budget_limits.*']); | 
					
						
							| 
									
										
										
										
											2015-05-20 06:50:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 11:13:38 +02:00
										 |  |  |         return $set; | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-22 16:36:56 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param TransactionCurrency $currency | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							| 
									
										
										
										
											2016-12-22 16:36:56 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $amount          = '0'; | 
					
						
							|  |  |  |         $availableBudget = $this->user->availableBudgets() | 
					
						
							|  |  |  |                                       ->where('transaction_currency_id', $currency->id) | 
					
						
							| 
									
										
										
										
											2018-03-07 20:21:51 +01:00
										 |  |  |                                       ->where('start_date', $start->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                                       ->where('end_date', $end->format('Y-m-d 00:00:00'))->first(); | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (null !== $availableBudget) { | 
					
						
							| 
									
										
										
										
											2018-04-02 15:10:40 +02:00
										 |  |  |             $amount = (string)$availableBudget->amount; | 
					
						
							| 
									
										
										
										
											2016-12-22 16:36:56 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $amount; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |     /** @noinspection MoreThanThreeArgumentsInspection */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-18 05:38:23 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $return           = []; | 
					
						
							|  |  |  |         $availableBudgets = $this->user->availableBudgets() | 
					
						
							|  |  |  |                                        ->where('start_date', $start->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                                        ->where('end_date', $end->format('Y-m-d 00:00:00'))->get(); | 
					
						
							|  |  |  |         /** @var AvailableBudget $availableBudget */ | 
					
						
							|  |  |  |         foreach ($availableBudgets as $availableBudget) { | 
					
						
							|  |  |  |             $return[$availableBudget->transaction_currency_id] = $availableBudget->amount; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns all available budget objects. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAvailableBudgets(): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->user->availableBudgets()->get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 16:40:46 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Calculate the average amount in the budgets available in this period. | 
					
						
							|  |  |  |      * Grouped by day. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAverageAvailable(Carbon $start, Carbon $end): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Collection $list */ | 
					
						
							|  |  |  |         $list = $this->user->availableBudgets() | 
					
						
							|  |  |  |                            ->where('start_date', '>=', $start->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                            ->where('end_date', '<=', $end->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                            ->get(); | 
					
						
							|  |  |  |         if (0 === $list->count()) { | 
					
						
							|  |  |  |             return '0'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $total = '0'; | 
					
						
							|  |  |  |         $days  = 0; | 
					
						
							|  |  |  |         /** @var AvailableBudget $availableBudget */ | 
					
						
							|  |  |  |         foreach ($list as $availableBudget) { | 
					
						
							|  |  |  |             $total = bcadd($availableBudget->amount, $total); | 
					
						
							|  |  |  |             $days  += $availableBudget->start_date->diffInDays($availableBudget->end_date); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-22 18:50:27 +02:00
										 |  |  |         return bcdiv($total, (string)$days); | 
					
						
							| 
									
										
										
										
											2018-07-14 16:40:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-12-03 21:03:20 +01:00
										 |  |  |      * This method is being used to generate the budget overview in the year/multi-year report. Its used | 
					
						
							|  |  |  |      * in both the year/multi-year budget overview AND in the accompanying chart. | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param Collection $budgets | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-11-26 07:18:20 +01:00
										 |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-12-04 18:02:19 +01:00
										 |  |  |     public function getBudgetPeriodReport(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-11-26 08:41:15 +01:00
										 |  |  |         $carbonFormat = Navigation::preferredCarbonFormat($start, $end); | 
					
						
							| 
									
										
										
										
											2016-12-03 20:38:13 +01:00
										 |  |  |         $data         = []; | 
					
						
							|  |  |  |         // prep data array:
 | 
					
						
							|  |  |  |         /** @var Budget $budget */ | 
					
						
							|  |  |  |         foreach ($budgets as $budget) { | 
					
						
							|  |  |  |             $data[$budget->id] = [ | 
					
						
							|  |  |  |                 'name'    => $budget->name, | 
					
						
							|  |  |  |                 'sum'     => '0', | 
					
						
							|  |  |  |                 'entries' => [], | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-12-03 20:38:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // get all transactions:
 | 
					
						
							| 
									
										
										
										
											2018-08-11 14:33:47 +02:00
										 |  |  |         /** @var TransactionCollectorInterface $collector */ | 
					
						
							|  |  |  |         $collector = app(TransactionCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2016-12-03 20:38:13 +01:00
										 |  |  |         $collector->setAccounts($accounts)->setRange($start, $end); | 
					
						
							|  |  |  |         $collector->setBudgets($budgets); | 
					
						
							| 
									
										
										
										
											2018-08-11 14:33:47 +02:00
										 |  |  |         $transactions = $collector->getTransactions(); | 
					
						
							| 
									
										
										
										
											2016-11-26 07:18:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-03 20:38:13 +01:00
										 |  |  |         // loop transactions:
 | 
					
						
							|  |  |  |         /** @var Transaction $transaction */ | 
					
						
							| 
									
										
										
										
											2016-11-26 07:18:20 +01:00
										 |  |  |         foreach ($transactions as $transaction) { | 
					
						
							| 
									
										
										
										
											2018-04-02 15:10:40 +02:00
										 |  |  |             $budgetId                          = max((int)$transaction->transaction_journal_budget_id, (int)$transaction->transaction_budget_id); | 
					
						
							| 
									
										
										
										
											2016-12-03 20:38:13 +01:00
										 |  |  |             $date                              = $transaction->date->format($carbonFormat); | 
					
						
							|  |  |  |             $data[$budgetId]['entries'][$date] = bcadd($data[$budgetId]['entries'][$date] ?? '0', $transaction->transaction_amount); | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-12-03 21:03:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-26 07:18:20 +01:00
										 |  |  |         return $data; | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-05-06 10:32:26 +02:00
										 |  |  |     public function getBudgets(): Collection | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         /** @var Collection $set */ | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |         $set = $this->user->budgets()->orderBy('order', 'DESC') | 
					
						
							| 
									
										
										
										
											2019-05-04 20:58:43 +02:00
										 |  |  |                           ->orderBy('name', 'ASC')->get(); | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $set; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  |     /** @noinspection MoreThanThreeArgumentsInspection */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 20:35:14 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get all budgets with these ID's. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array $budgetIds | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getByIds(array $budgetIds): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->user->budgets()->whereIn('id', $budgetIds)->get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 10:32:26 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getInactiveBudgets(): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Collection $set */ | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |         $set = $this->user->budgets()->orderBy('order', 'DESC') | 
					
						
							| 
									
										
										
										
											2019-05-04 20:58:43 +02:00
										 |  |  |                           ->orderBy('name', 'ASC')->where('active', 0)->get(); | 
					
						
							| 
									
										
										
										
											2016-05-06 10:32:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $set; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-18 05:38:23 +01:00
										 |  |  |     /** @noinspection MoreThanThreeArgumentsInspection */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-04 18:02:19 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							| 
									
										
										
										
											2016-12-04 18:02:19 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getNoBudgetPeriodReport(Collection $accounts, Carbon $start, Carbon $end): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $carbonFormat = Navigation::preferredCarbonFormat($start, $end); | 
					
						
							| 
									
										
										
										
											2018-08-11 14:33:47 +02:00
										 |  |  |         /** @var TransactionCollectorInterface $collector */ | 
					
						
							|  |  |  |         $collector = app(TransactionCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2016-12-04 18:02:19 +01:00
										 |  |  |         $collector->setAccounts($accounts)->setRange($start, $end); | 
					
						
							|  |  |  |         $collector->setTypes([TransactionType::WITHDRAWAL]); | 
					
						
							|  |  |  |         $collector->withoutBudget(); | 
					
						
							| 
									
										
										
										
											2018-08-11 14:33:47 +02:00
										 |  |  |         $transactions = $collector->getTransactions(); | 
					
						
							| 
									
										
										
										
											2016-12-04 18:02:19 +01:00
										 |  |  |         $result       = [ | 
					
						
							|  |  |  |             'entries' => [], | 
					
						
							| 
									
										
										
										
											2018-04-02 15:10:40 +02:00
										 |  |  |             'name'    => (string)trans('firefly.no_budget'), | 
					
						
							| 
									
										
										
										
											2016-12-04 18:02:19 +01:00
										 |  |  |             'sum'     => '0', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($transactions as $transaction) { | 
					
						
							|  |  |  |             $date = $transaction->date->format($carbonFormat); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!isset($result['entries'][$date])) { | 
					
						
							|  |  |  |                 $result['entries'][$date] = '0'; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $result['entries'][$date] = bcadd($result['entries'][$date], $transaction->transaction_amount); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-02 14:12:09 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $query | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function searchBudget(string $query): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $query = sprintf('%%%s%%', $query); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->user->budgets()->where('name', 'LIKE', $query)->get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-22 16:36:56 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param TransactionCurrency $currency | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * @param string $amount | 
					
						
							| 
									
										
										
										
											2016-12-22 16:36:56 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |      * @return AvailableBudget | 
					
						
							| 
									
										
										
										
											2016-12-22 16:36:56 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |     public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): AvailableBudget | 
					
						
							| 
									
										
										
										
											2016-12-22 16:36:56 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $availableBudget = $this->user->availableBudgets() | 
					
						
							|  |  |  |                                       ->where('transaction_currency_id', $currency->id) | 
					
						
							| 
									
										
										
										
											2018-03-07 20:21:51 +01:00
										 |  |  |                                       ->where('start_date', $start->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                                       ->where('end_date', $end->format('Y-m-d 00:00:00'))->first(); | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (null === $availableBudget) { | 
					
						
							| 
									
										
										
										
											2016-12-22 16:36:56 +01:00
										 |  |  |             $availableBudget = new AvailableBudget; | 
					
						
							|  |  |  |             $availableBudget->user()->associate($this->user); | 
					
						
							|  |  |  |             $availableBudget->transactionCurrency()->associate($currency); | 
					
						
							| 
									
										
										
										
											2018-02-04 09:22:52 +01:00
										 |  |  |             $availableBudget->start_date = $start->format('Y-m-d 00:00:00'); | 
					
						
							|  |  |  |             $availableBudget->end_date   = $end->format('Y-m-d 00:00:00'); | 
					
						
							| 
									
										
										
										
											2016-12-22 16:36:56 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         $availableBudget->amount = $amount; | 
					
						
							|  |  |  |         $availableBudget->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |         return $availableBudget; | 
					
						
							| 
									
										
										
										
											2016-12-22 16:36:56 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 15:18:09 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param int $order | 
					
						
							| 
									
										
										
										
											2018-10-17 15:18:09 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function setBudgetOrder(Budget $budget, int $order): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $budget->order = $order; | 
					
						
							|  |  |  |         $budget->save(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-30 16:46:30 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param User $user | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-07-22 18:50:27 +02:00
										 |  |  |     public function setUser(User $user): void | 
					
						
							| 
									
										
										
										
											2017-01-30 16:46:30 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->user = $user; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 09:07:04 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Collection $budgets | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |      * TODO refactor me. | 
					
						
							| 
									
										
										
										
											2018-12-12 20:30:25 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function spentInPeriodMc(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |         /** @var GroupCollectorInterface $collector */ | 
					
						
							|  |  |  |         $collector = app(GroupCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2018-12-12 20:30:25 +01:00
										 |  |  |         $collector->setUser($this->user); | 
					
						
							|  |  |  |         $collector->setRange($start, $end)->setBudgets($budgets)->withBudgetInformation(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($accounts->count() > 0) { | 
					
						
							|  |  |  |             $collector->setAccounts($accounts); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |         // TODO possible candidate for getExtractedGroups
 | 
					
						
							|  |  |  |         $set        = $collector->getGroups(); | 
					
						
							| 
									
										
										
										
											2018-12-12 20:30:25 +01:00
										 |  |  |         $return     = []; | 
					
						
							|  |  |  |         $total      = []; | 
					
						
							|  |  |  |         $currencies = []; | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |         /** @var array $group */ | 
					
						
							|  |  |  |         foreach ($set as $group) { | 
					
						
							|  |  |  |             /** @var array $transaction */ | 
					
						
							|  |  |  |             foreach ($group['transactions'] as $transaction) { | 
					
						
							|  |  |  |                 $code = $transaction['currency_code']; | 
					
						
							|  |  |  |                 if (!isset($currencies[$code])) { | 
					
						
							|  |  |  |                     $currencies[$code] = [ | 
					
						
							|  |  |  |                         'id'             => $transaction['currency_id'], | 
					
						
							|  |  |  |                         'decimal_places' => $transaction['currency_decimal_places'], | 
					
						
							|  |  |  |                         'code'           => $transaction['currency_code'], | 
					
						
							|  |  |  |                         'symbol'         => $transaction['currency_symbol'], | 
					
						
							|  |  |  |                     ]; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 $total[$code] = isset($total[$code]) ? bcadd($total[$code], $transaction['amount']) : $transaction['amount']; | 
					
						
							| 
									
										
										
										
											2018-12-12 20:30:25 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |         /** | 
					
						
							|  |  |  |          * @var string $code | 
					
						
							|  |  |  |          * @var string $spent | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2018-12-12 20:30:25 +01:00
										 |  |  |         foreach ($total as $code => $spent) { | 
					
						
							|  |  |  |             /** @var TransactionCurrency $currency */ | 
					
						
							|  |  |  |             $currency = $currencies[$code]; | 
					
						
							|  |  |  |             $return[] = [ | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |                 'currency_id'             => $currency['id'], | 
					
						
							| 
									
										
										
										
											2018-12-19 19:02:16 +01:00
										 |  |  |                 'currency_code'           => $code, | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |                 'currency_symbol'         => $currency['symbol'], | 
					
						
							|  |  |  |                 'currency_decimal_places' => $currency['decimal_places'], | 
					
						
							|  |  |  |                 'amount'                  => round($spent, $currency['decimal_places']), | 
					
						
							| 
									
										
										
										
											2018-12-12 20:30:25 +01:00
										 |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-11 17:17:43 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							| 
									
										
										
										
											2016-05-11 17:17:43 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-01-05 09:10:04 +01:00
										 |  |  |     public function spentInPeriodWoBudget(Collection $accounts, Carbon $start, Carbon $end): string | 
					
						
							| 
									
										
										
										
											2016-05-11 17:17:43 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         /** @var GroupCollectorInterface $collector */ | 
					
						
							|  |  |  |         $collector = app(GroupCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2017-02-05 16:16:15 +01:00
										 |  |  |         $collector->setUser($this->user); | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         $collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL]) | 
					
						
							|  |  |  |                   ->withoutBudget(); | 
					
						
							| 
									
										
										
										
											2016-05-11 17:17:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($accounts->count() > 0) { | 
					
						
							| 
									
										
										
										
											2017-01-05 09:07:56 +01:00
										 |  |  |             $collector->setAccounts($accounts); | 
					
						
							| 
									
										
										
										
											2016-05-11 17:17:43 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-01-05 09:07:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         return $collector->getSum(); | 
					
						
							| 
									
										
										
										
											2016-05-06 06:15:46 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-22 06:40:25 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							| 
									
										
										
										
											2018-12-22 06:40:25 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var TransactionCollectorInterface $collector */ | 
					
						
							|  |  |  |         $collector = app(TransactionCollectorInterface::class); | 
					
						
							|  |  |  |         $collector->setUser($this->user); | 
					
						
							|  |  |  |         $collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withoutBudget(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($accounts->count() > 0) { | 
					
						
							|  |  |  |             $collector->setAccounts($accounts); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (0 === $accounts->count()) { | 
					
						
							|  |  |  |             $collector->setAllAssetAccounts(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-18 05:38:23 +01:00
										 |  |  |         $set        = $collector->getTransactions(); | 
					
						
							| 
									
										
										
										
											2018-12-22 06:40:25 +01:00
										 |  |  |         $return     = []; | 
					
						
							|  |  |  |         $total      = []; | 
					
						
							|  |  |  |         $currencies = []; | 
					
						
							|  |  |  |         /** @var Transaction $transaction */ | 
					
						
							|  |  |  |         foreach ($set as $transaction) { | 
					
						
							|  |  |  |             $code = $transaction->transaction_currency_code; | 
					
						
							|  |  |  |             if (!isset($currencies[$code])) { | 
					
						
							|  |  |  |                 $currencies[$code] = $transaction->transactionCurrency; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $total[$code] = isset($total[$code]) ? bcadd($total[$code], $transaction->transaction_amount) : $transaction->transaction_amount; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         foreach ($total as $code => $spent) { | 
					
						
							|  |  |  |             /** @var TransactionCurrency $currency */ | 
					
						
							|  |  |  |             $currency = $currencies[$code]; | 
					
						
							|  |  |  |             $return[] = [ | 
					
						
							|  |  |  |                 'currency_id'             => $currency->id, | 
					
						
							|  |  |  |                 'currency_code'           => $code, | 
					
						
							|  |  |  |                 'currency_symbol'         => $currency->symbol, | 
					
						
							|  |  |  |                 'currency_decimal_places' => $currency->decimal_places, | 
					
						
							|  |  |  |                 'amount'                  => round($spent, $currency->decimal_places), | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param array $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Budget | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-05 22:00:03 +02:00
										 |  |  |     public function store(array $data): Budget | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $newBudget = new Budget( | 
					
						
							|  |  |  |             [ | 
					
						
							| 
									
										
										
										
											2016-10-23 12:19:32 +02:00
										 |  |  |                 'user_id' => $this->user->id, | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |                 'name'    => $data['name'], | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $newBudget->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $newBudget; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |     /** @noinspection MoreThanThreeArgumentsInspection */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param array $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return BudgetLimit | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function storeBudgetLimit(array $data): BudgetLimit | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->cleanupBudgets(); | 
					
						
							|  |  |  |         /** @var Budget $budget */ | 
					
						
							|  |  |  |         $budget = $data['budget']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-09 08:45:53 +01:00
										 |  |  |         // if no currency has been provided, use the user's default currency:
 | 
					
						
							|  |  |  |         /** @var TransactionCurrencyFactory $factory */ | 
					
						
							|  |  |  |         $factory  = app(TransactionCurrencyFactory::class); | 
					
						
							|  |  |  |         $currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null); | 
					
						
							|  |  |  |         if (null === $currency) { | 
					
						
							|  |  |  |             $currency = app('amount')->getDefaultCurrencyByUser($this->user); | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-09 08:45:53 +01:00
										 |  |  |         // find limit with same date range.
 | 
					
						
							|  |  |  |         // if it exists, return that one.
 | 
					
						
							|  |  |  |         $limit = $budget->budgetlimits() | 
					
						
							|  |  |  |                         ->where('budget_limits.start_date', $data['start']->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                         ->where('budget_limits.end_date', $data['end']->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                         ->where('budget_limits.transaction_currency_id', $currency->id) | 
					
						
							|  |  |  |                         ->get(['budget_limits.*'])->first(); | 
					
						
							|  |  |  |         if (null !== $limit) { | 
					
						
							|  |  |  |             return $limit; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |         Log::debug('No existing budget limit, create a new one'); | 
					
						
							| 
									
										
										
										
											2018-12-09 08:45:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |         // or create one and return it.
 | 
					
						
							|  |  |  |         $limit = new BudgetLimit; | 
					
						
							|  |  |  |         $limit->budget()->associate($budget); | 
					
						
							| 
									
										
										
										
											2018-12-09 08:45:53 +01:00
										 |  |  |         $limit->start_date              = $data['start']->format('Y-m-d 00:00:00'); | 
					
						
							|  |  |  |         $limit->end_date                = $data['end']->format('Y-m-d 00:00:00'); | 
					
						
							|  |  |  |         $limit->amount                  = $data['amount']; | 
					
						
							|  |  |  |         $limit->transaction_currency_id = $currency->id; | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |         $limit->save(); | 
					
						
							|  |  |  |         Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $data['amount'])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $limit; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's 5.
 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function cleanupBudgets(): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // delete limits with amount 0:
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             BudgetLimit::where('amount', 0)->delete(); | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Could not delete budget limit: %s', $e->getMessage())); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Budget::where('order', 0)->update(['order' => 100]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // do the clean up by hand because Sqlite can be tricky with this.
 | 
					
						
							|  |  |  |         $budgetLimits = BudgetLimit::orderBy('created_at', 'DESC')->get(['id', 'budget_id', 'start_date', 'end_date']); | 
					
						
							|  |  |  |         $count        = []; | 
					
						
							|  |  |  |         /** @var BudgetLimit $budgetLimit */ | 
					
						
							|  |  |  |         foreach ($budgetLimits as $budgetLimit) { | 
					
						
							|  |  |  |             $key = $budgetLimit->budget_id . '-' . $budgetLimit->start_date->format('Y-m-d') . $budgetLimit->end_date->format('Y-m-d'); | 
					
						
							|  |  |  |             if (isset($count[$key])) { | 
					
						
							|  |  |  |                 // delete it!
 | 
					
						
							|  |  |  |                 try { | 
					
						
							|  |  |  |                     BudgetLimit::find($budgetLimit->id)->delete(); | 
					
						
							|  |  |  |                 } catch (Exception $e) { | 
					
						
							|  |  |  |                     Log::debug(sprintf('Could not delete budget limit: %s', $e->getMessage())); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $count[$key] = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param array $data | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return Budget | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-05 22:00:03 +02:00
										 |  |  |     public function update(Budget $budget, array $data): Budget | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-07-25 19:43:02 +02:00
										 |  |  |         $oldName        = $budget->name; | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         $budget->name   = $data['name']; | 
					
						
							|  |  |  |         $budget->active = $data['active']; | 
					
						
							|  |  |  |         $budget->save(); | 
					
						
							| 
									
										
										
										
											2018-07-25 19:43:02 +02:00
										 |  |  |         $this->updateRuleTriggers($oldName, $data['name']); | 
					
						
							|  |  |  |         $this->updateRuleActions($oldName, $data['name']); | 
					
						
							| 
									
										
										
										
											2018-07-22 08:27:18 +02:00
										 |  |  |         app('preferences')->mark(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         return $budget; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $oldName | 
					
						
							|  |  |  |      * @param string $newName | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function updateRuleTriggers(string $oldName, string $newName): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $types    = ['budget_is',]; | 
					
						
							|  |  |  |         $triggers = RuleTrigger::leftJoin('rules', 'rules.id', '=', 'rule_triggers.rule_id') | 
					
						
							|  |  |  |                                ->where('rules.user_id', $this->user->id) | 
					
						
							|  |  |  |                                ->whereIn('rule_triggers.trigger_type', $types) | 
					
						
							|  |  |  |                                ->where('rule_triggers.trigger_value', $oldName) | 
					
						
							|  |  |  |                                ->get(['rule_triggers.*']); | 
					
						
							|  |  |  |         Log::debug(sprintf('Found %d triggers to update.', $triggers->count())); | 
					
						
							|  |  |  |         /** @var RuleTrigger $trigger */ | 
					
						
							|  |  |  |         foreach ($triggers as $trigger) { | 
					
						
							|  |  |  |             $trigger->trigger_value = $newName; | 
					
						
							|  |  |  |             $trigger->save(); | 
					
						
							|  |  |  |             Log::debug(sprintf('Updated trigger %d: %s', $trigger->id, $trigger->trigger_value)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string $oldName | 
					
						
							|  |  |  |      * @param string $newName | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function updateRuleActions(string $oldName, string $newName): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $types   = ['set_budget',]; | 
					
						
							|  |  |  |         $actions = RuleAction::leftJoin('rules', 'rules.id', '=', 'rule_actions.rule_id') | 
					
						
							|  |  |  |                              ->where('rules.user_id', $this->user->id) | 
					
						
							|  |  |  |                              ->whereIn('rule_actions.action_type', $types) | 
					
						
							|  |  |  |                              ->where('rule_actions.action_value', $oldName) | 
					
						
							|  |  |  |                              ->get(['rule_actions.*']); | 
					
						
							|  |  |  |         Log::debug(sprintf('Found %d actions to update.', $actions->count())); | 
					
						
							|  |  |  |         /** @var RuleAction $action */ | 
					
						
							|  |  |  |         foreach ($actions as $action) { | 
					
						
							|  |  |  |             $action->action_value = $newName; | 
					
						
							|  |  |  |             $action->save(); | 
					
						
							|  |  |  |             Log::debug(sprintf('Updated action %d: %s', $action->id, $action->action_value)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param AvailableBudget $availableBudget | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param array $data | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return AvailableBudget | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function updateAvailableBudget(AvailableBudget $availableBudget, array $data): AvailableBudget | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $existing = $this->user->availableBudgets() | 
					
						
							|  |  |  |                                ->where('transaction_currency_id', $data['transaction_currency_id']) | 
					
						
							|  |  |  |                                ->where('start_date', $data['start_date']->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                                ->where('end_date', $data['end_date']->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                                ->where('id', '!=', $availableBudget->id) | 
					
						
							|  |  |  |                                ->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (null !== $existing) { | 
					
						
							|  |  |  |             throw new FireflyException(sprintf('An entry already exists for these parameters: available budget object with ID #%d', $existing->id)); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-12-21 16:38:10 +01:00
										 |  |  |         $availableBudget->transaction_currency_id = $data['currency_id']; | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |         $availableBudget->start_date              = $data['start_date']; | 
					
						
							|  |  |  |         $availableBudget->end_date                = $data['end_date']; | 
					
						
							|  |  |  |         $availableBudget->amount                  = $data['amount']; | 
					
						
							|  |  |  |         $availableBudget->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $availableBudget; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param BudgetLimit $budgetLimit | 
					
						
							| 
									
										
										
										
											2019-05-24 05:28:41 +02:00
										 |  |  |      * @param array $data | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return BudgetLimit | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function updateBudgetLimit(BudgetLimit $budgetLimit, array $data): BudgetLimit | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->cleanupBudgets(); | 
					
						
							|  |  |  |         /** @var Budget $budget */ | 
					
						
							|  |  |  |         $budget = $data['budget']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $budgetLimit->budget()->associate($budget); | 
					
						
							| 
									
										
										
										
											2018-12-09 08:45:53 +01:00
										 |  |  |         $budgetLimit->start_date = $data['start']->format('Y-m-d 00:00:00'); | 
					
						
							|  |  |  |         $budgetLimit->end_date   = $data['end']->format('Y-m-d 00:00:00'); | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |         $budgetLimit->amount     = $data['amount']; | 
					
						
							| 
									
										
										
										
											2018-12-09 08:45:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // if no currency has been provided, use the user's default currency:
 | 
					
						
							|  |  |  |         /** @var TransactionCurrencyFactory $factory */ | 
					
						
							|  |  |  |         $factory  = app(TransactionCurrencyFactory::class); | 
					
						
							|  |  |  |         $currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null); | 
					
						
							|  |  |  |         if (null === $currency) { | 
					
						
							|  |  |  |             $currency = app('amount')->getDefaultCurrencyByUser($this->user); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-12-21 16:38:10 +01:00
										 |  |  |         $currency->enabled = true; | 
					
						
							|  |  |  |         $currency->save(); | 
					
						
							| 
									
										
										
										
											2018-12-09 08:45:53 +01:00
										 |  |  |         $budgetLimit->transaction_currency_id = $currency->id; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |         $budgetLimit->save(); | 
					
						
							|  |  |  |         Log::debug(sprintf('Updated budget limit with ID #%d and amount %s', $budgetLimit->id, $data['amount'])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $budgetLimit; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							| 
									
										
										
										
											2017-11-18 11:32:35 +01:00
										 |  |  |      * @param string $amount | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |      * @return BudgetLimit|null | 
					
						
							| 
									
										
										
										
											2018-07-25 19:43:02 +02:00
										 |  |  |      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.ExcessiveMethodLength) | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |     public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $amount): ?BudgetLimit | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-02-19 20:32:33 +01:00
										 |  |  |         $this->cleanupBudgets(); | 
					
						
							| 
									
										
										
										
											2017-10-15 14:38:12 +02:00
										 |  |  |         // count the limits:
 | 
					
						
							|  |  |  |         $limits = $budget->budgetlimits() | 
					
						
							| 
									
										
										
										
											2018-02-04 09:22:52 +01:00
										 |  |  |                          ->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                          ->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00')) | 
					
						
							| 
									
										
										
										
											2017-10-15 14:38:12 +02:00
										 |  |  |                          ->get(['budget_limits.*'])->count(); | 
					
						
							| 
									
										
										
										
											2017-11-24 17:05:22 +01:00
										 |  |  |         Log::debug(sprintf('Found %d budget limits.', $limits)); | 
					
						
							| 
									
										
										
										
											2018-07-25 19:43:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-29 20:52:02 +01:00
										 |  |  |         // there might be a budget limit for these dates:
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         /** @var BudgetLimit $limit */ | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |         $limit = $budget->budgetlimits() | 
					
						
							| 
									
										
										
										
											2018-02-04 09:22:52 +01:00
										 |  |  |                         ->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                         ->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00')) | 
					
						
							| 
									
										
										
										
											2016-12-29 20:52:02 +01:00
										 |  |  |                         ->first(['budget_limits.*']); | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-15 14:38:12 +02:00
										 |  |  |         // if more than 1 limit found, delete the others:
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if ($limits > 1 && null !== $limit) { | 
					
						
							| 
									
										
										
										
											2017-11-24 17:05:22 +01:00
										 |  |  |             Log::debug(sprintf('Found more than 1, delete all except #%d', $limit->id)); | 
					
						
							| 
									
										
										
										
											2017-10-15 14:38:12 +02:00
										 |  |  |             $budget->budgetlimits() | 
					
						
							| 
									
										
										
										
											2018-02-04 09:22:52 +01:00
										 |  |  |                    ->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                    ->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00')) | 
					
						
							| 
									
										
										
										
											2017-10-15 14:38:12 +02:00
										 |  |  |                    ->where('budget_limits.id', '!=', $limit->id)->delete(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |         // delete if amount is zero.
 | 
					
						
							| 
									
										
										
										
											2017-11-24 17:05:22 +01:00
										 |  |  |         // Returns 0 if the two operands are equal,
 | 
					
						
							|  |  |  |         // 1 if the left_operand is larger than the right_operand, -1 otherwise.
 | 
					
						
							|  |  |  |         if (null !== $limit && bccomp($amount, '0') <= 0) { | 
					
						
							|  |  |  |             Log::debug(sprintf('%s is zero, delete budget limit #%d', $amount, $limit->id)); | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |             try { | 
					
						
							|  |  |  |                 $limit->delete(); | 
					
						
							|  |  |  |             } catch (Exception $e) { | 
					
						
							|  |  |  |                 Log::debug(sprintf('Could not delete limit: %s', $e->getMessage())); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return null; | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         // update if exists:
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (null !== $limit) { | 
					
						
							| 
									
										
										
										
											2017-11-24 17:05:22 +01:00
										 |  |  |             Log::debug(sprintf('Existing budget limit is #%d, update this to amount %s', $limit->id, $amount)); | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |             $limit->amount = $amount; | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |             $limit->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |             return $limit; | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-24 17:05:22 +01:00
										 |  |  |         Log::debug('No existing budget limit, create a new one'); | 
					
						
							| 
									
										
										
										
											2016-12-29 20:52:02 +01:00
										 |  |  |         // or create one and return it.
 | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |         $limit = new BudgetLimit; | 
					
						
							|  |  |  |         $limit->budget()->associate($budget); | 
					
						
							| 
									
										
										
										
											2018-07-25 06:45:25 +02:00
										 |  |  |         $limit->start_date = $start->startOfDay(); | 
					
						
							|  |  |  |         $limit->end_date   = $end->startOfDay(); | 
					
						
							| 
									
										
										
										
											2016-12-29 20:52:02 +01:00
										 |  |  |         $limit->amount     = $amount; | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |         $limit->save(); | 
					
						
							| 
									
										
										
										
											2017-11-24 17:05:22 +01:00
										 |  |  |         Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $amount)); | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         return $limit; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-29 08:14:32 +02:00
										 |  |  | } |