Improvements to taxing

This commit is contained in:
Deon George
2013-12-05 16:22:23 +11:00
parent 8ba487a4a6
commit 778eada7f0
13 changed files with 253 additions and 212 deletions

View File

@@ -43,6 +43,108 @@ class Model_Invoice_Item extends ORM_OSB {
'invoice_item_tax'=>FALSE,
);
// The total of all discounts
public function discount() {
return Currency::round($this->discount_amt);
}
// Display the period that a transaction applies
public function period() {
return ($this->date_start == $this->date_stop) ? Config::date($this->date_start) : sprintf('%s -> %s',Config::date($this->date_start),Config::date($this->date_stop));
}
public function save(Validation $validation = NULL) {
// Our items will be clobbered once we save the object, so we need to save it here.
$subitems = $this->subitems();
// Save the invoice item
parent::save($validation);
// Need to save the associated items and their taxes
if ($this->loaded()) {
foreach ($subitems as $iito) {
$iito->invoice_item_id = $this->id;
if (! $iito->changed())
continue;
$iito->save();
if (! $iito->saved()) {
$this->void = 1;
$this->save();
break;
}
}
} else
throw new Kohana_Exception('Couldnt save invoice_item for some reason?');
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;
return $format ? Currency::display($result) : Currency::round($result);
}
// Sum up the tax that applies to this invoice item
public function tax($format=FALSE) {
$result = 0;
foreach ($this->subitems() as $iito)
$result += $iito->amount;
return $format ? Currency::display($result) : Currency::round($result);
}
public function tax_items() {
$result = array();
foreach ($this->subitems() as $iito) {
if (! isset($result[$iit->tax_id]))
$result[$iit->tax_id] = 0;
$result[$iito->tax_id] += $iito->amount;
}
return $result;
}
/**
* The title for invoice items
*/
@@ -53,6 +155,17 @@ class Model_Invoice_Item extends ORM_OSB {
return 'Unknown Item';
}
public function total($format=FALSE) {
$result = $this->void ? 0 : $this->subtotal()+$this->tax()-$this->discount();
return $format ? Currency::display($result) : Currency::round($result);
}
// Display a transaction number
public function trannum() {
return sprintf('%03s-%06s',$this->item_type,$this->id);
}
public function invoice_line() {
if ($this->charge_id)
return $this->charge->description.' '.$this->period();
@@ -62,69 +175,9 @@ class Model_Invoice_Item extends ORM_OSB {
throw HTTP_Exception::factory(501,'Unable to render invoice item :id',array(':id'=>$this->id));
}
// Display a transaction number
public function trannum() {
return sprintf('%03s-%06s',$this->item_type,$this->id);
}
// Display the period that a transaction applies
public function period() {
if ($this->date_start == $this->date_stop)
return Config::date($this->date_start);
else
return sprintf('%s -> %s',Config::date($this->date_start),Config::date($this->date_stop));
}
// Sum up the tax that applies to this invoice item
public function tax($format=FALSE) {
$result = 0;
foreach ($this->invoice_item_tax->find_all() as $iit)
$result += $iit->amount;
// @todo This shouldnt be required.
if (! $result)
$result += round($this->price_base*$this->quantity*.1,2);
$result = Currency::round($result);
return $format ? Currency::display($result) : $result;
}
public function tax_items() {
$result = array();
foreach ($this->invoice_item_tax->find_all() as $iit) {
if (! isset($result[$iit->tax_id]))
$result[$iit->tax_id] = 0;
$result[$iit->tax_id] += $iit->amount;
}
return $result;
}
// This total of this item before discounts and taxes
public function subtotal($format=FALSE) {
$result = Currency::round($this->price_base*$this->quantity);
return $format ? Currency::display($result) : $result;
}
// The total of all discounts
public function discount() {
return Currency::round($this->discount_amt);
}
public function total($format=FALSE) {
$result = $this->void ? 0 : Currency::round($this->subtotal()+$this->tax()-$this->discount());
return $format ? Currency::display($result) : $result;
}
/**
* Name for an invoice item
* @deprecated, use StaticList_ItemType()
*/
public function name() {
switch ($this->item_type) {
@@ -186,40 +239,5 @@ class Model_Invoice_Item extends ORM_OSB {
return array('Item'=>$this->item_type);
}
}
public function save(Validation $validation = NULL) {
// Save the invoice item
parent::save($validation);
// Need to save the discounts associated with the invoice_item
if ($this->saved()) {
$iito = ORM::factory('Invoice_Item_Tax');
if ($this->_sub_items) {
foreach (array('tax') as $i)
foreach ($this->_sub_items[$i] as $io)
if ($io->changed())
$io->save();
// Add TAX details
} else
// @todo tax parameters should come from user session
foreach (Tax::detail(61,NULL,$this->subtotal()) as $tax) {
$iito->clear();
$iito->invoice_item_id = $this->id;
$iito->tax_id = $tax['id'];
// @todo Rounding here should come from a global config
$iito->amount = round($tax['amount'],2);
$iito->save();
if (! $iito->saved())
throw new Kohana_Exception('Couldnt save tax for some reason - failed save()?');
}
} else
throw new Kohana_Exception('Couldnt save invoice_item for some reason?');
return $this;
}
}
?>