firefly-iii/app/Repositories/Account/OperationsRepository.php

483 lines
20 KiB
PHP
Raw Normal View History

2019-09-03 15:35:41 -05:00
<?php
/**
* OperationsRepository.php
2020-02-16 07:00:57 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2019-09-03 15:35:41 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-09-03 15:35:41 -05: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-09-03 15:35:41 -05:00
*
* This program is distributed in the hope that it will be useful,
2019-09-03 15:35:41 -05: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-09-03 15:35:41 -05: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-09-03 15:35:41 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2021-03-05 09:28:59 -06:00
use FireflyIII\Models\TransactionCurrency;
2019-09-03 15:35:41 -05:00
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
2023-02-19 01:43:28 -06:00
use Illuminate\Contracts\Auth\Authenticatable;
2019-09-03 15:35:41 -05:00
use Illuminate\Support\Collection;
/**
* Class OperationsRepository
*/
class OperationsRepository implements OperationsRepositoryInterface
{
2021-03-05 13:17:39 -06:00
private User $user;
2019-09-03 15:35:41 -05:00
/**
* This method returns a list of all the withdrawal transaction journals (as arrays) set in that period
* which have the specified accounts. 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): array
{
$journals = $this->getTransactions($start, $end, $accounts, TransactionType::WITHDRAWAL);
2019-09-03 15:35:41 -05:00
return $this->sortByCurrency($journals, 'negative');
2019-09-03 15:35:41 -05:00
}
2021-03-30 23:29:08 -05:00
/**
2023-06-21 05:34:58 -05:00
* Collect transactions with some parameters
2021-03-30 23:29:08 -05:00
*/
2023-06-21 05:34:58 -05:00
private function getTransactions(Carbon $start, Carbon $end, Collection $accounts, string $type): array
{
2023-06-21 05:34:58 -05:00
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)->setTypes([$type]);
$collector->setBothAccounts($accounts);
$collector->withCategoryInformation()->withAccountInformation()->withBudgetInformation()->withTagInformation();
2021-03-30 23:29:08 -05:00
2023-06-21 05:34:58 -05:00
return $collector->getExtractedJournals();
}
2021-03-30 23:29:08 -05:00
public function setUser(null|Authenticatable|User $user): void
{
if ($user instanceof User) {
$this->user = $user;
}
}
2023-06-21 05:34:58 -05:00
private function sortByCurrency(array $journals, string $direction): array
{
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$journalId = (int)$journal['transaction_journal_id'];
2023-12-09 23:45:59 -06:00
$array[$currencyId] ??= [
2023-06-21 05:34:58 -05:00
'currency_id' => $journal['currency_id'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
'transaction_journals' => [],
];
2023-05-29 06:56:55 -05:00
2023-06-21 05:34:58 -05:00
$array[$currencyId]['transaction_journals'][$journalId] = [
2023-12-20 12:35:52 -06:00
'amount' => app('steam')->{$direction}((string)$journal['amount']), // @phpstan-ignore-line
2023-06-21 05:34:58 -05:00
'date' => $journal['date'],
'transaction_journal_id' => $journalId,
'budget_name' => $journal['budget_name'],
'category_name' => $journal['category_name'],
'source_account_id' => $journal['source_account_id'],
'source_account_name' => $journal['source_account_name'],
'source_account_iban' => $journal['source_account_iban'],
'destination_account_id' => $journal['destination_account_id'],
'destination_account_name' => $journal['destination_account_name'],
'destination_account_iban' => $journal['destination_account_iban'],
'tags' => $journal['tags'],
'description' => $journal['description'],
'transaction_group_id' => $journal['transaction_group_id'],
];
}
2023-05-29 06:56:55 -05:00
2023-06-21 05:34:58 -05:00
return $array;
2023-05-29 06:56:55 -05:00
}
/**
* This method returns a list of all the deposit transaction journals (as arrays) set in that period
* which have the specified accounts. It's grouped per currency, with as few details in the array
* as possible. Amounts are always positive.
*/
public function listIncome(Carbon $start, Carbon $end, ?Collection $accounts = null): array
{
$journals = $this->getTransactions($start, $end, $accounts, TransactionType::DEPOSIT);
return $this->sortByCurrency($journals, 'positive');
}
/**
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function sumExpenses(
Carbon $start,
Carbon $end,
?Collection $accounts = null,
?Collection $expense = null,
?TransactionCurrency $currency = null
): array {
$journals = $this->getTransactionsForSum(TransactionType::WITHDRAWAL, $start, $end, $accounts, $expense, $currency);
return $this->groupByCurrency($journals, 'negative');
}
2023-05-29 06:56:55 -05:00
/**
2023-11-30 22:15:59 -06:00
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
2023-12-22 13:12:38 -06:00
* @SuppressWarnings(PHPMD.NPathComplexity)
2021-03-30 23:29:08 -05:00
*/
private function getTransactionsForSum(
2023-06-21 05:34:58 -05:00
string $type,
Carbon $start,
Carbon $end,
?Collection $accounts = null,
?Collection $opposing = null,
2022-10-30 08:24:28 -05:00
?TransactionCurrency $currency = null
2023-12-20 12:35:52 -06:00
): array {
2021-03-30 23:29:08 -05:00
$start->startOfDay();
$end->endOfDay();
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)->setTypes([$type])->withAccountInformation();
2021-03-30 23:29:08 -05:00
// depends on transaction type:
if (TransactionType::WITHDRAWAL === $type) {
2021-03-30 23:29:08 -05:00
if (null !== $accounts) {
$collector->setSourceAccounts($accounts);
}
if (null !== $opposing) {
$collector->setDestinationAccounts($opposing);
2021-03-30 23:29:08 -05:00
}
}
if (TransactionType::DEPOSIT === $type) {
if (null !== $accounts) {
$collector->setDestinationAccounts($accounts);
}
if (null !== $opposing) {
$collector->setSourceAccounts($opposing);
2021-03-30 23:29:08 -05:00
}
}
2021-05-24 01:06:56 -05:00
// supports only accounts, not opposing.
if (TransactionType::TRANSFER === $type && null !== $accounts) {
$collector->setAccounts($accounts);
2021-05-07 12:32:08 -05:00
}
2021-03-30 23:29:08 -05:00
if (null !== $currency) {
$collector->setCurrency($currency);
}
$journals = $collector->getExtractedJournals();
2021-03-30 23:29:08 -05:00
// same but for foreign currencies:
if (null !== $currency) {
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)->setTypes([$type])->withAccountInformation()
2023-12-20 12:35:52 -06:00
->setForeignCurrency($currency)
;
if (TransactionType::WITHDRAWAL === $type) {
if (null !== $accounts) {
$collector->setSourceAccounts($accounts);
}
if (null !== $opposing) {
$collector->setDestinationAccounts($opposing);
}
2021-03-30 23:29:08 -05:00
}
if (TransactionType::DEPOSIT === $type) {
if (null !== $accounts) {
$collector->setDestinationAccounts($accounts);
}
if (null !== $opposing) {
$collector->setSourceAccounts($opposing);
}
2021-03-30 23:29:08 -05:00
}
$result = $collector->getExtractedJournals();
2021-03-30 23:29:08 -05:00
// do not use array_merge because you want keys to overwrite (otherwise you get double results):
$journals = $result + $journals;
2021-03-30 23:29:08 -05:00
}
return $journals;
}
private function groupByCurrency(array $journals, string $direction): array
{
2021-03-30 23:29:08 -05:00
$array = [];
foreach ($journals as $journal) {
2022-12-29 12:42:26 -06:00
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] ??= [
2022-12-29 12:42:26 -06:00
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
2023-12-20 12:35:52 -06:00
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->{$direction}($journal['amount'])); // @phpstan-ignore-line
2021-03-30 23:29:08 -05:00
// also do foreign amount:
$foreignId = (int)$journal['foreign_currency_id'];
if (0 !== $foreignId) {
$array[$foreignId] ??= [
2022-12-29 12:42:26 -06:00
'sum' => '0',
'currency_id' => $foreignId,
'currency_name' => $journal['foreign_currency_name'],
'currency_symbol' => $journal['foreign_currency_symbol'],
'currency_code' => $journal['foreign_currency_code'],
'currency_decimal_places' => $journal['foreign_currency_decimal_places'],
];
2023-12-20 12:35:52 -06:00
$array[$foreignId]['sum'] = bcadd($array[$foreignId]['sum'], app('steam')->{$direction}($journal['foreign_amount'])); // @phpstan-ignore-line
2021-03-30 23:29:08 -05:00
}
}
return $array;
}
/**
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function sumExpensesByDestination(
Carbon $start,
Carbon $end,
?Collection $accounts = null,
?Collection $expense = null,
?TransactionCurrency $currency = null
): array {
$journals = $this->getTransactionsForSum(TransactionType::WITHDRAWAL, $start, $end, $accounts, $expense, $currency);
return $this->groupByDirection($journals, 'destination', 'negative');
}
private function groupByDirection(array $journals, string $direction, string $method): array
{
$array = [];
$idKey = sprintf('%s_account_id', $direction);
$nameKey = sprintf('%s_account_name', $direction);
2021-03-30 23:29:08 -05:00
foreach ($journals as $journal) {
$key = sprintf('%s-%s', $journal[$idKey], $journal['currency_id']);
$array[$key] ??= [
2022-12-29 12:42:26 -06:00
'id' => $journal[$idKey],
'name' => $journal[$nameKey],
'sum' => '0',
'currency_id' => $journal['currency_id'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
2023-12-20 12:35:52 -06:00
$array[$key]['sum'] = bcadd($array[$key]['sum'], app('steam')->{$method}((string)$journal['amount'])); // @phpstan-ignore-line
2021-03-30 23:29:08 -05:00
// also do foreign amount:
2022-12-29 12:42:26 -06:00
if (0 !== (int)$journal['foreign_currency_id']) {
$key = sprintf('%s-%s', $journal[$idKey], $journal['foreign_currency_id']);
$array[$key] ??= [
2022-12-29 12:42:26 -06:00
'id' => $journal[$idKey],
'name' => $journal[$nameKey],
'sum' => '0',
'currency_id' => $journal['foreign_currency_id'],
'currency_name' => $journal['foreign_currency_name'],
'currency_symbol' => $journal['foreign_currency_symbol'],
'currency_code' => $journal['foreign_currency_code'],
'currency_decimal_places' => $journal['foreign_currency_decimal_places'],
];
2023-12-20 12:35:52 -06:00
$array[$key]['sum'] = bcadd($array[$key]['sum'], app('steam')->{$method}((string)$journal['foreign_amount'])); // @phpstan-ignore-line
2021-03-30 23:29:08 -05:00
}
}
return $array;
}
2021-04-03 00:16:43 -05:00
/**
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function sumExpensesBySource(
Carbon $start,
Carbon $end,
?Collection $accounts = null,
?Collection $expense = null,
?TransactionCurrency $currency = null
): array {
$journals = $this->getTransactionsForSum(TransactionType::WITHDRAWAL, $start, $end, $accounts, $expense, $currency);
return $this->groupByDirection($journals, 'source', 'negative');
}
/**
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function sumIncome(
Carbon $start,
Carbon $end,
?Collection $accounts = null,
?Collection $revenue = null,
?TransactionCurrency $currency = null
): array {
$journals = $this->getTransactionsForSum(TransactionType::DEPOSIT, $start, $end, $accounts, $revenue, $currency);
return $this->groupByCurrency($journals, 'positive');
}
/**
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function sumIncomeByDestination(
Carbon $start,
Carbon $end,
?Collection $accounts = null,
?Collection $revenue = null,
?TransactionCurrency $currency = null
): array {
$journals = $this->getTransactionsForSum(TransactionType::DEPOSIT, $start, $end, $accounts, $revenue, $currency);
return $this->groupByDirection($journals, 'destination', 'positive');
}
/**
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function sumIncomeBySource(
Carbon $start,
Carbon $end,
?Collection $accounts = null,
?Collection $revenue = null,
?TransactionCurrency $currency = null
): array {
$journals = $this->getTransactionsForSum(TransactionType::DEPOSIT, $start, $end, $accounts, $revenue, $currency);
return $this->groupByDirection($journals, 'source', 'positive');
}
public function sumTransfers(Carbon $start, Carbon $end, ?Collection $accounts = null, ?TransactionCurrency $currency = null): array
{
$journals = $this->getTransactionsForSum(TransactionType::TRANSFER, $start, $end, $accounts, null, $currency);
return $this->groupByEither($journals);
}
2021-04-03 00:16:43 -05:00
private function groupByEither(array $journals): array
{
$return = [];
2023-12-20 12:35:52 -06:00
2021-04-03 00:16:43 -05:00
/** @var array $journal */
foreach ($journals as $journal) {
$return = $this->groupByEitherJournal($return, $journal);
}
$final = [];
2021-04-03 00:16:43 -05:00
foreach ($return as $array) {
2022-12-29 12:42:26 -06:00
$array['difference_float'] = (float)$array['difference'];
$array['in_float'] = (float)$array['in'];
$array['out_float'] = (float)$array['out'];
2021-04-03 00:16:43 -05:00
$final[] = $array;
}
return $final;
}
private function groupByEitherJournal(array $return, array $journal): array
{
$sourceId = $journal['source_account_id'];
$destinationId = $journal['destination_account_id'];
$currencyId = $journal['currency_id'];
$sourceKey = sprintf('%d-%d', $currencyId, $sourceId);
$destKey = sprintf('%d-%d', $currencyId, $destinationId);
$amount = app('steam')->positive($journal['amount']);
2021-04-03 00:16:43 -05:00
// source first
2023-12-09 23:45:59 -06:00
$return[$sourceKey] ??= [
2022-12-29 12:42:26 -06:00
'id' => (string)$sourceId,
'name' => $journal['source_account_name'],
'difference' => '0',
'difference_float' => 0,
'in' => '0',
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string)$currencyId,
'currency_code' => $journal['currency_code'],
];
2021-04-03 00:16:43 -05:00
// dest next:
$return[$destKey] ??= [
2022-12-29 12:42:26 -06:00
'id' => (string)$destinationId,
'name' => $journal['destination_account_name'],
'difference' => '0',
'difference_float' => 0,
'in' => '0',
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string)$currencyId,
'currency_code' => $journal['currency_code'],
];
2021-04-03 00:16:43 -05:00
// source account? money goes out!
$return[$sourceKey]['out'] = bcadd($return[$sourceKey]['out'], app('steam')->negative($amount));
$return[$sourceKey]['difference'] = bcadd($return[$sourceKey]['out'], $return[$sourceKey]['in']);
// destination account? money comes in:
$return[$destKey]['in'] = bcadd($return[$destKey]['in'], $amount);
$return[$destKey]['difference'] = bcadd($return[$destKey]['out'], $return[$destKey]['in']);
2021-04-03 00:16:43 -05:00
// foreign currency
if (null !== $journal['foreign_currency_id'] && null !== $journal['foreign_amount']) {
$currencyId = $journal['foreign_currency_id'];
$sourceKey = sprintf('%d-%d', $currencyId, $sourceId);
$destKey = sprintf('%d-%d', $currencyId, $destinationId);
$amount = app('steam')->positive($journal['foreign_amount']);
2021-04-03 00:16:43 -05:00
// same as above:
// source first
2023-12-09 23:45:59 -06:00
$return[$sourceKey] ??= [
2022-12-29 12:42:26 -06:00
'id' => (string)$sourceId,
'name' => $journal['source_account_name'],
'difference' => '0',
'difference_float' => 0,
'in' => '0',
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string)$currencyId,
'currency_code' => $journal['foreign_currency_code'],
];
2021-04-03 00:16:43 -05:00
// dest next:
$return[$destKey] ??= [
2022-12-29 12:42:26 -06:00
'id' => (string)$destinationId,
'name' => $journal['destination_account_name'],
'difference' => '0',
'difference_float' => 0,
'in' => '0',
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string)$currencyId,
'currency_code' => $journal['foreign_currency_code'],
];
2021-04-03 00:16:43 -05:00
// source account? money goes out! (same as above)
$return[$sourceKey]['out'] = bcadd($return[$sourceKey]['out'], app('steam')->negative($amount));
$return[$sourceKey]['difference'] = bcadd($return[$sourceKey]['out'], $return[$sourceKey]['in']);
// destination account? money comes in:
$return[$destKey]['in'] = bcadd($return[$destKey]['in'], $amount);
$return[$destKey]['difference'] = bcadd($return[$destKey]['out'], $return[$destKey]['in']);
2021-04-03 00:16:43 -05:00
}
return $return;
}
}