firefly-iii/app/Helpers/Report/ReportQuery.php

237 lines
8.7 KiB
PHP
Raw Normal View History

2015-02-23 13:25:48 -06:00
<?php
namespace FireflyIII\Helpers\Report;
use Auth;
use Carbon\Carbon;
2015-03-31 15:46:11 -05:00
use Crypt;
2015-02-23 14:19:16 -06:00
use FireflyIII\Models\Account;
2015-05-16 08:43:58 -05:00
use FireflyIII\Models\Budget;
2015-02-23 13:25:48 -06:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
2015-02-23 13:25:48 -06:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
/**
* Class ReportQuery
*
* @package FireflyIII\Helpers\Report
*/
class ReportQuery implements ReportQueryInterface
{
/**
* Covers tags
*
* @param Account $account
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return float
*/
2015-12-13 02:41:22 -06:00
public function spentInBudget(Account $account, Budget $budget, Carbon $start, Carbon $end)
{
return Auth::user()->transactionjournals()
2015-12-12 13:10:52 -06:00
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->transactionTypes([TransactionType::WITHDRAWAL])
->where('transactions.account_id', $account->id)
->before($end)
->after($start)
->where('budget_transaction_journal.budget_id', $budget->id)
->get(['transaction_journals.*'])->sum('amount');
}
2015-05-16 09:47:52 -05:00
/**
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
2015-06-13 03:02:36 -05:00
* @return string
2015-05-16 09:47:52 -05:00
*/
2015-07-06 15:12:35 -05:00
public function spentNoBudget(Account $account, Carbon $start, Carbon $end)
2015-05-16 09:47:52 -05:00
{
2015-06-13 03:02:36 -05:00
return
2015-05-16 09:47:52 -05:00
Auth::user()->transactionjournals()
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->transactionTypes([TransactionType::WITHDRAWAL])
2015-05-16 09:47:52 -05:00
->where('transactions.account_id', $account->id)
->before($end)
->after($start)
2015-06-13 03:02:36 -05:00
->whereNull('budget_transaction_journal.budget_id')->get(['transaction_journals.*'])->sum('amount');
2015-03-29 04:51:26 -05:00
}
2015-03-29 05:12:06 -05:00
/**
* @param Carbon $start
* @param Carbon $end
*
* @return Builder
*/
protected function queryJournalsWithTransactions(Carbon $start, Carbon $end)
{
$query = TransactionJournal::
leftJoin(
2015-06-03 14:25:11 -05:00
'transactions as t_from', function (JoinClause $join) {
2015-03-29 05:12:06 -05:00
$join->on('t_from.transaction_journal_id', '=', 'transaction_journals.id')->where('t_from.amount', '<', 0);
}
)
2015-06-06 16:09:12 -05:00
->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');
2015-03-29 05:12:06 -05:00
$query->before($end)->after($start)->where('transaction_journals.user_id', Auth::user()->id);
return $query;
}
/**
* This method works the same way as ReportQueryInterface::incomeInPeriod does, but instead of returning results
* will simply list the transaction journals only. This should allow any follow up counting to be accurate with
* regards to tags. It will only get the incomes to the specified accounts.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return Collection
*/
2015-12-13 02:41:22 -06:00
public function incomeInPeriod(Carbon $start, Carbon $end, Collection $accounts)
{
$query = $this->queryJournalsWithTransactions($start, $end);
$ids = [];
/** @var Account $account */
foreach ($accounts as $account) {
$ids[] = $account->id;
}
// OR is a deposit
2015-12-13 13:39:26 -06:00
// OR any transfer TO the accounts in $accounts, not FROM any of the accounts in $accounts.
$query->where(
2015-12-13 13:39:26 -06:00
function (Builder $query) use ($ids) {
$query->where(
function (Builder $q) {
$q->where('transaction_types.type', TransactionType::DEPOSIT);
}
);
$query->orWhere(
2015-12-13 13:39:26 -06:00
function (Builder $q) use ($ids) {
2015-12-12 05:36:36 -06:00
$q->where('transaction_types.type', TransactionType::TRANSFER);
2015-12-13 13:41:35 -06:00
$q->whereNotIn('ac_from.id',$ids);
$q->whereIn('ac_to.id', $ids);
2015-12-12 05:36:36 -06:00
}
);
}
);
// only include selected accounts.
2015-12-11 04:32:22 -06:00
$query->whereIn('ac_to.id', $ids);
$query->orderBy('transaction_journals.date');
// get everything
$data = $query->get(
['transaction_journals.*', 'transaction_types.type', 'ac_from.name as name', 'ac_from.id as account_id', 'ac_from.encrypted as account_encrypted']
);
$data->each(
function (TransactionJournal $journal) {
if (intval($journal->account_encrypted) == 1) {
$journal->name = Crypt::decrypt($journal->name);
}
}
);
$data = $data->filter(
function (TransactionJournal $journal) {
if ($journal->amount != 0) {
return $journal;
}
return null;
}
);
return $data;
}
/**
2015-12-13 03:05:13 -06:00
* See ReportQueryInterface::incomeInPeriod
*
* This method returns all "expense" journals in a certain period, which are both transfers to a shared account
* and "ordinary" withdrawals. The query used is almost equal to ReportQueryInterface::journalsByRevenueAccount but it does
* not group and returns different fields.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return Collection
*
*/
2015-12-13 02:41:22 -06:00
public function expenseInPeriod(Carbon $start, Carbon $end, Collection $accounts)
{
$ids = [];
/** @var Account $account */
foreach ($accounts as $account) {
$ids[] = $account->id;
}
$query = $this->queryJournalsWithTransactions($start, $end);
2015-12-11 09:39:33 -06:00
2015-12-12 13:10:52 -06:00
// withdrawals from any account are an expense.
2015-12-13 13:39:26 -06:00
// transfers away, from an account in the list, to an account not in the list, are an expense.
2015-12-11 09:39:33 -06:00
$query->where(
2015-12-12 13:10:52 -06:00
function (Builder $query) use ($ids) {
$query->where(
2015-12-12 13:10:52 -06:00
function (Builder $q) {
$q->where('transaction_types.type', TransactionType::WITHDRAWAL);
}
);
$query->orWhere(
2015-12-12 13:10:52 -06:00
function (Builder $q) use ($ids) {
$q->where('transaction_types.type', TransactionType::TRANSFER);
2015-12-13 13:41:35 -06:00
$q->whereIn('ac_from.id', $ids);
$q->whereNotIn('ac_to.id', $ids);
2015-12-12 13:07:33 -06:00
}
);
}
);
// expense goes from the selected accounts:
$query->whereIn('ac_from.id', $ids);
$query->orderBy('transaction_journals.date');
$data = $query->get( // get everything
['transaction_journals.*', 'transaction_types.type', 'ac_to.name as name', 'ac_to.id as account_id', 'ac_to.encrypted as account_encrypted']
);
$data->each(
function (TransactionJournal $journal) {
if (intval($journal->account_encrypted) == 1) {
$journal->name = Crypt::decrypt($journal->name);
}
}
);
return $data;
}
2015-03-29 01:14:32 -05:00
}