93 lines
2.3 KiB
PHP
93 lines
2.3 KiB
PHP
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
/**
|
|
* This class provides invoice information
|
|
*
|
|
* @package OSB
|
|
* @subpackage Invoice
|
|
* @category Helpers
|
|
* @author Deon George
|
|
* @copyright (c) 2010 Deon George
|
|
* @license http://dev.leenooks.net/license.html
|
|
*/
|
|
class Invoice {
|
|
public static function instance() {
|
|
return new Invoice;
|
|
}
|
|
|
|
/**
|
|
* Return a list of invoices for an invoice
|
|
*
|
|
* @param $id int Service ID
|
|
* @param $paid boolean Optionally only list the ones that are not paid.
|
|
* @return array
|
|
*/
|
|
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()
|
|
*/
|
|
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
|
|
*/
|
|
public static function servicedue($id) {
|
|
$due = 0;
|
|
|
|
foreach (Invoice::servicelist($id,FALSE) as $item)
|
|
if ($due < $item['due'])
|
|
$due = $item['due'];
|
|
|
|
return $due;
|
|
}
|
|
|
|
public static function balance($id) {
|
|
$invoice = ORM::factory('invoice')
|
|
->where('id','=',$id)
|
|
->find();
|
|
|
|
// @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;
|
|
}
|
|
}
|
|
?>
|