2010-11-29 22:41:08 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class supports Services
|
|
|
|
*
|
|
|
|
* @package OSB
|
|
|
|
* @subpackage Service
|
|
|
|
* @category Models
|
|
|
|
* @author Deon George
|
|
|
|
* @copyright (c) 2010 Open Source Billing
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
class Model_Service extends ORMOSB {
|
|
|
|
// Relationships
|
2011-10-14 05:44:12 +00:00
|
|
|
protected $_has_one = array(
|
2011-12-16 23:31:35 +00:00
|
|
|
'affiliate'=>array('far_key'=>'affiliate_id','foreign_key'=>'id'),
|
2011-10-14 05:44:12 +00:00
|
|
|
'service_billing'=>array('far_key'=>'account_billing_id','foreign_key'=>'id'),
|
|
|
|
);
|
2010-11-29 22:41:08 +00:00
|
|
|
protected $_has_many = array(
|
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'),
|
|
|
|
);
|
|
|
|
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(
|
|
|
|
'active'=>array(
|
|
|
|
array('StaticList_YesNo::display',array(':value')),
|
|
|
|
),
|
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(
|
|
|
|
array('StaticList_RecurSchedule::display',array(':value')),
|
|
|
|
),
|
2010-11-29 22:41:08 +00:00
|
|
|
);
|
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
/**
|
|
|
|
* Return the object of the product plugin
|
|
|
|
*/
|
2012-06-22 05:12:59 +00:00
|
|
|
public function plugin($type='') {
|
2011-07-14 09:09:03 +00:00
|
|
|
if (! $this->product->prod_plugin_file)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (! is_numeric($this->product->prod_plugin_data))
|
|
|
|
throw new Kohana_Exception('Missing plugin_id for :product (:type)',array(':product'=>$this->product->id,':type'=>$this->product->prod_plugin_file));
|
|
|
|
|
2012-06-22 05:12:59 +00:00
|
|
|
$o = ORM::factory(sprintf('service_plugin_%s',$this->product->prod_plugin_file),array('service_id'=>$this->id));
|
|
|
|
|
|
|
|
return $type ? $o->$type : $o;
|
2011-07-14 09:09:03 +00:00
|
|
|
}
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
/**
|
|
|
|
* Display the service number
|
|
|
|
*/
|
2011-07-14 09:09:03 +00:00
|
|
|
public function id() {
|
2010-11-29 22:41:08 +00:00
|
|
|
return sprintf('%05s',$this->id);
|
|
|
|
}
|
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
/**
|
|
|
|
* Display the service product name
|
|
|
|
*/
|
|
|
|
public function name() {
|
2011-09-28 06:46:22 +00:00
|
|
|
return is_null($plugin=$this->plugin()) ? $this->product->name() : $plugin->name();
|
2011-07-14 09:09:03 +00:00
|
|
|
}
|
|
|
|
|
2012-03-30 04:13:01 +00:00
|
|
|
/**
|
|
|
|
* 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-09-28 06:46:22 +00:00
|
|
|
public function service_name() {
|
|
|
|
return is_null($plugin=$this->plugin()) ? $this->name() : $plugin->service_name();
|
2011-07-14 09:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function service_view() {
|
2011-09-28 06:46:22 +00:00
|
|
|
return is_null($plugin=$this->plugin()) ? HTML::nbsp('') : $plugin->service_view();
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-09-28 06:46:22 +00:00
|
|
|
/**
|
|
|
|
* Display the product feature summary
|
|
|
|
*/
|
|
|
|
public function product_feature_summary() {
|
|
|
|
return $this->product->feature_summary();
|
2011-07-14 09:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render some details for specific calls, eg: invoice
|
|
|
|
*/
|
|
|
|
public function details($type) {
|
|
|
|
switch ($type) {
|
2011-09-28 06:46:22 +00:00
|
|
|
case 'invoice_detail_items':
|
2011-07-14 09:09:03 +00:00
|
|
|
if (is_null($plugin = $this->plugin()))
|
|
|
|
return array();
|
|
|
|
else
|
|
|
|
return $plugin->_details($type);
|
|
|
|
break;
|
|
|
|
|
|
|
|
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() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
/** LIST FUNCTIONS **/
|
|
|
|
|
2011-12-16 23:31:35 +00:00
|
|
|
private function _list_active() {
|
|
|
|
return $this->where('active','=',1);
|
|
|
|
}
|
|
|
|
|
2011-09-28 06:46:22 +00:00
|
|
|
public function list_active() {
|
2011-12-16 23:31:35 +00:00
|
|
|
return $this->_list_active()->find_all();
|
2011-09-28 06:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function list_bylistgroup($cat) {
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
$cats = ORM::factory('product_category')->list_bylistgroup($cat);
|
|
|
|
|
|
|
|
foreach ($this->list_active() as $so) {
|
2011-12-27 01:06:04 +00:00
|
|
|
if (! $so->product->avail_category OR ! preg_match('/^a:/',$so->product->avail_category))
|
2011-09-28 06:46:22 +00:00
|
|
|
continue;
|
|
|
|
|
2011-12-27 01:06:04 +00:00
|
|
|
$pc = unserialize($so->product->avail_category);
|
2011-09-28 06:46:22 +00:00
|
|
|
if (array_intersect($pc,array_keys($cats)))
|
|
|
|
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) {
|
2011-12-16 23:31:35 +00:00
|
|
|
return $this->_list_active()
|
|
|
|
->where_open()->where('suspend_billing','IS',NULL)->or_where('suspend_billing','=','0')->where_close()
|
2012-01-02 01:35:47 +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() {
|
|
|
|
return $this->_list_active()->where('queue','=','PROVISION');
|
2011-10-10 23:38:21 +00:00
|
|
|
}
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
?>
|