Some code cleanup.

This commit is contained in:
James Cole
2015-06-05 19:02:23 +02:00
parent 40e49ffc37
commit 681167bc1b
9 changed files with 171 additions and 84 deletions

View File

@@ -71,6 +71,7 @@ use Watson\Validating\ValidatingTrait;
* @property-read mixed $correct_amount * @property-read mixed $correct_amount
* @method static \FireflyIII\Models\TransactionJournal orderBy * @method static \FireflyIII\Models\TransactionJournal orderBy
* @method static \FireflyIII\Models\TransactionJournal|null first * @method static \FireflyIII\Models\TransactionJournal|null first
* @property-read mixed $source_account
*/ */
class TransactionJournal extends Model class TransactionJournal extends Model
{ {
@@ -294,14 +295,17 @@ class TransactionJournal extends Model
*/ */
public function getDestinationAccountAttribute() public function getDestinationAccountAttribute()
{ {
/** @var Transaction $transaction */ $cache = new CacheProperties;
foreach ($this->transactions()->get() as $transaction) { $cache->addProperty($this->id);
if (floatval($transaction->amount) > 0) { $cache->addProperty('destinationAccount');
return $transaction->account;
}
}
return $this->transactions()->first()->account; if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$account = $this->transactions()->where('amount', '>', 0)->first()->account;
$cache->store($account);
return $account;
} }
/** /**
@@ -325,6 +329,23 @@ class TransactionJournal extends Model
} }
/**
* @return Account
*/
public function getSourceAccountAttribute()
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
$cache->addProperty('destinationAccount');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$account = $this->transactions()->where('amount', '<', 0)->first()->account;
$cache->store($account);
return $account;
}
/** /**
* @codeCoverageIgnore * @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @return \Illuminate\Database\Eloquent\Relations\HasMany

View File

@@ -8,6 +8,7 @@ use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\LimitRepetition; use FireflyIII\Models\LimitRepetition;
use FireflyIII\Repositories\Shared\ComponentRepository; use FireflyIII\Repositories\Shared\ComponentRepository;
use FireflyIII\Support\CacheProperties;
use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@@ -113,7 +114,17 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
*/ */
public function getCurrentRepetition(Budget $budget, Carbon $date) public function getCurrentRepetition(Budget $budget, Carbon $date)
{ {
return $budget->limitrepetitions()->where('limit_repetitions.startdate', $date)->first(['limit_repetitions.*']); $cache = new CacheProperties;
$cache->addProperty($budget->id);
$cache->addProperty($date);
$cache->addProperty('getCurrentRepetition');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$data = $budget->limitrepetitions()->where('limit_repetitions.startdate', $date)->first(['limit_repetitions.*']);
$cache->store($data);
return $data;
} }
/** /**
@@ -150,9 +161,18 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
*/ */
public function getJournals(Budget $budget, LimitRepetition $repetition = null, $take = 50) public function getJournals(Budget $budget, LimitRepetition $repetition = null, $take = 50)
{ {
$cache = new CacheProperties;
$cache->addProperty($budget->id);
if ($repetition) {
$cache->addProperty($repetition->id);
}
$cache->addProperty($take);
$cache->addProperty('getJournals');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$offset = intval(Input::get('page')) > 0 ? intval(Input::get('page')) * $take : 0; $offset = intval(Input::get('page')) > 0 ? intval(Input::get('page')) * $take : 0;
$setQuery = $budget->transactionJournals()->withRelevantData()->take($take)->offset($offset) $setQuery = $budget->transactionJournals()->withRelevantData()->take($take)->offset($offset)
->orderBy('transaction_journals.date', 'DESC') ->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC') ->orderBy('transaction_journals.order', 'ASC')
@@ -169,7 +189,11 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
$set = $setQuery->get(['transaction_journals.*']); $set = $setQuery->get(['transaction_journals.*']);
$count = $countQuery->count(); $count = $countQuery->count();
return new LengthAwarePaginator($set, $count, $take, $offset);
$paginator = new LengthAwarePaginator($set, $count, $take, $offset);
$cache->store($paginator);
return $paginator;
} }
/** /**

View File

@@ -3,6 +3,7 @@
namespace FireflyIII\Repositories\Shared; namespace FireflyIII\Repositories\Shared;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Support\CacheProperties;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
/** /**
@@ -25,6 +26,21 @@ class ComponentRepository
*/ */
protected function spentInPeriod($object, Carbon $start, Carbon $end, $shared = false) protected function spentInPeriod($object, Carbon $start, Carbon $end, $shared = false)
{ {
// we must cache this.
$cache = new CacheProperties;
$cache->addProperty($object->id);
$cache->addProperty(get_class($object));
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($shared);
$cache->addProperty('spentInPeriod');
if($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
if ($shared === true) { if ($shared === true) {
// shared is true. // shared is true.
// always ignore transfers between accounts! // always ignore transfers between accounts!
@@ -51,6 +67,8 @@ class ComponentRepository
->get(['transaction_journals.*'])->sum('amount'); ->get(['transaction_journals.*'])->sum('amount');
} }
$cache->store($sum);
return $sum; return $sum;
} }
} }

View File

@@ -4,6 +4,7 @@ namespace FireflyIII\Support\Twig;
use Auth; use Auth;
use FireflyIII\Models\LimitRepetition; use FireflyIII\Models\LimitRepetition;
use FireflyIII\Support\CacheProperties;
use Twig_Extension; use Twig_Extension;
use Twig_SimpleFunction; use Twig_SimpleFunction;
@@ -22,6 +23,12 @@ class Budget extends Twig_Extension
$functions = []; $functions = [];
$functions[] = new Twig_SimpleFunction( $functions[] = new Twig_SimpleFunction(
'spentInRepetitionCorrected', function (LimitRepetition $repetition) { 'spentInRepetitionCorrected', function (LimitRepetition $repetition) {
$cache = new CacheProperties;
$cache->addProperty($repetition->id);
$cache->addProperty('spentInRepetitionCorrected');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$sum $sum
= Auth::user()->transactionjournals() = Auth::user()->transactionjournals()
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
@@ -31,8 +38,9 @@ class Budget extends Twig_Extension
->after($repetition->startdate) ->after($repetition->startdate)
->where('limit_repetitions.id', '=', $repetition->id) ->where('limit_repetitions.id', '=', $repetition->id)
->get(['transaction_journals.*'])->sum('amount'); ->get(['transaction_journals.*'])->sum('amount');
$cache->store($sum);
return floatval($sum); return $sum;
} }
); );

View File

@@ -87,8 +87,18 @@ class Journal extends Twig_Extension
$functions[] = new Twig_SimpleFunction( $functions[] = new Twig_SimpleFunction(
'relevantTags', function(TransactionJournal $journal) { 'relevantTags', function(TransactionJournal $journal) {
$cache = new CacheProperties;
$cache->addProperty('relevantTags');
$cache->addProperty($journal->id);
if($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
if ($journal->tags->count() == 0) { if ($journal->tags->count() == 0) {
return App::make('amount')->formatJournal($journal); $string = App::make('amount')->formatJournal($journal);
$cache->store($string);
return $string;
} }
@@ -97,9 +107,10 @@ class Journal extends Twig_Extension
// return tag formatted for a "balancing act", even if other // return tag formatted for a "balancing act", even if other
// tags are present. // tags are present.
$amount = App::make('amount')->format($journal->actual_amount, false); $amount = App::make('amount')->format($journal->actual_amount, false);
$string = '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount
return '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount
. '"><i class="fa fa-fw fa-refresh"></i> ' . $tag->tag . '</a>'; . '"><i class="fa fa-fw fa-refresh"></i> ' . $tag->tag . '</a>';
$cache->store($string);
return $string;
} }
/* /*
@@ -107,9 +118,10 @@ class Journal extends Twig_Extension
*/ */
if ($tag->tagMode == 'advancePayment' && $journal->transactionType->type == 'Deposit') { if ($tag->tagMode == 'advancePayment' && $journal->transactionType->type == 'Deposit') {
$amount = App::make('amount')->formatJournal($journal, false); $amount = App::make('amount')->formatJournal($journal, false);
$string = '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount
return '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount
. '"><i class="fa fa-fw fa-sort-numeric-desc"></i> ' . $tag->tag . '</a>'; . '"><i class="fa fa-fw fa-sort-numeric-desc"></i> ' . $tag->tag . '</a>';
$cache->store($string);
return $string;
} }
/* /*
* AdvancePayment with a withdrawal will show the amount with a link to * AdvancePayment with a withdrawal will show the amount with a link to
@@ -118,13 +130,17 @@ class Journal extends Twig_Extension
if ($tag->tagMode == 'advancePayment' && $journal->transactionType->type == 'Withdrawal') { if ($tag->tagMode == 'advancePayment' && $journal->transactionType->type == 'Withdrawal') {
$amount = App::make('amount')->formatJournal($journal); $amount = App::make('amount')->formatJournal($journal);
return '<a href="' . route('tags.show', [$tag->id]) . '">' . $amount . '</a>'; $string = '<a href="' . route('tags.show', [$tag->id]) . '">' . $amount . '</a>';
$cache->store($string);
return $string;
} }
if ($tag->tagMode == 'nothing') { if ($tag->tagMode == 'nothing') {
// return the amount: // return the amount:
return App::make('amount')->formatJournal($journal); $string = App::make('amount')->formatJournal($journal);
$cache->store($string);
return $string;
} }
} }

View File

@@ -64,17 +64,17 @@
{{journal.date.formatLocalized(monthAndDayFormat)}} {{journal.date.formatLocalized(monthAndDayFormat)}}
</td> </td>
<td class="hidden-xs"> <td class="hidden-xs">
{% if journal.transactions[0].account.accountType.type == 'Cash account' %} {% if journal.source_account.accountType.type == 'Cash account' %}
<span class="text-success">(cash)</span> <span class="text-success">(cash)</span>
{% else %} {% else %}
<a href="{{route('accounts.show',journal.transactions[0].account_id)}}">{{journal.transactions[0].account.name}}</a> <a href="{{route('accounts.show',journal.source_account.id)}}">{{journal.source_account.name}}</a>
{% endif %} {% endif %}
</td> </td>
<td class="hidden-xs"> <td class="hidden-xs">
{% if journal.transactions[1].account.accountType.type == 'Cash account' %} {% if journal.destination_account.accountType.type == 'Cash account' %}
<span class="text-success">(cash)</span> <span class="text-success">(cash)</span>
{% else %} {% else %}
<a href="{{route('accounts.show',journal.transactions[1].account_id)}}">{{journal.transactions[1].account.name}}</a> <a href="{{route('accounts.show',journal.destination_account.id)}}">{{journal.destination_account.name}}</a>
{% endif %} {% endif %}
</td> </td>