Revamping invoice rendering
This commit is contained in:
@@ -43,7 +43,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
|
||||
// Items belonging to an invoice
|
||||
protected $_sub_items_load = array(
|
||||
'invoice_item'=>array('service_id','item_type','date_start','date_stop'),
|
||||
'invoice_item'=>array('service_id','product_id','item_type','date_start','date_stop'),
|
||||
);
|
||||
|
||||
protected $_compress_column = array(
|
||||
@@ -67,12 +67,6 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
return new Cart_Item(1,sprintf('Invoice: %s',$this->refnum()),$this->due());
|
||||
}
|
||||
|
||||
public function add_sub_item(Model_Invoice_Item $iio,$taxed=FALSE) {
|
||||
$iio->add_sub_item($this->account->country,$taxed);
|
||||
|
||||
$this->subitem_add($iio);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if this invoice is already in the cart
|
||||
*/
|
||||
@@ -80,7 +74,16 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
return count(Cart::instance()->get($this->mid(),$this->id));
|
||||
}
|
||||
|
||||
// Our local methods
|
||||
/** LOCAL METHODS **/
|
||||
|
||||
/**
|
||||
* Add items to the invoice while building it
|
||||
*/
|
||||
public function add_sub_item(Model_Invoice_Item $iio,$taxed=FALSE) {
|
||||
$iio->add_sub_item($this->account->country,$taxed);
|
||||
|
||||
$this->subitem_add($iio);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of valid checkout options for this invoice
|
||||
@@ -136,6 +139,66 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
return array_keys($this->_render['SCHED']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of items to render, in appropriate order
|
||||
*/
|
||||
public function items_render() {
|
||||
$result = $track = array();
|
||||
|
||||
// If the invoice hasnt been saved, then we'll need to assign some fake IDs to the items
|
||||
$c = 0;
|
||||
if (! $this->_loaded)
|
||||
foreach ($this->_sub_items as $iio)
|
||||
$iio->id = 'FAKE:'.$c++;
|
||||
|
||||
// First work through our recurring schedule items
|
||||
$result['s'] = array();
|
||||
foreach ($this->recur_schedules() as $rs)
|
||||
foreach ($this->recur_schedule_services($rs) as $sid)
|
||||
foreach ($this->service_products($sid) as $pid) {
|
||||
// Get all the services with this recur schedule first, then get service charges with rs=NULL
|
||||
foreach ($this->_sub_items as $iio) {
|
||||
|
||||
// If this is not the recur schedule or service we are working with, skip it
|
||||
if ($iio->service_id != $sid OR $iio->product_id != $pid OR $iio->recurring_schedule != $rs OR in_array($iio->id,$track))
|
||||
continue;
|
||||
|
||||
// Ensure we dont process this item again
|
||||
array_push($track,$iio->id);
|
||||
|
||||
if (! $iio->void)
|
||||
array_push($result['s'],$iio);
|
||||
}
|
||||
|
||||
// Get all the items for this service with a NULL rs
|
||||
foreach ($this->_sub_items as $iio) {
|
||||
// If this is not the recur schedule or service we are working with, skip it
|
||||
if ($iio->service_id != $sid OR $iio->product_id != $pid OR ! is_null($iio->recurring_schedule) OR in_array($iio->id,$track))
|
||||
continue;
|
||||
|
||||
// Ensure we dont process this item again
|
||||
array_push($track,$iio->id);
|
||||
|
||||
if (! $iio->void)
|
||||
array_push($result['s'],$iio);
|
||||
}
|
||||
}
|
||||
|
||||
// Next get the items we havent already got
|
||||
$result['other'] = array();
|
||||
foreach ($this->_sub_items as $iio)
|
||||
if (! in_array($iio->id,$track))
|
||||
array_push($result['other'],$iio);
|
||||
|
||||
// Debug
|
||||
#foreach ($result['s'] as $iio)
|
||||
# echo Debug::vars(array('s'=>$iio->object()));
|
||||
#foreach ($result['other'] as $iio)
|
||||
# echo Debug::vars(array('o'=>$iio->object()));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a summary of all itemss on an invoice
|
||||
*/
|
||||
@@ -230,6 +293,38 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For a particular recurring schedule, get al lthe services
|
||||
*/
|
||||
private function recur_schedule_services($rs) {
|
||||
$result = array();
|
||||
|
||||
if (! $rs)
|
||||
return $result;
|
||||
|
||||
foreach ($this->_sub_items as $o)
|
||||
if ($o->service_id AND $o->recurring_schedule == $rs AND ! in_array($o->service_id,$result))
|
||||
array_push($result,$o->service_id);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the recurring schedules on an invoice
|
||||
* Exclude any NULL recurring schedules
|
||||
*/
|
||||
private function recur_schedules() {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->_sub_items as $o)
|
||||
if (! is_null($o->recurring_schedule) AND ! in_array($o->recurring_schedule,$result))
|
||||
array_push($result,$o->recurring_schedule);
|
||||
|
||||
sort($result);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the reminder value
|
||||
*/
|
||||
@@ -389,6 +484,29 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* For a particular service, get all the products
|
||||
*/
|
||||
private function service_products($sid) {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->_sub_items as $o)
|
||||
if ($o->product_id AND $o->service_id == $sid AND ! in_array($o->product_id,$result))
|
||||
array_push($result,$o->product_id);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function service_products_total($sid,$pid,$format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->_sub_items as $o)
|
||||
if ($o->service_id == $sid AND $o->product_id == $pid)
|
||||
$result += $o->total();
|
||||
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
public function set_remind($key,$value,$add=FALSE) {
|
||||
$x = $this->reminders;
|
||||
|
||||
|
Reference in New Issue
Block a user