From 8ad1ede0c5df634fa5a03ef6ae9913c06a541d96 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 4 Mar 2015 20:47:00 +0100 Subject: [PATCH] Popups in the reports as we've always promised. --- app/Helpers/Report/ReportQuery.php | 59 +++++++++++++- app/Helpers/Report/ReportQueryInterface.php | 24 ++++++ app/Http/Controllers/ReportController.php | 81 ++++++++++++++++++- app/Http/routes.php | 5 ++ public/js/reports.js | 20 +++++ resources/views/layouts/default.blade.php | 4 + resources/views/reports/budget.blade.php | 46 ++--------- .../reports/modal-journal-list.blade.php | 14 ++++ 8 files changed, 207 insertions(+), 46 deletions(-) create mode 100644 resources/views/reports/modal-journal-list.blade.php diff --git a/app/Helpers/Report/ReportQuery.php b/app/Helpers/Report/ReportQuery.php index d0067b25bd..57bfd3d6e4 100644 --- a/app/Helpers/Report/ReportQuery.php +++ b/app/Helpers/Report/ReportQuery.php @@ -87,14 +87,32 @@ class ReportQuery implements ReportQueryInterface ->whereNull('otherJournals.deleted_at') ->where('transactions.account_id', $account->id) ->whereNotNull('transaction_group_transaction_journal.transaction_group_id') - ->first( + ->get( [ - DB::Raw('SUM(`transactions`.`amount`) as `amount`') + 'transaction_journals.*', + 'transactions.amount' ] ); + + return $set; + } + + /** + * This method will get the sum of all expenses in a certain time period that have no budget + * and are balanced by a transfer to make up for it. + * + * @param Account $account + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function balancedTransactionsSum(Account $account, Carbon $start, Carbon $end) + { + $set = $this->balancedTransactionsList($account, $start, $end); $sum = 0; - if (!is_null($set)) { - $sum = floatval($set->amount); + foreach($set as $entry) { + $sum += floatval($entry->amount); } return $sum; @@ -167,7 +185,40 @@ class ReportQuery implements ReportQueryInterface return $set; + } + /** + * Get a list of transaction journals that have no budget, filtered for the specified account + * and the specified date range. + * + * @param Account $account + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function getTransactionsWithoutBudget(Account $account, Carbon $start, Carbon $end) + { + $set = TransactionJournal:: + leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') + ->leftJoin('budgets', 'budgets.id', '=', 'budget_transaction_journal.budget_id') + ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id') + ->leftJoin( + 'transactions', function (JoinClause $join) { + $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0); + } + ) + ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id') + ->before($end) + ->after($start) + ->where('accounts.id', $account->id) + ->where('transaction_journals.user_id', Auth::user()->id) + ->where('transaction_types.type', 'Withdrawal') + ->whereNull('budgets.id') + ->orderBy('transaction_journals.date', 'ASC') + ->get(['budgets.name', 'transactions.amount', 'transaction_journals.*']); + + return $set; } /** diff --git a/app/Helpers/Report/ReportQueryInterface.php b/app/Helpers/Report/ReportQueryInterface.php index 1bce968341..17ab74eb47 100644 --- a/app/Helpers/Report/ReportQueryInterface.php +++ b/app/Helpers/Report/ReportQueryInterface.php @@ -120,6 +120,18 @@ interface ReportQueryInterface */ public function getBudgetSummary(Account $account, Carbon $start, Carbon $end); + /** + * Get a list of transaction journals that have no budget, filtered for the specified account + * and the specified date range. + * + * @param Account $account + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function getTransactionsWithoutBudget(Account $account, Carbon $start, Carbon $end); + /** * This method will get a list of all expenses in a certain time period that have no budget * and are balanced by a transfer to make up for it. @@ -131,4 +143,16 @@ interface ReportQueryInterface * @return Collection */ public function balancedTransactionsList(Account $account, Carbon $start, Carbon $end); + + /** + * This method will get the sum of all expenses in a certain time period that have no budget + * and are balanced by a transfer to make up for it. + * + * @param Account $account + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function balancedTransactionsSum(Account $account, Carbon $start, Carbon $end); } \ No newline at end of file diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index 2afc7123bc..0da67524bd 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -7,10 +7,12 @@ use FireflyIII\Helpers\Report\ReportHelperInterface; use FireflyIII\Helpers\Report\ReportQueryInterface; use FireflyIII\Http\Requests; use FireflyIII\Models\Account; +use FireflyIII\Models\TransactionJournal; use Illuminate\Database\Query\JoinClause; +use Session; use Steam; use View; -use Session; + /** * Class ReportController * @@ -59,7 +61,7 @@ class ReportController extends Controller $accounts->each( function (Account $account) use ($start, $end, $query) { $budgets = $query->getBudgetSummary($account, $start, $end); - $balancedAmount = $query->balancedTransactionsList($account, $start, $end); + $balancedAmount = $query->balancedTransactionsSum($account, $start, $end); $array = []; foreach ($budgets as $budget) { $id = intval($budget->id); @@ -104,7 +106,7 @@ class ReportController extends Controller * End getBudgetsForMonth DONE */ - return view('reports.budget', compact('subTitle', 'subTitleIcon', 'date', 'accounts', 'budgets', 'dayEarly')); + return view('reports.budget', compact('subTitle', 'year', 'month', 'subTitleIcon', 'date', 'accounts', 'budgets', 'dayEarly')); } @@ -124,6 +126,79 @@ class ReportController extends Controller return view('reports.index', compact('years', 'months', 'title', 'mainTitleIcon')); } + /** + * @param Account $account + * @param string $year + * @param string $month + * + * @return \Illuminate\View\View + */ + public function modalBalancedTransfers(Account $account, $year = '2014', $month = '1', ReportQueryInterface $query) + { + + try { + new Carbon($year . '-' . $month . '-01'); + } catch (Exception $e) { + return view('error')->with('message', 'Invalid date'); + } + $start = new Carbon($year . '-' . $month . '-01'); + $end = clone $start; + $end->endOfMonth(); + + $journals = $query->balancedTransactionsList($account, $start, $end); + + return view('reports.modal-journal-list', compact('journals')); + + + } + + public function modalLeftUnbalanced(Account $account, $year = '2014', $month = '1', ReportQueryInterface $query) + { + try { + new Carbon($year . '-' . $month . '-01'); + } catch (Exception $e) { + return view('error')->with('message', 'Invalid date'); + } + $start = new Carbon($year . '-' . $month . '-01'); + $end = clone $start; + $end->endOfMonth(); + $set = $query->getTransactionsWithoutBudget($account, $start, $end); + + $journals = $set->filter( + function (TransactionJournal $journal) { + $count = $journal->transactiongroups()->where('relation', 'balance')->count(); + if ($count == 0) { + return $journal; + } + } + ); + + return view('reports.modal-journal-list', compact('journals')); + } + + /** + * @param Account $account + * @param string $year + * @param string $month + * + * @return \Illuminate\View\View + */ + public function modalNoBudget(Account $account, $year = '2014', $month = '1', ReportQueryInterface $query) + { + try { + new Carbon($year . '-' . $month . '-01'); + } catch (Exception $e) { + return view('error')->with('message', 'Invalid date'); + } + $start = new Carbon($year . '-' . $month . '-01'); + $end = clone $start; + $end->endOfMonth(); + $journals = $query->getTransactionsWithoutBudget($account, $start, $end); + + return view('reports.modal-journal-list', compact('journals')); + + } + /** * @param string $year * @param string $month diff --git a/app/Http/routes.php b/app/Http/routes.php index 664c1dd38b..f50c8f9ee0 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -311,6 +311,11 @@ Route::group( Route::get('/reports/{year}/{month}', ['uses' => 'ReportController@month', 'as' => 'reports.month']); Route::get('/reports/budget/{year}/{month}', ['uses' => 'ReportController@budget', 'as' => 'reports.budget']); + // pop ups for budget report: + Route::get('/reports/modal/{account}/{year}/{month}/no-budget', ['uses' => 'ReportController@modalNoBudget','as' => 'reports.no-budget']); + Route::get('/reports/modal/{account}/{year}/{month}/balanced-transfers', ['uses' => 'ReportController@modalBalancedTransfers','as' => 'reports.balanced-transfers']); + Route::get('/reports/modal/{account}/{year}/{month}/left-unbalanced', ['uses' => 'ReportController@modalLeftUnbalanced','as' => 'reports.left-unbalanced']); + /** * Search Controller */ diff --git a/public/js/reports.js b/public/js/reports.js index 4dee6cbbdd..db674c2f91 100644 --- a/public/js/reports.js +++ b/public/js/reports.js @@ -6,4 +6,24 @@ if (typeof(google) != 'undefined') { googleStackedColumnChart('chart/budgets/spending/' + year, 'budgets'); } +} + + +$(function () { + $('.openModal').on('click', openModal); +}); + +function openModal(e) { + "use strict"; + var target = $(e.target).parent(); + var URL = target.attr('href'); + + $.get(URL).success(function (data) { + $('#defaultModal').empty().html(data).modal('show'); + + }).fail(function () { + alert('Could not load data.'); + }); + + return false; } \ No newline at end of file diff --git a/resources/views/layouts/default.blade.php b/resources/views/layouts/default.blade.php index cf12b29c91..d260d81dde 100644 --- a/resources/views/layouts/default.blade.php +++ b/resources/views/layouts/default.blade.php @@ -111,6 +111,10 @@ + + + diff --git a/resources/views/reports/budget.blade.php b/resources/views/reports/budget.blade.php index d47cae3336..32aa2d7a34 100644 --- a/resources/views/reports/budget.blade.php +++ b/resources/views/reports/budget.blade.php @@ -42,18 +42,16 @@ @foreach($budgets as $id => $budget) - {{{$budget['name']}}} - @if($id == 0) - - @endif - + {{{$budget['name']}}} {!! Amount::format($budget['amount']) !!} @foreach($accounts as $account) @if(isset($account->budgetInformation[$id])) @if($id == 0) - {!! Amount::format($account->budgetInformation[$id]['amount']) !!} + + {!! Amount::format($account->budgetInformation[$id]['amount']) !!} + @else {!! Amount::format($account->budgetInformation[$id]['amount']) !!} @endif @@ -70,46 +68,15 @@ {!! Amount::format($budget['amount'] + $spent) !!} @endforeach - - Without budget - - - @foreach($accounts as $account) - @if(isset($account->budgetInformation[0])) - - {!! Amount::format($account->budgetInformation[0]['amount']) !!} - - @else - {!! Amount::format(0) !!} - @endif - @endforeach -   - Balanced by transfers @foreach($accounts as $account) - {!! Amount::format($account->balancedAmount) !!} + {!! Amount::format($account->balancedAmount) !!} @endforeach   - Left unbalanced @foreach($accounts as $account) @@ -118,7 +85,7 @@ ?> @if(isset($account->budgetInformation[0])) - {!! Amount::format($account->budgetInformation[0]['amount'] + $account->balancedAmount) !!} + {!! Amount::format($account->budgetInformation[0]['amount'] + $account->balancedAmount) !!} @else {!! Amount::format(0) !!} @@ -152,5 +119,6 @@ @stop @section('scripts') + @stop diff --git a/resources/views/reports/modal-journal-list.blade.php b/resources/views/reports/modal-journal-list.blade.php new file mode 100644 index 0000000000..50ad3086b6 --- /dev/null +++ b/resources/views/reports/modal-journal-list.blade.php @@ -0,0 +1,14 @@ +