diff --git a/app/Helpers/Collector/JournalCollector.php b/app/Helpers/Collector/JournalCollector.php index 030e7ec7f1..bc9ca44616 100644 --- a/app/Helpers/Collector/JournalCollector.php +++ b/app/Helpers/Collector/JournalCollector.php @@ -206,6 +206,22 @@ class JournalCollector return $this; } + /** + * @param Collection $bills + * + * @return JournalCollector + */ + public function setBills(Collection $bills): JournalCollector + { + if ($bills->count() > 0) { + $billIds = $bills->pluck('id')->toArray(); + $this->query->whereIn('transaction_journals.bill_id', $billIds); + } + + return $this; + + } + /** * @param Category $category * diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php index b95ac24315..27c76bb3c5 100644 --- a/app/Helpers/Report/ReportHelper.php +++ b/app/Helpers/Report/ReportHelper.php @@ -14,17 +14,17 @@ declare(strict_types = 1); namespace FireflyIII\Helpers\Report; use Carbon\Carbon; -use DB; use FireflyIII\Helpers\Collection\Bill as BillCollection; use FireflyIII\Helpers\Collection\BillLine; use FireflyIII\Helpers\Collection\Category as CategoryCollection; use FireflyIII\Helpers\Collection\Expense; use FireflyIII\Helpers\Collection\Income; +use FireflyIII\Helpers\Collector\JournalCollector; use FireflyIII\Helpers\FiscalHelperInterface; use FireflyIII\Models\Bill; -use FireflyIII\Models\Budget; use FireflyIII\Models\Category; use FireflyIII\Models\Tag; +use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Account\AccountTaskerInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface; @@ -79,7 +79,9 @@ class ReportHelper implements ReportHelperInterface /** @var BillRepositoryInterface $repository */ $repository = app(BillRepositoryInterface::class); $bills = $repository->getBillsForAccounts($accounts); - $journals = $repository->getAllJournalsInRange($bills, $start, $end); + $collector = new JournalCollector(auth()->user()); + $collector->setAccounts($accounts)->setRange($start, $end)->setBills($bills); + $journals = $collector->getJournals(); $collection = new BillCollection; /** @var Bill $bill */ @@ -93,14 +95,14 @@ class ReportHelper implements ReportHelperInterface // is hit in period? $entry = $journals->filter( - function (TransactionJournal $journal) use ($bill) { - return $journal->bill_id === $bill->id; + function (Transaction $transaction) use ($bill) { + return $transaction->bill_id === $bill->id; } ); $first = $entry->first(); if (!is_null($first)) { $billLine->setTransactionJournalId($first->id); - $billLine->setAmount($first->journalAmount); + $billLine->setAmount($first->transaction_amount); $billLine->setHit(true); } diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index f086a4b19b..2a34567986 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -116,41 +116,6 @@ class BillRepository implements BillRepositoryInterface return $set; } - /** - * Returns all journals connected to these bills in the given range. Amount paid - * is stored in "journalAmount" as a negative number. - * - * @param Collection $bills - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getAllJournalsInRange(Collection $bills, Carbon $start, Carbon $end): Collection - { - $ids = $bills->pluck('id')->toArray(); - - $set = $this->user->transactionJournals() - ->leftJoin( - 'transactions', function (JoinClause $join) { - $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0); - } - ) - ->whereIn('bill_id', $ids) - ->before($end) - ->after($start) - ->groupBy(['transaction_journals.bill_id', 'transaction_journals.id']) - ->get( - [ - 'transaction_journals.bill_id', - 'transaction_journals.id', - DB::raw('SUM(transactions.amount) AS journalAmount'), - ] - ); - - return $set; - } - /** * @return Collection */ diff --git a/app/Repositories/Bill/BillRepositoryInterface.php b/app/Repositories/Bill/BillRepositoryInterface.php index 26e5f22aef..8e7024240e 100644 --- a/app/Repositories/Bill/BillRepositoryInterface.php +++ b/app/Repositories/Bill/BillRepositoryInterface.php @@ -57,18 +57,6 @@ interface BillRepositoryInterface */ public function getActiveBills(): Collection; - /** - * Returns all journals connected to these bills in the given range. Amount paid - * is stored in "journalAmount" as a negative number. - * - * @param Collection $bills - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getAllJournalsInRange(Collection $bills, Carbon $start, Carbon $end): Collection; - /** * @return Collection */