Work on invoices, products and other minor things

This commit is contained in:
Deon George
2011-10-11 19:52:31 +11:00
parent 50a096e22a
commit 718c42be65
12 changed files with 393 additions and 257 deletions

View File

@@ -11,8 +11,6 @@
* @license http://dev.osbill.net/license.html
*/
class Model_Invoice extends ORMOSB {
private $invoice_items = array();
protected $_belongs_to = array(
'account'=>array()
);
@@ -28,20 +26,6 @@ class Model_Invoice extends ORMOSB {
'id'=>'DESC',
);
/**
* @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 $_display_filters = array(
'date_orig'=>array(
array('Config::date',array(':value')),
@@ -63,6 +47,9 @@ class Model_Invoice extends ORMOSB {
),
);
// Items belonging to an invoice
private $invoice_items = array();
/**
* Display the Invoice Number
*/
@@ -82,18 +69,20 @@ class Model_Invoice extends ORMOSB {
*/
public function due($format=FALSE) {
// If the invoice is active calculate the due amount
$result = $this->status ? round($this->total_amt-$this->credit_amt-$this->billed_amt,Kohana::config('config.currency_format')) : 0;
$result = $this->status ? round($this->total()-$this->payments_total(),2) : 0;
return $format ? Currency::display($result) : $result;
}
/**
* Return a list of invoice items for this invoice.
*
* We only return the items, if the invoice hasnt been changed.
*/
public function items() {
return ($this->loaded() AND ! $this->_changed) ? $this->invoice_item->order_by('service_id,item_type,module_id')->find_all() : NULL;
// If we havent been changed, we'll load the records from the DB.
if ($this->loaded() AND ! $this->_changed)
return $this->invoice_item->order_by('service_id,item_type,module_id')->find_all()->as_array();
else
return $this->invoice_items;
}
/**
@@ -102,8 +91,19 @@ class Model_Invoice extends ORMOSB {
public function subtotal($format=FALSE) {
$result = 0;
// @todo This rounding should be a system config.
foreach ($this->items() as $ito)
$result += $ito->subtotal();
$result += round($ito->subtotal(),2);
return $format ? Currency::display($result) : $result;
}
public function discount($format=FALSE) {
$result = 0;
// @todo This rounding should be a system config.
foreach ($this->items() as $ito)
$result += round($ito->discount_amt,2);
return $format ? Currency::display($result) : $result;
}
@@ -111,12 +111,9 @@ class Model_Invoice extends ORMOSB {
public function tax($format=FALSE) {
$result = 0;
// @todo This rounding should be a system config.
foreach ($this->items() as $ito)
$result += $ito->tax();
// @todo This should eventually be removed.
if (! $result AND $this->tax_amt)
$result = $this->tax_amt;
$result += round($ito->tax(),2);
return $format ? Currency::display($result) : $result;
}
@@ -127,11 +124,12 @@ class Model_Invoice extends ORMOSB {
public function total($format=FALSE) {
$result = 0;
// @todo This rounding should be a system config.
foreach ($this->items() as $ito)
$result += $ito->total();
$result += round($ito->total(),2);
// Reduce by any credits
$result -= $this->credit_amt;
$result -= round($this->credit_amt,2);
return $format ? Currency::display($result) : $result;
}
@@ -150,31 +148,75 @@ class Model_Invoice extends ORMOSB {
}
/**
* Return a list of our main invoice items (item_id=0)
* Get a list of services on an invoice
*
* We use this to list details by service on an invoice.
*/
public function items_main() {
return ($this->loaded() AND ! $this->_changed) ? $this->invoice_item->where('item_type','=',0)->find_all() : NULL;
public function items_services(array $items=array()) {
$result = array();
if (! $items)
$items = $this->items();
foreach ($items as $ito)
if ($ito->service_id AND empty($result[$ito->service_id]))
$result[$ito->service_id] = $ito->service;
return $result;
}
/**
* Return a list of our sub invoice items (item_id!=0)
* Return all invoice items for a service optionally by recurring schedule
*/
public function items_service($sid,$rs=NULL) {
$result = array();
$items = $this->items();
Sort::MAsort($items,'item_type');
foreach ($items as $ito)
if ($ito->service_id == $sid AND (is_null($rs) OR $ito->recurring_schedule == $rs))
array_push($result,$ito);
return $result;
}
/**
* Return a list of periods and services
*
* @param sid int Service ID
* This is so that we can list items summarised by billing period
*/
public function items_sub($sid) {
return ($this->loaded() AND ! $this->_changed) ? $this->invoice_item->where('service_id','=',$sid)->and_where('item_type','<>',0)->find_all() : NULL;
public function items_service_periods() {
$result = array();
$c = array();
foreach ($this->items() 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))
continue;
array_push($c,$ito->service_id);
$result[$ito->recurring_schedule][] = $ito;
}
return $result;
}
/**
* Summarise the items on an invoice
*
* We summaries based on product.
*/
public function items_summary() {
$result = array();
foreach ($this->items_main() as $ito) {
$unique = TRUE;
foreach ($this->items() as $ito) {
// We conly summaries item_type=0
if (! $ito->item_type == 0)
continue;
$t = $ito->product_id;
$t = $ito->product->name();
if (! isset($result[$t])) {
$result[$t]['quantity'] = 0;
$result[$t]['subtotal'] = 0;
@@ -187,22 +229,13 @@ class Model_Invoice extends ORMOSB {
return $result;
}
/**
* Find all the invoice items relating to a service
*
* @param int Service ID
*/
private function list_items_service($sid) {
return $this->invoice_item->where('service_id','=',$sid)->find_all();
}
/**
* Calculate the total for items for a service
*/
public function items_service_total($sid) {
$total = 0;
foreach ($this->list_items_service($sid) as $ito)
foreach ($this->items_service($sid) as $ito)
$total += $ito->total();
return $total;
@@ -210,41 +243,26 @@ class Model_Invoice extends ORMOSB {
/**
* Calculate the tax of items for a service
* @todo This can be optimised
*/
public function items_service_tax($sid) {
$total = 0;
foreach ($this->list_items_service($sid) as $ito)
$total += $ito->tax_amt;
foreach ($this->items_service($sid) as $ito)
$total += $ito->tax();
return $total;
}
// @todo Add discounts
/**
* Return a list of items based on a sort criteria
* Calculate the discounts of items for a service
*/
public function sorted_service_items($index) {
$summary = array();
public function items_service_discount($sid) {
$total = 0;
foreach ($this->items() as $ito) {
$key = $ito->service->$index;
foreach ($this->items_service($sid) as $ito)
$total += $ito->discount();
if (! isset($summary[$key]['items'])) {
$summary[$key]['items'] = array();
$summary[$key]['total'] = 0;
}
// Only record items with item_type=0
if ($ito->item_type == 0)
array_push($summary[$key]['items'],$ito);
$summary[$key]['total'] += $ito->total();
}
return $summary;
return $total;
}
/**
@@ -264,8 +282,8 @@ class Model_Invoice extends ORMOSB {
}
// @todo This should be removed eventually
if (! $summary AND $this->tax_amt)
$summary[1] = $this->tax_amt;
if (! $summary)
$summary[1] = $this->tax();
return $summary;
}
@@ -274,6 +292,9 @@ class Model_Invoice extends ORMOSB {
* Add an item to an invoice
*/
public function add_item() {
if ($this->loaded() and ! $this->invoice_items)
throw new Kohana_Exception('Need to load invoice_items?');
$c = count($this->invoice_items);
$this->invoice_items[$c] = ORM::factory('invoice_item');
@@ -281,26 +302,31 @@ class Model_Invoice extends ORMOSB {
return $this->invoice_items[$c];
}
public function min_due($date) {
// @todo This should be configurable;
return ($date < time()) ? time() : $date;
}
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();
// Save the invoice
parent::save($validation);
// Need to save the associated items and their taxes
if ($this->saved()) {
$tax_amt = 0;
$discount_amt = 0;
foreach ($items as $iio) {
$iio->invoice_id = $this->id;
foreach ($this->items() as $invoice_item) {
$invoice_item->invoice_id = $this->id;
if (! $invoice_item->check()) {
if (! $iio->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();
$iio->save();
if (! $invoice_item->saved()) {
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'=>$invoice->id));
}
@@ -313,28 +339,6 @@ class Model_Invoice extends ORMOSB {
throw new Kohana_Exception('Couldnt save invoice for some reason?');
}
public function calc_total(Validate $array, $field) {
$array[$field] = 0;
// @todo Rounding here should come from a global config
foreach ($this->invoice_items as $ito)
$array[$field] += round($ito->price_base+$ito->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 $ito)
$array[$field] += round(Tax::total(61,NULL,$ito->price_base+$ito->price_setup),2);
$this->_changed[$field] = $field;
}
/**
* Check the reminder value
*/
@@ -386,23 +390,30 @@ class Model_Invoice extends ORMOSB {
/** LIST FUNCTIONS **/
private function _list_due() {
// @todo This rounding should be a system configuration
return $this->where('round(total_amt-ifnull(credit_amt,0),2)','>','=billed_amt')
->and_where('status','=',1)
->order_by('due_date,account_id,id');
static $result = array();
if (! $result)
foreach (ORM::factory('invoice')->where('status','=',1)->find_all() as $io)
if ($io->due())
array_push($result,$io);
return $result;
}
/**
* Identify all the invoices that are due
*/
public function list_overdue($time=NULL) {
$result = array();
if (is_null($time))
$time = time();
// @todo This rounding should be a system configuration
return $this->_list_due()
->and_where('due_date','<=',$time)
->find_all();
foreach ($this->_list_due() as $io)
if ($io->due_date <= $time)
array_push($result,$io);
return $result;
}
/**
@@ -428,15 +439,16 @@ class Model_Invoice extends ORMOSB {
* Return a list of invoices that are due, excluding overdue.
*/
public function list_due($time=NULL) {
if (is_null($time))
return $this->_list_due()
->and_where('due_date','>',time())
->find_all();
else
return $this->_list_due()
->and_where('due_date','<=',$time)
->and_where('due_date','>',time())
->find_all();
$result = array();
foreach ($this->_list_due() as $io)
if ($io->due_date > time())
if (is_null($time))
array_push($result,$io);
elseif ($this->due_date <= $time)
array_push($result,$io);
return $result;
}
}
?>

View File

@@ -32,7 +32,6 @@ class Model_Invoice_Item extends ORMOSB {
),
);
// Display a transaction number
public function trannum() {
return sprintf('%03s-%06s',$this->item_type,$this->id);
@@ -41,7 +40,7 @@ class Model_Invoice_Item extends ORMOSB {
// 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));
return Config::date($this->date_start);
else
return sprintf('%s -> %s',Config::date($this->date_start),Config::date($this->date_stop));
@@ -49,17 +48,21 @@ class Model_Invoice_Item extends ORMOSB {
// Sum up the tax that applies to this invoice item
public function tax() {
$amount = 0;
$result = 0;
foreach ($this->invoice_item_tax->find_all() as $iit)
$amount += $iit->amount;
$result += $iit->amount;
return $amount;
// @todo This shouldnt be required.
if (! $result)
$result += round($this->subtotal() *.1,2);
return $result;
}
// This total of this item before discounts and taxes
public function subtotal() {
return ($this->price_base)*$this->quantity;
return $this->price_base*$this->quantity;
}
// The total of all discounts
@@ -72,6 +75,15 @@ class Model_Invoice_Item extends ORMOSB {
return round($this->subtotal()+$this->tax()-$this->discount(),2);
}
public function name() {
return $this->product_name ? $this->product_name : ($this->item_type == 0 ? _('Service') : _('Other'));
}
public function detail() {
return ($this->item_type == 0 OR $this->quantity == 1) ? HTML::nbsp('') : sprintf('%s@%3.2f',$this->quantity,$this->price_base);
}
// @todo This might not be required.
public function invoice_detail_items() {
if ($this->item_type != 0)
return;
@@ -81,36 +93,29 @@ class Model_Invoice_Item extends ORMOSB {
public function save(Validation $validation = NULL) {
// Save the invoice item
parent::save();
parent::save($validation);
// 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;
$iito = ORM::factory('invoice_item_tax');
// 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;
$iito->clear();
$iito->invoice_item_id = $this->id;
$iito->tax_id = $tax['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'];
$iito->amount = round($tax['amount'],2);
if (! $invoice_item_tax->check())
if (! $iito->check())
throw new Kohana_Exception('Couldnt save tax for some reason - failed check()?');
$invoice_item_tax->save();
$iito->save();
if (! $invoice_item_tax->saved())
if (! $iito->saved())
throw new Kohana_Exception('Couldnt save tax for some reason - failed save()?');
}
// Save DISCOUNT details
// @todo calculate discounts
parent::save();
} else
throw new Kohana_Exception('Couldnt save invoice_item for some reason?');
}