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
2 changed files with 50 additions and 24 deletions

View File

@@ -163,13 +163,12 @@ class ReportHelper implements ReportHelperInterface
foreach ($budgets as $budget) { foreach ($budgets as $budget) {
$data[$budget->id] = [ $data[$budget->id] = [
'name' => $budget->name, '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; return $data;
} }
@@ -387,27 +386,28 @@ class ReportHelper implements ReportHelperInterface
} }
/** /**
* @param Collection $set * @param array $data
* @param int $budgetId
* @param int $year
* *
* @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( * @var int $budgetId
function (TransactionJournal $object) use ($budgetId, $year) { * @var array $set
return intval($object->the_year) === $year && $budgetId === intval($object->budget_id); */
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; 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> <a href="{{ route('budgets.show', id) }}">{{ info.name }}</a>
{% endif %} {% endif %}
</td> </td>
{% set sum = 0 %}
{% for amount in info.entries %} {% for amount in info.entries %}
<td data-value="{{ amount }}"> <td data-value="{{ amount }}">
{{ amount|formatAmount }} {{ amount|formatAmount }}
</td> </td>
{% set sum = sum + amount %}
{% endfor %} {% endfor %}
<td data-value="{{ sum }}"> <td data-value="{{ info.sum }}">
{{ sum|formatAmount }} {{ info.sum|formatAmount }}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}