From 114788567dca8987c632abb0f7a3d846b7a612d7 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 10 Mar 2015 19:57:20 +0100 Subject: [PATCH] More code for previous. --- app/Helpers/Report/ReportHelper.php | 45 ++++++----- app/Helpers/Report/ReportHelperInterface.php | 43 +++++----- app/Helpers/Report/ReportQuery.php | 85 +++++++++++--------- app/Helpers/Report/ReportQueryInterface.php | 3 +- app/Http/Controllers/ReportController.php | 13 +-- 5 files changed, 101 insertions(+), 88 deletions(-) diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php index 9d3f513b1f..156c2396b3 100644 --- a/app/Helpers/Report/ReportHelper.php +++ b/app/Helpers/Report/ReportHelper.php @@ -7,7 +7,6 @@ use Carbon\Carbon; use FireflyIII\Models\Account; use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Collection; -use Session; /** * Class ReportHelper @@ -87,7 +86,7 @@ class ReportHelper implements ReportHelperInterface $end = Carbon::now(); $months = []; while ($start <= $end) { - $year = $start->format('Y'); + $year = $start->format('Y'); $months[$year][] = [ 'formatted' => $start->format('F Y'), 'month' => intval($start->format('m')), @@ -119,33 +118,37 @@ class ReportHelper implements ReportHelperInterface /** * @param Carbon $date + * @param bool $showSharedReports * * @return array */ - public function yearBalanceReport(Carbon $date) + public function yearBalanceReport(Carbon $date, $showSharedReports = false) { - $start = clone $date; - $end = clone $date; - $sharedAccounts = []; - $sharedCollection = \Auth::user()->accounts() - ->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id') - ->where('account_meta.name', '=', 'accountRole') - ->where('account_meta.data', '=', json_encode('sharedAsset')) - ->get(['accounts.id']); + $start = clone $date; + $end = clone $date; + $sharedAccounts = []; + if ($showSharedReports === false) { + $sharedCollection = \Auth::user()->accounts() + ->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id') + ->where('account_meta.name', '=', 'accountRole') + ->where('account_meta.data', '=', json_encode('sharedAsset')) + ->get(['accounts.id']); - foreach ($sharedCollection as $account) { - $sharedAccounts[] = $account->id; + foreach ($sharedCollection as $account) { + $sharedAccounts[] = $account->id; + } } - $accounts = \Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name','ASC')->get(['accounts.*'])->filter( - function (Account $account) use ($sharedAccounts) { - if (!in_array($account->id, $sharedAccounts)) { - return $account; - } + $accounts = \Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name', 'ASC')->get(['accounts.*']) + ->filter( + function (Account $account) use ($sharedAccounts) { + if (!in_array($account->id, $sharedAccounts)) { + return $account; + } - return null; - } - ); + return null; + } + ); $report = []; $start->startOfYear()->subDay(); $end->endOfYear(); diff --git a/app/Helpers/Report/ReportHelperInterface.php b/app/Helpers/Report/ReportHelperInterface.php index 49b9b541ac..dedc6b38cb 100644 --- a/app/Helpers/Report/ReportHelperInterface.php +++ b/app/Helpers/Report/ReportHelperInterface.php @@ -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. * @@ -54,4 +33,26 @@ interface ReportHelperInterface * @return Collection */ 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); } \ No newline at end of file diff --git a/app/Helpers/Report/ReportQuery.php b/app/Helpers/Report/ReportQuery.php index be762c34e8..a465799d03 100644 --- a/app/Helpers/Report/ReportQuery.php +++ b/app/Helpers/Report/ReportQuery.php @@ -447,55 +447,60 @@ class ReportQuery implements ReportQueryInterface * * @param Carbon $start * @param Carbon $end + * @param bool $showSharedReports * * @return Collection */ - public function journalsByRevenueAccount(Carbon $start, Carbon $end) + public function journalsByRevenueAccount(Carbon $start, Carbon $end, $showSharedReports = false) { - return TransactionJournal:: + $query = TransactionJournal:: leftJoin( 'transactions as t_from', function (JoinClause $join) { $join->on('t_from.transaction_journal_id', '=', 'transaction_journals.id')->where('t_from.amount', '<', 0); } ) - ->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') - ->where( - function ($query) { - $query->where( - function ($q) { - $q->where('transaction_types.type', 'Deposit'); - $q->where('acm_to.data', '!=', '"sharedAsset"'); - } - ); - $query->orWhere( - function ($q) { - $q->where('transaction_types.type', 'Transfer'); - $q->where('acm_from.data', '=', '"sharedAsset"'); - } - ); - } - ) - ->before($end)->after($start) - ->where('transaction_journals.user_id', Auth::user()->id) - ->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`')]); + ->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'); + if ($showSharedReports === false) { + + $query->where( + function ($query) { + $query->where( + function ($q) { + $q->where('transaction_types.type', 'Deposit'); + $q->where('acm_to.data', '!=', '"sharedAsset"'); + } + ); + $query->orWhere( + function ($q) { + $q->where('transaction_types.type', 'Transfer'); + $q->where('acm_from.data', '=', '"sharedAsset"'); + } + ); + } + ); + } + $query->before($end)->after($start) + ->where('transaction_journals.user_id', Auth::user()->id) + ->groupBy('t_from.account_id')->orderBy('amount'); + + return $query->get(['t_from.account_id as account_id', 'ac_from.name as name', DB::Raw('SUM(t_from.amount) as `amount`')]); } /** diff --git a/app/Helpers/Report/ReportQueryInterface.php b/app/Helpers/Report/ReportQueryInterface.php index 9ae250b8cf..e67581a047 100644 --- a/app/Helpers/Report/ReportQueryInterface.php +++ b/app/Helpers/Report/ReportQueryInterface.php @@ -136,10 +136,11 @@ interface ReportQueryInterface * * @param Carbon $start * @param Carbon $end + * @param bool $showSharedReports * * @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 diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index a8d3bf1912..f54ef7c911 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -363,16 +363,19 @@ class ReportController extends Controller } catch (Exception $e) { return view('error')->with('message', 'Invalid date.'); } - $date = new Carbon('01-01-' . $year); - $end = clone $date; + + $pref = Preferences::get('showSharedReports', false); + $showSharedReports = $pref->data; + $date = new Carbon('01-01-' . $year); + $end = clone $date; $end->endOfYear(); $title = 'Reports'; $subTitle = $year; $subTitleIcon = 'fa-bar-chart'; $mainTitleIcon = 'fa-line-chart'; - $balances = $helper->yearBalanceReport($date); - $groupedIncomes = $query->journalsByRevenueAccount($date, $end); - $groupedExpenses = $query->journalsByExpenseAccount($date, $end); + $balances = $helper->yearBalanceReport($date, $showSharedReports); + $groupedIncomes = $query->journalsByRevenueAccount($date, $end, $showSharedReports); + $groupedExpenses = $query->journalsByExpenseAccount($date, $end, $showSharedReports); //$groupedExpenses = $helper-> expensesGroupedByAccount($date, $end, 15);