More code for previous.

This commit is contained in:
James Cole
2015-03-10 19:57:20 +01:00
parent 17ae4b7d2a
commit 114788567d
5 changed files with 101 additions and 88 deletions

View File

@@ -7,7 +7,6 @@ use Carbon\Carbon;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Session;
/** /**
* Class ReportHelper * Class ReportHelper
@@ -119,14 +118,16 @@ class ReportHelper implements ReportHelperInterface
/** /**
* @param Carbon $date * @param Carbon $date
* @param bool $showSharedReports
* *
* @return array * @return array
*/ */
public function yearBalanceReport(Carbon $date) public function yearBalanceReport(Carbon $date, $showSharedReports = false)
{ {
$start = clone $date; $start = clone $date;
$end = clone $date; $end = clone $date;
$sharedAccounts = []; $sharedAccounts = [];
if ($showSharedReports === false) {
$sharedCollection = \Auth::user()->accounts() $sharedCollection = \Auth::user()->accounts()
->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id') ->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')
->where('account_meta.name', '=', 'accountRole') ->where('account_meta.name', '=', 'accountRole')
@@ -136,8 +137,10 @@ class ReportHelper implements ReportHelperInterface
foreach ($sharedCollection as $account) { foreach ($sharedCollection as $account) {
$sharedAccounts[] = $account->id; $sharedAccounts[] = $account->id;
} }
}
$accounts = \Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name','ASC')->get(['accounts.*'])->filter( $accounts = \Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name', 'ASC')->get(['accounts.*'])
->filter(
function (Account $account) use ($sharedAccounts) { function (Account $account) use ($sharedAccounts) {
if (!in_array($account->id, $sharedAccounts)) { if (!in_array($account->id, $sharedAccounts)) {
return $account; return $account;

View File

@@ -14,27 +14,6 @@ interface ReportHelperInterface
{ {
/**
* @param Carbon $date
*
* @return array
*/
public function listOfMonths(Carbon $date);
/**
* @param Carbon $date
*
* @return array
*/
public function listOfYears(Carbon $date);
/**
* @param Carbon $date
*
* @return array
*/
public function yearBalanceReport(Carbon $date);
/** /**
* This methods fails to take in account transfers FROM shared accounts. * This methods fails to take in account transfers FROM shared accounts.
* *
@@ -54,4 +33,26 @@ interface ReportHelperInterface
* @return Collection * @return Collection
*/ */
public function getBudgetsForMonth(Carbon $date); public function getBudgetsForMonth(Carbon $date);
/**
* @param Carbon $date
*
* @return array
*/
public function listOfMonths(Carbon $date);
/**
* @param Carbon $date
*
* @return array
*/
public function listOfYears(Carbon $date);
/**
* @param Carbon $date
* @param bool $showSharedReports
*
* @return array
*/
public function yearBalanceReport(Carbon $date, $showSharedReports = false);
} }

View File

@@ -447,12 +447,13 @@ class ReportQuery implements ReportQueryInterface
* *
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* @param bool $showSharedReports
* *
* @return Collection * @return Collection
*/ */
public function journalsByRevenueAccount(Carbon $start, Carbon $end) public function journalsByRevenueAccount(Carbon $start, Carbon $end, $showSharedReports = false)
{ {
return TransactionJournal:: $query = TransactionJournal::
leftJoin( 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);
@@ -475,8 +476,10 @@ class ReportQuery implements ReportQueryInterface
$join->on('ac_to.id', '=', 'acm_to.account_id')->where('acm_to.name', '=', 'accountRole'); $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') ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');
->where( if ($showSharedReports === false) {
$query->where(
function ($query) { function ($query) {
$query->where( $query->where(
function ($q) { function ($q) {
@@ -491,11 +494,13 @@ class ReportQuery implements ReportQueryInterface
} }
); );
} }
) );
->before($end)->after($start) }
$query->before($end)->after($start)
->where('transaction_journals.user_id', Auth::user()->id) ->where('transaction_journals.user_id', Auth::user()->id)
->groupBy('t_from.account_id')->orderBy('amount') ->groupBy('t_from.account_id')->orderBy('amount');
->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`')]);
} }
/** /**

View File

@@ -136,10 +136,11 @@ interface ReportQueryInterface
* *
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* @param bool $showSharedReports
* *
* @return Collection * @return Collection
*/ */
public function journalsByRevenueAccount(Carbon $start, Carbon $end); public function journalsByRevenueAccount(Carbon $start, Carbon $end, $showSharedReports = false);
/** /**
* With an equally misleading name, this query returns are transfers to shared accounts. These are considered * With an equally misleading name, this query returns are transfers to shared accounts. These are considered

View File

@@ -363,6 +363,9 @@ class ReportController extends Controller
} catch (Exception $e) { } catch (Exception $e) {
return view('error')->with('message', 'Invalid date.'); return view('error')->with('message', 'Invalid date.');
} }
$pref = Preferences::get('showSharedReports', false);
$showSharedReports = $pref->data;
$date = new Carbon('01-01-' . $year); $date = new Carbon('01-01-' . $year);
$end = clone $date; $end = clone $date;
$end->endOfYear(); $end->endOfYear();
@@ -370,9 +373,9 @@ class ReportController extends Controller
$subTitle = $year; $subTitle = $year;
$subTitleIcon = 'fa-bar-chart'; $subTitleIcon = 'fa-bar-chart';
$mainTitleIcon = 'fa-line-chart'; $mainTitleIcon = 'fa-line-chart';
$balances = $helper->yearBalanceReport($date); $balances = $helper->yearBalanceReport($date, $showSharedReports);
$groupedIncomes = $query->journalsByRevenueAccount($date, $end); $groupedIncomes = $query->journalsByRevenueAccount($date, $end, $showSharedReports);
$groupedExpenses = $query->journalsByExpenseAccount($date, $end); $groupedExpenses = $query->journalsByExpenseAccount($date, $end, $showSharedReports);
//$groupedExpenses = $helper-> expensesGroupedByAccount($date, $end, 15); //$groupedExpenses = $helper-> expensesGroupedByAccount($date, $end, 15);