Various code cleanup.

This commit is contained in:
James Cole
2016-11-26 10:53:20 +01:00
parent 22d2a523fb
commit 0e66939408
3 changed files with 53 additions and 135 deletions

View File

@@ -43,14 +43,5 @@ interface BudgetChartGeneratorInterface
* *
* @return array * @return array
*/ */
public function period(array $entries) : array; public function period(array $entries): array;
/**
* @param Collection $budgets
* @param Collection $entries
*
* @return array
*/
public function year(Collection $budgets, Collection $entries): array;
} }

View File

@@ -133,42 +133,4 @@ class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface
return $data; return $data;
} }
/**
* @param Collection $budgets
* @param Collection $entries
*
* @return array
*/
public function year(Collection $budgets, Collection $entries): array
{
// language:
$format = (string)trans('config.month');
$data = [
'labels' => [],
'datasets' => [],
];
foreach ($budgets as $budget) {
$data['labels'][] = $budget->name;
}
// also add "no budget"
$data['labels'][] = strval(trans('firefly.no_budget'));
/** @var array $entry */
foreach ($entries as $entry) {
$array = [
'label' => $entry[0]->formatLocalized($format),
'data' => [],
];
array_shift($entry);
$array['data'] = $entry;
$data['datasets'][] = $array;
}
$data['count'] = count($data['datasets']);
return $data;
}
} }

View File

@@ -60,10 +60,12 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
$accountIds = join(',', $this->accounts->pluck('id')->toArray()); $accountIds = join(',', $this->accounts->pluck('id')->toArray());
$categoryIds = join(',', $this->categories->pluck('id')->toArray()); $categoryIds = join(',', $this->categories->pluck('id')->toArray());
$reportType = 'category'; $reportType = 'category';
$accountSummary = $this->getObjectSummary($this->getSpentAccountSummary(), $this->getEarnedAccountSummary()); $expenses = $this->getExpenses();
$categorySummary = $this->getObjectSummary($this->getSpentCategorySummary(), $this->getEarnedCategorySummary()); $income = $this->getIncome();
$averageExpenses = $this->getAverages($this->getExpenses(), SORT_ASC); $accountSummary = $this->getObjectSummary($this->summarizeByAccount($expenses), $this->summarizeByAccount($income));
$averageIncome = $this->getAverages($this->getIncome(), SORT_DESC); $categorySummary = $this->getObjectSummary($this->summarizeByCategory($expenses), $this->summarizeByCategory($income));
$averageExpenses = $this->getAverages($expenses, SORT_ASC);
$averageIncome = $this->getAverages($income, SORT_DESC);
$topExpenses = $this->getTopExpenses(); $topExpenses = $this->getTopExpenses();
$topIncome = $this->getTopIncome(); $topIncome = $this->getTopIncome();
@@ -173,43 +175,6 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
return $result; return $result;
} }
/**
* @return array
*/
private function getEarnedAccountSummary(): array
{
$transactions = $this->getIncome();
$result = [];
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$accountId = $transaction->account_id;
$result[$accountId] = $result[$accountId] ?? '0';
$result[$accountId] = bcadd($transaction->transaction_amount, $result[$accountId]);
}
return $result;
}
/**
* @return array
*/
private function getEarnedCategorySummary(): array
{
$transactions = $this->getIncome();
$result = [];
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$jrnlCatId = intval($transaction->transaction_journal_category_id);
$transCatId = intval($transaction->transaction_category_id);
$categoryId = max($jrnlCatId, $transCatId);
$result[$categoryId] = $result[$categoryId] ?? '0';
$result[$categoryId] = bcadd($transaction->transaction_amount, $result[$categoryId]);
}
return $result;
}
/** /**
* @return Collection * @return Collection
*/ */
@@ -269,12 +234,12 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
* @var int $accountId * @var int $accountId
* @var string $entry * @var string $entry
*/ */
foreach ($spent as $accountId => $entry) { foreach ($spent as $objectId => $entry) {
if (!isset($return[$accountId])) { if (!isset($return[$objectId])) {
$return[$accountId] = ['spent' => 0, 'earned' => 0]; $return[$objectId] = ['spent' => 0, 'earned' => 0];
} }
$return[$accountId]['spent'] = $entry; $return[$objectId]['spent'] = $entry;
} }
unset($entry); unset($entry);
@@ -282,56 +247,18 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
* @var int $accountId * @var int $accountId
* @var string $entry * @var string $entry
*/ */
foreach ($earned as $accountId => $entry) { foreach ($earned as $objectId => $entry) {
if (!isset($return[$accountId])) { if (!isset($return[$objectId])) {
$return[$accountId] = ['spent' => 0, 'earned' => 0]; $return[$objectId] = ['spent' => 0, 'earned' => 0];
} }
$return[$accountId]['earned'] = $entry; $return[$objectId]['earned'] = $entry;
} }
return $return; return $return;
} }
/**
* @return array
*/
private function getSpentAccountSummary(): array
{
$transactions = $this->getExpenses();
$result = [];
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$accountId = $transaction->account_id;
$result[$accountId] = $result[$accountId] ?? '0';
$result[$accountId] = bcadd($transaction->transaction_amount, $result[$accountId]);
}
return $result;
}
/**
* @return array
*/
private function getSpentCategorySummary(): array
{
$transactions = $this->getExpenses();
$result = [];
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$jrnlCatId = intval($transaction->transaction_journal_category_id);
$transCatId = intval($transaction->transaction_category_id);
$categoryId = max($jrnlCatId, $transCatId);
$result[$categoryId] = $result[$categoryId] ?? '0';
$result[$categoryId] = bcadd($transaction->transaction_amount, $result[$categoryId]);
}
return $result;
}
/** /**
* @return Collection * @return Collection
@@ -368,4 +295,42 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
return $transactions; return $transactions;
} }
/**
* @param Collection $collection
*
* @return array
*/
private function summarizeByAccount(Collection $collection): array
{
$result = [];
/** @var Transaction $transaction */
foreach ($collection as $transaction) {
$accountId = $transaction->account_id;
$result[$accountId] = $result[$accountId] ?? '0';
$result[$accountId] = bcadd($transaction->transaction_amount, $result[$accountId]);
}
return $result;
}
/**
* @param Collection $collection
*
* @return array
*/
private function summarizeByCategory(Collection $collection): array
{
$result = [];
/** @var Transaction $transaction */
foreach ($collection as $transaction) {
$jrnlCatId = intval($transaction->transaction_journal_category_id);
$transCatId = intval($transaction->transaction_category_id);
$categoryId = max($jrnlCatId, $transCatId);
$result[$categoryId] = $result[$categoryId] ?? '0';
$result[$categoryId] = bcadd($transaction->transaction_amount, $result[$categoryId]);
}
return $result;
}
} }