invoiceninja/app/models/Credit.php

57 lines
813 B
PHP
Raw Normal View History

2013-12-01 22:58:25 +02:00
<?php
2013-12-04 18:20:14 +02:00
class Credit extends EntityModel
2013-12-31 11:43:39 +02:00
{
2013-12-01 22:58:25 +02:00
public function invoice()
{
2014-02-19 15:28:29 +02:00
return $this->belongsTo('Invoice')->withTrashed();
2013-12-01 22:58:25 +02:00
}
2013-12-05 17:23:24 +02:00
public function client()
{
2014-02-19 15:28:29 +02:00
return $this->belongsTo('Client')->withTrashed();
2013-12-05 17:23:24 +02:00
}
2013-12-01 22:58:25 +02:00
public function getName()
{
2014-01-08 20:09:47 +00:00
return '';
2013-12-01 22:58:25 +02:00
}
public function getEntityType()
{
return ENTITY_CREDIT;
}
2014-01-16 21:12:46 +00: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;
}
2013-12-01 22:58:25 +02:00
}
Credit::created(function($credit)
{
2013-12-05 17:23:24 +02:00
Activity::createCredit($credit);
2014-01-14 11:52:56 +00:00
});
Credit::updating(function($credit)
{
Activity::updateCredit($credit);
});
Credit::deleting(function($credit)
{
Activity::archiveCredit($credit);
2013-12-01 22:58:25 +02:00
});