2010-11-29 22:41:08 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides invoice information
|
|
|
|
*
|
2013-03-19 22:35:19 +00:00
|
|
|
* @package Invoice
|
2010-11-29 22:41:08 +00:00
|
|
|
* @category Helpers
|
|
|
|
* @author Deon George
|
2013-03-19 22:35:19 +00:00
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
|
|
|
* @license http://dev.osbill.net/license.html
|
2010-11-29 22:41:08 +00:00
|
|
|
*/
|
|
|
|
class Invoice {
|
2011-05-02 12:28:17 +00:00
|
|
|
// This invoice Object
|
|
|
|
private $io;
|
|
|
|
|
2013-06-13 13:35:19 +00:00
|
|
|
public function __construct(Model_Invoice $io=NULL) {
|
|
|
|
$this->io = is_null($io) ? ORM::factory('Invoice') : $io;
|
2011-07-14 09:09:03 +00:00
|
|
|
}
|
|
|
|
|
2013-06-13 13:35:19 +00:00
|
|
|
public static function instance(Model_Invoice $io=NULL) {
|
2011-07-14 09:09:03 +00:00
|
|
|
return new Invoice($io);
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-13 13:35:19 +00:00
|
|
|
* Add a Service to an Invoice
|
2010-11-29 22:41:08 +00:00
|
|
|
*
|
2013-06-13 13:35:19 +00:00
|
|
|
* @param $so Model_Servie
|
2010-11-29 22:41:08 +00:00
|
|
|
*/
|
2013-06-13 13:35:19 +00:00
|
|
|
public function add_service(Model_Service $so) {
|
|
|
|
$pdata = Period::details($so->recur_schedule,$so->product->price_recurr_day,$so->invoiced_to()+86400,FALSE,$so->product->price_recurr_strict);
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-06-13 13:35:19 +00:00
|
|
|
$iio = ORM::factory('Invoice_Item');
|
|
|
|
$iio->service_id = $so->id;
|
|
|
|
$iio->product_id = $so->product_id;
|
|
|
|
$iio->quantity = $pdata['prorata'];
|
|
|
|
$iio->price_base = $so->price();
|
|
|
|
$iio->recurring_schedule = $so->recur_schedule;
|
|
|
|
$iio->date_start = $pdata['start_time'];
|
|
|
|
$iio->date_stop = $pdata['end_time'];
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-06-13 13:35:19 +00:00
|
|
|
// Service Billing
|
|
|
|
$iio->item_type = StaticList_ItemType::index('MAIN');
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-06-13 13:35:19 +00:00
|
|
|
$this->io->add_item($iio);
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-06-13 13:35:19 +00:00
|
|
|
// Check if there are any charges
|
|
|
|
$c = ORM::factory('Charge')
|
|
|
|
->where('service_id','=',$so->id)
|
|
|
|
->where('processed','is',NULL);
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-06-13 13:35:19 +00:00
|
|
|
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->quantity = $co->quantity;
|
|
|
|
$iio->price_base = $co->amount;
|
|
|
|
$iio->date_start = $co->date_orig;
|
|
|
|
$iio->date_stop = $co->date_orig;
|
|
|
|
|
|
|
|
// @todo This should be $co->item_type;
|
|
|
|
$iio->item_type = StaticList_ItemType::index('EXCESS');
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-06-13 13:35:19 +00:00
|
|
|
$this->io->add_item($iio);
|
|
|
|
}
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-06-13 13:35:19 +00:00
|
|
|
return $this;
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2013-06-13 13:35:19 +00:00
|
|
|
public function render($type,$section) {
|
|
|
|
switch ($type) {
|
|
|
|
case 'html':
|
|
|
|
switch ($section) {
|
|
|
|
case 'body':
|
|
|
|
return View::factory('invoice/user/view/body')
|
|
|
|
->set('o',$this->io);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw HTTP_Exception::factory(501,'Unknown section type :section',array(':section'=>$section));
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw HTTP_Exception::factory(501,'Unknown render type :type',array(':type'=>$type));
|
|
|
|
}
|
2011-05-02 12:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a PDF invoice
|
|
|
|
*/
|
2011-07-14 09:09:03 +00:00
|
|
|
public function pdf() {
|
2013-03-22 20:37:04 +00:00
|
|
|
$invoice_class = Kohana::classname('Invoice_TCPDF_'.Kohana::$config->load('invoice')->driver);
|
2011-05-02 12:28:17 +00:00
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
$pdf = new $invoice_class($this->io);
|
2011-05-02 12:28:17 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2011-08-26 01:16:48 +00:00
|
|
|
if ($this->io->billing_status !=1 && $this->io->due_date <= time())
|
2011-07-14 09:09:03 +00:00
|
|
|
$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();
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|