| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * BudgetLimitController.php | 
					
						
							| 
									
										
										
										
											2020-01-31 07:32:04 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Http\Controllers\Budget; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  | use FireflyIII\Http\Controllers\Controller; | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  | use FireflyIII\Models\Budget; | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  | use FireflyIII\Models\BudgetLimit; | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  | use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2025-02-23 12:35:13 +01:00
										 |  |  | use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  | use FireflyIII\Support\Http\Controllers\DateCalculation; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  | use Illuminate\Contracts\View\Factory; | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  | use Illuminate\Http\JsonResponse; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  | use Illuminate\Http\RedirectResponse; | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  | use Illuminate\Routing\Redirector; | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  | use Illuminate\View\View; | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class BudgetLimitController | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class BudgetLimitController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  |     use DateCalculation; | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 06:49:22 +02:00
										 |  |  |     private BudgetLimitRepositoryInterface $blRepository; | 
					
						
							| 
									
										
										
										
											2020-10-23 06:15:56 +02:00
										 |  |  |     private CurrencyRepositoryInterface    $currencyRepos; | 
					
						
							|  |  |  |     private OperationsRepositoryInterface  $opsRepository; | 
					
						
							|  |  |  |     private BudgetRepositoryInterface      $repository; | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * AmountController constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							|  |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |                 app('view')->share('title', (string) trans('firefly.budgets')); | 
					
						
							| 
									
										
										
										
											2020-05-01 17:29:50 -03:00
										 |  |  |                 app('view')->share('mainTitleIcon', 'fa-pie-chart'); | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |                 $this->repository    = app(BudgetRepositoryInterface::class); | 
					
						
							|  |  |  |                 $this->opsRepository = app(OperationsRepositoryInterface::class); | 
					
						
							|  |  |  |                 $this->blRepository  = app(BudgetLimitRepositoryInterface::class); | 
					
						
							|  |  |  |                 $this->currencyRepos = app(CurrencyRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |      * @return Factory|View | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function create(Budget $budget, Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-31 08:00:44 +01:00
										 |  |  |         $collection   = $this->currencyRepos->get(); | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  |         $budgetLimits = $this->blRepository->getBudgetLimits($budget, $start, $end); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |         // remove already budgeted currencies with the same date range
 | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $currencies   = $collection->filter( | 
					
						
							| 
									
										
										
										
											2023-04-26 11:31:46 +02:00
										 |  |  |             static function (TransactionCurrency $currency) use ($budgetLimits, $start, $end) { | 
					
						
							|  |  |  |                 /** @var BudgetLimit $limit */ | 
					
						
							|  |  |  |                 foreach ($budgetLimits as $limit) { | 
					
						
							|  |  |  |                     if ($limit->transaction_currency_id === $currency->id && $limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end) | 
					
						
							|  |  |  |                     ) { | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  |                         return false; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |         return view('budgets.budget-limits.create', compact('start', 'end', 'currencies', 'budget')); | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |      * @return Redirector|RedirectResponse | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     public function delete(BudgetLimit $budgetLimit) | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $this->blRepository->destroyBudgetLimit($budgetLimit); | 
					
						
							|  |  |  |         session()->flash('success', trans('firefly.deleted_bl')); | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         return redirect(route('budgets.index')); | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @return Factory|View | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function edit(BudgetLimit $budgetLimit) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $notes = $this->blRepository->getNoteText($budgetLimit); | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |         return view('budgets.budget-limits.edit', compact('budgetLimit', 'notes')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |      * @return Factory|View | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     public function show(BudgetLimit $budgetLimit) | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $notes = $this->blRepository->getNoteText($budgetLimit); | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         return view('budgets.budget-limits.show', compact('budgetLimit', 'notes')); | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-31 13:32:42 +01:00
										 |  |  |      * TODO why redirect AND json response? | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-10-23 06:15:56 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |     public function store(Request $request): JsonResponse|RedirectResponse | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Going to store new budget-limit.', $request->all()); | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  |         // first search for existing one and update it if necessary.
 | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |         $currency = $this->currencyRepos->find((int) $request->get('transaction_currency_id')); | 
					
						
							|  |  |  |         $budget   = $this->repository->find((int) $request->get('budget_id')); | 
					
						
							| 
									
										
										
										
											2025-05-24 05:40:20 +02:00
										 |  |  |         if (!$currency instanceof TransactionCurrency || !$budget instanceof Budget) { | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  |             throw new FireflyException('No valid currency or budget.'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $start    = Carbon::createFromFormat('Y-m-d', $request->get('start')); | 
					
						
							|  |  |  |         $end      = Carbon::createFromFormat('Y-m-d', $request->get('end')); | 
					
						
							| 
									
										
										
										
											2023-11-26 12:10:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-24 05:40:20 +02:00
										 |  |  |         if (!$start instanceof Carbon || !$end instanceof Carbon) { | 
					
						
							| 
									
										
										
										
											2025-10-05 12:57:58 +02:00
										 |  |  |             return response()->json(); | 
					
						
							| 
									
										
										
										
											2023-11-26 12:10:42 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $amount   = (string) $request->get('amount'); | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  |         $start->startOfDay(); | 
					
						
							| 
									
										
										
										
											2020-10-01 18:43:41 +02:00
										 |  |  |         $end->startOfDay(); | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-13 06:26:51 +02:00
										 |  |  |         if ('' === $amount) { | 
					
						
							| 
									
										
										
										
											2025-10-05 12:57:58 +02:00
										 |  |  |             return response()->json(); | 
					
						
							| 
									
										
										
										
											2021-04-13 06:26:51 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Start: %s, end: %s', $start->format('Y-m-d'), $end->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $limit    = $this->blRepository->find($budget, $currency, $start, $end); | 
					
						
							| 
									
										
										
										
											2022-08-23 05:46:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // sanity check on amount:
 | 
					
						
							| 
									
										
										
										
											2022-12-24 05:06:39 +01:00
										 |  |  |         if (0 === bccomp($amount, '0')) { | 
					
						
							| 
									
										
										
										
											2025-05-24 05:40:20 +02:00
										 |  |  |             if ($limit instanceof BudgetLimit) { | 
					
						
							| 
									
										
										
										
											2022-10-01 16:35:29 +02:00
										 |  |  |                 $this->blRepository->destroyBudgetLimit($limit); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-01 16:35:29 +02:00
										 |  |  |             // return empty=ish array:
 | 
					
						
							| 
									
										
										
										
											2025-10-05 12:57:58 +02:00
										 |  |  |             return response()->json(); | 
					
						
							| 
									
										
										
										
											2022-08-23 05:46:43 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |         if ((int) $amount > 268435456) { // intentional cast to integer
 | 
					
						
							| 
									
										
										
										
											2022-10-08 06:05:59 +02:00
										 |  |  |             $amount = '268435456'; | 
					
						
							| 
									
										
										
										
											2022-08-23 05:46:43 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-24 05:06:39 +01:00
										 |  |  |         if (-1 === bccomp($amount, '0')) { | 
					
						
							| 
									
										
										
										
											2022-10-18 20:37:33 +02:00
										 |  |  |             $amount = bcmul($amount, '-1'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-08-23 05:46:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-24 05:40:20 +02:00
										 |  |  |         if ($limit instanceof BudgetLimit) { | 
					
						
							| 
									
										
										
										
											2021-04-13 06:26:51 +02:00
										 |  |  |             $limit->amount = $amount; | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  |             $limit->save(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-05-24 05:40:20 +02:00
										 |  |  |         if (!$limit instanceof BudgetLimit) { | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  |             $limit = $this->blRepository->store( | 
					
						
							|  |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2020-10-23 06:15:56 +02:00
										 |  |  |                     'budget_id'   => $request->get('budget_id'), | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |                     'currency_id' => (int) $request->get('transaction_currency_id'), | 
					
						
							| 
									
										
										
										
											2020-10-23 06:15:56 +02:00
										 |  |  |                     'start_date'  => $start, | 
					
						
							|  |  |  |                     'end_date'    => $end, | 
					
						
							| 
									
										
										
										
											2021-04-13 06:26:51 +02:00
										 |  |  |                     'amount'      => $amount, | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  |                 ] | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |         // parse notes, if any.
 | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $notes    = (string) $request->get('notes'); | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |         $this->blRepository->setNoteText($limit, $notes); | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($request->expectsJson()) { | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |             $array                           = $limit->toArray(); | 
					
						
							| 
									
										
										
										
											2022-10-01 16:35:29 +02:00
										 |  |  |             // add some extra metadata:
 | 
					
						
							| 
									
										
										
										
											2025-09-10 16:07:19 +02:00
										 |  |  |             $spentArr                        = $this->opsRepository->sumExpenses($limit->start_date, $limit->end_date, null, new Collection()->push($budget), $currency); | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |             $array['spent']                  = $spentArr[$currency->id]['sum'] ?? '0'; | 
					
						
							| 
									
										
										
										
											2025-05-04 13:47:00 +02:00
										 |  |  |             $array['left_formatted']         = app('amount')->formatAnything($limit->transactionCurrency, bcadd($array['spent'], (string) $array['amount'])); | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |             $array['amount_formatted']       = app('amount')->formatAnything($limit->transactionCurrency, $limit['amount']); | 
					
						
							|  |  |  |             $array['days_left']              = (string) $this->activeDaysLeft($start, $end); | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  |             // left per day:
 | 
					
						
							| 
									
										
										
										
											2025-05-04 13:47:00 +02:00
										 |  |  |             $array['left_per_day']           = 0 === bccomp('0', $array['days_left']) ? bcadd((string) $array['spent'], (string) $array['amount']) : bcdiv(bcadd((string) $array['spent'], (string) $array['amount']), $array['days_left']); | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // left per day formatted.
 | 
					
						
							|  |  |  |             $array['left_per_day_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $array['left_per_day']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |             // notes:
 | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |             $array['notes']                  = $this->blRepository->getNoteText($limit); | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 10:48:18 +02:00
										 |  |  |             return response()->json($array); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 06:26:38 +01:00
										 |  |  |         return redirect(route('budgets.index')); | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |     public function update(Request $request, BudgetLimit $budgetLimit): JsonResponse|RedirectResponse | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $amount                          = (string) $request->get('amount'); | 
					
						
							| 
									
										
										
										
											2021-04-13 06:26:51 +02:00
										 |  |  |         if ('' === $amount) { | 
					
						
							|  |  |  |             $amount = '0'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |         if ((int) $amount > 268435456) { // 268 million, intentional integer
 | 
					
						
							| 
									
										
										
										
											2024-01-06 14:23:20 +01:00
										 |  |  |             $amount = '268435456'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-08-23 05:46:59 +02:00
										 |  |  |         // sanity check on amount:
 | 
					
						
							| 
									
										
										
										
											2022-12-24 05:06:39 +01:00
										 |  |  |         if (0 === bccomp($amount, '0')) { | 
					
						
							| 
									
										
										
										
											2022-10-01 16:35:29 +02:00
										 |  |  |             $budgetId = $budgetLimit->budget_id; | 
					
						
							|  |  |  |             $currency = $budgetLimit->transactionCurrency; | 
					
						
							|  |  |  |             $this->blRepository->destroyBudgetLimit($budgetLimit); | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |             $array    = [ | 
					
						
							| 
									
										
										
										
											2022-10-01 16:35:29 +02:00
										 |  |  |                 'budget_id'               => $budgetId, | 
					
						
							|  |  |  |                 'left_formatted'          => app('amount')->formatAnything($currency, '0'), | 
					
						
							|  |  |  |                 'left_per_day_formatted'  => app('amount')->formatAnything($currency, '0'), | 
					
						
							|  |  |  |                 'transaction_currency_id' => $currency->id, | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-01 16:35:29 +02:00
										 |  |  |             return response()->json($array); | 
					
						
							| 
									
										
										
										
											2022-08-23 05:46:59 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-01-06 14:23:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-24 05:06:39 +01:00
										 |  |  |         if (-1 === bccomp($amount, '0')) { | 
					
						
							| 
									
										
										
										
											2022-10-18 20:37:33 +02:00
										 |  |  |             $amount = bcmul($amount, '-1'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $notes                           = (string) $request->get('notes'); | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         if (strlen($notes) > 32768) { | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |             $notes = substr($notes, 0, 32768); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 05:46:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $limit                           = $this->blRepository->update($budgetLimit, ['amount' => $amount, 'notes' => $notes]); | 
					
						
							| 
									
										
										
										
											2023-07-09 18:45:44 +02:00
										 |  |  |         app('preferences')->mark(); | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $array                           = $limit->toArray(); | 
					
						
							| 
									
										
										
										
											2019-09-01 11:13:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $spentArr                        = $this->opsRepository->sumExpenses( | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |             $limit->start_date, | 
					
						
							|  |  |  |             $limit->end_date, | 
					
						
							|  |  |  |             null, | 
					
						
							| 
									
										
										
										
											2025-09-10 16:07:19 +02:00
										 |  |  |             new Collection()->push($budgetLimit->budget), | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |             $budgetLimit->transactionCurrency | 
					
						
							| 
									
										
										
										
											2019-09-01 11:13:03 +02:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $daysLeft                        = $this->activeDaysLeft($limit->start_date, $limit->end_date); | 
					
						
							|  |  |  |         $array['spent']                  = $spentArr[$budgetLimit->transactionCurrency->id]['sum'] ?? '0'; | 
					
						
							| 
									
										
										
										
											2025-05-04 13:47:00 +02:00
										 |  |  |         $array['left_formatted']         = app('amount')->formatAnything($limit->transactionCurrency, bcadd($array['spent'], (string) $array['amount'])); | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $array['amount_formatted']       = app('amount')->formatAnything($limit->transactionCurrency, $limit['amount']); | 
					
						
							|  |  |  |         $array['days_left']              = (string) $daysLeft; | 
					
						
							| 
									
										
										
										
											2025-05-04 13:47:00 +02:00
										 |  |  |         $array['left_per_day']           = 0 === $daysLeft ? bcadd((string) $array['spent'], (string) $array['amount']) : bcdiv(bcadd((string) $array['spent'], (string) $array['amount']), $array['days_left']); | 
					
						
							| 
									
										
										
										
											2019-09-01 11:13:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // left per day formatted.
 | 
					
						
							| 
									
										
										
										
											2022-12-24 05:06:39 +01:00
										 |  |  |         $array['amount']                 = app('steam')->bcround($limit['amount'], $limit->transactionCurrency->decimal_places); | 
					
						
							| 
									
										
										
										
											2019-09-01 11:13:03 +02:00
										 |  |  |         $array['left_per_day_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $array['left_per_day']); | 
					
						
							| 
									
										
										
										
											2024-11-30 06:19:21 +01:00
										 |  |  |         if ('true' === $request->get('redirect')) { | 
					
						
							|  |  |  |             return redirect(route('budgets.index')); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 11:13:03 +02:00
										 |  |  |         return response()->json($array); | 
					
						
							| 
									
										
										
										
											2019-08-31 21:47:55 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  | } |