Fix for invoice with sub-items

This commit is contained in:
Deon George 2016-07-27 16:55:07 +10:00
parent 5ab2d6205f
commit f426502707
3 changed files with 39 additions and 46 deletions

View File

@ -70,7 +70,7 @@ class Invoice {
// Service Billing // Service Billing
$iio->item_type = 0; $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 // Check if there are any charges
$c = ORM::factory('Charge') $c = ORM::factory('Charge')
@ -89,7 +89,7 @@ class Invoice {
$iio->date_stop = $co->date_orig; $iio->date_stop = $co->date_orig;
$iio->item_type = $co->type; $iio->item_type = $co->type;
$this->_io->subitem_add($iio,$this->_io->account->country); $this->_io->add_sub_item($iio);
} }
return $this; return $this;

View File

@ -43,7 +43,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
// Items belonging to an invoice // Items belonging to an invoice
protected $_sub_items_load = array( 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( 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()); 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 * Return if this invoice is already in the cart
*/ */
@ -403,17 +409,6 @@ class Model_Invoice extends ORM_OSB implements Cartable {
return $this->saved(); 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. * Return a list of invoice items for this invoice.
* @param type [CHARGE|CREDIT|ALL] * @param type [CHARGE|CREDIT|ALL]

View File

@ -33,9 +33,38 @@ class Model_Invoice_Item extends ORM_OSB {
// Items belonging to an invoice item // Items belonging to an invoice item
protected $_sub_items_load = array( 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 // The total of all discounts
public function discount() { public function discount() {
return Currency::round($this->discount_amt); return Currency::round($this->discount_amt);
@ -138,37 +167,6 @@ class Model_Invoice_Item extends ORM_OSB {
return $this; 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 // This total of this item before discounts and taxes
public function subtotal($format=FALSE) { public function subtotal($format=FALSE) {
$result = $this->price_base*$this->quantity; $result = $this->price_base*$this->quantity;