1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Models/Expense.php
2016-01-06 20:52:09 +01:00

80 lines
1.5 KiB
PHP

<?php namespace App\Models;
use Utils;
use DB;
use Carbon;
use Laracasts\Presenter\PresentableTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Events\ExpenseWasCreated;
class Expense extends EntityModel
{
use SoftDeletes;
use PresentableTrait;
protected $dates = ['deleted_at'];
protected $presenter = 'App\Ninja\Presenters\ExpensePresenter';
protected $fillable = [
'vendor_id',
];
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();
}
public function getName()
{
return '';
}
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));
});