diff --git a/app/Http/Controllers/BillController.php b/app/Http/Controllers/BillController.php index f1c9cb2db4..47ca757b4d 100644 --- a/app/Http/Controllers/BillController.php +++ b/app/Http/Controllers/BillController.php @@ -11,6 +11,7 @@ declare(strict_types = 1); namespace FireflyIII\Http\Controllers; +use Carbon\Carbon; use FireflyIII\Http\Requests\BillFormRequest; use FireflyIII\Models\Bill; use FireflyIII\Models\TransactionJournal; @@ -190,15 +191,20 @@ class BillController extends Controller */ public function show(BillRepositoryInterface $repository, Bill $bill) { - $page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page')); - $pageSize = Preferences::get('transactionPageSize', 50)->data; - $journals = $repository->getJournals($bill, $page, $pageSize); + /** @var Carbon $date */ + $date = session('start'); + $year = $date->year; + $page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page')); + $pageSize = Preferences::get('transactionPageSize', 50)->data; + $journals = $repository->getJournals($bill, $page, $pageSize); + $yearAverage = $repository->getYearAverage($bill, $date); + $overallAverage = $repository->getOverallAverage($bill); $journals->setPath('/bills/show/' . $bill->id); $bill->nextExpectedMatch = $repository->nextExpectedMatch($bill); $hideBill = true; $subTitle = e($bill->name); - return view('bills.show', compact('journals', 'hideBill', 'bill', 'subTitle')); + return view('bills.show', compact('journals', 'yearAverage', 'overallAverage', 'year', 'hideBill', 'bill', 'subTitle')); } /** diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index 7a6c99375d..43aabe96cf 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -557,4 +557,52 @@ class BillRepository implements BillRepositoryInterface return $wordMatch; } + + /** + * @param Bill $bill + * @param Carbon $date + * + * @return string + */ + public function getYearAverage(Bill $bill, Carbon $date): string + { + $journals = $bill->transactionjournals() + ->where('date', '>=', $date->year . '-01-01') + ->where('date', '<=', $date->year . '-12-31') + ->get(); + $sum = '0'; + $count = strval($journals->count()); + /** @var TransactionJournal $journal */ + foreach ($journals as $journal) { + $sum = bcadd($sum, TransactionJournal::amountPositive($journal)); + } + $avg = '0'; + if ($journals->count() > 0) { + $avg = bcdiv($sum, $count); + } + + return $avg; + } + + /** + * @param $bill + * + * @return string + */ + public function getOverallAverage($bill): string + { + $journals = $bill->transactionjournals()->get(); + $sum = '0'; + $count = strval($journals->count()); + /** @var TransactionJournal $journal */ + foreach ($journals as $journal) { + $sum = bcadd($sum, TransactionJournal::amountPositive($journal)); + } + $avg = '0'; + if ($journals->count() > 0) { + $avg = bcdiv($sum, $count); + } + + return $avg; + } } diff --git a/app/Repositories/Bill/BillRepositoryInterface.php b/app/Repositories/Bill/BillRepositoryInterface.php index 3305e26d68..494b4d2d7f 100644 --- a/app/Repositories/Bill/BillRepositoryInterface.php +++ b/app/Repositories/Bill/BillRepositoryInterface.php @@ -32,6 +32,21 @@ interface BillRepositoryInterface */ public function destroy(Bill $bill): bool; + /** + * @param Bill $bill + * @param Carbon $date + * + * @return string + */ + public function getYearAverage(Bill $bill, Carbon $date): string; + + /** + * @param $bill + * + * @return string + */ + public function getOverallAverage($bill): string; + /** * Find a bill by ID. * diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 077c75cb33..ecb93831af 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -415,6 +415,8 @@ return [ 'cannot_scan_inactive_bill' => 'Inactive bills cannot be scanned.', 'rescanned_bill' => 'Rescanned everything.', 'bill_date_little_relevance' => 'The only part of this date used by Firefly is the day. It is only useful when your bill arrives at exactly the same date every month. If the payment date of your bills varies, simply use the first of the month.', + 'average_bill_amount_year' => 'Average bill amount (:year)', + 'average_bill_amount_overall' => 'Average bill amount (overall)', // accounts: 'details_for_asset' => 'Details for asset account ":name"', diff --git a/resources/views/bills/show.twig b/resources/views/bills/show.twig index ab5303ab6e..45cf8cddb1 100644 --- a/resources/views/bills/show.twig +++ b/resources/views/bills/show.twig @@ -60,6 +60,14 @@ {% endif %} + + {{ trans('firefly.average_bill_amount_year', {year: year}) }} + {{ '0'|formatAmount }} + + + {{ 'average_bill_amount_overall'|_ }} + {{ '0'|formatAmount }} +