Improvements to invoice

This commit is contained in:
Deon George
2013-06-13 23:35:19 +10:00
parent 25a47cac3a
commit 6875dc3693
15 changed files with 321 additions and 200 deletions

View File

@@ -75,15 +75,95 @@ class Model_Invoice extends ORM_OSB implements Cartable {
/**
* Add an item to an invoice
*/
public function add_item() {
public function add_item(Model_Invoice_Item $iio=NULL) {
// Just to check if an invoice is called from the DB, that it should have items with it.
if ($this->loaded() and ! $this->invoice_items)
throw new Kohana_Exception('Need to load invoice_items?');
throw HTTP_Exception::factory(501,'Need dto load invoice_items?');
$c = count($this->invoice_items);
// @todo This is the old calling of this function, which should be removed
if (is_null($iio)) {
$c = count($this->invoice_items);
$this->invoice_items[$c] = ORM::factory('Invoice_Item');
$this->invoice_items[$c] = ORM::factory('Invoice_Item');
return $this->invoice_items[$c];
return $this->invoice_items[$c];
}
array_push($this->invoice_items,$iio);
}
/**
* Return the recurring schedules that are on an invoice
*
* @param $period Return an Array of items for that period
*/
public function items_periods($period=NULL) {
$result = array();
foreach ($this->items() as $ito) {
// We are only interested in item_type=0
if ($ito->item_type != 0)
continue;
if (is_null($period) AND ! in_array($ito->recurring_schedule,$result))
array_push($result,$ito->recurring_schedule);
elseif ($ito->recurring_schedule == $period)
array_push($result,$ito);
}
return $result;
}
/**
* Return the extra items on an invoice relating to an invoice_item
* IE: item_type != 0
*/
public function service_items_extra(Model_Invoice_Item $o) {
$result = array();
// At the moment, we only return extra items pertaining to a service
if (! $o->service_id)
return $result;
foreach ($this->items() as $iio)
if ($iio->item_type != 0 AND $iio->service_id == $o->service_id)
array_push($result,$iio);
return $result;
}
/**
* Return the total of all items relating to a service
*/
public function service_items_tax(Model_Invoice_Item $o,$format=FALSE) {
$result = 0;
// At the moment, we only return extra items pertaining to a service
if (! $o->service_id)
return $result;
foreach ($this->items() as $iio)
if ($iio->service_id == $o->service_id)
$result += $iio->tax();
return $format ? Currency::display($result) : $result;
}
/**
* Return the total of all items relating to a service
*/
public function service_items_total(Model_Invoice_Item $o,$format=FALSE) {
$result = 0;
// At the moment, we only return extra items pertaining to a service
if (! $o->service_id)
return $result;
foreach ($this->items() as $iio)
if ($iio->service_id == $o->service_id)
$result += $iio->total();
return $format ? Currency::display($result) : $result;
}
/**

View File

@@ -9,7 +9,7 @@
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Invoice_Item extends ORM_OSB {
class Model_Invoice_Item extends ORM_OSB implements Invoicable {
// Relationships
protected $_belongs_to = array(
'product'=>array(),
@@ -39,6 +39,16 @@ class Model_Invoice_Item extends ORM_OSB {
private $subitems = array();
private $subitems_loaded = FALSE;
/** INTERFACE REQUIREMENTS **/
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));
}
public function __construct($id = NULL) {
// Load our model.
parent::__construct($id);
@@ -71,7 +81,7 @@ class Model_Invoice_Item extends ORM_OSB {
}
// Sum up the tax that applies to this invoice item
public function tax() {
public function tax($format=FALSE) {
$result = 0;
foreach ($this->invoice_item_tax->find_all() as $iit)
@@ -81,7 +91,9 @@ class Model_Invoice_Item extends ORM_OSB {
if (! $result)
$result += round($this->price_base*$this->quantity*.1,2);
return Currency::round($result);
$result = Currency::round($result);
return $format ? Currency::display($result) : $result;
}
public function tax_items() {
@@ -98,8 +110,10 @@ class Model_Invoice_Item extends ORM_OSB {
}
// This total of this item before discounts and taxes
public function subtotal() {
return Currency::round($this->price_base*$this->quantity);
public function subtotal($format=FALSE) {
$result = Currency::round($this->price_base*$this->quantity);
return $format ? Currency::display($result) : $result;
}
// The total of all discounts
@@ -107,8 +121,10 @@ class Model_Invoice_Item extends ORM_OSB {
return Currency::round($this->discount_amt);
}
public function total() {
return Currency::round($this->subtotal()+$this->tax()-$this->discount());
public function total($format=FALSE) {
$result = Currency::round($this->subtotal()+$this->tax()-$this->discount());
return $format ? Currency::display($result) : $result;
}
/**