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
2011-05-03 09:49:04 +10:00

94 lines
2.6 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides invoice item capabilities.
*
* @package OSB
* @subpackage Invoice
* @category Models
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Invoice_Item extends ORMOSB {
protected $_belongs_to = array(
'product'=>array(),
'invoice'=>array(),
'service'=>array()
);
protected $_has_many = array(
'invoice_item_tax'=>array()
);
protected $_updated_column = FALSE; // @todo No update columns
// 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 sprintf('%s: %s',_('Date'),Config::date($this->date_start));
else
return sprintf('%s: %s -> %s',_('Period'),Config::date($this->date_start),Config::date($this->date_stop));
}
/**
* On invoices where there are multiple charges for the same item
* (eg spanning the next invoice period), this should show the period
* range for the additional sub-items.
*/
public function invoice_display() {
return $this->period();
}
public function subtotal() {
return ($this->price_base+$this->price_setup)*$this->quantity;
}
public function total() {
return ($this->price_base+$this->price_setup)*$this->quantity+$this->tax_amt-$this->discount_amt;
}
public function save() {
// Save the invoice item
parent::save();
// Need to save the taxes and discounts associated with the invoice_item
if ($this->saved()) {
$invoice_item_tax = ORM::factory('invoice_item_tax');
$tax_total = 0;
// Save TAX details
// @todo tax parameters should come from user session
foreach (Tax::detail(61,NULL,$this->subtotal()) as $tax) {
$invoice_item_tax->clear();
$invoice_item_tax->invoice_item_id = $this->id;
// @todo Rounding here should come from a global config
$tax_total += ($invoice_item_tax->amount = round($tax['amount'],2));
$invoice_item_tax->tax_id = $tax['id'];
if (! $invoice_item_tax->check())
throw new Kohana_Exception('Couldnt save tax for some reason - failed check()?');
$invoice_item_tax->save();
if (! $invoice_item_tax->saved())
throw new Kohana_Exception('Couldnt save tax for some reason - failed save()?');
}
// Save DISCOUNT details
// @todo calculate discounts
$this->tax_amt = $tax_total;
$this->total_amt = $this->total();
parent::save();
} else
throw new Kohana_Exception('Couldnt save invoice_item for some reason?');
}
}
?>