Skip to content

Firefly III: a personal finances manager

More queries filtered.

James Cole27cabb398e66
diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php
index c677a1bfac..b96ef459af 100644
--- a/app/Helpers/Report/ReportHelper.php
+++ b/app/Helpers/Report/ReportHelper.php
@@ -451,6 +451,7 @@ class ReportHelper implements ReportHelperInterface
/** @var \FireflyIII\Repositories\Bill\BillRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Bill\BillRepositoryInterface');
$bills = $repository->getBillsForAccounts($accounts);
+ $journals = $repository->getAllJournalsInRange($bills, $start, $end);
$collection = new BillCollection;
/** @var Bill $bill */
@@ -463,16 +464,17 @@ class ReportHelper implements ReportHelperInterface
// is hit in period?
bcscale(2);
- $set = $repository->getJournalsInRange($bill, $start, $end);
- if ($set->count() == 0) {
- $billLine->setHit(false);
- } else {
- $billLine->setHit(true);
- $amount = '0';
- foreach ($set as $entry) {
- $amount = bcadd($amount, $entry->amount);
+
+ $entry = $journals->filter(
+ function (TransactionJournal $journal) use ($bill) {
+ return $journal->bill_id == $bill->id;
}
- $billLine->setAmount($amount);
+ );
+ if (!is_null($entry->first())) {
+ $billLine->setAmount($entry->first()->journalAmount);
+ $billLine->setHit(true);
+ } else {
+ $billLine->setHit(false);
}
$collection->addBill($billLine);
diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php
index 7a5c11c985..54bba39d9f 100644
--- a/app/Repositories/Bill/BillRepository.php
+++ b/app/Repositories/Bill/BillRepository.php
@@ -35,6 +35,40 @@ class BillRepository implements BillRepositoryInterface
return $bill->delete();
}
+ /**
+ * 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)
+ {
+ $ids = $bills->pluck('id')->toArray();
+
+ $set = Auth::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')
+ ->get(
+ [
+ 'transaction_journals.bill_id',
+ DB::Raw('SUM(`transactions`.`amount`) as `journalAmount`')
+ ]
+ );
+
+ return $set;
+ }
+
/**
* @return Collection
@@ -63,20 +97,22 @@ class BillRepository implements BillRepositoryInterface
*/
public function getBillsForAccounts(Collection $accounts)
{
- /** @var Collection $set */
- $set = Auth::user()->bills()->orderBy('name', 'ASC')->get();
$ids = $accounts->pluck('id')->toArray();
- $set = $set->filter(
- function (Bill $bill) use ($ids) {
- // get transaction journals from or to any of the mentioned accounts.
- // when zero, return null.
- $journals = $bill->transactionjournals()->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
- ->whereIn('transactions.account_id', $ids)->count();
-
- return ($journals > 0);
-
- }
- );
+ $set = Auth::user()->bills()
+ ->leftJoin(
+ 'transaction_journals', function (JoinClause $join) {
+ $join->on('transaction_journals.bill_id', '=', 'bills.id')->whereNull('transaction_journals.deleted_at');
+ }
+ )
+ ->leftJoin(
+ 'transactions', function (JoinClause $join) {
+ $join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
+ }
+ )
+ ->whereIn('transactions.account_id', $ids)
+ ->whereNull('transaction_journals.deleted_at')
+ ->groupBy('bills.id')
+ ->get(['bills.*']);
$set = $set->sortBy(
function (Bill $bill) {
diff --git a/app/Repositories/Bill/BillRepositoryInterface.php b/app/Repositories/Bill/BillRepositoryInterface.php
index d171aedcdb..0cf52754bf 100644
--- a/app/Repositories/Bill/BillRepositoryInterface.php
+++ b/app/Repositories/Bill/BillRepositoryInterface.php
@@ -59,6 +59,19 @@ interface BillRepositoryInterface
*/
public function destroy(Bill $bill);
+ /**
+ * 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);
+
+
/**
* @return Collection
*/