Make some parts of the budget repository multi-currency

This commit is contained in:
James Cole
2019-09-02 16:52:35 +02:00
parent 8246d901e7
commit 0fd7e4363d
10 changed files with 408 additions and 185 deletions

View File

@@ -66,17 +66,13 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
*/
public function generate(): string
{
$accountIds = implode(',', $this->accounts->pluck('id')->toArray());
$budgetIds = implode(',', $this->budgets->pluck('id')->toArray());
$expenses = $this->getExpenses();
$accountSummary = $this->summarizeByAccount($expenses);
$budgetSummary = $this->summarizeByBudget($expenses);
$averageExpenses = $this->getAverages($expenses, SORT_ASC);
$topExpenses = $this->getTopExpenses();
// render!
$accountIds = implode(',', $this->accounts->pluck('id')->toArray());
$budgetIds = implode(',', $this->budgets->pluck('id')->toArray());
try {
$result = view('reports.budget.month', compact('accountIds', 'budgetIds', 'accountSummary', 'budgetSummary', 'averageExpenses', 'topExpenses'))
$result = view(
'reports.budget.month',
compact('accountIds', 'budgetIds')
)
->with('start', $this->start)->with('end', $this->end)
->with('budgets', $this->budgets)
->with('accounts', $this->accounts)
@@ -89,57 +85,6 @@ 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.
*
@@ -231,4 +176,31 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
{
return $this;
}
/**
* 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;
}
}