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);
|
||||
|
Reference in New Issue
Block a user