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 @@