Improved service display

This commit is contained in:
Deon George
2011-07-14 19:09:03 +10:00
parent 46c3b9a075
commit 27cdab1fe4
37 changed files with 1319 additions and 1042 deletions

View File

@@ -37,19 +37,31 @@ class Model_Invoice extends ORMOSB {
'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()),
protected $_display_filters = array(
'date_orig'=>array(
array('Config::date',array(':value')),
),
'due_date'=>array(
array('Config::date',array(':value')),
),
'billed_amt'=>array(
array('Currency::display',array(':value')),
),
'credit_amt'=>array(
array('Currency::display',array(':value')),
),
'status'=>array(
array('StaticList_YesNo::display',array(':value')),
),
'total_amt'=>array(
array('Currency::display',array(':value')),
),
);
/**
* Display the Invoice Number
*/
public function invnum() {
public function id() {
return sprintf('%06s',$this->id);
}
@@ -64,127 +76,115 @@ class Model_Invoice extends ORMOSB {
* Display the amount due
*/
public function due($format=FALSE) {
$result = 0;
// If the invoice is active calculate the due amount
if ($this->status)
// @todo This rounding should be a system setting
$result = round($this->total_amt-$this->credit_amt-$this->billed_amt,2);
$result = $this->status ? round($this->total_amt-$this->credit_amt-$this->billed_amt,Kohana::config('config.currency_format')) : 0;
if ($format)
return Currency::display($result);
else
return $result;
}
/**
* Return a total invoices overdue excluding this invoice
*/
public function other_due($format=FALSE) {
$result = $this->account->invoices_due_total()-$this->due();
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 $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() {
// 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();
return ($this->loaded() AND ! $this->_changed) ? $this->invoice_item->order_by('service_id,item_type,module_id')->find_all() : NULL;
}
echo kohana::debug(array('BEFORE'=>$this->invoice_item));
if (! $this->invoice_items)
$this->invoice_items = $this->invoice_item;
/**
* Return the subtotal of all items
*/
public function subtotal($format=FALSE) {
$result = 0;
echo kohana::debug(array('AFTER'=>$this->invoice_items));die();
return $this->invoice_items;
foreach ($this->items() as $ito)
$result += $ito->subtotal();
return $format ? Currency::display($result) : $result;
}
public function tax($format=FALSE) {
$result = 0;
foreach ($this->items() as $ito)
$result += $ito->tax_amt;
return $format ? Currency::display($result) : $result;
}
/**
* Return the total of all items
*/
public function total($format=FALSE) {
$result = 0;
foreach ($this->items() as $ito)
$result += $ito->total();
// Reduce by any credits
$result -= $this->credit_amt;
return $format ? Currency::display($result) : $result;
}
public function payments() {
return ($this->loaded() AND ! $this->_changed) ? $this->payments->find_all() : NULL;
}
public function payments_total($format=FALSE) {
$result = 0;
foreach ($this->payments() as $po)
$result += $po->total_amt;
return $format ? Currency::display($result) : $result;
}
/**
* 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 ($this->loaded() AND ! $this->_changed) ? $this->invoice_item->where('item_type','=',0)->find_all() : NULL;
}
/**
* Return a list of our sub invoice items (item_id!=0)
*
* @param sid int Service ID
*/
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();
return ($this->loaded() AND ! $this->_changed) ? $this->invoice_item->where('service_id','=',$sid)->and_where('item_type','<>',0)->find_all() : NULL;
}
/**
* Summarise the items on an invoice
*/
public function items_summary() {
$sum = array();
$result = array();
foreach ($this->items_main() as $item) {
foreach ($this->items_main() as $ito) {
$unique = TRUE;
# Unique line item
if (isset($sum[$item->product->sku])) {
# Is unique price/attributes?
foreach ($sum[$item->product->sku] as $sid => $flds) {
if ($flds->price_base == $item->price_base &&
$flds->price_setup == $item->price_setup) {
$sum[$item->product->sku][$sid]->quantity += $item->quantity;
$unique = FALSE;
break;
}
}
$t = $ito->product->summary();
if (! isset($result[$t])) {
$result[$t]['quantity'] = 0;
$result[$t]['subtotal'] = 0;
}
# Unique line item
if ($unique)
$sum[$item->product->sku][] = $item;
$result[$t]['quantity'] += $ito->quantity;
$result[$t]['subtotal'] += $ito->subtotal();
}
if (count($sum)) {
$items = array();
foreach ($sum as $sku => $item)
foreach ($item as $sitem)
array_push($items,$sitem);
return $result;
}
return $items;
}
/**
* Find all the invoice items relating to a service
*
* @param int Service ID
*/
private function items_service($sid) {
return $this->invoice_item->where('service_id','=',$sid)->find_all();
}
/**
@@ -193,8 +193,8 @@ class Model_Invoice extends ORMOSB {
public function items_service_total($sid) {
$total = 0;
foreach ($this->invoice_item->where('service_id','=',$sid)->find_all() as $item)
$total += $item->total();
foreach ($this->items_service($sid) as $ito)
$total += $ito->total();
return $total;
}
@@ -205,8 +205,8 @@ class Model_Invoice extends ORMOSB {
public function items_service_tax($sid) {
$total = 0;
foreach ($this->invoice_item->where('service_id','=',$sid)->find_all() as $item)
$total += $item->tax_amt;
foreach ($this->items_service($sid) as $ito)
$total += $ito->tax_amt;
return $total;
}
@@ -217,8 +217,8 @@ class Model_Invoice extends ORMOSB {
public function sorted_service_items($index) {
$summary = array();
foreach ($this->items() as $item) {
$key = $item->service->$index;
foreach ($this->items() as $ito) {
$key = $ito->service->$index;
if (! isset($summary[$key]['items'])) {
$summary[$key]['items'] = array();
@@ -226,10 +226,10 @@ class Model_Invoice extends ORMOSB {
}
// Only record items with item_type=0
if ($item->item_type == 0)
array_push($summary[$key]['items'],$item);
if ($ito->item_type == 0)
array_push($summary[$key]['items'],$ito);
$summary[$key]['total'] += $item->total();
$summary[$key]['total'] += $ito->total();
}
return $summary;
@@ -241,8 +241,8 @@ class Model_Invoice extends ORMOSB {
public function tax_summary() {
$summary = array();
foreach ($this->items() as $item) {
foreach ($item->invoice_item_tax->find_all() as $item_tax) {
foreach ($this->items() 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;
else
@@ -253,6 +253,9 @@ class Model_Invoice extends ORMOSB {
return $summary;
}
/**
* Add an item to an invoice
*/
public function add_item() {
$c = count($this->invoice_items);
@@ -299,8 +302,8 @@ class Model_Invoice extends ORMOSB {
$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);
foreach ($this->invoice_items as $ito)
$array[$field] += round($ito->price_base+$ito->price_setup,2);
$this->_changed[$field] = $field;
}
@@ -311,8 +314,8 @@ class Model_Invoice extends ORMOSB {
// @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);
foreach ($this->invoice_items as $ito)
$array[$field] += round(Tax::total(61,NULL,$ito->price_base+$ito->price_setup),2);
$this->_changed[$field] = $field;
}

View File

@@ -32,16 +32,14 @@ class Model_Invoice_Item extends ORMOSB {
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));
return sprintf('%s -> %s',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 invoice_detail_items() {
if ($this->item_type != 0)
return;
return $this->service->details('invoice');
}
public function subtotal() {