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

280 lines
11 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 Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
2015-02-23 14:19:16 -06:00
use Steam;
2015-02-23 13:25:48 -06:00
/**
* Class ReportQuery
*
* @package FireflyIII\Helpers\Report
*/
class ReportQuery implements ReportQueryInterface
{
/**
2015-07-09 02:41:54 -05:00
* See ReportQueryInterface::incomeInPeriodCorrected.
*
* This method's length is caused mainly by the query build stuff. Therefor:
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*
* @param Carbon $start
* @param Carbon $end
* @param bool $includeShared
*
* @return Collection
*
*/
public function expenseInPeriodCorrected(Carbon $start, Carbon $end, $includeShared = false)
{
$query = $this->queryJournalsWithTransactions($start, $end);
if ($includeShared === false) {
$query->where(
2015-06-06 16:09:12 -05:00
function (Builder $query) {
$query->where(
2015-06-06 16:09:12 -05:00
function (Builder $q) { // only get withdrawals not from a shared account
$q->where('transaction_types.type', 'Withdrawal');
$q->where('acm_from.data', '!=', '"sharedAsset"');
}
);
$query->orWhere(
2015-06-06 16:09:12 -05:00
function (Builder $q) { // and transfers from a shared account.
$q->where('transaction_types.type', 'Transfer');
$q->where('acm_to.data', '=', '"sharedAsset"');
$q->where('acm_from.data', '!=', '"sharedAsset"');
}
);
}
);
} else {
$query->where('transaction_types.type', 'Withdrawal'); // any withdrawal is fine.
}
$query->orderBy('transaction_journals.date');
2015-07-07 03:05:11 -05:00
$data = $query->get( // get everything
2015-05-20 12:55:53 -05:00
['transaction_journals.*', 'transaction_types.type', 'ac_to.name as name', 'ac_to.id as account_id', 'ac_to.encrypted as account_encrypted']
);
2015-05-20 00:20:02 -05:00
$data->each(
2015-06-06 16:09:12 -05:00
function (TransactionJournal $journal) {
2015-05-20 00:20:02 -05:00
if (intval($journal->account_encrypted) == 1) {
$journal->name = Crypt::decrypt($journal->name);
}
}
);
return $data;
}
2015-02-23 14:19:16 -06:00
/**
* Get a users accounts combined with various meta-data related to the start and end date.
*
* @param Carbon $start
* @param Carbon $end
* @param bool $includeShared
2015-02-23 14:19:16 -06:00
*
* @return Collection
*/
public function getAllAccounts(Carbon $start, Carbon $end, $includeShared = false)
2015-02-23 14:19:16 -06:00
{
$query = Auth::user()->accounts()->orderBy('accounts.name', 'ASC')
2015-06-06 16:09:12 -05:00
->accountTypeIn(['Default account', 'Asset account', 'Cash account']);
if ($includeShared === false) {
$query->leftJoin(
2015-06-03 14:25:11 -05:00
'account_meta', function (JoinClause $join) {
$join->on('account_meta.account_id', '=', 'accounts.id')->where('account_meta.name', '=', 'accountRole');
}
)
2015-06-06 16:09:12 -05:00
->where(
function (Builder $query) {
2015-06-06 16:09:12 -05:00
$query->where('account_meta.data', '!=', '"sharedAsset"');
$query->orWhereNull('account_meta.data');
2015-06-06 16:09:12 -05:00
}
);
}
$set = $query->get(['accounts.*']);
2015-02-23 14:19:16 -06:00
$set->each(
2015-06-06 16:09:12 -05:00
function (Account $account) use ($start, $end) {
2015-05-03 03:07:18 -05:00
/**
* The balance for today always incorporates transactions
* made on today. So to get todays "start" balance, we sub one
* day.
*/
$yesterday = clone $start;
$yesterday->subDay();
2015-02-23 14:19:16 -06:00
/** @noinspection PhpParamsInspection */
2015-05-03 03:07:18 -05:00
$account->startBalance = Steam::balance($account, $yesterday);
2015-02-23 14:19:16 -06:00
$account->endBalance = Steam::balance($account, $end);
}
);
return $set;
}
2015-02-23 13:25:48 -06:00
/**
* 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.
2015-05-22 08:05:32 -05:00
*
* This method returns all "income" journals in a certain period, which are both transfers from a shared account
* and "ordinary" deposits. 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 bool $includeShared
*
* @return Collection
*/
public function incomeInPeriodCorrected(Carbon $start, Carbon $end, $includeShared = false)
{
$query = $this->queryJournalsWithTransactions($start, $end);
if ($includeShared === false) {
// only get deposits not to a shared account
// and transfers to a shared account.
$query->where(
2015-06-06 16:09:12 -05:00
function (Builder $query) {
$query->where(
2015-06-06 16:09:12 -05:00
function (Builder $q) {
$q->where('transaction_types.type', 'Deposit');
$q->where('acm_to.data', '!=', '"sharedAsset"');
}
);
$query->orWhere(
2015-06-06 16:09:12 -05:00
function (Builder $q) {
$q->where('transaction_types.type', 'Transfer');
$q->where('acm_from.data', '=', '"sharedAsset"');
$q->where('acm_to.data','!=','"sharedAsset"');
}
);
}
);
} else {
// any deposit is fine.
$query->where('transaction_types.type', 'Deposit');
}
$query->orderBy('transaction_journals.date');
// get everything
2015-05-20 12:55:53 -05:00
$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']
);
2015-05-20 00:20:02 -05:00
$data->each(
2015-06-06 16:09:12 -05:00
function (TransactionJournal $journal) {
2015-05-20 00:20:02 -05:00
if (intval($journal->account_encrypted) == 1) {
$journal->name = Crypt::decrypt($journal->name);
}
}
);
$data = $data->filter(
2015-06-06 16:09:12 -05:00
function (TransactionJournal $journal) {
2015-05-20 00:20:02 -05:00
if ($journal->amount != 0) {
return $journal;
}
2015-06-03 14:25:11 -05:00
2015-05-26 01:17:58 -05:00
return null;
}
2015-05-20 00:20:02 -05:00
);
return $data;
}
2015-02-23 13:25:48 -06:00
/**
* Covers tags
*
* @param Account $account
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return float
*/
public function spentInBudgetCorrected(Account $account, Budget $budget, Carbon $start, Carbon $end)
{
2015-07-26 12:07:02 -05:00
bcscale(2);
return bcmul(
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(['Withdrawal'])
->where('transactions.account_id', $account->id)
->before($end)
->after($start)
->where('budget_transaction_journal.budget_id', $budget->id)
->get(['transaction_journals.*'])->sum('amount'), -1
);
}
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(['Withdrawal'])
->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;
}
2015-03-29 01:14:32 -05:00
}