Updated charge, Invoice improvements and other minor fixes
This commit is contained in:
@@ -38,8 +38,9 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
);
|
||||
|
||||
// Items belonging to an invoice
|
||||
private $invoice_items = array();
|
||||
private $invoice_items_unsorted = TRUE;
|
||||
protected $_sub_items_load = array(
|
||||
'invoice_item'=>'service_id,item_type',
|
||||
);
|
||||
|
||||
/** INTERFACE REQUIREMENTS **/
|
||||
public function cart_item() {
|
||||
@@ -53,32 +54,21 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
return count(Cart::instance()->get($this->mid(),$this->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Intercept our object load, so that we can load our subitems
|
||||
*/
|
||||
protected function _load_values(array $values) {
|
||||
parent::_load_values($values);
|
||||
|
||||
if ($this->_loaded)
|
||||
$this->invoice_items = $this->invoice_item->find_all()->as_array();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an item to an invoice
|
||||
*/
|
||||
public function add_item(Model_Invoice_Item $iio=NULL) {
|
||||
// @todo This is the old calling of this function, which should be removed
|
||||
if (is_null($iio)) {
|
||||
$c = count($this->invoice_items);
|
||||
$c = count($this->_sub_items);
|
||||
|
||||
$this->invoice_items[$c] = ORM::factory('Invoice_Item');
|
||||
$this->_sub_items[$c] = ORM::factory('Invoice_Item');
|
||||
|
||||
return $this->invoice_items[$c];
|
||||
return $this->_sub_items[$c];
|
||||
}
|
||||
|
||||
array_push($this->invoice_items,$iio);
|
||||
array_push($this->_sub_items,$iio);
|
||||
$this->_sub_items_sorted = FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,7 +76,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
*
|
||||
* @param $period Return an Array of items for that period
|
||||
*/
|
||||
public function items_periods($period=NULL) {
|
||||
public function items_periods($period=FALSE) {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->items() as $ito) {
|
||||
@@ -94,15 +84,60 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
if ($ito->item_type != 0)
|
||||
continue;
|
||||
|
||||
if (is_null($period) AND ! in_array($ito->recurring_schedule,$result))
|
||||
if ($period === FALSE AND ! in_array($ito->recurring_schedule,$result))
|
||||
array_push($result,$ito->recurring_schedule);
|
||||
elseif ($ito->recurring_schedule == $period)
|
||||
array_push($result,$ito);
|
||||
|
||||
elseif ($ito->recurring_schedule == $period) {
|
||||
// If we have this service already, we'll skip
|
||||
if (! Object::in_array('service_id',$ito->service_id,$result))
|
||||
array_push($result,$ito);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the item titles that are on an invoice
|
||||
*
|
||||
* @param $title Return an Array of items for that title
|
||||
*/
|
||||
public function items_titles($title=NULL,$period=NULL) {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->items() as $ito) {
|
||||
if ($ito->service_id) {
|
||||
if (is_null($title) AND ! in_array($ito->title(),$result) AND (is_null($period) OR ($period == $ito->recurring_schedule)))
|
||||
array_push($result,$ito->title());
|
||||
elseif (($ito->title() == $title AND (is_null($period) OR ($period == $ito->recurring_schedule))) OR is_null($ito->recurring_schedule))
|
||||
array_push($result,$ito);
|
||||
|
||||
} else {
|
||||
throw HTTP_Exception::factory(501,'Not handled non-services');
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the service items on an invoice relating to an invoice_item
|
||||
* IE: item_type == 0
|
||||
*/
|
||||
public function service_items(Model_Invoice_Item $o) {
|
||||
$result = array();
|
||||
|
||||
// At the moment, we only return 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 extra items on an invoice relating to an invoice_item
|
||||
* IE: item_type != 0
|
||||
@@ -195,7 +230,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
public function items($type='ALL') {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->invoice_items as $ito) {
|
||||
foreach ($this->_sub_items as $ito) {
|
||||
$return = FALSE;
|
||||
|
||||
switch ($type) {
|
||||
@@ -429,9 +464,11 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
// Our items will be clobbered once we save the object, so we need to save it here.
|
||||
$items = $this->items();
|
||||
|
||||
// If this is a new invoice, we'll need to do more work
|
||||
$new = ! $this->loaded();
|
||||
|
||||
// Save the invoice
|
||||
if ($this->changed())
|
||||
parent::save($validation);
|
||||
parent::save($validation);
|
||||
|
||||
// Need to save the associated items and their taxes
|
||||
if ($this->loaded()) {
|
||||
@@ -453,6 +490,27 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
throw new Kohana_Exception('Problem saving invoice_item for invoice :invoice - Failed save()',array(':invoice'=>$this->id));
|
||||
}
|
||||
|
||||
// If this is a new invoice, we'll need to update some other items
|
||||
if ($new) {
|
||||
// Update next invoice date
|
||||
if ($iio->item_type == 0) {
|
||||
$iio->service->date_next_invoice = $iio->date_stop+86400;
|
||||
|
||||
// @todo Mark invoice as cancelled and write a memo, then...
|
||||
if (! $iio->service->save())
|
||||
throw new Kohana_Exception('Problem saving service :service for invoice_item :invoice_item - Failed save()',array(':invoice_item'=>$iio->id,':service'=>$iio->service_id));
|
||||
}
|
||||
|
||||
// Update charge iteme
|
||||
if ($iio->charge_id) {
|
||||
$iio->charge->processed = 1;
|
||||
|
||||
// @todo Mark invoice as cancelled and write a memo, then...
|
||||
if (! $iio->charge->save())
|
||||
throw new Kohana_Exception('Problem saving charge :charge for invoice_item :invoice_item - Failed save()',array(':invoice_item'=>$iio->id,':charge'=>$iio->charge_id));
|
||||
}
|
||||
}
|
||||
|
||||
// @todo Need to save discount information
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user