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
|
||||
}
|
||||
|
||||
|
@@ -12,7 +12,7 @@
|
||||
* 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 implements Invoicable {
|
||||
class Model_Invoice_Item extends ORM_OSB {
|
||||
// Relationships
|
||||
protected $_belongs_to = array(
|
||||
'product'=>array(),
|
||||
@@ -38,11 +38,21 @@ class Model_Invoice_Item extends ORM_OSB implements Invoicable {
|
||||
),
|
||||
);
|
||||
|
||||
// Items belonging to an invoice
|
||||
private $subitems = array();
|
||||
private $subitems_loaded = FALSE;
|
||||
// Items belonging to an invoice item
|
||||
protected $_sub_items_load = array(
|
||||
'invoice_item_tax'=>FALSE,
|
||||
);
|
||||
|
||||
/**
|
||||
* 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;
|
||||
else
|
||||
return 'Unknown Item';
|
||||
}
|
||||
|
||||
/** INTERFACE REQUIREMENTS **/
|
||||
public function invoice_line() {
|
||||
if ($this->charge_id)
|
||||
return $this->charge->description.' '.$this->period();
|
||||
@@ -52,23 +62,6 @@ class Model_Invoice_Item extends ORM_OSB implements Invoicable {
|
||||
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);
|
||||
|
||||
return $this->load_sub_items();
|
||||
}
|
||||
|
||||
private function load_sub_items() {
|
||||
// Load our sub items
|
||||
if (! $this->subitems_loaded AND $this->loaded()) {
|
||||
$this->subitems['tax'] = $this->invoice_item_tax->find_all()->as_array();
|
||||
$this->subitems_loaded = TRUE;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Display a transaction number
|
||||
public function trannum() {
|
||||
return sprintf('%03s-%06s',$this->item_type,$this->id);
|
||||
@@ -125,7 +118,7 @@ class Model_Invoice_Item extends ORM_OSB implements Invoicable {
|
||||
}
|
||||
|
||||
public function total($format=FALSE) {
|
||||
$result = Currency::round($this->subtotal()+$this->tax()-$this->discount());
|
||||
$result = $this->void ? 0 : Currency::round($this->subtotal()+$this->tax()-$this->discount());
|
||||
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
@@ -205,9 +198,9 @@ class Model_Invoice_Item extends ORM_OSB implements Invoicable {
|
||||
if ($this->saved()) {
|
||||
$iito = ORM::factory('Invoice_Item_Tax');
|
||||
|
||||
if ($this->subitems_loaded) {
|
||||
if ($this->_sub_items) {
|
||||
foreach (array('tax') as $i)
|
||||
foreach ($this->subitems[$i] as $io)
|
||||
foreach ($this->_sub_items[$i] as $io)
|
||||
if ($io->changed())
|
||||
$io->save();
|
||||
|
||||
|
Reference in New Issue
Block a user