Revamping invoice PDF rendering and standardisation work

This commit is contained in:
Deon George
2016-08-10 16:07:00 +10:00
parent a560c5f4fa
commit 24bb4a701b
27 changed files with 229 additions and 475 deletions

View File

@@ -113,7 +113,7 @@ class Invoice {
elseif ($this->_io->billing_status == 1)
$pdfo->drawInvoicePaidNotice();
if ($this->_io->account->invoices_due_total())
if ($this->_io->account->invoices_due_total() > $this->_io->due())
$pdfo->drawSummaryInvoicesDue();
$pdfo->drawSummaryLineItems();
@@ -134,8 +134,6 @@ class Invoice {
}
public function render($type,$section,$args=array()) {
$this->_io->pre_render();
switch ($type) {
case 'email':
switch ($section) {

View File

@@ -50,8 +50,8 @@ abstract class Invoice_TCPDF extends TCPDF {
$this->SetCreator('Open Source Billing');
$this->SetAuthor($this->co->name());
$this->SetTitle(sprintf('%s Invoice',$this->co->name()));
$this->SetSubject(sprintf('Invoice #%06s',$this->io->id()));
$this->SetKeywords($this->io->id());
$this->SetSubject(sprintf('Invoice #%06s',$this->io->refnum(TRUE)));
$this->SetKeywords($this->io->refnum(TRUE));
$this->SetAutoPageBreak(TRUE,25);
$this->SetHeaderMargin(1);
$this->SetFooterMargin(10);

View File

@@ -9,7 +9,7 @@
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Invoice_TCPDF_Default extends Invoice_Tcpdf {
class Invoice_Tcpdf_Default extends Invoice_Tcpdf {
// Current line being printed
public $sum_y = 0;
@@ -87,7 +87,7 @@ class Invoice_TCPDF_Default extends Invoice_Tcpdf {
$this->SetFont('helvetica','',10);
$this->SetXY($x,$y); $this->Cell(0,0,_('Invoice Number'));
$this->SetFont('helvetica','B',11);
$this->SetXY($x,$y); $this->Cell(0,0,$this->io->id(),0,0,'R');
$this->SetXY($x,$y); $this->Cell(0,0,$this->io->refnum(TRUE),0,0,'R');
// Company Address
// $y = 216;
@@ -142,7 +142,7 @@ class Invoice_TCPDF_Default extends Invoice_Tcpdf {
$this->SetFont('helvetica','B',11);
$this->SetXY($x,$y); $this->Cell(0,0,'TAX INVOICE');
$this->SetFont('helvetica','B',11);
$this->SetXY($x,$y); $this->Cell(0,0,$this->io->id(),0,0,'R');
$this->SetXY($x,$y); $this->Cell(0,0,$this->io->refnum(TRUE),0,0,'R');
// Invoice number at top of page.
$y += 7;
@@ -261,7 +261,7 @@ class Invoice_TCPDF_Default extends Invoice_Tcpdf {
* Draw previous invoices due
*/
public function drawSummaryInvoicesDue() {
$x = 125; $y = $this->sum_y ? $this->sum_y : 50;
$x = 125; $y = $this->sum_y ? $this->sum_y : 55;
$items = $this->io->account->invoices_due($this->io->date_orig);
@@ -281,7 +281,7 @@ class Invoice_TCPDF_Default extends Invoice_Tcpdf {
foreach ($items as $line) {
if (++$i < $this->itemsPreviousMax) {
$this->SetXY($x,$y);
$this->Cell(0,0,sprintf('%s %s',$line->display('date_orig'),$line->id()));
$this->Cell(0,0,sprintf('%s %s',$line->display('date_orig'),$line->refnum(TRUE)));
$this->Cell(0,0,$line->due(TRUE),0,0,'R'); $y += 5;
} else {
@@ -442,26 +442,20 @@ class Invoice_TCPDF_Default extends Invoice_Tcpdf {
public function drawDetailLineItems() {
$this->i = 0;
foreach ($this->io->items_periods() as $rs) {
$sids=array();
foreach ($this->io->items_periods($rs) as $iio) {
if (in_array($iio->service_id,$sids))
continue;
array_push($sids,$iio->service_id);
$this->drawLineItem($iio);
foreach ($this->io->items_render() as $key => $items) {
switch ($key) {
case 's':
$this->drawLineItems($items);
break;
}
}
}
/**
* Draws Invoice Detail Item
*
* @todo need to make sure that this pages well, when there are many items (with many sub details).
* Draws Invoice Detail Items
*/
private function drawLineItem($ito) {
private function drawLineItems(array $items) {
$track['rs'] = $track = array();
$x = 10;
if ($this->i == 0 || $this->i%$this->max_lines_page == 0) {
$this->y = 5;
@@ -470,7 +464,7 @@ class Invoice_TCPDF_Default extends Invoice_Tcpdf {
$this->SetFont('helvetica','B',12);
$this->SetXY($x,$this->y); $this->Cell(0,0,_('Itemised Charges'));
$this->Cell(0,0,_('Page #').$this->PageNo(),0,0,'R');
$this->SetXY($x,$this->y); $this->Cell(0,0,_('Invoice #').$this->io->id(),0,0,'C');
$this->SetXY($x,$this->y); $this->Cell(0,0,_('Invoice #').$this->io->refnum(TRUE),0,0,'C');
// Draw table headers
$this->y += 10;
@@ -489,68 +483,68 @@ class Invoice_TCPDF_Default extends Invoice_Tcpdf {
$this->SetY($this->y);
}
$this->SetFont('helvetica','',8);
$this->SetX($x);
$this->Cell(0,0,$ito->title());
$this->SetFont('helvetica','',7);
$last = '';
foreach ($items as $iio) {
// So we dont render the same recurring schedule over again
if (! is_null($iio->recurring_schedule) AND ! in_array($iio->recurring_schedule,$track['rs'])) {
array_push($track['rs'],$iio->recurring_schedule);
// echo $iio->display('recurring_schedule');
}
if ($z=$this->io->service_items($ito)) {
foreach ($z as $eiio) {
$this->y += 3;
// If we are rendering a new service, reset the product id tracking
if ($iio->service_id != $last) {
if ($last) {
$this->y += 6;
$this->SetY($this->y);
}
$last = $iio->service_id;
$track['p'] = array();
}
// So we dont render the same product title over again
if (! in_array($iio->product_id,$track['p'])) {
array_push($track['p'],$iio->product_id);
$this->SetFont('helvetica','',8);
$this->SetX($x);
$this->Cell(0,0,sprintf('%s: %s',$iio->service->refnum(TRUE),$iio->name($iio->invoice->account->language)));
$this->SetFont('helvetica','',7);
$this->y += 1;
if ($iio->service->attributes()) {
foreach ($iio->service->attributes() as $k=>$v) {
$this->SetFont('helvetica','I',6);
$this->y += 3;
$this->SetXY($x+10,$this->y); $this->Cell(0,0,$k);
$this->SetFont('helvetica','',6);
$this->SetXY($x+40,$this->y); $this->Cell(0,0,$v);
}
}
$this->y += 2;
$this->SetY($this->y);
$this->SetX($x+10);
$this->Cell(10,0,$eiio->invoice_line());
$this->i++;
}
if ($eiio->price_base) {
$this->SetX($x+160);
$this->Cell(10,0,Currency::display($eiio->price_base),0,0,'R');
}
if ($eiio->quantity) {
$this->SetX($x+130);
$this->Cell(10,0,$eiio->quantity,0,0,'R');
}
$this->y += 3;
$this->SetY($this->y);
$this->SetX($x+10);
$this->Cell(10,0,sprintf('%s: (%s)',$iio->namesub($iio->invoice->account->language),$iio->refnum()));
if ($iio->quantity) {
$this->SetX($x+130);
$this->Cell(0,0,$eiio->total(TRUE),0,0,'R');
$this->Cell(10,0,sprintf('%3.2f',$iio->quantity),0,0,'R');
}
if ($iio->price_base) {
$this->SetX($x+160);
$this->Cell(10,0,Currency::display($iio->price_base),0,0,'R');
}
$this->SetX($x+130);
$this->Cell(0,0,$iio->total(TRUE),0,0,'R');
}
if ($z=$this->io->service_items_extra($ito)) {
foreach ($z as $eiio) {
$this->y += 3;
$this->SetY($this->y);
$this->SetX($x+10);
$this->Cell(10,0,$eiio->invoice_line());
if ($eiio->price_base) {
$this->SetX($x+160);
$this->Cell(10,0,Currency::display($eiio->price_base),0,0,'R');
}
if ($eiio->quantity) {
$this->SetX($x+130);
$this->Cell(10,0,$eiio->quantity,0,0,'R');
}
$this->SetX($x+130);
$this->Cell(0,0,$eiio->total(TRUE),0,0,'R');
}
}
$this->y += 2;
if ($ito->invoice_detail_items())
foreach ($ito->invoice_detail_items() as $k=>$v) {
$this->SetFont('helvetica','I',6);
$this->y += 3;
$this->SetXY($x+10,$this->y); $this->Cell(0,0,$k);
$this->SetFont('helvetica','',6);
$this->SetXY($x+40,$this->y); $this->Cell(0,0,$v);
}
$this->y += 5;
$this->SetY($this->y);
$this->i++;
}
}
?>

View File

@@ -53,6 +53,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
private $_render = array();
/** REQUIRED ABSTRACT METHODS **/
/**
* Display the Invoice Reference Number
*/
@@ -116,29 +117,6 @@ class Model_Invoice extends ORM_OSB implements Cartable {
->where('module_data','=',$this);
}
/**
* Display the Invoice Number
*/
public function id() {
return sprintf('%06s',$this->id);
}
/**
* Return the recurring schedules that are on an invoice
*
* @param $period Return an Array of items for that period
*/
public function items_periods($period=FALSE) {
if (is_null($period))
return isset($this->_render['OTHER']) ? $this->_render['OTHER'] : array();
elseif ($period)
return isset($this->_render['SCHED'][$period]) ? $this->_render['SCHED'][$period] : array();
else
return array_keys($this->_render['SCHED']);
}
/**
* Return an array of items to render, in appropriate order
*/
@@ -184,10 +162,19 @@ class Model_Invoice extends ORM_OSB implements Cartable {
}
}
// Get service items that dont have a recurring schedule
foreach ($this->_sub_items as $iio)
if (! $iio->void AND ! in_array($iio->id,$track) AND $iio->service_id) {
// Ensure we dont process this item again
array_push($track,$iio->id);
array_push($result['s'],$iio);
}
// Next get the items we havent already got
$result['other'] = array();
foreach ($this->_sub_items as $iio)
if (! in_array($iio->id,$track))
if (! $iio->void AND ! in_array($iio->id,$track))
array_push($result['other'],$iio);
// Debug
@@ -226,29 +213,6 @@ class Model_Invoice extends ORM_OSB implements Cartable {
return $result;
}
/**
* Return the item titles that are on an invoice
*
* @param $title Return an Array of items for that title
*/
public function items_titles($title=NULL,$period=NULL) {
$result = array();
foreach ($this->subitems() as $ito) {
if ($ito->service_id) {
if (is_null($title) AND ! in_array($ito->title(),$result) AND (is_null($period) OR ($period == $ito->recurring_schedule)))
array_push($result,$ito->title());
elseif (($ito->title() == $title AND (is_null($period) OR ($period == $ito->recurring_schedule))) OR is_null($ito->recurring_schedule))
array_push($result,$ito);
} else {
throw HTTP_Exception::factory(501,'Not handled non-services');
}
}
return $result;
}
public function payments() {
return $this->payment_item->find_all();
}
@@ -262,37 +226,6 @@ class Model_Invoice extends ORM_OSB implements Cartable {
return $format ? Currency::display($result) : $result;
}
/**
* This function takes care of all the invoice items so that they are rendered only once
*
* We organise items by
* + recurring_schedule for service items item_type=0
* + recurring_schedule for service items for item_type!=0 (that have an item_type=0 previously selected)
* + Other Items
*/
public function pre_render() {
$this->_render = array();
$this->_render['SCHED'] = array();
$processed = array();
foreach ($this->subitems() as $iio) {
if ($iio->id AND in_array($iio->id,$processed))
continue;
if ($iio->recurring_schedule) {
$this->_render['SCHED'][$iio->recurring_schedule][] = $iio;
} elseif (Object::in_array('service_id',$iio->service_id,$this->_render['SCHED'],TRUE)) {
// Do nothing
} else {
$this->_render['OTHER'][] = $iio;
}
array_push($processed,$iio->id);
}
}
/**
* For a particular recurring schedule, get al lthe services
*/
@@ -418,72 +351,6 @@ class Model_Invoice extends ORM_OSB implements Cartable {
return $this;
}
/**
* Return the service items on an invoice relating to an invoice_item
* IE: item_type == 0
*/
public function service_items(Model_Invoice_Item $o) {
$result = array();
// At the moment, we only return items pertaining to a service
if (! $o->service_id)
return $result;
foreach ($this->subitems() as $iio)
if ($iio->item_type == 0 AND $iio->service_id == $o->service_id)
array_push($result,$iio);
return $result;
}
/**
* Return the extra items on an invoice relating to an invoice_item
* IE: item_type != 0
*/
public function service_items_extra(Model_Invoice_Item $o) {
$result = array();
foreach ($this->subitems() as $iio)
if ($iio->item_type != 0 AND (! $o->service_id OR ($iio->service_id == $o->service_id)))
array_push($result,$iio);
return $result;
}
/**
* Return the total of all items relating to a service
*/
public function service_items_tax(Model_Invoice_Item $o,$format=FALSE) {
$result = 0;
// At the moment, we only return extra items pertaining to a service
if (! $o->service_id)
return $result;
foreach ($this->subitems() as $iio)
if ($iio->service_id == $o->service_id)
$result += $iio->tax();
return $format ? Currency::display($result) : $result;
}
/**
* Return the total of all items relating to a service
*/
public function service_items_total(Model_Invoice_Item $o,$format=FALSE) {
$result = 0;
// At the moment, we only return extra items pertaining to a service
if (! $o->service_id)
return $result;
foreach ($this->subitems() as $iio)
if ($iio->service_id == $o->service_id)
$result += $iio->total();
return $format ? Currency::display($result) : $result;
}
/**
* For a particular service, get all the products
*/

View File

@@ -51,10 +51,10 @@ class Model_Invoice_Item extends ORM_OSB {
0=>'Product/Service', // * Line Charge Topic on Invoice.
1=>'Hardware', // *
2=>'Service Relocation Fee', // * Must have corresponding SERVICE_ID
3=>'Service Change Fee', // * Must have corresponding SERVICE_ID
4=>'Service Connection Fee', // * Must have corresponding SERVICE_ID
3=>'Service Change', // * Must have corresponding SERVICE_ID
4=>'Service Connection', // * Must have corresponding SERVICE_ID
5=>'Excess Usage', // * Excess Service Item, of item 0, must have corresponding SERVICE_ID
6=>'Service Cancellation Fee', // * Must have corresponding SERVICE_ID
6=>'Service Cancellation', // * Must have corresponding SERVICE_ID
7=>'Extra Product/Service Charge', // * Service Billing in advance, Must have corresponding SERVICE_ID
8=>'Product Addition', // * Additional Product Customisation, Must have corresponding SERVICE_ID
120=>'Credit/Debit Transfer', // * SERVICE_ID is NULL, MODULE_ID is NULL, MODULE_REF is NULL : INVOICE_ID is NOT NULL
@@ -72,10 +72,13 @@ class Model_Invoice_Item extends ORM_OSB {
switch ($this->item_type) {
case 0:
case 2:
case 3:
case 4:
case 5:
return sprintf('%s: %s',$this->product->name($variable),$this->service->namesub($variable));
case 124:
return StaticList_ItemType::get($this->item_type);
default:
return sprintf('Unknown [%s-%s]',$this->item_type,$this->id);
}
@@ -88,8 +91,12 @@ class Model_Invoice_Item extends ORM_OSB {
switch ($this->item_type) {
case 0:
return sprintf('%s: %s',StaticList_ItemType::get($this->item_type),$this->period());
case 2:
return sprintf('%s: %s',StaticList_ItemType::get($this->item_type),$this->_module() ? $this->_module()->display('date_charge') : $this->period());
case 3:
case 4:
case 6:
return sprintf('%s: %s',StaticList_ItemType::get($this->item_type),($this->_module()->attributes ? $this->_module()->namesub($variable) : $this->_module()->display('date_charge')));
case 5:
return $this->_module()->namesub($variable);
default:
@@ -109,7 +116,7 @@ class Model_Invoice_Item extends ORM_OSB {
/**
* Module assocated with the invoice item
*/
public function _module() {
private function _module() {
if (! $this->module_id OR ! $this->module_ref)
return NULL;
@@ -164,10 +171,18 @@ class Model_Invoice_Item extends ORM_OSB {
switch ($this->item_type) {
case 0:
// @todo Validate if module_id = charge and module_ref corresponds with the line item
if (! $this->service_id OR ! $this->product_id OR ($this->module_id AND $this->module_id != 30 AND ! is_null($this>module_id)) OR $this->product_name OR is_null($this->recurring_schedule))
if (! $this->service_id OR ! $this->product_id OR ($this->module_id AND $this->module_id != 30 AND ! is_null($this>module_ref)) OR $this->product_name OR is_null($this->recurring_schedule))
return FALSE;
break;
case 1:
if (! $this->product_id OR $this->module_id OR $this->module_ref OR $this->product_name OR ! is_null($this->recurring_schedule))
return FALSE;
break;
// These items come from the charge module, so we should have a charge module_id and ref
case 2:
if (! $this->service_id OR ! $this->product_id OR ($this->module_id AND $this->module_id != 30 AND ! is_null($this>module_ref)) OR $this->product_name OR ! is_null($this->recurring_schedule))
return FALSE;
break;
case 3:
case 4:
if (! $this->service_id OR ! $this->product_id OR ! $this->module_id OR ! $this->module_ref OR $this->product_name OR ! is_null($this->recurring_schedule))
@@ -178,65 +193,23 @@ class Model_Invoice_Item extends ORM_OSB {
if (! $this->service_id OR ! $this->product_id OR ! $this->module_id OR ! $this->module_ref OR $this->product_name OR ! is_null($this->recurring_schedule))
return FALSE;
break;
case 124:
if ($this->service_id OR $this->product_id OR $this->module_id OR $this->module_ref OR $this->product_name OR ! is_null($this->recurring_schedule))
return FALSE;
break;
}
// If we get here, assume it is valid
return TRUE;
}
/**
* The line that will be printed on an invoice
*
* @todo This method includes some database format validation routines, which can be removed when the database
* is completly transformed.
*/
public function invoice_line() {
$ii = NULL;
// Our module is responsible for rending the invoice line
$ii = ($this->module_id AND method_exists($this->module(),'invoice_item')) ? $this->module()->invoice_item($this->item_type) : StaticList_ItemType::get($this->item_type);
switch ($this->item_type) {
// Service Charges
case 0:
return ((! $this->service_id OR $this->product_id OR $this->product_name OR ! $this->recurring_schedule OR ! $this->date_start OR ! $this->date_stop) ? '+ ' : '').$ii.' '.$this->period();
case 1:
// @todo
return $this->product_name;
case 2:
case 3:
case 4:
case 5:
case 6:
return ((! $this->service_id OR $this->product_id OR $this->product_name OR $this->recurring_schedule OR ! $this->date_start OR ! $this->date_stop) ? '+ ' : '').$ii;
case 7:
return ((! $this->service_id OR $this->product_id OR $this->product_name OR ! $this->date_start OR ! $this->date_stop) ? '+ ' : '').$ii.' '.$this->period();
case 8:
return $this->product_name;
case 124:
case 125:
case 126:
case 127:
// @todo
return $ii;
// @todo DB records to fix.
default:
throw HTTP_Exception::factory(501,'Unable to render invoice item :id',array(':id'=>$this->id));
}
}
/**
* Return an instance of the Model of this charge
*/
public function module() {
return $this->_module();
}
// Display the period that a transaction applies
public function period() {
return ($this->date_start == $this->date_stop) ? Site::Date($this->date_start) : sprintf('%s -> %s',Site::Date($this->date_start),Site::Date($this->date_stop));
@@ -322,23 +295,5 @@ class Model_Invoice_Item extends ORM_OSB {
return $format ? Currency::display($result) : Currency::round($result);
}
// Display a transaction number
public function trannum() {
return sprintf('%03s-%06s',$this->item_type,$this->id);
}
public function invoice_detail_items() {
switch ($this->item_type) {
case 0:
return $this->service->details('invoice_detail_items');
case 4:
return array('Charge'=>_('Service Connection Fee'));
case 5:
return $this->charge->details('invoice_detail_items');
default:
return array('Item'=>$this->item_type);
}
}
}
?>