Removed a lot of references to the old collector.

This commit is contained in:
James Cole
2019-05-29 21:52:08 +02:00
parent 280b2efee6
commit 7e2159d12c
17 changed files with 876 additions and 664 deletions

View File

@@ -28,9 +28,8 @@ namespace FireflyIII\Generator\Report\Audit;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Generator\Report\ReportGeneratorInterface;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Illuminate\Support\Collection;
@@ -72,12 +71,10 @@ class MonthReportGenerator implements ReportGeneratorInterface
$reportType = 'audit';
$accountIds = implode(',', $this->accounts->pluck('id')->toArray());
$hideable = ['buttons', 'icon', 'description', 'balance_before', 'amount', 'balance_after', 'date',
'interest_date', 'book_date', 'process_date',
// three new optional fields.
'due_date', 'payment_date', 'invoice_date',
'from', 'to', 'budget', 'category', 'bill',
// more new optional fields
'internal_reference', 'notes',
'create_date', 'update_date',
];
try {
@@ -96,7 +93,7 @@ class MonthReportGenerator implements ReportGeneratorInterface
* Get the audit report.
*
* @param Account $account
* @param Carbon $date
* @param Carbon $date
*
* @return array
*
@@ -112,11 +109,11 @@ class MonthReportGenerator implements ReportGeneratorInterface
$accountRepository = app(AccountRepositoryInterface::class);
$accountRepository->setUser($account->user);
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
$collector->setAccounts(new Collection([$account]))->setRange($this->start, $this->end);
$journals = $collector->getTransactions();
$journals = $journals->reverse();
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts(new Collection([$account]))->setRange($this->start, $this->end)
->withAccountInformation();
$journals = $collector->getExtractedJournals();
$dayBeforeBalance = app('steam')->balance($account, $date);
$startBalance = $dayBeforeBalance;
$currency = $currencyRepos->findNull((int)$accountRepository->getMetaValue($account, 'currency_id'));
@@ -125,23 +122,23 @@ class MonthReportGenerator implements ReportGeneratorInterface
throw new FireflyException('Unexpected NULL value in account currency preference.');
}
/** @var Transaction $transaction */
foreach ($journals as $transaction) {
$transaction->before = $startBalance;
$transactionAmount = $transaction->transaction_amount;
foreach ($journals as $index => $journal) {
$journals[$index]['balance_before'] = $startBalance;
$transactionAmount = $journal['amount'];
if ($currency->id === $transaction->foreign_currency_id) {
$transactionAmount = $transaction->transaction_foreign_amount;
if ($currency->id === $journal['foreign_currency_id']) {
$transactionAmount = $journal['foreign_amount'];
}
$newBalance = bcadd($startBalance, $transactionAmount);
$transaction->after = $newBalance;
$startBalance = $newBalance;
$newBalance = bcadd($startBalance, $transactionAmount);
$journals[$index]['balance_after'] = $newBalance;
$startBalance = $newBalance;
}
$return = [
'journals' => $journals->reverse(),
'exists' => $journals->count() > 0,
'journals' => $journals,
'exists' => count($journals) > 0,
'end' => $this->end->formatLocalized((string)trans('config.month_and_day')),
'endBalance' => app('steam')->balance($account, $this->end),
'dayBefore' => $date->formatLocalized((string)trans('config.month_and_day')),

View File

@@ -27,11 +27,7 @@ namespace FireflyIII\Generator\Report\Budget;
use Carbon\Carbon;
use FireflyIII\Generator\Report\ReportGeneratorInterface;
use FireflyIII\Generator\Report\Support;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Helpers\Filter\OpposingAccountFilter;
use FireflyIII\Helpers\Filter\PositiveAmountFilter;
use FireflyIII\Helpers\Filter\TransferFilter;
use FireflyIII\Models\Transaction;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\TransactionType;
use Illuminate\Support\Collection;
use Log;
@@ -50,7 +46,7 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
private $budgets;
/** @var Carbon The end date. */
private $end;
/** @var Collection The expenses in the report. */
/** @var array The expenses in the report. */
private $expenses;
/** @var Carbon The start date. */
private $start;
@@ -93,6 +89,57 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
return $result;
}
/**
* Get the expenses.
*
* @return array
*/
protected function getExpenses(): array
{
if (count($this->expenses) > 0) {
Log::debug('Return previous set of expenses.');
return $this->expenses;
}
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
->setTypes([TransactionType::WITHDRAWAL])
->withAccountInformation()
->withBudgetInformation()
->setBudgets($this->budgets);
$journals = $collector->getExtractedJournals();
$this->expenses = $journals;
return $journals;
}
/**
* Summarize a collection by its budget.
*
* @param array $array
*
* @return array
*/
private function summarizeByBudget(array $array): array
{
$result = [
'sum' => '0',
];
/** @var array $journal */
foreach ($array as $journal) {
$budgetId = (int)$journal['budget_id'];
$result[$budgetId] = $result[$budgetId] ?? '0';
$result[$budgetId] = bcadd($journal['amount'], $result[$budgetId]);
$result['sum'] = bcadd($result['sum'], $journal['amount']);
}
return $result;
}
/**
* Set the involved accounts.
*
@@ -184,58 +231,4 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
{
return $this;
}
/**
* Get the expenses.
*
* @return Collection
*/
protected function getExpenses(): Collection
{
if ($this->expenses->count() > 0) {
Log::debug('Return previous set of expenses.');
return $this->expenses;
}
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
->setTypes([TransactionType::WITHDRAWAL])
->setBudgets($this->budgets)->withOpposingAccount();
$collector->removeFilter(TransferFilter::class);
$collector->addFilter(OpposingAccountFilter::class);
$collector->addFilter(PositiveAmountFilter::class);
$transactions = $collector->getTransactions();
$this->expenses = $transactions;
return $transactions;
}
/**
* Summarize a collection by its budget.
*
* @param Collection $collection
*
* @return array
*/
private function summarizeByBudget(Collection $collection): array
{
$result = [
'sum' => '0',
];
/** @var Transaction $transaction */
foreach ($collection as $transaction) {
$jrnlBudId = (int)$transaction->transaction_journal_budget_id;
$transBudId = (int)$transaction->transaction_budget_id;
$budgetId = max($jrnlBudId, $transBudId);
$result[$budgetId] = $result[$budgetId] ?? '0';
$result[$budgetId] = bcadd($transaction->transaction_amount, $result[$budgetId]);
$result['sum'] = bcadd($result['sum'], $transaction->transaction_amount);
}
return $result;
}
}

View File

@@ -27,6 +27,7 @@ namespace FireflyIII\Generator\Report\Category;
use Carbon\Carbon;
use FireflyIII\Generator\Report\ReportGeneratorInterface;
use FireflyIII\Generator\Report\Support;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Helpers\Filter\NegativeAmountFilter;
use FireflyIII\Helpers\Filter\OpposingAccountFilter;
@@ -51,9 +52,9 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
private $categories;
/** @var Carbon The end date */
private $end;
/** @var Collection The expenses */
/** @var array The expenses */
private $expenses;
/** @var Collection The income in the report. */
/** @var array The income in the report. */
private $income;
/** @var Carbon The start date. */
private $start;
@@ -201,27 +202,23 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
/**
* Get the expenses for this report.
*
* @return Collection
* @return array
*/
protected function getExpenses(): Collection
protected function getExpenses(): array
{
if ($this->expenses->count() > 0) {
if (count($this->expenses) > 0) {
Log::debug('Return previous set of expenses.');
return $this->expenses;
}
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
->setCategories($this->categories)->withOpposingAccount();
$collector->removeFilter(TransferFilter::class);
->setCategories($this->categories)->withAccountInformation();
$collector->addFilter(OpposingAccountFilter::class);
$collector->addFilter(PositiveAmountFilter::class);
$transactions = $collector->getTransactions();
$transactions = $collector->getExtractedJournals();
$this->expenses = $transactions;
return $transactions;
@@ -230,24 +227,22 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
/**
* Get the income for this report.
*
* @return Collection
* @return array
*/
protected function getIncome(): Collection
protected function getIncome(): array
{
if ($this->income->count() > 0) {
if (count($this->income) > 0) {
return $this->income;
}
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
->setCategories($this->categories)->withOpposingAccount();
->setCategories($this->categories)->withAccountInformation();
$collector->addFilter(OpposingAccountFilter::class);
$collector->addFilter(NegativeAmountFilter::class);
$transactions = $collector->getTransactions();
$transactions = $collector->getExtractedJournals();
$this->income = $transactions;
return $transactions;
@@ -256,20 +251,18 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
/**
* Summarize the category.
*
* @param Collection $collection
* @param array $array
*
* @return array
*/
private function summarizeByCategory(Collection $collection): array
private function summarizeByCategory(array $array): array
{
$result = [];
/** @var Transaction $transaction */
foreach ($collection as $transaction) {
$jrnlCatId = (int)$transaction->transaction_journal_category_id;
$transCatId = (int)$transaction->transaction_category_id;
$categoryId = max($jrnlCatId, $transCatId);
/** @var array $journal */
foreach ($array as $journal) {
$categoryId = (int)$journal['category_id'];
$result[$categoryId] = $result[$categoryId] ?? '0';
$result[$categoryId] = bcadd($transaction->transaction_amount, $result[$categoryId]);
$result[$categoryId] = bcadd($journal['amount'], $result[$categoryId]);
}
return $result;

View File

@@ -24,13 +24,12 @@ declare(strict_types=1);
namespace FireflyIII\Generator\Report;
use FireflyIII\Models\Transaction;
use Illuminate\Support\Collection;
/**
* Class Support.
* @method Collection getExpenses()
* @method Collection getIncome()
* @method array getExpenses()
* @method array getIncome()
*
* @codeCoverageIgnore
*/
@@ -39,53 +38,63 @@ class Support
/**
* Get the top expenses.
*
* @return Collection
* @return array
*/
public function getTopExpenses(): Collection
public function getTopExpenses(): array
{
return $this->getExpenses()->sortBy('transaction_amount');
$expenses = $this->getExpenses();
usort($expenses, function ($a, $b) {
return $a['amount'] <=> $b['amount'];
});
return $expenses;
}
/**
* Get the top income.
*
* @return Collection
* @return array
*/
public function getTopIncome(): Collection
public function getTopIncome(): array
{
return $this->getIncome()->sortByDesc('transaction_amount');
$income = $this->getIncome();
usort($income, function ($a, $b) {
return $b['amount'] <=> $a['amount'];
});
return $income;
}
/**
* Get averages from a collection.
*
* @param Collection $collection
* @param int $sortFlag
* @param array $array
* @param int $sortFlag
*
* @return array
*/
protected function getAverages(Collection $collection, int $sortFlag): array
protected function getAverages(array $array, int $sortFlag): array
{
$result = [];
/** @var Transaction $transaction */
foreach ($collection as $transaction) {
/** @var array $journal */
foreach ($array as $journal) {
// opposing name and ID:
$opposingId = $transaction->opposing_account_id;
$opposingId = $journal['destination_account_id'];
// is not set?
if (!isset($result[$opposingId])) {
$name = $transaction->opposing_account_name;
$name = $journal['destination_account_name'];
$result[$opposingId] = [
'name' => $name,
'count' => 1,
'id' => $opposingId,
'average' => $transaction->transaction_amount,
'sum' => $transaction->transaction_amount,
'average' => $journal['amount'],
'sum' => $journal['amount'],
];
continue;
}
++$result[$opposingId]['count'];
$result[$opposingId]['sum'] = bcadd($result[$opposingId]['sum'], $transaction->transaction_amount);
$result[$opposingId]['sum'] = bcadd($result[$opposingId]['sum'], $journal['amount']);
$result[$opposingId]['average'] = bcdiv($result[$opposingId]['sum'], (string)$result[$opposingId]['count']);
}
@@ -151,18 +160,18 @@ class Support
/**
* Summarize the data by account.
*
* @param Collection $collection
* @param array $array
*
* @return array
*/
protected function summarizeByAccount(Collection $collection): array
protected function summarizeByAccount(array $array): array
{
$result = [];
/** @var Transaction $transaction */
foreach ($collection as $transaction) {
$accountId = $transaction->account_id;
/** @var array $journal */
foreach ($array as $journal) {
$accountId = $journal['source_account_id'] ?? 0;
$result[$accountId] = $result[$accountId] ?? '0';
$result[$accountId] = bcadd($transaction->transaction_amount, $result[$accountId]);
$result[$accountId] = bcadd($journal['amount'], $result[$accountId]);
}
return $result;

View File

@@ -28,12 +28,7 @@ namespace FireflyIII\Generator\Report\Tag;
use Carbon\Carbon;
use FireflyIII\Generator\Report\ReportGeneratorInterface;
use FireflyIII\Generator\Report\Support;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Helpers\Filter\DoubleTransactionFilter;
use FireflyIII\Helpers\Filter\NegativeAmountFilter;
use FireflyIII\Helpers\Filter\OpposingAccountFilter;
use FireflyIII\Helpers\Filter\PositiveAmountFilter;
use FireflyIII\Helpers\Filter\TransferFilter;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Tag;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
@@ -52,9 +47,9 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
private $accounts;
/** @var Carbon The end date */
private $end;
/** @var Collection The expenses involved */
/** @var array The expenses involved */
private $expenses;
/** @var Collection The income involved */
/** @var array The income involved */
private $income;
/** @var Carbon The start date */
private $start;
@@ -107,6 +102,80 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
return $result;
}
/**
* Get expense collection for report.
*
* @return array
*/
protected function getExpenses(): array
{
if (count($this->expenses) > 0) {
Log::debug('Return previous set of expenses.');
return $this->expenses;
}
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
->setTags($this->tags)->withAccountInformation();
$journals = $collector->getExtractedJournals();
$this->expenses = $journals;
return $journals;
}
/**
* Get the income for this report.
*
* @return array
*/
protected function getIncome(): array
{
if (count($this->income) > 0) {
return $this->income;
}
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
->setTags($this->tags)->withAccountInformation();
$journals = $collector->getExtractedJournals();
$this->income = $journals;
return $journals;
}
/**
* Summarize by tag.
*
* @param array $array
*
* @return array
*/
protected function summarizeByTag(array $array): array
{
$tagIds = array_map('\intval', $this->tags->pluck('id')->toArray());
$result = [];
/** @var array $journal */
foreach ($array as $journal) {
/** @var Tag $journalTag */
foreach ($journal['tag_ids'] as $journalTag) {
$journalTagId = (int)$journalTag;
if (\in_array($journalTagId, $tagIds, true)) {
$result[$journalTagId] = $result[$journalTagId] ?? '0';
$result[$journalTagId] = bcadd($journal['amount'], $result[$journalTagId]);
}
}
}
return $result;
}
/**
* Set the accounts.
*
@@ -198,88 +267,4 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
return $this;
}
/**
* Get expense collection for report.
*
* @return Collection
*/
protected function getExpenses(): Collection
{
if ($this->expenses->count() > 0) {
Log::debug('Return previous set of expenses.');
return $this->expenses;
}
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
->setTags($this->tags)->withOpposingAccount();
$collector->removeFilter(TransferFilter::class);
$collector->addFilter(OpposingAccountFilter::class);
$collector->addFilter(PositiveAmountFilter::class);
$collector->addFilter(DoubleTransactionFilter::class);
$transactions = $collector->getTransactions();
$this->expenses = $transactions;
return $transactions;
}
/**
* Get the income for this report.
*
* @return Collection
*/
protected function getIncome(): Collection
{
if ($this->income->count() > 0) {
return $this->income;
}
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
->setTags($this->tags)->withOpposingAccount();
$collector->addFilter(OpposingAccountFilter::class);
$collector->addFilter(NegativeAmountFilter::class);
$collector->addFilter(DoubleTransactionFilter::class);
$transactions = $collector->getTransactions();
$this->income = $transactions;
return $transactions;
}
/**
* Summarize by tag.
*
* @param Collection $collection
*
* @return array
*/
protected function summarizeByTag(Collection $collection): array
{
$tagIds = array_map('\intval', $this->tags->pluck('id')->toArray());
$result = [];
/** @var Transaction $transaction */
foreach ($collection as $transaction) {
$journal = $transaction->transactionJournal;
$journalTags = $journal->tags;
/** @var Tag $journalTag */
foreach ($journalTags as $journalTag) {
$journalTagId = (int)$journalTag->id;
if (\in_array($journalTagId, $tagIds, true)) {
$result[$journalTagId] = $result[$journalTagId] ?? '0';
$result[$journalTagId] = bcadd($transaction->transaction_amount, $result[$journalTagId]);
}
}
}
return $result;
}
}