OSB enhancements to date
This commit is contained in:
21
modules/invoice/classes/controller/admin/invoice.php
Normal file
21
modules/invoice/classes/controller/admin/invoice.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class provides invoice capabilities.
|
||||
*
|
||||
* @package OSB
|
||||
* @subpackage Invoice
|
||||
* @category Controllers/Admin
|
||||
* @author Deon George
|
||||
* @copyright (c) 2010 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class Controller_Admin_Invoice extends Controller_TemplateDefault {
|
||||
public function action_convert() {
|
||||
if (Config::sitemode() != KOHANA::DEVELOPMENT)
|
||||
throw new Kohana_Exception(__METHOD__.' can only be run in development');
|
||||
else
|
||||
throw new Kohana_Exception(__METHOD__.' can be run in development');
|
||||
}
|
||||
}
|
||||
?>
|
21
modules/invoice/classes/controller/invoice.php
Normal file
21
modules/invoice/classes/controller/invoice.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class provides invoice capabilities.
|
||||
*
|
||||
* @package OSB
|
||||
* @subpackage Invoice
|
||||
* @category Controllers
|
||||
* @author Deon George
|
||||
* @copyright (c) 2010 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class Controller_Invoice extends Controller_TemplateDefault {
|
||||
public function action_display() {
|
||||
// @todo - this should be a global config item
|
||||
$mediapath = Route::get('default/media');
|
||||
|
||||
$this->template->content = Kohana::debug(func_get_args());
|
||||
}
|
||||
}
|
||||
?>
|
55
modules/invoice/classes/controller/user/invoice.php
Normal file
55
modules/invoice/classes/controller/user/invoice.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class provides User Invoice functions
|
||||
*
|
||||
* @package OSB
|
||||
* @subpackage Invoice
|
||||
* @category Controllers/User
|
||||
* @author Deon George
|
||||
* @copyright (c) 2010 Deon George
|
||||
* @license http://dev.leenooks.net/license.html
|
||||
*/
|
||||
class Controller_User_Invoice extends Controller_TemplateDefault {
|
||||
public $secure_actions = array(
|
||||
'list'=>TRUE,
|
||||
'view'=>FALSE,
|
||||
);
|
||||
|
||||
/**
|
||||
* Show a product
|
||||
*/
|
||||
public function action_list() {
|
||||
$id = Auth::instance()->get_user()->id;
|
||||
$ao = ORM::factory('account',$id);
|
||||
|
||||
if (! $ao->loaded())
|
||||
throw new Kohana_Exception('Account doesnt exist :account ?',array(':account'=>$id));
|
||||
|
||||
Block::add(array(
|
||||
'title'=>sprintf('%s: %s - %s',_('Invoices For'),$ao->accnum(),$ao->name(TRUE)),
|
||||
'body'=>View::factory('invoice/list')
|
||||
->set('invoices',$ao->invoice->find_all()),
|
||||
));
|
||||
|
||||
$this->template->content = Block::factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* View an Invoice
|
||||
*/
|
||||
public function action_view($id) {
|
||||
$io = ORM::factory('invoice',$id);
|
||||
|
||||
if (! $io->loaded() OR ! Auth::instance()->authorised($io->account_id)) {
|
||||
$this->template->content = 'Unauthorised or doesnt exist?';
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// @todo media path probably should be a config item
|
||||
$this->template->content = View::factory('invoice/html')
|
||||
->set('mediapath',Route::get('default/media'))
|
||||
->set('invoice',$io);
|
||||
}
|
||||
}
|
||||
?>
|
92
modules/invoice/classes/invoice.php
Normal file
92
modules/invoice/classes/invoice.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
?>
|
267
modules/invoice/classes/model/invoice.php
Normal file
267
modules/invoice/classes/model/invoice.php
Normal file
@@ -0,0 +1,267 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class provides invoice capabilities.
|
||||
*
|
||||
* @package OSB
|
||||
* @subpackage Invoice
|
||||
* @category Models
|
||||
* @author Deon George
|
||||
* @copyright (c) 2010 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class Model_Invoice extends ORMOSB {
|
||||
private $invoice_items = array();
|
||||
|
||||
protected $_belongs_to = array(
|
||||
'account'=>array()
|
||||
);
|
||||
protected $_has_many = array(
|
||||
'invoice_item'=>array(),
|
||||
'invoice_item_tax'=>array(),
|
||||
'service'=>array('through'=>'invoice_item'),
|
||||
'payment'=>array('through'=>'payment_item'),
|
||||
);
|
||||
|
||||
/**
|
||||
* @var array Filters to render values properly
|
||||
*/
|
||||
protected $_filters = array(
|
||||
// @todo This rounding should be a global configuration
|
||||
'total_amt'=>array('round'=>array('2')),
|
||||
);
|
||||
|
||||
protected $_callbacks = array(
|
||||
'id'=>array('get_next_id'),
|
||||
'total_amt'=>array('calc_total'),
|
||||
'tax_amt'=>array('calc_tax'),
|
||||
);
|
||||
|
||||
protected $_formats = array(
|
||||
'date_orig'=>array('Config::date'=>array()),
|
||||
'due_date'=>array('Config::date'=>array()),
|
||||
'billed_amt'=>array('Currency::display'=>array()),
|
||||
'credit_amt'=>array('Currency::display'=>array()),
|
||||
'status'=>array('StaticList_YesNo::display'=>array()),
|
||||
'total_amt'=>array('Currency::display'=>array()),
|
||||
);
|
||||
|
||||
/**
|
||||
* Display the Invoice Number
|
||||
*/
|
||||
public function invnum() {
|
||||
return sprintf('%06s',$this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the Invoice Reference Number
|
||||
*/
|
||||
public function refnum() {
|
||||
return sprintf('%02s-%04s-%06s',Config::siteid(),$this->account_id,$this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the amount due
|
||||
*/
|
||||
public function due($format=FALSE) {
|
||||
$result = 0;
|
||||
// If the invoice is active calculate the due amount
|
||||
if ($this->status)
|
||||
$result = $this->total_amt-$this->credit_amt-$this->billed_amt;
|
||||
|
||||
if ($format)
|
||||
return Currency::display($result);
|
||||
else
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function subtotal($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->items() as $item)
|
||||
$result += $item->subtotal();
|
||||
|
||||
if ($format)
|
||||
return Currency::display($result);
|
||||
else
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function total($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->items() as $item)
|
||||
$result += $item->total();
|
||||
|
||||
// Reduce any credits
|
||||
$result -= $this->credit_amt;
|
||||
|
||||
if ($format)
|
||||
return Currency::display($result);
|
||||
else
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of invoice items for this invoice.
|
||||
*/
|
||||
public function items() {
|
||||
// Get our invoice items for an existing invoice
|
||||
if ($this->id AND $this->loaded() AND ! $this->_changed)
|
||||
return $this->invoice_item->order_by('service_id,item_type,module_id')->find_all();
|
||||
|
||||
echo kohana::debug(array('BEFORE'=>$this->invoice_item));
|
||||
if (! $this->invoice_items)
|
||||
$this->invoice_items = $this->invoice_item;
|
||||
|
||||
echo kohana::debug(array('AFTER'=>$this->invoice_items));die();
|
||||
return $this->invoice_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of our main invoice items (item_id=0)
|
||||
*/
|
||||
public function items_main() {
|
||||
if ($this->id AND $this->loaded() AND ! $this->_changed)
|
||||
return $this->invoice_item->where('item_type','=',0)->find_all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of our sub invoice items (item_id!=0)
|
||||
*/
|
||||
public function items_sub($sid) {
|
||||
if ($this->id AND $this->loaded() AND ! $this->_changed)
|
||||
return $this->invoice_item->where('service_id','=',$sid)->and_where('item_type','<>',0)->find_all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the total for items for a service
|
||||
*/
|
||||
public function items_service_total($sid) {
|
||||
$total = 0;
|
||||
|
||||
foreach ($this->invoice_item->where('service_id','=',$sid)->find_all() as $item)
|
||||
$total += $item->total();
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the tax of items for a service
|
||||
*/
|
||||
public function items_service_tax($sid) {
|
||||
$total = 0;
|
||||
|
||||
foreach ($this->invoice_item->where('service_id','=',$sid)->find_all() as $item)
|
||||
$total += $item->tax_amt;
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of items based on a sort criteria
|
||||
*/
|
||||
public function sorted_service_items($index) {
|
||||
$summary = array();
|
||||
|
||||
foreach ($this->items() as $item) {
|
||||
$key = $item->service->$index;
|
||||
|
||||
if (! isset($summary[$key]['items'])) {
|
||||
$summary[$key]['items'] = array();
|
||||
$summary[$key]['total'] = 0;
|
||||
}
|
||||
|
||||
// Only record items with item_type=0
|
||||
if ($item->item_type == 0)
|
||||
array_push($summary[$key]['items'],$item);
|
||||
|
||||
$summary[$key]['total'] += $item->total();
|
||||
}
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of taxes used on this invoice
|
||||
*/
|
||||
public function tax_summary() {
|
||||
$summary = array();
|
||||
|
||||
foreach ($this->items() as $item) {
|
||||
foreach ($item->invoice_item_tax->find_all() as $item_tax) {
|
||||
if (! isset($summary[$item_tax->tax_id]))
|
||||
$summary[$item_tax->tax_id] = $item_tax->amount;
|
||||
else
|
||||
$summary[$item_tax->tax_id] += $item_tax->amount;
|
||||
}
|
||||
}
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
public function add_item() {
|
||||
$c = count($this->invoice_items);
|
||||
|
||||
$this->invoice_items[$c] = ORM::factory('invoice_item');
|
||||
|
||||
return $this->invoice_items[$c];
|
||||
}
|
||||
|
||||
public function save() {
|
||||
// Save the invoice
|
||||
parent::save();
|
||||
|
||||
// Need to save the associated items and their taxes
|
||||
if ($this->saved()) {
|
||||
$tax_amt = 0;
|
||||
$discount_amt = 0;
|
||||
|
||||
foreach ($this->items() as $invoice_item) {
|
||||
$invoice_item->invoice_id = $this->id;
|
||||
|
||||
if (! $invoice_item->check()) {
|
||||
// @todo Mark invoice as cancelled and write a memo, then...
|
||||
throw new Kohana_Exception('Problem saving invoice_item for invoice :invoice - Failed check()',array(':invoice'=>$invoice->id));
|
||||
}
|
||||
|
||||
$invoice_item->save();
|
||||
|
||||
if (! $invoice_item->saved()) {
|
||||
// @todo Mark invoice as cancelled and write a memo, then...
|
||||
throw new Kohana_Exception('Problem saving invoice_item for invoice :invoice - Failed save()',array(':invoice'=>$invoice->id));
|
||||
}
|
||||
|
||||
// @todo Need to save tax information
|
||||
// @todo Need to save discount information
|
||||
}
|
||||
|
||||
} else
|
||||
throw new Kohana_Exception('Couldnt save invoice for some reason?');
|
||||
|
||||
echo Kohana::debug(array('saved'=>$this));
|
||||
}
|
||||
|
||||
public function calc_total(Validate $array, $field) {
|
||||
$array[$field] = 0;
|
||||
|
||||
// @todo Rounding here should come from a global config
|
||||
foreach ($this->invoice_items as $item)
|
||||
$array[$field] += round($item->price_base+$item->price_setup,2);
|
||||
|
||||
$this->_changed[$field] = $field;
|
||||
}
|
||||
|
||||
public function calc_tax(Validate $array, $field) {
|
||||
$array[$field] = 0;
|
||||
|
||||
// @todo Rounding here should come from a global config
|
||||
// @todo tax should be evaluated per item
|
||||
// @todo tax parameters should come from user session
|
||||
foreach ($this->invoice_items as $item)
|
||||
$array[$field] += round(Tax::total(61,NULL,$item->price_base+$item->price_setup),2);
|
||||
|
||||
$this->_changed[$field] = $field;
|
||||
}
|
||||
}
|
||||
?>
|
93
modules/invoice/classes/model/invoice/item.php
Normal file
93
modules/invoice/classes/model/invoice/item.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class provides invoice item capabilities.
|
||||
*
|
||||
* @package OSB
|
||||
* @subpackage Invoice
|
||||
* @category Models
|
||||
* @author Deon George
|
||||
* @copyright (c) 2010 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class Model_Invoice_Item extends ORMOSB {
|
||||
protected $_belongs_to = array(
|
||||
'product'=>array(),
|
||||
'invoice'=>array(),
|
||||
'service'=>array()
|
||||
);
|
||||
protected $_has_many = array(
|
||||
'invoice_item_tax'=>array()
|
||||
);
|
||||
protected $_updated_column = FALSE; // @todo No update columns
|
||||
|
||||
// Display a transaction number
|
||||
public function trannum() {
|
||||
return sprintf('%03s-%06s',$this->item_type,$this->id);
|
||||
}
|
||||
|
||||
// Display the period that a transaction applies
|
||||
public function period() {
|
||||
if ($this->date_start == $this->date_stop)
|
||||
return sprintf('%s: %s',_('Date'),Config::date($this->date_start));
|
||||
|
||||
else
|
||||
return sprintf('%s: %s -> %s',_('Period'),Config::date($this->date_start),Config::date($this->date_stop));
|
||||
}
|
||||
|
||||
/**
|
||||
* On invoices where there are multiple charges for the same item
|
||||
* (eg spanning the next invoice period), this should show the period
|
||||
* range for the additional sub-items.
|
||||
*/
|
||||
public function invoice_display() {
|
||||
return $this->period();
|
||||
}
|
||||
|
||||
public function subtotal() {
|
||||
return ($this->price_base+$this->price_setup)*$this->quantity;
|
||||
}
|
||||
|
||||
public function total() {
|
||||
return ($this->price_base+$this->price_setup)*$this->quantity+$this->tax_amt-$this->discount_amt;
|
||||
}
|
||||
|
||||
public function save() {
|
||||
// Save the invoice item
|
||||
parent::save();
|
||||
|
||||
// Need to save the taxes and discounts associated with the invoice_item
|
||||
if ($this->saved()) {
|
||||
$invoice_item_tax = ORM::factory('invoice_item_tax');
|
||||
$tax_total = 0;
|
||||
|
||||
// Save TAX details
|
||||
// @todo tax parameters should come from user session
|
||||
foreach (Tax::detail(61,NULL,$this->subtotal()) as $tax) {
|
||||
$invoice_item_tax->clear();
|
||||
$invoice_item_tax->invoice_item_id = $this->id;
|
||||
// @todo Rounding here should come from a global config
|
||||
$tax_total += ($invoice_item_tax->amount = round($tax['amount'],2));
|
||||
$invoice_item_tax->tax_id = $tax['id'];
|
||||
|
||||
if (! $invoice_item_tax->check())
|
||||
throw new Kohana_Exception('Couldnt save tax for some reason - failed check()?');
|
||||
|
||||
$invoice_item_tax->save();
|
||||
|
||||
if (! $invoice_item_tax->saved())
|
||||
throw new Kohana_Exception('Couldnt save tax for some reason - failed save()?');
|
||||
}
|
||||
|
||||
// Save DISCOUNT details
|
||||
// @todo calculate discounts
|
||||
|
||||
$this->tax_amt = $tax_total;
|
||||
$this->total_amt = $this->total();
|
||||
parent::save();
|
||||
|
||||
} else
|
||||
throw new Kohana_Exception('Couldnt save invoice_item for some reason?');
|
||||
}
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user