From c259a46ed34b6f061b00e0ea71a15ec8036cf536 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 20 Mar 2015 22:39:07 +0100 Subject: [PATCH] Clean up sql codes. --- app/Http/Middleware/Reminders.php | 3 +- .../Account/AccountRepository.php | 10 +++++-- app/Support/Amount.php | 21 ++++++++++---- resources/views/list/journals-tiny.blade.php | 28 +++++++++++++------ 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/app/Http/Middleware/Reminders.php b/app/Http/Middleware/Reminders.php index 5f031cacc2..b5a3a2f7b6 100644 --- a/app/Http/Middleware/Reminders.php +++ b/app/Http/Middleware/Reminders.php @@ -45,7 +45,7 @@ class Reminders */ public function handle($request, Closure $next) { - if ($this->auth->check() && !$request->isJson()) { + if ($this->auth->check() && !$request->isXmlHttpRequest()) { // do reminders stuff. $piggyBanks = $this->auth->user()->piggyBanks()->where('remind_me', 1)->get(); $today = new Carbon; @@ -85,7 +85,6 @@ class Reminders View::share('reminders', $reminders); } - return $next($request); } } \ No newline at end of file diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 506f0b9ddd..104a6b6a13 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -64,6 +64,10 @@ class AccountRepository implements AccountRepositoryInterface } /** + * This method is used on the front page where (in turn) its viewed journals-tiny.php which (in turn) + * is almost the only place where formatJournal is used. Aka, we can use some custom querying to get some specific. + * fields using left joins. + * * @param Account $account * @param Carbon $start * @param Carbon $end @@ -74,15 +78,17 @@ class AccountRepository implements AccountRepositoryInterface { return Auth::user() ->transactionjournals() - ->with(['transactions', 'transactioncurrency', 'transactiontype']) + ->with(['transactions']) ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')->where('accounts.id', $account->id) + ->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transaction_journals.transaction_currency_id') + ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id') ->before($end) ->after($start) ->orderBy('transaction_journals.date', 'DESC') ->orderBy('transaction_journals.id', 'DESC') ->take(10) - ->get(['transaction_journals.*']); + ->get(['transaction_journals.*', 'transaction_currencies.symbol', 'transaction_types.type']); } /** diff --git a/app/Support/Amount.php b/app/Support/Amount.php index 7d06975956..fc6868cf97 100644 --- a/app/Support/Amount.php +++ b/app/Support/Amount.php @@ -90,12 +90,23 @@ class Amount public function formatJournal(TransactionJournal $journal, $coloured = true) { $showPositive = true; - $symbol = $journal->transactionCurrency->symbol; - $amount = 0; - switch ($journal->transactionType->type) { - case 'Withdrawal': - $showPositive = false; + if (is_null($journal->symbol)) { + $symbol = $journal->transactionCurrency->symbol; + } else { + $symbol = $journal->symbol; } + $amount = 0; + + if (is_null($journal->type)) { + $type = $journal->transactionType->type; + } else { + $type = $journal->type; + } + + if ($type == 'Withdrawal') { + $showPositive = false; + } + foreach ($journal->transactions as $t) { if (floatval($t->amount) > 0 && $showPositive === true) { $amount = floatval($t->amount); diff --git a/resources/views/list/journals-tiny.blade.php b/resources/views/list/journals-tiny.blade.php index f646679401..8d90b43698 100644 --- a/resources/views/list/journals-tiny.blade.php +++ b/resources/views/list/journals-tiny.blade.php @@ -2,14 +2,26 @@ @foreach($transactions as $journal) - @if($journal->transactiontype->type == 'Withdrawal') - - @endif - @if($journal->transactiontype->type == 'Deposit') - - @endif - @if($journal->transactiontype->type == 'Transfer') - + @if(is_null($journal->type)) + @if($journal->transactiontype->type == 'Withdrawal') + + @endif + @if($journal->transactiontype->type == 'Deposit') + + @endif + @if($journal->transactiontype->type == 'Transfer') + + @endif + @else + @if($journal->type == 'Withdrawal') + + @endif + @if($journal->type == 'Deposit') + + @endif + @if($journal->type == 'Transfer') + + @endif @endif {{{$journal->description}}}