mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-03 19:37:32 -05:00 
			
		
		
		
	
		
			
				
	
	
		
			117 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Models;
 | 
						|
 | 
						|
use App\Events\CreditWasCreated;
 | 
						|
use Illuminate\Database\Eloquent\SoftDeletes;
 | 
						|
use Laracasts\Presenter\PresentableTrait;
 | 
						|
 | 
						|
/**
 | 
						|
 * Class Credit.
 | 
						|
 */
 | 
						|
class Credit extends EntityModel
 | 
						|
{
 | 
						|
    use SoftDeletes;
 | 
						|
    use PresentableTrait;
 | 
						|
 | 
						|
    /**
 | 
						|
     * @var array
 | 
						|
     */
 | 
						|
    protected $dates = ['deleted_at'];
 | 
						|
    /**
 | 
						|
     * @var string
 | 
						|
     */
 | 
						|
    protected $presenter = 'App\Ninja\Presenters\CreditPresenter';
 | 
						|
 | 
						|
    /**
 | 
						|
     * @var array
 | 
						|
     */
 | 
						|
    protected $fillable = [
 | 
						|
        'public_notes',
 | 
						|
        'private_notes',
 | 
						|
    ];
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 | 
						|
     */
 | 
						|
    public function account()
 | 
						|
    {
 | 
						|
        return $this->belongsTo('App\Models\Account');
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public function user()
 | 
						|
    {
 | 
						|
        return $this->belongsTo('App\Models\User')->withTrashed();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public function invoice()
 | 
						|
    {
 | 
						|
        return $this->belongsTo('App\Models\Invoice')->withTrashed();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public function client()
 | 
						|
    {
 | 
						|
        return $this->belongsTo('App\Models\Client')->withTrashed();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public function getName()
 | 
						|
    {
 | 
						|
        return '';
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public function getRoute()
 | 
						|
    {
 | 
						|
        return "/credits/{$this->public_id}";
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public function getEntityType()
 | 
						|
    {
 | 
						|
        return ENTITY_CREDIT;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $amount
 | 
						|
     *
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    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));
 | 
						|
});
 |