mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
More queries filtered.
This commit is contained in:
parent
64dbb14241
commit
27cabb398e
@ -451,6 +451,7 @@ class ReportHelper implements ReportHelperInterface
|
|||||||
/** @var \FireflyIII\Repositories\Bill\BillRepositoryInterface $repository */
|
/** @var \FireflyIII\Repositories\Bill\BillRepositoryInterface $repository */
|
||||||
$repository = app('FireflyIII\Repositories\Bill\BillRepositoryInterface');
|
$repository = app('FireflyIII\Repositories\Bill\BillRepositoryInterface');
|
||||||
$bills = $repository->getBillsForAccounts($accounts);
|
$bills = $repository->getBillsForAccounts($accounts);
|
||||||
|
$journals = $repository->getAllJournalsInRange($bills, $start, $end);
|
||||||
$collection = new BillCollection;
|
$collection = new BillCollection;
|
||||||
|
|
||||||
/** @var Bill $bill */
|
/** @var Bill $bill */
|
||||||
@ -463,16 +464,17 @@ class ReportHelper implements ReportHelperInterface
|
|||||||
|
|
||||||
// is hit in period?
|
// is hit in period?
|
||||||
bcscale(2);
|
bcscale(2);
|
||||||
$set = $repository->getJournalsInRange($bill, $start, $end);
|
|
||||||
if ($set->count() == 0) {
|
$entry = $journals->filter(
|
||||||
$billLine->setHit(false);
|
function (TransactionJournal $journal) use ($bill) {
|
||||||
} else {
|
return $journal->bill_id == $bill->id;
|
||||||
$billLine->setHit(true);
|
|
||||||
$amount = '0';
|
|
||||||
foreach ($set as $entry) {
|
|
||||||
$amount = bcadd($amount, $entry->amount);
|
|
||||||
}
|
}
|
||||||
$billLine->setAmount($amount);
|
);
|
||||||
|
if (!is_null($entry->first())) {
|
||||||
|
$billLine->setAmount($entry->first()->journalAmount);
|
||||||
|
$billLine->setHit(true);
|
||||||
|
} else {
|
||||||
|
$billLine->setHit(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
$collection->addBill($billLine);
|
$collection->addBill($billLine);
|
||||||
|
@ -35,6 +35,40 @@ class BillRepository implements BillRepositoryInterface
|
|||||||
return $bill->delete();
|
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
|
* @return Collection
|
||||||
@ -63,20 +97,22 @@ class BillRepository implements BillRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getBillsForAccounts(Collection $accounts)
|
public function getBillsForAccounts(Collection $accounts)
|
||||||
{
|
{
|
||||||
/** @var Collection $set */
|
|
||||||
$set = Auth::user()->bills()->orderBy('name', 'ASC')->get();
|
|
||||||
$ids = $accounts->pluck('id')->toArray();
|
$ids = $accounts->pluck('id')->toArray();
|
||||||
$set = $set->filter(
|
$set = Auth::user()->bills()
|
||||||
function (Bill $bill) use ($ids) {
|
->leftJoin(
|
||||||
// get transaction journals from or to any of the mentioned accounts.
|
'transaction_journals', function (JoinClause $join) {
|
||||||
// when zero, return null.
|
$join->on('transaction_journals.bill_id', '=', 'bills.id')->whereNull('transaction_journals.deleted_at');
|
||||||
$journals = $bill->transactionjournals()->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
}
|
||||||
->whereIn('transactions.account_id', $ids)->count();
|
)
|
||||||
|
->leftJoin(
|
||||||
return ($journals > 0);
|
'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(
|
$set = $set->sortBy(
|
||||||
function (Bill $bill) {
|
function (Bill $bill) {
|
||||||
|
@ -59,6 +59,19 @@ interface BillRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function destroy(Bill $bill);
|
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
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user