Big refactor to remove the deprecated transaction collector.

This commit is contained in:
James Cole
2019-05-30 12:31:19 +02:00
parent 10a6ff9bf8
commit 8b7e87ae57
117 changed files with 1314 additions and 1208 deletions

View File

@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
@@ -52,8 +53,8 @@ class AccountTasker implements AccountTaskerInterface
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -119,8 +120,8 @@ class AccountTasker implements AccountTaskerInterface
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
@@ -130,23 +131,15 @@ class AccountTasker implements AccountTaskerInterface
// get all expenses for the given accounts in the given period!
// also transfers!
// get all transactions:
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($accounts)->setRange($start, $end);
$collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
->withOpposingAccount();
$transactions = $collector->getTransactions();
$transactions = $transactions->filter(
function (Transaction $transaction) {
// return negative amounts only.
if (bccomp($transaction->transaction_amount, '0') === -1) {
return $transaction;
}
return false;
}
);
$expenses = $this->groupByOpposing($transactions);
->withAccountInformation();
$journals = $collector->getExtractedJournals();
$expenses = $this->groupByDestination($journals);
// sort the result
// Obtain a list of columns
@@ -161,8 +154,66 @@ class AccountTasker implements AccountTaskerInterface
}
/**
* @param Carbon $start
* @param Carbon $end
* @param array $array
*
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function groupByDestination(array $array): array
{
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
$currencies = [$defaultCurrency->id => $defaultCurrency,];
$expenses = [];
$countAccounts = []; // if count remains 0 use original name, not the name with the currency.
/** @var array $journal */
foreach ($array as $journal) {
$opposingId = (int)$journal['destination_account_id'];
$currencyId = (int)$journal['currency_id'];
$key = sprintf('%s-%s', $opposingId, $currencyId);
$name = sprintf('%s (%s)', $journal['destination_account_name'], $journal['currency_name']);
$countAccounts[$opposingId] = isset($countAccounts[$opposingId]) ? $countAccounts[$opposingId] + 1 : 1;
if (!isset($expenses[$key])) {
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepos->findNull($currencyId);
$expenses[$key] = [
'id' => $opposingId,
'name' => $name,
'original' => $journal['destination_account_name'],
'sum' => '0',
'average' => '0',
'currencies' => [],
'single_currency' => $currencies[$currencyId],
'count' => 0,
];
}
$expenses[$key]['currencies'][] = (int)$journal['currency_id'];
$expenses[$key]['sum'] = bcadd($expenses[$key]['sum'], $journal['amount']);
++$expenses[$key]['count'];
}
// do averages:
$keys = array_keys($expenses);
foreach ($keys as $key) {
$opposingId = $expenses[$key]['id'];
if (1 === $countAccounts[$opposingId]) {
$expenses[$key]['name'] = $expenses[$key]['original'];
}
if ($expenses[$key]['count'] > 1) {
$expenses[$key]['average'] = bcdiv($expenses[$key]['sum'], (string)$expenses[$key]['count']);
}
$expenses[$key]['currencies'] = count(array_unique($expenses[$key]['currencies']));
$expenses[$key]['all_currencies'] = count($currencies);
}
return $expenses;
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
@@ -172,23 +223,14 @@ class AccountTasker implements AccountTaskerInterface
// get all expenses for the given accounts in the given period!
// also transfers!
// get all transactions:
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($accounts)->setRange($start, $end);
$collector->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
->withOpposingAccount();
$transactions = $collector->getTransactions();
$transactions = $transactions->filter(
function (Transaction $transaction) {
// return positive amounts only.
if (1 === bccomp($transaction->transaction_amount, '0')) {
return $transaction;
}
return false;
}
);
$income = $this->groupByOpposing($transactions);
->withAccountInformation();
$income = $this->groupByDestination($collector->getExtractedJournals());
// sort the result
// Obtain a list of columns
@@ -209,62 +251,4 @@ class AccountTasker implements AccountTaskerInterface
{
$this->user = $user;
}
/**
* @param Collection $transactions
*
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function groupByOpposing(Collection $transactions): array
{
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
$currencies = [$defaultCurrency->id => $defaultCurrency,];
$expenses = [];
$countAccounts = []; // if count remains 0 use original name, not the name with the currency.
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$opposingId = (int)$transaction->opposing_account_id;
$currencyId = (int)$transaction->transaction_currency_id;
$key = sprintf('%s-%s', $opposingId, $currencyId);
$name = sprintf('%s (%s)', $transaction->opposing_account_name, $transaction->transaction_currency_code);
$countAccounts[$opposingId] = isset($countAccounts[$opposingId]) ? $countAccounts[$opposingId] + 1 : 1;
if (!isset($expenses[$key])) {
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepos->findNull($currencyId);
$expenses[$key] = [
'id' => $opposingId,
'name' => $name,
'original' => $transaction->opposing_account_name,
'sum' => '0',
'average' => '0',
'currencies' => [],
'single_currency' => $currencies[$currencyId],
'count' => 0,
];
}
$expenses[$key]['currencies'][] = (int)$transaction->transaction_currency_id;
$expenses[$key]['sum'] = bcadd($expenses[$key]['sum'], $transaction->transaction_amount);
++$expenses[$key]['count'];
}
// do averages:
$keys = array_keys($expenses);
foreach ($keys as $key) {
$opposingId = $expenses[$key]['id'];
if (1 === $countAccounts[$opposingId]) {
$expenses[$key]['name'] = $expenses[$key]['original'];
}
if ($expenses[$key]['count'] > 1) {
$expenses[$key]['average'] = bcdiv($expenses[$key]['sum'], (string)$expenses[$key]['count']);
}
$expenses[$key]['currencies'] = \count(array_unique($expenses[$key]['currencies']));
$expenses[$key]['all_currencies'] = \count($currencies);
}
return $expenses;
}
}