firefly-iii/app/Models/TransactionJournal.php

493 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-05 12:02:23 -05:00
* @property-read mixed $source_account
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'];
2015-05-23 10:11:16 -05:00
protected $hidden = ['encrypted'];
2015-02-09 00:56:24 -06:00
protected $rules
2015-05-23 10:11:16 -05:00
= [
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-05-17 09:12:00 -05:00
/*
* If the journal has tags, it gets complicated.
*/
if ($this->tags->count() == 0) {
2015-06-03 14:25:11 -05:00
$cache->store($amount);
2015-06-03 11:22:47 -05:00
2015-05-17 09:12:00 -05:00
return $amount;
}
2015-05-24 04:13:46 -05:00
2015-05-17 09:12:00 -05:00
// 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:
2015-05-20 11:09:44 -05:00
/** @var Tag $advancePayment */
$advancePayment = $this->tags()->where('tagMode', 'advancePayment')->first();
if ($advancePayment && $this->transactionType->type == 'Withdrawal') {
2015-05-17 09:12:00 -05:00
// loop other deposits, remove from our amount.
2015-05-20 11:09:44 -05:00
$others = $advancePayment->transactionJournals()->transactionTypes(['Deposit'])->get();
2015-05-17 09:12:00 -05:00
foreach ($others as $other) {
2015-05-27 00:27:05 -05:00
$amount = bcsub($amount, $other->actual_amount);
2015-05-17 09:12:00 -05:00
}
2015-06-03 14:25:11 -05:00
$cache->store($amount);
2015-05-18 10:02:18 -05:00
2015-05-17 09:12:00 -05:00
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:
2015-05-20 11:09:44 -05:00
if ($advancePayment && $this->transactionType->type == 'Deposit') {
2015-06-03 14:25:11 -05:00
$cache->store('0');
2015-06-03 11:22:47 -05:00
2015-05-24 04:13:46 -05:00
return '0';
}
2015-05-20 11:09:44 -05:00
// is balancing act?
$balancingAct = $this->tags()->where('tagMode', 'balancingAct')->first();
2015-05-24 04:13:46 -05:00
if ($balancingAct) {
2015-05-20 11:09:44 -05:00
// this is the expense:
if ($this->transactionType->type == 'Withdrawal') {
$transfer = $balancingAct->transactionJournals()->transactionTypes(['Transfer'])->first();
if ($transfer) {
2015-05-27 00:27:05 -05:00
$amount = bcsub($amount, $transfer->actual_amount);
2015-06-03 14:25:11 -05:00
$cache->store($amount);
2015-05-20 11:09:44 -05:00
return $amount;
}
2015-05-24 04:13:46 -05:00
} // @codeCoverageIgnore
} // @codeCoverageIgnore
2015-05-20 11:09:44 -05:00
2015-06-03 14:25:11 -05:00
$cache->store($amount);
2015-06-03 11:22:47 -05:00
2015-05-17 09:12:00 -05:00
return $amount;
}
/**
* @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags()
{
return $this->belongsToMany('FireflyIII\Models\Tag');
2015-04-07 03:14:10 -05:00
}
2015-05-03 05:58:55 -05:00
/**
2015-05-17 05:49:09 -05:00
* @return Account
2015-05-03 05:58:55 -05:00
*/
2015-04-28 10:10:52 -05:00
public function getAssetAccountAttribute()
{
2015-05-26 05:19:11 -05:00
// if it's a deposit, it's the one thats positive
// if it's a withdrawal, it's the one thats negative
// otherwise, it's either (return first one):
switch ($this->transactionType->type) {
case 'Deposit':
return $this->transactions()->where('amount', '>', 0)->first()->account;
case 'Withdrawal':
return $this->transactions()->where('amount', '<', 0)->first()->account;
2015-04-28 10:10:52 -05:00
}
2015-05-17 05:49:09 -05:00
return $this->transactions()->first()->account;
2015-05-26 05:19:11 -05:00
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-06-02 15:07:38 -05:00
/**
* @return string
*/
public function getCorrectAmountAttribute()
{
switch ($this->transactionType->type) {
case 'Deposit':
return $this->transactions()->where('amount', '>', 0)->first()->amount;
case 'Withdrawal':
return $this->transactions()->where('amount', '<', 0)->first()->amount;
}
return '0';
}
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'];
}
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-05-26 05:19:11 -05:00
/**
* @return Account
*/
public function getExpenseAccountAttribute()
{
// if it's a deposit, it's the one thats negative
// if it's a withdrawal, it's the one thats positive
// otherwise, it's either (return first one):
switch ($this->transactionType->type) {
case 'Deposit':
return $this->transactions()->where('amount', '<', 0)->first()->account;
case 'Withdrawal':
return $this->transactions()->where('amount', '>', 0)->first()->account;
}
return $this->transactions()->first()->account;
}
2015-06-05 12:02:23 -05:00
/**
* @return Account
*/
public function getSourceAccountAttribute()
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
$cache->addProperty('destinationAccount');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$account = $this->transactions()->where('amount', '<', 0)->first()->account;
$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', 'budgets', 'categories']
);
}
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
}