Small optimisations.

Signed-off-by: James Cole <thegrumpydictator@gmail.com>
This commit is contained in:
James Cole 2016-11-02 07:23:11 +01:00
parent 4ba34ab511
commit b980b5baea
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
2 changed files with 50 additions and 24 deletions

View File

@ -163,13 +163,12 @@ class ReportHelper implements ReportHelperInterface
foreach ($budgets as $budget) {
$data[$budget->id] = [
'name' => $budget->name,
'entries' => [],
'entries' => $this->filterAmounts($queryResult, $budget->id, $years),
'sum' => '0',
];
foreach ($years as $year) {
// filter query result here!
$data[$budget->id]['entries'][$year] = $this->filterAmount($queryResult, $budget->id, $year);
}
}
// filter out empty ones and fill sum:
$data = $this->getBudgetMultiYearMeta($data);
return $data;
}
@ -387,27 +386,28 @@ class ReportHelper implements ReportHelperInterface
}
/**
* @param Collection $set
* @param int $budgetId
* @param int $year
* @param array $data
*
* @return string
* @return array
*/
protected function filterAmount(Collection $set, int $budgetId, int $year): string
protected function getBudgetMultiYearMeta(array $data): array
{
/** @var stdClass $object */
$result = $set->filter(
function (TransactionJournal $object) use ($budgetId, $year) {
return intval($object->the_year) === $year && $budgetId === intval($object->budget_id);
/**
* @var int $budgetId
* @var array $set
*/
foreach ($data as $budgetId => $set) {
$sum = '0';
foreach ($set['entries'] as $amount) {
$sum = bcadd($amount, $sum);
}
$data[$budgetId]['sum'] = $sum;
if (bccomp('0', $sum) === 0) {
unset($data[$budgetId]);
}
);
$amount = '0';
if (!is_null($result->first())) {
$amount = $result->first()->sum_of_period;
}
return $amount;
return $data;
}
/**
@ -437,5 +437,33 @@ class ReportHelper implements ReportHelperInterface
return $sum;
}
/**
* @param Collection $set
* @param int $budgetId
* @param array $years
*
* @return array
*/
private function filterAmounts(Collection $set, int $budgetId, array $years):array
{
$arr = [];
foreach ($years as $year) {
/** @var stdClass $object */
$result = $set->filter(
function (TransactionJournal $object) use ($budgetId, $year) {
return intval($object->the_year) === $year && $budgetId === intval($object->budget_id);
}
);
$amount = '0';
if (!is_null($result->first())) {
$amount = $result->first()->sum_of_period;
}
$arr[$year] = $amount;
}
return $arr;
}
}

View File

@ -113,15 +113,13 @@
<a href="{{ route('budgets.show', id) }}">{{ info.name }}</a>
{% endif %}
</td>
{% set sum = 0 %}
{% for amount in info.entries %}
<td data-value="{{ amount }}">
{{ amount|formatAmount }}
</td>
{% set sum = sum + amount %}
{% endfor %}
<td data-value="{{ sum }}">
{{ sum|formatAmount }}
<td data-value="{{ info.sum }}">
{{ info.sum|formatAmount }}
</td>
</tr>
{% endfor %}