| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * AvailableBudgetTransformer.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:57:18 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +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. | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +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. | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +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/>. | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Transformers; | 
					
						
							| 
									
										
										
										
											2021-04-06 13:30:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  | use FireflyIII\Models\AvailableBudget; | 
					
						
							| 
									
										
										
										
											2018-12-22 06:40:25 +01:00
										 |  |  | use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2019-08-30 09:13:10 +02:00
										 |  |  | use FireflyIII\Repositories\Budget\NoBudgetRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2019-08-30 08:19:55 +02:00
										 |  |  | use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Class AvailableBudgetTransformer | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-12-16 13:55:19 +01:00
										 |  |  | class AvailableBudgetTransformer extends AbstractTransformer | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     private NoBudgetRepositoryInterface   $noBudgetRepository; | 
					
						
							| 
									
										
										
										
											2021-03-06 07:20:49 +01:00
										 |  |  |     private OperationsRepositoryInterface $opsRepository; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     private BudgetRepositoryInterface     $repository; | 
					
						
							| 
									
										
										
										
											2018-12-22 06:40:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * CurrencyTransformer constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-12-16 13:55:19 +01:00
										 |  |  |     public function __construct() | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-30 09:13:10 +02:00
										 |  |  |         $this->repository         = app(BudgetRepositoryInterface::class); | 
					
						
							|  |  |  |         $this->opsRepository      = app(OperationsRepositoryInterface::class); | 
					
						
							|  |  |  |         $this->noBudgetRepository = app(NoBudgetRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Transform the note. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function transform(AvailableBudget $availableBudget): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-12-22 06:40:25 +01:00
										 |  |  |         $this->repository->setUser($availableBudget->user); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-12 20:30:25 +01:00
										 |  |  |         $currency = $availableBudget->transactionCurrency; | 
					
						
							|  |  |  |         $data     = [ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             'id'                      => (string) $availableBudget->id, | 
					
						
							| 
									
										
										
										
											2018-12-19 06:06:01 +01:00
										 |  |  |             'created_at'              => $availableBudget->created_at->toAtomString(), | 
					
						
							|  |  |  |             'updated_at'              => $availableBudget->updated_at->toAtomString(), | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             'currency_id'             => (string) $currency->id, | 
					
						
							| 
									
										
										
										
											2018-12-19 06:06:01 +01:00
										 |  |  |             'currency_code'           => $currency->code, | 
					
						
							|  |  |  |             'currency_symbol'         => $currency->symbol, | 
					
						
							| 
									
										
										
										
											2023-11-26 12:10:42 +01:00
										 |  |  |             'currency_decimal_places' => $currency->decimal_places, | 
					
						
							| 
									
										
										
										
											2022-12-27 21:13:18 +01:00
										 |  |  |             'amount'                  => app('steam')->bcround($availableBudget->amount, $currency->decimal_places), | 
					
						
							| 
									
										
										
										
											2021-04-01 21:06:40 +02:00
										 |  |  |             'start'                   => $availableBudget->start_date->toAtomString(), | 
					
						
							|  |  |  |             'end'                     => $availableBudget->end_date->endOfDay()->toAtomString(), | 
					
						
							| 
									
										
										
										
											2018-12-22 06:40:25 +01:00
										 |  |  |             'spent_in_budgets'        => [], | 
					
						
							|  |  |  |             'spent_no_budget'         => [], | 
					
						
							|  |  |  |             'links'                   => [ | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |                 [ | 
					
						
							|  |  |  |                     'rel' => 'self', | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |                     'uri' => '/available_budgets/'.$availableBudget->id, | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2018-12-22 06:40:25 +01:00
										 |  |  |         $start    = $this->parameters->get('start'); | 
					
						
							|  |  |  |         $end      = $this->parameters->get('end'); | 
					
						
							|  |  |  |         if (null !== $start && null !== $end) { | 
					
						
							|  |  |  |             $data['spent_in_budgets'] = $this->getSpentInBudgets(); | 
					
						
							| 
									
										
										
										
											2019-02-12 21:49:28 +01:00
										 |  |  |             $data['spent_no_budget']  = $this->spentOutsideBudgets(); | 
					
						
							| 
									
										
										
										
											2018-12-22 06:40:25 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-24 08:33:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-22 06:40:25 +01:00
										 |  |  |     private function getSpentInBudgets(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $allActive = $this->repository->getActiveBudgets(); | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |         $sums      = $this->opsRepository->sumExpenses($this->parameters->get('start'), $this->parameters->get('end'), null, $allActive); | 
					
						
							| 
									
										
										
										
											2019-02-12 21:49:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 13:30:09 +02:00
										 |  |  |         return array_values($sums); | 
					
						
							| 
									
										
										
										
											2018-12-22 06:40:25 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function spentOutsideBudgets(): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-06 13:30:09 +02:00
										 |  |  |         $sums = $this->noBudgetRepository->sumExpenses($this->parameters->get('start'), $this->parameters->get('end')); | 
					
						
							| 
									
										
										
										
											2018-12-22 06:40:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 13:30:09 +02:00
										 |  |  |         return array_values($sums); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |