| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * BudgetLimitTransformer.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:57:18 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +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 13:20:29 +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 13:20:29 +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 13:20:29 +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 13:20:29 +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 13:20:29 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Transformers; | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  | use FireflyIII\Models\BudgetLimit; | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2024-11-30 05:42:59 +01:00
										 |  |  | use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2020-11-20 06:24:08 +01:00
										 |  |  | use FireflyIII\Repositories\Budget\OperationsRepository; | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  | use FireflyIII\Support\Facades\Amount; | 
					
						
							| 
									
										
										
										
											2020-11-20 06:24:08 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2020-11-15 14:02:29 +01:00
										 |  |  | use League\Fractal\Resource\Item; | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class BudgetLimitTransformer | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-12-16 13:55:19 +01:00
										 |  |  | class BudgetLimitTransformer extends AbstractTransformer | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-17 16:44:24 +01:00
										 |  |  |     protected array $availableIncludes | 
					
						
							| 
									
										
										
										
											2020-11-15 14:02:29 +01:00
										 |  |  |         = [ | 
					
						
							|  |  |  |             'budget', | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  |     protected bool                $convertToNative; | 
					
						
							| 
									
										
										
										
											2025-05-04 17:41:26 +02:00
										 |  |  |     protected TransactionCurrency $default; | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-01-19 19:07:19 +01:00
										 |  |  |         $this->default         = Amount::getNativeCurrency(); | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  |         $this->convertToNative = Amount::convertToNative(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 14:02:29 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Include Budget | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Item | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function includeBudget(BudgetLimit $limit) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |         return $this->item($limit->budget, new BudgetTransformer(), 'budgets'); | 
					
						
							| 
									
										
										
										
											2020-11-15 14:02:29 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Transform the note. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function transform(BudgetLimit $budgetLimit): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |         $repository            = app(OperationsRepository::class); | 
					
						
							|  |  |  |         $limitRepos            = app(BudgetLimitRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2020-11-20 06:24:08 +01:00
										 |  |  |         $repository->setUser($budgetLimit->budget->user); | 
					
						
							| 
									
										
										
										
											2024-11-30 05:42:59 +01:00
										 |  |  |         $limitRepos->setUser($budgetLimit->budget->user); | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |         $expenses              = $repository->sumExpenses( | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |             $budgetLimit->start_date, | 
					
						
							|  |  |  |             $budgetLimit->end_date, | 
					
						
							|  |  |  |             null, | 
					
						
							|  |  |  |             new Collection([$budgetLimit->budget]), | 
					
						
							|  |  |  |             $budgetLimit->transactionCurrency | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2020-07-12 17:32:48 +02:00
										 |  |  |         $currency              = $budgetLimit->transactionCurrency; | 
					
						
							|  |  |  |         $amount                = $budgetLimit->amount; | 
					
						
							| 
									
										
										
										
											2024-11-30 05:42:59 +01:00
										 |  |  |         $notes                 = $limitRepos->getNoteText($budgetLimit); | 
					
						
							| 
									
										
										
										
											2020-07-12 17:32:48 +02:00
										 |  |  |         $currencyDecimalPlaces = 2; | 
					
						
							|  |  |  |         $currencyId            = null; | 
					
						
							|  |  |  |         $currencyName          = null; | 
					
						
							|  |  |  |         $currencyCode          = null; | 
					
						
							|  |  |  |         $currencySymbol        = null; | 
					
						
							| 
									
										
										
										
											2018-12-09 08:45:53 +01:00
										 |  |  |         if (null !== $currency) { | 
					
						
							| 
									
										
										
										
											2020-07-12 17:32:48 +02:00
										 |  |  |             $amount                = $budgetLimit->amount; | 
					
						
							| 
									
										
										
										
											2023-11-05 19:41:37 +01:00
										 |  |  |             $currencyId            = $currency->id; | 
					
						
							| 
									
										
										
										
											2020-07-12 17:32:48 +02:00
										 |  |  |             $currencyName          = $currency->name; | 
					
						
							|  |  |  |             $currencyCode          = $currency->code; | 
					
						
							|  |  |  |             $currencySymbol        = $currency->symbol; | 
					
						
							|  |  |  |             $currencyDecimalPlaces = $currency->decimal_places; | 
					
						
							| 
									
										
										
										
											2018-12-09 08:45:53 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |         $amount                = app('steam')->bcround($amount, $currencyDecimalPlaces); | 
					
						
							|  |  |  |         $default               = $this->default; | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  |         if (!$this->convertToNative) { | 
					
						
							|  |  |  |             $default = null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 14:02:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-23 19:11:25 +02:00
										 |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  |             'id'                             => (string) $budgetLimit->id, | 
					
						
							|  |  |  |             'created_at'                     => $budgetLimit->created_at->toAtomString(), | 
					
						
							|  |  |  |             'updated_at'                     => $budgetLimit->updated_at->toAtomString(), | 
					
						
							|  |  |  |             'start'                          => $budgetLimit->start_date->toAtomString(), | 
					
						
							|  |  |  |             'end'                            => $budgetLimit->end_date->endOfDay()->toAtomString(), | 
					
						
							|  |  |  |             'budget_id'                      => (string) $budgetLimit->budget_id, | 
					
						
							|  |  |  |             'currency_id'                    => (string) $currencyId, | 
					
						
							|  |  |  |             'currency_code'                  => $currencyCode, | 
					
						
							|  |  |  |             'currency_name'                  => $currencyName, | 
					
						
							|  |  |  |             'currency_decimal_places'        => $currencyDecimalPlaces, | 
					
						
							|  |  |  |             'currency_symbol'                => $currencySymbol, | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             'native_currency_id'             => $default instanceof TransactionCurrency ? (string) $default->id : null, | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  |             'native_currency_code'           => $default?->code, | 
					
						
							|  |  |  |             'native_currency_symbol'         => $default?->symbol, | 
					
						
							|  |  |  |             'native_currency_decimal_places' => $default?->decimal_places, | 
					
						
							|  |  |  |             'amount'                         => $amount, | 
					
						
							|  |  |  |             'native_amount'                  => $this->convertToNative ? app('steam')->bcround($budgetLimit->native_amount, $default->decimal_places) : null, | 
					
						
							|  |  |  |             'period'                         => $budgetLimit->period, | 
					
						
							|  |  |  |             'spent'                          => $expenses[$currencyId]['sum'] ?? '0', // will be in native if convertToNative.
 | 
					
						
							|  |  |  |             'notes'                          => '' === $notes ? null : $notes, | 
					
						
							|  |  |  |             'links'                          => [ | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |                 [ | 
					
						
							|  |  |  |                     'rel' => 'self', | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |                     'uri' => '/budgets/limits/'.$budgetLimit->id, | 
					
						
							| 
									
										
										
										
											2018-06-24 13:20:29 +02:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |