Files
firefly-iii/app/Repositories/Budget/OperationsRepository.php

363 lines
16 KiB
PHP
Raw Normal View History

2019-08-29 21:33:12 +02:00
<?php
2019-08-29 21:33:12 +02:00
/**
* OperationsRepository.php
2020-02-16 14:00:57 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2019-08-29 21:33:12 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-08-29 21:33:12 +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.
2019-08-29 21:33:12 +02:00
*
* This program is distributed in the hope that it will be useful,
2019-08-29 21:33:12 +02:00
* 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.
2019-08-29 21:33:12 +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/>.
2019-08-29 21:33:12 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Repositories\Budget;
use Carbon\Carbon;
use Deprecated;
use FireflyIII\Enums\TransactionTypeEnum;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2022-03-28 12:24:16 +02:00
use FireflyIII\Models\Account;
2019-08-29 21:33:12 +02:00
use FireflyIII\Models\Budget;
2019-08-30 08:19:55 +02:00
use FireflyIII\Models\TransactionCurrency;
2022-03-28 12:24:16 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use FireflyIII\Support\Report\Summarizer\TransactionSummarizer;
2025-02-23 12:28:27 +01:00
use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface;
use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait;
use Illuminate\Support\Collection;
2024-12-24 06:34:12 +01:00
use Illuminate\Support\Facades\Log;
use Override;
2019-08-29 21:33:12 +02:00
/**
* Class OperationsRepository
*/
2025-02-23 12:28:27 +01:00
class OperationsRepository implements OperationsRepositoryInterface, UserGroupInterface
2019-08-29 21:33:12 +02:00
{
2025-02-23 12:28:27 +01:00
use UserGroupTrait;
2019-08-29 21:33:12 +02:00
/**
* A method that returns the amount of money budgeted per day for this budget,
* on average.
*/
public function budgetedPerDay(Budget $budget): string
{
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Now with budget #%d "%s"', $budget->id, $budget->name));
2019-08-29 21:33:12 +02:00
$total = '0';
$count = 0;
foreach ($budget->budgetlimits as $limit) {
$diff = (int) $limit->start_date->diffInDays($limit->end_date, true);
2019-08-29 21:33:12 +02:00
$diff = 0 === $diff ? 1 : $diff;
2023-11-05 19:41:37 +01:00
$amount = $limit->amount;
$perDay = bcdiv((string) $amount, (string) $diff);
2019-08-29 21:33:12 +02:00
$total = bcadd($total, $perDay);
2023-12-20 19:35:52 +01:00
++$count;
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Found %d budget limits. Per day is %s, total is %s', $count, $perDay, $total));
2019-08-29 21:33:12 +02:00
}
$avg = $total;
2019-08-29 21:33:12 +02:00
if ($count > 0) {
$avg = bcdiv($total, (string) $count);
2019-08-29 21:33:12 +02:00
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('%s / %d = %s = average.', $total, $count, $avg));
2019-08-29 21:33:12 +02:00
return $avg;
}
2019-08-30 08:12:15 +02:00
/**
* This method is being used to generate the budget overview in the year/multi-year report. Its used
* in both the year/multi-year budget overview AND in the accompanying chart.
*/
2025-06-14 10:18:58 +02:00
#[Deprecated]
2019-08-30 08:12:15 +02:00
public function getBudgetPeriodReport(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array
{
$carbonFormat = app('navigation')->preferredCarbonFormat($start, $end);
$data = [];
2023-12-20 19:35:52 +01:00
2019-08-30 08:12:15 +02:00
// get all transactions:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2019-08-30 08:12:15 +02:00
$collector->setAccounts($accounts)->setRange($start, $end);
$collector->setBudgets($budgets);
$journals = $collector->getExtractedJournals();
2019-08-30 08:12:15 +02:00
// loop transactions:
/** @var array $journal */
foreach ($journals as $journal) {
// prep data array for currency:
$budgetId = (int) $journal['budget_id'];
$budgetName = $journal['budget_name'];
$currencyId = (int) $journal['currency_id'];
$key = sprintf('%d-%d', $budgetId, $currencyId);
2019-08-30 08:12:15 +02:00
$data[$key] ??= [
2022-12-29 19:42:26 +01:00
'id' => $budgetId,
'name' => sprintf('%s (%s)', $budgetName, $journal['currency_name']),
'sum' => '0',
'currency_id' => $currencyId,
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_decimal_places' => $journal['currency_decimal_places'],
'entries' => [],
];
2019-08-30 08:12:15 +02:00
$date = $journal['date']->format($carbonFormat);
$data[$key]['entries'][$date] = bcadd($data[$key]['entries'][$date] ?? '0', (string) $journal['amount']);
2019-08-30 08:12:15 +02:00
}
return $data;
}
/**
* This method returns a list of all the withdrawal transaction journals (as arrays) set in that period
* which have the specified budget set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always negative.
*/
public function listExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $budgets = null): array
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2025-01-03 09:05:19 +01:00
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionTypeEnum::WITHDRAWAL->value]);
2025-05-27 17:06:15 +02:00
if ($accounts instanceof Collection && $accounts->count() > 0) {
$collector->setAccounts($accounts);
}
2025-05-27 17:06:15 +02:00
if ($budgets instanceof Collection && $budgets->count() > 0) {
$collector->setBudgets($budgets);
}
2025-05-27 17:06:15 +02:00
if (!$budgets instanceof Collection || 0 === $budgets->count()) {
$collector->setBudgets($this->getBudgets());
}
$collector->withBudgetInformation()->withAccountInformation()->withCategoryInformation();
$journals = $collector->getExtractedJournals();
$array = [];
2025-08-01 06:12:36 +02:00
// if needs conversion to primary.
$convertToPrimary = Amount::convertToPrimary($this->user);
$primaryCurrency = Amount::getPrimaryCurrencyByUserGroup($this->userGroup);
2025-08-01 06:12:36 +02:00
$currencyId = (int) $primaryCurrency->id;
$currencyCode = $primaryCurrency->code;
$currencyName = $primaryCurrency->name;
$currencySymbol = $primaryCurrency->symbol;
$currencyDecimalPlaces = $primaryCurrency->decimal_places;
$converter = new ExchangeRateConverter();
$currencies = [
2025-08-01 06:12:36 +02:00
$currencyId => $primaryCurrency,
];
foreach ($journals as $journal) {
$amount = app('steam')->negative($journal['amount']);
$journalCurrencyId = (int) $journal['currency_id'];
2025-08-01 06:12:36 +02:00
if (false === $convertToPrimary) {
$currencyId = $journalCurrencyId;
$currencyName = $journal['currency_name'];
$currencySymbol = $journal['currency_symbol'];
$currencyCode = $journal['currency_code'];
$currencyDecimalPlaces = $journal['currency_decimal_places'];
}
2025-08-01 06:12:36 +02:00
if (true === $convertToPrimary && $journalCurrencyId !== $currencyId) {
2025-09-07 14:49:49 +02:00
$currencies[$journalCurrencyId] ??= Amount::getTransactionCurrencyById($journalCurrencyId);
$amount = $converter->convert($currencies[$journalCurrencyId], $primaryCurrency, $journal['date'], $amount);
}
$budgetId = (int) $journal['budget_id'];
$budgetName = (string) $journal['budget_name'];
// catch "no budget" entries.
if (0 === $budgetId) {
continue;
}
// info about the currency:
$array[$currencyId] ??= [
2022-12-29 19:42:26 +01:00
'budgets' => [],
'currency_id' => $currencyId,
'currency_name' => $currencyName,
'currency_symbol' => $currencySymbol,
'currency_code' => $currencyCode,
'currency_decimal_places' => $currencyDecimalPlaces,
2022-12-29 19:42:26 +01:00
];
// info about the categories:
2023-12-10 06:45:59 +01:00
$array[$currencyId]['budgets'][$budgetId] ??= [
2022-12-29 19:42:26 +01:00
'id' => $budgetId,
'name' => $budgetName,
'transaction_journals' => [],
];
// add journal to array:
// only a subset of the fields.
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId]['budgets'][$budgetId]['transaction_journals'][$journalId] = [
'amount' => $amount,
'destination_account_id' => $journal['destination_account_id'],
'destination_account_name' => $journal['destination_account_name'],
2025-08-07 19:48:00 +02:00
'destination_account_type' => $journal['destination_account_type'],
'currency_id' => $journalCurrencyId,
'source_account_id' => $journal['source_account_id'],
'source_account_name' => $journal['source_account_name'],
2025-08-07 19:48:00 +02:00
'source_account_type' => $journal['source_account_type'],
'category_name' => $journal['category_name'],
'description' => $journal['description'],
'transaction_group_id' => $journal['transaction_group_id'],
'date' => $journal['date'],
];
}
return $array;
}
private function getBudgets(): Collection
{
/** @var BudgetRepositoryInterface $repos */
$repos = app(BudgetRepositoryInterface::class);
return $repos->getActiveBudgets();
}
2019-08-30 08:19:55 +02:00
/**
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.ExcessiveParameterList")
*/
2022-10-30 14:24:28 +01:00
public function sumExpenses(
2023-06-21 12:34:58 +02:00
Carbon $start,
Carbon $end,
?Collection $accounts = null,
?Collection $budgets = null,
2025-04-21 08:03:32 +02:00
?TransactionCurrency $currency = null,
2025-08-01 06:12:36 +02:00
bool $convertToPrimary = false
): array {
2025-08-01 06:12:36 +02:00
Log::debug(sprintf('Start of %s(date, date, array, array, "%s", %s).', __METHOD__, $currency?->code, var_export($convertToPrimary, true)));
2024-12-24 06:34:12 +01:00
// 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.
// 2024-12-24 disable the exclusion for now.
$repository = app(AccountRepositoryInterface::class);
2022-03-28 12:24:16 +02:00
$repository->setUser($this->user);
$subset = $repository->getAccountsByType(config('firefly.valid_liabilities'));
$selection = new Collection();
2022-03-28 12:24:16 +02:00
/** @var Account $account */
2022-03-29 16:42:10 +02:00
foreach ($subset as $account) {
2022-03-28 12:24:16 +02:00
if ('credit' === $repository->getMetaValue($account, 'liability_direction')) {
$selection->push($account);
}
}
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2022-03-28 12:24:16 +02:00
$collector->setUser($this->user)
->setRange($start, $end)
2024-12-24 10:29:07 +01:00
// ->excludeDestinationAccounts($selection)
->setTypes([TransactionTypeEnum::WITHDRAWAL->value])
;
2025-05-27 17:06:15 +02:00
if ($accounts instanceof Collection) {
$collector->setAccounts($accounts);
}
2025-05-27 17:06:15 +02:00
if (!$budgets instanceof Collection) {
$budgets = $this->getBudgets();
}
2025-05-27 17:06:15 +02:00
if ($currency instanceof TransactionCurrency) {
2025-04-21 08:03:32 +02:00
Log::debug(sprintf('Limit to normal currency %s', $currency->code));
$collector->setNormalCurrency($currency);
}
2025-04-22 20:41:08 +02:00
if ($budgets->count() > 0) {
$collector->setBudgets($budgets);
}
$journals = $collector->getExtractedJournals();
2020-10-01 12:48:27 +02:00
2024-12-24 06:34:12 +01:00
// same but for transactions in the foreign currency:
2025-05-27 17:06:15 +02:00
if ($currency instanceof TransactionCurrency) {
Log::debug('STOP looking for transactions in the foreign currency.');
2020-10-01 12:48:27 +02:00
}
$summarizer = new TransactionSummarizer($this->user);
2025-08-01 06:12:36 +02:00
// 2025-04-21 overrule "convertToPrimary" because in this particular view, we never want to do this.
$summarizer->setConvertToPrimary($convertToPrimary);
return $summarizer->groupByCurrencyId($journals, 'negative', false);
2021-03-12 06:20:01 +01:00
}
2025-08-03 07:53:36 +02:00
public function sumCollectedExpenses(array $expenses, Carbon $start, Carbon $end, TransactionCurrency $transactionCurrency, bool $convertToPrimary = false): array
{
Log::debug(sprintf('Start of %s.', __METHOD__));
$summarizer = new TransactionSummarizer($this->user);
$summarizer->setConvertToPrimary($convertToPrimary);
2025-08-03 20:17:50 +02:00
// filter $journals by range AND currency if it is present.
$expenses = array_filter($expenses, static function (array $expense) use ($start, $end, $transactionCurrency): bool {
2025-08-03 07:53:36 +02:00
return $expense['date']->between($start, $end) && $expense['currency_id'] === $transactionCurrency->id;
});
return $summarizer->groupByCurrencyId($expenses, 'negative', false);
}
public function sumCollectedExpensesByBudget(array $expenses, Budget $budget, bool $convertToPrimary = false): array
2025-08-03 20:17:50 +02:00
{
Log::debug(sprintf('Start of %s.', __METHOD__));
$summarizer = new TransactionSummarizer($this->user);
$summarizer->setConvertToPrimary($convertToPrimary);
// filter $journals by range AND currency if it is present.
$expenses = array_filter($expenses, static function (array $expense) use ($budget): bool {
return $expense['budget_id'] === $budget->id;
});
return $summarizer->groupByCurrencyId($expenses, 'negative', false);
}
#[Override]
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));
// 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.
// 2024-12-24 disable the exclusion for now.
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($this->user);
$subset = $repository->getAccountsByType(config('firefly.valid_liabilities'));
$selection = new Collection();
/** @var Account $account */
foreach ($subset as $account) {
if ('credit' === $repository->getMetaValue($account, 'liability_direction')) {
$selection->push($account);
}
}
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)
->setRange($start, $end)
// ->excludeDestinationAccounts($selection)
->setTypes([TransactionTypeEnum::WITHDRAWAL->value])
;
if ($accounts instanceof Collection) {
$collector->setAccounts($accounts);
}
if (!$budgets instanceof Collection) {
$budgets = $this->getBudgets();
}
if ($currency instanceof TransactionCurrency) {
Log::debug(sprintf('Limit to normal currency %s', $currency->code));
$collector->setNormalCurrency($currency);
}
if ($budgets->count() > 0) {
$collector->setBudgets($budgets);
}
return $collector->getExtractedJournals();
}
}