OSB enhancements to date

This commit is contained in:
Deon George
2010-11-30 09:41:08 +11:00
parent 8715a2059b
commit ec6a542bc3
478 changed files with 23423 additions and 9309 deletions

View File

@@ -0,0 +1,267 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides invoice 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 extends ORMOSB {
private $invoice_items = array();
protected $_belongs_to = array(
'account'=>array()
);
protected $_has_many = array(
'invoice_item'=>array(),
'invoice_item_tax'=>array(),
'service'=>array('through'=>'invoice_item'),
'payment'=>array('through'=>'payment_item'),
);
/**
* @var array Filters to render values properly
*/
protected $_filters = array(
// @todo This rounding should be a global configuration
'total_amt'=>array('round'=>array('2')),
);
protected $_callbacks = array(
'id'=>array('get_next_id'),
'total_amt'=>array('calc_total'),
'tax_amt'=>array('calc_tax'),
);
protected $_formats = array(
'date_orig'=>array('Config::date'=>array()),
'due_date'=>array('Config::date'=>array()),
'billed_amt'=>array('Currency::display'=>array()),
'credit_amt'=>array('Currency::display'=>array()),
'status'=>array('StaticList_YesNo::display'=>array()),
'total_amt'=>array('Currency::display'=>array()),
);
/**
* Display the Invoice Number
*/
public function invnum() {
return sprintf('%06s',$this->id);
}
/**
* Display the Invoice Reference Number
*/
public function refnum() {
return sprintf('%02s-%04s-%06s',Config::siteid(),$this->account_id,$this->id);
}
/**
* Display the amount due
*/
public function due($format=FALSE) {
$result = 0;
// If the invoice is active calculate the due amount
if ($this->status)
$result = $this->total_amt-$this->credit_amt-$this->billed_amt;
if ($format)
return Currency::display($result);
else
return $result;
}
public function subtotal($format=FALSE) {
$result = 0;
foreach ($this->items() as $item)
$result += $item->subtotal();
if ($format)
return Currency::display($result);
else
return $result;
}
public function total($format=FALSE) {
$result = 0;
foreach ($this->items() as $item)
$result += $item->total();
// Reduce any credits
$result -= $this->credit_amt;
if ($format)
return Currency::display($result);
else
return $result;
}
/**
* Return a list of invoice items for this invoice.
*/
public function items() {
// Get our invoice items for an existing invoice
if ($this->id AND $this->loaded() AND ! $this->_changed)
return $this->invoice_item->order_by('service_id,item_type,module_id')->find_all();
echo kohana::debug(array('BEFORE'=>$this->invoice_item));
if (! $this->invoice_items)
$this->invoice_items = $this->invoice_item;
echo kohana::debug(array('AFTER'=>$this->invoice_items));die();
return $this->invoice_items;
}
/**
* Return a list of our main invoice items (item_id=0)
*/
public function items_main() {
if ($this->id AND $this->loaded() AND ! $this->_changed)
return $this->invoice_item->where('item_type','=',0)->find_all();
}
/**
* Return a list of our sub invoice items (item_id!=0)
*/
public function items_sub($sid) {
if ($this->id AND $this->loaded() AND ! $this->_changed)
return $this->invoice_item->where('service_id','=',$sid)->and_where('item_type','<>',0)->find_all();
}
/**
* Calculate the total for items for a service
*/
public function items_service_total($sid) {
$total = 0;
foreach ($this->invoice_item->where('service_id','=',$sid)->find_all() as $item)
$total += $item->total();
return $total;
}
/**
* Calculate the tax of items for a service
*/
public function items_service_tax($sid) {
$total = 0;
foreach ($this->invoice_item->where('service_id','=',$sid)->find_all() as $item)
$total += $item->tax_amt;
return $total;
}
/**
* Return a list of items based on a sort criteria
*/
public function sorted_service_items($index) {
$summary = array();
foreach ($this->items() as $item) {
$key = $item->service->$index;
if (! isset($summary[$key]['items'])) {
$summary[$key]['items'] = array();
$summary[$key]['total'] = 0;
}
// Only record items with item_type=0
if ($item->item_type == 0)
array_push($summary[$key]['items'],$item);
$summary[$key]['total'] += $item->total();
}
return $summary;
}
/**
* Return a list of taxes used on this invoice
*/
public function tax_summary() {
$summary = array();
foreach ($this->items() as $item) {
foreach ($item->invoice_item_tax->find_all() as $item_tax) {
if (! isset($summary[$item_tax->tax_id]))
$summary[$item_tax->tax_id] = $item_tax->amount;
else
$summary[$item_tax->tax_id] += $item_tax->amount;
}
}
return $summary;
}
public function add_item() {
$c = count($this->invoice_items);
$this->invoice_items[$c] = ORM::factory('invoice_item');
return $this->invoice_items[$c];
}
public function save() {
// Save the invoice
parent::save();
// Need to save the associated items and their taxes
if ($this->saved()) {
$tax_amt = 0;
$discount_amt = 0;
foreach ($this->items() as $invoice_item) {
$invoice_item->invoice_id = $this->id;
if (! $invoice_item->check()) {
// @todo Mark invoice as cancelled and write a memo, then...
throw new Kohana_Exception('Problem saving invoice_item for invoice :invoice - Failed check()',array(':invoice'=>$invoice->id));
}
$invoice_item->save();
if (! $invoice_item->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'=>$invoice->id));
}
// @todo Need to save tax information
// @todo Need to save discount information
}
} else
throw new Kohana_Exception('Couldnt save invoice for some reason?');
echo Kohana::debug(array('saved'=>$this));
}
public function calc_total(Validate $array, $field) {
$array[$field] = 0;
// @todo Rounding here should come from a global config
foreach ($this->invoice_items as $item)
$array[$field] += round($item->price_base+$item->price_setup,2);
$this->_changed[$field] = $field;
}
public function calc_tax(Validate $array, $field) {
$array[$field] = 0;
// @todo Rounding here should come from a global config
// @todo tax should be evaluated per item
// @todo tax parameters should come from user session
foreach ($this->invoice_items as $item)
$array[$field] += round(Tax::total(61,NULL,$item->price_base+$item->price_setup),2);
$this->_changed[$field] = $field;
}
}
?>

View File

@@ -0,0 +1,93 @@
<?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?');
}
}
?>