Some new report data. Also, the report page now uses in excess of 3000 queries. Lol

This commit is contained in:
James Cole 2014-12-06 09:16:54 +01:00
parent 1997666196
commit f4ecf2d1aa
6 changed files with 301 additions and 22 deletions

View File

@ -34,8 +34,9 @@ class AccountController extends BaseController
$subTitleIcon = 'fa-download';
break;
}
$subTitle = 'Create a new ' . $what . ' account';
return View::make('accounts.create')->with('subTitle', 'Create a new ' . $what . ' account')->with('what', $what)->with(compact('subTitleIcon'));
return View::make('accounts.create', compact('subTitleIcon', 'what', 'subTitle'));
}
/**
@ -45,9 +46,9 @@ class AccountController extends BaseController
*/
public function delete(Account $account)
{
return View::make('accounts.delete')->with('account', $account)->with(
'subTitle', 'Delete ' . strtolower($account->accountType->type) . ' "' . $account->name . '"'
);
$subTitle = 'Delete ' . strtolower($account->accountType->type) . ' "' . $account->name . '"';
return View::make('accounts.delete', compact('account', 'subTitle'));
}
/**
@ -98,29 +99,31 @@ class AccountController extends BaseController
}
/*
* Delete it
* Delete the initial balance as well.
*/
if ($initialBalance) {
$acct->destroy($initialBalance);
}
$name = $account->name;
$acct->destroy($account);
Session::flash('success', 'The account was deleted.');
$return = 'asset';
switch ($type) {
case 'Asset account':
case 'Default account':
return Redirect::route('accounts.index', 'asset');
break;
case 'Expense account':
case 'Beneficiary account':
return Redirect::route('accounts.index', 'expense');
$return = 'expense';
break;
case 'Revenue account':
return Redirect::route('accounts.index', 'revenue');
$return = 'revenue';
break;
}
Session::flash('success', 'The ' . $return . ' account "' . e($name) . '" was deleted.');
return Redirect::route('accounts.index', $return);
}

View File

@ -221,23 +221,67 @@ class ReportController extends BaseController
*/
public function year($year)
{
Config::set('app.debug',false);
try {
$date = new Carbon('01-01-' . $year);
} catch (Exception $e) {
App::abort(500);
}
$date = new Carbon('01-01-' . $year);
/** @var \FireflyIII\Database\TransactionJournal $tj */
$tj = App::make('FireflyIII\Database\TransactionJournal');
/** @var \FireflyIII\Database\Account $accountRepository */
$accountRepository = App::make('FireflyIII\Database\Account');
/** @var \FireflyIII\Database\Report $reportRepository */
$reportRepository = App::make('FireflyIII\Database\Report');
$accounts = $accountRepository->getAssetAccounts();
// get some sums going
$summary = [];
/** @var \Account $account */
$accounts->each(
function (\Account $account) {
if ($account->getMeta('accountRole') == 'sharedExpense') {
$account->sharedExpense = true;
} else {
$account->sharedExpense = false;
}
}
);
$end = clone $date;
$end->endOfYear();
while ($date < $end) {
$summary[] = ['month' => $date->format('F'), 'income' => $tj->getSumOfIncomesByMonth($date), 'expense' => $tj->getSumOfExpensesByMonth($date),];
$month = $date->format('F');
$income = 0;
$incomeShared = 0;
$expense = 0;
$expenseShared = 0;
foreach ($accounts as $account) {
if ($account->sharedExpense === true) {
$incomeShared += $reportRepository->getIncomeByMonth($account, $date);
$expenseShared += $reportRepository->getExpenseByMonth($account, $date);
} else {
$income += $reportRepository->getIncomeByMonth($account, $date);
$expense += $reportRepository->getExpenseByMonth($account, $date);
}
}
$summary[] = [
'month' => $month,
'income' => $income,
'expense' => $expense,
'incomeShared' => $incomeShared,
'expenseShared' => $expenseShared,
];
$date->addMonth();
}

View File

@ -0,0 +1,27 @@
<?php
namespace FireflyIII\Database\Ifaces;
use Carbon\Carbon;
interface ReportInterface
{
/**
* @param \Account $account
* @param Carbon $month
*
* @return float
*/
public function getExpenseByMonth(\Account $account, Carbon $month);
/**
* @param \Account $account
* @param Carbon $month
*
* @return float
*/
public function getIncomeByMonth(\Account $account, Carbon $month);
}

View File

@ -0,0 +1,171 @@
<?php
/**
* Created by PhpStorm.
* User: sander
* Date: 06/12/14
* Time: 08:40
*/
namespace FireflyIII\Database;
use Carbon\Carbon;
use FireflyIII\Database\Ifaces\ReportInterface;
class Report implements ReportInterface
{
use SwitchUser;
/**
* @param \Account $account
* @param Carbon $month
*
* @return float
*/
public function getExpenseByMonth(\Account $account, Carbon $month)
{
if (isset($account->sharedExpense) && $account->sharedExpense === true) {
$shared = true;
} else {
if (isset($account->sharedExpense) && $account->sharedExpense === false) {
$shared = false;
} else {
$shared = ($account->getMeta('accountRole') == 'sharedExpense');
}
}
$start = clone $month;
$end = clone $month;
$start->startOfMonth();
$end->endOfMonth();
$sum = 0;
// get all journals.
$journals = \TransactionJournal::whereIn(
'id', function ($query) use ($account, $start, $end) {
$query->select('transaction_journal_id')
->from('transactions')
->where('account_id', $account->id);
}
)->before($end)->after($start)->get();
if ($shared) {
$expenses = $journals->filter(
function (\TransactionJournal $journal) use ($account) {
// any withdrawal is an expense:
if ($journal->transactionType->type == 'Withdrawal') {
return $journal;
}
// any transfer away from this account is an expense.
if ($journal->transactionType->type == 'Transfer') {
/** @var \Transaction $t */
foreach ($journal->transactions as $t) {
if ($t->account_id == $account->id && floatval($t->amount) < 0) {
return $journal;
}
}
}
}
);
} else {
$expenses = $journals->filter(
function (\TransactionJournal $journal) use ($account) {
// only withdrawals are expenses:
if ($journal->transactionType->type == 'Withdrawal') {
return $journal;
}
// transfers TO a shared account are also expenses.
if ($journal->transactionType->type == 'Transfer') {
/** @var \Transaction $t */
foreach ($journal->transactions() as $t) {
if ($t->account->getMeta('accountRole') == 'sharedExpense') {
echo '#'.$journal->id.' is a shared expense!<br>';
return $journal;
}
}
}
}
);
}
/** @var \TransactionJournal $expense */
foreach ($expenses as $expense) {
$sum += $expense->getAmount();
}
return $sum;
}
/**
* @param \Account $account
* @param Carbon $month
*
* @return float
*/
public function getIncomeByMonth(\Account $account, Carbon $month)
{
if (isset($account->sharedExpense) && $account->sharedExpense === true) {
$shared = true;
} else {
if (isset($account->sharedExpense) && $account->sharedExpense === false) {
$shared = false;
} else {
$shared = ($account->getMeta('accountRole') == 'sharedExpense');
}
}
$start = clone $month;
$end = clone $month;
$start->startOfMonth();
$end->endOfMonth();
$sum = 0;
// get all journals.
$journals = \TransactionJournal::whereIn(
'id', function ($query) use ($account, $start, $end) {
$query->select('transaction_journal_id')
->from('transactions')
->where('account_id', $account->id);
}
)->before($end)->after($start)->get();
if ($shared) {
$incomes = $journals->filter(
function (\TransactionJournal $journal) use ($account) {
// any deposit is an income:
if ($journal->transactionType->type == 'Deposit') {
return $journal;
}
// any transfer TO this account is an income.
if ($journal->transactionType->type == 'Transfer') {
/** @var \Transaction $t */
foreach ($journal->transactions as $t) {
if ($t->account_id == $account->id && floatval($t->amount) > 0) {
return $journal;
}
}
}
}
);
} else {
$incomes = $journals->filter(
function (\TransactionJournal $journal) use ($account) {
// only deposits are incomes:
if ($journal->transactionType->type == 'Deposit') {
return $journal;
}
}
);
}
/** @var \TransactionJournal $expense */
foreach ($incomes as $income) {
$sum += $income->getAmount();
}
return $sum;
}
}

View File

@ -110,14 +110,6 @@ class Account extends Ardent
$query->with(['accountmeta']);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
public function transactionjournals()
{
return $this->hasManyThrough('TransactionJournal', 'Transaction', 'transaction_journal_id', 'id');
}
/**
* Transactions.
*

View File

@ -28,7 +28,7 @@
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">
Summary
Summary (without shared accounts)
</div>
<table class="table table-striped">
<tr>
@ -66,6 +66,48 @@
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">
Summary (shared accounts only)
</div>
<table class="table table-striped">
<tr>
<td></td>
@foreach($summary as $entry)
<th>{{$entry['month']}}</th>
@endforeach
<th>Sum</th>
</tr>
<tr>
<th>In</th>
<?php $inSum = 0;?>
@foreach($summary as $entry)
<td>{{mf($entry['incomeShared'])}}</td>
<?php $inSum+=$entry['incomeShared'];?>
@endforeach
<td>{{mf($inSum)}}</td>
</tr>
<th>Out</th>
<?php $outSum = 0;?>
@foreach($summary as $entry)
<td>{{mf($entry['expenseShared']*-1)}}</td>
<?php $outSum+=$entry['expenseShared']*-1;?>
@endforeach
<td>{{mf($outSum)}}</td>
<tr>
<th>Difference</th>
@foreach($summary as $entry)
<td>{{mf($entry['incomeShared']- $entry['expenseShared'])}}</td>
@endforeach
<td>{{mf($inSum + $outSum)}}</td>
</tr>
</table>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="panel panel-default">