2025-08-03 07:12:06 +02:00
|
|
|
<?php
|
2025-08-03 16:45:49 +02:00
|
|
|
|
2025-08-03 07:12:06 +02:00
|
|
|
/*
|
|
|
|
* AvailableBudgetEnrichment.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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Support\JsonApi\Enrichments;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Models\AvailableBudget;
|
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
|
|
|
use FireflyIII\Models\UserGroup;
|
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Budget\NoBudgetRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
|
|
|
|
use FireflyIII\Support\Facades\Amount;
|
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2025-08-03 16:45:49 +02:00
|
|
|
use Override;
|
2025-08-03 07:12:06 +02:00
|
|
|
|
|
|
|
class AvailableBudgetEnrichment implements EnrichmentInterface
|
|
|
|
{
|
2025-08-03 07:53:36 +02:00
|
|
|
private User $user;
|
|
|
|
private UserGroup $userGroup;
|
|
|
|
private TransactionCurrency $primaryCurrency;
|
|
|
|
private bool $convertToPrimary = false;
|
|
|
|
private array $ids = [];
|
|
|
|
private Collection $collection;
|
|
|
|
private array $spentInBudgets = [];
|
|
|
|
private array $spentOutsideBudgets = [];
|
|
|
|
private array $pcSpentInBudgets = [];
|
|
|
|
private array $pcSpentOutsideBudgets = [];
|
2025-08-03 07:12:06 +02:00
|
|
|
private readonly NoBudgetRepositoryInterface $noBudgetRepository;
|
|
|
|
private readonly OperationsRepositoryInterface $opsRepository;
|
|
|
|
private readonly BudgetRepositoryInterface $repository;
|
|
|
|
|
|
|
|
|
2025-08-03 16:45:49 +02:00
|
|
|
private ?Carbon $start = null;
|
|
|
|
private ?Carbon $end = null;
|
2025-08-03 07:12:06 +02:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
2025-08-03 07:53:36 +02:00
|
|
|
$this->primaryCurrency = Amount::getPrimaryCurrency();
|
|
|
|
$this->convertToPrimary = Amount::convertToPrimary();
|
2025-08-03 07:12:06 +02:00
|
|
|
$this->noBudgetRepository = app(NoBudgetRepositoryInterface::class);
|
|
|
|
$this->opsRepository = app(OperationsRepositoryInterface::class);
|
|
|
|
$this->repository = app(BudgetRepositoryInterface::class);
|
|
|
|
}
|
|
|
|
|
2025-08-03 16:45:49 +02:00
|
|
|
#[Override]
|
|
|
|
public function enrich(Collection $collection): Collection
|
2025-08-03 07:12:06 +02:00
|
|
|
{
|
|
|
|
$this->collection = $collection;
|
|
|
|
$this->collectIds();
|
|
|
|
$this->collectSpentInfo();
|
|
|
|
$this->appendCollectedData();
|
|
|
|
|
|
|
|
return $this->collection;
|
|
|
|
}
|
|
|
|
|
2025-08-03 16:45:49 +02:00
|
|
|
#[Override]
|
|
|
|
public function enrichSingle(array|Model $model): array|Model
|
2025-08-03 07:12:06 +02:00
|
|
|
{
|
|
|
|
Log::debug(__METHOD__);
|
|
|
|
$collection = new Collection([$model]);
|
|
|
|
$collection = $this->enrich($collection);
|
|
|
|
|
|
|
|
return $collection->first();
|
|
|
|
}
|
|
|
|
|
2025-08-03 16:45:49 +02:00
|
|
|
#[Override]
|
|
|
|
public function setUser(User $user): void
|
2025-08-03 07:12:06 +02:00
|
|
|
{
|
2025-08-03 07:53:36 +02:00
|
|
|
$this->user = $user;
|
2025-08-03 07:12:06 +02:00
|
|
|
$this->setUserGroup($user->userGroup);
|
|
|
|
}
|
|
|
|
|
2025-08-03 16:45:49 +02:00
|
|
|
#[Override]
|
|
|
|
public function setUserGroup(UserGroup $userGroup): void
|
2025-08-03 07:12:06 +02:00
|
|
|
{
|
|
|
|
$this->userGroup = $userGroup;
|
|
|
|
$this->noBudgetRepository->setUserGroup($userGroup);
|
|
|
|
$this->opsRepository->setUserGroup($userGroup);
|
|
|
|
$this->repository->setUserGroup($userGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function collectIds(): void
|
|
|
|
{
|
|
|
|
/** @var AvailableBudget $availableBudget */
|
|
|
|
foreach ($this->collection as $availableBudget) {
|
|
|
|
$this->ids[] = (int) $availableBudget->id;
|
|
|
|
}
|
|
|
|
$this->ids = array_unique($this->ids);
|
|
|
|
}
|
|
|
|
|
2025-08-03 07:53:36 +02:00
|
|
|
private function collectSpentInfo(): void
|
2025-08-03 07:12:06 +02:00
|
|
|
{
|
2025-08-03 07:53:36 +02:00
|
|
|
$start = $this->collection->min('start_date');
|
|
|
|
$end = $this->collection->max('end_date');
|
|
|
|
$allActive = $this->repository->getActiveBudgets();
|
|
|
|
$spentInBudgets = $this->opsRepository->collectExpenses($start, $end, null, $allActive, null);
|
|
|
|
$spentOutsideBudgets = $this->noBudgetRepository->collectExpenses($start, $end, null, null, null);
|
|
|
|
foreach ($this->collection as $availableBudget) {
|
2025-08-03 16:45:49 +02:00
|
|
|
$id = (int) $availableBudget->id;
|
|
|
|
$filteredSpentInBudgets = $this->opsRepository->sumCollectedExpenses($spentInBudgets, $availableBudget->start_date, $availableBudget->end_date, $availableBudget->transactionCurrency, false);
|
|
|
|
$filteredSpentOutsideBudgets = $this->opsRepository->sumCollectedExpenses($spentOutsideBudgets, $availableBudget->start_date, $availableBudget->end_date, $availableBudget->transactionCurrency, false);
|
2025-08-03 07:53:36 +02:00
|
|
|
$this->spentInBudgets[$id] = array_values($filteredSpentInBudgets);
|
|
|
|
$this->spentOutsideBudgets[$id] = array_values($filteredSpentOutsideBudgets);
|
|
|
|
|
|
|
|
if (true === $this->convertToPrimary) {
|
|
|
|
$pcFilteredSpentInBudgets = $this->opsRepository->sumCollectedExpenses($spentInBudgets, $availableBudget->start_date, $availableBudget->end_date, $availableBudget->transactionCurrency, true);
|
|
|
|
$pcFilteredSpentOutsideBudgets = $this->opsRepository->sumCollectedExpenses($spentOutsideBudgets, $availableBudget->start_date, $availableBudget->end_date, $availableBudget->transactionCurrency, true);
|
|
|
|
$this->pcSpentInBudgets[$id] = array_values($pcFilteredSpentInBudgets);
|
|
|
|
$this->pcSpentOutsideBudgets[$id] = array_values($pcFilteredSpentOutsideBudgets);
|
|
|
|
}
|
2025-08-03 07:12:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
// filter arrays on date.
|
|
|
|
// send them to sumCollection thing.
|
|
|
|
// save.
|
|
|
|
}
|
|
|
|
|
|
|
|
// first collect, then filter and append.
|
|
|
|
}
|
|
|
|
|
|
|
|
private function appendCollectedData(): void
|
|
|
|
{
|
|
|
|
$spentInsideBudgets = $this->spentInBudgets;
|
|
|
|
$spentOutsideBudgets = $this->spentOutsideBudgets;
|
|
|
|
$pcSpentInBudgets = $this->pcSpentInBudgets;
|
|
|
|
$pcSpentOutsideBudgets = $this->pcSpentOutsideBudgets;
|
|
|
|
$this->collection = $this->collection->map(function (AvailableBudget $item) use ($spentInsideBudgets, $spentOutsideBudgets, $pcSpentInBudgets, $pcSpentOutsideBudgets) {
|
|
|
|
$id = (int) $item->id;
|
|
|
|
$meta = [
|
|
|
|
'spent_in_budgets' => $spentInsideBudgets[$id] ?? [],
|
|
|
|
'pc_spent_in_budgets' => $pcSpentInBudgets[$id] ?? [],
|
2025-08-03 07:53:36 +02:00
|
|
|
|
|
|
|
'spent_outside_budgets' => $spentOutsideBudgets[$id] ?? [],
|
|
|
|
'pc_spent_outside_budgets' => $pcSpentOutsideBudgets[$id] ?? [],
|
2025-08-03 07:12:06 +02:00
|
|
|
];
|
|
|
|
$item->meta = $meta;
|
2025-08-03 16:45:49 +02:00
|
|
|
|
2025-08-03 07:12:06 +02:00
|
|
|
return $item;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|