2016-01-06 20:52:09 +01:00
|
|
|
<?php namespace App\Models;
|
|
|
|
|
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
use App\Events\ExpenseWasCreated;
|
2016-01-08 19:01:00 +01:00
|
|
|
use App\Events\ExpenseWasUpdated;
|
2016-01-15 11:31:12 +01:00
|
|
|
use App\Events\ExpenseWasDeleted;
|
2016-01-06 20:52:09 +01:00
|
|
|
|
|
|
|
class Expense extends EntityModel
|
|
|
|
{
|
2016-01-07 16:14:11 +01:00
|
|
|
// Expenses
|
2016-01-06 20:52:09 +01:00
|
|
|
use SoftDeletes;
|
|
|
|
use PresentableTrait;
|
2016-01-15 11:31:12 +01:00
|
|
|
|
2016-01-19 20:35:15 +01:00
|
|
|
protected $dates = ['deleted_at','expense_date'];
|
2016-01-06 20:52:09 +01:00
|
|
|
protected $presenter = 'App\Ninja\Presenters\ExpensePresenter';
|
|
|
|
|
2016-01-08 19:01:00 +01:00
|
|
|
protected $fillable = [
|
2016-01-21 21:36:49 +01:00
|
|
|
'client_id',
|
|
|
|
'vendor_id',
|
|
|
|
'currency_id',
|
2016-01-08 19:01:00 +01:00
|
|
|
'amount',
|
2016-01-20 23:09:10 +01:00
|
|
|
'foreign_amount',
|
2016-01-08 19:01:00 +01:00
|
|
|
'exchange_rate',
|
|
|
|
'private_notes',
|
|
|
|
'public_notes',
|
2016-01-26 21:22:33 +01:00
|
|
|
'bank_id',
|
|
|
|
'transaction_id',
|
2016-01-15 11:31:12 +01:00
|
|
|
];
|
2016-01-06 20:52:09 +01:00
|
|
|
public function account()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Account');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\User');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function vendor()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Vendor')->withTrashed();
|
|
|
|
}
|
|
|
|
|
2016-01-21 20:15:30 +01:00
|
|
|
public function client()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Client')->withTrashed();
|
|
|
|
}
|
|
|
|
|
2016-01-21 23:29:10 +01:00
|
|
|
public function invoice()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Invoice')->withTrashed();
|
|
|
|
}
|
|
|
|
|
2016-01-06 20:52:09 +01:00
|
|
|
public function getName()
|
|
|
|
{
|
2016-01-08 19:01:00 +01:00
|
|
|
if($this->expense_number)
|
|
|
|
return $this->expense_number;
|
2016-01-15 11:31:12 +01:00
|
|
|
|
2016-01-08 19:01:00 +01:00
|
|
|
return $this->public_id;
|
|
|
|
}
|
2016-01-15 11:31:12 +01:00
|
|
|
|
2016-01-31 14:10:33 +01:00
|
|
|
public function getCurrencyId()
|
|
|
|
{
|
|
|
|
return $this->client ? $this->client->currency_id : $this->currency_id;
|
|
|
|
}
|
|
|
|
|
2016-01-08 19:01:00 +01:00
|
|
|
public function getDisplayName()
|
|
|
|
{
|
|
|
|
return $this->getName();
|
2016-01-06 20:52:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-08 19:01:00 +01:00
|
|
|
public function getRoute()
|
|
|
|
{
|
|
|
|
return "/expenses/{$this->public_id}";
|
|
|
|
}
|
2016-01-15 11:31:12 +01:00
|
|
|
|
2016-01-06 20:52:09 +01:00
|
|
|
public function getEntityType()
|
|
|
|
{
|
|
|
|
return ENTITY_EXPENSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function apply($amount)
|
|
|
|
{
|
|
|
|
if ($amount > $this->balance) {
|
|
|
|
$applied = $this->balance;
|
|
|
|
$this->balance = 0;
|
|
|
|
} else {
|
|
|
|
$applied = $amount;
|
|
|
|
$this->balance = $this->balance - $amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->save();
|
|
|
|
|
|
|
|
return $applied;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Expense::creating(function ($expense) {
|
|
|
|
$expense->setNullValues();
|
|
|
|
});
|
|
|
|
|
|
|
|
Expense::created(function ($expense) {
|
|
|
|
event(new ExpenseWasCreated($expense));
|
|
|
|
});
|
|
|
|
|
|
|
|
Expense::updating(function ($expense) {
|
|
|
|
$expense->setNullValues();
|
|
|
|
});
|
|
|
|
|
|
|
|
Expense::updated(function ($expense) {
|
|
|
|
event(new ExpenseWasUpdated($expense));
|
|
|
|
});
|
|
|
|
|
2016-01-15 11:31:12 +01:00
|
|
|
Expense::deleting(function ($expense) {
|
|
|
|
$expense->setNullValues();
|
|
|
|
});
|
2016-01-06 20:52:09 +01:00
|
|
|
|
2016-01-15 11:31:12 +01:00
|
|
|
Expense::deleted(function ($expense) {
|
|
|
|
event(new ExpenseWasDeleted($expense));
|
|
|
|
});
|