More queries filtered.

This commit is contained in:
James Cole 2016-01-01 21:07:15 +01:00
parent 64dbb14241
commit 27cabb398e
3 changed files with 73 additions and 22 deletions

View File

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

View File

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

View File

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