Improvements to invoice

This commit is contained in:
Deon George
2013-06-13 23:35:19 +10:00
parent 25a47cac3a
commit 6875dc3693
15 changed files with 321 additions and 200 deletions

View File

@@ -13,80 +13,78 @@ class Invoice {
// This invoice Object
private $io;
public function __construct($io) {
$this->io = $io;
public function __construct(Model_Invoice $io=NULL) {
$this->io = is_null($io) ? ORM::factory('Invoice') : $io;
}
public static function instance($io) {
public static function instance(Model_Invoice $io=NULL) {
return new Invoice($io);
}
/**
* Return a list of invoices for an service
* Add a Service to an Invoice
*
* @param $id int Service ID
* @param $paid boolean Optionally only list the ones that are not paid.
* @return array
* @param $so Model_Servie
*/
// @todo Function Not Used
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();
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);
$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'];
}
$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'];
// Service Billing
$iio->item_type = StaticList_ItemType::index('MAIN');
$this->io->add_item($iio);
// Check if there are any charges
$c = ORM::factory('Charge')
->where('service_id','=',$so->id)
->where('processed','is',NULL);
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');
$this->io->add_item($iio);
}
return $service_invoices;
return $this;
}
/**
* 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()
*/
// @todo Function Not Used
public static function servicetotal($id,$paid=TRUE) {
$total = 0;
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;
foreach (Invoice::servicelist($id,$paid) as $item)
$total += $item['total'];
default:
throw HTTP_Exception::factory(501,'Unknown section type :section',array(':section'=>$section));
}
return $total;
}
break;
/**
* Return the earliest due date of an outstanding invoice
*
* @param $id int Service ID
* @return datetime
*/
// @todo Function Not Used
public static function servicedue($id) {
$due = 0;
foreach (Invoice::servicelist($id,FALSE) as $item)
if ($due < $item['due'])
$due = $item['due'];
return $due;
}
// @todo Function Not Used
public static function balance($id) {
return ORM::factory('Invoice',$id)->due();
default:
throw HTTP_Exception::factory(501,'Unknown render type :type',array(':type'=>$type));
}
}
/**