From 78deb1420df823abfcce6da8d90db6bc8961afeb Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 23 Oct 2016 14:56:05 +0200 Subject: [PATCH] Some fixes for bills. --- app/Http/Controllers/BillController.php | 24 +++----- app/Repositories/Bill/BillRepository.php | 19 +++++- .../Bill/BillRepositoryInterface.php | 13 ++++- resources/lang/en_US/firefly.php | 3 +- resources/lang/en_US/list.php | 2 +- resources/views/list/bills.twig | 58 ++++++++++--------- 6 files changed, 70 insertions(+), 49 deletions(-) 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)); + + // 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); } ); 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 @@ {{ trans('list.name') }} {{ trans('list.matchesOn') }} {{ trans('list.matchingAmount') }} - {{ trans('list.paid_current_period') }} - {{ trans('list.next_expected_match') }} + {{ trans('list.paid_current_period') }} + {{ trans('list.next_expected_match') }} {{ trans('list.active') }} {{ trans('list.automatch') }} {{ trans('list.repeat_freq') }} @@ -35,37 +35,41 @@ {{ entry.amount_max|formatAmount }} - {% if not entry.lastFoundMatch.isFuture %} - - {{ 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 %} + + {{ 'not_expected_period'|_ }} - {% else %} - - {{ 'unknown'|_ }} + + {{ entry.nextExpectedMatch.formatLocalized(monthAndDayFormat) }} {% endif %} - - {% if entry.nextExpectedMatch.year == 1900 %} - - {{ 'unknown'|_ }} + {% if entry.paidDates.count() == 0 and entry.payDates.count() > 0 and entry.active %} + + {{ 'not_or_not_yet'|_ }} - {% else %} - - ~ {{ entry.nextExpectedMatch.formatLocalized(monthAndDayFormat) }} + + {{ entry.nextExpectedMatch.formatLocalized(monthAndDayFormat) }} + + {% endif %} + {% if entry.paidDates.count() == entry.payDates.count() and entry.payDates.count() > 0 and entry.active %} + + {% for date in entry.paidDates %} + {{ date.formatLocalized(monthAndDayFormat) }}
+ {% endfor %} + + + {{ entry.nextExpectedMatch.formatLocalized(monthAndDayFormat) }} + + {% endif %} + {% if not entry.active %} + + ~ + + + ~ {% endif %} - {% if entry.active %}