Work on invoice printing - to clean up

This commit is contained in:
Deon George
2011-05-02 22:28:17 +10:00
parent 2f7a10804e
commit 8013aadc4c
16 changed files with 1045 additions and 73 deletions

View File

@@ -57,7 +57,7 @@ class Model_Invoice extends ORMOSB {
* Display the Invoice Reference Number
*/
public function refnum() {
return sprintf('%02s-%04s-%06s',Config::siteid(),$this->account_id,$this->id);
return sprintf('%s-%06s',$this->account->accnum(),$this->id);
}
/**
@@ -67,7 +67,20 @@ class Model_Invoice extends ORMOSB {
$result = 0;
// If the invoice is active calculate the due amount
if ($this->status)
$result = $this->total_amt-$this->credit_amt-$this->billed_amt;
// @todo This rounding should be a system setting
$result = round($this->total_amt-$this->credit_amt-$this->billed_amt,2);
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);
@@ -134,6 +147,46 @@ class Model_Invoice extends ORMOSB {
return $this->invoice_item->where('service_id','=',$sid)->and_where('item_type','<>',0)->find_all();
}
/**
* Summarise the items on an invoice
*/
public function items_summary() {
$sum = array();
foreach ($this->items_main() as $item) {
$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;
}
}
}
# Unique line item
if ($unique)
$sum[$item->product->sku][] = $item;
}
if (count($sum)) {
$items = array();
foreach ($sum as $sku => $item)
foreach ($item as $sitem)
array_push($items,$sitem);
return $items;
}
}
/**
* Calculate the total for items for a service
*/
@@ -162,33 +215,33 @@ class Model_Invoice extends ORMOSB {
* Return a list of items based on a sort criteria
*/
public function sorted_service_items($index) {
$summary = array();
$summary = array();
foreach ($this->items() as $item) {
$key = $item->service->$index;
foreach ($this->items() as $item) {
$key = $item->service->$index;
if (! isset($summary[$key]['items'])) {
$summary[$key]['items'] = array();
$summary[$key]['total'] = 0;
}
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);
array_push($summary[$key]['items'],$item);
$summary[$key]['total'] += $item->total();
}
$summary[$key]['total'] += $item->total();
}
return $summary;
return $summary;
}
/**
* Return a list of taxes used on this invoice
*/
public function tax_summary() {
$summary = array();
$summary = array();
foreach ($this->items() as $item) {
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;
@@ -197,7 +250,7 @@ class Model_Invoice extends ORMOSB {
}
}
return $summary;
return $summary;
}
public function add_item() {