| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-23 11:26:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * BudgetLimitEnrichment.php | 
					
						
							|  |  |  |  * Copyright (c) 2025 james@firefly-iii.org | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-04 05:42:41 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  | namespace FireflyIII\Support\JsonApi\Enrichments; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use FireflyIII\Models\Budget; | 
					
						
							|  |  |  | use FireflyIII\Models\BudgetLimit; | 
					
						
							|  |  |  | use FireflyIII\Models\Note; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							|  |  |  | use FireflyIII\Models\UserGroup; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Budget\OperationsRepository; | 
					
						
							|  |  |  | use FireflyIII\Support\Facades\Amount; | 
					
						
							|  |  |  | use FireflyIII\User; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Model; | 
					
						
							|  |  |  | use Illuminate\Support\Collection; | 
					
						
							|  |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class BudgetLimitEnrichment implements EnrichmentInterface | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-09-14 07:45:54 +02:00
										 |  |  |     private Collection                   $collection; | 
					
						
							| 
									
										
										
										
											2025-10-05 12:57:58 +02:00
										 |  |  |     private bool                         $convertToPrimary; // @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2025-10-05 13:03:51 +02:00
										 |  |  |     private array                        $currencies  = []; | 
					
						
							|  |  |  |     private array                        $currencyIds = []; | 
					
						
							| 
									
										
										
										
											2025-09-14 07:45:54 +02:00
										 |  |  |     private Carbon                       $end; | 
					
						
							| 
									
										
										
										
											2025-10-05 13:03:51 +02:00
										 |  |  |     private array                        $expenses    = []; | 
					
						
							|  |  |  |     private array                        $ids         = []; | 
					
						
							|  |  |  |     private array                        $notes       = []; | 
					
						
							|  |  |  |     private array                        $pcExpenses  = []; | 
					
						
							| 
									
										
										
										
											2025-09-07 17:42:16 +02:00
										 |  |  |     private readonly TransactionCurrency $primaryCurrency; | 
					
						
							| 
									
										
										
										
											2025-09-26 06:05:37 +02:00
										 |  |  |     private Carbon                       $start; | 
					
						
							|  |  |  |     private User                         $user; | 
					
						
							|  |  |  |     private UserGroup                    $userGroup; | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->convertToPrimary = Amount::convertToPrimary(); | 
					
						
							|  |  |  |         $this->primaryCurrency  = Amount::getPrimaryCurrency(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function enrich(Collection $collection): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->collection = $collection; | 
					
						
							|  |  |  |         $this->collectIds(); | 
					
						
							| 
									
										
										
										
											2025-08-07 19:37:36 +02:00
										 |  |  |         $this->collectCurrencies(); | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  |         $this->collectNotes(); | 
					
						
							|  |  |  |         $this->collectBudgets(); | 
					
						
							| 
									
										
										
										
											2025-08-17 16:47:19 +02:00
										 |  |  |         $this->stringifyIds(); | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  |         $this->appendCollectedData(); | 
					
						
							| 
									
										
										
										
											2025-08-04 05:42:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  |         return $this->collection; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-26 19:43:39 +02:00
										 |  |  |     public function enrichSingle(array|Model $model): array|Model | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         Log::debug(__METHOD__); | 
					
						
							|  |  |  |         $collection = new Collection()->push($model); | 
					
						
							|  |  |  |         $collection = $this->enrich($collection); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $collection->first(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setUser(User $user): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->user      = $user; | 
					
						
							|  |  |  |         $this->userGroup = $user->userGroup; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setUserGroup(UserGroup $userGroup): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->userGroup = $userGroup; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function appendCollectedData(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->collection = $this->collection->map(function (BudgetLimit $item) { | 
					
						
							|  |  |  |             $id         = (int)$item->id; | 
					
						
							| 
									
										
										
										
											2025-08-07 19:37:36 +02:00
										 |  |  |             $currencyId = (int)$item->transaction_currency_id; | 
					
						
							|  |  |  |             if (0 === $currencyId) { | 
					
						
							|  |  |  |                 $currencyId = $this->primaryCurrency->id; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  |             $meta       = [ | 
					
						
							|  |  |  |                 'notes'    => $this->notes[$id] ?? null, | 
					
						
							|  |  |  |                 'spent'    => $this->expenses[$id] ?? [], | 
					
						
							|  |  |  |                 'pc_spent' => $this->pcExpenses[$id] ?? [], | 
					
						
							| 
									
										
										
										
											2025-08-07 19:37:36 +02:00
										 |  |  |                 'currency' => $this->currencies[$currencyId], | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  |             ]; | 
					
						
							|  |  |  |             $item->meta = $meta; | 
					
						
							| 
									
										
										
										
											2025-08-04 05:42:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  |             return $item; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function collectBudgets(): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-26 19:43:39 +02:00
										 |  |  |         $budgetIds  = $this->collection->pluck('budget_id')->unique()->toArray(); | 
					
						
							|  |  |  |         $budgets    = Budget::whereIn('id', $budgetIds)->get(); | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-13 07:18:32 +02:00
										 |  |  |         $repository = app(OperationsRepository::class); | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  |         $repository->setUser($this->user); | 
					
						
							| 
									
										
										
										
											2025-10-05 12:57:58 +02:00
										 |  |  |         $expenses   = $repository->collectExpenses($this->start, $this->end, null, $budgets); | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var BudgetLimit $budgetLimit */ | 
					
						
							|  |  |  |         foreach ($this->collection as $budgetLimit) { | 
					
						
							| 
									
										
										
										
											2025-09-14 07:45:54 +02:00
										 |  |  |             Log::debug(sprintf('Filtering expenses for budget limit #%d (budget #%d)', $budgetLimit->id, $budgetLimit->budget_id)); | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  |             $id                  = (int)$budgetLimit->id; | 
					
						
							| 
									
										
										
										
											2025-08-23 11:14:52 +02:00
										 |  |  |             $filteredExpenses    = $this->filterToBudget($expenses, $budgetLimit->budget_id); | 
					
						
							| 
									
										
										
										
											2025-10-05 12:57:58 +02:00
										 |  |  |             $filteredExpenses    = $repository->sumCollectedExpenses($filteredExpenses, $budgetLimit->start_date, $budgetLimit->end_date, $budgetLimit->transactionCurrency); | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  |             $this->expenses[$id] = array_values($filteredExpenses); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (true === $this->convertToPrimary && $budgetLimit->transactionCurrency->id !== $this->primaryCurrency->id) { | 
					
						
							|  |  |  |                 $pcFilteredExpenses    = $repository->sumCollectedExpenses($expenses, $budgetLimit->start_date, $budgetLimit->end_date, $budgetLimit->transactionCurrency, true); | 
					
						
							|  |  |  |                 $this->pcExpenses[$id] = array_values($pcFilteredExpenses); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (true === $this->convertToPrimary && $budgetLimit->transactionCurrency->id === $this->primaryCurrency->id) { | 
					
						
							| 
									
										
										
										
											2025-08-03 20:17:50 +02:00
										 |  |  |                 $this->pcExpenses[$id] = $this->expenses[$id] ?? []; | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-08-07 19:37:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private function collectCurrencies(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->currencies[$this->primaryCurrency->id] = $this->primaryCurrency; | 
					
						
							|  |  |  |         $currencies                                   = TransactionCurrency::whereIn('id', $this->currencyIds)->whereNot('id', $this->primaryCurrency->id)->get(); | 
					
						
							|  |  |  |         foreach ($currencies as $currency) { | 
					
						
							|  |  |  |             $this->currencies[(int)$currency->id] = $currency; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-08-17 16:47:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-26 06:05:37 +02:00
										 |  |  |     private function collectIds(): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-26 19:43:39 +02:00
										 |  |  |         $this->start       = $this->collection->min('start_date') ?? Carbon::now()->startOfMonth(); | 
					
						
							|  |  |  |         $this->end         = $this->collection->max('end_date') ?? Carbon::now()->endOfMonth(); | 
					
						
							| 
									
										
										
										
											2025-09-26 06:05:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var BudgetLimit $limit */ | 
					
						
							|  |  |  |         foreach ($this->collection as $limit) { | 
					
						
							|  |  |  |             $id          = (int)$limit->id; | 
					
						
							|  |  |  |             $this->ids[] = $id; | 
					
						
							|  |  |  |             if (0 !== (int)$limit->transaction_currency_id) { | 
					
						
							|  |  |  |                 $this->currencyIds[$id] = (int)$limit->transaction_currency_id; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $this->ids         = array_unique($this->ids); | 
					
						
							|  |  |  |         $this->currencyIds = array_unique($this->currencyIds); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function collectNotes(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $notes = Note::query()->whereIn('noteable_id', $this->ids) | 
					
						
							| 
									
										
										
										
											2025-09-26 19:43:39 +02:00
										 |  |  |             ->whereNotNull('notes.text') | 
					
						
							|  |  |  |             ->where('notes.text', '!=', '') | 
					
						
							|  |  |  |             ->where('noteable_type', BudgetLimit::class)->get(['notes.noteable_id', 'notes.text'])->toArray() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2025-09-26 06:05:37 +02:00
										 |  |  |         foreach ($notes as $note) { | 
					
						
							|  |  |  |             $this->notes[(int)$note['noteable_id']] = (string)$note['text']; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function filterToBudget(array $expenses, int $budget): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-26 19:43:39 +02:00
										 |  |  |         $result = array_filter($expenses, fn (array $item) => (int)$item['budget_id'] === $budget); | 
					
						
							| 
									
										
										
										
											2025-09-26 06:05:37 +02:00
										 |  |  |         Log::debug(sprintf('filterToBudget for budget #%d, from %d to %d items', $budget, count($expenses), count($result))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-17 16:47:19 +02:00
										 |  |  |     private function stringifyIds(): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-26 19:43:39 +02:00
										 |  |  |         $this->expenses   = array_map(fn ($first) => array_map(function ($second) { | 
					
						
							| 
									
										
										
										
											2025-09-07 17:42:16 +02:00
										 |  |  |             $second['currency_id'] = (string)($second['currency_id'] ?? 0); | 
					
						
							| 
									
										
										
										
											2025-08-17 16:56:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-07 17:42:16 +02:00
										 |  |  |             return $second; | 
					
						
							|  |  |  |         }, $first), $this->expenses); | 
					
						
							| 
									
										
										
										
											2025-08-17 16:47:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-26 19:43:39 +02:00
										 |  |  |         $this->pcExpenses = array_map(fn ($first) => array_map(function ($second) { | 
					
						
							| 
									
										
										
										
											2025-09-07 17:42:16 +02:00
										 |  |  |             $second['currency_id'] = (string)($second['currency_id'] ?? 0); | 
					
						
							| 
									
										
										
										
											2025-08-17 16:56:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-07 17:42:16 +02:00
										 |  |  |             return $second; | 
					
						
							|  |  |  |         }, $first), $this->expenses); | 
					
						
							| 
									
										
										
										
											2025-08-17 16:47:19 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-08-03 17:42:07 +02:00
										 |  |  | } |