Fix query.

This commit is contained in:
James Cole
2015-12-12 20:10:52 +01:00
parent 9bd1503cb4
commit a985e09282

View File

@@ -191,14 +191,14 @@ class ReportQuery implements ReportQueryInterface
{ {
return Auth::user()->transactionjournals() return Auth::user()->transactionjournals()
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->transactionTypes([TransactionType::WITHDRAWAL]) ->transactionTypes([TransactionType::WITHDRAWAL])
->where('transactions.account_id', $account->id) ->where('transactions.account_id', $account->id)
->before($end) ->before($end)
->after($start) ->after($start)
->where('budget_transaction_journal.budget_id', $budget->id) ->where('budget_transaction_journal.budget_id', $budget->id)
->get(['transaction_journals.*'])->sum('amount'); ->get(['transaction_journals.*'])->sum('amount');
} }
/** /**
@@ -288,7 +288,6 @@ class ReportQuery implements ReportQueryInterface
// OR is a transfer from NOT a shared account to a shared account. // OR is a transfer from NOT a shared account to a shared account.
$query->where( $query->where(
function (Builder $query) { function (Builder $query) {
$query->where( $query->where(
@@ -367,28 +366,20 @@ class ReportQuery implements ReportQueryInterface
$query = $this->queryJournalsWithTransactions($start, $end); $query = $this->queryJournalsWithTransactions($start, $end);
// withdrawals from any account are an expense.
// transfers away, to an account not in the list, are an expense.
$query->where( $query->where(
function (Builder $query) { function (Builder $query) use ($ids) {
$query->where( $query->where(
function (Builder $q) { // only get withdrawals from any account: function (Builder $q) {
$q->where('transaction_types.type', TransactionType::WITHDRAWAL); $q->where('transaction_types.type', TransactionType::WITHDRAWAL);
//$q->where('acm_from.data', '!=', '"sharedAsset"');
} }
); );
$query->orWhere( $query->orWhere(
function (Builder $q) { // and transfers from a shared account to a not-shared account. function (Builder $q) use ($ids) {
$q->where('transaction_types.type', TransactionType::TRANSFER); $q->where('transaction_types.type', TransactionType::TRANSFER);
$q->where('acm_to.data', '=', '"sharedAsset"'); $q->whereNotIn('ac_to.id', $ids);
$q->where('acm_from.data', '!=', '"sharedAsset"');
}
);
// OR transfers from a NOT shared account to a shared account.
$query->orWhere(
function (Builder $q) { // and transfers from a shared account to a not-shared account.
$q->where('transaction_types.type', TransactionType::TRANSFER);
$q->where('acm_to.data', '!=', '"sharedAsset"');
$q->where('acm_from.data', '=', '"sharedAsset"');
} }
); );
} }