First attempt at including expense report.

This commit is contained in:
James Cole 2015-12-11 16:36:40 +01:00
parent 67fe35d564
commit ad01891a67
5 changed files with 118 additions and 10 deletions

View File

@ -473,4 +473,25 @@ class ReportHelper implements ReportHelperInterface
return $object;
}
/**
* Get a full report on the users expenses during the period for a list of accounts.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return Expense
*/
public function getExpenseReportForList($start, $end, Collection $accounts)
{
$object = new Expense;
$set = $this->query->expenseInPeriodCorrectedForList($start, $end, $accounts);
foreach ($set as $entry) {
$object->addToTotal($entry->amount_positive);
$object->addOrCreateExpense($entry);
}
return $object;
}
}

View File

@ -93,6 +93,17 @@ interface ReportHelperInterface
*/
public function getExpenseReport($start, $end, $shared);
/**
* Get a full report on the users expenses during the period for a list of accounts.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return Expense
*/
public function getExpenseReportForList($start, $end, Collection $accounts);
/**
* Get a full report on the users incomes during the period.
*

View File

@ -12,7 +12,6 @@ use FireflyIII\Models\TransactionType;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
use Steam;
/**
* Class ReportQuery
@ -336,4 +335,65 @@ class ReportQuery implements ReportQueryInterface
return $data;
}
/**
* See ReportQueryInterface::incomeInPeriodCorrected
*
* 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
*
*/
public function expenseInPeriodCorrectedForList(Carbon $start, Carbon $end, Collection $accounts)
{
$ids = [];
/** @var Account $account */
foreach ($accounts as $account) {
$ids[] = $account->id;
}
$query = $this->queryJournalsWithTransactions($start, $end);
$query->where(
function (Builder $query) {
$query->where(
function (Builder $q) { // only get withdrawals not from a shared account
$q->where('transaction_types.type', TransactionType::WITHDRAWAL);
$q->where('acm_from.data', '!=', '"sharedAsset"');
}
);
$query->orWhere(
function (Builder $q) { // and transfers from a shared account.
$q->where('transaction_types.type', TransactionType::TRANSFER);
$q->where('acm_to.data', '=', '"sharedAsset"');
$q->where('acm_from.data', '!=', '"sharedAsset"');
}
);
}
);
// 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;
}
}

View File

@ -31,6 +31,22 @@ interface ReportQueryInterface
*/
public function expenseInPeriodCorrected(Carbon $start, Carbon $end, $includeShared = false);
/**
* See ReportQueryInterface::incomeInPeriodCorrected
*
* 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
*
*/
public function expenseInPeriodCorrectedForList(Carbon $start, Carbon $end, Collection $accounts);
/**
* Get a users accounts combined with various meta-data related to the start and end date.
*

View File

@ -243,7 +243,7 @@ class ReportController extends Controller
// get report stuff!
$accounts = $this->helper->getAccountReportForList($start, $end, $list);
$incomes = $this->helper->getIncomeReportForList($start, $end, $list);
// $expenses = $this->helper->getExpenseReportForList($start, $end, $list);
$expenses = $this->helper->getExpenseReportForList($start, $end, $list);
// $budgets = $this->helper->getBudgetReportForList($start, $end, $list);
// $categories = $this->helper->getCategoryReportForList($start, $end, $list);
// $balance = $this->helper->getBalanceReportForList($start, $end, $list);