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

@ -128,6 +128,17 @@ class Controller_Reseller_Account extends Controller_Account {
'id'=>array('url'=>URL::link('user','service/view/')),
))
);
$i = Invoice::instance();
foreach ($ao->service->list_active() as $io)
if (! $io->suspend_billing)
$i->add_service($io);
Block::factory()
->title(sprintf('Next Invoice Items for Account: %s',$ao->accnum()))
->title_icon('icon-info-sign')
->span(6)
->body($i->render('html','body'));
}
}
?>

View File

@ -15,7 +15,7 @@ class Model_Auth_UserDefault extends Model_Auth_User {
'username' => array(
array('not_empty'),
array('min_length', array(':value', 4)),
array('max_length', array(':value', 32)),
array('max_length', array(':value', 256)),
),
'email' => array(
array('not_empty'),

View File

@ -0,0 +1,28 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This is class renders Item Type responses and forms.
*
* @package OSB
* @category Helpers
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class StaticList_ItemType extends StaticList {
protected function _table() {
return array(
0=>'MAIN', // Line Charge Topic on Invoice, eg: Service Name
5=>'EXCESS', // Excess Service Item, of item 0
);
}
public static function get($value) {
return static::factory()->_get($value);
}
public static function index($key) {
return array_search($key,static::factory()->table());
}
}
?>

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'];
return $total;
default:
throw HTTP_Exception::factory(501,'Unknown section type :section',array(':section'=>$section));
}
/**
* 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;
break;
foreach (Invoice::servicelist($id,FALSE) as $item)
if ($due < $item['due'])
$due = $item['due'];
return $due;
default:
throw HTTP_Exception::factory(501,'Unknown render type :type',array(':type'=>$type));
}
// @todo Function Not Used
public static function balance($id) {
return ORM::factory('Invoice',$id)->due();
}
/**

View File

@ -75,10 +75,13 @@ 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?');
// @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');
@ -86,6 +89,83 @@ class Model_Invoice extends ORM_OSB implements Cartable {
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;
}
/**
* Return a list of valid checkout options for this invoice
*/

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

View File

@ -20,7 +20,7 @@
<div class="span5">
<div class="row">
<div class="dl-horizontal">
<div class="dl-horizontal pull-right">
<dt>Tax Invoice</dt>
<dd><?php echo $o->id(); ?></dd>
<dt>Issue Date</dt>
@ -45,112 +45,54 @@
<div class="span11">
<h4>Charge Details</h4>
<div class="accordion" id="charges">
<div class="accordion-group">
<table class="table table-striped table-condensed table-hover" id="list-table" border="0">
<tbody>
<?php foreach ($o->items_periods() as $rs) : ?>
<tr><th colspan="5"><?php echo StaticList_RecurSchedule::get($rs); ?></th></tr>
<?php foreach ($o->items_index('period') as $rs => $items) : ?>
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#charges" data-target="#collapse_<?php echo $rs; ?>">
<?php if ($rs) :
printf('%s - %s service(s)',StaticList_RecurSchedule::get($rs),count($items));
else :
echo 'Other Items';
endif ?>
</a>
</div>
<?php if ($items) : ?>
<div id="collapse_<?php echo $rs; ?>" class="accordion-body collapse in">
<table>
<?php
foreach ($items as $k=>$service_id) :
$i = 0;
$lp = NULL;
$ito_tax = NULL;
foreach ($o->items_service($service_id) as $ito) {
$ito_tax = $ito;
// Our first line we show the Service Details
if ($ito->item_type == 0 AND $ito->product_id != $lp) {
$lp = $ito->product_id; ?>
<!-- Service Information -->
<tr class="head">
<td><?php echo HTML::anchor(URL::link('user','service/view/'.$ito->service_id),$ito->service->id()); ?></td>
<td colspan="5"><?php printf('%s - %s',$ito->product->title(),$ito->service->name()); ?> (<?php echo $ito->product_id; ?>)</td>
<td class="right"><?php echo ($i++==0 ? Currency::display($o->items_service_total($ito->service_id)) : '&nbsp;');?></td>
<?php foreach ($o->items_periods($rs) as $iio) : ?>
<?php if ($iio->service_id) : ?>
<tr>
<th>&nbsp;</th>
<th colspan="2"><?php echo HTML::anchor(URL::link('user','service/view/'.$iio->service_id),$iio->service->id()).' '.$iio->service->service_name(); ?></th>
<th>&nbsp;</th>
<th><div class="text-right"><?php echo $o->service_items_total($iio,TRUE); ?></div></th>
</tr>
<!-- END Product Information -->
<?php } ?>
<!-- Product Sub Information -->
<tr>
<td>&nbsp;</td>
<td><?php echo $ito->trannum();?></td>
<td><?php echo $ito->name();?></td>
<td><?php echo $ito->detail();?></td>
<td><?php echo $ito->period();?></td>
<td class="right"><?php echo Currency::display($ito->subtotal());?>&nbsp;</td>
<td>&nbsp;</td>
<td><?php printf('%s (%s)',$iio->invoice_line(),$iio->display('id')); ?></td>
<td><div class="text-right"><?php echo $iio->subtotal(TRUE); ?></div></td>
<td>&nbsp;</td>
</tr>
<!-- END Product Sub Information -->
<!-- Service Discount Information -->
<?php if ($ito->discount_amt) { ?>
<tr>
<td colspan="4">&nbsp;</td>
<td><?php echo _('Discounts'); ?></td>
<td class="right">(<?php echo Currency::display($o->items_service_discount($ito->service_id));?>)</td>
</tr>
<?php } ?>
<!-- END Service Discount Information -->
<?php } ?>
<?php if ($ito_tax) { ?>
<!-- Product Sub Items Tax -->
<?php if ($x=$o->service_items_extra($iio)) : ?>
<?php foreach ($x as $eiio) : ?>
<tr>
<td colspan="4">&nbsp;</td>
<td><?php echo _('Taxes'); ?></td>
<td class="right"><?php echo Currency::display($o->items_service_tax($ito->service_id));?>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><?php printf('%s (%s)',$eiio->invoice_line(),$eiio->display('id')); ?></td>
<td><div class="text-right"><?php echo $eiio->subtotal(TRUE); ?></div></td>
<td>&nbsp;</td>
</tr>
<!-- END Product Sub Items Tax -->
<?php } ?>
<?php endforeach ?>
<?php endif ?>
</table>
</div>
<?php endforeach ?>
<?php if ($o->items_index('account')) : ?>
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#charges" data-target="#collapse_other">Other Items</a>
</div>
<div id="collapse_<?php echo $rs; ?>" class="accordion-body collapse">
<table>
<?php foreach ($o->items_index('account') as $id => $ito) : ?>
<tr>
<td>&nbsp;</td>
<td><?php echo $ito->trannum();?></td>
<td><?php echo $ito->name();?></td>
<td><?php echo $ito->detail();?></td>
<td><?php echo $ito->period();?></td>
<td class="right"><?php echo Currency::display($ito->subtotal());?>&nbsp;</td>
<td>&nbsp;</td>
<td><?php echo 'Taxes'; ?></td>
<td><div class="text-right"><?php echo $o->service_items_tax($iio,TRUE); ?></div></td>
<td>&nbsp;</td>
</tr>
<!-- Product Sub Items Tax -->
<tr>
<td colspan="4">&nbsp;</td>
<td><?php echo _('Taxes'); ?></td>
<td class="right"><?php echo Currency::display($o->items_service_tax($ito->service_id));?>&nbsp;</td>
</tr>
<!-- Product End Sub Items Tax -->
<?php else : ?>
<?php endif ?>
<?php endforeach ?>
<?php endforeach ?>
</tbody>
</table>
</div>
<?php endif ?>
</div>
</div>
</div> <!-- /span -->
</div>
@ -162,7 +104,7 @@
<div class="span5">
<div class="row">
<div class="dl-horizontal">
<div class="dl-horizontal pull-right">
<!-- Sub Total -->
<dt>Sub Total</dt>
<dd><?php echo $o->subtotal(TRUE); ?></dd>
@ -191,11 +133,12 @@
<!-- END Invoice Taxes Total -->
<!-- Invoice Total -->
<dt>Total This Invoice:</dt>
<dt>Total Invoice:</dt>
<dd><?php echo $o->total(TRUE); ?></dd>
<!-- END Invoice Total -->
<!-- Account Total Due -->
<dt>Total Outstanding This Account:</dt>
<dt>Account Due:</dt>
<dd><?php echo $o->account->invoices_due_total(NULL,TRUE); ?></dd>
<!-- END Account Total Due -->
@ -205,4 +148,5 @@
</div> <!-- /span -->
</div>
<?php echo Form::button(URL::link('user','invoice/download/'.$o->id),'Download detailed invoice',array('class'=>'btn btn-primary pull-right')); ?></td>
<br>
<?php echo HTML::anchor(URL::link('user','invoice/download/'.$o->id),'Download Invoice',array('class'=>'btn pull-right')); ?></td>

View File

@ -0,0 +1,36 @@
<table class="table table-striped table-condensed table-hover" id="list-table">
<tbody>
<?php foreach ($o->items_periods() as $rs) : ?>
<tr><th colspan="5"><?php echo StaticList_RecurSchedule::get($rs); ?></th></tr>
<?php foreach ($o->items_periods($rs) as $iio) : ?>
<?php if ($iio->service_id) : ?>
<tr>
<th>&nbsp;</th>
<th colspan="2"><?php echo $iio->service->service_name(); ?></th>
<th>&nbsp;</th>
<th><div class="text-right"><?php echo $o->service_items_total($iio,TRUE); ?></div></th>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
<td><?php echo $iio->invoice_line(); ?></td>
<td><div class="text-right"><?php echo $iio->total(TRUE); ?></div></td>
<td>&nbsp;</td>
</tr>
<?php if ($x=$o->service_items_extra($iio)) : ?>
<?php foreach ($x as $eiio) : ?>
<tr>
<td colspan="2">&nbsp;</td>
<td><?php echo $eiio->invoice_line(); ?></td>
<td><div class="text-right"><?php echo $eiio->total(TRUE); ?></div></td>
<td>&nbsp;</td>
</tr>
<?php endforeach ?>
<?php endif ?>
<?php endif ?>
<?php endforeach ?>
<?php endforeach ?>
</tbody>
</table>

View File

@ -1,7 +1,9 @@
<div class="pull-right">
<?php
echo Form::open('cart/add');
echo Form::hidden('module_id',$mid);
echo Form::hidden('module_item',$o->id);
echo Form::hidden('module_id',$mid,array('nocg'=>TRUE));
echo Form::hidden('module_item',$o->id,array('nocg'=>TRUE));
echo Form::button('submit','Add to Cart',array('class'=>'btn btn-primary','nocg'=>TRUE));
echo Form::close('cart/add');
?>
Add to cart for payment: <?php echo StaticList_YesNo::form('cart_add',true); ?>
<?php echo Form::submit('submit','Add to Cart'); echo Form::close('cart/add'); ?>
</div>

View File

@ -185,6 +185,9 @@ $(document).ready(function() {
public function action_view() {
$po = ORM::factory('Product',$this->request->param('id'));
if (! $po->loaded())
throw HTTP_Exception::factory(403,'Product either doesnt exist, or you are not authorised to see it');
Block::factory()
->title(sprintf('%s: %s',_('Current Services Using this Product'),$po->title()))
->title_icon('icon-th-list')

View File

@ -34,22 +34,6 @@
<td>Taxable</td>
<td class="data"><?php echo StaticList_YesNo::form('taxable',$so->taxable); ?></td>
</tr>
<tr>
<td>Type</td>
<td class="data"><?php echo StaticList_RecurType::get($so->recur_type); ?></td>
</tr>
<tr>
<td>User Can Change Schedule</td>
<td class="data"><?php echo StaticList_YesNo::form('recur_schedule_change',$so->recur_schedule_change); ?></td>
</tr>
<tr>
<td>User Can Cancel</td>
<td class="data"><?php echo StaticList_YesNo::form('recur_cancel',$so->recur_cancel); ?></td>
</tr>
<tr>
<td>User Can Modify</td>
<td class="data"><?php echo StaticList_YesNo::form('recur_modify',$so->recur_modify); ?></td>
</tr>
<tr>
<td>Suspend Billing</td>
<td class="data"><?php echo StaticList_YesNo::form('suspend_billing',$so->suspend_billing); ?></td>

View File

@ -84,7 +84,12 @@
'due(TRUE)'=>'Due',
))
->prepend(array(
'id'=>array('url'=>URL::link('user','invoice/download/')),
'id'=>array('url'=>URL::link('user','invoice/view/')),
)); ?>
</fieldset>
<fieldset class="span5">
<legend>Next Invoice Charges</legend>
<?php echo Invoice::instance()->add_service($o)->render('html','body'); ?>
</fieldset>
</div> <!-- /row -->