178 lines
4.4 KiB
PHP
178 lines
4.4 KiB
PHP
<?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
|
|
*
|
|
* Fields:
|
|
* + queue: PROVISION (to be provisioned)
|
|
*/
|
|
class Model_Service extends ORMOSB {
|
|
// Relationships
|
|
protected $_has_one = array(
|
|
'affiliate'=>array('far_key'=>'affiliate_id','foreign_key'=>'id'),
|
|
'service_billing'=>array('far_key'=>'account_billing_id','foreign_key'=>'id'),
|
|
);
|
|
protected $_has_many = array(
|
|
'invoice_item'=>array('far_key'=>'id'),
|
|
'invoice'=>array('through'=>'invoice_item'),
|
|
);
|
|
protected $_belongs_to = array(
|
|
'product'=>array(),
|
|
'account'=>array(),
|
|
);
|
|
|
|
/**
|
|
* Filters used to format the display of values into friendlier values
|
|
*/
|
|
protected $_display_filters = array(
|
|
'active'=>array(
|
|
array('StaticList_YesNo::display',array(':value')),
|
|
),
|
|
'date_last_invoice'=>array(
|
|
array('Config::date',array(':value')),
|
|
),
|
|
'date_next_invoice'=>array(
|
|
array('Config::date',array(':value')),
|
|
),
|
|
'recur_schedule'=>array(
|
|
array('StaticList_RecurSchedule::display',array(':value')),
|
|
),
|
|
'price'=>array(
|
|
array('Tax::add',array(':value')),
|
|
array('Currency::display',array(':value')),
|
|
),
|
|
);
|
|
|
|
/**
|
|
* Return the object of the product plugin
|
|
*/
|
|
public function plugin() {
|
|
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));
|
|
|
|
return ORM::factory(sprintf('service_plugin_%s',$this->product->prod_plugin_file),array('service_id'=>$this->id));
|
|
}
|
|
|
|
/**
|
|
* Display the service number
|
|
*/
|
|
public function id() {
|
|
return sprintf('%05s',$this->id);
|
|
}
|
|
|
|
/**
|
|
* Display the service product name
|
|
*/
|
|
public function name() {
|
|
return is_null($plugin=$this->plugin()) ? $this->product->name() : $plugin->name();
|
|
}
|
|
|
|
public function service_name() {
|
|
return is_null($plugin=$this->plugin()) ? $this->name() : $plugin->service_name();
|
|
}
|
|
|
|
public function service_view() {
|
|
return is_null($plugin=$this->plugin()) ? HTML::nbsp('') : $plugin->service_view();
|
|
}
|
|
|
|
/**
|
|
* Display the product feature summary
|
|
*/
|
|
public function product_feature_summary() {
|
|
return $this->product->feature_summary();
|
|
}
|
|
|
|
/**
|
|
* Render some details for specific calls, eg: invoice
|
|
*/
|
|
public function details($type) {
|
|
switch ($type) {
|
|
case 'invoice_detail_items':
|
|
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));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Enable the plugin to store data
|
|
*/
|
|
public function admin_update() {
|
|
if (is_null($plugin = $this->plugin()))
|
|
return NULL;
|
|
else
|
|
return $plugin->admin_update();
|
|
}
|
|
|
|
public function transactions() {
|
|
return $this->invoice_item->order_by('date_start,date_stop')->find_all();
|
|
}
|
|
|
|
// @todo To implement
|
|
public function charges_new() {
|
|
return 0;
|
|
}
|
|
|
|
/** LIST FUNCTIONS **/
|
|
|
|
private function _list_active() {
|
|
return $this->where('active','=',1);
|
|
}
|
|
|
|
public function list_active() {
|
|
return $this->_list_active()->find_all();
|
|
}
|
|
|
|
public function list_bylistgroup($cat) {
|
|
$result = array();
|
|
|
|
$cats = ORM::factory('product_category')->list_bylistgroup($cat);
|
|
|
|
foreach ($this->list_active() as $so) {
|
|
if (! $so->product->avail_category OR ! preg_match('/^a:/',$so->product->avail_category))
|
|
continue;
|
|
|
|
$pc = unserialize($so->product->avail_category);
|
|
if (array_intersect($pc,array_keys($cats)))
|
|
array_push($result,$so);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* List services that need to be billed.
|
|
*
|
|
* @param $days int Additional number of days to add to the query, above the module config.
|
|
*/
|
|
public function list_invoicesoon($days=0) {
|
|
return $this->_list_active()
|
|
->where_open()->where('suspend_billing','IS',NULL)->or_where('suspend_billing','=','0')->where_close()
|
|
->where('date_next_invoice','<',time()+(ORM::factory('invoice')->config('GEN_DAYS')+$days)*86400)
|
|
->find_all();
|
|
}
|
|
|
|
/**
|
|
* List services that need to be provisioned
|
|
*/
|
|
public function list_provision() {
|
|
return $this->_list_active()->where('queue','=','PROVISION');
|
|
}
|
|
}
|
|
?>
|