Fix another missing filter for #10803

This commit is contained in:
James Cole
2025-09-14 07:45:54 +02:00
parent 4aa911420a
commit 30df6684cb
2 changed files with 28 additions and 26 deletions

View File

@@ -314,7 +314,7 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
#[Override] #[Override]
public function collectExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $budgets = null, ?TransactionCurrency $currency = null): array public function collectExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $budgets = null, ?TransactionCurrency $currency = null): array
{ {
Log::debug(sprintf('Start of %s(date, date, array, array, "%s").', __METHOD__, $currency?->code)); Log::debug(sprintf('Start of %s(%s, %s, array, array, "%s").', __METHOD__, $start->toW3cString(), $end->toW3cString(), $currency?->code));
// this collector excludes all transfers TO liabilities (which are also withdrawals) // this collector excludes all transfers TO liabilities (which are also withdrawals)
// because those expenses only become expenses once they move from the liability to the friend. // because those expenses only become expenses once they move from the liability to the friend.
// 2024-12-24 disable the exclusion for now. // 2024-12-24 disable the exclusion for now.

View File

@@ -40,18 +40,18 @@ use Illuminate\Support\Facades\Log;
class BudgetLimitEnrichment implements EnrichmentInterface class BudgetLimitEnrichment implements EnrichmentInterface
{ {
private User $user; private User $user;
private UserGroup $userGroup; // @phpstan-ignore-line private UserGroup $userGroup; // @phpstan-ignore-line
private Collection $collection; private Collection $collection;
private array $ids = []; private array $ids = [];
private array $notes = []; private array $notes = [];
private Carbon $start; private Carbon $start;
private Carbon $end; private Carbon $end;
private array $expenses = []; private array $expenses = [];
private array $pcExpenses = []; private array $pcExpenses = [];
private array $currencyIds = []; private array $currencyIds = [];
private array $currencies = []; private array $currencies = [];
private bool $convertToPrimary = true; private bool $convertToPrimary = true;
private readonly TransactionCurrency $primaryCurrency; private readonly TransactionCurrency $primaryCurrency;
public function __construct() public function __construct()
@@ -95,8 +95,8 @@ class BudgetLimitEnrichment implements EnrichmentInterface
private function collectIds(): void private function collectIds(): void
{ {
$this->start = $this->collection->min('start_date') ?? Carbon::now()->startOfMonth(); $this->start = $this->collection->min('start_date') ?? Carbon::now()->startOfMonth();
$this->end = $this->collection->max('end_date') ?? Carbon::now()->endOfMonth(); $this->end = $this->collection->max('end_date') ?? Carbon::now()->endOfMonth();
/** @var BudgetLimit $limit */ /** @var BudgetLimit $limit */
foreach ($this->collection as $limit) { foreach ($this->collection as $limit) {
@@ -113,10 +113,9 @@ class BudgetLimitEnrichment implements EnrichmentInterface
private function collectNotes(): void private function collectNotes(): void
{ {
$notes = Note::query()->whereIn('noteable_id', $this->ids) $notes = Note::query()->whereIn('noteable_id', $this->ids)
->whereNotNull('notes.text') ->whereNotNull('notes.text')
->where('notes.text', '!=', '') ->where('notes.text', '!=', '')
->where('noteable_type', BudgetLimit::class)->get(['notes.noteable_id', 'notes.text'])->toArray() ->where('noteable_type', BudgetLimit::class)->get(['notes.noteable_id', 'notes.text'])->toArray();
;
foreach ($notes as $note) { foreach ($notes as $note) {
$this->notes[(int)$note['noteable_id']] = (string)$note['text']; $this->notes[(int)$note['noteable_id']] = (string)$note['text'];
} }
@@ -145,18 +144,19 @@ class BudgetLimitEnrichment implements EnrichmentInterface
private function collectBudgets(): void private function collectBudgets(): void
{ {
$budgetIds = $this->collection->pluck('budget_id')->unique()->toArray(); $budgetIds = $this->collection->pluck('budget_id')->unique()->toArray();
$budgets = Budget::whereIn('id', $budgetIds)->get(); $budgets = Budget::whereIn('id', $budgetIds)->get();
$repository = app(OperationsRepository::class); $repository = app(OperationsRepository::class);
$repository->setUser($this->user); $repository->setUser($this->user);
$expenses = $repository->collectExpenses($this->start, $this->end, null, $budgets, null); $expenses = $repository->collectExpenses($this->start, $this->end, null, $budgets, null);
/** @var BudgetLimit $budgetLimit */ /** @var BudgetLimit $budgetLimit */
foreach ($this->collection as $budgetLimit) { foreach ($this->collection as $budgetLimit) {
Log::debug(sprintf('Filtering expenses for budget limit #%d (budget #%d)', $budgetLimit->id, $budgetLimit->budget_id));
$id = (int)$budgetLimit->id; $id = (int)$budgetLimit->id;
$filteredExpenses = $this->filterToBudget($expenses, $budgetLimit->budget_id); $filteredExpenses = $this->filterToBudget($expenses, $budgetLimit->budget_id);
$filteredExpenses = $repository->sumCollectedExpenses($expenses, $budgetLimit->start_date, $budgetLimit->end_date, $budgetLimit->transactionCurrency, false); $filteredExpenses = $repository->sumCollectedExpenses($filteredExpenses, $budgetLimit->start_date, $budgetLimit->end_date, $budgetLimit->transactionCurrency, false);
$this->expenses[$id] = array_values($filteredExpenses); $this->expenses[$id] = array_values($filteredExpenses);
if (true === $this->convertToPrimary && $budgetLimit->transactionCurrency->id !== $this->primaryCurrency->id) { if (true === $this->convertToPrimary && $budgetLimit->transactionCurrency->id !== $this->primaryCurrency->id) {
@@ -180,13 +180,13 @@ class BudgetLimitEnrichment implements EnrichmentInterface
private function stringifyIds(): void private function stringifyIds(): void
{ {
$this->expenses = array_map(fn ($first) => array_map(function ($second) { $this->expenses = array_map(fn($first) => array_map(function ($second) {
$second['currency_id'] = (string)($second['currency_id'] ?? 0); $second['currency_id'] = (string)($second['currency_id'] ?? 0);
return $second; return $second;
}, $first), $this->expenses); }, $first), $this->expenses);
$this->pcExpenses = array_map(fn ($first) => array_map(function ($second) { $this->pcExpenses = array_map(fn($first) => array_map(function ($second) {
$second['currency_id'] = (string)($second['currency_id'] ?? 0); $second['currency_id'] = (string)($second['currency_id'] ?? 0);
return $second; return $second;
@@ -195,6 +195,8 @@ class BudgetLimitEnrichment implements EnrichmentInterface
private function filterToBudget(array $expenses, int $budget): array private function filterToBudget(array $expenses, int $budget): array
{ {
return array_filter($expenses, fn (array $item) => (int)$item['budget_id'] === $budget); $result = array_filter($expenses, fn(array $item) => (int)$item['budget_id'] === $budget);
Log::debug(sprintf('filterToBudget for budget #%d, from %d to %d items', $budget, count($expenses), count($result)));
return $result;
} }
} }