firefly-iii/app/models/TransactionJournal.php

244 lines
6.2 KiB
PHP
Raw Normal View History

<?php
use Carbon\Carbon;
2014-12-06 05:12:55 -06:00
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Watson\Validating\ValidatingTrait;
2014-12-20 08:00:53 -06:00
use \Illuminate\Database\Eloquent\Model as Eloquent;
2015-01-02 01:59:16 -06:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2014-12-13 14:59:02 -06:00
/**
* Class TransactionJournal
*/
2014-12-06 05:12:55 -06:00
class TransactionJournal extends Eloquent
{
2014-12-06 05:12:55 -06:00
use SoftDeletingTrait, ValidatingTrait;
2014-12-31 09:45:12 -06:00
protected $rules
2014-11-18 02:47:50 -06:00
= ['transaction_type_id' => 'required|exists:transaction_types,id',
'transaction_currency_id' => 'required|exists:transaction_currencies,id',
'description' => 'required|between:1,255',
'date' => 'required|date',
'completed' => 'required|between:0,1'];
2014-12-14 13:40:02 -06:00
protected $fillable
= ['transaction_type_id', 'transaction_currency_id', 'user_id',
'description', 'date', 'completed'];
2014-08-10 11:22:42 -05:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
2014-08-10 11:22:42 -05:00
*/
public function budgets()
{
return $this->belongsToMany(
2015-01-01 12:35:10 -06:00
'Budget'
);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function categories()
{
return $this->belongsToMany(
2015-01-01 12:35:10 -06:00
'Category'
);
}
2014-11-02 11:46:01 -06:00
/**
2014-11-17 13:45:55 -06:00
* TODO remove this method in favour of something in the FireflyIII libraries.
2014-11-18 02:47:50 -06:00
*
2014-12-13 14:59:02 -06:00
* @param Account $account
*
2014-11-02 11:46:01 -06:00
* @return float
*/
2014-12-07 08:37:53 -06:00
public function getAmount(\Account $account = null)
2014-11-02 11:46:01 -06:00
{
2014-11-08 03:16:12 -06:00
2014-11-02 11:46:01 -06:00
foreach ($this->transactions as $t) {
2014-12-07 08:37:53 -06:00
if (!is_null($account) && $account->id == $t->account_id) {
return floatval($t->amount);
}
2014-11-02 11:46:01 -06:00
if (floatval($t->amount) > 0) {
return floatval($t->amount);
}
}
2014-11-12 15:37:44 -06:00
2014-11-08 03:16:12 -06:00
return -0.01;
2014-11-02 11:46:01 -06:00
}
2014-09-08 03:38:26 -05:00
/**
2014-11-12 15:37:44 -06:00
* @return array
2014-09-08 03:38:26 -05:00
*/
2014-11-12 15:37:44 -06:00
public function getDates()
2014-09-08 03:38:26 -05:00
{
2014-11-12 15:37:44 -06:00
return ['created_at', 'updated_at', 'date'];
2014-09-08 03:38:26 -05:00
}
2014-11-17 00:33:18 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function piggyBankEvents()
2014-11-17 00:33:18 -06:00
{
return $this->hasMany('PiggyBankEvent');
2014-11-17 00:33:18 -06:00
}
2014-08-10 11:22:42 -05:00
/**
2014-11-12 15:37:44 -06:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
2014-08-10 11:22:42 -05:00
*/
2014-12-29 13:28:17 -06:00
public function bill()
{
2014-12-29 13:28:17 -06:00
return $this->belongsTo('Bill');
}
/**
* @param Builder $query
* @param Account $account
*/
2015-01-02 01:59:16 -06:00
public function scopeAccountIs($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);
}
2014-08-10 11:22:42 -05:00
/**
* @param $query
2014-11-02 11:46:01 -06:00
* @param Carbon $date
*
* @return mixed
2014-08-10 11:22:42 -05:00
*/
public function scopeAfter($query, Carbon $date)
{
2014-12-28 01:54:53 -06:00
return $query->where('transaction_journals.date', '>=', $date->format('Y-m-d 00:00:00'));
}
2014-08-10 11:22:42 -05:00
/**
* @param $query
2014-11-02 11:46:01 -06:00
* @param Carbon $date
*
* @return mixed
2014-08-10 11:22:42 -05:00
*/
public function scopeBefore($query, Carbon $date)
{
2014-12-28 01:54:53 -06:00
return $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00'));
}
2014-12-13 14:59:02 -06:00
/**
* @param Builder $query
* @param $amount
*/
2015-01-02 01:59:16 -06:00
public function scopeLessThan($query, $amount)
{
if (is_null($this->joinedTransactions)) {
2014-11-02 11:46:01 -06:00
$query->leftJoin(
2014-11-12 15:37:44 -06:00
'transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id'
2014-11-02 11:46:01 -06:00
);
$this->joinedTransactions = true;
}
2014-11-12 15:37:44 -06:00
$query->where('transactions.amount', '<=', $amount);
}
2014-12-13 14:59:02 -06:00
/**
* @param Builder $query
* @param $amount
*/
2015-01-02 01:59:16 -06:00
public function scopeMoreThan($query, $amount)
{
if (is_null($this->joinedTransactions)) {
2014-11-02 11:46:01 -06:00
$query->leftJoin(
2014-11-12 15:37:44 -06:00
'transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id'
2014-11-02 11:46:01 -06:00
);
$this->joinedTransactions = true;
}
2014-11-12 15:37:44 -06:00
$query->where('transactions.amount', '>=', $amount);
}
/**
* @param $query
* @param Carbon $date
*
* @return mixed
*/
public function scopeOnDate($query, Carbon $date)
{
return $query->where('date', '=', $date->format('Y-m-d'));
}
2014-12-13 14:59:02 -06:00
/**
* @param Builder $query
* @param array $types
*/
2015-01-02 01:59:16 -06:00
public function scopeTransactionTypes($query, array $types)
{
if (is_null($this->joinedTransactionTypes)) {
2014-11-02 11:46:01 -06:00
$query->leftJoin(
2014-11-12 15:37:44 -06:00
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
2014-11-02 11:46:01 -06:00
);
$this->joinedTransactionTypes = true;
}
$query->whereIn('transaction_types.type', $types);
}
/**
* Automatically includes the 'with' parameters to get relevant related
* objects.
*
* @param $query
*/
2015-01-02 01:59:16 -06:00
public function scopeWithRelevantData($query)
{
$query->with(
2015-01-02 01:59:16 -06:00
['transactions' => function (HasMany $q) {
2014-11-02 11:46:01 -06:00
$q->orderBy('amount', 'ASC');
2014-12-29 13:28:17 -06:00
}, 'transactiontype', 'budgets','categories', 'transactions.account.accounttype', 'bill', 'budgets', 'categories']
);
}
2014-08-10 11:22:42 -05:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
2014-08-10 11:22:42 -05:00
*/
public function transactionCurrency()
{
return $this->belongsTo('TransactionCurrency');
}
2014-08-10 11:22:42 -05:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
2014-08-10 11:22:42 -05:00
*/
public function transactionType()
{
return $this->belongsTo('TransactionType');
}
2014-12-13 14:59:02 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
2014-12-06 05:12:55 -06:00
public function transactiongroups()
{
return $this->belongsToMany('TransactionGroup');
}
2014-08-10 11:22:42 -05:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
2014-08-10 11:22:42 -05:00
*/
public function transactions()
{
return $this->hasMany('Transaction');
2014-07-09 05:56:06 -05:00
}
2014-08-10 11:22:42 -05:00
/**
* User
2014-08-10 11:22:42 -05:00
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
2014-08-10 11:22:42 -05:00
*/
public function user()
{
return $this->belongsTo('User');
2014-07-09 05:56:06 -05:00
}
2015-01-01 23:16:49 -06:00
}