Skip to content

Firefly III: a personal finances manager

Some fixes for bills.

James Cole78deb1420df8
diff --git a/app/Http/Controllers/BillController.php b/app/Http/Controllers/BillController.php
index aef8266959..d6095d8360 100644
--- a/app/Http/Controllers/BillController.php
+++ b/app/Http/Controllers/BillController.php
@@ -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));
- }
-
- $bill->lastPaidAmount = $average;
- $bill->paidInPeriod = ($start <= $bill->lastFoundMatch) && ($end >= $bill->lastFoundMatch);
+ // 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->nextExpectedMatch = $repository->nextExpectedMatch($bill, $lastDate);
}
);
diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php
index 74c69de499..dd7b6631cc 100644
--- a/app/Repositories/Bill/BillRepository.php
+++ b/app/Repositories/Bill/BillRepository.php
@@ -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.
*
diff --git a/app/Repositories/Bill/BillRepositoryInterface.php b/app/Repositories/Bill/BillRepositoryInterface.php
index 82f849c468..4c70efc871 100644
--- a/app/Repositories/Bill/BillRepositoryInterface.php
+++ b/app/Repositories/Bill/BillRepositoryInterface.php
@@ -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.
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index 2ebe1b7693..c5a986c6cd 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -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"',
diff --git a/resources/lang/en_US/list.php b/resources/lang/en_US/list.php
index ba44232dd4..a4ec03ab72 100644
--- a/resources/lang/en_US/list.php
+++ b/resources/lang/en_US/list.php
@@ -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',
diff --git a/resources/views/list/bills.twig b/resources/views/list/bills.twig
index 1f2a8a51f7..2443bda422 100644
--- a/resources/views/list/bills.twig
+++ b/resources/views/list/bills.twig
@@ -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>