1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/models/Credit.php

57 lines
813 B
PHP
Raw Normal View History

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