Improvements to invoice display and other misc items
This commit is contained in:
@@ -40,10 +40,13 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
|
||||
// Items belonging to an invoice
|
||||
protected $_sub_items_load = array(
|
||||
'invoice_item'=>'service_id,item_type',
|
||||
'invoice_item'=>'service_id,item_type,date_start,date_stop',
|
||||
);
|
||||
|
||||
/** INTERFACE REQUIREMENTS **/
|
||||
private $_render = array();
|
||||
|
||||
// Our required Interface Methods
|
||||
|
||||
public function cart_item() {
|
||||
return new Cart_Item(1,sprintf('Invoice: %s',$this->refnum()),$this->due());
|
||||
}
|
||||
@@ -55,26 +58,83 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
return count(Cart::instance()->get($this->mid(),$this->id));
|
||||
}
|
||||
|
||||
// Our local methods
|
||||
|
||||
/**
|
||||
* Return a list of valid checkout options for this invoice
|
||||
*/
|
||||
public function checkout() {
|
||||
$due = $this->due();
|
||||
|
||||
return ORM::factory('Checkout')
|
||||
->where_active()
|
||||
->where('amount_min','<=',$due)
|
||||
->where_open()
|
||||
->and_where('amount_max','>=',$due)
|
||||
->or_where('amount_max','is',null)
|
||||
->where_close()->find_all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the amount due
|
||||
*/
|
||||
public function due($format=FALSE) {
|
||||
// If the invoice is active calculate the due amount
|
||||
$result = $this->status ? Currency::round($this->total()-$this->payments_total(),1) : 0;
|
||||
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
public function email() {
|
||||
return ORM::factory('Email_Log')
|
||||
->where('module_id','=',$this->mid())
|
||||
->where('module_data','=',$this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the Invoice Number
|
||||
*/
|
||||
public function id() {
|
||||
return sprintf('%06s',$this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the recurring schedules that are on an invoice
|
||||
*
|
||||
* @param $period Return an Array of items for that period
|
||||
*/
|
||||
public function items_periods($period=FALSE) {
|
||||
if (is_null($period))
|
||||
return isset($this->_render['OTHER']) ? $this->_render['OTHER'] : array();
|
||||
|
||||
elseif ($period)
|
||||
return isset($this->_render['SCHED'][$period]) ? $this->_render['SCHED'][$period] : array();
|
||||
|
||||
else
|
||||
return array_keys($this->_render['SCHED']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a summary of all itemss on an invoice
|
||||
*/
|
||||
public function items_summary() {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->subitems() as $ito) {
|
||||
// We are only interested in item_type=0
|
||||
if ($ito->item_type != 0)
|
||||
foreach ($this->subitems() as $iio) {
|
||||
// We only summarise item_type=0
|
||||
if (! $iio->item_type == 0)
|
||||
continue;
|
||||
|
||||
if ($period === FALSE AND ! in_array($ito->recurring_schedule,$result))
|
||||
array_push($result,$ito->recurring_schedule);
|
||||
if ($iio->module() instanceof Model_Product) {
|
||||
$p = $iio->module()->title();
|
||||
|
||||
elseif ($ito->recurring_schedule == $period) {
|
||||
// If we have this service already, we'll skip
|
||||
if (! Object::in_array('service_id',$ito->service_id,$result))
|
||||
array_push($result,$ito);
|
||||
if (! isset($result[$p])) {
|
||||
$result[$p]['quantity'] = 0;
|
||||
$result[$p]['subtotal'] = 0;
|
||||
}
|
||||
|
||||
$result[$p]['quantity']++;
|
||||
$result[$p]['subtotal'] += $iio->subtotal();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,6 +164,67 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function payments() {
|
||||
return $this->payment_item->find_all();
|
||||
}
|
||||
|
||||
public function payments_total($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->payments() as $po)
|
||||
$result += $po->alloc_amt;
|
||||
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function takes care of all the invoice items so that they are rendered only once
|
||||
*
|
||||
* We organise items by
|
||||
* + recurring_schedule for service items item_type=0
|
||||
* + recurring_schedule for service items for item_type!=0 (that have an item_type=0 previously selected)
|
||||
* + Other Items
|
||||
*/
|
||||
public function pre_render() {
|
||||
$this->_render = array();
|
||||
$this->_render['SCHED'] = array();
|
||||
$processed = array();
|
||||
|
||||
foreach ($this->subitems() as $iio) {
|
||||
if (in_array($iio->id,$processed))
|
||||
continue;
|
||||
|
||||
if ($iio->recurring_schedule) {
|
||||
$this->_render['SCHED'][$iio->recurring_schedule][] = $iio;
|
||||
|
||||
} elseif (Object::in_array('service_id',$iio->service_id,$this->_render['SCHED'],TRUE)) {
|
||||
// Do nothing
|
||||
|
||||
} else {
|
||||
$this->_render['OTHER'][] = $iio;
|
||||
}
|
||||
|
||||
array_push($processed,$iio->id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the Invoice Reference Number
|
||||
*/
|
||||
public function refnum() {
|
||||
return sprintf('%s-%06s',$this->account->accnum(),$this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the reminder value
|
||||
*/
|
||||
public function remind($key) {
|
||||
if (isset($this->reminders[$key]))
|
||||
return (is_array($this->reminders[$key])) ? end($this->reminders[$key]) : $this->reminders[$key];
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the array of Email Template Objects
|
||||
*/
|
||||
@@ -128,361 +249,6 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the service items on an invoice relating to an invoice_item
|
||||
* IE: item_type == 0
|
||||
*/
|
||||
public function service_items(Model_Invoice_Item $o) {
|
||||
$result = array();
|
||||
|
||||
// At the moment, we only return items pertaining to a service
|
||||
if (! $o->service_id)
|
||||
return $result;
|
||||
|
||||
foreach ($this->subitems() as $iio)
|
||||
if ($iio->item_type == 0 AND $iio->service_id == $o->service_id)
|
||||
array_push($result,$iio);
|
||||
|
||||
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->subitems() 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->subitems() 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->subitems() as $iio)
|
||||
if ($iio->service_id == $o->service_id)
|
||||
$result += $iio->total();
|
||||
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an item to an invoice
|
||||
*/
|
||||
public function subitem_add(Model_Invoice_Item $iio,Model_Country $co,$taxed=FALSE) {
|
||||
$iio->subitem_add($co,$taxed);
|
||||
|
||||
array_push($this->_sub_items,$iio);
|
||||
|
||||
$this->_sub_items_sorted = FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of invoice items for this invoice.
|
||||
* @param type [CHARGE|CREDIT|ALL]
|
||||
* @see invoice_items
|
||||
*/
|
||||
public function subitems($type='ALL') {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->_sub_items as $ito) {
|
||||
// Ignore voided items
|
||||
if ($ito->void)
|
||||
continue;
|
||||
|
||||
$return = FALSE;
|
||||
|
||||
switch ($type) {
|
||||
case 'CHARGE':
|
||||
if ($ito->product_id OR $ito->quantity > 0)
|
||||
$return = TRUE;
|
||||
break;
|
||||
|
||||
case 'CREDIT':
|
||||
if (! $ito->product_id AND $ito->quantity < 0)
|
||||
$return = TRUE;
|
||||
break;
|
||||
|
||||
case 'ALL':
|
||||
default:
|
||||
$return = TRUE;
|
||||
break;
|
||||
}
|
||||
|
||||
if ($return)
|
||||
array_push($result,$ito);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of valid checkout options for this invoice
|
||||
*/
|
||||
public function checkout() {
|
||||
$due = $this->due();
|
||||
|
||||
return ORM::factory('Checkout')
|
||||
->where_active()
|
||||
->where('amount_min','<=',$due)
|
||||
->where_open()
|
||||
->and_where('amount_max','>=',$due)
|
||||
->or_where('amount_max','is',null)
|
||||
->where_close()->find_all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the amount due
|
||||
*/
|
||||
public function due($format=FALSE) {
|
||||
// If the invoice is active calculate the due amount
|
||||
$result = $this->status ? $this->total()-$this->payments_total() : 0;
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the Invoice Number
|
||||
*/
|
||||
public function id() {
|
||||
return sprintf('%06s',$this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide a sorted list of items by an index
|
||||
*/
|
||||
public function items_index($index) {
|
||||
static $result = array();
|
||||
|
||||
// We'll return a cached result for quicker processing
|
||||
if (! $this->_changed AND array_key_exists($index,$result))
|
||||
return $result[$index];
|
||||
|
||||
foreach ($this->subitems() as $ito) {
|
||||
switch ($index) {
|
||||
case 'account':
|
||||
if (! $ito->service_id)
|
||||
$result[$index][$ito->id] = $ito;
|
||||
|
||||
break;
|
||||
|
||||
case 'period':
|
||||
// We only show the services in this period
|
||||
if (! is_null($ito->recurring_schedule) AND (empty($result[$index][$ito->recurring_schedule]) OR ! in_array($ito->service_id,$result[$index][$ito->recurring_schedule])))
|
||||
$result[$index][$ito->recurring_schedule][] = $ito->service_id;
|
||||
|
||||
break;
|
||||
|
||||
case 'service':
|
||||
default:
|
||||
if ($ito->service_id)
|
||||
$result[$index][$ito->service_id][] = $ito;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return array_key_exists($index,$result) ? $result[$index] : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of invoice_items for a service_id on an invoice
|
||||
*
|
||||
* We use this to list details by service on an invoice.
|
||||
*/
|
||||
// @todo to retire
|
||||
public function items_services(array $items=array()) {
|
||||
$result = array();
|
||||
if (! $items)
|
||||
$items = $this->subitems();
|
||||
|
||||
foreach ($items as $ito)
|
||||
if ($ito->service_id AND empty($result[$ito->service_id]))
|
||||
$result[$ito->service_id] = $ito;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
// @todo to retire
|
||||
public function items_invoice() {
|
||||
$result = array();
|
||||
$items = $this->subitems();
|
||||
|
||||
foreach ($items as $ito)
|
||||
if (! $ito->service_id AND empty($result[$ito->id]))
|
||||
$result[$ito->id] = $ito;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all invoice items for a specific service
|
||||
*/
|
||||
public function items_service($service_id) {
|
||||
$svs = $this->items_index('service');
|
||||
|
||||
if (array_key_exists($service_id,$svs)) {
|
||||
Sort::MAsort($svs[$service_id],'item_type');
|
||||
|
||||
return $svs[$service_id];
|
||||
} else
|
||||
return array();
|
||||
}
|
||||
|
||||
// @todo to retire
|
||||
/**
|
||||
* Return a list of periods and services
|
||||
*
|
||||
* This is so that we can list items summarised by billing period
|
||||
*/
|
||||
public function items_service_periods() {
|
||||
$result = array();
|
||||
|
||||
$c = array();
|
||||
foreach ($this->subitems() as $ito)
|
||||
if ($ito->service_id) {
|
||||
// If we have already covered a service with no recurring_schedule
|
||||
if (! $ito->recurring_schedule AND in_array($ito->service_id,$c))
|
||||
continue;
|
||||
|
||||
array_push($c,$ito->service_id);
|
||||
|
||||
$result[$ito->recurring_schedule][] = $ito;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarise the items on an invoice
|
||||
*
|
||||
* We summaries based on product.
|
||||
*/
|
||||
// @todo
|
||||
public function items_summary() {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->subitems() as $ito) {
|
||||
// We only summarise item_type=0
|
||||
if (! $ito->item_type == 0)
|
||||
continue;
|
||||
|
||||
$t = $ito->product->title();
|
||||
|
||||
if (! isset($result[$t])) {
|
||||
$result[$t]['quantity'] = 0;
|
||||
$result[$t]['subtotal'] = 0;
|
||||
}
|
||||
|
||||
$result[$t]['quantity'] += $ito->quantity;
|
||||
$result[$t]['subtotal'] += $ito->subtotal();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the total for items for a service
|
||||
*/
|
||||
public function items_service_total($sid) {
|
||||
$total = 0;
|
||||
|
||||
foreach ($this->items_service($sid) as $ito)
|
||||
$total += $ito->total();
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the tax of items for a service
|
||||
*/
|
||||
public function items_service_tax($sid) {
|
||||
$total = 0;
|
||||
|
||||
foreach ($this->items_service($sid) as $ito)
|
||||
$total += $ito->tax();
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the discounts of items for a service
|
||||
*/
|
||||
public function items_service_discount($sid) {
|
||||
$total = 0;
|
||||
|
||||
foreach ($this->items_service($sid) as $ito)
|
||||
$total += $ito->discount();
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
public function min_due($date) {
|
||||
return strtotime(date('Y-M-d',($date < time()) ? time()+ORM::factory('Invoice')->config('DUE_DAYS_MIN')*86400 : $date));
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the Invoice Reference Number
|
||||
*/
|
||||
public function refnum() {
|
||||
return sprintf('%s-%06s',$this->account->accnum(),$this->id);
|
||||
}
|
||||
|
||||
public function payments() {
|
||||
return $this->payment_item->find_all();
|
||||
}
|
||||
|
||||
public function payments_total($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->payments() as $po)
|
||||
$result += $po->alloc_amt;
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the reminder value
|
||||
*/
|
||||
public function remind($key) {
|
||||
if (isset($this->reminders[$key]))
|
||||
return (is_array($this->reminders[$key])) ? end($this->reminders[$key]) : $this->reminders[$key];
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
public function save(Validation $validation = NULL) {
|
||||
// Our items will be clobbered once we save the object, so we need to save it here.
|
||||
$subitems = $this->subitems();
|
||||
@@ -522,12 +288,12 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
}
|
||||
|
||||
// Update charge iteme
|
||||
if ($iio->charge_id) {
|
||||
$iio->charge->processed = 1;
|
||||
if (($x=$iio->module()) AND ($x instanceof Model_Charge)) {
|
||||
$x->processed = 1;
|
||||
|
||||
// @todo Mark invoice as cancelled and write a memo, then...
|
||||
if (! $iio->charge->save())
|
||||
throw new Kohana_Exception('Problem saving charge :charge for invoice_item :invoice_item - Failed save()',array(':invoice_item'=>$iio->id,':charge'=>$iio->charge_id));
|
||||
if (! $x->save())
|
||||
throw new Kohana_Exception('Problem saving charge :charge for invoice_item :invoice_item - Failed save()',array(':invoice_item'=>$iio->id,':charge'=>$x->id));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -541,6 +307,72 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the service items on an invoice relating to an invoice_item
|
||||
* IE: item_type == 0
|
||||
*/
|
||||
public function service_items(Model_Invoice_Item $o) {
|
||||
$result = array();
|
||||
|
||||
// At the moment, we only return items pertaining to a service
|
||||
if (! $o->service_id)
|
||||
return $result;
|
||||
|
||||
foreach ($this->subitems() as $iio)
|
||||
if ($iio->item_type == 0 AND $iio->service_id == $o->service_id)
|
||||
array_push($result,$iio);
|
||||
|
||||
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();
|
||||
|
||||
foreach ($this->subitems() as $iio)
|
||||
if ($iio->item_type != 0 AND (! $o->service_id OR ($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->subitems() 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->subitems() as $iio)
|
||||
if ($iio->service_id == $o->service_id)
|
||||
$result += $iio->total();
|
||||
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
public function set_remind($key,$value,$add=FALSE) {
|
||||
$x = $this->reminders;
|
||||
|
||||
@@ -563,6 +395,56 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
return $this->saved();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an item to an invoice
|
||||
*/
|
||||
public function subitem_add(Model_Invoice_Item $iio,Model_Country $co,$taxed=FALSE) {
|
||||
$iio->subitem_add($co,$taxed);
|
||||
|
||||
array_push($this->_sub_items,$iio);
|
||||
|
||||
$this->_sub_items_sorted = FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of invoice items for this invoice.
|
||||
* @param type [CHARGE|CREDIT|ALL]
|
||||
* @see invoice_items
|
||||
*/
|
||||
public function subitems($type='ALL') {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->_sub_items as $iio) {
|
||||
// Ignore voided items
|
||||
if ($iio->void)
|
||||
continue;
|
||||
|
||||
$return = FALSE;
|
||||
|
||||
switch ($type) {
|
||||
case 'CHARGE':
|
||||
if ($iio->quantity > 0)
|
||||
$return = TRUE;
|
||||
break;
|
||||
|
||||
case 'CREDIT':
|
||||
if ($iio->quantity < 0)
|
||||
$return = TRUE;
|
||||
break;
|
||||
|
||||
case 'ALL':
|
||||
default:
|
||||
$return = TRUE;
|
||||
break;
|
||||
}
|
||||
|
||||
if ($return)
|
||||
array_push($result,$iio);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the subtotal of all items
|
||||
*/
|
||||
@@ -572,7 +454,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
foreach ($this->subitems() as $ito)
|
||||
$result += $ito->subtotal();
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
public function tax($format=FALSE) {
|
||||
@@ -581,7 +463,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
foreach ($this->subitems() as $ito)
|
||||
$result += $ito->tax();
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -591,12 +473,12 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
public function tax_summary() {
|
||||
$summary = array();
|
||||
|
||||
foreach ($this->subitems() as $ito) {
|
||||
foreach ($ito->invoice_item_tax->find_all() as $item_tax) {
|
||||
if (! isset($summary[$item_tax->tax_id]))
|
||||
$summary[$item_tax->tax_id] = $item_tax->amount;
|
||||
foreach ($this->subitems() as $iio) {
|
||||
foreach ($iio->tax->find_all() as $iito) {
|
||||
if (! isset($summary[$iito->tax_id]))
|
||||
$summary[$iito->tax_id] = $iito->amount;
|
||||
else
|
||||
$summary[$item_tax->tax_id] += $item_tax->amount;
|
||||
$summary[$iito->tax_id] += $iito->amount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -617,7 +499,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
foreach ($this->subitems() as $ito)
|
||||
$result += $ito->total();
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
public function total_charges($format=FALSE) {
|
||||
@@ -626,7 +508,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
foreach ($this->subitems('CHARGE') as $ito)
|
||||
$result += $ito->subtotal()+$ito->tax();
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
public function total_credits($format=FALSE) {
|
||||
@@ -635,7 +517,7 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
foreach ($this->subitems('CREDIT') as $ito)
|
||||
$result += ($ito->subtotal()+$ito->tax())*-1;
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
public function total_discounts($format=FALSE) {
|
||||
@@ -644,10 +526,18 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
foreach ($this->subitems() as $ito)
|
||||
$result += $ito->discount();
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
/** LIST FUNCTIONS **/
|
||||
private function _where_unprocessed() {
|
||||
return $this->where_open()->where('process_status','!=',1)->or_where('process_status','is',NULL)->where_close();
|
||||
}
|
||||
|
||||
public function where_unprocessed() {
|
||||
return $this->_where_unprocessed();
|
||||
}
|
||||
|
||||
// Our list function
|
||||
|
||||
/**
|
||||
* Search for invoices matching a term
|
||||
@@ -687,12 +577,26 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function _where_unprocessed() {
|
||||
return $this->where_open()->where('process_status','!=',1)->or_where('process_status','is',NULL)->where_close();
|
||||
/**
|
||||
* Return a list of invoices that are due, excluding overdue.
|
||||
*/
|
||||
public function list_due($time=NULL,$authorised=TRUE) {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->_list_due($authorised) as $io)
|
||||
if (is_null($time) OR $io->due_date > $time)
|
||||
array_push($result,$io);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function where_unprocessed() {
|
||||
return $this->_where_unprocessed();
|
||||
public function list_due_total($format=FALSE,$time=NULL,$authorised=TRUE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->list_due($time,$authorised) as $io)
|
||||
$result += $io->due();
|
||||
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -730,28 +634,6 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of invoices that are due, excluding overdue.
|
||||
*/
|
||||
public function list_due($time=NULL,$authorised=TRUE) {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->_list_due($authorised) as $io)
|
||||
if (is_null($time) OR $io->due_date > $time)
|
||||
array_push($result,$io);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function list_due_total($format=FALSE,$time=NULL,$authorised=TRUE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->list_due($time,$authorised) as $io)
|
||||
$result += $io->due();
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of invoices that need to be sent.
|
||||
* @todo This should be optimised a little to return only invoices to send, instead of looking for them.
|
||||
@@ -759,23 +641,5 @@ class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
public function list_tosend() {
|
||||
return ORM::factory('Invoice')->where_active()->where_open()->where('print_status','is',NULL)->or_where('print_status','!=',1)->where_close();
|
||||
}
|
||||
|
||||
public function html() {
|
||||
// @todo This should be in a config file.
|
||||
$css = '<style type="text/css">';
|
||||
$css .= 'table.box-left { border: 1px solid #AAAACC; margin-right: auto; }';
|
||||
$css .= 'tr.head { font-weight: bold; }';
|
||||
$css .= 'td.head { font-weight: bold; }';
|
||||
$css .= 'td.right { text-align: right; }';
|
||||
$css .= 'tr.odd { background-color: #FCFCFE; }';
|
||||
$css .= 'tr.even { background-color: #F6F6F8; }';
|
||||
$css .= '</style>';
|
||||
|
||||
$output = View::factory('invoice/user/email')
|
||||
->set('mediapath',Route::get('default/media'))
|
||||
->set('io',$this);
|
||||
|
||||
return $css.$output;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@@ -8,22 +8,15 @@
|
||||
* @author Deon George
|
||||
* @copyright (c) 2009-2013 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*
|
||||
* Column Definitions:
|
||||
* + item_type: 0=MAIN Service Item,2=?,3=?,4=Connection/Setup,5=Excess Service Item,6=Change Service,126=Payment Fee,127=Late Fee
|
||||
*/
|
||||
class Model_Invoice_Item extends ORM_OSB {
|
||||
// Relationships
|
||||
protected $_belongs_to = array(
|
||||
'product'=>array(),
|
||||
'invoice'=>array(),
|
||||
'service'=>array()
|
||||
);
|
||||
protected $_has_one = array(
|
||||
'charge'=>array('far_key'=>'charge_id','foreign_key'=>'id')
|
||||
);
|
||||
protected $_has_many = array(
|
||||
'invoice_item_tax'=>array('far_key'=>'id')
|
||||
'tax'=>array('model'=>'Invoice_Item_Tax','far_key'=>'id')
|
||||
);
|
||||
|
||||
protected $_display_filters = array(
|
||||
@@ -40,7 +33,7 @@ class Model_Invoice_Item extends ORM_OSB {
|
||||
|
||||
// Items belonging to an invoice item
|
||||
protected $_sub_items_load = array(
|
||||
'invoice_item_tax'=>FALSE,
|
||||
'tax'=>FALSE,
|
||||
);
|
||||
|
||||
// The total of all discounts
|
||||
@@ -48,6 +41,63 @@ class Model_Invoice_Item extends ORM_OSB {
|
||||
return Currency::round($this->discount_amt);
|
||||
}
|
||||
|
||||
/**
|
||||
* The line that will be printed on an invoice
|
||||
*
|
||||
* @todo This method includes some database format validation routines, which can be removed when the database
|
||||
* is completly transformed.
|
||||
*/
|
||||
public function invoice_line() {
|
||||
$ii = NULL;
|
||||
|
||||
// Our module is responsible for rending the invoice line
|
||||
$ii = ($this->module_id AND method_exists($this->module(),'invoice_item')) ? $this->module()->invoice_item($this->item_type) : StaticList_ItemType::get($this->item_type);
|
||||
|
||||
switch ($this->item_type) {
|
||||
// Service Charges
|
||||
case 0:
|
||||
case 7:
|
||||
return ((! $this->service_id OR $this->product_id OR $this->charge_id OR $this->product_name OR ! $this->recurring_schedule OR ! $this->date_start OR ! $this->date_stop) ? '+ ' : '').$ii.' '.$this->period();
|
||||
|
||||
case 1:
|
||||
// @todo
|
||||
return $ii;
|
||||
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
case 5:
|
||||
case 6:
|
||||
return ((! $this->service_id OR $this->product_id OR $this->charge_id OR $this->product_name OR $this->recurring_schedule OR ! $this->date_start OR ! $this->date_stop) ? '+ ' : '').$ii;
|
||||
|
||||
case 124:
|
||||
case 125:
|
||||
case 126:
|
||||
case 127:
|
||||
// @todo
|
||||
return $ii;
|
||||
|
||||
// @todo DB records to fix.
|
||||
default:
|
||||
if ($this->charge_id)
|
||||
return '*'.($ii ? $ii : $this->charge->description).' '.$this->period();
|
||||
else
|
||||
throw HTTP_Exception::factory(501,'Unable to render invoice item :id',array(':id'=>$this->id));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the Model of this charge
|
||||
*/
|
||||
public function module() {
|
||||
$x = ORM::factory('Module',$this->module_id);
|
||||
|
||||
if (! $x->loaded())
|
||||
throw new Kohana_Exception('Module :module doesnt exist?',array(':module'=>$this->module_id));
|
||||
|
||||
return ORM::factory('Module',$this->module_id)->instance($this->module_ref);
|
||||
}
|
||||
|
||||
// Display the period that a transaction applies
|
||||
public function period() {
|
||||
return ($this->date_start == $this->date_stop) ? Config::date($this->date_start) : sprintf('%s -> %s',Config::date($this->date_start),Config::date($this->date_stop));
|
||||
@@ -71,13 +121,13 @@ class Model_Invoice_Item extends ORM_OSB {
|
||||
$iito->save();
|
||||
|
||||
if (! $iito->saved()) {
|
||||
$this->void = 1;
|
||||
$this->save();
|
||||
|
||||
break;
|
||||
}
|
||||
$this->void = 1;
|
||||
$this->save();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} else
|
||||
throw new Kohana_Exception('Couldnt save invoice_item for some reason?');
|
||||
|
||||
@@ -119,7 +169,7 @@ class Model_Invoice_Item extends ORM_OSB {
|
||||
public function subtotal($format=FALSE) {
|
||||
$result = $this->price_base*$this->quantity;
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
return $format ? Currency::display($result) : $result;
|
||||
}
|
||||
|
||||
// Sum up the tax that applies to this invoice item
|
||||
@@ -149,8 +199,12 @@ class Model_Invoice_Item extends ORM_OSB {
|
||||
* The title for invoice items
|
||||
*/
|
||||
public function title() {
|
||||
if ($this->product_id AND $this->service_id)
|
||||
return $this->product_id ? sprintf('%s: %s',$this->product->title(),$this->service->name()) : $this->service->name;
|
||||
if ($this->service_id AND $this->module_id AND method_exists($this->module(),'invoice_title'))
|
||||
return $this->module_ref ? sprintf('%s: %s',$this->module()->invoice_title(),$this->service->name()) : $this->service->name();
|
||||
elseif ($x=$this->module() AND ($x instanceof Model_Charge) AND $x->product_id)
|
||||
return $x->product->title().' '.$this->service->name();
|
||||
elseif ($this->product_id)
|
||||
return sprintf('* %s: %s',$this->product->invoice_title(),$this->service->name());
|
||||
else
|
||||
return 'Unknown Item';
|
||||
}
|
||||
@@ -166,67 +220,6 @@ class Model_Invoice_Item extends ORM_OSB {
|
||||
return sprintf('%03s-%06s',$this->item_type,$this->id);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
* Name for an invoice item
|
||||
* @deprecated, use StaticList_ItemType()
|
||||
*/
|
||||
public function name() {
|
||||
switch ($this->item_type) {
|
||||
case 0: return _('Service');
|
||||
|
||||
case 1: return _('Item');
|
||||
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
case 6:
|
||||
case 126:
|
||||
case 127: return _('Charge');
|
||||
|
||||
case 5: return $this->charge->description;
|
||||
|
||||
default: return _('Other');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Detail behind an invoice item
|
||||
*/
|
||||
public function detail() {
|
||||
switch ($this->item_type) {
|
||||
case 0: return '';
|
||||
|
||||
case 1: return _('Hardware');
|
||||
|
||||
case 2: return _('Service Relocation Fee');
|
||||
|
||||
case 3: return _('Service Change Fee');
|
||||
|
||||
case 4: return _('Service Connection Fee');
|
||||
|
||||
case 5: return sprintf('%s@%3.2f',$this->quantity,$this->price_base);
|
||||
|
||||
case 6: return _('Service Excess Fee');
|
||||
|
||||
case 125: return _('Payment Fee');
|
||||
|
||||
case 126: return _('Rounding');
|
||||
|
||||
case 127: return _('Late Payment Fee');
|
||||
|
||||
default: '';
|
||||
}
|
||||
}
|
||||
|
||||
public function invoice_detail_items() {
|
||||
switch ($this->item_type) {
|
||||
case 0:
|
||||
|
Reference in New Issue
Block a user