2010-11-29 22:41:08 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides invoice item capabilities.
|
|
|
|
*
|
2013-03-19 22:35:19 +00:00
|
|
|
* @package Invoice
|
2010-11-29 22:41:08 +00:00
|
|
|
* @category Models
|
|
|
|
* @author Deon George
|
2013-03-19 22:35:19 +00:00
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
2010-11-29 22:41:08 +00:00
|
|
|
* @license http://dev.osbill.net/license.html
|
|
|
|
*/
|
2012-12-10 21:48:30 +00:00
|
|
|
class Model_Invoice_Item extends ORM_OSB {
|
2011-10-10 23:38:21 +00:00
|
|
|
// Relationships
|
2010-11-29 22:41:08 +00:00
|
|
|
protected $_belongs_to = array(
|
|
|
|
'product'=>array(),
|
|
|
|
'invoice'=>array(),
|
|
|
|
'service'=>array()
|
|
|
|
);
|
2011-10-14 05:44:12 +00:00
|
|
|
protected $_has_one = array(
|
|
|
|
'charge'=>array('far_key'=>'charge_id','foreign_key'=>'id')
|
|
|
|
);
|
2010-11-29 22:41:08 +00:00
|
|
|
protected $_has_many = array(
|
2011-07-13 22:59:32 +00:00
|
|
|
'invoice_item_tax'=>array('far_key'=>'id')
|
2010-11-29 22:41:08 +00:00
|
|
|
);
|
2011-10-10 23:38:21 +00:00
|
|
|
|
|
|
|
protected $_display_filters = array(
|
2011-10-14 05:44:12 +00:00
|
|
|
'date_orig'=>array(
|
|
|
|
array('Config::date',array(':value')),
|
|
|
|
),
|
2011-10-10 23:38:21 +00:00
|
|
|
'date_start'=>array(
|
|
|
|
array('Config::date',array(':value')),
|
|
|
|
),
|
|
|
|
'date_stop'=>array(
|
|
|
|
array('Config::date',array(':value')),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2012-01-11 23:23:35 +00:00
|
|
|
// Items belonging to an invoice
|
|
|
|
private $subitems = array();
|
2012-01-29 06:23:24 +00:00
|
|
|
private $subitems_loaded = FALSE;
|
2012-01-11 23:23:35 +00:00
|
|
|
|
|
|
|
public function __construct($id = NULL) {
|
|
|
|
// Load our model.
|
|
|
|
parent::__construct($id);
|
|
|
|
|
|
|
|
return $this->load_sub_items();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function load_sub_items() {
|
|
|
|
// Load our sub items
|
2012-01-29 06:23:24 +00:00
|
|
|
if (! $this->subitems_loaded AND $this->loaded()) {
|
2012-01-11 23:23:35 +00:00
|
|
|
$this->subitems['tax'] = $this->invoice_item_tax->find_all()->as_array();
|
2012-01-29 06:23:24 +00:00
|
|
|
$this->subitems_loaded = TRUE;
|
2012-01-11 23:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
// Display a transaction number
|
|
|
|
public function trannum() {
|
|
|
|
return sprintf('%03s-%06s',$this->item_type,$this->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display the period that a transaction applies
|
|
|
|
public function period() {
|
|
|
|
if ($this->date_start == $this->date_stop)
|
2011-10-11 08:52:31 +00:00
|
|
|
return Config::date($this->date_start);
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
else
|
2011-07-14 09:09:03 +00:00
|
|
|
return sprintf('%s -> %s',Config::date($this->date_start),Config::date($this->date_stop));
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-10-10 23:38:21 +00:00
|
|
|
// Sum up the tax that applies to this invoice item
|
|
|
|
public function tax() {
|
2011-10-11 08:52:31 +00:00
|
|
|
$result = 0;
|
2011-07-14 09:09:03 +00:00
|
|
|
|
2011-10-10 23:38:21 +00:00
|
|
|
foreach ($this->invoice_item_tax->find_all() as $iit)
|
2011-10-11 08:52:31 +00:00
|
|
|
$result += $iit->amount;
|
|
|
|
|
|
|
|
// @todo This shouldnt be required.
|
|
|
|
if (! $result)
|
2013-02-09 12:40:18 +00:00
|
|
|
$result += round($this->price_base*$this->quantity*.1,2);
|
2011-10-10 23:38:21 +00:00
|
|
|
|
2012-01-09 01:35:24 +00:00
|
|
|
return Currency::round($result);
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-10-10 23:38:21 +00:00
|
|
|
// This total of this item before discounts and taxes
|
2010-11-29 22:41:08 +00:00
|
|
|
public function subtotal() {
|
2012-01-09 01:35:24 +00:00
|
|
|
return Currency::round($this->price_base*$this->quantity);
|
2011-10-10 23:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The total of all discounts
|
|
|
|
public function discount() {
|
2012-01-09 01:35:24 +00:00
|
|
|
return Currency::round($this->discount_amt);
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function total() {
|
2012-01-09 01:35:24 +00:00
|
|
|
return Currency::round($this->subtotal()+$this->tax()-$this->discount());
|
2011-10-10 23:38:21 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 01:35:24 +00:00
|
|
|
/**
|
|
|
|
* Name for an invoice item
|
|
|
|
*/
|
2011-10-11 08:52:31 +00:00
|
|
|
public function name() {
|
2012-01-09 01:35:24 +00:00
|
|
|
switch ($this->item_type) {
|
|
|
|
case 0: return _('Service');
|
|
|
|
|
|
|
|
case 1: return _('Item');
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 6:
|
|
|
|
case 126:
|
|
|
|
case 127: return _('Charge');
|
|
|
|
|
|
|
|
case 5: return $this->charge->description;
|
|
|
|
|
|
|
|
default: return _('Other');
|
|
|
|
}
|
2011-10-11 08:52:31 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 01:35:24 +00:00
|
|
|
/**
|
|
|
|
* Detail behind an invoice item
|
|
|
|
*/
|
2011-10-11 08:52:31 +00:00
|
|
|
public function detail() {
|
2012-01-09 01:35:24 +00:00
|
|
|
switch ($this->item_type) {
|
|
|
|
case 0: return '';
|
2011-10-11 08:52:31 +00:00
|
|
|
|
2012-01-09 01:35:24 +00:00
|
|
|
case 1: return _('Hardware');
|
|
|
|
|
|
|
|
case 2: return _('Service Relocation Fee');
|
|
|
|
|
|
|
|
case 3: return _('Service Change Fee');
|
2011-10-14 05:44:12 +00:00
|
|
|
|
2012-01-09 01:35:24 +00:00
|
|
|
case 4: return _('Service Connection Fee');
|
2011-10-10 23:38:21 +00:00
|
|
|
|
2012-01-09 01:35:24 +00:00
|
|
|
case 5: return sprintf('%s@%3.2f',$this->quantity,$this->price_base);
|
|
|
|
|
|
|
|
case 6: return _('Service Excess Fee');
|
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
case 125: return _('Payment Fee');
|
|
|
|
|
2012-01-09 01:35:24 +00:00
|
|
|
case 126: return _('Rounding');
|
|
|
|
|
|
|
|
case 127: return _('Late Payment Fee');
|
|
|
|
|
|
|
|
default: '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function invoice_detail_items() {
|
|
|
|
switch ($this->item_type) {
|
|
|
|
case 0:
|
|
|
|
return $this->service->details('invoice_detail_items');
|
|
|
|
case 4:
|
|
|
|
return array('Charge'=>_('Service Connection Fee'));
|
|
|
|
case 5:
|
|
|
|
return $this->charge->details('invoice_detail_items');
|
|
|
|
default:
|
|
|
|
return array('Item'=>$this->item_type);
|
|
|
|
}
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-07-13 22:59:32 +00:00
|
|
|
public function save(Validation $validation = NULL) {
|
2013-01-15 06:07:54 +00:00
|
|
|
if (! $this->changed())
|
|
|
|
return;
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
// Save the invoice item
|
2011-10-11 08:52:31 +00:00
|
|
|
parent::save($validation);
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2012-01-11 23:23:35 +00:00
|
|
|
// Need to save the discounts associated with the invoice_item
|
2010-11-29 22:41:08 +00:00
|
|
|
if ($this->saved()) {
|
2012-11-09 23:13:57 +00:00
|
|
|
$iito = ORM::factory('Invoice_Item_Tax');
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2012-01-11 23:23:35 +00:00
|
|
|
if ($this->subitems_loaded) {
|
|
|
|
foreach (array('tax') as $i)
|
|
|
|
foreach ($this->subitems[$i] as $io)
|
|
|
|
if ($io->changed())
|
|
|
|
$io->save();
|
|
|
|
|
|
|
|
// Add TAX details
|
|
|
|
} else
|
|
|
|
// @todo tax parameters should come from user session
|
|
|
|
foreach (Tax::detail(61,NULL,$this->subtotal()) as $tax) {
|
|
|
|
$iito->clear();
|
|
|
|
$iito->invoice_item_id = $this->id;
|
|
|
|
$iito->tax_id = $tax['id'];
|
|
|
|
// @todo Rounding here should come from a global config
|
|
|
|
$iito->amount = round($tax['amount'],2);
|
|
|
|
|
|
|
|
if (! $iito->check())
|
|
|
|
throw new Kohana_Exception('Couldnt save tax for some reason - failed check()?');
|
|
|
|
|
|
|
|
$iito->save();
|
|
|
|
|
|
|
|
if (! $iito->saved())
|
|
|
|
throw new Kohana_Exception('Couldnt save tax for some reason - failed save()?');
|
|
|
|
}
|
2010-11-29 22:41:08 +00:00
|
|
|
} else
|
|
|
|
throw new Kohana_Exception('Couldnt save invoice_item for some reason?');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|