mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Simplified (or tried to) some code. [skip ci]
This commit is contained in:
@@ -270,6 +270,7 @@ class TagController extends Controller
|
||||
'tagMode' => $request->get('tagMode'),
|
||||
];
|
||||
|
||||
|
||||
$repository->update($tag, $data);
|
||||
|
||||
Session::flash('success', 'Tag "' . e($data['tag']) . '" updated.');
|
||||
|
@@ -44,7 +44,7 @@ class Tag extends Model
|
||||
protected $fillable = ['user_id', 'tag', 'date', 'description', 'longitude', 'latitude', 'zoomLevel', 'tagMode'];
|
||||
protected $rules
|
||||
= [
|
||||
'tag' => 'required|min:1|uniqueObjectForUser:tags,tag',
|
||||
'tag' => 'required|min:1',
|
||||
'description' => 'min:1',
|
||||
'date' => 'date',
|
||||
'latitude' => 'numeric|min:-90|max:90',
|
||||
|
@@ -146,7 +146,6 @@ class TransactionJournal extends Model
|
||||
return $cache->get(); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
|
||||
$amount = '0';
|
||||
bcscale(2);
|
||||
/** @var Transaction $t */
|
||||
@@ -155,59 +154,61 @@ class TransactionJournal extends Model
|
||||
$amount = $t->amount;
|
||||
}
|
||||
}
|
||||
$count = $this->tags->count();
|
||||
|
||||
/*
|
||||
* If the journal has tags, it gets complicated.
|
||||
*/
|
||||
if ($this->tags->count() == 0) {
|
||||
$cache->store($amount);
|
||||
|
||||
return $amount;
|
||||
if ($count === 1) {
|
||||
// get amount for single tag:
|
||||
$amount = $this->amountByTag($this->tags()->first(), $amount);
|
||||
}
|
||||
|
||||
// if journal is part of advancePayment AND journal is a withdrawal,
|
||||
// then journal is being repaid by other journals, so the actual amount will lower:
|
||||
/** @var Tag $advancePayment */
|
||||
$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);
|
||||
if ($count > 1) {
|
||||
// get amount for either tag.
|
||||
$amount = $this->amountByTags($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);
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
@@ -232,7 +253,7 @@ class TransactionJournal extends Model
|
||||
return $this->transactions()->where('amount', '<', 0)->first()->amount;
|
||||
}
|
||||
|
||||
return '0';
|
||||
return $this->transactions()->where('amount', '>', 0)->first()->amount;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -187,23 +187,26 @@ class Journal extends Twig_Extension
|
||||
return $string;
|
||||
}
|
||||
|
||||
if ($tag->tagMode == 'advancePayment' && $journal->transactionType->type == 'Deposit') {
|
||||
$amount = App::make('amount')->formatJournal($journal, false);
|
||||
$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>';
|
||||
if ($tag->tagMode == 'advancePayment') {
|
||||
if ($journal->transactionType->type == 'Deposit') {
|
||||
$amount = App::make('amount')->formatJournal($journal, false);
|
||||
$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;
|
||||
}
|
||||
/*
|
||||
* 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);
|
||||
return $string;
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -239,7 +239,7 @@ class FireflyValidator extends Validator
|
||||
// exclude?
|
||||
$table = $parameters[0];
|
||||
$field = $parameters[1];
|
||||
$exclude = isset($parameters[3]) ? intval($parameters[3]) : 0;
|
||||
$exclude = isset($parameters[2]) ? intval($parameters[2]) : 0;
|
||||
|
||||
// get entries from table
|
||||
$set = DB::table($table)->where('user_id', Auth::user()->id)->where('id', '!=', $exclude)->get([$field]);
|
||||
|
@@ -57,7 +57,7 @@
|
||||
{% if not hideTags %}
|
||||
{{ relevantTags(journal)|raw }}
|
||||
{% else %}
|
||||
{{ journal.correctAmount|formatAmount }}
|
||||
{{ journal.correct_amount|formatAmount }}
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>
|
||||
|
@@ -248,6 +248,24 @@ class TagControllerTest extends TestCase
|
||||
|
||||
$this->call('POST', '/tags/update/' . $tag->id, $data);
|
||||
$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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -329,7 +329,7 @@ class TransactionJournalModelTest extends TestCase
|
||||
// get asset account:
|
||||
$result = $journal->correct_amount;
|
||||
|
||||
$this->assertEquals('0', $result);
|
||||
$this->assertEquals('300', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user