Fix for invoice with sub-items
This commit is contained in:
@@ -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;
|
||||
|
Reference in New Issue
Block a user