Some fixes for bills.

This commit is contained in:
James Cole
2016-10-23 14:56:05 +02:00
parent e092515dff
commit 78deb1420d
6 changed files with 70 additions and 49 deletions

View File

@@ -137,23 +137,15 @@ class BillController extends Controller
$bills = $repository->getBills();
$bills->each(
function (Bill $bill) use ($repository, $start, $end) {
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill, new Carbon);
$bill->lastFoundMatch = $repository->lastFoundMatch($bill);
$journals = $repository->getJournalsInRange($bill, $start, $end);
// loop journals, find average:
$average = '0';
$count = $journals->count();
if ($count > 0) {
$sum = '0';
foreach ($journals as $journal) {
$sum = bcadd($sum, TransactionJournal::amountPositive($journal));
}
$average = bcdiv($sum, strval($count));
// paid in this period?
$bill->paidDates = $repository->getPaidDatesInRange($bill, $start, $end);
$bill->payDates = $repository->getPayDatesInRange($bill, $start, $end);
$lastDate = clone $start;
if ($bill->paidDates->count() >= $bill->payDates->count()) {
$lastDate = $end;
}
$bill->lastPaidAmount = $average;
$bill->paidInPeriod = ($start <= $bill->lastFoundMatch) && ($end >= $bill->lastFoundMatch);
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill, $lastDate);
}
);

View File

@@ -326,11 +326,11 @@ class BillRepository implements BillRepositoryInterface
}
/**
* @param $bill
* @param Bill $bill
*
* @return string
*/
public function getOverallAverage($bill): string
public function getOverallAverage(Bill $bill): string
{
$journals = $bill->transactionJournals()->get();
$sum = '0';
@@ -347,6 +347,21 @@ class BillRepository implements BillRepositoryInterface
return $avg;
}
/**
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getPaidDatesInRange(Bill $bill, Carbon $start, Carbon $end): Collection
{
$dates = $bill->transactionJournals()->before($end)->after($start)->get(['transaction_journals.date'])->pluck('date');
return $dates;
}
/**
* Between start and end, tells you on which date(s) the bill is expected to hit.
*

View File

@@ -125,11 +125,20 @@ interface BillRepositoryInterface
public function getJournalsInRange(Bill $bill, Carbon $start, Carbon $end): Collection;
/**
* @param $bill
* @param Bill $bill
*
* @return string
*/
public function getOverallAverage($bill): string;
public function getOverallAverage(Bill $bill): string;
/**
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getPaidDatesInRange(Bill $bill, Carbon $start, Carbon $end): Collection;
/**
* Between start and end, tells you on which date(s) the bill is expected to hit.

View File

@@ -427,7 +427,8 @@ return [
'rescanned_bill' => 'Rescanned everything.',
'average_bill_amount_year' => 'Average bill amount (:year)',
'average_bill_amount_overall' => 'Average bill amount (overall)',
'not_or_not_yet' => 'Not (yet)',
'not_expected_period' => 'Not expected this period',
// accounts:
'details_for_asset' => 'Details for asset account ":name"',
'details_for_expense' => 'Details for expense account ":name"',

View File

@@ -33,7 +33,7 @@ return [
'split_number' => 'Split #',
'destination' => 'Destination',
'source' => 'Source',
'expectedMatch' => 'Expected match',
'next_expected_match' => 'Next expected match',
'automatch' => 'Auto match?',
'repeat_freq' => 'Repeats',
'description' => 'Description',

View File

@@ -5,8 +5,8 @@
<th>{{ trans('list.name') }}</th>
<th class="hidden-sm hidden-xs">{{ trans('list.matchesOn') }}</th>
<th colspan="2">{{ trans('list.matchingAmount') }}</th>
<th class="hidden-sm hidden-xs">{{ trans('list.paid_current_period') }}</th>
<th class="hidden-sm hidden-xs">{{ trans('list.next_expected_match') }}</th>
<th class="hidden-sm hidden-xs" data-defaultsort="disabled">{{ trans('list.paid_current_period') }}</th>
<th class="hidden-sm hidden-xs" data-defaultsort="disabled">{{ trans('list.next_expected_match') }}</th>
<th class="hidden-sm hidden-xs">{{ trans('list.active') }}</th>
<th class="hidden-sm hidden-xs">{{ trans('list.automatch') }}</th>
<th class="hidden-sm hidden-xs">{{ trans('list.repeat_freq') }}</th>
@@ -35,37 +35,41 @@
<td data-value="{{ entry.amount_max }}">
{{ entry.amount_max|formatAmount }}
</td>
{% if not entry.lastFoundMatch.isFuture %}
<td class="hidden-sm hidden-xs
{% if entry.active %}
{% if entry.paidInPeriod %}
bg-success
{% else %}
bg-danger
{% endif %}
{% endif %}
" data-value="{{ entry.lastFoundMatch.format('U') }}">
{{ entry.lastFoundMatch.formatLocalized(monthAndDayFormat) }}
{% if entry.active and entry.paidInPeriod %}
({{ entry.lastPaidAmount|formatAmountPlain }})
{% endif %}
{% if entry.paidDates.count() == 0 and entry.payDates.count() == 0 and entry.active %}
<td class="text-muted">
{{ 'not_expected_period'|_ }}
</td>
{% else %}
<td class="hidden-sm hidden-xs" data-value="0">
<em>{{ 'unknown'|_ }}</em>
<td class=" hidden-sm hidden-xs">
{{ entry.nextExpectedMatch.formatLocalized(monthAndDayFormat) }}
</td>
{% endif %}
{% if entry.nextExpectedMatch.year == 1900 %}
<td class="hidden-sm hidden-xs" data-value="0">
<em>{{ 'unknown'|_ }}</em>
{% if entry.paidDates.count() == 0 and entry.payDates.count() > 0 and entry.active %}
<td class="text-danger">
{{ 'not_or_not_yet'|_ }}
</td>
{% else %}
<td class="hidden-sm hidden-xs" data-value="{{ entry.nextExpectedMatch.format('U') }}">
~ {{ entry.nextExpectedMatch.formatLocalized(monthAndDayFormat) }}
<td class=" hidden-sm hidden-xs">
{{ entry.nextExpectedMatch.formatLocalized(monthAndDayFormat) }}
</td>
{% endif %}
{% if entry.paidDates.count() == entry.payDates.count() and entry.payDates.count() > 0 and entry.active %}
<td class="text-success">
{% for date in entry.paidDates %}
{{ date.formatLocalized(monthAndDayFormat) }}<br/>
{% endfor %}
</td>
<td class=" hidden-sm hidden-xs">
{{ entry.nextExpectedMatch.formatLocalized(monthAndDayFormat) }}
</td>
{% endif %}
{% if not entry.active %}
<td class="text-muted">
~
</td>
<td class="text-muted hidden-sm hidden-xs">
~
</td>
{% endif %}
<td class="hidden-sm hidden-xs" data-value="{{ entry.active }}">
{% if entry.active %}
<i class="fa fa-fw fa-check"></i>