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

145 lines
3.3 KiB
PHP
Raw Normal View History

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;
2011-07-14 09:09:03 +00:00
public function __construct($io) {
$this->io = $io;
}
public static function instance($io) {
return new Invoice($io);
2010-11-29 22:41:08 +00:00
}
/**
2011-05-02 12:28:17 +00:00
* Return a list of invoices for an service
2010-11-29 22:41:08 +00:00
*
* @param $id int Service ID
* @param $paid boolean Optionally only list the ones that are not paid.
* @return array
*/
2011-05-02 12:28:17 +00:00
// @todo Function Not Used
2010-11-29 22:41:08 +00:00
public static function servicelist($id,$paid=TRUE) {
// @todo need to add the db prefix
$invoices = DB::Query(Database::SELECT,'
SELECT i.id AS iid,i.due_date AS due FROM ab_invoice i,ab_invoice_item ii WHERE ii.invoice_id=i.id AND service_id=:id GROUP BY i.id
')
->param(':id',$id)
->execute();
$service_invoices = array();
foreach ($invoices as $item) {
if ($bal = Invoice::balance($item['iid']) OR $paid) {
$service_invoices[$item['iid']]['id'] = $item['iid'];
$service_invoices[$item['iid']]['total'] = $bal;
$service_invoices[$item['iid']]['due'] = $item['due'];
}
}
return $service_invoices;
}
/**
* Return the total of amount outstanding for a service
*
* @param $id int Service ID
* @param $paid boolean Optionally only list the ones that are not paid.
* @return real Total amount outstanding
* @see Invoice::listservice()
*/
2011-05-02 12:28:17 +00:00
// @todo Function Not Used
2010-11-29 22:41:08 +00:00
public static function servicetotal($id,$paid=TRUE) {
$total = 0;
foreach (Invoice::servicelist($id,$paid) as $item)
$total += $item['total'];
return $total;
}
/**
* Return the earliest due date of an outstanding invoice
*
* @param $id int Service ID
* @return datetime
*/
2011-05-02 12:28:17 +00:00
// @todo Function Not Used
2010-11-29 22:41:08 +00:00
public static function servicedue($id) {
$due = 0;
foreach (Invoice::servicelist($id,FALSE) as $item)
if ($due < $item['due'])
$due = $item['due'];
return $due;
}
2011-05-02 12:28:17 +00:00
// @todo Function Not Used
2010-11-29 22:41:08 +00:00
public static function balance($id) {
2012-11-09 23:13:57 +00:00
return ORM::factory('Invoice',$id)->due();
2011-05-02 12:28:17 +00:00
}
/**
* Generate a PDF invoice
*/
2011-07-14 09:09:03 +00:00
public function pdf() {
2012-11-09 23:13:57 +00:00
$invoice_class = sprintf('Invoice_TCPDF_%s',ucfirst(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
}
}
?>