diff --git a/modules/invoice/classes/Invoice.php b/modules/invoice/classes/Invoice.php index 0eb27783..f8d0bfb8 100644 --- a/modules/invoice/classes/Invoice.php +++ b/modules/invoice/classes/Invoice.php @@ -70,7 +70,7 @@ class Invoice { // Service Billing $iio->item_type = 0; - $this->_io->subitem_add($iio,$this->_io->account->country); + $this->_io->add_sub_item($iio); // Check if there are any charges $c = ORM::factory('Charge') @@ -89,7 +89,7 @@ class Invoice { $iio->date_stop = $co->date_orig; $iio->item_type = $co->type; - $this->_io->subitem_add($iio,$this->_io->account->country); + $this->_io->add_sub_item($iio); } return $this; diff --git a/modules/invoice/classes/Model/Invoice.php b/modules/invoice/classes/Model/Invoice.php index 18b5e712..52a8c5eb 100644 --- a/modules/invoice/classes/Model/Invoice.php +++ b/modules/invoice/classes/Model/Invoice.php @@ -43,7 +43,7 @@ class Model_Invoice extends ORM_OSB implements Cartable { // Items belonging to an invoice protected $_sub_items_load = array( - 'invoice_item'=>'service_id,item_type,date_start,date_stop', + 'invoice_item'=>array('service_id','item_type','date_start','date_stop'), ); protected $_compress_column = array( @@ -58,6 +58,12 @@ 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 */ @@ -403,17 +409,6 @@ class Model_Invoice extends ORM_OSB implements Cartable { return $this->saved(); } - /** - * Add an item to an invoice - */ - public function subitem_add(Model_Invoice_Item $iio,Model_Country $co,$taxed=FALSE) { - $iio->subitem_add($co,$taxed); - - array_push($this->_sub_items,$iio); - - $this->_sub_items_sorted = FALSE; - } - /** * Return a list of invoice items for this invoice. * @param type [CHARGE|CREDIT|ALL] diff --git a/modules/invoice/classes/Model/Invoice/Item.php b/modules/invoice/classes/Model/Invoice/Item.php index ca06ae6d..9df2a2a2 100644 --- a/modules/invoice/classes/Model/Invoice/Item.php +++ b/modules/invoice/classes/Model/Invoice/Item.php @@ -33,9 +33,38 @@ class Model_Invoice_Item extends ORM_OSB { // Items belonging to an invoice item protected $_sub_items_load = array( - 'tax'=>FALSE, + 'tax'=>array(), ); + /** + * Add tax to our item + * + * @param Model_Country the country to get the tax rates + * @param Boolean Is this price inc/ex tax + */ + public function add_sub_item(Model_Country $co,$taxed=FALSE) { + $orig = $this->subtotal(); + $tax = 0; + + foreach ($co->tax->find_all() as $to) { + $iito = ORM::factory('Invoice_Item_Tax'); + + $iito->tax_id = $to->id; + $iito->amount = Currency::round($taxed ? ($orig-$orig/(1+$to->rate)) : $orig*$to->rate); + + $tax += $iito->amount; + $this->subitem_add($iito); + } + + // If taxed, we need to reduce our base_rate to a pre-tax amount + if ($taxed) { + $this->price_base -= Currency::round($tax/$this->quantity,1); + + // If there is any rounding, we'll take it off the last IITO + $iito->amount += $orig-$this->total(); + } + } + // The total of all discounts public function discount() { return Currency::round($this->discount_amt); @@ -138,37 +167,6 @@ class Model_Invoice_Item extends ORM_OSB { return $this; } - /** - * Add tax to our item - * - * @param Model_Country the country to get the tax rates - * @param Boolean Is this price inc/ex tax - */ - public function subitem_add(Model_Country $co,$taxed=FALSE) { - $orig = $this->subtotal(); - $tax = 0; - - foreach ($co->tax->find_all() as $to) { - $iito = ORM::factory('Invoice_Item_Tax'); - - $iito->tax_id = $to->id; - $iito->amount = Currency::round($taxed ? ($orig-$orig/(1+$to->rate)) : $orig*$to->rate); - - $tax += $iito->amount; - array_push($this->_sub_items,$iito); - } - - // If taxed, we need to reduce our base_rate to a pre-tax amount - if ($taxed) { - $this->price_base -= Currency::round($tax/$this->quantity,1); - - // If there is any rounding, we'll take it off the last IITO - $iito->amount += $orig-$this->total(); - } - - $this->_sub_items_sorted = FALSE; - } - // This total of this item before discounts and taxes public function subtotal($format=FALSE) { $result = $this->price_base*$this->quantity;