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

@ -8,13 +8,13 @@ use Zizaco\Entrust\EntrustPermission;
* Class Permission
*
* @package FireflyIII\Models
* @property integer $id
* @property string $name
* @property string $display_name
* @property string $description
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\Config::get('entrust.role')[] $roles
* @property integer $id
* @property string $name
* @property string $display_name
* @property string $description
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\Config::get('entrust.role')[] $roles
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Permission whereId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Permission whereName($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Permission whereDisplayName($value)

View File

@ -8,14 +8,14 @@ use Zizaco\Entrust\EntrustRole;
* Class Role
*
* @package FireflyIII\Models
* @property integer $id
* @property string $name
* @property string $display_name
* @property string $description
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\Config::get('auth.model')[] $users
* @property-read \Illuminate\Database\Eloquent\Collection|\Config::get('entrust.permission')[] $perms
* @property integer $id
* @property string $name
* @property string $display_name
* @property string $description
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\Config::get('auth.model')[] $users
* @property-read \Illuminate\Database\Eloquent\Collection|\Config::get('entrust.permission')[] $perms
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Role whereId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Role whereName($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Role whereDisplayName($value)

View File

@ -68,9 +68,10 @@ use Watson\Validating\ValidatingTrait;
* @property mixed account_id
* @property mixed name
* @property mixed symbol
* @property-read mixed $correct_amount
* @property-read mixed $correct_amount
* @method static \FireflyIII\Models\TransactionJournal orderBy
* @method static \FireflyIII\Models\TransactionJournal|null first
* @property-read mixed $source_account
*/
class TransactionJournal extends Model
{
@ -294,14 +295,17 @@ class TransactionJournal extends Model
*/
public function getDestinationAccountAttribute()
{
/** @var Transaction $transaction */
foreach ($this->transactions()->get() as $transaction) {
if (floatval($transaction->amount) > 0) {
return $transaction->account;
}
}
$cache = new CacheProperties;
$cache->addProperty($this->id);
$cache->addProperty('destinationAccount');
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
* @return \Illuminate\Database\Eloquent\Relations\HasMany
@ -415,7 +436,7 @@ class TransactionJournal extends Model
public function scopeWithRelevantData(EloquentBuilder $query)
{
$query->with(
['transactions' => function(HasMany $q) {
['transactions' => function (HasMany $q) {
$q->orderBy('amount', 'ASC');
}, 'transactiontype', 'transactioncurrency', 'budgets', 'categories', 'transactions.account.accounttype', 'bill', 'budgets', 'categories']
);

View File

@ -8,6 +8,7 @@ use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Repositories\Shared\ComponentRepository;
use FireflyIII\Support\CacheProperties;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
@ -79,10 +80,10 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
/** @var Collection $repetitions */
return LimitRepetition::
leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00'))
->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00'))
->where('budget_limits.budget_id', $budget->id)
->get(['limit_repetitions.*']);
->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00'))
->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00'))
->where('budget_limits.budget_id', $budget->id)
->get(['limit_repetitions.*']);
}
/**
@ -113,7 +114,17 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
*/
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,13 +161,22 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
*/
public function getJournals(Budget $budget, LimitRepetition $repetition = null, $take = 50)
{
$offset = intval(Input::get('page')) > 0 ? intval(Input::get('page')) * $take : 0;
$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;
$setQuery = $budget->transactionJournals()->withRelevantData()->take($take)->offset($offset)
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC');
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC');
$countQuery = $budget->transactionJournals();
@ -169,7 +189,11 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
$set = $setQuery->get(['transaction_journals.*']);
$count = $countQuery->count();
return new LengthAwarePaginator($set, $count, $take, $offset);
$paginator = new LengthAwarePaginator($set, $count, $take, $offset);
$cache->store($paginator);
return $paginator;
}
/**
@ -196,9 +220,9 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
public function getLimitAmountOnDate(Budget $budget, Carbon $date)
{
$repetition = LimitRepetition::leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
->where('limit_repetitions.startdate', $date->format('Y-m-d 00:00:00'))
->where('budget_limits.budget_id', $budget->id)
->first(['limit_repetitions.*']);
->where('limit_repetitions.startdate', $date->format('Y-m-d 00:00:00'))
->where('budget_limits.budget_id', $budget->id)
->first(['limit_repetitions.*']);
if ($repetition) {
return floatval($repetition->amount);
@ -216,15 +240,15 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
public function getWithoutBudget(Carbon $start, Carbon $end)
{
return Auth::user()
->transactionjournals()
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->whereNull('budget_transaction_journal.id')
->before($end)
->after($start)
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC')
->get(['transaction_journals.*']);
->transactionjournals()
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->whereNull('budget_transaction_journal.id')
->before($end)
->after($start)
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC')
->get(['transaction_journals.*']);
}
/**
@ -236,22 +260,22 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
public function getWithoutBudgetSum(Carbon $start, Carbon $end)
{
$noBudgetSet = Auth::user()
->transactionjournals()
->whereNotIn(
'transaction_journals.id', function (QueryBuilder $query) use ($start, $end) {
$query
->select('transaction_journals.id')
->from('transaction_journals')
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->where('transaction_journals.date', '>=', $start->format('Y-m-d 00:00:00'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d 00:00:00'))
->whereNotNull('budget_transaction_journal.budget_id');
}
)
->after($start)
->before($end)
->transactionTypes(['Withdrawal'])
->get(['transaction_journals.*'])->sum('amount');
->transactionjournals()
->whereNotIn(
'transaction_journals.id', function (QueryBuilder $query) use ($start, $end) {
$query
->select('transaction_journals.id')
->from('transaction_journals')
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->where('transaction_journals.date', '>=', $start->format('Y-m-d 00:00:00'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d 00:00:00'))
->whereNotNull('budget_transaction_journal.budget_id');
}
)
->after($start)
->before($end)
->transactionTypes(['Withdrawal'])
->get(['transaction_journals.*'])->sum('amount');
return floatval($noBudgetSet) * -1;
}

View File

@ -3,6 +3,7 @@
namespace FireflyIII\Repositories\Shared;
use Carbon\Carbon;
use FireflyIII\Support\CacheProperties;
use Illuminate\Database\Query\JoinClause;
/**
@ -25,6 +26,21 @@ class ComponentRepository
*/
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) {
// shared is true.
// always ignore transfers between accounts!
@ -51,6 +67,8 @@ class ComponentRepository
->get(['transaction_journals.*'])->sum('amount');
}
$cache->store($sum);
return $sum;
}
}

View File

@ -4,6 +4,7 @@ namespace FireflyIII\Support\Twig;
use Auth;
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Support\CacheProperties;
use Twig_Extension;
use Twig_SimpleFunction;
@ -21,18 +22,25 @@ class Budget extends Twig_Extension
{
$functions = [];
$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
= Auth::user()->transactionjournals()
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budget_transaction_journal.budget_id')
->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
->before($repetition->enddate)
->after($repetition->startdate)
->where('limit_repetitions.id', '=', $repetition->id)
->get(['transaction_journals.*'])->sum('amount');
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budget_transaction_journal.budget_id')
->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
->before($repetition->enddate)
->after($repetition->startdate)
->where('limit_repetitions.id', '=', $repetition->id)
->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(
'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) {
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
// tags are present.
$amount = App::make('amount')->format($journal->actual_amount, false);
return '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount
$string = '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount
. '"><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') {
$amount = App::make('amount')->formatJournal($journal, false);
return '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount
$string = '<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>';
$cache->store($string);
return $string;
}
/*
* 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') {
$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') {
// return the amount:
return App::make('amount')->formatJournal($journal);
$string = App::make('amount')->formatJournal($journal);
$cache->store($string);
return $string;
}
}

View File

@ -33,7 +33,7 @@ use Zizaco\Entrust\Traits\EntrustUserTrait;
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\User wherePassword($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\User whereReset($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\User whereRememberToken($value)
* @property-read \Illuminate\Database\Eloquent\Collection|\Config::get('entrust.role')[] $roles
* @property-read \Illuminate\Database\Eloquent\Collection|\Config::get('entrust.role')[] $roles
*/
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{

View File

@ -64,17 +64,17 @@
{{journal.date.formatLocalized(monthAndDayFormat)}}
</td>
<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>
{% 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 %}
</td>
<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>
{% 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 %}
</td>