Overhaul ADSL traffic reporting/graphing
This commit is contained in:
@@ -12,10 +12,20 @@
|
||||
class Model_Product_Plugin_Adsl extends Model_Product_Plugin {
|
||||
protected $_table_name = 'adsl_plan';
|
||||
|
||||
// We dont use the update column
|
||||
protected $_updated_column = FALSE;
|
||||
|
||||
// Relationships
|
||||
protected $_belongs_to = array(
|
||||
'adsl_supplier_plan'=>array('model'=>'ADSL_Supplier_Plan'),
|
||||
'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'),
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters used to format the display of values into friendlier values
|
||||
*/
|
||||
protected $_display_filters = array(
|
||||
'extra_down_peak'=>array(
|
||||
array('Tax::add',array(':value')),
|
||||
@@ -25,45 +35,178 @@ class Model_Product_Plugin_Adsl extends Model_Product_Plugin {
|
||||
array('Tax::add',array(':value')),
|
||||
array('Currency::display',array(':value')),
|
||||
),
|
||||
'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)),
|
||||
),
|
||||
);
|
||||
|
||||
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
|
||||
public function admin_update() {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Our required abstract methods
|
||||
public function cost($annual=FALSE) {
|
||||
$x = $this->adsl_supplier_plan->total();
|
||||
$x = $this->supplier_plan->display('base_cost');
|
||||
|
||||
return $annual ? $x*12 : $x;
|
||||
}
|
||||
|
||||
public function feature_summary() {
|
||||
// @todo This view should render based on the the results of this::allowance();
|
||||
return View::factory('product/plugin/adsl/feature_summary')
|
||||
->set('po',$this);
|
||||
return View::factory(sprintf('product/plugin/%s/feature_summary',$this->plugin()))
|
||||
->set('o',$this);
|
||||
}
|
||||
|
||||
public function supplier() {
|
||||
return $this->adsl_supplier_plan->supplier_id;
|
||||
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
|
||||
* @param bool Display the over alloance numbers
|
||||
* @param int Divide the numbers
|
||||
*/
|
||||
public function allowance(array $data=array(),$format=FALSE,$over=FALSE,$divide=0) {
|
||||
$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;
|
||||
|
||||
if (isset($x[$k]))
|
||||
$result[$k] = $divide ? ceil($x[$k]/$divide) : $x[$k];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the ADSL allowance as a peak/offpeak metric
|
||||
* Get all the products using this plan
|
||||
*/
|
||||
public function allowance($string=TRUE) {
|
||||
$output = ADSL::allowance(array(
|
||||
'base_down_peak'=>$this->base_down_peak,
|
||||
'base_down_offpeak'=>$this->base_down_offpeak,
|
||||
'base_up_peak'=>$this->base_up_peak,
|
||||
'base_up_offpeak'=>$this->base_up_offpeak,
|
||||
'extra_down_peak'=>$this->extra_down_peak,
|
||||
'extra_down_offpeak'=>$this->extra_down_offpeak,
|
||||
'extra_up_peak'=>$this->extra_up_peak,
|
||||
'extra_up_offpeak'=>$this->extra_up_offpeak,
|
||||
));
|
||||
public function products($active=FALSE) {
|
||||
$x = ORM::factory('Product')
|
||||
->where('prod_plugin_file','=','ADSL')
|
||||
->and_where('prod_plugin_data','=',$this);
|
||||
|
||||
return $string ? implode('/',$output) : $output;
|
||||
if ($active)
|
||||
$x->where_active();
|
||||
|
||||
return $x;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
Reference in New Issue
Block a user