Add tag count to journal for easier amount calculations [skip ci]

This commit is contained in:
James Cole 2015-06-14 11:52:07 +02:00
parent e36717259b
commit 1e86794416
3 changed files with 97 additions and 23 deletions

View File

@ -96,6 +96,32 @@ class Tag extends Model
return ['created_at', 'updated_at', 'date'];
}
/**
* Save the model to the database.
*
* @param array $options
*
* @return bool
*/
public function save(array $options = [])
{
foreach ($this->transactionjournals()->get() as $journal) {
$count = $journal->tags()->count();
$journal->tag_count = $count;
$journal->save();
}
parent::save($options);
}
/**
* @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function transactionjournals()
{
return $this->belongsToMany('FireflyIII\Models\TransactionJournal');
}
/**
* @codeCoverageIgnore
*
@ -140,15 +166,6 @@ class Tag extends Model
$this->attributes['tag'] = Crypt::encrypt($value);
}
/**
* @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function transactionjournals()
{
return $this->belongsToMany('FireflyIII\Models\TransactionJournal');
}
/**
* @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo

View File

@ -72,15 +72,19 @@ use Watson\Validating\ValidatingTrait;
* @method static \FireflyIII\Models\TransactionJournal orderBy
* @method static \FireflyIII\Models\TransactionJournal|null first
* @property-read mixed $source_account
* @property integer $tag_count
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereTagCount($value)
*/
class TransactionJournal extends Model
{
use SoftDeletes, ValidatingTrait;
protected $fillable = ['user_id', 'transaction_type_id', 'bill_id', 'transaction_currency_id', 'description', 'completed', 'date', 'encrypted'];
protected $hidden = ['encrypted'];
protected $fillable
= ['user_id', 'transaction_type_id', 'bill_id', 'transaction_currency_id', 'description', 'completed', 'date', 'encrypted', 'tag_count'];
protected $hidden = ['encrypted'];
protected $rules
= [
= [
'user_id' => 'required|exists:users,id',
'transaction_type_id' => 'required|exists:transaction_types,id',
'bill_id' => 'exists:bills,id',
@ -154,7 +158,7 @@ class TransactionJournal extends Model
$amount = $t->amount;
}
}
$count = $this->tags->count();
$count = $this->tags()->count();
if ($count === 1) {
// get amount for single tag:
@ -211,15 +215,6 @@ class TransactionJournal extends Model
}
/**
* @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags()
{
return $this->belongsToMany('FireflyIII\Models\Tag');
}
/**
* @param string $amount
*
@ -274,6 +269,29 @@ class TransactionJournal extends Model
return ['created_at', 'updated_at', 'date', 'deleted_at'];
}
/**
* Save the model to the database.
*
* @param array $options
*
* @return bool
*/
public function save(array $options = [])
{
$count = $this->tags()->count();
$this->tag_count = $count;
parent::save($options);
}
/**
* @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags()
{
return $this->belongsToMany('FireflyIII\Models\Tag');
}
/**
* @codeCoverageIgnore
*
@ -418,7 +436,7 @@ class TransactionJournal extends Model
$query->with(
['transactions' => function (HasMany $q) {
$q->orderBy('amount', 'ASC');
}, 'transactiontype', 'transactioncurrency', 'budgets', 'categories', 'transactions.account.accounttype', 'bill', 'budgets', 'categories']
}, 'transactionType', 'transactionCurrency', 'budgets', 'categories', 'transactions.account.accounttype', 'bill']
);
}

View File

@ -0,0 +1,39 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
/**
* Class ChangesForV345
*/
class ChangesForV345 extends Migration
{
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table(
'transaction_journals', function (Blueprint $table) {
$table->dropColumn('tag_count');
}
);
}
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
Schema::table(
'transaction_journals', function (Blueprint $table) {
$table->smallInteger('tag_count', false, true)->default(0);
}
);
}
}