2014-06-29 15:12:33 -05:00
|
|
|
<?php
|
|
|
|
|
2014-08-30 07:27:05 -05:00
|
|
|
use Carbon\Carbon;
|
2015-01-02 02:06:44 -06:00
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
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;
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
|
|
|
* Class Transaction
|
|
|
|
*/
|
2014-12-06 05:12:55 -06:00
|
|
|
class Transaction extends Eloquent
|
2014-06-29 15:12:33 -05:00
|
|
|
{
|
2014-12-06 05:12:55 -06:00
|
|
|
use SoftDeletingTrait, ValidatingTrait;
|
2014-07-03 14:31:32 -05:00
|
|
|
public static $rules
|
2014-11-18 02:47:50 -06:00
|
|
|
= ['account_id' => 'numeric|required|exists:accounts,id',
|
|
|
|
'transaction_journal_id' => 'numeric|required|exists:transaction_journals,id',
|
|
|
|
'description' => 'between:1,255',
|
2014-11-12 15:37:44 -06:00
|
|
|
'amount' => 'required|between:-65536,65536|not_in:0,0.00',];
|
2014-07-03 14:31:32 -05:00
|
|
|
|
2014-06-29 15:12:33 -05:00
|
|
|
|
2014-08-10 11:22:42 -05:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2014-06-29 15:12:33 -05:00
|
|
|
public function account()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('Account');
|
|
|
|
}
|
|
|
|
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
2015-01-02 02:06:44 -06:00
|
|
|
* @param EloquentBuilder $query
|
2014-12-13 14:59:02 -06:00
|
|
|
* @param Account $account
|
|
|
|
*/
|
2015-01-02 02:06:44 -06:00
|
|
|
public function scopeAccountIs(EloquentBuilder $query, Account $account)
|
2014-08-30 07:27:05 -05:00
|
|
|
{
|
|
|
|
$query->where('transactions.account_id', $account->id);
|
|
|
|
}
|
|
|
|
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
2015-01-02 02:06:44 -06:00
|
|
|
* @param EloquentBuilder $query
|
2014-12-13 14:59:02 -06:00
|
|
|
* @param Carbon $date
|
|
|
|
*/
|
2015-01-02 02:06:44 -06:00
|
|
|
public function scopeAfter(EloquentBuilder $query, Carbon $date)
|
2014-08-30 07:27:05 -05:00
|
|
|
{
|
|
|
|
if (is_null($this->joinedJournals)) {
|
2014-09-21 01:25:30 -05:00
|
|
|
$query->leftJoin(
|
2014-11-12 15:37:44 -06:00
|
|
|
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
2014-09-21 01:25:30 -05:00
|
|
|
);
|
2014-08-30 07:27:05 -05:00
|
|
|
$this->joinedJournals = true;
|
|
|
|
}
|
|
|
|
$query->where('transaction_journals.date', '>=', $date->format('Y-m-d'));
|
|
|
|
}
|
|
|
|
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
2015-01-02 02:06:44 -06:00
|
|
|
* @param EloquentBuilder $query
|
2014-12-13 14:59:02 -06:00
|
|
|
* @param Carbon $date
|
|
|
|
*/
|
2015-01-02 02:06:44 -06:00
|
|
|
public function scopeBefore(EloquentBuilder $query, Carbon $date)
|
2014-08-30 07:27:05 -05:00
|
|
|
{
|
|
|
|
if (is_null($this->joinedJournals)) {
|
2014-09-21 01:25:30 -05:00
|
|
|
$query->leftJoin(
|
2014-11-12 15:37:44 -06:00
|
|
|
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
2014-09-21 01:25:30 -05:00
|
|
|
);
|
2014-08-30 07:27:05 -05:00
|
|
|
$this->joinedJournals = true;
|
|
|
|
}
|
|
|
|
$query->where('transaction_journals.date', '<=', $date->format('Y-m-d'));
|
|
|
|
}
|
|
|
|
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
2015-01-02 02:06:44 -06:00
|
|
|
* @param EloquentBuilder $query
|
2014-12-13 14:59:02 -06:00
|
|
|
* @param $amount
|
|
|
|
*/
|
2015-01-02 02:06:44 -06:00
|
|
|
public function scopeLessThan(EloquentBuilder $query, $amount)
|
2014-08-30 07:27:05 -05:00
|
|
|
{
|
|
|
|
$query->where('amount', '<', $amount);
|
|
|
|
}
|
|
|
|
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
2015-01-02 02:06:44 -06:00
|
|
|
* @param EloquentBuilder $query
|
2014-12-13 14:59:02 -06:00
|
|
|
* @param $amount
|
|
|
|
*/
|
2015-01-02 02:06:44 -06:00
|
|
|
public function scopeMoreThan(EloquentBuilder $query, $amount)
|
2014-08-30 07:27:05 -05:00
|
|
|
{
|
|
|
|
$query->where('amount', '>', $amount);
|
|
|
|
}
|
|
|
|
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
2015-01-02 02:06:44 -06:00
|
|
|
* @param EloquentBuilder $query
|
2014-12-13 14:59:02 -06:00
|
|
|
* @param array $types
|
|
|
|
*/
|
2015-01-02 02:06:44 -06:00
|
|
|
public function scopeTransactionTypes(EloquentBuilder $query, array $types)
|
2014-08-30 07:27:05 -05:00
|
|
|
{
|
|
|
|
if (is_null($this->joinedJournals)) {
|
2014-09-21 01:25:30 -05:00
|
|
|
$query->leftJoin(
|
2014-11-12 15:37:44 -06:00
|
|
|
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
2014-09-21 01:25:30 -05:00
|
|
|
);
|
2014-08-30 07:27:05 -05:00
|
|
|
$this->joinedJournals = true;
|
|
|
|
}
|
|
|
|
if (is_null($this->joinedTransactionTypes)) {
|
2014-09-21 01:25:30 -05:00
|
|
|
$query->leftJoin(
|
2014-11-12 15:37:44 -06:00
|
|
|
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
|
2014-09-21 01:25:30 -05:00
|
|
|
);
|
2014-08-30 07:27:05 -05:00
|
|
|
$this->joinedTransactionTypes = true;
|
|
|
|
}
|
|
|
|
$query->whereIn('transaction_types.type', $types);
|
|
|
|
}
|
|
|
|
|
2014-08-10 11:22:42 -05:00
|
|
|
/**
|
2014-08-16 05:13:50 -05:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2014-08-10 11:22:42 -05:00
|
|
|
*/
|
2014-08-16 05:13:50 -05:00
|
|
|
public function transactionJournal()
|
2014-07-05 12:44:26 -05:00
|
|
|
{
|
2014-08-16 05:13:50 -05:00
|
|
|
return $this->belongsTo('TransactionJournal');
|
2014-07-05 12:44:26 -05:00
|
|
|
}
|
2015-01-01 23:16:49 -06:00
|
|
|
}
|