Various code cleanup.

This commit is contained in:
James Cole 2016-11-26 10:53:20 +01:00
parent 22d2a523fb
commit 0e66939408
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 53 additions and 135 deletions

View File

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

View File

@ -133,42 +133,4 @@ class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface
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());
$categoryIds = join(',', $this->categories->pluck('id')->toArray());
$reportType = 'category';
$accountSummary = $this->getObjectSummary($this->getSpentAccountSummary(), $this->getEarnedAccountSummary());
$categorySummary = $this->getObjectSummary($this->getSpentCategorySummary(), $this->getEarnedCategorySummary());
$averageExpenses = $this->getAverages($this->getExpenses(), SORT_ASC);
$averageIncome = $this->getAverages($this->getIncome(), SORT_DESC);
$expenses = $this->getExpenses();
$income = $this->getIncome();
$accountSummary = $this->getObjectSummary($this->summarizeByAccount($expenses), $this->summarizeByAccount($income));
$categorySummary = $this->getObjectSummary($this->summarizeByCategory($expenses), $this->summarizeByCategory($income));
$averageExpenses = $this->getAverages($expenses, SORT_ASC);
$averageIncome = $this->getAverages($income, SORT_DESC);
$topExpenses = $this->getTopExpenses();
$topIncome = $this->getTopIncome();
@ -173,43 +175,6 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
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
*/
@ -269,12 +234,12 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
* @var int $accountId
* @var string $entry
*/
foreach ($spent as $accountId => $entry) {
if (!isset($return[$accountId])) {
$return[$accountId] = ['spent' => 0, 'earned' => 0];
foreach ($spent as $objectId => $entry) {
if (!isset($return[$objectId])) {
$return[$objectId] = ['spent' => 0, 'earned' => 0];
}
$return[$accountId]['spent'] = $entry;
$return[$objectId]['spent'] = $entry;
}
unset($entry);
@ -282,56 +247,18 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
* @var int $accountId
* @var string $entry
*/
foreach ($earned as $accountId => $entry) {
if (!isset($return[$accountId])) {
$return[$accountId] = ['spent' => 0, 'earned' => 0];
foreach ($earned as $objectId => $entry) {
if (!isset($return[$objectId])) {
$return[$objectId] = ['spent' => 0, 'earned' => 0];
}
$return[$accountId]['earned'] = $entry;
$return[$objectId]['earned'] = $entry;
}
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
@ -368,4 +295,42 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
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;
}
}