2013-10-10 02:44:53 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides invoice item capabilities.
|
|
|
|
*
|
|
|
|
* @package Invoice
|
|
|
|
* @category Models
|
|
|
|
* @author Deon George
|
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
|
|
|
* @license http://dev.osbill.net/license.html
|
|
|
|
*/
|
2016-08-15 11:23:18 +00:00
|
|
|
class Model_Invoice_Item extends ORM {
|
2013-10-10 02:44:53 +00:00
|
|
|
// Relationships
|
|
|
|
protected $_belongs_to = array(
|
|
|
|
'invoice'=>array(),
|
2016-08-03 06:25:26 +00:00
|
|
|
'module'=>array(),
|
|
|
|
'product'=>array(),
|
|
|
|
'service'=>array(),
|
2013-10-10 02:44:53 +00:00
|
|
|
);
|
2016-08-03 06:25:26 +00:00
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
protected $_has_many = array(
|
2013-12-19 23:00:32 +00:00
|
|
|
'tax'=>array('model'=>'Invoice_Item_Tax','far_key'=>'id')
|
2013-10-10 02:44:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
protected $_display_filters = array(
|
|
|
|
'date_orig'=>array(
|
2014-08-25 04:41:07 +00:00
|
|
|
array('Site::Date',array(':value')),
|
2013-10-10 02:44:53 +00:00
|
|
|
),
|
|
|
|
'date_start'=>array(
|
2014-08-25 04:41:07 +00:00
|
|
|
array('Site::Date',array(':value')),
|
2013-10-10 02:44:53 +00:00
|
|
|
),
|
|
|
|
'date_stop'=>array(
|
2014-08-25 04:41:07 +00:00
|
|
|
array('Site::Date',array(':value')),
|
2013-10-10 02:44:53 +00:00
|
|
|
),
|
2016-08-03 06:25:26 +00:00
|
|
|
'recurring_schedule'=>array(
|
|
|
|
array('StaticList_RecurSchedule::get',array(':value')),
|
|
|
|
),
|
2013-10-10 02:44:53 +00:00
|
|
|
);
|
|
|
|
|
2013-09-06 05:39:56 +00:00
|
|
|
// Items belonging to an invoice item
|
|
|
|
protected $_sub_items_load = array(
|
2016-07-27 06:55:07 +00:00
|
|
|
'tax'=>array(),
|
2013-09-06 05:39:56 +00:00
|
|
|
);
|
|
|
|
|
2016-08-03 04:00:51 +00:00
|
|
|
/**
|
|
|
|
* Invoice Item Types
|
2016-08-03 06:25:26 +00:00
|
|
|
*
|
|
|
|
* @see isValid()
|
2016-08-03 04:00:51 +00:00
|
|
|
*/
|
|
|
|
private $_item_type = array(
|
2016-08-03 06:25:26 +00:00
|
|
|
0=>'Product/Service', // * Line Charge Topic on Invoice.
|
|
|
|
1=>'Hardware', // *
|
|
|
|
2=>'Service Relocation Fee', // * Must have corresponding SERVICE_ID
|
2016-08-10 06:07:00 +00:00
|
|
|
3=>'Service Change', // * Must have corresponding SERVICE_ID
|
|
|
|
4=>'Service Connection', // * Must have corresponding SERVICE_ID
|
2016-08-03 06:25:26 +00:00
|
|
|
5=>'Excess Usage', // * Excess Service Item, of item 0, must have corresponding SERVICE_ID
|
2016-08-10 06:07:00 +00:00
|
|
|
6=>'Service Cancellation', // * Must have corresponding SERVICE_ID
|
2016-08-03 06:25:26 +00:00
|
|
|
7=>'Extra Product/Service Charge', // * Service Billing in advance, Must have corresponding SERVICE_ID
|
|
|
|
8=>'Product Addition', // * Additional Product Customisation, Must have corresponding SERVICE_ID
|
|
|
|
120=>'Credit/Debit Transfer', // * SERVICE_ID is NULL, MODULE_ID is NULL, MODULE_REF is NULL : INVOICE_ID is NOT NULL
|
|
|
|
124=>'Late Payment Fee', // * SERVICE_ID is NULL, MODULE_ID = CHECKOUT MODULE,
|
|
|
|
125=>'Payment Fee', // * SERVICE_ID is NULL, MODULE_ID = CHECKOUT MODULE, MODULE_REF = CHECKOUT NAME
|
|
|
|
126=>'Other', // * MODEL_ID should be a module
|
|
|
|
127=>'Rounding', // * SERVICE_ID is NULL, MODULE_ID is NULL, MODULE_REF is NULL
|
2016-08-03 04:00:51 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/** REQUIRED ABSTRACT METHODS **/
|
|
|
|
|
|
|
|
/** LOCAL METHODS **/
|
|
|
|
|
2016-08-03 06:25:26 +00:00
|
|
|
/**
|
|
|
|
* Module assocated with the invoice item
|
|
|
|
*/
|
2016-08-10 06:07:00 +00:00
|
|
|
private function _module() {
|
2016-08-03 06:25:26 +00:00
|
|
|
if (! $this->module_id OR ! $this->module_ref)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return $this->module->instance($this->module_ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the list of available item types
|
|
|
|
*/
|
2016-08-03 04:00:51 +00:00
|
|
|
public function _types() {
|
|
|
|
return $this->_item_type;
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:55:07 +00:00
|
|
|
/**
|
|
|
|
* Add tax to our item
|
|
|
|
*
|
|
|
|
* @param Model_Country the country to get the tax rates
|
|
|
|
* @param Boolean Is this price inc/ex tax
|
|
|
|
*/
|
|
|
|
public function add_sub_item(Model_Country $co,$taxed=FALSE) {
|
|
|
|
$orig = $this->subtotal();
|
|
|
|
$tax = 0;
|
|
|
|
|
|
|
|
foreach ($co->tax->find_all() as $to) {
|
|
|
|
$iito = ORM::factory('Invoice_Item_Tax');
|
|
|
|
|
|
|
|
$iito->tax_id = $to->id;
|
|
|
|
$iito->amount = Currency::round($taxed ? ($orig-$orig/(1+$to->rate)) : $orig*$to->rate);
|
|
|
|
|
|
|
|
$tax += $iito->amount;
|
|
|
|
$this->subitem_add($iito);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If taxed, we need to reduce our base_rate to a pre-tax amount
|
|
|
|
if ($taxed) {
|
|
|
|
$this->price_base -= Currency::round($tax/$this->quantity,1);
|
|
|
|
|
|
|
|
// If there is any rounding, we'll take it off the last IITO
|
|
|
|
$iito->amount += $orig-$this->total();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-05 05:22:23 +00:00
|
|
|
// The total of all discounts
|
|
|
|
public function discount() {
|
|
|
|
return Currency::round($this->discount_amt);
|
2013-09-06 05:39:56 +00:00
|
|
|
}
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2016-08-03 06:25:26 +00:00
|
|
|
/**
|
|
|
|
* Validate that this record is correct
|
|
|
|
*/
|
|
|
|
private function isValid() {
|
|
|
|
switch ($this->item_type) {
|
|
|
|
case 0:
|
|
|
|
// @todo Validate if module_id = charge and module_ref corresponds with the line item
|
2016-08-10 06:07:00 +00:00
|
|
|
if (! $this->service_id OR ! $this->product_id OR ($this->module_id AND $this->module_id != 30 AND ! is_null($this>module_ref)) OR $this->product_name OR is_null($this->recurring_schedule))
|
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (! $this->product_id OR $this->module_id OR $this->module_ref OR $this->product_name OR ! is_null($this->recurring_schedule))
|
2016-08-03 06:25:26 +00:00
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
// These items come from the charge module, so we should have a charge module_id and ref
|
2016-08-10 06:07:00 +00:00
|
|
|
case 2:
|
|
|
|
if (! $this->service_id OR ! $this->product_id OR ($this->module_id AND $this->module_id != 30 AND ! is_null($this>module_ref)) OR $this->product_name OR ! is_null($this->recurring_schedule))
|
|
|
|
return FALSE;
|
|
|
|
break;
|
2016-08-03 06:25:26 +00:00
|
|
|
case 3:
|
|
|
|
case 4:
|
2016-08-15 11:23:18 +00:00
|
|
|
case 7:
|
2016-08-03 06:25:26 +00:00
|
|
|
if (! $this->service_id OR ! $this->product_id OR ! $this->module_id OR ! $this->module_ref OR $this->product_name OR ! is_null($this->recurring_schedule))
|
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
// These items come from the charge module, so we should have a charge module_id and ref
|
|
|
|
case 5:
|
|
|
|
if (! $this->service_id OR ! $this->product_id OR ! $this->module_id OR ! $this->module_ref OR $this->product_name OR ! is_null($this->recurring_schedule))
|
|
|
|
return FALSE;
|
|
|
|
break;
|
2016-08-10 06:07:00 +00:00
|
|
|
case 124:
|
|
|
|
if ($this->service_id OR $this->product_id OR $this->module_id OR $this->module_ref OR $this->product_name OR ! is_null($this->recurring_schedule))
|
|
|
|
return FALSE;
|
|
|
|
break;
|
2016-08-03 06:25:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here, assume it is valid
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-12-19 23:00:32 +00:00
|
|
|
/**
|
|
|
|
* Return an instance of the Model of this charge
|
|
|
|
*/
|
2016-08-03 06:25:26 +00:00
|
|
|
public function module() {
|
|
|
|
return $this->_module();
|
2013-12-19 23:00:32 +00:00
|
|
|
}
|
2016-08-10 06:07:00 +00:00
|
|
|
|
2016-08-15 11:23:18 +00:00
|
|
|
public function name($variable=NULL) {
|
|
|
|
if (! $this->isValid())
|
|
|
|
return sprintf('Record Error [%s-%s]',$this->item_type,$this->id);
|
|
|
|
|
|
|
|
switch ($this->item_type) {
|
|
|
|
case 0:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
return sprintf('%s: %s',$this->product->name($variable),$this->service->namesub($variable));
|
|
|
|
case 124:
|
|
|
|
return StaticList_ItemType::get($this->item_type);
|
|
|
|
default:
|
|
|
|
return sprintf('Unknown [%s-%s]',$this->item_type,$this->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function namesub($variable=NULL) {
|
|
|
|
if (! $this->isValid())
|
|
|
|
return sprintf('Record Error [%s-%s]',$this->item_type,$this->id);
|
|
|
|
|
|
|
|
switch ($this->item_type) {
|
|
|
|
case 0:
|
|
|
|
return sprintf('%s: %s',StaticList_ItemType::get($this->item_type),$this->period());
|
|
|
|
case 2:
|
|
|
|
return sprintf('%s: %s',StaticList_ItemType::get($this->item_type),$this->_module() ? $this->_module()->display('date_charge') : $this->period());
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 6:
|
|
|
|
return sprintf('%s: %s',StaticList_ItemType::get($this->item_type),($this->_module()->attributes ? $this->_module()->namesub($variable) : $this->_module()->display('date_charge')));
|
|
|
|
case 5:
|
|
|
|
case 7:
|
|
|
|
return $this->_module()->namesub($variable);
|
|
|
|
default:
|
|
|
|
return sprintf('Unknown [%s-%s]',$this->item_type,$this->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-05 05:22:23 +00:00
|
|
|
// Display the period that a transaction applies
|
|
|
|
public function period() {
|
2014-08-25 04:41:07 +00:00
|
|
|
return ($this->date_start == $this->date_stop) ? Site::Date($this->date_start) : sprintf('%s -> %s',Site::Date($this->date_start),Site::Date($this->date_stop));
|
2013-06-13 13:35:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 11:23:18 +00:00
|
|
|
/**
|
|
|
|
* Display the Invoice Item Reference Number
|
|
|
|
*/
|
|
|
|
public function refnum($short=FALSE) {
|
|
|
|
return $short ? '' : sprintf('%03s-',$this->item_type).sprintf('%06s',$this->id);
|
|
|
|
}
|
|
|
|
|
2013-12-05 05:22:23 +00:00
|
|
|
public function save(Validation $validation = NULL) {
|
|
|
|
// Our items will be clobbered once we save the object, so we need to save it here.
|
|
|
|
$subitems = $this->subitems();
|
|
|
|
|
|
|
|
// Save the invoice item
|
|
|
|
parent::save($validation);
|
|
|
|
|
|
|
|
// Need to save the associated items and their taxes
|
|
|
|
if ($this->loaded()) {
|
|
|
|
foreach ($subitems as $iito) {
|
|
|
|
$iito->invoice_item_id = $this->id;
|
|
|
|
|
|
|
|
if (! $iito->changed())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$iito->save();
|
|
|
|
|
|
|
|
if (! $iito->saved()) {
|
2013-12-19 23:00:32 +00:00
|
|
|
$this->void = 1;
|
|
|
|
$this->save();
|
2013-12-05 05:22:23 +00:00
|
|
|
|
2013-12-19 23:00:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-12-05 05:22:23 +00:00
|
|
|
}
|
2013-12-19 23:00:32 +00:00
|
|
|
|
2013-12-05 05:22:23 +00:00
|
|
|
} else
|
|
|
|
throw new Kohana_Exception('Couldnt save invoice_item for some reason?');
|
2013-12-31 06:18:49 +00:00
|
|
|
|
2013-12-05 05:22:23 +00:00
|
|
|
return $this;
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 05:22:23 +00:00
|
|
|
// This total of this item before discounts and taxes
|
|
|
|
public function subtotal($format=FALSE) {
|
2016-08-03 06:25:26 +00:00
|
|
|
$result = Currency::round($this->price_base*$this->quantity);
|
2013-12-05 05:22:23 +00:00
|
|
|
|
2013-12-19 23:00:32 +00:00
|
|
|
return $format ? Currency::display($result) : $result;
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sum up the tax that applies to this invoice item
|
2013-06-13 13:35:19 +00:00
|
|
|
public function tax($format=FALSE) {
|
2013-10-10 02:44:53 +00:00
|
|
|
$result = 0;
|
|
|
|
|
2013-12-05 05:22:23 +00:00
|
|
|
foreach ($this->subitems() as $iito)
|
|
|
|
$result += $iito->amount;
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-12-05 05:22:23 +00:00
|
|
|
return $format ? Currency::display($result) : Currency::round($result);
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 13:53:04 +00:00
|
|
|
public function tax_items() {
|
|
|
|
$result = array();
|
|
|
|
|
2013-12-05 05:22:23 +00:00
|
|
|
foreach ($this->subitems() as $iito) {
|
2014-01-07 12:29:18 +00:00
|
|
|
if (! isset($result[$iito->tax_id]))
|
|
|
|
$result[$iito->tax_id] = 0;
|
2013-05-14 13:53:04 +00:00
|
|
|
|
2013-12-05 05:22:23 +00:00
|
|
|
$result[$iito->tax_id] += $iito->amount;
|
2013-05-14 13:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2013-12-05 05:22:23 +00:00
|
|
|
public function total($format=FALSE) {
|
|
|
|
$result = $this->void ? 0 : $this->subtotal()+$this->tax()-$this->discount();
|
|
|
|
|
|
|
|
return $format ? Currency::display($result) : Currency::round($result);
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|