Simplified (or tried to) some code. [skip ci]

This commit is contained in:
James Cole
2015-06-07 09:09:27 +02:00
parent 583d4f3249
commit e95273b72b
8 changed files with 108 additions and 65 deletions

View File

@@ -270,6 +270,7 @@ class TagController extends Controller
'tagMode' => $request->get('tagMode'), 'tagMode' => $request->get('tagMode'),
]; ];
$repository->update($tag, $data); $repository->update($tag, $data);
Session::flash('success', 'Tag "' . e($data['tag']) . '" updated.'); Session::flash('success', 'Tag "' . e($data['tag']) . '" updated.');

View File

@@ -44,7 +44,7 @@ class Tag extends Model
protected $fillable = ['user_id', 'tag', 'date', 'description', 'longitude', 'latitude', 'zoomLevel', 'tagMode']; protected $fillable = ['user_id', 'tag', 'date', 'description', 'longitude', 'latitude', 'zoomLevel', 'tagMode'];
protected $rules protected $rules
= [ = [
'tag' => 'required|min:1|uniqueObjectForUser:tags,tag', 'tag' => 'required|min:1',
'description' => 'min:1', 'description' => 'min:1',
'date' => 'date', 'date' => 'date',
'latitude' => 'numeric|min:-90|max:90', 'latitude' => 'numeric|min:-90|max:90',

View File

@@ -146,7 +146,6 @@ class TransactionJournal extends Model
return $cache->get(); // @codeCoverageIgnore return $cache->get(); // @codeCoverageIgnore
} }
$amount = '0'; $amount = '0';
bcscale(2); bcscale(2);
/** @var Transaction $t */ /** @var Transaction $t */
@@ -155,59 +154,61 @@ class TransactionJournal extends Model
$amount = $t->amount; $amount = $t->amount;
} }
} }
$count = $this->tags->count();
/* if ($count === 1) {
* If the journal has tags, it gets complicated. // get amount for single tag:
*/ $amount = $this->amountByTag($this->tags()->first(), $amount);
if ($this->tags->count() == 0) {
$cache->store($amount);
return $amount;
} }
// if journal is part of advancePayment AND journal is a withdrawal, if ($count > 1) {
// then journal is being repaid by other journals, so the actual amount will lower: // get amount for either tag.
/** @var Tag $advancePayment */ $amount = $this->amountByTags($amount);
$advancePayment = $this->tags()->where('tagMode', 'advancePayment')->first();
if ($advancePayment && $this->transactionType->type == 'Withdrawal') {
// loop other deposits, remove from our amount.
$others = $advancePayment->transactionJournals()->transactionTypes(['Deposit'])->get();
foreach ($others as $other) {
$amount = bcsub($amount, $other->actual_amount);
}
$cache->store($amount);
return $amount;
} }
// if this journal is part of an advancePayment AND the journal is a deposit,
// then the journal amount is correcting a withdrawal, and the amount is zero:
if ($advancePayment && $this->transactionType->type == 'Deposit') {
$cache->store('0');
return '0';
}
// is balancing act?
$balancingAct = $this->tags()->where('tagMode', 'balancingAct')->first();
if ($balancingAct) {
// this is the expense:
if ($this->transactionType->type == 'Withdrawal') {
$transfer = $balancingAct->transactionJournals()->transactionTypes(['Transfer'])->first();
if ($transfer) {
$amount = bcsub($amount, $transfer->actual_amount);
$cache->store($amount);
return $amount;
}
} // @codeCoverageIgnore
} // @codeCoverageIgnore
$cache->store($amount); $cache->store($amount);
return $amount; return $amount;
}
/**
* Assuming the journal has only one tag. Parameter amount is used as fallback.
*
* @param Tag $tag
* @param string $amount
*
* @return string
*/
protected function amountByTag(Tag $tag, $amount)
{
if ($tag->tagMode == 'advancePayment') {
if ($this->transactionType->type == 'Withdrawal') {
$others = $tag->transactionJournals()->transactionTypes(['Deposit'])->get();
foreach ($others as $other) {
$amount = bcsub($amount, $other->actual_amount);
}
return $amount;
}
if ($this->transactionType->type == 'Deposit') {
return '0';
}
}
if ($tag->tagMode == 'balancingAct') {
if ($this->transactionType->type == 'Withdrawal') {
$transfer = $tag->transactionJournals()->transactionTypes(['Transfer'])->first();
if ($transfer) {
$amount = bcsub($amount, $transfer->actual_amount);
return $amount;
}
}
}
return $amount;
} }
/** /**
@@ -219,6 +220,26 @@ class TransactionJournal extends Model
return $this->belongsToMany('FireflyIII\Models\Tag'); return $this->belongsToMany('FireflyIII\Models\Tag');
} }
/**
* @param string $amount
*
* @return string
*/
public function amountByTags($amount)
{
$firstBalancingAct = $this->tags()->where('tagMode', 'balancingAct')->first();
if ($firstBalancingAct) {
return $this->amountByTag($firstBalancingAct, $amount);
}
$firstAdvancePayment = $this->tags()->where('tagMode', 'advancePayment')->first();
if ($firstAdvancePayment) {
return $this->amountByTag($firstAdvancePayment, $amount);
}
return $amount;
}
/** /**
* @return string * @return string
*/ */
@@ -232,7 +253,7 @@ class TransactionJournal extends Model
return $this->transactions()->where('amount', '<', 0)->first()->amount; return $this->transactions()->where('amount', '<', 0)->first()->amount;
} }
return '0'; return $this->transactions()->where('amount', '>', 0)->first()->amount;
} }
/** /**

View File

@@ -187,23 +187,26 @@ class Journal extends Twig_Extension
return $string; return $string;
} }
if ($tag->tagMode == 'advancePayment' && $journal->transactionType->type == 'Deposit') { if ($tag->tagMode == 'advancePayment') {
$amount = App::make('amount')->formatJournal($journal, false); if ($journal->transactionType->type == 'Deposit') {
$string = '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount $amount = App::make('amount')->formatJournal($journal, false);
. '"><i class="fa fa-fw fa-sort-numeric-desc"></i> ' . $tag->tag . '</a>'; $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>';
return $string; return $string;
} }
/*
* AdvancePayment with a withdrawal will show the amount with a link to
* the tag. The TransactionJournal should properly calculate the amount.
*/
if ($tag->tagMode == 'advancePayment' && $journal->transactionType->type == 'Withdrawal') {
$amount = App::make('amount')->formatJournal($journal);
$string = '<a href="' . route('tags.show', [$tag->id]) . '">' . $amount . '</a>'; /*
* AdvancePayment with a withdrawal will show the amount with a link to
* the tag. The TransactionJournal should properly calculate the amount.
*/
if ($journal->transactionType->type == 'Withdrawal') {
$amount = App::make('amount')->formatJournal($journal);
return $string; $string = '<a href="' . route('tags.show', [$tag->id]) . '">' . $amount . '</a>';
return $string;
}
} }

View File

@@ -239,7 +239,7 @@ class FireflyValidator extends Validator
// exclude? // exclude?
$table = $parameters[0]; $table = $parameters[0];
$field = $parameters[1]; $field = $parameters[1];
$exclude = isset($parameters[3]) ? intval($parameters[3]) : 0; $exclude = isset($parameters[2]) ? intval($parameters[2]) : 0;
// get entries from table // get entries from table
$set = DB::table($table)->where('user_id', Auth::user()->id)->where('id', '!=', $exclude)->get([$field]); $set = DB::table($table)->where('user_id', Auth::user()->id)->where('id', '!=', $exclude)->get([$field]);

View File

@@ -57,7 +57,7 @@
{% if not hideTags %} {% if not hideTags %}
{{ relevantTags(journal)|raw }} {{ relevantTags(journal)|raw }}
{% else %} {% else %}
{{ journal.correctAmount|formatAmount }} {{ journal.correct_amount|formatAmount }}
{% endif %} {% endif %}
</td> </td>
<td> <td>

View File

@@ -248,6 +248,24 @@ class TagControllerTest extends TestCase
$this->call('POST', '/tags/update/' . $tag->id, $data); $this->call('POST', '/tags/update/' . $tag->id, $data);
$this->assertResponseStatus(302); $this->assertResponseStatus(302);
$this->assertSessionHas('success');
}
public function testUpdateNoNameChange()
{
$tag = FactoryMuffin::create('FireflyIII\Models\Tag');
$this->be($tag->user);
$data = [
'_token' => 'replaceMe',
'tag' => $tag->tag,
'tagMode' => 'nothing',
'id' => $tag->id,
];
$this->call('POST', '/tags/update/' . $tag->id, $data);
$this->assertResponseStatus(302);
$this->assertSessionHas('success');
} }
/** /**

View File

@@ -329,7 +329,7 @@ class TransactionJournalModelTest extends TestCase
// get asset account: // get asset account:
$result = $journal->correct_amount; $result = $journal->correct_amount;
$this->assertEquals('0', $result); $this->assertEquals('300', $result);
} }
/** /**