Improvements to invoice

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

View File

@@ -201,7 +201,6 @@ class Controller_Task_Invoice extends Controller_Task {
$iio->quantity = $pdata['prorata'];
$iio->item_type = 0; // Service Billing
$iio->discount_amt = NULL; // @todo
$iio->price_type = $so->product->price_type;
$iio->price_base = $so->price();
$iio->recurring_schedule = $so->recur_schedule;
$iio->date_start = $pdata['start_time'];

View File

@@ -64,7 +64,6 @@ class Controller_User_Invoice extends Controller_Invoice {
throw HTTP_Exception::factory(403,'Service either doesnt exist, or you are not authorised to see it');
$output .= View::factory('invoice/user/view')
->set('mediapath',Route::get('default/media'))
->set('o',$io);
if ($io->due() AND ! $io->cart_exists())

View File

@@ -0,0 +1,16 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This interface ensures that all invoice processing objects have the right methods
*
* @package Invoice
* @category Interface
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
interface Invoicable {
// Render the line item for an invoice
public function invoice_line();
}
?>

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));
}
}
/**

View File

@@ -75,15 +75,95 @@ class Model_Invoice extends ORM_OSB implements Cartable {
/**
* Add an item to an invoice
*/
public function add_item() {
public function add_item(Model_Invoice_Item $iio=NULL) {
// Just to check if an invoice is called from the DB, that it should have items with it.
if ($this->loaded() and ! $this->invoice_items)
throw new Kohana_Exception('Need to load invoice_items?');
throw HTTP_Exception::factory(501,'Need dto load invoice_items?');
$c = count($this->invoice_items);
// @todo This is the old calling of this function, which should be removed
if (is_null($iio)) {
$c = count($this->invoice_items);
$this->invoice_items[$c] = ORM::factory('Invoice_Item');
$this->invoice_items[$c] = ORM::factory('Invoice_Item');
return $this->invoice_items[$c];
return $this->invoice_items[$c];
}
array_push($this->invoice_items,$iio);
}
/**
* Return the recurring schedules that are on an invoice
*
* @param $period Return an Array of items for that period
*/
public function items_periods($period=NULL) {
$result = array();
foreach ($this->items() as $ito) {
// We are only interested in item_type=0
if ($ito->item_type != 0)
continue;
if (is_null($period) AND ! in_array($ito->recurring_schedule,$result))
array_push($result,$ito->recurring_schedule);
elseif ($ito->recurring_schedule == $period)
array_push($result,$ito);
}
return $result;
}
/**
* Return the extra items on an invoice relating to an invoice_item
* IE: item_type != 0
*/
public function service_items_extra(Model_Invoice_Item $o) {
$result = array();
// At the moment, we only return extra items pertaining to a service
if (! $o->service_id)
return $result;
foreach ($this->items() as $iio)
if ($iio->item_type != 0 AND $iio->service_id == $o->service_id)
array_push($result,$iio);
return $result;
}
/**
* Return the total of all items relating to a service
*/
public function service_items_tax(Model_Invoice_Item $o,$format=FALSE) {
$result = 0;
// At the moment, we only return extra items pertaining to a service
if (! $o->service_id)
return $result;
foreach ($this->items() as $iio)
if ($iio->service_id == $o->service_id)
$result += $iio->tax();
return $format ? Currency::display($result) : $result;
}
/**
* Return the total of all items relating to a service
*/
public function service_items_total(Model_Invoice_Item $o,$format=FALSE) {
$result = 0;
// At the moment, we only return extra items pertaining to a service
if (! $o->service_id)
return $result;
foreach ($this->items() as $iio)
if ($iio->service_id == $o->service_id)
$result += $iio->total();
return $format ? Currency::display($result) : $result;
}
/**

View File

@@ -9,7 +9,7 @@
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Invoice_Item extends ORM_OSB {
class Model_Invoice_Item extends ORM_OSB implements Invoicable {
// Relationships
protected $_belongs_to = array(
'product'=>array(),
@@ -39,6 +39,16 @@ class Model_Invoice_Item extends ORM_OSB {
private $subitems = array();
private $subitems_loaded = FALSE;
/** INTERFACE REQUIREMENTS **/
public function invoice_line() {
if ($this->charge_id)
return $this->charge->description.' '.$this->period();
elseif ($this->service_id)
return 'Service '.$this->period();
else
throw HTTP_Exception::factory(501,'Unable to render invoice item :id',array(':id'=>$this->id));
}
public function __construct($id = NULL) {
// Load our model.
parent::__construct($id);
@@ -71,7 +81,7 @@ class Model_Invoice_Item extends ORM_OSB {
}
// Sum up the tax that applies to this invoice item
public function tax() {
public function tax($format=FALSE) {
$result = 0;
foreach ($this->invoice_item_tax->find_all() as $iit)
@@ -81,7 +91,9 @@ class Model_Invoice_Item extends ORM_OSB {
if (! $result)
$result += round($this->price_base*$this->quantity*.1,2);
return Currency::round($result);
$result = Currency::round($result);
return $format ? Currency::display($result) : $result;
}
public function tax_items() {
@@ -98,8 +110,10 @@ class Model_Invoice_Item extends ORM_OSB {
}
// This total of this item before discounts and taxes
public function subtotal() {
return Currency::round($this->price_base*$this->quantity);
public function subtotal($format=FALSE) {
$result = Currency::round($this->price_base*$this->quantity);
return $format ? Currency::display($result) : $result;
}
// The total of all discounts
@@ -107,8 +121,10 @@ class Model_Invoice_Item extends ORM_OSB {
return Currency::round($this->discount_amt);
}
public function total() {
return Currency::round($this->subtotal()+$this->tax()-$this->discount());
public function total($format=FALSE) {
$result = Currency::round($this->subtotal()+$this->tax()-$this->discount());
return $format ? Currency::display($result) : $result;
}
/**