Overhaul ADSL traffic reporting/graphing
This commit is contained in:
@@ -19,7 +19,11 @@ class Model_Service_Plugin_Adsl extends Model_Service_Plugin {
|
||||
);
|
||||
|
||||
protected $_has_one = array(
|
||||
'adsl_plan'=>array('model'=>'ADSL_Plan','far_key'=>'provided_adsl_plan_id','foreign_key'=>'id'),
|
||||
'provided_plan'=>array('model'=>'Product_Plugin_Adsl','far_key'=>'provided_adsl_plan_id','foreign_key'=>'id'),
|
||||
);
|
||||
|
||||
protected $_has_many = array(
|
||||
'traffic'=>array('model'=>'Service_Plugin_Adsl_Traffic','foreign_key'=>'service','far_key'=>'service_username'),
|
||||
);
|
||||
|
||||
protected $_display_filters = array(
|
||||
@@ -33,7 +37,7 @@ class Model_Service_Plugin_Adsl extends Model_Service_Plugin {
|
||||
|
||||
// Required abstract functions
|
||||
public function expire() {
|
||||
// @todo This should work out if the invoices are currently due, then the expire is the invoice date, otherwise the next invoice date.
|
||||
// We'll leave it to the Service record to determine when this service expires
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -41,6 +45,15 @@ class Model_Service_Plugin_Adsl extends Model_Service_Plugin {
|
||||
return $this->service_number;
|
||||
}
|
||||
|
||||
public function username_value() {
|
||||
return $this->service_username;
|
||||
}
|
||||
|
||||
public function password_value() {
|
||||
return $this->service_password;
|
||||
}
|
||||
|
||||
// Override our parent function to include some JS.
|
||||
public function admin_update() {
|
||||
Script::factory()
|
||||
->type('stdin')
|
||||
@@ -68,24 +81,130 @@ $(document).ready(function() {
|
||||
return parent::admin_update();
|
||||
}
|
||||
|
||||
public function product() {
|
||||
if ($this->provided_adsl_plan_id)
|
||||
return $this->adsl_plan;
|
||||
else
|
||||
return $this->service->product->plugin();
|
||||
/** LOCAL FUNCTIONS **/
|
||||
|
||||
/**
|
||||
* If we override the plan that the customers gets (from what the supplier provides).
|
||||
*/
|
||||
public function admin_plan() {
|
||||
return $this->provided_adsl_plan_id ? $this->provided_plan : $this->service->product->plugin();
|
||||
}
|
||||
|
||||
public function service_view() {
|
||||
return View::factory('service/user/plugin/adsl/view')
|
||||
->set('o',$this);
|
||||
/**
|
||||
* Calculate our contract start and end dates
|
||||
*/
|
||||
public function contract_date_start($format=FALSE) {
|
||||
return $format ? Config::date($this->service_contract_date) : $this->service_contract_date;
|
||||
}
|
||||
|
||||
public function username_value() {
|
||||
return $this->service_username;
|
||||
public function contract_date_end($format=FALSE) {
|
||||
$x = strtotime(sprintf('+%s months',$this->contract_term),$this->service_contract_date);
|
||||
|
||||
return $format ? Config::date($x) : $x;
|
||||
}
|
||||
|
||||
public function password_value() {
|
||||
return $this->service_password;
|
||||
/**
|
||||
* This function will return the months that have traffic data.
|
||||
* This array can be used in a select list to display the traffic for that month
|
||||
*/
|
||||
public function get_traffic_months() {
|
||||
$x = array_keys($this->get_traffic_monthlytype());
|
||||
|
||||
if (! $x)
|
||||
return array();
|
||||
|
||||
$months = array_combine($x,$x);
|
||||
|
||||
arsort($months);
|
||||
|
||||
return $months;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get daily traffic data, broken down by type
|
||||
* @todo This needs to get the plan that was invoiced, not the current plan..
|
||||
*/
|
||||
public function get_traffic_dailytype($period) {
|
||||
$result = array();
|
||||
|
||||
if (is_null($period))
|
||||
$period = strtotime('yesterday');
|
||||
|
||||
$t = $this->traffic
|
||||
->where('date','>=',date('Y-m-d',mktime(0,0,0,date('m',$period),1,date('Y',$period))))
|
||||
->and_where('date','<=',date('Y-m-d',strtotime('last day of '.date('M Y',$period))));
|
||||
|
||||
foreach ($t->find_all() as $to) {
|
||||
$day = date('j',strtotime($to->date));
|
||||
|
||||
$result[$day] = $this->plan()->allowance($to->traffic_data());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get monthly traffic data, broken down by type
|
||||
* @todo This needs to get the plan that was invoiced, not the current plan..
|
||||
*/
|
||||
public function get_traffic_monthlytype($period=NULL,$periodstart=NULL,$format=FALSE,$divide=0) {
|
||||
$result = array();
|
||||
|
||||
if (is_null($period))
|
||||
$period = strtotime('yesterday');
|
||||
if (is_null($periodstart))
|
||||
$periodstart = mktime(0,0,0,date('m',$period),1,date('Y',$period)-1);
|
||||
|
||||
$t = $this->traffic
|
||||
->select(array('date_format(date,\'%y-%m\')','month'))
|
||||
->selectsummetric()
|
||||
->and_where('date','>=',date('Y-m-d',$periodstart))
|
||||
->and_where('date','<=',date('Y-m-d',strtotime('last day of '.date('M Y',$period))))
|
||||
->group_by('date_format(date,\'%Y-%m\')');
|
||||
|
||||
foreach ($t->find_all() as $to) {
|
||||
$index = $to->month;
|
||||
|
||||
$result[$index] = $this->plan()->allowance($to->traffic_data(),$format,FALSE,$divide);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get traffic type data, broken down by day
|
||||
*/
|
||||
public function get_traffic_typedaily($period=NULL) {
|
||||
$result = array();
|
||||
|
||||
if (is_null($period))
|
||||
$period = strtotime('yesterday');
|
||||
|
||||
foreach ($this->get_traffic_dailytype($period) as $day => $data)
|
||||
foreach ($data as $metric => $value)
|
||||
$result[$metric][$day] = $value;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get traffic type data, broken down by month
|
||||
*/
|
||||
public function get_traffic_typemonthly($period=NULL) {
|
||||
$result = array();
|
||||
|
||||
if (is_null($period))
|
||||
$period = strtotime('yesterday');
|
||||
|
||||
foreach ($this->get_traffic_monthlytype($period) as $day => $data)
|
||||
foreach ($data as $metric => $value)
|
||||
$result[$metric][$day] = $value;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function hasOffPeak() {
|
||||
return $this->plan()->hasOffPeak();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,147 +214,130 @@ $(document).ready(function() {
|
||||
return $this->ipaddress ? $this->ipaddress : _('Dynamic');
|
||||
}
|
||||
|
||||
public function contract_date_start() {
|
||||
return Config::date($this->service_contract_date);
|
||||
}
|
||||
|
||||
public function contract_date_end($format=FALSE) {
|
||||
$x = strtotime(sprintf('+%s months',$this->contract_term),$this->service_contract_date);
|
||||
|
||||
return $format ? Config::date($x) : $x;
|
||||
}
|
||||
|
||||
public function hasOffpeak() {
|
||||
return ((is_null($this->product()->base_down_offpeak) OR $this->product()->base_down_offpeak) AND (is_null($this->product()->base_up_offpeak) OR $this->product()->base_up_offpeak)) ? TRUE : FALSE;
|
||||
/**
|
||||
* If we override the plan that the customers gets (from what the supplier provides).
|
||||
* @todo This needs to get the plan that was invoiced, not the current plan..
|
||||
*/
|
||||
public function plan($month=NULL) {
|
||||
return is_null($month) ? $this->service->product->plugin() : $this->plandate($month);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will return the months that have traffic data.
|
||||
* This array can be used in a select list to display the traffic for that month
|
||||
* For a particular month, select the PLAN that the user was on
|
||||
*/
|
||||
public function get_traffic_months() {
|
||||
$keys = $months = array();
|
||||
|
||||
foreach ($this->get_traffic_data_monthly() as $metric => $data)
|
||||
$keys = array_unique(array_merge($keys,array_keys($data)));
|
||||
|
||||
foreach ($keys as $v)
|
||||
$months[$v] = $v;
|
||||
|
||||
arsort($months);
|
||||
|
||||
return $months;
|
||||
public function plandate($month) {
|
||||
throw new Kohana_Exception('This function hasnt been written yet.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the total traffic used in a month
|
||||
* Calculate the Excess Traffic Charges
|
||||
*/
|
||||
private function get_traffic_data_month($period=NULL,$cache=0) {
|
||||
public function traffic_excess($period=NULL,$charge=FALSE,$format=FALSE) {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->get_traffic_data_daily($period,TRUE,$cache) as $tdata)
|
||||
foreach ($tdata as $k => $v)
|
||||
if (isset($result[$k]))
|
||||
$result[$k] += $v;
|
||||
else
|
||||
$result[$k] = $v;
|
||||
if ($this->plan()->extra_charged) {
|
||||
if (is_null($period))
|
||||
$period = strtotime('yesterday');
|
||||
|
||||
return $result;
|
||||
}
|
||||
if ($x=$this->get_traffic_monthlytype(strtotime('last day of '.date('M Y',$period)),strtotime('first day of '.date('M Y',$period)))) {
|
||||
|
||||
/**
|
||||
* Return an array of the data used in a month by day
|
||||
*/
|
||||
public function get_traffic_data_daily($period=NULL,$bydate=FALSE,$cache=0) {
|
||||
$result = array();
|
||||
$c = $this->plan()->cost_extra();
|
||||
foreach ($this->plan()->allowance(array_pop($x),FALSE,TRUE,1000) as $k=>$v)
|
||||
if (isset($c[$k]) AND $v > 0) {
|
||||
$result[$k] = $charge ? $c[$k]*$v : $v;
|
||||
|
||||
if (is_null($period))
|
||||
$period = strtotime('yesterday');
|
||||
|
||||
$t = ORM::factory('Service_Plugin_Adsl_Traffic')
|
||||
->where('service','=',$this->service_username)
|
||||
->and_where('date','>=',date('Y-m-d',mktime(0,0,0,date('m',$period),1,date('Y',$period))))
|
||||
->and_where('date','<=',date('Y-m-d',strtotime('last day of '.date('M Y',$period))))
|
||||
->cached($cache);
|
||||
|
||||
foreach ($t->find_all() as $to) {
|
||||
$day = date('d',strtotime($to->date));
|
||||
|
||||
if ($bydate)
|
||||
$result[$day] = $to->traffic($this->service->product->plugin());
|
||||
else
|
||||
foreach ($to->traffic($this->service->product->plugin()) as $k => $v)
|
||||
$result[$k][$day] = $v;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of the data used in a year by month
|
||||
*/
|
||||
public function get_traffic_data_monthly($period=NULL,$bydate=FALSE) {
|
||||
$result = array();
|
||||
|
||||
if (is_null($period))
|
||||
$period = strtotime('yesterday');
|
||||
|
||||
$t = ORM::factory('Service_Plugin_Adsl_Traffic')
|
||||
->select(
|
||||
array('date_format(date,\'%y-%m\')','month'),
|
||||
array('sum(up_peak)','up_peak'),
|
||||
array('sum(up_offpeak)','up_offpeak'),
|
||||
array('sum(down_peak)','down_peak'),
|
||||
array('sum(down_offpeak)','down_offpeak')
|
||||
)
|
||||
->where('service','=',$this->service_username)
|
||||
->and_where('date','>=',date('Y-m-d',mktime(0,0,0,date('m',$period),1,date('Y',$period)-1)))
|
||||
->and_where('date','<=',date('Y-m-d',strtotime('last day of '.date('M Y',$period))))
|
||||
->group_by('date_format(date,\'%Y-%m\')');
|
||||
|
||||
foreach ($t->find_all() as $to)
|
||||
if ($bydate)
|
||||
$result[$to->month] = $to->traffic($this->service->product->plugin());
|
||||
else
|
||||
foreach ($to->traffic($this->service->product->plugin()) as $k => $v)
|
||||
$result[$k][$to->month] = $v;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function traffic_month($month,$string=TRUE,$cache=0) {
|
||||
return $string ? implode('/',$this->get_traffic_data_month($month,$cache)) :
|
||||
$this->get_traffic_data_month($month,$cache);
|
||||
}
|
||||
|
||||
public function traffic_lastmonth($string=TRUE) {
|
||||
// We need it to be last month as of yesterday
|
||||
return $this->traffic_month(strtotime('last month')-86400,$string);
|
||||
}
|
||||
|
||||
public function traffic_thismonth($string=TRUE) {
|
||||
return $this->traffic_month(strtotime('yesterday'),$string);
|
||||
}
|
||||
|
||||
public function traffic_lastmonth_exceed($all=FALSE,$date=NULL) {
|
||||
$result = array();
|
||||
|
||||
if (is_null($date))
|
||||
$date = strtotime('last month')-86400;
|
||||
|
||||
foreach ($this->traffic_month($date,FALSE,0) as $k => $v) {
|
||||
// We shouldnt need to eval for nulls, since the traffic calc does that
|
||||
if ($all OR ($v > $this->service->product->plugin()->$k)) {
|
||||
$result[$k]['allowance'] = $this->service->product->plugin()->$k;
|
||||
$result[$k]['used'] = $v;
|
||||
$result[$k]['shaped'] = (! empty($this->service->product->plugin()->extra_shaped) AND $this->service->product->plugin()->extra_shaped AND $v > $this->service->product->plugin()->$k) ? TRUE : FALSE;
|
||||
$result[$k]['excess'] = (! empty($this->service->product->plugin()->extra_charged) AND $this->service->product->plugin()->extra_charged AND $v > $this->service->product->plugin()->$k) ? $v-$this->service->product->plugin()->$k : 0;
|
||||
$result[$k]['rate'] = $this->service->product->plugin()->{ADSL::map($k)};
|
||||
$result[$k]['charge'] = ceil(($result[$k]['excess'])/1000)*$result[$k]['rate'];
|
||||
if ($charge AND $format)
|
||||
$result[$k] = Currency::display(Tax::add($result[$k]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
if (! $result AND $charge AND $format)
|
||||
$result = array(Currency::display(0));
|
||||
|
||||
return $format ? join('/',$result) : $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a chart of traffic
|
||||
*/
|
||||
public function traffic_graph($month=NULL) {
|
||||
$highchart = HighChart::factory('Combo');
|
||||
|
||||
$c=0;
|
||||
// If we came in via a post to show a particular month, then show that, otherwise show the yearly result
|
||||
if (! is_null($month) AND trim($month)) {
|
||||
$highchart->title(sprintf('DSL traffic usage for %s',$_POST['month']));
|
||||
$x = $this->get_traffic_typedaily(strtotime($_POST['month'].'-01'));
|
||||
|
||||
} else {
|
||||
$highchart->title(sprintf('Monthly DSL traffic usage as at %s',$this->traffic->find_last()->date));
|
||||
$x = $this->get_traffic_typemonthly();
|
||||
}
|
||||
|
||||
foreach ($x as $k => $details) {
|
||||
$highchart->series('column','yl')
|
||||
->name($this->traffic->friendly($k))
|
||||
->data($x[$k])
|
||||
->index($c)
|
||||
->order($c++*-1); // This is a kludge to get around highcharts rendering from the bottom up.
|
||||
|
||||
$highchart->autopie($k);
|
||||
}
|
||||
|
||||
|
||||
return (string)$highchart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the traffic for a month
|
||||
*/
|
||||
public function traffic_month($period,$format=FALSE,$divide=0) {
|
||||
$x = $this->get_traffic_monthlytype(strtotime('last day of '.date('M Y',$period)),strtotime('first day of '.date('M Y',$period)),$format,$divide);
|
||||
|
||||
return $x ? array_pop($x) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a table of traffic
|
||||
*/
|
||||
public function traffic_table($month=NULL) {
|
||||
// If we came in via a post to show a particular month, then show that, otherwise show the yearly result
|
||||
if (! is_null($month) AND trim($month)) {
|
||||
$x = $this->get_traffic_dailytype(strtotime($_POST['month'].'-01'));
|
||||
$index = 'Date';
|
||||
|
||||
} else {
|
||||
$x = $this->get_traffic_monthlytype();
|
||||
$index = 'Month';
|
||||
}
|
||||
|
||||
return View::factory(sprintf('service/user/plugin/%s/table_traffic',$this->plugin()))
|
||||
->set('index',$index)
|
||||
->set('th',array_keys($this->plan()->allowance()))
|
||||
->set('td',$x)
|
||||
->set('o',$this->traffic);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for services matching a term
|
||||
*/
|
||||
public function list_autocomplete($term,$index,$value,array $label,array $limit=array(),array $options=NULL) {
|
||||
// We only show service numbers.
|
||||
if (! is_numeric($term))
|
||||
return array();
|
||||
|
||||
$ao = Auth::instance()->get_user();
|
||||
|
||||
$options['key'] = 'id';
|
||||
$options['object'] = DB::select($this->_table_name.'.id',$this->_table_name.'.service_number')
|
||||
->from($this->_table_name)
|
||||
->join('service')
|
||||
->on('service.id','=',$this->_table_name.'.service_id')
|
||||
->where('service.account_id','IN',$ao->RTM->customers($ao->RTM))
|
||||
->and_where($this->_table_name.'.service_number','like','%'.$term.'%');
|
||||
|
||||
return parent::list_autocomplete($term,$index,$value,$label,$limit,$options);
|
||||
}
|
||||
|
||||
public function template_variables($array) {
|
||||
@@ -307,7 +409,7 @@ $(document).ready(function() {
|
||||
/**
|
||||
* This function will take an array of numbers and change it into a cumulative array
|
||||
*/
|
||||
public function cumulative($array) {
|
||||
private function cumulative($array) {
|
||||
$result = array();
|
||||
$s = 0;
|
||||
|
||||
@@ -330,7 +432,6 @@ $(document).ready(function() {
|
||||
throw new Kohana_Exception('Huh? How did this get called, for a non ADSL product (:ppf)',array(':ppf'=>$this->service_id));
|
||||
|
||||
$allowance = $this->service->product->plugin()->allowance(FALSE);
|
||||
|
||||
$period = strtotime('yesterday');
|
||||
$traffic_data = $this->get_traffic_data_daily($period,FALSE);
|
||||
$traffic = $this->get_traffic_data_month($period);
|
||||
@@ -379,59 +480,45 @@ $(document).ready(function() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a google chart of traffic
|
||||
* Return an array of the data used in a month by day
|
||||
*/
|
||||
public function graph_traffic($month=NULL) {
|
||||
$google = GoogleChart::factory('Legacy')
|
||||
->type('vertical_bar');
|
||||
private function get_traffic_data_daily($period=NULL,$bydate=FALSE) {
|
||||
$result = array();
|
||||
|
||||
// If we came in via a post to show a particular month, then show that, otherwise show the yearly result
|
||||
if (! is_null($month) AND trim($month)) {
|
||||
$google->title(sprintf('DSL traffic usage for %s',$_POST['month']));
|
||||
$traffic_data = $this->get_traffic_data_daily(strtotime($_POST['month'].'-01'));
|
||||
if (is_null($period))
|
||||
$period = strtotime('yesterday');
|
||||
|
||||
foreach ($traffic_data as $k => $details)
|
||||
$google->sdata(array('yl'=>($x=isset($friendly[$k]) ? $friendly[$k] : $k)),array($x=>$traffic_data[$k]));
|
||||
$t = ORM::factory('Service_Plugin_Adsl_Traffic')
|
||||
->where('service','=',$this->service_username)
|
||||
->and_where('date','>=',date('Y-m-d',mktime(0,0,0,date('m',$period),1,date('Y',$period))));
|
||||
|
||||
foreach ($traffic_data as $k => $details)
|
||||
$google->sdata(array('yr'=>($x=isset($friendly['cumulative'.$k]) ? $friendly['cumulative'.$k] : 'cumulative'.$k)),array($x=>$this->cumulative($traffic_data[$k])));
|
||||
foreach ($t->find_all() as $to) {
|
||||
$day = (string)date('d',strtotime($to->date));
|
||||
|
||||
} else {
|
||||
// @todo Change the date to the last record date
|
||||
$google->title(sprintf('Monthly DSL traffic usage as at %s',Config::date(strtotime('yesterday'))));
|
||||
$traffic_data = $this->get_traffic_data_monthly();
|
||||
|
||||
foreach ($traffic_data as $k => $details)
|
||||
$google->sdata(array('yl'=>($x=isset($friendly[$k]) ? $friendly[$k] : $k)),array($x=>$traffic_data[$k]));
|
||||
if ($bydate)
|
||||
$result[$day] = $to->traffic($this->service->product->plugin());
|
||||
else
|
||||
foreach ($to->traffic($this->service->product->plugin()) as $k => $v)
|
||||
$result[$k][$day] = (int)$v;
|
||||
}
|
||||
|
||||
return (string)$google;
|
||||
}
|
||||
|
||||
public function table_traffic($month=NULL) {
|
||||
return View::factory('service/user/plugin/adsl/table_traffic')
|
||||
->set('traffic',$this->traffic_month((! is_null($month) AND trim($month)) ? strtotime($month.'-01') : NULL,FALSE));
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for services matching a term
|
||||
* Calculate the total traffic used in a month
|
||||
*/
|
||||
public function list_autocomplete($term,$index,$value,array $label,array $limit=array(),array $options=NULL) {
|
||||
// We only show invoice numbers.
|
||||
if (! is_numeric($term))
|
||||
return array();
|
||||
private function get_traffic_data_month($period=NULL,$cache=0) {
|
||||
$result = array();
|
||||
|
||||
$ao = Auth::instance()->get_user();
|
||||
foreach ($this->get_traffic_data_daily($period,TRUE,$cache) as $tdata)
|
||||
foreach ($tdata as $k => $v)
|
||||
if (isset($result[$k]))
|
||||
$result[$k] += $v;
|
||||
else
|
||||
$result[$k] = $v;
|
||||
|
||||
$options['key'] = 'id';
|
||||
$options['object'] = DB::select($this->_table_name.'.id',$this->_table_name.'.service_number')
|
||||
->from($this->_table_name)
|
||||
->join('service')
|
||||
->on('service.id','=',$this->_table_name.'.service_id')
|
||||
->where('service.account_id','IN',$ao->RTM->customers($ao->RTM))
|
||||
->and_where($this->_table_name.'.service_number','like','%'.$term.'%');
|
||||
|
||||
return parent::list_autocomplete($term,$index,$value,$label,$limit,$options);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@@ -21,13 +21,22 @@ class Model_Service_Plugin_Adsl_Traffic extends ORM_OSB {
|
||||
'plan'=>array('model'=>'Service_Plugin_Adsl','foreign_key'=>'service_username','far_key'=>'service'),
|
||||
);
|
||||
|
||||
private $metrics = array(
|
||||
'up_peak',
|
||||
'up_offpeak',
|
||||
'down_peak',
|
||||
'down_offpeak',
|
||||
'peer',
|
||||
'internal',
|
||||
public static $metrics = array(
|
||||
'down_peak'=>'base_down_peak',
|
||||
'down_offpeak'=>'base_down_offpeak',
|
||||
'up_peak'=>'base_up_peak',
|
||||
'up_offpeak'=>'base_up_offpeak',
|
||||
'peer'=>'base_down_peak',
|
||||
'internal'=>'base_down_offpeak',
|
||||
);
|
||||
|
||||
private $_friendly = array(
|
||||
'base_up_peak'=>'UP Peak',
|
||||
'base_up_offpeak'=>'UP Offpeak',
|
||||
'base_down_peak'=>'DOWN Peak',
|
||||
'base_down_offpeak'=>'DOWN Offpeak',
|
||||
'base_peer'=>'Peer',
|
||||
'base_internal'=>'Internal',
|
||||
);
|
||||
|
||||
public function rules() {
|
||||
@@ -39,12 +48,16 @@ class Model_Service_Plugin_Adsl_Traffic extends ORM_OSB {
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function friendly($name) {
|
||||
return isset($this->_friendly[$name]) ? $this->_friendly[$name] : $name;
|
||||
}
|
||||
|
||||
public function save(Validation $validation = NULL) {
|
||||
// If all our values are zero/NULL, we'll ignore if our previous one was.
|
||||
if ($this->total() !== 0)
|
||||
return parent::save($validation);
|
||||
|
||||
$l = ORM::factory($this->_object_name)->where('service','=',$this->service)->where('supplier_id','=',$this->supplier_id)->list_last();
|
||||
$l = ORM::factory($this->_object_name)->where('service','=',$this->service)->where('supplier_id','=',$this->supplier_id)->find_last();
|
||||
|
||||
if ($l->total() !== 0)
|
||||
return parent::save($validation);
|
||||
@@ -57,10 +70,23 @@ class Model_Service_Plugin_Adsl_Traffic extends ORM_OSB {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the select statements to SUM up the traffic metrics that we use
|
||||
*/
|
||||
public function selectsummetric() {
|
||||
foreach (Model_Service_Plugin_Adsl_Traffic::$metrics as $metric=>$v)
|
||||
$this->select(array("sum(".$metric.")",$metric));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Total up the traffic into the metrics we use
|
||||
*/
|
||||
public function total() {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->metrics as $metric) {
|
||||
foreach (array_keys(Model_Service_Plugin_Adsl_Traffic::$metrics) as $metric) {
|
||||
if (is_null($this->$metric))
|
||||
$this->$metric = 0;
|
||||
|
||||
@@ -70,21 +96,26 @@ class Model_Service_Plugin_Adsl_Traffic extends ORM_OSB {
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function traffic(Model_Product_Plugin_Adsl $plan) {
|
||||
// Roll up the charges according to the product plan configuration
|
||||
return ADSL::allowance(array(
|
||||
'base_down_peak'=>is_null($plan->base_down_peak) ? NULL : $this->down_peak,
|
||||
'base_down_offpeak'=>is_null($plan->base_down_offpeak) ? NULL : $this->down_offpeak,
|
||||
'base_up_peak'=>is_null($plan->base_up_peak) ? NULL : $this->up_peak,
|
||||
'base_up_offpeak'=>is_null($plan->base_up_offpeak) ? NULL : $this->up_offpeak,
|
||||
'extra_down_peak'=>$plan->extra_down_peak,
|
||||
'extra_down_offpeak'=>$plan->extra_down_offpeak,
|
||||
'extra_up_peak'=>$plan->extra_up_peak,
|
||||
'extra_up_offpeak'=>$plan->extra_up_offpeak,
|
||||
));
|
||||
/**
|
||||
* Collapse our traffic data into an array as per $this->_metric
|
||||
*/
|
||||
public function traffic_data() {
|
||||
$result = array();
|
||||
|
||||
foreach (Model_Service_Plugin_Adsl_Traffic::$metrics as $metric=>$v) {
|
||||
if (! isset($result[$v]))
|
||||
$result[$v] = 0;
|
||||
|
||||
$result[$v] += $this->{$metric};
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function list_last() {
|
||||
/**
|
||||
* Find the last traffic record
|
||||
*/
|
||||
public function find_last() {
|
||||
return $this->order_by('date','DESC')->order_by('time','DESC')->find();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user