1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 12:12:48 +01:00
invoiceninja/app/Models/Credit.php
2016-06-08 08:11:05 +03:00

68 lines
1.3 KiB
PHP

<?php namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Events\CreditWasCreated;
use Laracasts\Presenter\PresentableTrait;
class Credit extends EntityModel
{
use SoftDeletes;
use PresentableTrait;
protected $dates = ['deleted_at'];
protected $presenter = 'App\Ninja\Presenters\CreditPresenter';
public function account()
{
return $this->belongsTo('App\Models\Account');
}
public function user()
{
return $this->belongsTo('App\Models\User')->withTrashed();
}
public function invoice()
{
return $this->belongsTo('App\Models\Invoice')->withTrashed();
}
public function client()
{
return $this->belongsTo('App\Models\Client')->withTrashed();
}
public function getName()
{
return '';
}
public function getEntityType()
{
return ENTITY_CREDIT;
}
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;
}
}
Credit::creating(function ($credit) {
});
Credit::created(function ($credit) {
event(new CreditWasCreated($credit));
});