Improvements to invoice display and other misc items

This commit is contained in:
Deon George
2013-12-20 10:00:32 +11:00
parent 778eada7f0
commit e19518c505
43 changed files with 1122 additions and 887 deletions

View File

@@ -8,22 +8,15 @@
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*
* Column Definitions:
* + item_type: 0=MAIN Service Item,2=?,3=?,4=Connection/Setup,5=Excess Service Item,6=Change Service,126=Payment Fee,127=Late Fee
*/
class Model_Invoice_Item extends ORM_OSB {
// Relationships
protected $_belongs_to = array(
'product'=>array(),
'invoice'=>array(),
'service'=>array()
);
protected $_has_one = array(
'charge'=>array('far_key'=>'charge_id','foreign_key'=>'id')
);
protected $_has_many = array(
'invoice_item_tax'=>array('far_key'=>'id')
'tax'=>array('model'=>'Invoice_Item_Tax','far_key'=>'id')
);
protected $_display_filters = array(
@@ -40,7 +33,7 @@ class Model_Invoice_Item extends ORM_OSB {
// Items belonging to an invoice item
protected $_sub_items_load = array(
'invoice_item_tax'=>FALSE,
'tax'=>FALSE,
);
// The total of all discounts
@@ -48,6 +41,63 @@ class Model_Invoice_Item extends ORM_OSB {
return Currency::round($this->discount_amt);
}
/**
* The line that will be printed on an invoice
*
* @todo This method includes some database format validation routines, which can be removed when the database
* is completly transformed.
*/
public function invoice_line() {
$ii = NULL;
// Our module is responsible for rending the invoice line
$ii = ($this->module_id AND method_exists($this->module(),'invoice_item')) ? $this->module()->invoice_item($this->item_type) : StaticList_ItemType::get($this->item_type);
switch ($this->item_type) {
// Service Charges
case 0:
case 7:
return ((! $this->service_id OR $this->product_id OR $this->charge_id OR $this->product_name OR ! $this->recurring_schedule OR ! $this->date_start OR ! $this->date_stop) ? '+ ' : '').$ii.' '.$this->period();
case 1:
// @todo
return $ii;
case 2:
case 3:
case 4:
case 5:
case 6:
return ((! $this->service_id OR $this->product_id OR $this->charge_id OR $this->product_name OR $this->recurring_schedule OR ! $this->date_start OR ! $this->date_stop) ? '+ ' : '').$ii;
case 124:
case 125:
case 126:
case 127:
// @todo
return $ii;
// @todo DB records to fix.
default:
if ($this->charge_id)
return '*'.($ii ? $ii : $this->charge->description).' '.$this->period();
else
throw HTTP_Exception::factory(501,'Unable to render invoice item :id',array(':id'=>$this->id));
}
}
/**
* Return an instance of the Model of this charge
*/
public function module() {
$x = ORM::factory('Module',$this->module_id);
if (! $x->loaded())
throw new Kohana_Exception('Module :module doesnt exist?',array(':module'=>$this->module_id));
return ORM::factory('Module',$this->module_id)->instance($this->module_ref);
}
// Display the period that a transaction applies
public function period() {
return ($this->date_start == $this->date_stop) ? Config::date($this->date_start) : sprintf('%s -> %s',Config::date($this->date_start),Config::date($this->date_stop));
@@ -71,13 +121,13 @@ class Model_Invoice_Item extends ORM_OSB {
$iito->save();
if (! $iito->saved()) {
$this->void = 1;
$this->save();
break;
}
$this->void = 1;
$this->save();
break;
}
}
} else
throw new Kohana_Exception('Couldnt save invoice_item for some reason?');
@@ -119,7 +169,7 @@ class Model_Invoice_Item extends ORM_OSB {
public function subtotal($format=FALSE) {
$result = $this->price_base*$this->quantity;
return $format ? Currency::display($result) : Currency::round($result);
return $format ? Currency::display($result) : $result;
}
// Sum up the tax that applies to this invoice item
@@ -149,8 +199,12 @@ class Model_Invoice_Item extends ORM_OSB {
* The title for invoice items
*/
public function title() {
if ($this->product_id AND $this->service_id)
return $this->product_id ? sprintf('%s: %s',$this->product->title(),$this->service->name()) : $this->service->name;
if ($this->service_id AND $this->module_id AND method_exists($this->module(),'invoice_title'))
return $this->module_ref ? sprintf('%s: %s',$this->module()->invoice_title(),$this->service->name()) : $this->service->name();
elseif ($x=$this->module() AND ($x instanceof Model_Charge) AND $x->product_id)
return $x->product->title().' '.$this->service->name();
elseif ($this->product_id)
return sprintf('* %s: %s',$this->product->invoice_title(),$this->service->name());
else
return 'Unknown Item';
}
@@ -166,67 +220,6 @@ class Model_Invoice_Item extends ORM_OSB {
return sprintf('%03s-%06s',$this->item_type,$this->id);
}
public function invoice_line() {
if ($this->charge_id)
return $this->charge->description.' '.$this->period();
elseif ($this->service_id)
return 'Service '.$this->period();
else
throw HTTP_Exception::factory(501,'Unable to render invoice item :id',array(':id'=>$this->id));
}
/**
* Name for an invoice item
* @deprecated, use StaticList_ItemType()
*/
public function name() {
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');
}
}
/**
* Detail behind an invoice item
*/
public function detail() {
switch ($this->item_type) {
case 0: return '';
case 1: return _('Hardware');
case 2: return _('Service Relocation Fee');
case 3: return _('Service Change Fee');
case 4: return _('Service Connection Fee');
case 5: return sprintf('%s@%3.2f',$this->quantity,$this->price_base);
case 6: return _('Service Excess Fee');
case 125: return _('Payment Fee');
case 126: return _('Rounding');
case 127: return _('Late Payment Fee');
default: '';
}
}
public function invoice_detail_items() {
switch ($this->item_type) {
case 0: