This commit is contained in:
James Cole
2020-08-06 20:08:04 +02:00
parent ed0f04d644
commit eb256fced6
3 changed files with 90 additions and 41 deletions

View File

@@ -50,6 +50,7 @@ use Storage;
class BillRepository implements BillRepositoryInterface
{
use CreatesObjectGroups;
private User $user;
/**
@@ -387,26 +388,46 @@ class BillRepository implements BillRepositoryInterface
/**
* @param Bill $bill
*
* @return string
* @return array
*/
public function getOverallAverage(Bill $bill): string
public function getOverallAverage(Bill $bill): array
{
/** @var JournalRepositoryInterface $repos */
$repos = app(JournalRepositoryInterface::class);
$repos->setUser($this->user);
// get and sort on currency
$result = [];
$journals = $bill->transactionJournals()->get();
$sum = '0';
$count = (string)$journals->count();
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$sum = bcadd($sum, $repos->getJournalTotal($journal));
}
$avg = '0';
if ($journals->count() > 0) {
$avg = bcdiv($sum, $count);
/** @var Transaction $transaction */
$transaction = $journal->transactions()->where('amount', '<', 0)->first();
$currencyId = (int) $journal->transaction_currency_id;
$currency = $journal->transactionCurrency;
$result[$currencyId] = $result[$currencyId] ?? [
'sum' => '0',
'count' => 0,
'avg' => '0',
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
];
$result[$currencyId]['sum'] = bcadd($result[$currencyId]['sum'], $transaction->amount);
$result[$currencyId]['count']++;
}
return $avg;
// after loop, re-loop for avg.
/**
* @var int $currencyId
* @var array $arr
*/
foreach ($result as $currencyId => $arr) {
$result[$currencyId]['avg'] = bcdiv($arr['sum'], (string) $arr['count']);
}
return $result;
}
/**
@@ -535,30 +556,50 @@ class BillRepository implements BillRepositoryInterface
* @param Bill $bill
* @param Carbon $date
*
* @return string
* @return array
*/
public function getYearAverage(Bill $bill, Carbon $date): string
public function getYearAverage(Bill $bill, Carbon $date): array
{
/** @var JournalRepositoryInterface $repos */
$repos = app(JournalRepositoryInterface::class);
$repos->setUser($this->user);
// get and sort on currency
$result = [];
$journals = $bill->transactionJournals()
->where('date', '>=', $date->year . '-01-01 00:00:00')
->where('date', '<=', $date->year . '-12-31 23:59:59')
->get();
$sum = '0';
$count = (string)$journals->count();
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$sum = bcadd($sum, $repos->getJournalTotal($journal));
}
$avg = '0';
if ($journals->count() > 0) {
$avg = bcdiv($sum, $count);
/** @var Transaction $transaction */
$transaction = $journal->transactions()->where('amount', '<', 0)->first();
$currencyId = (int) $journal->transaction_currency_id;
$currency = $journal->transactionCurrency;
$result[$currencyId] = $result[$currencyId] ?? [
'sum' => '0',
'count' => 0,
'avg' => '0',
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
];
$result[$currencyId]['sum'] = bcadd($result[$currencyId]['sum'], $transaction->amount);
$result[$currencyId]['count']++;
}
return $avg;
// after loop, re-loop for avg.
/**
* @var int $currencyId
* @var array $arr
*/
foreach ($result as $currencyId => $arr) {
$result[$currencyId]['avg'] = bcdiv($arr['sum'], (string) $arr['count']);
}
return $result;
}
/**

View File

@@ -198,9 +198,9 @@ interface BillRepositoryInterface
/**
* @param Bill $bill
*
* @return string
* @return array
*/
public function getOverallAverage(Bill $bill): string;
public function getOverallAverage(Bill $bill): array;
/**
* @param int $size
@@ -254,9 +254,9 @@ interface BillRepositoryInterface
* @param Bill $bill
* @param Carbon $date
*
* @return string
* @return array
*/
public function getYearAverage(Bill $bill, Carbon $date): string;
public function getYearAverage(Bill $bill, Carbon $date): array;
/**
* Link a set of journals to a bill.

View File

@@ -55,11 +55,19 @@
</tr>
<tr>
<td>{{ trans('firefly.average_bill_amount_year', {year: year}) }}</td>
<td>{{ formatAmountByCurrency(object.data.currency,yearAverage) }}</td>
<td>
{% for avg in yearAverage %}
{{ formatAmountBySymbol(avg.avg, avg.currency_symbol, avg.currency_decimal_places, true) }}<br>
{% endfor %}
</td>
</tr>
<tr>
<td>{{ 'average_bill_amount_overall'|_ }}</td>
<td>{{ formatAmountByCurrency(object.data.currency, overallAverage) }}</td>
<td>
{% for avg in overallAverage %}
{{ formatAmountBySymbol(avg.avg, avg.currency_symbol, avg.currency_decimal_places, true) }}<br>
{% endfor %}
</td>
</tr>
</table>
</div>