Improvements to taxing
This commit is contained in:
@@ -55,23 +55,6 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
return count(Cart::instance()->get($this->mid(),$this->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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->_sub_items);
|
||||
|
||||
$this->_sub_items[$c] = ORM::factory('Invoice_Item');
|
||||
|
||||
return $this->_sub_items[$c];
|
||||
}
|
||||
|
||||
array_push($this->_sub_items,$iio);
|
||||
$this->_sub_items_sorted = FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the recurring schedules that are on an invoice
|
||||
*
|
||||
@@ -80,7 +63,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
public function items_periods($period=FALSE) {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->items() as $ito) {
|
||||
foreach ($this->subitems() as $ito) {
|
||||
// We are only interested in item_type=0
|
||||
if ($ito->item_type != 0)
|
||||
continue;
|
||||
@@ -106,7 +89,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
public function items_titles($title=NULL,$period=NULL) {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->items() as $ito) {
|
||||
foreach ($this->subitems() 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());
|
||||
@@ -156,7 +139,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
if (! $o->service_id)
|
||||
return $result;
|
||||
|
||||
foreach ($this->items() as $iio)
|
||||
foreach ($this->subitems() as $iio)
|
||||
if ($iio->item_type == 0 AND $iio->service_id == $o->service_id)
|
||||
array_push($result,$iio);
|
||||
|
||||
@@ -174,7 +157,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
if (! $o->service_id)
|
||||
return $result;
|
||||
|
||||
foreach ($this->items() as $iio)
|
||||
foreach ($this->subitems() as $iio)
|
||||
if ($iio->item_type != 0 AND $iio->service_id == $o->service_id)
|
||||
array_push($result,$iio);
|
||||
|
||||
@@ -191,7 +174,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
if (! $o->service_id)
|
||||
return $result;
|
||||
|
||||
foreach ($this->items() as $iio)
|
||||
foreach ($this->subitems() as $iio)
|
||||
if ($iio->service_id == $o->service_id)
|
||||
$result += $iio->tax();
|
||||
|
||||
@@ -208,13 +191,63 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
if (! $o->service_id)
|
||||
return $result;
|
||||
|
||||
foreach ($this->items() as $iio)
|
||||
foreach ($this->subitems() as $iio)
|
||||
if ($iio->service_id == $o->service_id)
|
||||
$result += $iio->total();
|
||||
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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]
|
||||
* @see invoice_items
|
||||
*/
|
||||
public function subitems($type='ALL') {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->_sub_items as $ito) {
|
||||
// Ignore voided items
|
||||
if ($ito->void)
|
||||
continue;
|
||||
|
||||
$return = FALSE;
|
||||
|
||||
switch ($type) {
|
||||
case 'CHARGE':
|
||||
if ($ito->product_id OR $ito->quantity > 0)
|
||||
$return = TRUE;
|
||||
break;
|
||||
|
||||
case 'CREDIT':
|
||||
if (! $ito->product_id AND $ito->quantity < 0)
|
||||
$return = TRUE;
|
||||
break;
|
||||
|
||||
case 'ALL':
|
||||
default:
|
||||
$return = TRUE;
|
||||
break;
|
||||
}
|
||||
|
||||
if ($return)
|
||||
array_push($result,$ito);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of valid checkout options for this invoice
|
||||
*/
|
||||
@@ -247,45 +280,6 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
return sprintf('%06s',$this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of invoice items for this invoice.
|
||||
* @param type [CHARGE|CREDIT|ALL]
|
||||
* @see invoice_items
|
||||
*/
|
||||
public function items($type='ALL') {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->_sub_items as $ito) {
|
||||
// Ignore voided items
|
||||
if ($ito->void)
|
||||
continue;
|
||||
|
||||
$return = FALSE;
|
||||
|
||||
switch ($type) {
|
||||
case 'CHARGE':
|
||||
if ($ito->product_id OR $ito->quantity > 0)
|
||||
$return = TRUE;
|
||||
break;
|
||||
|
||||
case 'CREDIT':
|
||||
if (! $ito->product_id AND $ito->quantity < 0)
|
||||
$return = TRUE;
|
||||
break;
|
||||
|
||||
case 'ALL':
|
||||
default:
|
||||
$return = TRUE;
|
||||
break;
|
||||
}
|
||||
|
||||
if ($return)
|
||||
array_push($result,$ito);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide a sorted list of items by an index
|
||||
*/
|
||||
@@ -296,7 +290,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
if (! $this->_changed AND array_key_exists($index,$result))
|
||||
return $result[$index];
|
||||
|
||||
foreach ($this->items() as $ito) {
|
||||
foreach ($this->subitems() as $ito) {
|
||||
switch ($index) {
|
||||
case 'account':
|
||||
if (! $ito->service_id)
|
||||
@@ -332,7 +326,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
public function items_services(array $items=array()) {
|
||||
$result = array();
|
||||
if (! $items)
|
||||
$items = $this->items();
|
||||
$items = $this->subitems();
|
||||
|
||||
foreach ($items as $ito)
|
||||
if ($ito->service_id AND empty($result[$ito->service_id]))
|
||||
@@ -344,7 +338,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
// @todo to retire
|
||||
public function items_invoice() {
|
||||
$result = array();
|
||||
$items = $this->items();
|
||||
$items = $this->subitems();
|
||||
|
||||
foreach ($items as $ito)
|
||||
if (! $ito->service_id AND empty($result[$ito->id]))
|
||||
@@ -377,7 +371,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
$result = array();
|
||||
|
||||
$c = array();
|
||||
foreach ($this->items() as $ito)
|
||||
foreach ($this->subitems() as $ito)
|
||||
if ($ito->service_id) {
|
||||
// If we have already covered a service with no recurring_schedule
|
||||
if (! $ito->recurring_schedule AND in_array($ito->service_id,$c))
|
||||
@@ -400,7 +394,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
public function items_summary() {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->items() as $ito) {
|
||||
foreach ($this->subitems() as $ito) {
|
||||
// We only summarise item_type=0
|
||||
if (! $ito->item_type == 0)
|
||||
continue;
|
||||
@@ -491,7 +485,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
|
||||
public function save(Validation $validation = NULL) {
|
||||
// Our items will be clobbered once we save the object, so we need to save it here.
|
||||
$items = $this->items();
|
||||
$subitems = $this->subitems();
|
||||
|
||||
// If this is a new invoice, we'll need to do more work
|
||||
$new = ! $this->loaded();
|
||||
@@ -501,7 +495,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
|
||||
// Need to save the associated items and their taxes
|
||||
if ($this->loaded()) {
|
||||
foreach ($items as $iio) {
|
||||
foreach ($subitems as $iio) {
|
||||
$iio->invoice_id = $this->id;
|
||||
|
||||
if (! $iio->changed())
|
||||
@@ -510,8 +504,10 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
$iio->save();
|
||||
|
||||
if (! $iio->saved()) {
|
||||
// @todo Mark invoice as cancelled and write a memo, then...
|
||||
throw new Kohana_Exception('Problem saving invoice_item for invoice :invoice - Failed save()',array(':invoice'=>$this->id));
|
||||
$this->void = 1;
|
||||
$this->save();
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// If this is a new invoice, we'll need to update some other items
|
||||
@@ -573,7 +569,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
public function subtotal($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->items() as $ito)
|
||||
foreach ($this->subitems() as $ito)
|
||||
$result += $ito->subtotal();
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
@@ -582,7 +578,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
public function tax($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->items() as $ito)
|
||||
foreach ($this->subitems() as $ito)
|
||||
$result += $ito->tax();
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
@@ -595,7 +591,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
public function tax_summary() {
|
||||
$summary = array();
|
||||
|
||||
foreach ($this->items() as $ito) {
|
||||
foreach ($this->subitems() as $ito) {
|
||||
foreach ($ito->invoice_item_tax->find_all() as $item_tax) {
|
||||
if (! isset($summary[$item_tax->tax_id]))
|
||||
$summary[$item_tax->tax_id] = $item_tax->amount;
|
||||
@@ -618,7 +614,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
$result = 0;
|
||||
|
||||
// This will include charges and credits
|
||||
foreach ($this->items() as $ito)
|
||||
foreach ($this->subitems() as $ito)
|
||||
$result += $ito->total();
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
@@ -627,7 +623,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
public function total_charges($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->items('CHARGE') as $ito)
|
||||
foreach ($this->subitems('CHARGE') as $ito)
|
||||
$result += $ito->subtotal()+$ito->tax();
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
@@ -636,7 +632,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
public function total_credits($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->items('CREDIT') as $ito)
|
||||
foreach ($this->subitems('CREDIT') as $ito)
|
||||
$result += ($ito->subtotal()+$ito->tax())*-1;
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
@@ -645,7 +641,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
public function total_discounts($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->items() as $ito)
|
||||
foreach ($this->subitems() as $ito)
|
||||
$result += $ito->discount();
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
Reference in New Issue
Block a user