This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/invoice/classes/Model/Invoice/Item.php

244 lines
5.9 KiB
PHP
Raw Normal View History

2013-10-10 02:44:53 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides invoice item capabilities.
*
* @package Invoice
* @category Models
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*
* Column Definitions:
* + item_type: 0=MAIN Service Item,2=?,3=?,4=Connection/Setup,5=Excess Service Item,6=Change Service,126=Payment Fee,127=Late Fee
2013-10-10 02:44:53 +00:00
*/
class Model_Invoice_Item extends ORM_OSB {
2013-10-10 02:44:53 +00:00
// Relationships
protected $_belongs_to = array(
'product'=>array(),
'invoice'=>array(),
'service'=>array()
);
protected $_has_one = array(
'charge'=>array('far_key'=>'charge_id','foreign_key'=>'id')
);
protected $_has_many = array(
'invoice_item_tax'=>array('far_key'=>'id')
);
protected $_display_filters = array(
'date_orig'=>array(
array('Config::date',array(':value')),
),
'date_start'=>array(
array('Config::date',array(':value')),
),
'date_stop'=>array(
array('Config::date',array(':value')),
),
);
// Items belonging to an invoice item
protected $_sub_items_load = array(
'invoice_item_tax'=>FALSE,
);
2013-12-05 05:22:23 +00:00
// The total of all discounts
public function discount() {
return Currency::round($this->discount_amt);
}
2013-10-10 02:44:53 +00:00
2013-12-05 05:22:23 +00:00
// 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));
2013-06-13 13:35:19 +00:00
}
2013-12-05 05:22:23 +00:00
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;
2013-10-10 02:44:53 +00:00
}
2013-12-05 05:22:23 +00:00
/**
* 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;
2013-10-10 02:44:53 +00:00
2013-12-05 05:22:23 +00:00
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);
2013-10-10 02:44:53 +00:00
}
// Sum up the tax that applies to this invoice item
2013-06-13 13:35:19 +00:00
public function tax($format=FALSE) {
2013-10-10 02:44:53 +00:00
$result = 0;
2013-12-05 05:22:23 +00:00
foreach ($this->subitems() as $iito)
$result += $iito->amount;
2013-10-10 02:44:53 +00:00
2013-12-05 05:22:23 +00:00
return $format ? Currency::display($result) : Currency::round($result);
2013-10-10 02:44:53 +00:00
}
public function tax_items() {
$result = array();
2013-12-05 05:22:23 +00:00
foreach ($this->subitems() as $iito) {
if (! isset($result[$iit->tax_id]))
$result[$iit->tax_id] = 0;
2013-12-05 05:22:23 +00:00
$result[$iito->tax_id] += $iito->amount;
}
return $result;
}
2013-12-05 05:22:23 +00:00
/**
* The title for invoice items
*/
public function title() {
if ($this->product_id AND $this->service_id)
return $this->product_id ? sprintf('%s: %s',$this->product->title(),$this->service->name()) : $this->service->name;
else
return 'Unknown Item';
2013-10-10 02:44:53 +00:00
}
2013-12-05 05:22:23 +00:00
public function total($format=FALSE) {
$result = $this->void ? 0 : $this->subtotal()+$this->tax()-$this->discount();
return $format ? Currency::display($result) : Currency::round($result);
2013-10-10 02:44:53 +00:00
}
2013-12-05 05:22:23 +00:00
// Display a transaction number
public function trannum() {
return sprintf('%03s-%06s',$this->item_type,$this->id);
}
2013-06-13 13:35:19 +00:00
2013-12-05 05:22:23 +00:00
public function invoice_line() {
if ($this->charge_id)
return $this->charge->description.' '.$this->period();
elseif ($this->service_id)
return 'Service '.$this->period();
else
throw HTTP_Exception::factory(501,'Unable to render invoice item :id',array(':id'=>$this->id));
2013-10-10 02:44:53 +00:00
}
/**
* Name for an invoice item
2013-12-05 05:22:23 +00:00
* @deprecated, use StaticList_ItemType()
2013-10-10 02:44:53 +00:00
*/
public function name() {
switch ($this->item_type) {
case 0: return _('Service');
case 1: return _('Item');
case 2:
case 3:
case 4:
case 6:
case 126:
case 127: return _('Charge');
case 5: return $this->charge->description;
default: return _('Other');
}
}
/**
* Detail behind an invoice item
*/
public function detail() {
switch ($this->item_type) {
case 0: return '';
case 1: return _('Hardware');
case 2: return _('Service Relocation Fee');
case 3: return _('Service Change Fee');
case 4: return _('Service Connection Fee');
case 5: return sprintf('%s@%3.2f',$this->quantity,$this->price_base);
case 6: return _('Service Excess Fee');
case 125: return _('Payment Fee');
case 126: return _('Rounding');
case 127: return _('Late Payment Fee');
default: '';
}
}
public function invoice_detail_items() {
switch ($this->item_type) {
case 0:
return $this->service->details('invoice_detail_items');
case 4:
return array('Charge'=>_('Service Connection Fee'));
case 5:
return $this->charge->details('invoice_detail_items');
default:
return array('Item'=>$this->item_type);
}
}
}
?>