mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Various code cleanup and sorting.
This commit is contained in:
@@ -45,6 +45,7 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Override;
|
||||
|
||||
/**
|
||||
* Class AccountRepository.
|
||||
@@ -65,12 +66,6 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
return true;
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function getAccountBalances(Account $account): Collection
|
||||
{
|
||||
return $account->accountBalances;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find account with same name OR same IBAN or both, but not the same type or ID.
|
||||
*/
|
||||
@@ -81,8 +76,7 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
/** @var Account $account */
|
||||
foreach ($accounts as $account) {
|
||||
$byName = $this->user->accounts()->where('name', $account->name)
|
||||
->where('id', '!=', $account->id)->first()
|
||||
;
|
||||
->where('id', '!=', $account->id)->first();
|
||||
if (null !== $byName) {
|
||||
$result->push($account);
|
||||
$result->push($byName);
|
||||
@@ -91,8 +85,7 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
}
|
||||
if (null !== $account->iban) {
|
||||
$byIban = $this->user->accounts()->where('iban', $account->iban)
|
||||
->where('id', '!=', $account->id)->first()
|
||||
;
|
||||
->where('id', '!=', $account->id)->first();
|
||||
if (null !== $byIban) {
|
||||
$result->push($account);
|
||||
$result->push($byIban);
|
||||
@@ -118,8 +111,7 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
$q1->where('account_meta.name', '=', 'account_number');
|
||||
$q1->where('account_meta.data', '=', $json);
|
||||
}
|
||||
)
|
||||
;
|
||||
);
|
||||
|
||||
if (0 !== count($types)) {
|
||||
$dbQuery->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
||||
@@ -146,7 +138,7 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
|
||||
public function findByName(string $name, array $types): ?Account
|
||||
{
|
||||
$query = $this->user->accounts();
|
||||
$query = $this->user->accounts();
|
||||
|
||||
if (0 !== count($types)) {
|
||||
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
||||
@@ -168,6 +160,12 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
return $account;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function getAccountBalances(Account $account): Collection
|
||||
{
|
||||
return $account->accountBalances;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return account type or null if not found.
|
||||
*/
|
||||
@@ -194,10 +192,10 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
{
|
||||
$query = $this->user->accounts()->with(
|
||||
[ // @phpstan-ignore-line
|
||||
'accountmeta' => static function (HasMany $query): void {
|
||||
$query->where('name', 'account_role');
|
||||
},
|
||||
'attachments',
|
||||
'accountmeta' => static function (HasMany $query): void {
|
||||
$query->where('name', 'account_role');
|
||||
},
|
||||
'attachments',
|
||||
]
|
||||
);
|
||||
if (0 !== count($types)) {
|
||||
@@ -213,7 +211,7 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
|
||||
public function getAttachments(Account $account): Collection
|
||||
{
|
||||
$set = $account->attachments()->get();
|
||||
$set = $account->attachments()->get();
|
||||
|
||||
/** @var Storage $disk */
|
||||
$disk = Storage::disk('upload');
|
||||
@@ -235,7 +233,7 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
public function getCashAccount(): Account
|
||||
{
|
||||
/** @var AccountType $type */
|
||||
$type = AccountType::where('type', AccountTypeEnum::CASH->value)->first();
|
||||
$type = AccountType::where('type', AccountTypeEnum::CASH->value)->first();
|
||||
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
@@ -247,10 +245,9 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
public function getCreditTransactionGroup(Account $account): ?TransactionGroup
|
||||
{
|
||||
$journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.account_id', $account->id)
|
||||
->transactionTypes([TransactionTypeEnum::LIABILITY_CREDIT->value])
|
||||
->first(['transaction_journals.*'])
|
||||
;
|
||||
->where('transactions.account_id', $account->id)
|
||||
->transactionTypes([TransactionTypeEnum::LIABILITY_CREDIT->value])
|
||||
->first(['transaction_journals.*']);
|
||||
|
||||
return $journal?->transactionGroup;
|
||||
}
|
||||
@@ -259,9 +256,9 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
{
|
||||
$query = $this->user->accounts()->with(
|
||||
[ // @phpstan-ignore-line
|
||||
'accountmeta' => static function (HasMany $query): void {
|
||||
$query->where('name', 'account_role');
|
||||
},
|
||||
'accountmeta' => static function (HasMany $query): void {
|
||||
$query->where('name', 'account_role');
|
||||
},
|
||||
]
|
||||
);
|
||||
if (0 !== count($types)) {
|
||||
@@ -294,11 +291,10 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
*/
|
||||
public function getOpeningBalanceAmount(Account $account, bool $convertToNative): ?string
|
||||
{
|
||||
$journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.account_id', $account->id)
|
||||
->transactionTypes([TransactionTypeEnum::OPENING_BALANCE->value, TransactionTypeEnum::LIABILITY_CREDIT->value])
|
||||
->first(['transaction_journals.*'])
|
||||
;
|
||||
$journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.account_id', $account->id)
|
||||
->transactionTypes([TransactionTypeEnum::OPENING_BALANCE->value, TransactionTypeEnum::LIABILITY_CREDIT->value])
|
||||
->first(['transaction_journals.*']);
|
||||
if (null === $journal) {
|
||||
return null;
|
||||
}
|
||||
@@ -319,10 +315,9 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
public function getOpeningBalanceDate(Account $account): ?string
|
||||
{
|
||||
return TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.account_id', $account->id)
|
||||
->transactionTypes([TransactionTypeEnum::OPENING_BALANCE->value, TransactionTypeEnum::LIABILITY_CREDIT->value])
|
||||
->first(['transaction_journals.*'])?->date->format('Y-m-d H:i:s')
|
||||
;
|
||||
->where('transactions.account_id', $account->id)
|
||||
->transactionTypes([TransactionTypeEnum::OPENING_BALANCE->value, TransactionTypeEnum::LIABILITY_CREDIT->value])
|
||||
->first(['transaction_journals.*'])?->date->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
public function getOpeningBalanceGroup(Account $account): ?TransactionGroup
|
||||
@@ -335,10 +330,9 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
public function getOpeningBalance(Account $account): ?TransactionJournal
|
||||
{
|
||||
return TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.account_id', $account->id)
|
||||
->transactionTypes([TransactionTypeEnum::OPENING_BALANCE->value])
|
||||
->first(['transaction_journals.*'])
|
||||
;
|
||||
->where('transactions.account_id', $account->id)
|
||||
->transactionTypes([TransactionTypeEnum::OPENING_BALANCE->value])
|
||||
->first(['transaction_journals.*']);
|
||||
}
|
||||
|
||||
public function getPiggyBanks(Account $account): Collection
|
||||
@@ -358,19 +352,18 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
$name = trans('firefly.reconciliation_account_name', ['name' => $account->name, 'currency' => $currency->code]);
|
||||
|
||||
/** @var AccountType $type */
|
||||
$type = AccountType::where('type', AccountTypeEnum::RECONCILIATION->value)->first();
|
||||
$type = AccountType::where('type', AccountTypeEnum::RECONCILIATION->value)->first();
|
||||
|
||||
/** @var null|Account $current */
|
||||
$current = $this->user->accounts()->where('account_type_id', $type->id)
|
||||
->where('name', $name)
|
||||
->first()
|
||||
;
|
||||
$current = $this->user->accounts()->where('account_type_id', $type->id)
|
||||
->where('name', $name)
|
||||
->first();
|
||||
|
||||
if (null !== $current) {
|
||||
return $current;
|
||||
}
|
||||
|
||||
$data = [
|
||||
$data = [
|
||||
'account_type_id' => null,
|
||||
'account_type_name' => AccountTypeEnum::RECONCILIATION->value,
|
||||
'active' => true,
|
||||
@@ -380,7 +373,7 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
];
|
||||
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($account->user);
|
||||
|
||||
return $factory->create($data);
|
||||
@@ -388,8 +381,8 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
|
||||
public function getAccountCurrency(Account $account): ?TransactionCurrency
|
||||
{
|
||||
$type = $account->accountType->type;
|
||||
$list = config('firefly.valid_currency_account_types');
|
||||
$type = $account->accountType->type;
|
||||
$list = config('firefly.valid_currency_account_types');
|
||||
|
||||
// return null if not in this list.
|
||||
if (!in_array($type, $list, true)) {
|
||||
@@ -453,7 +446,7 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
|
||||
public function maxOrder(string $type): int
|
||||
{
|
||||
$sets = [
|
||||
$sets = [
|
||||
AccountTypeEnum::ASSET->value => [AccountTypeEnum::DEFAULT->value, AccountTypeEnum::ASSET->value],
|
||||
AccountTypeEnum::EXPENSE->value => [AccountTypeEnum::EXPENSE->value, AccountTypeEnum::BENEFICIARY->value],
|
||||
AccountTypeEnum::REVENUE->value => [AccountTypeEnum::REVENUE->value],
|
||||
@@ -469,7 +462,7 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
}
|
||||
$specials = [AccountTypeEnum::CASH->value, AccountTypeEnum::INITIAL_BALANCE->value, AccountTypeEnum::IMPORT->value, AccountTypeEnum::RECONCILIATION->value];
|
||||
|
||||
$order = (int) $this->getAccountsByType($specials)->max('order');
|
||||
$order = (int) $this->getAccountsByType($specials)->max('order');
|
||||
app('log')->debug(sprintf('Return max order of "%s" set (specials!): %d', $type, $order));
|
||||
|
||||
return $order;
|
||||
@@ -520,13 +513,12 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
{
|
||||
/** @var null|TransactionJournal $first */
|
||||
$first = $account->transactions()
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
->orderBy('transaction_journals.date', 'ASC')
|
||||
->orderBy('transaction_journals.order', 'DESC')
|
||||
->where('transaction_journals.user_id', $this->user->id)
|
||||
->orderBy('transaction_journals.id', 'ASC')
|
||||
->first(['transaction_journals.id'])
|
||||
;
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
->orderBy('transaction_journals.date', 'ASC')
|
||||
->orderBy('transaction_journals.order', 'DESC')
|
||||
->where('transaction_journals.user_id', $this->user->id)
|
||||
->orderBy('transaction_journals.id', 'ASC')
|
||||
->first(['transaction_journals.id']);
|
||||
if (null !== $first) {
|
||||
/** @var null|TransactionJournal */
|
||||
return TransactionJournal::find($first->id);
|
||||
@@ -562,11 +554,10 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
}
|
||||
}
|
||||
// reset the rest to zero.
|
||||
$all = [AccountTypeEnum::DEFAULT->value, AccountTypeEnum::ASSET->value, AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::CREDITCARD->value, AccountTypeEnum::MORTGAGE->value];
|
||||
$all = [AccountTypeEnum::DEFAULT->value, AccountTypeEnum::ASSET->value, AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::CREDITCARD->value, AccountTypeEnum::MORTGAGE->value];
|
||||
$this->user->accounts()->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
||||
->whereNotIn('account_types.type', $all)
|
||||
->update(['order' => 0])
|
||||
;
|
||||
->whereNotIn('account_types.type', $all)
|
||||
->update(['order' => 0]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -583,12 +574,11 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
public function searchAccount(string $query, array $types, int $limit): Collection
|
||||
{
|
||||
$dbQuery = $this->user->accounts()
|
||||
->where('active', true)
|
||||
->orderBy('accounts.order', 'ASC')
|
||||
->orderBy('accounts.account_type_id', 'ASC')
|
||||
->orderBy('accounts.name', 'ASC')
|
||||
->with(['accountType'])
|
||||
;
|
||||
->where('active', true)
|
||||
->orderBy('accounts.order', 'ASC')
|
||||
->orderBy('accounts.account_type_id', 'ASC')
|
||||
->orderBy('accounts.name', 'ASC')
|
||||
->with(['accountType']);
|
||||
if ('' !== $query) {
|
||||
// split query on spaces just in case:
|
||||
$parts = explode(' ', $query);
|
||||
@@ -608,13 +598,12 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
public function searchAccountNr(string $query, array $types, int $limit): Collection
|
||||
{
|
||||
$dbQuery = $this->user->accounts()->distinct()
|
||||
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||
->where('accounts.active', true)
|
||||
->orderBy('accounts.order', 'ASC')
|
||||
->orderBy('accounts.account_type_id', 'ASC')
|
||||
->orderBy('accounts.name', 'ASC')
|
||||
->with(['accountType', 'accountMeta'])
|
||||
;
|
||||
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||
->where('accounts.active', true)
|
||||
->orderBy('accounts.order', 'ASC')
|
||||
->orderBy('accounts.account_type_id', 'ASC')
|
||||
->orderBy('accounts.name', 'ASC')
|
||||
->with(['accountType', 'accountMeta']);
|
||||
if ('' !== $query) {
|
||||
// split query on spaces just in case:
|
||||
$parts = explode(' ', $query);
|
||||
|
@@ -52,7 +52,6 @@ interface AccountRepositoryInterface
|
||||
* Moved here from account CRUD.
|
||||
*/
|
||||
public function count(array $types): int;
|
||||
public function getAccountBalances(Account $account): Collection;
|
||||
|
||||
/**
|
||||
* Moved here from account CRUD.
|
||||
@@ -72,6 +71,8 @@ interface AccountRepositoryInterface
|
||||
|
||||
public function findByName(string $name, array $types): ?Account;
|
||||
|
||||
public function getAccountBalances(Account $account): Collection;
|
||||
|
||||
public function getAccountCurrency(Account $account): ?TransactionCurrency;
|
||||
|
||||
/**
|
||||
|
@@ -47,28 +47,28 @@ class AccountTasker implements AccountTaskerInterface, UserGroupInterface
|
||||
*/
|
||||
public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array
|
||||
{
|
||||
$yesterday = clone $start;
|
||||
$yesterday = clone $start;
|
||||
$yesterday->subDay()->endOfDay(); // exactly up until $start but NOT including.
|
||||
$end->endOfDay(); // needs to be end of day to be correct.
|
||||
Log::debug(sprintf('getAccountReport: finalAccountsBalance("%s")', $yesterday->format('Y-m-d H:i:s')));
|
||||
Log::debug(sprintf('getAccountReport: finalAccountsBalance("%s")', $end->format('Y-m-d H:i:s')));
|
||||
$startSet = Steam::finalAccountsBalance($accounts, $yesterday);
|
||||
$endSet = Steam::finalAccountsBalance($accounts, $end);
|
||||
$startSet = Steam::finalAccountsBalance($accounts, $yesterday);
|
||||
$endSet = Steam::finalAccountsBalance($accounts, $end);
|
||||
Log::debug('Start of accountreport');
|
||||
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$defaultCurrency = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup);
|
||||
|
||||
$return = [
|
||||
$return = [
|
||||
'accounts' => [],
|
||||
'sums' => [],
|
||||
];
|
||||
|
||||
/** @var Account $account */
|
||||
foreach ($accounts as $account) {
|
||||
$id = $account->id;
|
||||
$currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
|
||||
$id = $account->id;
|
||||
$currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
|
||||
$return['sums'][$currency->id] ??= [
|
||||
'start' => '0',
|
||||
'end' => '0',
|
||||
@@ -79,7 +79,7 @@ class AccountTasker implements AccountTaskerInterface, UserGroupInterface
|
||||
'currency_name' => $currency->name,
|
||||
'currency_decimal_places' => $currency->decimal_places,
|
||||
];
|
||||
$entry = [
|
||||
$entry = [
|
||||
'name' => $account->name,
|
||||
'id' => $account->id,
|
||||
'currency_id' => $currency->id,
|
||||
@@ -90,9 +90,9 @@ class AccountTasker implements AccountTaskerInterface, UserGroupInterface
|
||||
];
|
||||
|
||||
// get first journal date:
|
||||
$first = $repository->oldestJournal($account);
|
||||
$entry['start_balance'] = $startSet[$account->id]['balance'] ?? '0';
|
||||
$entry['end_balance'] = $endSet[$account->id]['balance'] ?? '0';
|
||||
$first = $repository->oldestJournal($account);
|
||||
$entry['start_balance'] = $startSet[$account->id]['balance'] ?? '0';
|
||||
$entry['end_balance'] = $endSet[$account->id]['balance'] ?? '0';
|
||||
|
||||
// first journal exists, and is on start, then this is the actual opening balance:
|
||||
if (null !== $first && $first->date->isSameDay($yesterday) && TransactionTypeEnum::OPENING_BALANCE->value === $first->transactionType->type) {
|
||||
@@ -127,13 +127,13 @@ class AccountTasker implements AccountTaskerInterface, UserGroupInterface
|
||||
$collector->setSourceAccounts($accounts)->setRange($start, $end);
|
||||
$collector->excludeDestinationAccounts($accounts);
|
||||
$collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value, TransactionTypeEnum::TRANSFER->value])->withAccountInformation();
|
||||
$journals = $collector->getExtractedJournals();
|
||||
$journals = $collector->getExtractedJournals();
|
||||
|
||||
$report = $this->groupExpenseByDestination($journals);
|
||||
$report = $this->groupExpenseByDestination($journals);
|
||||
|
||||
// sort the result
|
||||
// Obtain a list of columns
|
||||
$sum = [];
|
||||
$sum = [];
|
||||
foreach ($report['accounts'] as $accountId => $row) {
|
||||
$sum[$accountId] = (float) $row['sum']; // intentional float
|
||||
}
|
||||
@@ -151,9 +151,9 @@ class AccountTasker implements AccountTaskerInterface, UserGroupInterface
|
||||
$defaultCurrency = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup);
|
||||
|
||||
/** @var CurrencyRepositoryInterface $currencyRepos */
|
||||
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
||||
$currencies = [$defaultCurrency->id => $defaultCurrency];
|
||||
$report = [
|
||||
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
||||
$currencies = [$defaultCurrency->id => $defaultCurrency];
|
||||
$report = [
|
||||
'accounts' => [],
|
||||
'sums' => [],
|
||||
];
|
||||
@@ -163,8 +163,8 @@ class AccountTasker implements AccountTaskerInterface, UserGroupInterface
|
||||
$sourceId = (int) $journal['destination_account_id'];
|
||||
$currencyId = (int) $journal['currency_id'];
|
||||
$key = sprintf('%s-%s', $sourceId, $currencyId);
|
||||
$currencies[$currencyId] ??= $currencyRepos->find($currencyId);
|
||||
$report['accounts'][$key] ??= [
|
||||
$currencies[$currencyId] ??= $currencyRepos->find($currencyId);
|
||||
$report['accounts'][$key] ??= [
|
||||
'id' => $sourceId,
|
||||
'name' => $journal['destination_account_name'],
|
||||
'sum' => '0',
|
||||
@@ -189,7 +189,7 @@ class AccountTasker implements AccountTaskerInterface, UserGroupInterface
|
||||
$report['accounts'][$key]['average'] = bcdiv($report['accounts'][$key]['sum'], (string) $report['accounts'][$key]['count']);
|
||||
}
|
||||
$currencyId = $report['accounts'][$key]['currency_id'];
|
||||
$report['sums'][$currencyId] ??= [
|
||||
$report['sums'][$currencyId] ??= [
|
||||
'sum' => '0',
|
||||
'currency_id' => $report['accounts'][$key]['currency_id'],
|
||||
'currency_name' => $report['accounts'][$key]['currency_name'],
|
||||
@@ -217,11 +217,11 @@ class AccountTasker implements AccountTaskerInterface, UserGroupInterface
|
||||
$collector->setDestinationAccounts($accounts)->setRange($start, $end);
|
||||
$collector->excludeSourceAccounts($accounts);
|
||||
$collector->setTypes([TransactionTypeEnum::DEPOSIT->value, TransactionTypeEnum::TRANSFER->value])->withAccountInformation();
|
||||
$report = $this->groupIncomeBySource($collector->getExtractedJournals());
|
||||
$report = $this->groupIncomeBySource($collector->getExtractedJournals());
|
||||
|
||||
// sort the result
|
||||
// Obtain a list of columns
|
||||
$sum = [];
|
||||
$sum = [];
|
||||
foreach ($report['accounts'] as $accountId => $row) {
|
||||
$sum[$accountId] = (float) $row['sum']; // intentional float
|
||||
}
|
||||
@@ -239,20 +239,20 @@ class AccountTasker implements AccountTaskerInterface, UserGroupInterface
|
||||
$defaultCurrency = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup);
|
||||
|
||||
/** @var CurrencyRepositoryInterface $currencyRepos */
|
||||
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
||||
$currencies = [$defaultCurrency->id => $defaultCurrency];
|
||||
$report = [
|
||||
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
||||
$currencies = [$defaultCurrency->id => $defaultCurrency];
|
||||
$report = [
|
||||
'accounts' => [],
|
||||
'sums' => [],
|
||||
];
|
||||
|
||||
/** @var array $journal */
|
||||
foreach ($array as $journal) {
|
||||
$sourceId = (int) $journal['source_account_id'];
|
||||
$currencyId = (int) $journal['currency_id'];
|
||||
$key = sprintf('%s-%s', $sourceId, $currencyId);
|
||||
$sourceId = (int) $journal['source_account_id'];
|
||||
$currencyId = (int) $journal['currency_id'];
|
||||
$key = sprintf('%s-%s', $sourceId, $currencyId);
|
||||
if (!array_key_exists($key, $report['accounts'])) {
|
||||
$currencies[$currencyId] ??= $currencyRepos->find($currencyId);
|
||||
$currencies[$currencyId] ??= $currencyRepos->find($currencyId);
|
||||
$report['accounts'][$key] = [
|
||||
'id' => $sourceId,
|
||||
'name' => $journal['source_account_name'],
|
||||
@@ -276,7 +276,7 @@ class AccountTasker implements AccountTaskerInterface, UserGroupInterface
|
||||
$report['accounts'][$key]['average'] = bcdiv($report['accounts'][$key]['sum'], (string) $report['accounts'][$key]['count']);
|
||||
}
|
||||
$currencyId = $report['accounts'][$key]['currency_id'];
|
||||
$report['sums'][$currencyId] ??= [
|
||||
$report['sums'][$currencyId] ??= [
|
||||
'sum' => '0',
|
||||
'currency_id' => $report['accounts'][$key]['currency_id'],
|
||||
'currency_name' => $report['accounts'][$key]['currency_name'],
|
||||
|
@@ -70,8 +70,8 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
{
|
||||
$array = [];
|
||||
foreach ($journals as $journal) {
|
||||
$currencyId = (int) $journal['currency_id'];
|
||||
$journalId = (int) $journal['transaction_journal_id'];
|
||||
$currencyId = (int) $journal['currency_id'];
|
||||
$journalId = (int) $journal['transaction_journal_id'];
|
||||
$array[$currencyId] ??= [
|
||||
'currency_id' => $journal['currency_id'],
|
||||
'currency_name' => $journal['currency_name'],
|
||||
@@ -123,7 +123,8 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
?Collection $accounts = null,
|
||||
?Collection $expense = null,
|
||||
?TransactionCurrency $currency = null
|
||||
): array {
|
||||
): array
|
||||
{
|
||||
$journals = $this->getTransactionsForSum(TransactionTypeEnum::WITHDRAWAL->value, $start, $end, $accounts, $expense, $currency);
|
||||
|
||||
return $this->groupByCurrency($journals, 'negative');
|
||||
@@ -140,7 +141,8 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
?Collection $accounts = null,
|
||||
?Collection $opposing = null,
|
||||
?TransactionCurrency $currency = null
|
||||
): array {
|
||||
): array
|
||||
{
|
||||
$start->startOfDay();
|
||||
$end->endOfDay();
|
||||
|
||||
@@ -173,15 +175,14 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
if (null !== $currency) {
|
||||
$collector->setCurrency($currency);
|
||||
}
|
||||
$journals = $collector->getExtractedJournals();
|
||||
$journals = $collector->getExtractedJournals();
|
||||
|
||||
// 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()
|
||||
->setForeignCurrency($currency)
|
||||
;
|
||||
->setForeignCurrency($currency);
|
||||
if (TransactionTypeEnum::WITHDRAWAL->value === $type) {
|
||||
if (null !== $accounts) {
|
||||
$collector->setSourceAccounts($accounts);
|
||||
@@ -199,10 +200,10 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
}
|
||||
}
|
||||
|
||||
$result = $collector->getExtractedJournals();
|
||||
$result = $collector->getExtractedJournals();
|
||||
|
||||
// do not use array_merge because you want keys to overwrite (otherwise you get double results):
|
||||
$journals = $result + $journals;
|
||||
$journals = $result + $journals;
|
||||
}
|
||||
|
||||
return $journals;
|
||||
@@ -224,7 +225,8 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
?Collection $accounts = null,
|
||||
?Collection $expense = null,
|
||||
?TransactionCurrency $currency = null
|
||||
): array {
|
||||
): array
|
||||
{
|
||||
$journals = $this->getTransactionsForSum(TransactionTypeEnum::WITHDRAWAL->value, $start, $end, $accounts, $expense, $currency);
|
||||
|
||||
return $this->groupByDirection($journals, 'destination', 'negative');
|
||||
@@ -246,7 +248,8 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
?Collection $accounts = null,
|
||||
?Collection $expense = null,
|
||||
?TransactionCurrency $currency = null
|
||||
): array {
|
||||
): array
|
||||
{
|
||||
$journals = $this->getTransactionsForSum(TransactionTypeEnum::WITHDRAWAL->value, $start, $end, $accounts, $expense, $currency);
|
||||
|
||||
return $this->groupByDirection($journals, 'source', 'negative');
|
||||
@@ -261,7 +264,8 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
?Collection $accounts = null,
|
||||
?Collection $revenue = null,
|
||||
?TransactionCurrency $currency = null
|
||||
): array {
|
||||
): array
|
||||
{
|
||||
$journals = $this->getTransactionsForSum(TransactionTypeEnum::DEPOSIT->value, $start, $end, $accounts, $revenue, $currency);
|
||||
|
||||
return $this->groupByCurrency($journals, 'positive');
|
||||
@@ -276,7 +280,8 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
?Collection $accounts = null,
|
||||
?Collection $revenue = null,
|
||||
?TransactionCurrency $currency = null
|
||||
): array {
|
||||
): array
|
||||
{
|
||||
$journals = $this->getTransactionsForSum(TransactionTypeEnum::DEPOSIT->value, $start, $end, $accounts, $revenue, $currency);
|
||||
|
||||
return $this->groupByDirection($journals, 'destination', 'positive');
|
||||
@@ -291,7 +296,8 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
?Collection $accounts = null,
|
||||
?Collection $revenue = null,
|
||||
?TransactionCurrency $currency = null
|
||||
): array {
|
||||
): array
|
||||
{
|
||||
$journals = $this->getTransactionsForSum(TransactionTypeEnum::DEPOSIT->value, $start, $end, $accounts, $revenue, $currency);
|
||||
|
||||
return $this->groupByDirection($journals, 'source', 'positive');
|
||||
@@ -312,7 +318,7 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
foreach ($journals as $journal) {
|
||||
$return = $this->groupByEitherJournal($return, $journal);
|
||||
}
|
||||
$final = [];
|
||||
$final = [];
|
||||
foreach ($return as $array) {
|
||||
$array['difference_float'] = (float) $array['difference'];
|
||||
$array['in_float'] = (float) $array['in'];
|
||||
@@ -325,12 +331,12 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
|
||||
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']);
|
||||
$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']);
|
||||
|
||||
// source first
|
||||
$return[$sourceKey] ??= [
|
||||
@@ -347,7 +353,7 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
];
|
||||
|
||||
// dest next:
|
||||
$return[$destKey] ??= [
|
||||
$return[$destKey] ??= [
|
||||
'id' => (string) $destinationId,
|
||||
'name' => $journal['destination_account_name'],
|
||||
'difference' => '0',
|
||||
@@ -365,15 +371,15 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
$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']);
|
||||
$return[$destKey]['in'] = bcadd($return[$destKey]['in'], $amount);
|
||||
$return[$destKey]['difference'] = bcadd($return[$destKey]['out'], $return[$destKey]['in']);
|
||||
|
||||
// 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']);
|
||||
$currencyId = $journal['foreign_currency_id'];
|
||||
$sourceKey = sprintf('%d-%d', $currencyId, $sourceId);
|
||||
$destKey = sprintf('%d-%d', $currencyId, $destinationId);
|
||||
$amount = app('steam')->positive($journal['foreign_amount']);
|
||||
|
||||
// same as above:
|
||||
// source first
|
||||
@@ -391,7 +397,7 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
];
|
||||
|
||||
// dest next:
|
||||
$return[$destKey] ??= [
|
||||
$return[$destKey] ??= [
|
||||
'id' => (string) $destinationId,
|
||||
'name' => $journal['destination_account_name'],
|
||||
'difference' => '0',
|
||||
@@ -408,8 +414,8 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
|
||||
$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']);
|
||||
$return[$destKey]['in'] = bcadd($return[$destKey]['in'], $amount);
|
||||
$return[$destKey]['difference'] = bcadd($return[$destKey]['out'], $return[$destKey]['in']);
|
||||
}
|
||||
|
||||
return $return;
|
||||
|
Reference in New Issue
Block a user