mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-03 19:07:33 -05:00 
			
		
		
		
	
		
			
				
	
	
		
			56 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			56 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php namespace App\Models;
 | 
						|
 | 
						|
class Credit extends EntityModel
 | 
						|
{
 | 
						|
    public function invoice()
 | 
						|
    {
 | 
						|
        return $this->belongsTo('Invoice')->withTrashed();
 | 
						|
    }
 | 
						|
 | 
						|
    public function client()
 | 
						|
    {
 | 
						|
        return $this->belongsTo('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::created(function ($credit) {
 | 
						|
    Activity::createCredit($credit);
 | 
						|
});
 | 
						|
 | 
						|
Credit::updating(function ($credit) {
 | 
						|
    Activity::updateCredit($credit);
 | 
						|
});
 | 
						|
 | 
						|
Credit::deleting(function ($credit) {
 | 
						|
    Activity::archiveCredit($credit);
 | 
						|
});
 | 
						|
 | 
						|
Credit::restoring(function ($credit) {
 | 
						|
    Activity::restoreCredit($credit);
 | 
						|
});
 |