mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-09 06:55:43 -06:00
Fix query.
This commit is contained in:
parent
55b8f03590
commit
3c1ff4d21f
@ -683,8 +683,17 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
}
|
||||
)
|
||||
->whereIn('transactions.account_id', $ids)
|
||||
->having('transaction_count', '=', 1)
|
||||
->transactionTypes([TransactionType::WITHDRAWAL])
|
||||
->first([DB::raw('SUM(`transactions`.`amount`) as `journalAmount`')]);
|
||||
->first(
|
||||
[
|
||||
DB::raw('SUM(`transactions`.`amount`) as `journalAmount`'),
|
||||
DB::raw('COUNT(`transactions`.`id`) as `transaction_count`'),
|
||||
]
|
||||
);
|
||||
if (is_null($entry)) {
|
||||
return '0';
|
||||
}
|
||||
if (is_null($entry->journalAmount)) {
|
||||
return '0';
|
||||
}
|
||||
|
@ -26,19 +26,38 @@ class ComponentRepository
|
||||
*/
|
||||
protected function commonBalanceInPeriod($object, Carbon $start, Carbon $end, Collection $accounts)
|
||||
{
|
||||
$ids = $accounts->pluck('id')->toArray();
|
||||
|
||||
|
||||
// all balances based on transaction journals:
|
||||
// TODO somehow exclude those with transactions below?
|
||||
// TODO needs a completely new query.
|
||||
$ids = $accounts->pluck('id')->toArray();
|
||||
$entry = $object->transactionjournals()
|
||||
->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE])
|
||||
->before($end)
|
||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
||||
->whereIn('accounts.id', $ids)
|
||||
->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE])
|
||||
->before($end)
|
||||
->after($start)
|
||||
->first([DB::raw('SUM(`transactions`.`amount`) as `journalAmount`')]);
|
||||
$amount = $entry->journalAmount ?? '0';
|
||||
|
||||
return $amount;
|
||||
// all balances based on individual transactions (at the moment, it's an "or or"):
|
||||
$entry = $object
|
||||
->transactions()
|
||||
// left join journals to get some meta-information.
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
// also left join transaction types so we can do the same type of filtering.
|
||||
->leftJoin('transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id')
|
||||
// need to do these manually.
|
||||
->whereIn('transaction_types.type', [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE])
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d 00:00:00'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d 00:00:00'))
|
||||
->whereIn('transactions.account_id', $ids)
|
||||
->first([DB::raw('SUM(`transactions`.`amount`) as `journalAmount`')]);
|
||||
|
||||
// sum of amount:
|
||||
$extraAmount = $entry->journalAmount ?? '0';
|
||||
$result = bcadd($amount, $extraAmount);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user