2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
use App\Events\CreditWasCreated;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2015-11-12 21:36:28 +01:00
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
2015-03-31 11:38:24 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class Credit.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
class Credit extends EntityModel
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
use SoftDeletes;
|
2015-11-12 21:36:28 +01:00
|
|
|
use PresentableTrait;
|
2016-06-08 07:11:05 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-03-31 11:38:24 +02:00
|
|
|
protected $dates = ['deleted_at'];
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-12 21:36:28 +01:00
|
|
|
protected $presenter = 'App\Ninja\Presenters\CreditPresenter';
|
2015-03-31 11:38:24 +02:00
|
|
|
|
2017-03-30 10:46:52 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
'public_notes',
|
|
|
|
'private_notes',
|
|
|
|
];
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
public function account()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Account');
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-11-12 21:36:28 +01:00
|
|
|
public function user()
|
|
|
|
{
|
2016-06-08 07:11:05 +02:00
|
|
|
return $this->belongsTo('App\Models\User')->withTrashed();
|
2015-11-12 21:36:28 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function invoice()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->belongsTo('App\Models\Invoice')->withTrashed();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function client()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->belongsTo('App\Models\Client')->withTrashed();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-11-30 19:21:50 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRoute()
|
|
|
|
{
|
|
|
|
return "/credits/{$this->public_id}";
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getEntityType()
|
|
|
|
{
|
|
|
|
return ENTITY_CREDIT;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $amount
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
Credit::creating(function ($credit) {
|
2015-03-16 22:45:25 +01:00
|
|
|
});
|
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
Credit::created(function ($credit) {
|
|
|
|
event(new CreditWasCreated($credit));
|
2016-06-08 07:11:05 +02:00
|
|
|
});
|