firefly-iii/app/Models/TransactionJournal.php

495 lines
17 KiB
PHP
Raw Normal View History

2015-02-05 21:52:16 -06:00
<?php namespace FireflyIII\Models;
use Carbon\Carbon;
2015-02-06 23:49:24 -06:00
use Crypt;
2015-06-03 11:22:47 -05:00
use FireflyIII\Support\CacheProperties;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
2015-02-22 01:38:46 -06:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
2015-02-09 00:56:24 -06:00
use Watson\Validating\ValidatingTrait;
2015-02-09 00:56:24 -06:00
/**
* Class TransactionJournal
*
* @package FireflyIII\Models
2015-07-15 14:06:26 -05:00
* @property integer $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property \Carbon\Carbon $deleted_at
* @property integer $user_id
* @property integer $transaction_type_id
* @property integer $bill_id
* @property integer $transaction_currency_id
* @property string $description
* @property boolean $completed
* @property \Carbon\Carbon $date
* @property boolean $encrypted
* @property integer $order
* @property integer $tag_count
* @property-read \FireflyIII\Models\Bill $bill
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Budget[] $budgets
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Category[] $categories
* @property-read mixed $actual_amount
* @property-read mixed $amount
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Tag[] $tags
* @property-read mixed $correct_amount
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Transaction[] $transactions
* @property-read mixed $destination_account
* @property-read mixed $source_account
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\PiggyBankEvent[] $piggyBankEvents
* @property-read \FireflyIII\Models\TransactionCurrency $transactionCurrency
* @property-read \FireflyIII\Models\TransactionType $transactionType
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\TransactionGroup[] $transactiongroups
* @property-read \FireflyIII\User $user
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereDeletedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereTransactionTypeId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereBillId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereTransactionCurrencyId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereDescription($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereCompleted($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereDate($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereEncrypted($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereOrder($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereTagCount($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal accountIs($account)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal after($date)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal before($date)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal onDate($date)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal transactionTypes($types)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal withRelevantData()
2015-07-15 14:06:26 -05:00
* @property-read bool $account_encrypted
* @property-read bool $joinedTransactions
* @property-read bool $joinedTransactionTypes
* @property-read int $account_id
2015-07-26 12:13:06 -05:00
* @property string $name
2015-07-15 14:06:26 -05:00
* @property-read string $symbol
2015-07-26 08:51:07 -05:00
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Attachment[] $attachments
2015-09-25 13:40:24 -05:00
* @property-read mixed $amount_positive
2015-02-09 00:56:24 -06:00
*/
2015-02-05 22:04:06 -06:00
class TransactionJournal extends Model
{
2015-02-09 00:56:24 -06:00
use SoftDeletes, ValidatingTrait;
protected $fillable
= ['user_id', 'transaction_type_id', 'bill_id', 'transaction_currency_id', 'description', 'completed', 'date', 'encrypted', 'tag_count'];
protected $hidden = ['encrypted'];
2015-02-09 00:56:24 -06:00
protected $rules
= [
2015-02-09 00:56:24 -06:00
'user_id' => 'required|exists:users,id',
'transaction_type_id' => 'required|exists:transaction_types,id',
'bill_id' => 'exists:bills,id',
'transaction_currency_id' => 'required|exists:transaction_currencies,id',
'description' => 'required|between:1,1024',
'completed' => 'required|boolean',
'date' => 'required|date',
'encrypted' => 'required|boolean'
];
2015-02-05 21:52:16 -06:00
2015-02-11 00:35:10 -06:00
/**
* @codeCoverageIgnore
2015-02-11 00:35:10 -06:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-02-05 22:14:27 -06:00
public function bill()
{
2015-02-05 22:35:00 -06:00
return $this->belongsTo('FireflyIII\Models\Bill');
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @codeCoverageIgnore
2015-02-11 00:35:10 -06:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
2015-02-05 22:14:27 -06:00
public function budgets()
{
2015-02-05 22:35:00 -06:00
return $this->belongsToMany('FireflyIII\Models\Budget');
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @codeCoverageIgnore
2015-02-11 00:35:10 -06:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
2015-02-05 22:14:27 -06:00
public function categories()
{
2015-02-05 22:35:00 -06:00
return $this->belongsToMany('FireflyIII\Models\Category');
2015-02-05 22:14:27 -06:00
}
2015-05-18 10:02:18 -05:00
/**
2015-05-24 04:13:46 -05:00
* @return string
2015-05-18 10:02:18 -05:00
*/
2015-09-25 13:40:24 -05:00
public function getAmountPositiveAttribute()
2015-05-18 10:02:18 -05:00
{
2015-05-24 04:13:46 -05:00
$amount = '0';
2015-05-18 10:02:18 -05:00
/** @var Transaction $t */
foreach ($this->transactions as $t) {
if ($t->amount > 0) {
2015-05-24 04:13:46 -05:00
$amount = $t->amount;
2015-05-18 10:02:18 -05:00
}
}
return $amount;
}
2015-04-07 03:14:10 -05:00
/**
* @return float
*/
public function getAmountAttribute()
{
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties();
$cache->addProperty($this->id);
$cache->addProperty('amount');
if ($cache->has()) {
2015-06-04 14:35:36 -05:00
return $cache->get(); // @codeCoverageIgnore
2015-06-03 11:22:47 -05:00
}
2015-05-24 04:13:46 -05:00
bcscale(2);
2015-09-25 13:40:24 -05:00
$type = $this->transactionType->type;
$transaction = $this->transactions->sortByDesc('amount')->first();
$amount = $transaction->amount;
if ($type == 'Withdrawal') {
$amount = $amount * -1;
2015-05-17 09:12:00 -05:00
}
$cache->store($amount);
2015-05-17 09:12:00 -05:00
return $amount;
2015-06-03 11:22:47 -05:00
}
2015-07-08 23:13:39 -05:00
/**
* @param Tag $tag
* @param $amount
*
* @return string
*/
protected function amountByTagAdvancePayment(Tag $tag, $amount)
{
if ($this->transactionType->type == 'Withdrawal') {
$others = $tag->transactionJournals()->transactionTypes(['Deposit'])->get();
foreach ($others as $other) {
2015-09-25 13:40:24 -05:00
$amount = bcsub($amount, $other->amount_positive);
2015-07-08 23:13:39 -05:00
}
return $amount;
}
if ($this->transactionType->type == 'Deposit') {
return '0';
}
return $amount;
}
/**
* @param $tag
* @param $amount
*
* @return string
*/
protected function amountByTagBalancingAct($tag, $amount)
{
if ($this->transactionType->type == 'Withdrawal') {
$transfer = $tag->transactionJournals()->transactionTypes(['Transfer'])->first();
if ($transfer) {
2015-09-25 13:40:24 -05:00
$amount = bcsub($amount, $transfer->amount_positive);
2015-07-08 23:13:39 -05:00
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') {
2015-07-08 23:13:39 -05:00
return $this->amountByTagAdvancePayment($tag, $amount);
}
2015-05-20 11:09:44 -05:00
if ($tag->tagMode == 'balancingAct') {
2015-07-08 23:13:39 -05:00
return $this->amountByTagBalancingAct($tag, $amount);
2015-05-20 11:09:44 -05:00
}
2015-06-03 11:22:47 -05:00
2015-05-17 09:12:00 -05:00
return $amount;
2015-05-17 09:12:00 -05:00
}
2015-06-15 12:25:54 -05:00
/**
* @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags()
{
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;
}
2015-06-03 11:22:47 -05:00
/**
* @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function transactions()
{
return $this->hasMany('FireflyIII\Models\Transaction');
}
2015-02-11 00:35:10 -06:00
/**
* @codeCoverageIgnore
* @return string[]
2015-02-11 00:35:10 -06:00
*/
public function getDates()
{
2015-02-07 06:15:40 -06:00
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;
2015-06-15 12:25:54 -05:00
return parent::save($options);
}
2015-02-11 00:35:10 -06:00
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05:00
*
2015-02-11 00:35:10 -06:00
* @param $value
*
* @return string
*/
2015-02-05 22:14:27 -06:00
public function getDescriptionAttribute($value)
{
if ($this->encrypted) {
return Crypt::decrypt($value);
}
return $value;
}
/**
* @return Account
*/
public function getDestinationAccountAttribute()
{
2015-06-05 12:02:23 -05:00
$cache = new CacheProperties;
$cache->addProperty($this->id);
$cache->addProperty('destinationAccount');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
2015-06-05 12:02:23 -05:00
$account = $this->transactions()->where('amount', '>', 0)->first()->account;
$cache->store($account);
2015-06-05 12:02:23 -05:00
return $account;
}
2015-06-05 12:02:23 -05:00
/**
* @return Account
*/
public function getSourceAccountAttribute()
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
$cache->addProperty('sourceAccount');
2015-06-05 12:02:23 -05:00
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$account = $this->transactions()->where('amount', '<', 0)->first()->account;
2015-06-06 08:36:12 -05:00
2015-06-05 12:02:23 -05:00
$cache->store($account);
return $account;
}
2015-02-11 00:35:10 -06:00
/**
* @codeCoverageIgnore
2015-02-11 00:35:10 -06:00
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-02-05 22:14:27 -06:00
public function piggyBankEvents()
{
2015-02-05 22:35:00 -06:00
return $this->hasMany('FireflyIII\Models\PiggyBankEvent');
2015-02-05 22:14:27 -06:00
}
2015-02-22 08:40:13 -06:00
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05:00
*
2015-02-22 08:40:13 -06:00
* @param EloquentBuilder $query
* @param Account $account
*/
public function scopeAccountIs(EloquentBuilder $query, Account $account)
{
if (!isset($this->joinedTransactions)) {
$query->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id');
$this->joinedTransactions = true;
}
$query->where('transactions.account_id', $account->id);
}
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05:00
*
* @param EloquentBuilder $query
* @param Carbon $date
*
* @return EloquentBuilder
*/
public function scopeAfter(EloquentBuilder $query, Carbon $date)
{
return $query->where('transaction_journals.date', '>=', $date->format('Y-m-d 00:00:00'));
}
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05:00
*
* @param EloquentBuilder $query
* @param Carbon $date
*
* @return EloquentBuilder
*/
public function scopeBefore(EloquentBuilder $query, Carbon $date)
{
return $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00'));
}
2015-02-22 08:40:13 -06:00
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05:00
*
2015-02-22 08:40:13 -06:00
* @param EloquentBuilder $query
* @param Carbon $date
*
* @return EloquentBuilder
2015-02-22 08:40:13 -06:00
*/
public function scopeOnDate(EloquentBuilder $query, Carbon $date)
{
return $query->where('date', '=', $date->format('Y-m-d'));
}
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05:00
*
* @param EloquentBuilder $query
* @param array $types
*/
public function scopeTransactionTypes(EloquentBuilder $query, array $types)
{
if (is_null($this->joinedTransactionTypes)) {
$query->leftJoin(
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
);
$this->joinedTransactionTypes = true;
}
$query->whereIn('transaction_types.type', $types);
}
2015-02-22 08:40:13 -06:00
/**
* @codeCoverageIgnore
2015-02-22 08:40:13 -06:00
* Automatically includes the 'with' parameters to get relevant related
* objects.
*
* @param EloquentBuilder $query
*/
public function scopeWithRelevantData(EloquentBuilder $query)
{
$query->with(
2015-06-05 12:02:23 -05:00
['transactions' => function (HasMany $q) {
2015-02-22 08:40:13 -06:00
$q->orderBy('amount', 'ASC');
}, 'transactionType', 'transactionCurrency', 'budgets', 'categories', 'transactions.account.accounttype', 'bill']
2015-02-22 08:40:13 -06:00
);
}
2015-02-11 00:35:10 -06:00
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05:00
*
2015-02-11 00:35:10 -06:00
* @param $value
*/
2015-02-05 22:14:27 -06:00
public function setDescriptionAttribute($value)
{
2015-05-08 07:00:49 -05:00
$this->attributes['description'] = Crypt::encrypt($value);
2015-02-05 22:14:27 -06:00
$this->attributes['encrypted'] = true;
}
2015-07-18 02:49:19 -05:00
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function attachments()
{
return $this->morphMany('FireflyIII\Models\Attachment', 'attachable');
2015-07-18 02:49:19 -05:00
}
2015-02-11 00:35:10 -06:00
/**
* @codeCoverageIgnore
2015-02-11 00:35:10 -06:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-02-05 22:14:27 -06:00
public function transactionCurrency()
{
2015-02-05 22:35:00 -06:00
return $this->belongsTo('FireflyIII\Models\TransactionCurrency');
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @codeCoverageIgnore
2015-02-11 00:35:10 -06:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-02-05 22:14:27 -06:00
public function transactionType()
{
2015-02-05 22:35:00 -06:00
return $this->belongsTo('FireflyIII\Models\TransactionType');
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @codeCoverageIgnore
2015-02-11 00:35:10 -06:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
2015-02-05 22:14:27 -06:00
public function transactiongroups()
{
2015-02-05 22:35:00 -06:00
return $this->belongsToMany('FireflyIII\Models\TransactionGroup');
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @codeCoverageIgnore
2015-02-11 00:35:10 -06:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-02-05 22:14:27 -06:00
public function user()
{
2015-02-05 22:35:00 -06:00
return $this->belongsTo('FireflyIII\User');
2015-02-05 22:14:27 -06:00
}
2015-02-05 21:52:16 -06:00
}