This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/invoice/classes/Invoice.php

176 lines
4.6 KiB
PHP
Raw Normal View History

2013-10-10 02:44:53 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides invoice information
*
* @package Invoice
* @category Helpers
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Invoice {
// This invoice Object
2013-12-05 05:22:23 +00:00
private $_io;
2013-10-10 02:44:53 +00:00
private $_methods = array(
2013-12-05 05:22:23 +00:00
'dump',
'min_due',
'save',
'saved',
2013-12-05 05:22:23 +00:00
'total',
);
// Enable passing calls to ORM::Invoice()
public function __call($method,array $args) {
if (in_array($method,$this->_methods))
2013-12-05 05:22:23 +00:00
return call_user_func_array(array($this->_io,$method),$args);
else
throw HTTP_Exception::factory(501,'Unknown/unauthorised method :method',array(':method'=>$method));
}
2013-06-13 13:35:19 +00:00
public function __construct(Model_Invoice $io=NULL) {
2013-12-05 05:22:23 +00:00
$this->_io = is_null($io) ? ORM::factory('Invoice') : $io;
// Set our invoice as valid
if (is_null($io))
2013-12-05 05:22:23 +00:00
$this->_io->status = 1;
2013-10-10 02:44:53 +00:00
}
2013-06-13 13:35:19 +00:00
public static function instance(Model_Invoice $io=NULL) {
2013-10-10 02:44:53 +00:00
return new Invoice($io);
}
/**
2013-06-13 13:35:19 +00:00
* Add a Service to an Invoice
2013-10-10 02:44:53 +00:00
*
2013-06-13 13:35:19 +00:00
* @param $so Model_Servie
2013-10-10 02:44:53 +00:00
*/
2013-06-13 13:35:19 +00:00
public function add_service(Model_Service $so) {
2013-12-05 05:22:23 +00:00
if ($this->_io->loaded())
throw HTTP_Exception::factory(501,'Cannot use add service :service to an existing invoice :invoice ',array(':service'=>$so->id,':invoice'=>$this->_io->id));
2013-12-05 05:22:23 +00:00
if (! $this->_io->account_id)
$this->_io->account_id = $so->account_id;
2013-12-05 05:22:23 +00:00
else if ($this->_io->account_id != $so->account_id)
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
2013-12-05 05:22:23 +00:00
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);
2013-06-13 13:35:19 +00:00
$pdata = Period::details($so->recur_schedule,$so->product->price_recurr_day,$so->invoiced_to()+86400,FALSE,$so->product->price_recurr_strict);
2013-10-10 02:44:53 +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'];
2013-10-10 02:44:53 +00:00
2013-06-13 13:35:19 +00:00
// Service Billing
$iio->item_type = 0;
2013-10-10 02:44:53 +00:00
2013-12-05 05:22:23 +00:00
$this->_io->subitem_add($iio,$this->_io->account->country);
2013-10-10 02:44:53 +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('void','is',NULL)
2013-06-13 13:35:19 +00:00
->where('processed','is',NULL);
2013-10-10 02:44:53 +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;
$iio->item_type = $co->type;
2013-10-10 02:44:53 +00:00
2013-12-05 05:22:23 +00:00
$this->_io->subitem_add($iio,$this->_io->account->country);
2013-06-13 13:35:19 +00:00
}
2013-10-10 02:44:53 +00:00
2013-06-13 13:35:19 +00:00
return $this;
2013-10-10 02:44:53 +00:00
}
public function render($type,$section,$args=array()) {
2013-06-13 13:35:19 +00:00
switch ($type) {
case 'html':
switch ($section) {
case 'body':
return View::factory('invoice/user/view/body')
->set('show_id',(isset($args['noid']) AND $args['noid']) ? FALSE : TRUE)
2013-12-05 05:22:23 +00:00
->set('o',$this->_io);
2013-06-13 13:35:19 +00:00
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));
}
2013-10-10 02:44:53 +00:00
}
/**
* Generate a PDF invoice
*/
public function pdf() {
$invoice_class = Kohana::classname('Invoice_TCPDF_'.Kohana::$config->load('invoice')->driver);
2013-12-05 05:22:23 +00:00
$pdf = new $invoice_class($this->_io);
2013-10-10 02:44:53 +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();
2013-12-05 05:22:23 +00:00
if ($this->_io->billing_status !=1 && $this->_io->due_date <= time())
2013-10-10 02:44:53 +00:00
$pdf->drawInvoiceDueNotice();
2013-12-05 05:22:23 +00:00
elseif($this->_io->billing_status == 1)
2013-10-10 02:44:53 +00:00
$pdf->drawInvoicePaidNotice();
2013-12-05 05:22:23 +00:00
if ($this->_io->account->invoices_due_total())
2013-10-10 02:44:53 +00:00
$pdf->drawSummaryInvoicesDue();
$pdf->drawSummaryLineItems();
// Next Page
$pdf->drawDetailLineItems();
// Draw any Custom functions:
$pdf->drawCustom();
}
}
?>