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;
}
/**