firefly-iii/app/Models/TransactionJournal.php

491 lines
18 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
* @SuppressWarnings (PHPMD.TooManyMethods)
2015-05-26 05:19:11 -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-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 $asset_account
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Transaction[] $transactions
* @property-read mixed $corrected_actual_amount
* @property-read mixed $destination_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 \FireflyIII\Models\TransactionJournal accountIs($account)
* @method static \FireflyIII\Models\TransactionJournal after($date)
* @method static \FireflyIII\Models\TransactionJournal before($date)
* @method static \FireflyIII\Models\TransactionJournal onDate($date)
* @method static \FireflyIII\Models\TransactionJournal transactionTypes($types)
* @method static \FireflyIII\Models\TransactionJournal withRelevantData()
2015-06-02 15:07:38 -05:00
* @property-read mixed $expense_account
* @property string account_encrypted
* @property bool joinedTransactions
* @property bool joinedTransactionTypes
* @property mixed account_id
* @property mixed name
* @property mixed symbol
2015-06-05 12:02:23 -05:00
* @property-read mixed $correct_amount
* @method static \FireflyIII\Models\TransactionJournal orderBy
* @method static \FireflyIII\Models\TransactionJournal|null first
2015-06-06 08:36:12 -05:00
* @property-read mixed $source_account
* @property integer $tag_count
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereTagCount($value)
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
*/
public function getActualAmountAttribute()
{
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
$amount = '0';
bcscale(2);
2015-04-07 03:14:10 -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-04-07 03:14:10 -05:00
}
}
2015-05-03 05:58:55 -05:00
2015-06-29 00:59:06 -05:00
if (intval($this->tag_count) === 1) {
// get amount for single tag:
$amount = $this->amountByTag($this->tags()->first(), $amount);
2015-05-17 09:12:00 -05:00
}
2015-05-24 04:13:46 -05:00
2015-06-29 00:59:06 -05:00
if (intval($this->tag_count) > 1) {
// get amount for either tag.
$amount = $this->amountByTags($amount);
2015-05-18 10:02:18 -05:00
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
}
/**
* 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);
}
2015-05-20 11:09:44 -05:00
return $amount;
}
if ($this->transactionType->type == 'Deposit') {
return '0';
}
}
2015-05-20 11:09:44 -05:00
if ($tag->tagMode == 'balancingAct') {
2015-05-20 11:09:44 -05:00
if ($this->transactionType->type == 'Withdrawal') {
$transfer = $tag->transactionJournals()->transactionTypes(['Transfer'])->first();
2015-05-20 11:09:44 -05:00
if ($transfer) {
2015-05-27 00:27:05 -05:00
$amount = bcsub($amount, $transfer->actual_amount);
2015-05-20 11:09:44 -05:00
return $amount;
}
}
}
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-05-03 05:58:55 -05:00
/**
2015-06-06 08:36:12 -05:00
* @return string
2015-05-03 05:58:55 -05:00
*/
2015-06-06 08:36:12 -05:00
public function getCorrectAmountAttribute()
2015-04-28 10:10:52 -05:00
{
2015-05-26 05:19:11 -05:00
switch ($this->transactionType->type) {
case 'Deposit':
2015-06-06 08:36:12 -05:00
return $this->transactions()->where('amount', '>', 0)->first()->amount;
2015-05-26 05:19:11 -05:00
case 'Withdrawal':
2015-06-06 08:36:12 -05:00
return $this->transactions()->where('amount', '<', 0)->first()->amount;
2015-04-28 10:10:52 -05:00
}
return $this->transactions()->where('amount', '>', 0)->first()->amount;
2015-04-28 10:10:52 -05:00
}
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-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
}