2010-11-29 22:41:08 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides invoice capabilities.
|
|
|
|
*
|
2013-03-19 22:35:19 +00:00
|
|
|
* @package Invoice
|
2010-11-29 22:41:08 +00:00
|
|
|
* @category Models
|
|
|
|
* @author Deon George
|
2013-03-19 22:35:19 +00:00
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
2010-11-29 22:41:08 +00:00
|
|
|
* @license http://dev.osbill.net/license.html
|
|
|
|
*/
|
2013-01-15 06:07:54 +00:00
|
|
|
class Model_Invoice extends ORM_OSB implements Cartable {
|
2010-11-29 22:41:08 +00:00
|
|
|
protected $_belongs_to = array(
|
|
|
|
'account'=>array()
|
|
|
|
);
|
|
|
|
protected $_has_many = array(
|
2011-07-13 22:59:32 +00:00
|
|
|
'invoice_item'=>array('far_key'=>'id'),
|
2011-10-10 23:38:21 +00:00
|
|
|
'invoice_item_tax'=>array('through'=>'invoice_item'),
|
2010-11-29 22:41:08 +00:00
|
|
|
'service'=>array('through'=>'invoice_item'),
|
|
|
|
'payment'=>array('through'=>'payment_item'),
|
2011-08-02 06:48:41 +00:00
|
|
|
'payment_item'=>array('far_key'=>'id'),
|
2010-11-29 22:41:08 +00:00
|
|
|
);
|
|
|
|
|
2011-08-02 06:20:11 +00:00
|
|
|
protected $_sorting = array(
|
|
|
|
'id'=>'DESC',
|
|
|
|
);
|
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
protected $_display_filters = array(
|
|
|
|
'date_orig'=>array(
|
|
|
|
array('Config::date',array(':value')),
|
|
|
|
),
|
|
|
|
'due_date'=>array(
|
|
|
|
array('Config::date',array(':value')),
|
|
|
|
),
|
|
|
|
'status'=>array(
|
2013-04-26 01:42:09 +00:00
|
|
|
array('StaticList_YesNo::get',array(':value')),
|
2011-07-14 09:09:03 +00:00
|
|
|
),
|
2010-11-29 22:41:08 +00:00
|
|
|
);
|
|
|
|
|
2013-02-08 11:41:29 +00:00
|
|
|
// Items belonging to an invoice
|
|
|
|
private $invoice_items = array();
|
2013-06-19 09:26:07 +00:00
|
|
|
private $invoice_items_unsorted = TRUE;
|
2013-02-08 11:41:29 +00:00
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
/** INTERFACE REQUIREMENTS **/
|
|
|
|
public function cart_item() {
|
|
|
|
return new Cart_Item(1,sprintf('Invoice: %s',$this->refnum()),$this->due());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return if this invoice is already in the cart
|
|
|
|
*/
|
|
|
|
public function cart_exists() {
|
|
|
|
return count(Cart::instance()->get($this->mid(),$this->id));
|
|
|
|
}
|
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
/**
|
|
|
|
* Intercept our object load, so that we can load our subitems
|
|
|
|
*/
|
|
|
|
protected function _load_values(array $values) {
|
|
|
|
parent::_load_values($values);
|
2012-01-09 01:35:24 +00:00
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
if ($this->_loaded)
|
|
|
|
$this->invoice_items = $this->invoice_item->find_all()->as_array();
|
2012-01-09 01:35:24 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
/**
|
2013-01-15 06:07:54 +00:00
|
|
|
* Add an item to an invoice
|
2010-11-29 22:41:08 +00:00
|
|
|
*/
|
2013-06-13 13:35:19 +00:00
|
|
|
public function add_item(Model_Invoice_Item $iio=NULL) {
|
|
|
|
// @todo This is the old calling of this function, which should be removed
|
|
|
|
if (is_null($iio)) {
|
|
|
|
$c = count($this->invoice_items);
|
2011-07-14 09:09:03 +00:00
|
|
|
|
2013-06-13 13:35:19 +00:00
|
|
|
$this->invoice_items[$c] = ORM::factory('Invoice_Item');
|
2011-05-02 12:28:17 +00:00
|
|
|
|
2013-06-13 13:35:19 +00:00
|
|
|
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;
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
/**
|
2013-01-15 06:07:54 +00:00
|
|
|
* Return a list of valid checkout options for this invoice
|
2011-07-14 09:09:03 +00:00
|
|
|
*/
|
2013-01-15 06:07:54 +00:00
|
|
|
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();
|
2012-08-01 12:34:59 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
/**
|
2013-01-15 06:07:54 +00:00
|
|
|
* Display the amount due
|
2011-07-14 09:09:03 +00:00
|
|
|
*/
|
2013-01-15 06:07:54 +00:00
|
|
|
public function due($format=FALSE) {
|
|
|
|
// If the invoice is active calculate the due amount
|
|
|
|
$result = $this->status ? $this->total()-$this->payments_total() : 0;
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2012-01-09 01:35:24 +00:00
|
|
|
return $format ? Currency::display($result) : Currency::round($result);
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
/**
|
|
|
|
* Display the Invoice Number
|
|
|
|
*/
|
|
|
|
public function id() {
|
|
|
|
return sprintf('%06s',$this->id);
|
2011-07-14 09:09:03 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
/**
|
2013-04-05 12:50:08 +00:00
|
|
|
* Return a list of invoice items for this invoice.
|
2013-02-08 11:41:29 +00:00
|
|
|
* @param type [CHARGE|CREDIT|ALL]
|
|
|
|
* @see invoice_items
|
2013-01-15 06:07:54 +00:00
|
|
|
*/
|
2013-02-08 11:41:29 +00:00
|
|
|
public function items($type='ALL') {
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach ($this->invoice_items as $ito) {
|
|
|
|
$return = FALSE;
|
2013-04-05 12:50:08 +00:00
|
|
|
|
2013-02-08 11:41:29 +00:00
|
|
|
switch ($type) {
|
|
|
|
case 'CHARGE':
|
|
|
|
if ($ito->quantity > 0)
|
|
|
|
$return = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'CREDIT':
|
|
|
|
if ($ito->quantity < 0)
|
|
|
|
$return = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ALL':
|
|
|
|
default:
|
|
|
|
$return = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($return)
|
|
|
|
array_push($result,$ito);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2012-01-09 01:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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->items() 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();
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-14 05:44:12 +00:00
|
|
|
* Get a list of invoice_items for a service_id on an invoice
|
2011-10-11 08:52:31 +00:00
|
|
|
*
|
|
|
|
* We use this to list details by service on an invoice.
|
2010-11-29 22:41:08 +00:00
|
|
|
*/
|
2012-01-09 01:35:24 +00:00
|
|
|
// @todo to retire
|
2011-10-11 08:52:31 +00:00
|
|
|
public function items_services(array $items=array()) {
|
|
|
|
$result = array();
|
|
|
|
if (! $items)
|
|
|
|
$items = $this->items();
|
|
|
|
|
|
|
|
foreach ($items as $ito)
|
2011-10-14 05:44:12 +00:00
|
|
|
if ($ito->service_id AND empty($result[$ito->service_id]))
|
|
|
|
$result[$ito->service_id] = $ito;
|
2011-10-11 08:52:31 +00:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2012-01-09 01:35:24 +00:00
|
|
|
// @todo to retire
|
|
|
|
public function items_invoice() {
|
2011-10-11 08:52:31 +00:00
|
|
|
$result = array();
|
|
|
|
$items = $this->items();
|
|
|
|
|
|
|
|
foreach ($items as $ito)
|
2012-01-09 01:35:24 +00:00
|
|
|
if (! $ito->service_id AND empty($result[$ito->id]))
|
|
|
|
$result[$ito->id] = $ito;
|
2011-10-11 08:52:31 +00:00
|
|
|
|
|
|
|
return $result;
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 01:35:24 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2010-11-29 22:41:08 +00:00
|
|
|
/**
|
2011-10-11 08:52:31 +00:00
|
|
|
* Return a list of periods and services
|
2011-07-14 09:09:03 +00:00
|
|
|
*
|
2011-10-11 08:52:31 +00:00
|
|
|
* This is so that we can list items summarised by billing period
|
2010-11-29 22:41:08 +00:00
|
|
|
*/
|
2011-10-11 08:52:31 +00:00
|
|
|
public function items_service_periods() {
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
$c = array();
|
|
|
|
foreach ($this->items() 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;
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-05-02 12:28:17 +00:00
|
|
|
/**
|
|
|
|
* Summarise the items on an invoice
|
2011-10-11 08:52:31 +00:00
|
|
|
*
|
|
|
|
* We summaries based on product.
|
2011-05-02 12:28:17 +00:00
|
|
|
*/
|
2012-01-09 01:35:24 +00:00
|
|
|
// @todo
|
2011-05-02 12:28:17 +00:00
|
|
|
public function items_summary() {
|
2011-07-14 09:09:03 +00:00
|
|
|
$result = array();
|
2011-05-02 12:28:17 +00:00
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
foreach ($this->items() as $ito) {
|
2011-10-14 05:44:12 +00:00
|
|
|
// We only summarise item_type=0
|
2011-10-11 08:52:31 +00:00
|
|
|
if (! $ito->item_type == 0)
|
|
|
|
continue;
|
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
$t = $ito->product->title();
|
2011-05-02 12:28:17 +00:00
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
if (! isset($result[$t])) {
|
|
|
|
$result[$t]['quantity'] = 0;
|
|
|
|
$result[$t]['subtotal'] = 0;
|
2011-05-02 12:28:17 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
$result[$t]['quantity'] += $ito->quantity;
|
|
|
|
$result[$t]['subtotal'] += $ito->subtotal();
|
2011-05-02 12:28:17 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
return $result;
|
|
|
|
}
|
2011-05-02 12:28:17 +00:00
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
/**
|
|
|
|
* Calculate the total for items for a service
|
|
|
|
*/
|
|
|
|
public function items_service_total($sid) {
|
|
|
|
$total = 0;
|
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
foreach ($this->items_service($sid) as $ito)
|
2011-07-14 09:09:03 +00:00
|
|
|
$total += $ito->total();
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
return $total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the tax of items for a service
|
|
|
|
*/
|
|
|
|
public function items_service_tax($sid) {
|
|
|
|
$total = 0;
|
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
foreach ($this->items_service($sid) as $ito)
|
|
|
|
$total += $ito->tax();
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
return $total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-11 08:52:31 +00:00
|
|
|
* Calculate the discounts of items for a service
|
2010-11-29 22:41:08 +00:00
|
|
|
*/
|
2011-10-11 08:52:31 +00:00
|
|
|
public function items_service_discount($sid) {
|
|
|
|
$total = 0;
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
foreach ($this->items_service($sid) as $ito)
|
|
|
|
$total += $ito->discount();
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
return $total;
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
public function min_due($date) {
|
|
|
|
return strtotime(date('Y-M-d',($date < time()) ? time()+ORM::factory('Invoice')->config('DUE_DAYS_MIN')*86400 : $date));
|
|
|
|
}
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
/**
|
2013-01-15 06:07:54 +00:00
|
|
|
* Display the Invoice Reference Number
|
2010-11-29 22:41:08 +00:00
|
|
|
*/
|
2013-01-15 06:07:54 +00:00
|
|
|
public function refnum() {
|
|
|
|
return sprintf('%s-%06s',$this->account->accnum(),$this->id);
|
|
|
|
}
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
public function payments() {
|
|
|
|
return $this->payment_item->find_all();
|
|
|
|
}
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
public function payments_total($format=FALSE) {
|
|
|
|
$result = 0;
|
2011-10-10 23:38:21 +00:00
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
foreach ($this->payments() as $po)
|
|
|
|
$result += $po->alloc_amt;
|
|
|
|
|
|
|
|
return $format ? Currency::display($result) : Currency::round($result);
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
/**
|
2013-01-15 06:07:54 +00:00
|
|
|
* Check the reminder value
|
2011-07-14 09:09:03 +00:00
|
|
|
*/
|
2013-01-15 06:07:54 +00:00
|
|
|
public function remind($key) {
|
2013-07-05 06:11:37 +00:00
|
|
|
if (isset($this->reminders[$key]))
|
|
|
|
return (is_array($this->reminders[$key])) ? end($this->reminders[$key]) : $this->reminders[$key];
|
2013-01-15 06:07:54 +00:00
|
|
|
else
|
|
|
|
return FALSE;
|
2011-10-11 08:52:31 +00:00
|
|
|
}
|
|
|
|
|
2011-05-14 07:35:33 +00:00
|
|
|
public function save(Validation $validation = NULL) {
|
2011-10-11 08:52:31 +00:00
|
|
|
// Our items will be clobbered once we save the object, so we need to save it here.
|
|
|
|
$items = $this->items();
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
// Save the invoice
|
2013-01-15 06:07:54 +00:00
|
|
|
if ($this->changed())
|
|
|
|
parent::save($validation);
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
// Need to save the associated items and their taxes
|
2013-01-15 06:07:54 +00:00
|
|
|
if ($this->loaded()) {
|
2011-10-11 08:52:31 +00:00
|
|
|
foreach ($items as $iio) {
|
|
|
|
$iio->invoice_id = $this->id;
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
if (! $iio->changed())
|
|
|
|
continue;
|
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
if (! $iio->check()) {
|
2010-11-29 22:41:08 +00:00
|
|
|
// @todo Mark invoice as cancelled and write a memo, then...
|
2013-01-15 06:07:54 +00:00
|
|
|
throw new Kohana_Exception('Problem saving invoice_item for invoice :invoice - Failed check()',array(':invoice'=>$this->id));
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
$iio->save();
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
if (! $iio->saved()) {
|
2010-11-29 22:41:08 +00:00
|
|
|
// @todo Mark invoice as cancelled and write a memo, then...
|
2013-01-15 06:07:54 +00:00
|
|
|
throw new Kohana_Exception('Problem saving invoice_item for invoice :invoice - Failed save()',array(':invoice'=>$this->id));
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @todo Need to save discount information
|
|
|
|
}
|
|
|
|
|
2011-10-14 05:44:12 +00:00
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
} else
|
|
|
|
throw new Kohana_Exception('Couldnt save invoice for some reason?');
|
2011-10-14 05:44:12 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 10:12:54 +00:00
|
|
|
public function set_remind($key,$value,$add=FALSE) {
|
2011-09-27 11:22:13 +00:00
|
|
|
// If our value is null, we'll remove it.
|
2013-07-05 06:11:37 +00:00
|
|
|
if (is_null($value) AND isset($this->reminders[$key]))
|
|
|
|
unset($this->reminders[$key]);
|
|
|
|
|
2012-03-30 04:13:01 +00:00
|
|
|
elseif ($add) {
|
2013-07-05 06:11:37 +00:00
|
|
|
if (! is_array($a=$this->reminders[$key]))
|
|
|
|
$this->reminders[$key] = array($a);
|
2012-03-30 04:13:01 +00:00
|
|
|
|
2013-07-05 06:11:37 +00:00
|
|
|
$this->reminders[$key][] = $value;
|
2012-03-30 04:13:01 +00:00
|
|
|
|
|
|
|
} else
|
2013-07-05 06:11:37 +00:00
|
|
|
$this->reminders[$key] = $value;
|
2011-09-27 11:22:13 +00:00
|
|
|
|
2011-09-26 10:12:54 +00:00
|
|
|
$this->save();
|
2013-07-05 06:11:37 +00:00
|
|
|
|
2011-09-26 10:12:54 +00:00
|
|
|
return $this->saved();
|
|
|
|
}
|
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
/**
|
|
|
|
* Return the subtotal of all items
|
|
|
|
*/
|
|
|
|
public function subtotal($format=FALSE) {
|
|
|
|
$result = 0;
|
|
|
|
|
|
|
|
foreach ($this->items() as $ito)
|
|
|
|
$result += $ito->subtotal();
|
|
|
|
|
|
|
|
return $format ? Currency::display($result) : Currency::round($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tax($format=FALSE) {
|
|
|
|
$result = 0;
|
|
|
|
|
|
|
|
foreach ($this->items() as $ito)
|
|
|
|
$result += $ito->tax();
|
|
|
|
|
|
|
|
return $format ? Currency::display($result) : Currency::round($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of taxes used on this invoice
|
|
|
|
* @todo Move some of this to invoice_item_tax.
|
|
|
|
*/
|
|
|
|
public function tax_summary() {
|
|
|
|
$summary = array();
|
|
|
|
|
|
|
|
foreach ($this->items() 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;
|
|
|
|
else
|
|
|
|
$summary[$item_tax->tax_id] += $item_tax->amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// @todo This should be removed eventually
|
|
|
|
if (! $summary)
|
|
|
|
$summary[1] = $this->tax();
|
|
|
|
|
|
|
|
return $summary;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the total of all items
|
|
|
|
*/
|
|
|
|
public function total($format=FALSE) {
|
|
|
|
$result = 0;
|
|
|
|
|
2013-02-26 03:19:32 +00:00
|
|
|
// This will include charges and credits
|
2013-01-15 06:07:54 +00:00
|
|
|
foreach ($this->items() as $ito)
|
|
|
|
$result += $ito->total();
|
|
|
|
|
|
|
|
return $format ? Currency::display($result) : Currency::round($result);
|
|
|
|
}
|
|
|
|
|
2013-02-08 11:41:29 +00:00
|
|
|
public function total_charges($format=FALSE) {
|
|
|
|
$result = 0;
|
|
|
|
|
|
|
|
foreach ($this->items('CHARGE') as $ito)
|
|
|
|
$result += $ito->subtotal()+$ito->tax();
|
|
|
|
|
|
|
|
return $format ? Currency::display($result) : Currency::round($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function total_credits($format=FALSE) {
|
|
|
|
$result = 0;
|
|
|
|
|
|
|
|
foreach ($this->items('CREDIT') as $ito)
|
|
|
|
$result += ($ito->subtotal()+$ito->tax())*-1;
|
|
|
|
|
|
|
|
return $format ? Currency::display($result) : Currency::round($result);
|
|
|
|
}
|
|
|
|
|
2013-02-26 03:19:32 +00:00
|
|
|
public function total_discounts($format=FALSE) {
|
2013-02-08 11:41:29 +00:00
|
|
|
$result = 0;
|
|
|
|
|
|
|
|
foreach ($this->items() as $ito)
|
|
|
|
$result += $ito->discount();
|
|
|
|
|
|
|
|
return $format ? Currency::display($result) : Currency::round($result);
|
|
|
|
}
|
|
|
|
|
2011-08-16 02:27:19 +00:00
|
|
|
/** LIST FUNCTIONS **/
|
|
|
|
|
2011-12-20 05:46:10 +00:00
|
|
|
/**
|
|
|
|
* Search for invoices matching a term
|
|
|
|
*/
|
2013-05-08 09:00:47 +00:00
|
|
|
public function list_autocomplete($term,$index,$value,array $label,array $limit=array(),array $options=NULL) {
|
|
|
|
// We only show invoice numbers.
|
|
|
|
if (! is_numeric($term))
|
|
|
|
return array();
|
2011-12-20 05:46:10 +00:00
|
|
|
|
2013-05-08 09:00:47 +00:00
|
|
|
$ao = Auth::instance()->get_user();
|
2011-12-20 05:46:10 +00:00
|
|
|
|
2013-05-08 09:00:47 +00:00
|
|
|
$this->clear();
|
|
|
|
$this->where_active();
|
2011-12-20 05:46:10 +00:00
|
|
|
|
2013-05-08 09:00:47 +00:00
|
|
|
// Build our where clause
|
|
|
|
$this->where_open()
|
|
|
|
->where('id','like','%'.$term.'%')
|
|
|
|
->where_close();
|
2011-12-20 05:46:10 +00:00
|
|
|
|
2013-05-08 09:00:47 +00:00
|
|
|
// Restrict results to authorised accounts
|
|
|
|
array_push($limit,array('account_id','IN',$ao->RTM->customers($ao->RTM)));
|
|
|
|
|
|
|
|
return parent::list_autocomplete($term,$index,$value,$label,$limit,$options);
|
2011-12-20 05:46:10 +00:00
|
|
|
}
|
|
|
|
|
2011-09-28 06:46:22 +00:00
|
|
|
private function _list_due() {
|
2011-10-11 08:52:31 +00:00
|
|
|
static $result = array();
|
|
|
|
|
|
|
|
if (! $result)
|
2013-02-08 11:41:29 +00:00
|
|
|
foreach ($this->_where_active()->_where_unprocessed()->find_all() as $io)
|
2011-10-11 08:52:31 +00:00
|
|
|
if ($io->due())
|
|
|
|
array_push($result,$io);
|
|
|
|
|
|
|
|
return $result;
|
2011-09-28 06:46:22 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 11:41:29 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2011-08-16 02:27:19 +00:00
|
|
|
/**
|
|
|
|
* Identify all the invoices that are due
|
|
|
|
*/
|
2011-09-28 06:46:22 +00:00
|
|
|
public function list_overdue($time=NULL) {
|
2011-10-11 08:52:31 +00:00
|
|
|
$result = array();
|
|
|
|
|
2011-08-16 02:27:19 +00:00
|
|
|
if (is_null($time))
|
|
|
|
$time = time();
|
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
foreach ($this->_list_due() as $io)
|
|
|
|
if ($io->due_date <= $time)
|
|
|
|
array_push($result,$io);
|
|
|
|
|
|
|
|
return $result;
|
2011-08-16 02:27:19 +00:00
|
|
|
}
|
|
|
|
|
2011-09-27 11:22:13 +00:00
|
|
|
/**
|
|
|
|
* Return a list of invoices that are over their due date with/without auto billing
|
|
|
|
*/
|
|
|
|
public function list_overdue_billing($time=NULL,$billing=FALSE) {
|
2013-04-05 12:50:08 +00:00
|
|
|
$result = array();
|
2011-09-27 11:22:13 +00:00
|
|
|
|
|
|
|
foreach ($this->list_overdue($time) as $io) {
|
|
|
|
$i = FALSE;
|
|
|
|
foreach ($io->service->find_all() as $so)
|
|
|
|
if (($billing AND $so->account_billing_id) OR (! $billing AND ! $so->account_billing_id)) {
|
2013-04-05 12:50:08 +00:00
|
|
|
array_push($result,$io);
|
2011-09-27 11:22:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-05 12:50:08 +00:00
|
|
|
return $result;
|
2011-09-27 11:22:13 +00:00
|
|
|
}
|
|
|
|
|
2011-08-16 02:27:19 +00:00
|
|
|
/**
|
|
|
|
* Return a list of invoices that are due, excluding overdue.
|
|
|
|
*/
|
|
|
|
public function list_due($time=NULL) {
|
2011-10-11 08:52:31 +00:00
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach ($this->_list_due() as $io)
|
2013-06-05 04:31:13 +00:00
|
|
|
if (is_null($time) OR $io->due_date > time())
|
2011-10-11 08:52:31 +00:00
|
|
|
if (is_null($time))
|
|
|
|
array_push($result,$io);
|
2011-10-14 05:44:12 +00:00
|
|
|
elseif ($io->due_date <= $time)
|
2011-10-11 08:52:31 +00:00
|
|
|
array_push($result,$io);
|
|
|
|
|
|
|
|
return $result;
|
2011-08-16 02:27:19 +00:00
|
|
|
}
|
2011-10-12 22:20:08 +00:00
|
|
|
|
2013-05-28 11:35:54 +00:00
|
|
|
public function list_due_total($format=FALSE,$time=NULL) {
|
|
|
|
$result = 0;
|
|
|
|
|
|
|
|
foreach ($this->list_due($time) as $io)
|
|
|
|
$result += $io->due();
|
|
|
|
|
|
|
|
return $format ? Currency::display($result) : Currency::round($result);
|
|
|
|
}
|
|
|
|
|
2011-10-14 05:44:12 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public function list_tosend() {
|
2012-11-09 23:13:57 +00:00
|
|
|
return ORM::factory('Invoice')->where_active()->where_open()->where('print_status','is',NULL)->or_where('print_status','!=',1)->where_close();
|
2011-10-14 05:44:12 +00:00
|
|
|
}
|
|
|
|
|
2011-10-12 22:20:08 +00:00
|
|
|
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>';
|
|
|
|
|
2011-12-22 10:20:19 +00:00
|
|
|
$output = View::factory('invoice/user/email')
|
2011-10-12 22:20:08 +00:00
|
|
|
->set('mediapath',Route::get('default/media'))
|
|
|
|
->set('io',$this);
|
|
|
|
|
|
|
|
return $css.$output;
|
|
|
|
}
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
?>
|