2013-10-10 02:44:53 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class supports Services
|
|
|
|
*
|
|
|
|
* @package Service
|
|
|
|
* @category Models
|
|
|
|
* @author Deon George
|
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
|
|
|
* @license http://dev.osbill.net/license.html
|
|
|
|
*
|
|
|
|
* Fields:
|
|
|
|
* + queue: PROVISION (to be provisioned)
|
|
|
|
*/
|
2016-08-15 11:23:18 +00:00
|
|
|
class Model_Service extends ORM {
|
2016-08-03 04:00:51 +00:00
|
|
|
private $_plugin = NULL;
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
// Relationships
|
|
|
|
protected $_has_one = array(
|
|
|
|
'service_billing'=>array('far_key'=>'account_billing_id','foreign_key'=>'id'),
|
|
|
|
);
|
|
|
|
protected $_has_many = array(
|
2013-06-04 11:50:41 +00:00
|
|
|
'charge'=>array('far_key'=>'id'),
|
2013-10-10 02:44:53 +00:00
|
|
|
'invoice_item'=>array('far_key'=>'id'),
|
|
|
|
'invoice'=>array('through'=>'invoice_item'),
|
|
|
|
'service_change'=>array('far_key'=>'id'),
|
2013-10-11 02:08:50 +00:00
|
|
|
'service_memo'=>array('far_key'=>'id'),
|
2013-10-10 02:44:53 +00:00
|
|
|
);
|
|
|
|
protected $_belongs_to = array(
|
|
|
|
'product'=>array(),
|
|
|
|
'account'=>array(),
|
|
|
|
);
|
|
|
|
|
2013-11-28 06:41:34 +00:00
|
|
|
protected $_save_message = TRUE;
|
|
|
|
|
2013-11-27 00:22:20 +00:00
|
|
|
// Validation rules
|
|
|
|
public function rules() {
|
|
|
|
$x = Arr::merge(parent::rules(), array(
|
|
|
|
'product_id' => array(
|
|
|
|
array('not_equal', array(':value', array(0))),
|
|
|
|
),
|
|
|
|
));
|
|
|
|
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
/**
|
|
|
|
* Filters used to format the display of values into friendlier values
|
|
|
|
*/
|
|
|
|
protected $_display_filters = array(
|
2014-06-25 00:57:36 +00:00
|
|
|
'date_start'=>array(
|
2014-08-25 04:41:07 +00:00
|
|
|
array('Site::Date',array(':value')),
|
2014-06-25 00:57:36 +00:00
|
|
|
),
|
|
|
|
'date_end'=>array(
|
2014-08-25 04:41:07 +00:00
|
|
|
array('Site::Date',array(':value')),
|
2014-06-25 00:57:36 +00:00
|
|
|
),
|
2013-10-10 02:44:53 +00:00
|
|
|
'date_last_invoice'=>array(
|
2014-08-25 04:41:07 +00:00
|
|
|
array('Site::Date',array(':value')),
|
2013-10-10 02:44:53 +00:00
|
|
|
),
|
|
|
|
'date_next_invoice'=>array(
|
2014-08-25 04:41:07 +00:00
|
|
|
array('Site::Date',array(':value')),
|
2013-10-10 02:44:53 +00:00
|
|
|
),
|
2014-01-23 13:20:48 +00:00
|
|
|
'external_billing'=>array(
|
|
|
|
array('StaticList_YesNo::get',array(':value',TRUE)),
|
|
|
|
),
|
2013-11-08 11:02:32 +00:00
|
|
|
'price_override'=>array(
|
|
|
|
array('Currency::display',array(':value')),
|
|
|
|
),
|
2013-10-10 02:44:53 +00:00
|
|
|
'recur_schedule'=>array(
|
2013-04-26 01:42:09 +00:00
|
|
|
array('StaticList_RecurSchedule::get',array(':value')),
|
2013-10-10 02:44:53 +00:00
|
|
|
),
|
2014-01-07 12:29:18 +00:00
|
|
|
'suspend_billing'=>array(
|
|
|
|
array('StaticList_YesNo::get',array(':value',TRUE)),
|
|
|
|
),
|
2013-10-10 02:44:53 +00:00
|
|
|
'status'=>array(
|
2013-11-08 11:02:32 +00:00
|
|
|
array('StaticList_YesNo::get',array(':value',TRUE)),
|
2013-10-10 02:44:53 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2013-11-27 00:22:20 +00:00
|
|
|
protected $_nullifempty = array(
|
|
|
|
'price_override',
|
|
|
|
);
|
|
|
|
|
2016-08-03 04:00:51 +00:00
|
|
|
protected $_form = array('id'=>'id','value'=>'name()');
|
2013-09-06 05:39:56 +00:00
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
// Cache our calls to our plugins
|
|
|
|
public static $plugin = array();
|
|
|
|
|
2016-08-03 04:00:51 +00:00
|
|
|
/** REQUIRED ABSTRACT METHODS **/
|
|
|
|
|
|
|
|
/** LOCAL METHODS **/
|
|
|
|
|
|
|
|
private function _plugin() {
|
|
|
|
if (! $this->_plugin AND $this->product->prod_plugin_file) {
|
|
|
|
$this->_plugin = ORM::factory(Kohana::classname(sprintf('Service_Plugin_%s',$this->product->prod_plugin_file)),array('service_id'=>$this->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_plugin;
|
|
|
|
}
|
|
|
|
|
2016-08-10 06:07:00 +00:00
|
|
|
/**
|
|
|
|
* Get the attributes of the service
|
|
|
|
*
|
|
|
|
* @return an array of attributes
|
|
|
|
*/
|
|
|
|
public function attributes($variable=NULL) {
|
|
|
|
return ($this->_plugin()) ? $this->_plugin()->attributes() : array();
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
/**
|
2013-06-04 11:50:41 +00:00
|
|
|
* Get the additional charges associated with this service
|
2013-10-10 02:44:53 +00:00
|
|
|
*/
|
2013-06-04 11:50:41 +00:00
|
|
|
public function charges($unprocessed=TRUE,$format=FALSE) {
|
|
|
|
$result = 0;
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
foreach ($this->charge_list($unprocessed) as $co)
|
|
|
|
$result += $co->total();
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
return $format ? Currency::display($result) : $result;
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
public function charge_list($unprocessed=FALSE) {
|
|
|
|
$x = $this->charge;
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
if ($unprocessed)
|
|
|
|
$x->where_open()
|
2013-09-06 05:39:56 +00:00
|
|
|
->where('processed','IS',NULL)
|
|
|
|
->or_where('processed','=',FALSE)
|
|
|
|
->where_close();
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-09-06 05:39:56 +00:00
|
|
|
return $x->and_where('void','IS',NULL)->find_all();
|
2013-10-10 02:44:53 +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)
|
2013-10-10 02:44:53 +00:00
|
|
|
$total += $io->due();
|
|
|
|
|
|
|
|
return $format ? Currency::display($total) : $total;
|
|
|
|
}
|
|
|
|
|
2013-11-22 04:36:50 +00:00
|
|
|
public function email() {
|
|
|
|
return ORM::factory('Email_Log')
|
|
|
|
->where('module_id','=',$this->mid())
|
|
|
|
->where('module_data','=',$this);
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2013-06-05 14:03:55 +00:00
|
|
|
$expire = is_null($expire) ? $this->paid_to() : $expire;
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2014-08-25 04:41:07 +00:00
|
|
|
return $format ? Site::Date($expire) : $expire;
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if a service expires in the next $days.
|
|
|
|
*/
|
2013-06-04 11:50:41 +00:00
|
|
|
public function expiring($days=0) {
|
2013-10-10 02:44:53 +00:00
|
|
|
return time()+$days*86400 > $this->expire();
|
|
|
|
}
|
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
/**
|
|
|
|
* List invoices for this service
|
|
|
|
*/
|
2016-07-27 04:25:17 +00:00
|
|
|
public function invoice_list($due=FALSE,$num=NULL) {
|
2013-06-04 11:50:41 +00:00
|
|
|
$result = array();
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
$x = $this->invoice->distinct('id');
|
2013-10-10 02:44:53 +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();
|
|
|
|
|
2016-07-27 04:25:17 +00:00
|
|
|
if (! is_null($num))
|
|
|
|
$x->limit($num);
|
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
foreach ($x->find_all() as $io)
|
|
|
|
if (! $due OR $io->due())
|
|
|
|
array_push($result,$io);
|
|
|
|
|
|
|
|
return $result;
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-06-05 14:03:55 +00:00
|
|
|
/**
|
|
|
|
* Show the date we are invoiced to
|
|
|
|
*/
|
|
|
|
public function invoiced_to($format=FALSE) {
|
2013-09-06 05:39:56 +00:00
|
|
|
$iio = $this->last_invoice_item()
|
|
|
|
->limit(1);
|
|
|
|
|
|
|
|
$iio->find();
|
|
|
|
|
2013-11-28 06:41:34 +00:00
|
|
|
$x = (! $iio->loaded() AND $this->date_next_invoice) ? $this->date_next_invoice-86400 : ($iio->total() < 0 ? $iio->date_start-86400 : $iio->date_stop);
|
2013-09-06 05:39:56 +00:00
|
|
|
|
2014-08-25 04:41:07 +00:00
|
|
|
return $format ? Site::Date($x) : $x;
|
2013-09-06 05:39:56 +00:00
|
|
|
}
|
2013-06-05 14:03:55 +00:00
|
|
|
|
2013-09-06 05:39:56 +00:00
|
|
|
private function last_invoice_item() {
|
2013-12-19 23:00:32 +00:00
|
|
|
return ORM::factory('Invoice_Item')
|
|
|
|
->where('item_type','IN',array(0,7))
|
|
|
|
->where('service_id','=',$this)
|
|
|
|
->where('invoice_item.void','IS',NULL)
|
|
|
|
->join('invoice')
|
|
|
|
->on('invoice.id','=','invoice_item.invoice_id')
|
|
|
|
->on('invoice.status','=',1)
|
2013-09-06 05:39:56 +00:00
|
|
|
->order_by('date_stop','DESC');
|
2013-06-05 14:03:55 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 11:23:18 +00:00
|
|
|
/**
|
|
|
|
* Display the service product name
|
|
|
|
*/
|
|
|
|
public function name($variable=NULL) {
|
|
|
|
if (! $variable instanceOf Model_Language)
|
|
|
|
$variable = Company::instance()->language();
|
|
|
|
|
|
|
|
return $this->product->name($variable).(($x=$this->namesub($variable)) ? ': '.$x : '');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function namesub($variable=NULL) {
|
|
|
|
return is_null($plugin=$this->_plugin()) ? '' : $plugin->name($variable);
|
|
|
|
}
|
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
/**
|
2013-06-05 14:03:55 +00:00
|
|
|
* Returns the date that an item has been paid to
|
|
|
|
*/
|
|
|
|
public function paid_to($format=FALSE) {
|
2013-12-19 23:00:32 +00:00
|
|
|
$x = $metric = NULL;
|
2013-06-05 14:03:55 +00:00
|
|
|
|
2013-09-06 05:39:56 +00:00
|
|
|
foreach ($this->last_invoice_item()->order_by('date_orig','DESC')->find_all() as $iio)
|
2013-06-05 14:03:55 +00:00
|
|
|
if ($iio->invoice->due() == 0) {
|
|
|
|
$x = $iio;
|
2013-12-19 23:00:32 +00:00
|
|
|
$metric = ($iio->total() < 0) ? $iio->date_start-86400 : $iio->date_stop;
|
2013-06-05 14:03:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-08-25 04:41:07 +00:00
|
|
|
return $format ? ($x ? Site::Date($metric) : ' ') : ($x ? $metric : NULL);
|
2013-06-05 14:03:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns TRUE of this service has a planned change
|
2013-06-04 11:50:41 +00:00
|
|
|
*/
|
|
|
|
public function pending_change() {
|
|
|
|
return $this->service_change()->loaded() ? TRUE : FALSE;
|
2013-10-10 02:44:53 +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;
|
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
if (! isset(Model_Service::$plugin[$this->id]))
|
|
|
|
Model_Service::$plugin[$this->id] = ORM::factory(Kohana::classname(sprintf('Service_Plugin_%s',$this->product->prod_plugin_file)),array('service_id'=>$this->id));
|
2013-06-04 11:50:41 +00:00
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
return $type ? Model_Service::$plugin[$this->id]->$type : Model_Service::$plugin[$this->id];
|
2013-06-04 11:50:41 +00:00
|
|
|
}
|
|
|
|
|
2013-11-27 00:22:20 +00:00
|
|
|
/**
|
|
|
|
* Enable the plugin to store data
|
|
|
|
*/
|
|
|
|
public function plugin_edit() {
|
|
|
|
return (is_null($x = $this->plugin())) ? NULL : $x->render_edit();
|
|
|
|
}
|
|
|
|
|
2016-08-15 11:23:18 +00:00
|
|
|
public function refnum($short=FALSE) {
|
|
|
|
return ($short ? '' : sprintf('%02s-',Site::id())).sprintf('%05s',$this->id);
|
|
|
|
}
|
|
|
|
|
2013-10-28 23:36:57 +00:00
|
|
|
public function revenue($annual=FALSE) {
|
|
|
|
$multiple = $annual ? Period::multiple($this->recur_schedule) : 1;
|
|
|
|
|
|
|
|
if ($this->suspend_billing)
|
|
|
|
$multiple = 0;
|
|
|
|
|
|
|
|
return $this->price(TRUE)*$multiple;
|
|
|
|
}
|
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
/**
|
|
|
|
* Return the service charge
|
|
|
|
*/
|
2013-11-08 11:02:32 +00:00
|
|
|
public function price($tax=FALSE,$format=FALSE,$original=FALSE) {
|
2013-06-04 11:50:41 +00:00
|
|
|
$x = $this->product->keyget('price_group',$this->recur_schedule);
|
|
|
|
|
|
|
|
// @todo This index shouldnt be hard coded.
|
2013-11-27 00:22:20 +00:00
|
|
|
$p = ! is_null($this->price) ? $this->price : (isset($x[$this->price_group]['price_base']) ? $x[$this->price_group]['price_base'] : NULL);
|
2013-06-04 11:50:41 +00:00
|
|
|
|
2013-11-08 11:02:32 +00:00
|
|
|
if (! $original AND ! is_null($this->price_override))
|
|
|
|
$p = $this->price_override;
|
2013-11-14 11:50:35 +00:00
|
|
|
|
2013-06-04 11:50:41 +00:00
|
|
|
if ($tax)
|
|
|
|
$p = Tax::add($p);
|
|
|
|
|
|
|
|
return $format ? Currency::display($p) : $p;
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
2016-08-10 06:07:00 +00:00
|
|
|
public function service_change() {
|
|
|
|
return $this->service_change->where_active()->where_open()->and_where('complete','!=',1)->or_where('complete','IS',null)->where_close()->find();
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-28 06:41:34 +00:00
|
|
|
public function service_view() {
|
|
|
|
return ! is_object($x=$this->plugin()) ? HTML::nbsp('') : $x->render_view();
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
public function transactions() {
|
2013-11-28 06:41:34 +00:00
|
|
|
return $this->invoice_item->order_by('date_start','ASC')->order_by('product_id','ASC')->order_by('date_stop','ASC');
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** LIST FUNCTIONS **/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for services matching a term
|
|
|
|
*/
|
2016-08-03 04:00:51 +00:00
|
|
|
public function list_autocomplete($term,$index,$value,array $label,array $limit=array(),array $options=array()) {
|
2013-06-05 14:03:55 +00:00
|
|
|
// We only show service numbers.
|
2013-09-06 05:39:56 +00:00
|
|
|
if (! is_numeric($term) AND (! $limit))
|
2013-05-08 09:00:47 +00:00
|
|
|
return array();
|
|
|
|
|
|
|
|
$ao = Auth::instance()->get_user();
|
2013-10-10 02:44:53 +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();
|
2013-10-10 02:44:53 +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)));
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-05-08 09:00:47 +00:00
|
|
|
return parent::list_autocomplete($term,$index,$value,$label,$limit,$options);
|
2013-10-10 02:44:53 +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')
|
2013-10-11 02:08:50 +00:00
|
|
|
->on($this->table_name().'.product_id','=','product.id')
|
2013-06-04 11:50:41 +00:00
|
|
|
->where('prod_plugin_file','=',$plugin)
|
|
|
|
->and_where('service.status','=',TRUE)
|
|
|
|
->find_all();
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
public function list_bylistgroup($cat) {
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach ($this->list_active() as $so)
|
2013-04-26 01:42:09 +00:00
|
|
|
if ($so->product->prod_plugin_file == $cat)
|
2013-10-10 02:44:53 +00:00
|
|
|
array_push($result,$so);
|
|
|
|
|
2016-08-03 04:00:51 +00:00
|
|
|
Sort::MASort($result,array('name()'));
|
2013-10-10 02:44:53 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List services expiring
|
|
|
|
*/
|
|
|
|
public function list_expiring($days=14) {
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach ($this->list_active() as $so)
|
2014-08-26 06:22:31 +00:00
|
|
|
if ($so->expiring($days) AND ! $so->external_billing)
|
2013-10-10 02:44:53 +00:00
|
|
|
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->_where_active()
|
2016-06-05 12:33:12 +00:00
|
|
|
->where_open()
|
|
|
|
->where('suspend_billing','IS',NULL)
|
|
|
|
->or_where('suspend_billing','=','0')
|
|
|
|
->where_close()
|
|
|
|
->where_open()
|
|
|
|
->or_where('external_billing','=','0')
|
|
|
|
->or_where('external_billing','IS',NULL)
|
|
|
|
->where_close()
|
2013-10-10 02:44:53 +00:00
|
|
|
->where('date_next_invoice','<',time()+(ORM::factory('Invoice')->config('GEN_DAYS')+$days)*86400)
|
|
|
|
->find_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|