From e4f45b5370bc8d7e97243038c2fa0a38d71718fa Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 29 Apr 2016 22:00:24 +0200 Subject: [PATCH] List of categories will check transactions as well. #142 --- .../Category/SingleCategoryRepository.php | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/app/Repositories/Category/SingleCategoryRepository.php b/app/Repositories/Category/SingleCategoryRepository.php index 5f0ec5b771..1c6e35f4b9 100644 --- a/app/Repositories/Category/SingleCategoryRepository.php +++ b/app/Repositories/Category/SingleCategoryRepository.php @@ -209,20 +209,36 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate /** * @param Category $category * - * @return Carbon|null + * @return Carbon */ public function getLatestActivity(Category $category): Carbon { + $first = new Carbon('1900-01-01'); + $second = new Carbon('1900-01-01'); $latest = $category->transactionjournals() ->orderBy('transaction_journals.date', 'DESC') ->orderBy('transaction_journals.order', 'ASC') ->orderBy('transaction_journals.id', 'DESC') ->first(); if ($latest) { - return $latest->date; + $first = $latest->date; } - return new Carbon('1900-01-01'); + // could also be a transaction, nowadays: + $latestTransaction = $category->transactions() + ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') + ->orderBy('transaction_journals.date', 'DESC') + ->orderBy('transaction_journals.order', 'ASC') + ->orderBy('transaction_journals.id', 'DESC') + ->first(['transactions.*', 'transaction_journals.date']); + if ($latestTransaction) { + $second = new Carbon($latestTransaction->date); + } + if ($first > $second) { + return $first; + } + + return $second; } /**