Improvements to invoice display and other misc items
This commit is contained in:
@@ -15,7 +15,6 @@ class Invoice {
|
||||
|
||||
private $_methods = array(
|
||||
'dump',
|
||||
'min_due',
|
||||
'save',
|
||||
'saved',
|
||||
'total',
|
||||
@@ -37,10 +36,6 @@ class Invoice {
|
||||
$this->_io->status = 1;
|
||||
}
|
||||
|
||||
public static function instance(Model_Invoice $io=NULL) {
|
||||
return new Invoice($io);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a Service to an Invoice
|
||||
*
|
||||
@@ -57,14 +52,15 @@ class Invoice {
|
||||
throw HTTP_Exception::factory(501,'Cannot add service :service to invoice - it is for a different account',array(':service'=>$so->id));
|
||||
|
||||
// Set the invoice due date
|
||||
if (! $this->_io->due_date OR $this->_io->due_date > $this->_io->min_due($so->date_next_invoice))
|
||||
$this->_io->due_date = $this->_io->min_due($so->date_next_invoice);
|
||||
if (! $this->_io->due_date OR $this->_io->due_date > $this->min_due($so->date_next_invoice))
|
||||
$this->_io->due_date = $this->min_due($so->date_next_invoice);
|
||||
|
||||
$pdata = Period::details($so->recur_schedule,$so->product->price_recurr_day,$so->invoiced_to()+86400,FALSE,$so->product->price_recurr_strict);
|
||||
|
||||
$iio = ORM::factory('Invoice_Item');
|
||||
$iio->service_id = $so->id;
|
||||
$iio->product_id = $so->product_id;
|
||||
$iio->module_id = $so->product->mid();
|
||||
$iio->module_ref = $so->product_id;
|
||||
$iio->quantity = $pdata['prorata'];
|
||||
$iio->price_base = $so->price();
|
||||
$iio->recurring_schedule = $so->recur_schedule;
|
||||
@@ -85,8 +81,8 @@ class Invoice {
|
||||
foreach ($c->find_all() as $co) {
|
||||
$iio = ORM::factory('Invoice_Item');
|
||||
$iio->service_id = $co->service_id;
|
||||
$iio->product_id = $co->product_id;
|
||||
$iio->charge_id = $co->id;
|
||||
$iio->module_id = $co->mid();
|
||||
$iio->module_ref = $co->id;
|
||||
$iio->quantity = $co->quantity;
|
||||
$iio->price_base = $co->amount;
|
||||
$iio->date_start = $co->date_orig;
|
||||
@@ -99,8 +95,82 @@ class Invoice {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw an invoice with a summary first page, and a detail subsequent pages
|
||||
*/
|
||||
private function draw_summary_invoice(Invoice_TCPDF $pdfo) {
|
||||
// Draw Invoice Basics
|
||||
$pdfo->drawCompanyLogo();
|
||||
$pdfo->drawCompanyAddress();
|
||||
$pdfo->drawInvoiceHeader();
|
||||
// @todo Get news from DB
|
||||
$pdfo->drawNews('');
|
||||
$pdfo->drawRemittenceStub();
|
||||
$pdfo->drawPaymentMethods();
|
||||
|
||||
if ($this->_io->billing_status !=1 && $this->_io->due_date <= time())
|
||||
$pdfo->drawInvoiceDueNotice();
|
||||
elseif ($this->_io->billing_status == 1)
|
||||
$pdfo->drawInvoicePaidNotice();
|
||||
|
||||
if ($this->_io->account->invoices_due_total())
|
||||
$pdfo->drawSummaryInvoicesDue();
|
||||
|
||||
$pdfo->drawSummaryLineItems();
|
||||
|
||||
// Next Page
|
||||
$pdfo->drawDetailLineItems();
|
||||
|
||||
// Draw any Custom functions:
|
||||
$pdfo->drawCustom();
|
||||
}
|
||||
|
||||
public static function instance(Model_Invoice $io=NULL) {
|
||||
return new Invoice($io);
|
||||
}
|
||||
|
||||
public function min_due($date) {
|
||||
return strtotime(date('Y-M-d',($date < time()) ? time()+ORM::factory('Invoice')->config('DUE_DAYS_MIN')*86400 : $date));
|
||||
}
|
||||
|
||||
public function render($type,$section,$args=array()) {
|
||||
$this->_io->pre_render();
|
||||
|
||||
switch ($type) {
|
||||
case 'email':
|
||||
switch ($section) {
|
||||
case 'all':
|
||||
$token = ORM::factory('Module_Method_Token')
|
||||
->method(array('invoice','user_download'))
|
||||
->account($this->_io->account)
|
||||
->expire(time()+86400*21)
|
||||
->uses(3)
|
||||
->generate();
|
||||
|
||||
$et = Email_Template::instance('task_invoice_send');
|
||||
$et->to = array('account'=>array($this->_io->account_id));
|
||||
$et->variables = array(
|
||||
'DUE'=>$this->_io->due(TRUE),
|
||||
'DUE_DATE'=>$this->_io->display('due_date'),
|
||||
'EMAIL'=>Company::instance()->email(),
|
||||
'FIRST_NAME'=>$this->_io->account->first_name,
|
||||
'HTML_INVOICE'=>View::factory('invoice/user/viewemail')->set('html',$this->render_html()),
|
||||
'INV_NUM'=>$this->_io->refnum(),
|
||||
'INV_URL'=>URL::site(URL::link('user','invoice/view/'.$this->_io->id),'http'),
|
||||
'INV_URL_DOWNLOAD'=>URL::site(URL::link('user',sprintf('invoice/download/%s?token=%s',$this->_io->id,$token)),'http'),
|
||||
'SITE_NAME'=>Company::instance()->name(),
|
||||
);
|
||||
|
||||
return $et->send();
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw HTTP_Exception::factory(501,'Unknown section type :section',array(':section'=>$section));
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'html':
|
||||
switch ($section) {
|
||||
case 'body':
|
||||
@@ -109,6 +179,26 @@ class Invoice {
|
||||
->set('o',$this->_io);
|
||||
break;
|
||||
|
||||
case 'all':
|
||||
return $this->render_html();
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw HTTP_Exception::factory(501,'Unknown section type :section',array(':section'=>$section));
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'pdf':
|
||||
switch ($section) {
|
||||
case 'all':
|
||||
if (isset($args['download']))
|
||||
return $this->render_pdf()->Output($args['download'],'D');
|
||||
else
|
||||
return $this->render_pdf();
|
||||
break;
|
||||
|
||||
default:
|
||||
throw HTTP_Exception::factory(501,'Unknown section type :section',array(':section'=>$section));
|
||||
}
|
||||
@@ -121,55 +211,37 @@ class Invoice {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a PDF invoice
|
||||
* Renders the invoice in HTML
|
||||
*/
|
||||
public function pdf() {
|
||||
$invoice_class = Kohana::classname('Invoice_TCPDF_'.Kohana::$config->load('invoice')->driver);
|
||||
private function render_html() {
|
||||
return View::factory('invoice/user/view')
|
||||
->set('o',$this->_io);
|
||||
}
|
||||
|
||||
$pdf = new $invoice_class($this->_io);
|
||||
/**
|
||||
* Renders the invoice as a PDF and returns the PDF object
|
||||
*
|
||||
* @return Object Invoice_TCPDF
|
||||
*/
|
||||
private function render_pdf() {
|
||||
$class = Kohana::classname('Invoice_TCPDF_'.Kohana::$config->load('invoice')->driver);
|
||||
$pdfo = new $class($this->_io);
|
||||
|
||||
if ($pdf->getTemplate()) {
|
||||
$pagecount = $pdf->setSourceFile($pdf->getTemplate());
|
||||
$tplidx = $pdf->ImportPage(1);
|
||||
if ($pdfo->getTemplate()) {
|
||||
$pagecount = $pdfo->setSourceFile($pdfo->getTemplate());
|
||||
$tplidx = $pdfo->ImportPage(1);
|
||||
}
|
||||
|
||||
$pdf->addPage();
|
||||
$pdfo->addPage();
|
||||
|
||||
# If we are using FPDI
|
||||
if (isset($tplidx))
|
||||
$pdf->useTemplate($tplidx);
|
||||
$pdfo->useTemplate($tplidx);
|
||||
|
||||
$this->draw_summary_invoice($pdf);
|
||||
$this->draw_summary_invoice($pdfo);
|
||||
|
||||
# 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->due_date <= time())
|
||||
$pdf->drawInvoiceDueNotice();
|
||||
elseif($this->_io->billing_status == 1)
|
||||
$pdf->drawInvoicePaidNotice();
|
||||
|
||||
if ($this->_io->account->invoices_due_total())
|
||||
$pdf->drawSummaryInvoicesDue();
|
||||
|
||||
$pdf->drawSummaryLineItems();
|
||||
|
||||
// Next Page
|
||||
$pdf->drawDetailLineItems();
|
||||
|
||||
// Draw any Custom functions:
|
||||
$pdf->drawCustom();
|
||||
return $pdfo;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
Reference in New Issue
Block a user