2010-11-29 22:41:08 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class supports Services
|
|
|
|
*
|
2013-03-19 22:35:19 +00:00
|
|
|
* @package Service
|
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
|
2011-12-16 23:31:35 +00:00
|
|
|
*
|
|
|
|
* Fields:
|
|
|
|
* + queue: PROVISION (to be provisioned)
|
2010-11-29 22:41:08 +00:00
|
|
|
*/
|
2012-12-10 21:48:30 +00:00
|
|
|
class Model_Service extends ORM_OSB {
|
2010-11-29 22:41:08 +00:00
|
|
|
// Relationships
|
2011-10-14 05:44:12 +00:00
|
|
|
protected $_has_one = array(
|
|
|
|
'service_billing'=>array('far_key'=>'account_billing_id','foreign_key'=>'id'),
|
|
|
|
);
|
2010-11-29 22:41:08 +00:00
|
|
|
protected $_has_many = array(
|
2013-06-04 11:50:41 +00:00
|
|
|
'charge'=>array('far_key'=>'id'),
|
2012-01-09 01:35:24 +00:00
|
|
|
'invoice_item'=>array('far_key'=>'id'),
|
2010-11-29 22:41:08 +00:00
|
|
|
'invoice'=>array('through'=>'invoice_item'),
|
2013-01-23 11:17:22 +00:00
|
|
|
'service_change'=>array('far_key'=>'id'),
|
2010-11-29 22:41:08 +00:00
|
|
|
);
|
|
|
|
protected $_belongs_to = array(
|
|
|
|
'product'=>array(),
|
|
|
|
'account'=>array(),
|
|
|
|
);
|
|
|
|
|
2011-05-14 07:35:33 +00:00
|
|
|
/**
|
|
|
|
* Filters used to format the display of values into friendlier values
|
|
|
|
*/
|
|
|
|
protected $_display_filters = array(
|
2011-07-14 09:09:03 +00:00
|
|
|
'date_last_invoice'=>array(
|
|
|
|
array('Config::date',array(':value')),
|
|
|
|
),
|
2011-05-14 07:35:33 +00:00
|
|
|
'date_next_invoice'=>array(
|
|
|
|
array('Config::date',array(':value')),
|
|
|
|
),
|
|
|
|
'recur_schedule'=>array(
|
2013-04-26 01:42:09 +00:00
|
|
|
array('StaticList_RecurSchedule::get',array(':value')),
|
2011-05-14 07:35:33 +00:00
|
|
|
),
|
2012-08-01 12:43:33 +00:00
|
|
|
'status'=>array(
|
2013-04-26 01:42:09 +00:00
|
|
|
array('StaticList_YesNo::get',array(':value')),
|
2012-08-01 12:43:33 +00:00
|
|
|
),
|
2010-11-29 22:41:08 +00:00
|
|
|
);
|
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
/**
|
2013-06-04 11:50:41 +00:00
|
|
|
* Get the additional charges associated with this service
|
2011-07-14 09:09:03 +00:00
|
|
|
*/
|
2013-06-04 11:50:41 +00:00
|
|
|
public function charges($unprocessed=TRUE,$format=FALSE) {
|
|
|
|
$result = 0;
|
2011-07-14 09:09:03 +00:00
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
foreach ($this->charge_list($unprocessed) as $co)
|
|
|
|
$result += $co->total();
|
2012-06-22 05:12:59 +00:00
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
return $format ? Currency::display($result) : $result;
|
2011-07-14 09:09:03 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
public function charge_list($unprocessed=FALSE) {
|
|
|
|
$x = $this->charge;
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
if ($unprocessed)
|
|
|
|
$x->where_open()
|
|
|
|
->where('processed','IS',NULL)
|
|
|
|
->or_where('processed','=',FALSE)
|
|
|
|
->where_close();
|
2011-07-14 09:09:03 +00:00
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
return $x->find_all();
|
2013-01-23 11:17:22 +00:00
|
|
|
}
|
|
|
|
|
2012-10-08 12:52:21 +00:00
|
|
|
/**
|
|
|
|
* Display how much is due on this service
|
|
|
|
*/
|
|
|
|
public function due($format=FALSE) {
|
|
|
|
$total = 0;
|
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
foreach ($this->invoice_list(TRUE) as $io)
|
2012-10-08 12:52:21 +00:00
|
|
|
$total += $io->due();
|
|
|
|
|
|
|
|
return $format ? Currency::display($total) : $total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When does this service expire
|
|
|
|
*/
|
|
|
|
public function expire($format=FALSE) {
|
|
|
|
// For plugins the plugin determins expiry
|
|
|
|
$expire = (is_null($plugin=$this->plugin()) ? NULL : $plugin->expire());
|
|
|
|
|
|
|
|
// If $expire is NULL, we'll use the next invoice date
|
|
|
|
$expire = is_null($expire) ? $this->date_next_invoice-86400 : $expire;
|
|
|
|
|
|
|
|
return $format ? Config::date($expire) : $expire;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if a service expires in the next $days.
|
|
|
|
*/
|
2013-06-04 11:50:41 +00:00
|
|
|
public function expiring($days=0) {
|
2012-10-08 12:52:21 +00:00
|
|
|
return time()+$days*86400 > $this->expire();
|
|
|
|
}
|
|
|
|
|
2012-03-30 04:13:01 +00:00
|
|
|
/**
|
2013-06-04 11:50:41 +00:00
|
|
|
* Display the service number
|
2012-03-30 04:13:01 +00:00
|
|
|
*/
|
2013-06-04 11:50:41 +00:00
|
|
|
public function id() {
|
|
|
|
return sprintf('%05s',$this->id);
|
|
|
|
}
|
2012-03-30 04:13:01 +00:00
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
/**
|
|
|
|
* List invoices for this service
|
|
|
|
*/
|
|
|
|
public function invoice_list($due=FALSE) {
|
|
|
|
$result = array();
|
2012-03-30 04:13:01 +00:00
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
$x = $this->invoice->distinct('id');
|
2012-03-30 04:13:01 +00:00
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
// If we only want due invoices, we can speed things up by only looking for unprocessed
|
|
|
|
if ($due)
|
|
|
|
$x->where_unprocessed();
|
|
|
|
|
|
|
|
foreach ($x->find_all() as $io)
|
|
|
|
if (! $due OR $io->due())
|
|
|
|
array_push($result,$io);
|
|
|
|
|
|
|
|
return $result;
|
2012-03-30 04:13:01 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
/**
|
|
|
|
* Display the service product name
|
|
|
|
*/
|
|
|
|
public function name() {
|
|
|
|
return is_null($plugin=$this->plugin()) ? $this->product->title() : $plugin->name();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns TRUE of this service has a planend change
|
|
|
|
*/
|
|
|
|
public function pending_change() {
|
|
|
|
return $this->service_change()->loaded() ? TRUE : FALSE;
|
2011-07-14 09:09:03 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
/**
|
|
|
|
* Show the product that this service will be changed to
|
|
|
|
*/
|
|
|
|
public function pending_product() {
|
|
|
|
return $this->service_change()->product;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the object of the product plugin
|
|
|
|
*/
|
|
|
|
public function plugin($type='') {
|
|
|
|
if (! $this->product->prod_plugin_file)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
$o = ORM::factory(Kohana::classname(sprintf('Service_Plugin_%s',$this->product->prod_plugin_file)),array('service_id'=>$this->id));
|
|
|
|
|
|
|
|
return $type ? $o->$type : $o;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function service_change() {
|
|
|
|
return $this->service_change->where_active()->where_open()->and_where('complete','!=',1)->or_where('complete','IS',null)->where_close()->find();
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-09-28 06:46:22 +00:00
|
|
|
/**
|
2013-06-04 11:50:41 +00:00
|
|
|
* Return a descriptive name for this service
|
2011-09-28 06:46:22 +00:00
|
|
|
*/
|
2013-06-04 11:50:41 +00:00
|
|
|
public function service_name() {
|
|
|
|
return is_null($plugin=$this->plugin()) ? $this->name() : $plugin->service_name();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the service charge
|
|
|
|
*/
|
|
|
|
public function price($tax=FALSE,$format=FALSE) {
|
|
|
|
$x = $this->product->keyget('price_group',$this->recur_schedule);
|
|
|
|
|
|
|
|
// @todo This index shouldnt be hard coded.
|
|
|
|
$p = $this->price ? $this->price : $x[$this->price_group]['price_base'];
|
|
|
|
|
|
|
|
if ($tax)
|
|
|
|
$p = Tax::add($p);
|
|
|
|
|
|
|
|
return $format ? Currency::display($p) : $p;
|
2011-07-14 09:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render some details for specific calls, eg: invoice
|
|
|
|
*/
|
|
|
|
public function details($type) {
|
2013-06-04 11:50:41 +00:00
|
|
|
$plugin = $this->plugin();
|
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
switch ($type) {
|
2011-09-28 06:46:22 +00:00
|
|
|
case 'invoice_detail_items':
|
2013-06-04 11:50:41 +00:00
|
|
|
return is_null($plugin) ? array() : $plugin->_details($type);
|
|
|
|
|
|
|
|
case 'service_view':
|
|
|
|
return is_null($plugin) ? HTML::nbsp('') : $plugin->service_view();
|
2011-07-14 09:09:03 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Kohana_Exception('Unkown detail request :type',array(':type'=>$type));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-16 23:31:35 +00:00
|
|
|
/**
|
|
|
|
* Enable the plugin to store data
|
|
|
|
*/
|
2011-07-14 09:09:03 +00:00
|
|
|
public function admin_update() {
|
|
|
|
if (is_null($plugin = $this->plugin()))
|
|
|
|
return NULL;
|
|
|
|
else
|
2011-12-16 23:31:35 +00:00
|
|
|
return $plugin->admin_update();
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 01:35:24 +00:00
|
|
|
public function transactions() {
|
2012-03-31 03:33:13 +00:00
|
|
|
return $this->invoice_item->order_by('date_start,date_stop');
|
2012-01-09 01:35:24 +00:00
|
|
|
}
|
|
|
|
|
2012-02-22 08:15:46 +00:00
|
|
|
// @todo To implement
|
|
|
|
public function charges_new() {
|
2013-06-04 11:50:41 +00:00
|
|
|
return $this->charges();
|
2012-02-22 08:15:46 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
/** LIST FUNCTIONS **/
|
|
|
|
|
2012-10-07 04:15:34 +00:00
|
|
|
/**
|
|
|
|
* Search for services 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();
|
|
|
|
|
|
|
|
$ao = Auth::instance()->get_user();
|
2012-10-07 04:15:34 +00:00
|
|
|
|
|
|
|
$this->clear();
|
|
|
|
$this->where_active();
|
|
|
|
|
|
|
|
// Build our where clause
|
|
|
|
$this->where_open()
|
2013-05-08 09:00:47 +00:00
|
|
|
->where('id','like','%'.$term.'%')
|
|
|
|
->where_close();
|
2012-10-07 04:15:34 +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)));
|
2012-10-07 04:15:34 +00:00
|
|
|
|
2013-05-08 09:00:47 +00:00
|
|
|
return parent::list_autocomplete($term,$index,$value,$label,$limit,$options);
|
2012-10-07 04:15:34 +00:00
|
|
|
}
|
2013-06-04 11:50:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List all products by their plugin type
|
|
|
|
*/
|
|
|
|
public function list_byplugin($plugin) {
|
|
|
|
return $this
|
|
|
|
->join('product')
|
|
|
|
->on($this->table_name().'.site_id','=','product.site_id') // @todo This should be automatic
|
|
|
|
->on($this->table_name().'.product_id','=','product.id')
|
|
|
|
->where('prod_plugin_file','=',$plugin)
|
|
|
|
->and_where('service.status','=',TRUE)
|
|
|
|
->find_all();
|
|
|
|
}
|
|
|
|
|
2011-09-28 06:46:22 +00:00
|
|
|
public function list_bylistgroup($cat) {
|
|
|
|
$result = array();
|
|
|
|
|
2013-01-11 06:03:13 +00:00
|
|
|
foreach ($this->list_active() as $so)
|
2013-04-26 01:42:09 +00:00
|
|
|
if ($so->product->prod_plugin_file == $cat)
|
2011-09-28 06:46:22 +00:00
|
|
|
array_push($result,$so);
|
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
Sort::MASort($result,'service_name()');
|
2011-09-28 06:46:22 +00:00
|
|
|
return $result;
|
|
|
|
}
|
2011-10-10 23:38:21 +00:00
|
|
|
|
2012-10-08 12:52:21 +00:00
|
|
|
/**
|
|
|
|
* List services expiring
|
|
|
|
*/
|
|
|
|
public function list_expiring($days=14) {
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach ($this->list_active() as $so)
|
|
|
|
if ($so->expiring($days))
|
|
|
|
array_push($result,$so);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2011-10-10 23:38:21 +00:00
|
|
|
/**
|
|
|
|
* List services that need to be billed.
|
2012-01-02 01:35:47 +00:00
|
|
|
*
|
|
|
|
* @param $days int Additional number of days to add to the query, above the module config.
|
2011-10-10 23:38:21 +00:00
|
|
|
*/
|
2012-01-02 01:35:47 +00:00
|
|
|
public function list_invoicesoon($days=0) {
|
2012-08-01 12:43:33 +00:00
|
|
|
return $this->_where_active()
|
2011-12-16 23:31:35 +00:00
|
|
|
->where_open()->where('suspend_billing','IS',NULL)->or_where('suspend_billing','=','0')->where_close()
|
2012-11-09 23:13:57 +00:00
|
|
|
->where('date_next_invoice','<',time()+(ORM::factory('Invoice')->config('GEN_DAYS')+$days)*86400)
|
2011-12-16 23:31:35 +00:00
|
|
|
->find_all();
|
|
|
|
}
|
2011-10-10 23:38:21 +00:00
|
|
|
|
2011-12-16 23:31:35 +00:00
|
|
|
/**
|
|
|
|
* List services that need to be provisioned
|
|
|
|
*/
|
|
|
|
public function list_provision() {
|
2012-08-01 12:43:33 +00:00
|
|
|
return $this->_where_active()->where('queue','=','PROVISION');
|
2011-10-10 23:38:21 +00:00
|
|
|
}
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
?>
|