Removed some double code.

This commit is contained in:
James Cole 2015-03-29 12:12:06 +02:00
parent bcf066ead7
commit 70a01c082b

View File

@ -210,30 +210,7 @@ class ReportQuery implements ReportQueryInterface
*/ */
public function incomeByPeriod(Carbon $start, Carbon $end, $showSharedReports = false) public function incomeByPeriod(Carbon $start, Carbon $end, $showSharedReports = false)
{ {
$query = TransactionJournal:: $query = $this->queryJournalsWithTransactions($start, $end);
leftJoin(
'transactions as t_from', function (JoinClause $join) {
$join->on('t_from.transaction_journal_id', '=', 'transaction_journals.id')->where('t_from.amount', '<', 0);
}
)
->leftJoin('accounts as ac_from', 't_from.account_id', '=', 'ac_from.id')
->leftJoin(
'account_meta as acm_from', function (JoinClause $join) {
$join->on('ac_from.id', '=', 'acm_from.account_id')->where('acm_from.name', '=', 'accountRole');
}
)
->leftJoin(
'transactions as t_to', function (JoinClause $join) {
$join->on('t_to.transaction_journal_id', '=', 'transaction_journals.id')->where('t_to.amount', '>', 0);
}
)
->leftJoin('accounts as ac_to', 't_to.account_id', '=', 'ac_to.id')
->leftJoin(
'account_meta as acm_to', function (JoinClause $join) {
$join->on('ac_to.id', '=', 'acm_to.account_id')->where('acm_to.name', '=', 'accountRole');
}
)
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');
if ($showSharedReports === false) { if ($showSharedReports === false) {
// only get deposits not to a shared account // only get deposits not to a shared account
// and transfers to a shared account. // and transfers to a shared account.
@ -257,9 +234,7 @@ class ReportQuery implements ReportQueryInterface
// any deposit is fine. // any deposit is fine.
$query->where('transaction_types.type', 'Deposit'); $query->where('transaction_types.type', 'Deposit');
} }
$query->before($end)->after($start) $query->groupBy('t_from.account_id')->orderBy('transaction_journals.date');
->where('transaction_journals.user_id', Auth::user()->id)
->groupBy('t_from.account_id')->orderBy('transaction_journals.date');
return $query->get( return $query->get(
['transaction_journals.id', ['transaction_journals.id',
@ -366,6 +341,7 @@ class ReportQuery implements ReportQueryInterface
*/ */
public function journalsByExpenseAccount(Carbon $start, Carbon $end, $showSharedReports = false) public function journalsByExpenseAccount(Carbon $start, Carbon $end, $showSharedReports = false)
{ {
$query = $this->queryJournalsWithTransactions($start, $end);
$query = TransactionJournal::leftJoin( $query = TransactionJournal::leftJoin(
'transactions as t_from', function (JoinClause $join) { 'transactions as t_from', function (JoinClause $join) {
$join->on('t_from.transaction_journal_id', '=', 'transaction_journals.id')->where('t_from.amount', '<', 0); $join->on('t_from.transaction_journal_id', '=', 'transaction_journals.id')->where('t_from.amount', '<', 0);
@ -412,8 +388,7 @@ class ReportQuery implements ReportQueryInterface
// any withdrawal goes: // any withdrawal goes:
$query->where('transaction_types.type', 'Withdrawal'); $query->where('transaction_types.type', 'Withdrawal');
} }
$query->before($end) $query->before($end)->after($start)
->after($start)
->where('transaction_journals.user_id', Auth::user()->id) ->where('transaction_journals.user_id', Auth::user()->id)
->groupBy('t_to.account_id') ->groupBy('t_to.account_id')
->orderBy('amount', 'DESC'); ->orderBy('amount', 'DESC');
@ -432,30 +407,7 @@ class ReportQuery implements ReportQueryInterface
*/ */
public function journalsByRevenueAccount(Carbon $start, Carbon $end, $showSharedReports = false) public function journalsByRevenueAccount(Carbon $start, Carbon $end, $showSharedReports = false)
{ {
$query = TransactionJournal:: $query = $this->queryJournalsWithTransactions($start, $end);
leftJoin(
'transactions as t_from', function (JoinClause $join) {
$join->on('t_from.transaction_journal_id', '=', 'transaction_journals.id')->where('t_from.amount', '<', 0);
}
)
->leftJoin('accounts as ac_from', 't_from.account_id', '=', 'ac_from.id')
->leftJoin(
'account_meta as acm_from', function (JoinClause $join) {
$join->on('ac_from.id', '=', 'acm_from.account_id')->where('acm_from.name', '=', 'accountRole');
}
)
->leftJoin(
'transactions as t_to', function (JoinClause $join) {
$join->on('t_to.transaction_journal_id', '=', 'transaction_journals.id')->where('t_to.amount', '>', 0);
}
)
->leftJoin('accounts as ac_to', 't_to.account_id', '=', 'ac_to.id')
->leftJoin(
'account_meta as acm_to', function (JoinClause $join) {
$join->on('ac_to.id', '=', 'acm_to.account_id')->where('acm_to.name', '=', 'accountRole');
}
)
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');
if ($showSharedReports === false) { if ($showSharedReports === false) {
// show queries where transfer type is deposit, and its not to a shared account // show queries where transfer type is deposit, and its not to a shared account
@ -480,9 +432,8 @@ class ReportQuery implements ReportQueryInterface
// any deposit goes: // any deposit goes:
$query->where('transaction_types.type', 'Deposit'); $query->where('transaction_types.type', 'Deposit');
} }
$query->before($end)->after($start)
->where('transaction_journals.user_id', Auth::user()->id) $query->groupBy('t_from.account_id')->orderBy('amount');
->groupBy('t_from.account_id')->orderBy('amount');
return $query->get(['t_from.account_id as account_id', 'ac_from.name as name', DB::Raw('SUM(t_from.amount) as `amount`')]); return $query->get(['t_from.account_id as account_id', 'ac_from.name as name', DB::Raw('SUM(t_from.amount) as `amount`')]);
} }
@ -582,7 +533,7 @@ class ReportQuery implements ReportQueryInterface
* *
* @return Builder * @return Builder
*/ */
public function queryJournalsNoBudget(Account $account, Carbon $start, Carbon $end) protected function queryJournalsNoBudget(Account $account, Carbon $start, Carbon $end)
{ {
return TransactionJournal:: return TransactionJournal::
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')
@ -603,4 +554,41 @@ class ReportQuery implements ReportQueryInterface
->orderBy('budgets.name', 'ASC'); ->orderBy('budgets.name', 'ASC');
} }
/**
* @param Carbon $start
* @param Carbon $end
*
* @return Builder
*/
protected function queryJournalsWithTransactions(Carbon $start, Carbon $end)
{
$query = TransactionJournal::
leftJoin(
'transactions as t_from', function (JoinClause $join) {
$join->on('t_from.transaction_journal_id', '=', 'transaction_journals.id')->where('t_from.amount', '<', 0);
}
)
->leftJoin('accounts as ac_from', 't_from.account_id', '=', 'ac_from.id')
->leftJoin(
'account_meta as acm_from', function (JoinClause $join) {
$join->on('ac_from.id', '=', 'acm_from.account_id')->where('acm_from.name', '=', 'accountRole');
}
)
->leftJoin(
'transactions as t_to', function (JoinClause $join) {
$join->on('t_to.transaction_journal_id', '=', 'transaction_journals.id')->where('t_to.amount', '>', 0);
}
)
->leftJoin('accounts as ac_to', 't_to.account_id', '=', 'ac_to.id')
->leftJoin(
'account_meta as acm_to', function (JoinClause $join) {
$join->on('ac_to.id', '=', 'acm_to.account_id')->where('acm_to.name', '=', 'accountRole');
}
)
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');
$query->before($end)->after($start)->where('transaction_journals.user_id', Auth::user()->id);
return $query;
}
} }