This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/adsl/classes/Model/Service/Plugin/Adsl.php

469 lines
14 KiB
PHP
Raw Permalink Normal View History

2013-10-10 02:44:53 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports Services
*
* @package ADSL
* @category Models
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Service_Plugin_Adsl extends Model_Service_Plugin {
protected $_table_name = 'service__adsl';
protected $_created_column = FALSE;
2013-10-10 02:44:53 +00:00
protected $_updated_column = FALSE;
// Relationships
protected $_has_one = array(
'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'),
2013-10-10 02:44:53 +00:00
);
protected $_display_filters = array(
'service_connect_date'=>array(
2014-08-25 04:41:07 +00:00
array('Site::Date',array(':value')),
2013-10-10 02:44:53 +00:00
),
'service_contract_date'=>array(
2014-08-25 04:41:07 +00:00
array('Site::Date',array(':value')),
2013-10-10 02:44:53 +00:00
),
);
2013-11-28 06:41:34 +00:00
protected $_save_message = TRUE;
/** REQUIRED ABSTRACT METHODS **/
public function attributes($variable=NULL) {
return array(
'Service Address'=>$this->service_address ? $this->display('service_address') : 'UNKNOWN',
'Contract Until'=>$this->contract_date_end(TRUE),
);
}
2013-11-28 06:41:34 +00:00
2013-10-10 02:44:53 +00:00
public function expire() {
// We'll leave it to the Service record to determine when this service expires
2013-10-10 02:44:53 +00:00
return NULL;
}
2013-11-28 06:41:34 +00:00
public function password() {
return $this->service_password;
}
2013-11-28 06:41:34 +00:00
public function username() {
return $this->service_username;
2013-11-09 04:51:08 +00:00
}
/** LOCAL METHODS **/
2013-10-10 02:44:53 +00:00
/**
* If we override the plan that the customers gets (from what the supplier provides).
2013-10-10 02:44:53 +00:00
*/
public function admin_plan() {
return $this->provided_adsl_plan_id ? $this->provided_plan : $this->service->product->plugin();
2013-10-10 02:44:53 +00:00
}
/**
* Calculate our contract start and end dates
*/
public function contract_date_start($format=FALSE) {
2014-08-25 04:41:07 +00:00
return $format ? Site::Date($this->service_contract_date) : $this->service_contract_date;
2013-10-10 02:44:53 +00:00
}
public function contract_date_end($format=FALSE) {
2013-11-28 06:41:34 +00:00
// ADSL Contract Terms are held in the ADSL Plan
$x = strtotime(sprintf('+%s months',$this->service->plugin()->contract_term),$this->service_contract_date);
2014-08-25 04:41:07 +00:00
return $format ? Site::Date($x) : $x;
2013-10-10 02:44:53 +00:00
}
2013-11-15 05:25:45 +00:00
/**
* This function will take an array of numbers and change it into a cumulative array
*/
private function cumulative($array) {
$result = array();
$s = 0;
foreach ($array as $k => $v)
$result[$k] = ($s += $v);
return $result;
}
2013-10-10 02:44:53 +00:00
/**
* 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());
2013-10-10 02:44:53 +00:00
if (! $x)
return array();
2013-10-10 02:44:53 +00:00
$months = array_combine($x,$x);
2013-10-10 02:44:53 +00:00
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..
2013-10-10 02:44:53 +00:00
*/
public function get_traffic_dailytype($period) {
2013-10-10 02:44:53 +00:00
$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());
}
2013-10-10 02:44:53 +00:00
return $result;
}
/**
* Get monthly traffic data, broken down by type
* @todo This needs to get the plan that was invoiced, not the current plan..
2013-10-10 02:44:53 +00:00
*/
public function get_traffic_monthlytype($period=NULL,$periodstart=NULL,$format=FALSE,$ceil=FALSE) {
2013-10-10 02:44:53 +00:00
$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);
2013-10-10 02:44:53 +00:00
$t = $this->traffic
->select(array('date_format(date,\'%y-%m\')','month'))
->selectsummetric()
->and_where('date','>=',date('Y-m-d',$periodstart))
2013-10-10 02:44:53 +00:00
->and_where('date','<=',date('Y-m-d',strtotime('last day of '.date('M Y',$period))))
->group_by('date_format(date,\'%Y-%m\')');
2013-10-10 02:44:53 +00:00
foreach ($t->find_all() as $to) {
$index = $to->month;
2013-10-10 02:44:53 +00:00
$result[$index] = $this->plan()->allowance($to->traffic_data(),$format,FALSE,$ceil);
2013-10-10 02:44:53 +00:00
}
return $result;
}
/**
* Get traffic type data, broken down by day
2013-10-10 02:44:53 +00:00
*/
public function get_traffic_typedaily($period=NULL) {
2013-10-10 02:44:53 +00:00
$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;
2013-10-10 02:44:53 +00:00
return $result;
}
/**
* Get traffic type data, broken down by month
*/
public function get_traffic_typemonthly($period=NULL,$ceil=FALSE) {
$result = array();
if (is_null($period))
$period = strtotime('yesterday');
foreach ($this->get_traffic_monthlytype($period,NULL,FALSE,$ceil) as $day => $data)
foreach ($data as $metric => $value)
$result[$metric][$day] = $value;
2013-10-10 02:44:53 +00:00
return $result;
}
public function hasOffPeak() {
return $this->plan()->hasOffPeak();
2013-10-10 02:44:53 +00:00
}
/**
* Return the IP Address for the service
*/
public function ipaddress() {
return $this->ipaddress ? $this->ipaddress : _('Dynamic');
2013-10-10 02:44:53 +00:00
}
public function name($variable=NULL) {
return $this->service_number;
}
public function namesub($variable=NULL) {
return $this->display('service_address');
}
/**
* 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);
}
/**
* For a particular month, select the PLAN that the user was on
*/
public function plandate($month) {
throw new Kohana_Exception('This function hasnt been written yet.');
2013-10-10 02:44:53 +00:00
}
2013-11-15 05:25:45 +00:00
/**
* Return the template variables, used mainly in emailing
*
* @param array Variables that need to be expanded
* @param array Data that was previously calculated
*/
public function template_variables(array $array,array $data=array()) {
$result = array();
$friendly = array(
'base_down_peak'=>'Peak',
'base_down_offpeak'=>'OffPeak',
'cumulative_base_down_peak'=>'Total Peak',
'cumulative_base_down_offpeak'=>'Total OffPeak',
);
if (! isset($data['allow']))
$data['allow'] = $this->plan()->allowance(array(),FALSE,TRUE);
if (! isset($data['last']))
$data['last'] = $this->traffic->find_last()->date();
if (! isset($data['used']))
$data['used'] = $this->traffic_month($data['last']);
if (! isset($data['day']))
$data['day'] = date('d',strtotime('yesterday'));
$daysleft = date('d',strtotime('last day of',$data['last']))-$data['day'];
$traffic_type = $this->get_traffic_typedaily($data['last']);
$google = GoogleChart::factory('Legacy')
->type('vertical_bar')
2014-08-25 04:41:07 +00:00
->title(sprintf('DSL traffic usage as at %s',Site::Date($data['last'])));
2013-11-15 05:25:45 +00:00
foreach ($traffic_type as $k => $details)
$google->sdata(array('yl'=>($x=$this->traffic->friendly($k))),array($x=>$details));
// Work out comulative numbers
foreach ($traffic_type as $k => $details)
$google->sdata(array('yr'=>($x='Cumulative '.$this->traffic->friendly($k))),array($x=>$this->cumulative($traffic_type[$k])));
foreach ($array as $item) {
switch ($item) {
case 'MONTH_GRAPH': $value = (string)$google; break;
case 'MONTH_TABLE': $value = $google->table(FALSE,array(
'table'=>'style="border: 1px solid #bebcb7; padding: 5px 5px; background: none repeat scroll 0% 0% #f8f7f5; font-size: 70%;"',
)); break;
case 'OFFPEAK_ALLOWANCE': $value = isset($data['allow']['base_down_offpeak']) ? $data['allow']['base_down_offpeak'].' MB' : '-'; break;
case 'OFFPEAK_USAGE': $value = isset($data['used']['base_down_offpeak']) ? $data['used']['base_down_offpeak'].' MB' : '-'; break;
case 'PEAK_ALLOWANCE': $value = isset($data['allow']['base_down_peak']) ? $data['allow']['base_down_peak'].' MB' : '-'; break;
case 'PEAK_USAGE': $value = isset($data['used']['base_down_peak']) ? $data['used']['base_down_peak'].' MB' : '-'; break;
case 'OFFPEAK_AVERAGE': $value = isset($data['used']['base_down_offpeak']) ? round($data['used']['base_down_offpeak']/$data['day'],2).' MB' : '-'; break;
case 'OFFPEAK_AVERAGE_REMAIN': $value = ((isset($data['used']['base_down_offpeak']) AND ($data['allow']['base_down_offpeak'] > $data['used']['base_down_offpeak']) AND $daysleft) ? round(($data['allow']['base_down_offpeak']-$data['used']['base_down_offpeak'])/$daysleft,2).' MB' : '-'); break;
case 'PEAK_AVERAGE': $value = isset($data['used']['base_down_peak']) ? round($data['used']['base_down_peak']/$data['day'],2).' MB' : '-'; break;
case 'PEAK_AVERAGE_REMAIN': $value = ((isset($data['used']['base_down_peak']) AND ($data['allow']['base_down_peak'] > $data['used']['base_down_peak']) AND $daysleft) ? round(($data['allow']['base_down_peak']-$data['used']['base_down_peak'])/$daysleft,2).' MB' : '-'); break;
case 'SERVICE_NUMBER': $value = $this->service_number; break;
2014-08-25 04:41:07 +00:00
case 'USAGE_DATE': $value = Site::Date($data['last']); break;
2013-11-15 05:25:45 +00:00
case 'USER_NAME': $value = $this->service->account->name(); break;
default:
$value = '';
}
$result[$item] = $value;
}
return $result;
}
/**
* Calculate the Excess Traffic Charges
*/
public function traffic_excess($period=NULL,$charge=FALSE,$format=FALSE) {
2013-10-10 02:44:53 +00:00
$result = array();
if ($this->plan()->extra_charged) {
if (is_null($period))
$period = strtotime('yesterday');
if ($x=$this->get_traffic_monthlytype(strtotime('last day of '.date('M Y',$period)),strtotime('first day of '.date('M Y',$period)))) {
$c = $this->plan()->cost_extra();
foreach ($this->plan()->allowance(array_pop($x),FALSE,TRUE,TRUE) as $k=>$v)
if (isset($c[$k]) AND $v > 0) {
$result[$k] = $charge ? $c[$k]*$v : $v;
if ($charge AND $format)
$result[$k] = Currency::display(Tax::add($result[$k]));
}
2013-10-10 02:44:53 +00:00
}
}
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');
switch ($this->plan()->metric) {
case '1000' : $highchart->ymetric('GB'); break;
case '1' : $highchart->ymetric('MB'); break;
default: $highchart->ymetric('?');
}
$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',Arr::get($_POST,'month')));
$x = $this->get_traffic_typedaily(strtotime(Arr::get($_POST,'month').'-01'));
} else {
$highchart->title(sprintf('Monthly DSL traffic usage as at %s',$this->traffic->find_last()->date));
$x = $this->get_traffic_typemonthly(NULL,TRUE);
}
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,$ceil=FALSE) {
$x = $this->get_traffic_monthlytype(strtotime('last day of '.date('M Y',$period)),strtotime('first day of '.date('M Y',$period)),$format,$ceil);
return $x ? array_pop($x) : 0;
}
2013-11-15 05:25:45 +00:00
/**
* Determine if we alert traffic
*
* We alert traffic if:
* + 80% of usage every 3 days
* + average daily usage > allowance every 5 days
* + last day of the period
*
* @return bool
*/
public function traffic_report() {
$result = array();
$result['day'] = date('d',strtotime('yesterday'));
$result['last'] = $this->traffic->find_last()->date();
if (! Period::inMonth($result['last'],strtotime('yesterday')))
return array();
2013-11-15 05:25:45 +00:00
$lastday = date('d',strtotime('last day of',$result['last']));
$result['allow'] = $this->plan()->allowance(array(),FALSE,TRUE);
$result['used'] = $this->traffic_month($result['last']);
if (! array_sum($result['used']))
2013-11-15 05:25:45 +00:00
return array();
// If we are the last day of the period, and we had traffic
if ($result['day'] == $lastday)
return $result;
foreach ($result['used'] as $k => $v) {
// If we are at 80% usage
// @todo This should be a setup config item
2013-11-15 05:25:45 +00:00
if ($v/($result['allow'][$k] > 0 ? $result['allow'][$k] : 1) >= .8 AND $result['day']%3 == 0)
return $result;
// If our average is greater than our allowance
// @todo This should be a setup config item
2013-11-15 05:25:45 +00:00
if ($result['day']%5 == 0 AND ($v/$result['day'] > $result['allow'][$k]/$result['day']))
return $result;
}
// If we get here, then we dont need to report usage.
return array();
}
/**
* 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(Arr::get($_POST,'month').'-01'));
$index = 'Date';
} else {
$x = $this->get_traffic_monthlytype(NULL,NULL,FALSE,TRUE);
$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
*/
2016-08-03 04:00:51 +00:00
public function list_autocomplete($term,$index,$value,array $label,array $limit=array(),array $options=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('service.active','=',1)
2013-11-15 05:25:45 +00:00
->where_open()
->and_where($this->_table_name.'.service_number','like','%'.$term.'%')
->or_where($this->_table_name.'.service_address','like','%'.$term.'%')
2013-11-28 06:41:34 +00:00
->or_where($this->_table_name.'.ipaddress','like','%'.$term.'%')
2013-11-15 05:25:45 +00:00
->where_close();
return parent::list_autocomplete($term,$index,$value,$label,$limit,$options);
2013-10-10 02:44:53 +00:00
}
}
?>