diff --git a/application/classes/company.php b/application/classes/company.php
index fd19965e..5184b52c 100644
--- a/application/classes/company.php
+++ b/application/classes/company.php
@@ -15,14 +15,57 @@ class Company {
return new Company;
}
+ public static function street() {
+ // @todo Details should be obtained from DB
+ return 'PO Box 149';
+ }
+
+ public static function city() {
+ // @todo Details should be obtained from DB
+ return 'Bendigo';
+ }
+
+ public static function state() {
+ // @todo Details should be obtained from DB
+ return 'VIC';
+ }
+
+ public static function pcode() {
+ // @todo Details should be obtained from DB
+ return '3550';
+ }
+
public static function address($ln='
') {
- // @todo Company address should be calculated
- return implode($ln,array('PO Box 149','Bendigo, VIC 3550'));
+ return implode($ln,array(static::street(),sprintf('%s, %s %s',static::city(),static::state(),static::pcode())));
+ }
+
+ public static function phone() {
+ // @todo Company phone should be obtained from db
+ return '03 5410 1135';
+ }
+
+ public static function fax() {
+ // @todo Details should be obtained from DB
+ return '03 5410 1145';
}
public static function contacts() {
- // @todo Company phone should be calculated
- return 'Tel: 03 5410 1135';
+ return 'Tel: '.static::phone();
+ }
+
+ public static function bsb() {
+ // @todo Details should be obtained from DB
+ return Kohana::config('config.bsb');
+ }
+
+ public static function account() {
+ // @todo Details should be obtained from DB
+ return Kohana::config('config.accnum');
+ }
+
+ public static function taxid() {
+ // @todo Details should be obtained from DB
+ return Kohana::config('config.taxid');
}
public static function render() {
diff --git a/application/classes/config.php b/application/classes/config.php
index fd71789f..2af30dd4 100644
--- a/application/classes/config.php
+++ b/application/classes/config.php
@@ -1,4 +1,37 @@
get('modules'))
+ return $cache->get('modules');
+
+ } else
+ $cache = '';
+
+ $modules = array();
+ $module_table = 'module';
+
+ if (class_exists('Model_'.ucfirst($module_table))) {
+ $mo = ORM::factory($module_table)->where('status','=',1)->find_all()->as_array();
+
+ foreach ($mo as $o)
+ $modules[$o->name] = MODPATH.$o->name;
+ }
+
+ if ($cache)
+ $cache->set('modules',$modules);
+
+ return $modules;
+ }
+}
?>
diff --git a/application/config/config.php b/application/config/config.php
index 5fe42c77..3553db50 100644
--- a/application/config/config.php
+++ b/application/config/config.php
@@ -32,6 +32,9 @@ return array(
'172.31.10.200'=>Kohana::DEVELOPMENT,
'www.graytech.net.au'=>Kohana::PRODUCTION,
),
+ 'bsb' => '633-000', // @todo This should come from the DB
+ 'accnum' => '120 440 821', // @todo This should come from the DB
'site_name' => 'Graytech Hosting Pty Ltd', // @todo This should come from the DB
+ 'taxid' => 'ABN: 49 106 229 476', // @todo This should come from the DB
);
?>
diff --git a/application/config/invoice.php b/application/config/invoice.php
new file mode 100644
index 00000000..f8fbf4b6
--- /dev/null
+++ b/application/config/invoice.php
@@ -0,0 +1,15 @@
+ 'TCPDF',
+);
+?>
diff --git a/modules/account/classes/model/account.php b/modules/account/classes/model/account.php
index 261a1064..62e51545 100644
--- a/modules/account/classes/model/account.php
+++ b/modules/account/classes/model/account.php
@@ -32,7 +32,7 @@ class Model_Account extends Model_Auth_UserDefault {
}
public function accnum() {
- return sprintf('%02s-%06s',Config::siteid(),$this->id);
+ return sprintf('%02s-%04s',Config::siteid(),$this->id);
}
public function date_last() {
@@ -73,4 +73,45 @@ class Model_Account extends Model_Auth_UserDefault {
return FALSE;
}
+
+ /**
+ * Get a list of all invoices for this account
+ */
+ public function invoices() {
+ $return = array();
+
+ foreach ($this->invoice->distinct('id')->find_all() as $invoice)
+ $return[$invoice->id] = $invoice;
+
+ return $return;
+ }
+
+ /**
+ * Get a list of due invoices for this account
+ */
+ public function invoices_due() {
+ $return = array();
+
+ foreach ($this->invoices() as $invoice)
+ if ($invoice->due())
+ $return[$invoice->id] = $invoice;
+
+ return $return;
+ }
+
+ /**
+ * Calculate the total of invoices due for this account
+ */
+ public function invoices_due_total($format=FALSE) {
+ $result = 0;
+
+ foreach ($this->invoices_due() as $invoice)
+ $result += $invoice->due();
+
+ if ($format)
+ return Currency::display($result);
+ else
+ return $result;
+ }
}
+?>
diff --git a/modules/invoice/classes/controller/user/invoice.php b/modules/invoice/classes/controller/user/invoice.php
index fa6240d0..0b50eebe 100644
--- a/modules/invoice/classes/controller/user/invoice.php
+++ b/modules/invoice/classes/controller/user/invoice.php
@@ -51,5 +51,14 @@ class Controller_User_Invoice extends Controller_TemplateDefault {
->set('mediapath',Route::get('default/media'))
->set('invoice',$io);
}
+
+ /**
+ * Download an invoice
+ */
+ public function action_download($id) {
+ $io = ORM::factory('invoice',$id);
+
+ return Invoice::instance()->pdf($io)->Output(sprintf('%s.pdf',$io->refnum()),'D');
+ }
}
?>
diff --git a/modules/invoice/classes/invoice.php b/modules/invoice/classes/invoice.php
index bc3eca5b..1f7dd829 100644
--- a/modules/invoice/classes/invoice.php
+++ b/modules/invoice/classes/invoice.php
@@ -11,17 +11,21 @@
* @license http://dev.leenooks.net/license.html
*/
class Invoice {
+ // This invoice Object
+ private $io;
+
public static function instance() {
return new Invoice;
}
/**
- * Return a list of invoices for an invoice
+ * Return a list of invoices for an service
*
* @param $id int Service ID
* @param $paid boolean Optionally only list the ones that are not paid.
* @return array
*/
+ // @todo Function Not Used
public static function servicelist($id,$paid=TRUE) {
// @todo need to add the db prefix
$invoices = DB::Query(Database::SELECT,'
@@ -50,6 +54,7 @@ SELECT i.id AS iid,i.due_date AS due FROM ab_invoice i,ab_invoice_item ii WHERE
* @return real Total amount outstanding
* @see Invoice::listservice()
*/
+ // @todo Function Not Used
public static function servicetotal($id,$paid=TRUE) {
$total = 0;
@@ -65,6 +70,7 @@ SELECT i.id AS iid,i.due_date AS due FROM ab_invoice i,ab_invoice_item ii WHERE
* @param $id int Service ID
* @return datetime
*/
+ // @todo Function Not Used
public static function servicedue($id) {
$due = 0;
@@ -75,18 +81,149 @@ SELECT i.id AS iid,i.due_date AS due FROM ab_invoice i,ab_invoice_item ii WHERE
return $due;
}
+ // @todo Function Not Used
public static function balance($id) {
- $invoice = ORM::factory('invoice')
- ->where('id','=',$id)
- ->find();
+ return ORM::factory('invoice',$id)->due();
+ }
- // @todo We should call check() here to re-run the validation, which re-calcs the total
- // @todo might need to cache these results for performance
- #if ($invoice->loaded() AND $invoice->check())
- if ($invoice->loaded())
- return $invoice->total_amt-$invoice->billed_amt-$invoice->credit_amt;
- else
- return 0;
+ /**
+ * Generate a PDF invoice
+ */
+ public function pdf($io) {
+ $invoice_class = sprintf('invoice_pdf_%s',Kohana::config('invoice.driver'));
+
+ if (! class_exists($invoice_class))
+ throw new Kohana_Exception('Invoice class :class doesnt exist',array(':class'=>$invoice_class));
+
+ $this->io = $io;
+ $pdf = new $invoice_class($io);
+
+ if ($pdf->getTemplate()) {
+ $pagecount = $pdf->setSourceFile($pdf->getTemplate());
+ $tplidx = $pdf->ImportPage(1);
+ }
+
+ $pdf->addPage();
+
+ # If we are using FPDI
+ if (isset($tplidx))
+ $pdf->useTemplate($tplidx);
+
+ $this->draw_summary_invoice($pdf);
+
+ # If we get here, all is OK.
+ return $pdf;
+ }
+
+ private function draw_summary_invoice($pdf) {
+ // Draw Invoice Basics
+ $pdf->drawCompanyLogo();
+ $pdf->drawCompanyAddress();
+ $pdf->drawInvoiceHeader();
+ // @todo Get news from DB
+ $pdf->drawNews('');
+ $pdf->drawRemittenceStub();
+ $pdf->drawPaymentMethods();
+
+ if ($this->io->billing_status !=1 && $this->io->suspend_billing != 1 && $this->io->due_date <= time())
+ $pdf->drawInvoiceDueNotice();
+ elseif($this->io->billing_status == 1)
+ $pdf->drawInvoicePaidNotice();
+
+ if ($this->io->account->invoices_due_total())
+ $pdf->drawSummaryInvoicesDue();
+
+ $pdf->drawSummaryLineItems();
+return;
+ #unset($pdf->itemsSummary);
+
+ # BEGIN loop for enumerating information in multiple ways on the invoice
+ $iteration = 0;
+ while ($pdf->drawLineItems_pre($iteration)) {
+ foreach ($this->sInvoiceItems() as $index => $items) {
+ # Get the date range if set
+ if (! empty($items['date_start']) && ! empty($items['date_stop'])) {
+ global $C_translate;
+ $C_translate->value('invoice','start',date(UNIX_DATE_FORMAT,$items['date_start']));
+ $C_translate->value('invoice','stop',date(UNIX_DATE_FORMAT,$items['date_stop']));
+ }
+
+ $line = array(
+ 'name'=>$this->sLineItemDesc($index),
+ 'domain'=>$items['domain_name'],
+ 'amount'=>$items['price_base'],
+ 'sku'=>$items['sku'],
+ 'qty'=>$items['quantity'],
+ 'cost'=>$items['price_base'],
+ 'attr'=>$items['product_attr'],
+ 'price_type'=>$items['price_type'],
+ 'price_base'=>$items['price_base'],
+ 'item_type'=>$items['item_type'],
+ 'total_amt'=>$items['total_amt']
+ );
+
+ if ($items['date_start'] && $items['date_stop'])
+ if ($items['date_start'] == $items['date_stop'])
+ $line['daterange'] = sprintf('%s',date(UNIX_DATE_FORMAT,$items['date_start']));
+ else
+ $line['daterange'] = sprintf('%s - %s',date(UNIX_DATE_FORMAT,$items['date_start']),date(UNIX_DATE_FORMAT,$items['date_stop']));
+
+ $pdf->drawLineItems($db,$line,$this->getRecordAttr('id'));
+
+ if ($items['price_setup']) {
+ $line = array(
+ 'name'=>sprintf('%s - %s',$this->sLineItemDesc($index),_('Setup Charge')),
+ 'amount'=>$items['price_setup'],
+ 'qty'=>'1',
+ 'sku'=>$items['sku'],
+ 'cost'=>$items['price_setup'],
+ 'price_base'=>$items['price_setup'],
+ 'price_type'=>999
+ );
+
+ $pdf->drawLineItems($db,$line,$this->getRecordAttr('id'));
+ }
+ }
+
+ if ($this->print['invoice']['discount_amt']) {
+ $line = array(
+ 'name'=>_('Discount'),
+ 'amount'=>-($this->print['invoice']['discount_amt']),
+ 'qty'=>'1',
+ 'cost'=>-($this->print['invoice']['discount_amt']),
+ 'price_base'=>-($this->print['invoice']['discount_amt']),
+ 'price_type'=>999);
+
+ $pdf->drawLineItems($db,$line,$this->getRecordAttr('id'));
+ }
+
+ if ($this->print['invoice']['tax_amt']) {
+ $rs = $db->Execute(sqlSelect($db,array('invoice_item_tax','tax'),'A.amount,B.description',sprintf('A.tax_id=B.id AND A.invoice_id=%s',$this->getRecordAttr('id'))));
+ if ($rs && $rs->RecordCount()) {
+ $taxes = array();
+
+ while (! $rs->EOF) {
+ if (! isset($taxes[$rs->fields['description']]))
+ $taxes[$rs->fields['description']] = $rs->fields['amount'];
+ else
+ $taxes[$rs->fields['description']] += $rs->fields['amount'];
+
+ $rs->MoveNext();
+ }
+
+ foreach ($taxes as $txds => $txamt) {
+ $line = array('name'=>$txds,'amount'=>$txamt,'total_amt'=>$txamt,'price_type'=>999);
+ $pdf->drawLineItems($db,$line,$this->getRecordAttr('id'));
+ }
+ }
+ }
+
+ # Increment the iteration
+ ++$iteration;
+ }
+
+ # Custom functions:
+ $pdf->drawCustom();
}
}
?>
diff --git a/modules/invoice/classes/invoice/pdf.php b/modules/invoice/classes/invoice/pdf.php
new file mode 100644
index 00000000..9d445c72
--- /dev/null
+++ b/modules/invoice/classes/invoice/pdf.php
@@ -0,0 +1,136 @@
+io = $io;
+
+ // Set up the invoice
+ $this->SetCreator('Open Source Billing');
+ $this->SetAuthor(Config::sitename());
+ $this->SetTitle(sprintf('%s Invoice',Config::sitename()));
+ $this->SetSubject(sprintf('Invoice #%06s',$this->io->invnum()));
+ $this->SetKeywords($this->io->invnum());
+ $this->SetAutoPageBreak(TRUE,25);
+ $this->SetHeaderMargin(1);
+ $this->SetFooterMargin(10);
+ $this->SetDisplayMode('fullwidth');
+ #$this->setHeaderFont(array('helvetica','',8));
+ $this->setFooterFont(array('helvetica','',8));
+ }
+
+ abstract public function drawCompanyLogo();
+ abstract public function drawCompanyAddress();
+ abstract public function drawInvoiceHeader();
+
+ /**
+ * Enable re-iteration of the invoices items, so that they can be displayed many ways
+ */
+ abstract public function drawLineItems($iteration);
+
+ /**
+ * This is called for each line item.
+ */
+ abstract public function drawLineItem($line);
+
+ /**
+ * Draws the summary on the first page
+ */
+ abstract public function drawSummaryLineItems();
+ abstract public function drawPaymentMethods();
+ abstract public function drawRemittenceStub();
+
+ public function drawCustom() {}
+ public function drawInvoiceDueNotice() {}
+ public function drawInvoicePaidNotice() {}
+ public function setLateFeeNotice() {}
+
+ /**
+ * Get a PDF invoice template
+ */
+ public function getTemplate() {}
+
+/*
+ public function setItemsFull($items) {
+ $this->itemsFull = $items;
+ }
+
+ public function setItemsPrevious($items) {
+ $this->itemsPrevious = $items;
+ }
+
+ public function setDateRange($periodStart,$periodEnd) {
+ $this->dateRange = sprintf('%s - %s',date(UNIX_DATE_FORMAT,$periodStart),date(UNIX_DATE_FORMAT,$periodEnd));
+ }
+
+ public function setCurrency($currency) {
+ $this->invoiceCurrency = $currency;
+ }
+
+ public function setDecimals($decimals) {
+ $this->invoiceDecimals = $decimals;
+ }
+
+ /**
+ * Render an amount into a currency display
+ */
+/*
+ protected function _currency($num) {
+ global $C_list;
+
+ if ($this->invoiceDecimals>3)
+ return $this->invoiceCurrency.number_format($num,$this->invoiceDecimals);
+ else
+ return $C_list->format_currency_num($num,$this->invoice['actual_billed_currency_id']);
+ }
+*/
+
+ /**
+ * Add a watermark to the PDF
+ */
+ public function addWaterMark($text) {
+ $this->SetFont('helvetica','B',50);
+ $this->SetTextColor(203,203,203);
+ $this->Rotate(0);
+ $this->Text(10,50,$text);
+ $this->Rotate(0);
+ $this->SetTextColor(0,0,0);
+ }
+}
+?>
diff --git a/modules/invoice/classes/invoice/pdf/tcpdf.php b/modules/invoice/classes/invoice/pdf/tcpdf.php
new file mode 100644
index 00000000..8d21c980
--- /dev/null
+++ b/modules/invoice/classes/invoice/pdf/tcpdf.php
@@ -0,0 +1,539 @@
+Image($logo,$x,$y,$size);
+ }
+
+ /**
+ * Draw the Company Address
+ */
+ public function drawCompanyAddress() {
+ # Add the company address next to the logo
+ $x = 40; $y = 7;
+
+ $this->SetFont('helvetica','B',10);
+ $this->SetXY($x,$y); $this->Cell(0,0,Config::sitename()); $y += 4;
+
+ $this->SetFont('helvetica','',10);
+ $this->SetXY($x,$y); $this->Cell(0,0,Company::taxid()); $y += 6;
+
+ $this->SetXY($x,$y); $this->Cell(0,0,Company::street()); $y += 4;
+ $this->SetXY($x,$y); $this->Cell(0,0,sprintf('%s, %s %s',Company::city(),Company::state(),Company::pcode())); $y += 4;
+
+ $y += 2;
+ $this->SetXY($x,$y); $this->Cell(0,0,'Phone:'); $this->SetXY($x+16,$y); $this->Cell(0,0,Company::phone()); $y += 4;
+ $this->SetXY($x,$y); $this->Cell(0,0,'Fax:'); $this->SetXY($x+16,$y); $this->Cell(0,0,Company::fax()); $y += 4;
+ $this->SetXY($x,$y); $this->Cell(0,0,'Web:'); $this->SetXY($x+16,$y); $this->addHtmlLink(URL::base(TRUE,TRUE),URL::base(TRUE,TRUE)); $y += 4;
+ }
+
+ public function drawRemittenceStub() {
+ # Draw the remittance line
+ $this->Line(9,195,200,195);
+
+ $x = 18; $y = 200;
+
+ $this->SetFont('helvetica','B',13);
+ $this->SetXY($x,$y); $this->Cell(0,0,_('Payment Remittence')); $y +=5;
+
+ $this->SetFont('helvetica','',8);
+ $this->SetXY($x,$y); $this->Cell(0,0,_('Please return this portion with your cheque or money order')); $y +=3;
+ $this->SetXY($x,$y); $this->Cell(0,0,_('made payable to').' '.Config::sitename());
+
+ # Due Date
+ $x = 110; $y = 200;
+ $this->SetFont('helvetica','',10);
+ $this->SetXY($x,$y); $this->Cell(0,0,_('Issue Date'));
+ $this->SetFont('helvetica','B',11);
+ $this->SetXY($x,$y); $this->Cell(0,0,$this->io->display('date_orig'),0,0,'R');
+
+ # Account ID
+ $y = 205;
+ $this->SetFont('helvetica','',10);
+ $this->SetXY($x,$y); $this->Cell(0,0,_('Account Number'));
+ $this->SetFont('helvetica','B',11);
+ $this->SetXY($x,$y); $this->Cell(0,0,$this->io->account->accnum(),0,0,'R');
+
+ # Invoice number
+ $y = 210;
+ $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->invnum(),0,0,'R');
+
+ # Company Address
+ $y = 216;
+ $this->SetFont('helvetica','',10);
+ $this->SetXY(18,$y); $this->Cell(0,0,Config::sitename()); $y += 4;
+ $this->SetXY(18,$y); $this->Cell(0,0,Company::street()); $y += 4;
+ $this->SetXY(18,$y); $this->Cell(0,0,sprintf('%s, %s %s',Company::city(),Company::state(),Company::pcode())); $y += 4;
+
+ # Previous Due
+ $y = 215;
+ $this->SetFont('helvetica','',9);
+ $this->SetXY($x,$y); $this->Cell(0,0,_('Previous Due'));
+ $this->SetXY($x,$y); $this->Cell(0,0,$this->io->other_due(TRUE),0,0,'R');
+
+ $y = 219;
+ $this->SetFont('helvetica','',9);
+ $this->SetXY($x,$y); $this->Cell(0,0,_('Amount Due').' '.$this->io->display('due_date'));
+ $this->SetXY($x,$y); $this->Cell(0,0,$this->io->due(TRUE),0,0,'R');
+
+ # Total Due
+ $y = 224;
+ $this->SetFont('helvetica','B',10);
+ $this->SetXY($x,$y); $this->Cell(0,0,_('Total Due'));
+ $this->SetXY($x,$y); $this->Cell(0,0,Currency::display($this->io->due() ? $this->io->account->invoices_due_total() : 0),0,0,'R');
+
+ # Draw the Customers Address
+ $x = 25; $y = 248;
+
+ $this->SetFont('helvetica','B',12);
+
+ if ($this->billToCompany && ! empty($this->io->account->company))
+ $name = $this->io->account->company;
+ else
+ $name = $this->io->account->name();
+
+ $this->SetXY($x,$y); $this->Cell(0,0,html_entity_decode($name,ENT_NOQUOTES)); $y += 5;
+ $this->SetXY($x,$y); $this->Cell(0,0,sprintf('%s %s ',$this->io->account->address1,$this->io->account->address2)); $y += 5;
+ $this->SetXY($x,$y); $this->Cell(0,0,sprintf('%s, %s %s',$this->io->account->city,$this->io->account->state,$this->io->account->zip)); $y += 5;
+ }
+
+ public function drawInvoiceHeader() {
+ $x = 125; $y = 10;
+
+ # Draw a box.
+ $this->SetFillColor(245);
+ $this->SetXY($x-1,$y-1); $this->Cell(0,35+($this->io->billed_amt ? 5 : 0)+($this->io->credit_amt ? 5 : 0),'',1,0,'',1);
+
+ # Draw a box around the invoice due date and amount due.
+ $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->invnum(),0,0,'R');
+
+ # Invoice number at top of page.
+ $y += 7;
+ $this->SetFont('helvetica','',10);
+ $this->SetXY($x,$y); $this->Cell(0,0,_('Issue Date')); $y += 5;
+ $this->SetXY($x,$y); $this->Cell(0,0,_('Amount Due'));
+
+ $y -= 5;
+ $this->SetFont('helvetica','B',11);
+ $this->SetXY($x,$y); $this->Cell(0,0,$this->io->display('date_orig'),0,0,'R'); $y += 5;
+ $this->SetXY($x,$y); $this->Cell(0,0,$this->io->display('due_date'),0,0,'R');
+
+ $y += 5;
+ $this->SetFont('helvetica','',10);
+ $this->SetXY($x,$y); $this->Cell(0,0,_('Previous Due'));
+ $this->SetFont('helvetica','B',11);
+ $this->SetXY($x+55,$y); $this->Cell(0,0,$this->io->other_due(TRUE),0,0,'R');
+
+ $y += 5;
+ $this->SetFont('helvetica','',10);
+ $this->SetXY($x,$y); $this->Cell(0,0,_('Current Charges'));
+ $this->SetFont('helvetica','B',11);
+ $this->SetXY($x+55,$y); $this->Cell(0,0,$this->io->total(TRUE),0,0,'R');
+
+ if ($this->io->billed_amt) {
+ $y += 5;
+ $this->SetFont('helvetica','',10);
+ $this->SetXY($x,$y); $this->Cell(0,0,'Payments Received');
+ $this->SetFont('helvetica','B',11);
+ $this->SetXY($x+55,$y); $this->Cell(0,0,$this->io->display('billed_amt'),0,0,'R');
+ }
+
+ if ($this->io->credit_amt) {
+ $y += 5;
+ $this->SetFont('helvetica','',10);
+ $this->SetXY($x,$y); $this->Cell(0,0,'Credits Received');
+ $this->SetFont('helvetica','B',11);
+ $this->SetXY($x+55,$y); $this->Cell(0,0,$this->io->display('credit_amt'),0,0,'R');
+ }
+
+ $y += 5;
+ $this->SetFont('helvetica','',10);
+ $this->SetXY($x,$y); $this->Cell(0,0,'Total Payable');
+ $this->SetFont('helvetica','B',11);
+ $this->SetXY($x+55,$y); $this->Cell(0,0,Currency::display($this->io->due() ? $this->io->account->invoices_due_total() : 0),0,0,'R');
+ }
+
+ #@todo Limit the size of the news to 6 lines
+ public function drawNews($news) {
+ if (! $news)
+ return;
+
+ $x = 9; $y = 170;
+
+ # Draw a box.
+ $this->SetFillColor(243);
+ $this->SetXY($x-1,$y-1); $this->Cell(0,20,'',1,0,'',1);
+
+ $this->SetFont('helvetica','',8);
+ $this->SetXY($x,$y); $this->MultiCell(0,3,str_replace('\n',"\n",$news),0,'L',0);
+ }
+
+ #@todo make this list dynamic
+ public function drawPaymentMethods() {
+ $x = 120; $y = 242;
+
+ # Draw a box.
+ $this->SetFillColor(235);
+ $this->SetXY($x-1,$y-2); $this->Cell(0,32,'',1,0,'',1);
+
+ $this->SetFont('helvetica','B',8);
+ $this->SetXY($x,$y); $this->Cell(0,0,'This invoice can also be paid by:'); $y += 4;
+
+ # Direct Credit
+ $logo = Kohana::find_file('media','img/invoice-payment-dd','png');
+ $this->Image($logo,$x+1,$y,8);
+ $this->SetFont('helvetica','B',8);
+ $this->SetXY($x+10,$y); $this->Cell(0,0,'Direct Credit to our Bank Account'); $y += 3;
+ $this->SetFont('helvetica','',8);
+ $this->SetXY($x+10,$y); $this->Cell(0,0,'BSB:'); $y += 3;
+ $this->SetXY($x+10,$y); $this->Cell(0,0,'ACCOUNT:'); $y += 3;
+ $this->SetXY($x+10,$y); $this->Cell(0,0,'REF:'); $y += 3;
+
+ $y -= 9;
+ $this->SetFont('helvetica','B',8);
+ $this->SetXY($x+30,$y); $this->Cell(0,0,Company::bsb()); $y += 3;
+ $this->SetXY($x+30,$y); $this->Cell(0,0,Company::account()); $y += 3;
+ $this->SetXY($x+30,$y); $this->Cell(0,0,$this->io->refnum()); $y += 3;
+
+/*
+ # Direct Debit
+ $y += 3;
+ $logo = sprintf('%s/%s',PATH_THEMES.DEFAULT_THEME,'invoice/invoice-payment-dd.png');
+ $this->Image($logo,$x+1,$y,8);
+ $this->SetFont('helvetica','B',8);
+ $this->SetXY($x+10,$y); $this->Cell(0,0,'Direct Debit'); $y += 3;
+ $this->SetFont('helvetica','',8);
+ $this->SetXY($x+10,$y); $this->Cell(0,0,'Please visit '); $this->SetXY($x+30,$y); $this->addHtmlLink($inv->print['site']['URL'].'?_page=invoice:user_view&id='.$inv->getPrintInvoiceNum(),$inv->print['site']['URL']); $y += 3;
+*/
+
+ # Paypal
+ $y += 3;
+ $logo = Kohana::find_file('media','img/invoice-payment-pp','png');
+ $this->Image($logo,$x+1,$y,8);
+ $this->SetFont('helvetica','B',8);
+ $this->SetXY($x+10,$y); $this->Cell(0,0,'Pay Pal/Credit Card'); $y += 3;
+ $this->SetFont('helvetica','',8);
+ $this->SetXY($x+10,$y); $this->Cell(0,0,'Please visit '); $this->SetXY($x+30,$y); $this->addHtmlLink(URL::base(TRUE,TRUE),URL::base(TRUE,TRUE)); $y += 3;
+ }
+
+ /**
+ * Draw previous invoices due
+ */
+ public function drawSummaryInvoicesDue() {
+ $x = 125; $y = $this->sum_y ? $this->sum_y : 50;
+
+ $items = $this->io->account->invoices_due();
+
+ # Calculate the box size
+ $box = count($items) < $this->itemsPreviousMax ? count($items) : $this->itemsPreviousMax;
+
+ # Draw a box.
+ $this->SetFillColor(245);
+ $this->SetXY($x-1,$y-1); $this->Cell(0,5*(1+$box)+1,'',1,0,'',1);
+
+ $this->SetFont('helvetica','B',11);
+ $this->SetXY($x,$y); $this->Cell(0,0,_('Previous Invoices due')); $y += 5;
+
+ $this->SetFont('helvetica','',11);
+ $i = 0;
+ $sum_total = 0;
+ foreach ($items as $line) {
+ if (++$i < $this->itemsPreviousMax) {
+ $this->SetXY($x,$y);
+ $this->Cell(0,0,sprintf('%s #%s',$line->display('date_orig'),$line->invnum()));
+ $this->Cell(0,0,$line->due(TRUE),0,0,'R'); $y += 5;
+
+ } else {
+ $sum_total += $line->due();
+ }
+ }
+
+ if ($sum_total) {
+ $this->SetXY($x,$y);
+ $this->SetFont('helvetica','I',11);
+ $this->Cell(0,0,'Other invoices');
+ $this->SetFont('helvetica','',11);
+ $this->Cell(0,0,Currency::display($sum_total),0,0,'R'); $y += 5;
+ }
+
+ $this->sum_y = $y+5;
+ }
+
+ /**
+ * Called before begining to loop the invoice_item table. Used to set initial values.
+ */
+ public function drawLineItems($iteration) {
+ $this->iteration = $iteration;
+ if ($iteration>1)
+ return false;
+
+ return true;
+ }
+
+ /**
+ * Called once per line item to add to the PDF invoice. This function serves to
+ * direct each iteration to a different function which handles a specific piece
+ * of the PDF building puzzle.
+ */
+ public function drawLineItem($line) {
+ switch($this->iteration) {
+ case 0:
+ $this->drawLineItems_0($line);
+ break;
+
+ default:
+ echo 'Unknown PDF iteration encountered. Halting.';
+ exit;
+ }
+ }
+
+ /**
+ * Draws the non-VoIP related items for iteration 0.
+ * @todo need to make sure that this pages well, when there are many items (with many sub details).
+ * @tood Need to replicate this to the other fpdf files
+ */
+ private function drawLineItems_0($line) {
+ if ($line['price_type'] == 0 && $line['item_type'] == 5)
+ return;
+
+ $x = 10;
+ if ($this->i == 0 || $this->i%51 == 0) {
+ $this->y = 5;
+ $this->AddPage();
+
+ $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 #').$invnum,0,0,'C');
+
+ # Draw table headers
+ $this->y += 10;
+ $this->SetFont('helvetica','B',8);
+ $this->SetXY($x,$this->y);
+ $this->Cell(0,0,_('Description'));
+ $this->SetX($x+135);
+ $this->Cell(0,0,_('Quantity'));
+ $this->SetX($x+160);
+ $this->Cell(10,0,_('Unit Cost'),0,0,'R');
+ $this->SetX($x+135);
+ $this->Cell(0,0,_('Amount'),0,0,'R');
+ $this->Line($x,$this->y+4,200,$this->y+4);
+ $this->y += 5;
+ $this->SetY($this->y);
+ }
+
+ $this->SetFont('helvetica','',8);
+ $this->SetX($x);
+ $this->Cell(0,0,$line['name']);
+
+ if (isset($line['price_base'])) {
+ $this->SetX($x+160);
+ $this->Cell(10,0,$this->_currency($line['price_base']),0,0,'R');
+ }
+
+ if (isset($line['qty'])) {
+ $this->SetX($x+130);
+ $this->Cell(10,0,$line['qty'],0,0,'R');
+ }
+
+ $this->SetX($x+130);
+ $this->Cell(0,0,$this->_currency($line['total_amt']),0,0,'R');
+
+ if ($this->show_service_range && $line['daterange']) {
+ $this->SetFont('helvetica','I',7);
+ $this->y += 3;
+ $this->SetXY($x+10,$this->y); $this->Cell(0,0,'Service Period');
+ $this->SetFont('helvetica','',7);
+ $this->SetXY($x+40,$this->y); $this->Cell(0,0,$line['daterange']);
+ }
+
+ if ($line['domain']) {
+ $this->SetFont('helvetica','I',7);
+ $this->y += 3;
+ $this->SetXY($x+10,$this->y); $this->Cell(0,0,'Domain');
+ $this->SetFont('helvetica','',7);
+ $this->SetXY($x+40,$this->y); $this->Cell(0,0,$line['domain']);
+ }
+
+ if ($line['attr']) {
+ $showchars = 20;
+ if (preg_match('/^a:/',$line['attr']))
+ $a = unserialize($line['attr']);
+ else {
+ $x = explode("\n",$line['attr']);
+ $a = array();
+ foreach ($x as $y)
+ if ($y) {
+ list($c,$d) = explode('==',$y);
+ $a[$c] = $d;
+ }
+ }
+
+ foreach ($a as $field=>$value) {
+
+ if (in_array($field,array('service_account_name','service_address')))
+ continue;
+
+ $this->SetFont('helvetica','I',7);
+ $this->y += 3;
+ $this->SetXY(20,$this->y); $this->Cell(0,0,strlen($field) > $showchars ? substr($field,0,$showchars-2).'...' : $field);
+ $this->SetFont('helvetica','',7);
+ $this->SetXY(50,$this->y); $this->Cell(0,0,$value);
+ }
+ }
+
+ $this->y += 5;
+ $this->SetY($this->y);
+ $this->i++;
+ }
+
+ /**
+ * This will draw the Summary Box, with the summary of the items
+ * on the invoice.
+ */
+ public function drawSummaryLineItems() {
+ if (! $this->show_itemized)
+ return;
+
+ $items = $this->io->items_summary();
+
+ # Calculate the box size
+ $box = count($items) < $this->itemsSummaryMax ? count($items) : $this->itemsSummaryMax;
+
+ $x = 10; $y = $this->sum_y ? $this->sum_y : 55;
+
+ # Draw a box.
+ $this->SetFillColor(245);
+ $this->SetXY($x-1,$y-1); $this->Cell(0,5*(
+ 1+1+1+3+($this->io->discount_amt ? 1 : 0)+($this->io->billed_amt ? 1 : 0)+($this->io->credit_amt ? 1 : 0)+$box
+ )+1,'',1,0,'',1);
+
+ $this->SetFont('helvetica','B',11);
+ $this->SetXY($x,$y); $this->Cell(0,0,_('Current Charges Summary for')); $y += 5;
+
+ $this->SetY($y);
+ $this->SetFont('helvetica','',9);
+
+ $i=0;
+ foreach($items as $line) {
+ $this->SetX($x);
+
+ $q = $line->quantity;
+ if (empty($q))
+ $q = 1;
+
+ $this->Cell(0,0,$q);
+ $this->SetX($x+8);
+ $this->Cell(0,0,sprintf('%s (%s)',$line->product->product_translate->find()->name,Currency::display($line->price_base)));
+ $this->SetX($x+135);
+ $this->Cell(0,0,Currency::display($line->price_base*$line->quantity+$line->price_setup),0,0,'R');
+ $y += 5;
+ $this->SetY($y);
+ $i++;
+ if ($i > $this->itemsSummaryMax) {
+ $this->SetFont('helvetica','B',11);
+ $this->SetX($x);
+ $this->Cell(0,0,_('The above is just a summary. To view a detailed list of charges, please visit our website.'));
+ break;
+ }
+ }
+
+ # Calculate our rounding error
+ $subtotal = 0;
+ foreach($items as $line)
+ $subtotal += $line->price_base*$line->quantity+$line->price_setup;
+
+ $subtotal -= $this->io->discount_amt;
+ $subtotal = round($subtotal,2);
+
+ if (round($this->io->total_amt-$this->io->tax_amt,2) != $subtotal) {
+ $this->SetFont('helvetica','',9);
+ $this->SetX($x);
+ $this->Cell(0,0,'Rounding');
+ $this->SetX($x+135);
+ $this->Cell(0,0,
+ Currency::display($this->io->total_amt-$this->io->tax_amt-$subtotal),0,0,'R');
+ $y += 5;
+ $this->SetY($y);
+ }
+
+ # Draw Discounts.
+ if ($this->io->discount_amt) {
+ $y += 5;
+ $this->SetY($y);
+ $this->SetFont('helvetica','B',9);
+ $this->SetX($x+8);
+ $this->Cell(0,0,_('Discount'));
+ $this->SetX($x+135);
+ $this->Cell(0,0,Currency::display(-$this->io->discount_amt),0,0,'R');
+ }
+
+ # Sub total and tax.
+ $y += 5;
+ $this->SetY($y);
+ $this->SetFont('helvetica','B',9);
+ $this->SetX($x+8);
+ $this->Cell(0,0,'Sub Total');
+ $this->SetX($x+135);
+ $this->Cell(0,0,Currency::display($this->io->total_amt-$this->io->tax_amt),0,0,'R');
+
+ $y += 5;
+ $this->SetY($y);
+ $this->SetX($x+8);
+ $this->Cell(0,0,'Taxes');
+ $this->SetX($x+135);
+ $this->Cell(0,0,Currency::display($this->io->tax_amt),0,0,'R');
+
+ $y += 5;
+ $this->SetY($y);
+ $this->SetX($x+8);
+ $this->Cell(0,0,'Total Charges');
+ $this->SetX($x+135);
+ $this->Cell(0,0,Currency::display($this->io->total_amt),0,0,'R');
+
+ # Show payments already received for this invoice
+ if ($this->io->billed_amt) {
+ $y += 5;
+ $this->SetY($y);
+ $this->SetX($x+8);
+ $this->Cell(0,0,'Payments Received');
+ $this->SetX($x+135);
+ $this->Cell(0,0,Currency::display($this->io->billed_amt),0,0,'R');
+ }
+
+ if ($this->io->credit_amt) {
+ $y += 5;
+ $this->SetY($y);
+ $this->SetFont('helvetica','B',9);
+ $this->SetX($x+8);
+ $this->Cell(0,0,_('Less Credits'));
+ $this->SetX($x+135);
+ $this->Cell(0,0,Currency::display(-$this->io->credit_amt),0,0,'R');
+ }
+
+ $y += 5;
+ $this->SetY($y);
+ $this->SetX($x+8);
+ $this->Cell(0,0,'Balance Due');
+ $this->SetX($x+135);
+ $this->Cell(0,0,$this->io->due(TRUE),0,0,'R');
+ }
+}
+?>
diff --git a/modules/invoice/classes/model/invoice.php b/modules/invoice/classes/model/invoice.php
index be7d7c44..4a6e6e7e 100644
--- a/modules/invoice/classes/model/invoice.php
+++ b/modules/invoice/classes/model/invoice.php
@@ -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() {
diff --git a/themes/default/invoice/invoice-payment-dd.png b/modules/invoice/media/img/invoice-payment-dd.png
similarity index 100%
rename from themes/default/invoice/invoice-payment-dd.png
rename to modules/invoice/media/img/invoice-payment-dd.png
diff --git a/themes/default/invoice/invoice-payment-pp.png b/modules/invoice/media/img/invoice-payment-pp.png
similarity index 100%
rename from themes/default/invoice/invoice-payment-pp.png
rename to modules/invoice/media/img/invoice-payment-pp.png
diff --git a/modules/service/classes/model/service.php b/modules/service/classes/model/service.php
index 5f718bcc..3dfa5d74 100644
--- a/modules/service/classes/model/service.php
+++ b/modules/service/classes/model/service.php
@@ -53,43 +53,6 @@ class Model_Service extends ORMOSB {
return $this->product->product_translate->find()->name;
}
- /**
- * Find invoices associated with this service
- */
- public function invoices() {
- $return = array();
-
- foreach ($this->invoice->distinct('id')->find_all() as $invoice) {
- $return[$invoice->id]['due'] = $invoice->due();
- }
-
- return $return;
- }
-
- /**
- * Find invoices currently outstanding associated with this service
- */
- public function invoices_due() {
- $return = array();
-
- foreach ($this->invoices() as $id => $invoice)
- if ($invoice['due'])
- array_push($return,$invoice);
-
- return $return;
- }
-
- /**
- * Calculate the total of invoices due for this service
- */
- public function invoices_due_total() {
- $total = 0;
- foreach ($this->invoices_due() as $invoice)
- $total += $invoice['due'];
-
- return $total;
- }
-
// @todo To implement
/**
* Calculate the tax for this item
diff --git a/modules/service/views/service/list/adslservices_header.php b/modules/service/views/service/list/adslservices_header.php
index 69dbd986..a23769d0 100644
--- a/modules/service/views/service/list/adslservices_header.php
+++ b/modules/service/views/service/list/adslservices_header.php
@@ -5,7 +5,7 @@
account->accnum(),$service->account->name()); ?> | -invoices_due_total())); ?> | +account->invoices_due_total())); ?> |