2013-10-10 02:44:53 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class supports ADSL products
|
|
|
|
*
|
|
|
|
* @package ADSL
|
|
|
|
* @category Models
|
|
|
|
* @author Deon George
|
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
|
|
|
* @license http://dev.osbill.net/license.html
|
|
|
|
*/
|
|
|
|
class Model_Product_Plugin_Adsl extends Model_Product_Plugin {
|
|
|
|
protected $_table_name = 'adsl_plan';
|
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
// We dont use the update column
|
|
|
|
protected $_updated_column = FALSE;
|
|
|
|
|
|
|
|
// Relationships
|
2013-10-10 02:44:53 +00:00
|
|
|
protected $_belongs_to = array(
|
2013-11-14 11:50:35 +00:00
|
|
|
'supplier_plan'=>array('model'=>'ADSL_Supplier_Plan','foreign_key'=>'adsl_supplier_plan_id'),
|
|
|
|
);
|
|
|
|
protected $_has_many = array(
|
|
|
|
'product'=>array('far_key'=>'id','foreign_key'=>'prod_plugin_data'),
|
2013-10-10 02:44:53 +00:00
|
|
|
);
|
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
/**
|
|
|
|
* Filters used to format the display of values into friendlier values
|
|
|
|
*/
|
2013-10-10 02:44:53 +00:00
|
|
|
protected $_display_filters = array(
|
|
|
|
'extra_down_peak'=>array(
|
|
|
|
array('Tax::add',array(':value')),
|
|
|
|
array('Currency::display',array(':value')),
|
|
|
|
),
|
|
|
|
'extra_down_offpeak'=>array(
|
|
|
|
array('Tax::add',array(':value')),
|
|
|
|
array('Currency::display',array(':value')),
|
|
|
|
),
|
2013-11-14 11:50:35 +00:00
|
|
|
'extra_up_peak'=>array(
|
|
|
|
array('Tax::add',array(':value')),
|
|
|
|
array('Currency::display',array(':value')),
|
|
|
|
),
|
|
|
|
'extra_up_offpeak'=>array(
|
|
|
|
array('Tax::add',array(':value')),
|
|
|
|
array('Currency::display',array(':value')),
|
|
|
|
),
|
|
|
|
'extra_charged'=>array(
|
|
|
|
array('StaticList_YesNo::get',array(':value',TRUE)),
|
|
|
|
),
|
2013-10-10 02:44:53 +00:00
|
|
|
);
|
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
protected $_nullifempty = array(
|
|
|
|
'base_down_peak',
|
|
|
|
'base_down_offpeak',
|
|
|
|
'base_up_peak',
|
|
|
|
'base_up_offpeak',
|
|
|
|
'extra_down_peak',
|
|
|
|
'extra_down_offpeak',
|
|
|
|
'extra_up_peak',
|
|
|
|
'extra_up_offpeak',
|
|
|
|
);
|
|
|
|
|
|
|
|
// Map the table fields
|
|
|
|
private $_map = array(
|
|
|
|
'base_up_offpeak'=>'extra_up_offpeak',
|
|
|
|
'base_down_offpeak'=>'extra_down_offpeak',
|
|
|
|
'base_up_peak'=>'extra_up_peak',
|
|
|
|
'base_down_peak'=>'extra_down_peak',
|
|
|
|
);
|
|
|
|
|
|
|
|
// Our required abstract methods
|
2013-10-10 02:44:53 +00:00
|
|
|
public function admin_update() {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2013-10-28 23:36:57 +00:00
|
|
|
public function cost($annual=FALSE) {
|
2013-11-14 11:50:35 +00:00
|
|
|
$x = $this->supplier_plan->display('base_cost');
|
2013-10-28 23:36:57 +00:00
|
|
|
|
|
|
|
return $annual ? $x*12 : $x;
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
public function feature_summary() {
|
2013-11-14 11:50:35 +00:00
|
|
|
return View::factory(sprintf('product/plugin/%s/feature_summary',$this->plugin()))
|
|
|
|
->set('o',$this);
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 23:36:57 +00:00
|
|
|
public function supplier() {
|
2013-11-14 11:50:35 +00:00
|
|
|
return $this->supplier_plan->supplier_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** LOCAL FUNCTIONS **/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the allowance array or traffic used array
|
|
|
|
*
|
|
|
|
* If:
|
|
|
|
*
|
|
|
|
* + UPLOADS are charged and there are no PEAK/OFFPEAK periods (therefore all
|
|
|
|
* traffic is charged), the allowance will be shown as 1 metric - TRAFFIC.
|
|
|
|
* + UPLOADS are charged and there are PEAK/OFFPEAK periods the allowance
|
|
|
|
* will be shown as 2 metrics - PEAK/OFFPEAK.
|
|
|
|
* + UPLOADS are NOT charged and there are no PEAK/OFFPEAK periods the allowance
|
|
|
|
* will be shown as 1 metrics - TRAFFIC.
|
|
|
|
* + UPLOADS are NOT charged and there are PEAK/OFFPEAK periods the allowance
|
|
|
|
* will be shown as 2 metrics - PEAK/OFFPEAK.
|
|
|
|
*
|
|
|
|
* Thus:
|
|
|
|
*
|
|
|
|
* + If base_x_Y is NULL, all Y traffic is FREE (ignore respective extra_x_Y setting).
|
|
|
|
* + If base_x_Y is a number, all Y traffic is FREE up to the number (evaluate extra_x_Y setting).
|
|
|
|
* + If extra_x_Y is a number, charge this amount for traffic over base_x_Y.
|
|
|
|
* + If extra_down_peak is NULL this is invalid, treat base_down_peak as NULL
|
|
|
|
* + If extra_down_offpeak is NULL add traffic_down_offpeak to traffic_down_peak
|
|
|
|
* + If extra_up_peak is NULL add traffic_up_peak to traffic_down_peak
|
|
|
|
* + If extra_up_offpeak is NULL add traffic_up_offpeak to traffic_down_offpeak
|
|
|
|
*
|
|
|
|
* @param array Traffic Used in each metric.
|
|
|
|
* @param bool Whether the output should be a string
|
2013-11-15 05:25:45 +00:00
|
|
|
* @param bool Display the over allowance numbers
|
2013-11-14 11:50:35 +00:00
|
|
|
* @param int Divide the numbers
|
|
|
|
*/
|
2013-11-18 04:18:50 +00:00
|
|
|
public function allowance(array $data=array(),$format=FALSE,$over=FALSE,$ceil=FALSE) {
|
2013-11-14 11:50:35 +00:00
|
|
|
$result = $x = array();
|
|
|
|
|
|
|
|
// Do we invert the result - showing allowance
|
|
|
|
$invert = $data ? FALSE : TRUE;
|
|
|
|
|
|
|
|
// Map the NULL relationships
|
|
|
|
$merge = array(
|
|
|
|
'extra_up_offpeak'=>'base_down_offpeak',
|
|
|
|
'extra_down_offpeak'=>'base_down_peak',
|
|
|
|
'extra_up_peak'=>'base_down_peak',
|
|
|
|
'extra_down_peak'=>'base_down_peak',
|
|
|
|
);
|
|
|
|
|
|
|
|
// Work out if we charge each period
|
|
|
|
foreach ($this->_map as $k => $v) {
|
|
|
|
// Anything NULL is not counted
|
|
|
|
if (is_null($this->{$k}))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (! isset($data[$k]))
|
|
|
|
$data[$k] = 0;
|
|
|
|
|
|
|
|
if ($over)
|
|
|
|
$data[$k] = $invert ? $this->{$k} : $data[$k]-$this->{$k};
|
|
|
|
|
|
|
|
// Do we charge for extra, or merge it to another field
|
|
|
|
if (! is_null($this->{$v})) {
|
|
|
|
if (isset($x[$k]))
|
|
|
|
$x[$k] += $data[$k];
|
|
|
|
else
|
|
|
|
$x[$k] = $data[$k];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (isset($x[$merge[$v]])) {
|
|
|
|
$x[$merge[$v]] += $data[$k];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$x[$merge[$v]] = $data[$k];
|
|
|
|
|
|
|
|
// If we had any data already in A for this merge, we'll need to add it too.
|
|
|
|
if (isset($x[$k])) {
|
|
|
|
$x[$merge[$v]] += $x[$k];
|
|
|
|
|
|
|
|
unset($x[$k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the output sorted
|
|
|
|
foreach (array_keys(Model_Service_Plugin_Adsl_Traffic::$metrics) as $k) {
|
|
|
|
$k = 'base_'.$k;
|
|
|
|
|
2013-11-18 04:18:50 +00:00
|
|
|
if (isset($x[$k])) {
|
|
|
|
$result[$k] = $this->metric ? round($x[$k]/$this->metric,2) : $x[$k];
|
|
|
|
|
|
|
|
if ($ceil)
|
|
|
|
$result[$k] = ceil($result[$k]);
|
|
|
|
}
|
2013-11-14 11:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $format ? join('/',array_values($result)) : $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cost_extra(array $data=array(),$format=FALSE) {
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach ($this->allowance($data) as $k => $v)
|
|
|
|
$result[$k] = $format ? $this->display($this->_map[$k]) : $this->{$this->_map[$k]};
|
|
|
|
|
|
|
|
return $format ? join('/',array_values($result)) : $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasOffpeak() {
|
|
|
|
if ((($this->base_down_offpeak || $this->base_down_offpeak == 0) AND (! is_null($this->extra_down_offpeak)))
|
|
|
|
OR (($this->base_up_offpeak || $this->base_up_offpeak == 0) AND (! is_null($this->extra_up_offpeak))))
|
|
|
|
|
|
|
|
return TRUE;
|
2013-10-28 23:36:57 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
/**
|
2013-11-14 11:50:35 +00:00
|
|
|
* Get all the products using this plan
|
2013-10-10 02:44:53 +00:00
|
|
|
*/
|
2013-11-14 11:50:35 +00:00
|
|
|
public function products($active=FALSE) {
|
|
|
|
$x = ORM::factory('Product')
|
|
|
|
->where('prod_plugin_file','=','ADSL')
|
|
|
|
->and_where('prod_plugin_data','=',$this);
|
|
|
|
|
|
|
|
if ($active)
|
|
|
|
$x->where_active();
|
|
|
|
|
|
|
|
return $x;
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|