array(), ); 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'), ); protected $_display_filters = array( 'service_connect_date'=>array( array('Config::date',array(':value')), ), 'service_contract_date'=>array( array('Config::date',array(':value')), ), ); // Required abstract functions public function expire() { // We'll leave it to the Service record to determine when this service expires return NULL; } public function name() { 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') ->data(' $(document).ready(function() { $("#service_connect_date_label").datepicker({ autoclose : true, startDate : now, format : "dd-M-yyyy", todayBtn : true, }).on("hide",function(ev) { $("input[id=service_connect_date]").val(ev.date.valueOf()/1000); }); $("#service_contract_date_label").datepicker({ autoclose : true, startDate : now, format : "dd-M-yyyy", todayBtn : true, }).on("hide",function(ev) { $("input[id=service_contract_date]").val(ev.date.valueOf()/1000); }); }); '); return parent::admin_update(); } /** 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(); } /** * 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 contract_date_end($format=FALSE) { $x = strtotime(sprintf('+%s months',$this->contract_term),$this->service_contract_date); return $format ? Config::date($x) : $x; } /** * 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(); } /** * Return the IP Address for the service */ public function ipaddress() { return $this->ipaddress ? $this->ipaddress : _('Dynamic'); } /** * 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.'); } /** * Calculate the Excess Traffic Charges */ public function traffic_excess($period=NULL,$charge=FALSE,$format=FALSE) { $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,1000) 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])); } } } 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) { $friendly = array( 'base_down_peak'=>'Peak', 'base_down_offpeak'=>'OffPeak', 'cumulative_base_down_peak'=>'Total Peak', 'cumulative_base_down_offpeak'=>'Total OffPeak', ); $result = array(); if ($this->service->product->prod_plugin_file != 'ADSL') 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); $traffic_type = $this->get_traffic_data_daily($period,TRUE); $day = count($traffic_type) ? max(array_keys($traffic_type)) : 1; $date = mktime(0,0,0,date('n',$period),$day,date('Y',$period)); $daysleft = date('d',strtotime('last day of',$date))-$day; $google = GoogleChart::factory('Legacy') ->type('vertical_bar') ->title(sprintf('DSL traffic usage as at %s',Config::date($date))); foreach ($traffic_data as $k => $details) $google->sdata(array('yl'=>($x=isset($friendly[$k]) ? $friendly[$k] : $k)),array($x=>$traffic_data[$k])); // Work out comulative numbers 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 ($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($allowance['base_down_offpeak']) ? $allowance['base_down_offpeak'].' MB' : '-'; break; case 'OFFPEAK_USAGE': $value = isset($traffic['base_down_offpeak']) ? $traffic['base_down_offpeak'].' MB' : '-'; break; case 'PEAK_ALLOWANCE': $value = isset($allowance['base_down_peak']) ? $allowance['base_down_peak'].' MB' : '-'; break; case 'PEAK_USAGE': $value = isset($traffic['base_down_peak']) ? $traffic['base_down_peak'].' MB' : '-'; break; case 'OFFPEAK_AVERAGE': $value = isset($traffic['base_down_offpeak']) ? round($traffic['base_down_offpeak']/$day,2).' MB' : '-'; break; case 'OFFPEAK_AVERAGE_REMAIN': $value = ((isset($traffic['base_down_offpeak']) AND ($allowance['base_down_offpeak'] > $traffic['base_down_offpeak']) AND $daysleft) ? round(($allowance['base_down_offpeak']-$traffic['base_down_offpeak'])/$daysleft,2).' MB' : '-'); break; case 'PEAK_AVERAGE': $value = isset($traffic['base_down_peak']) ? round($traffic['base_down_peak']/$day,2).' MB' : '-'; break; case 'PEAK_AVERAGE_REMAIN': $value = ((isset($traffic['base_down_peak']) AND ($allowance['base_down_peak'] > $traffic['base_down_peak']) AND $daysleft) ? round(($allowance['base_down_peak']-$traffic['base_down_peak'])/$daysleft,2).' MB' : '-'); break; case 'SERVICE_NUMBER': $value = $this->service_number; break; case 'USAGE_DATE': $value = Config::date($date); break; case 'USER_NAME': $value = $this->service->account->name(); break; default: $value = ''; } $result[$item] = $value; } return $result; } /** * 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; } /** * 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 */ public function report_traffic() { if ($this->service->product->prod_plugin_file != 'ADSL') 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); $traffic_type = $this->get_traffic_data_daily($period,TRUE); // @todo If no data comes in, then this can be stuck reporting traffic for an old date. $day = count($traffic_type) ? max(array_keys($traffic_type)) : 1; $lastday = date('d',strtotime('last day of',$period)); foreach ($traffic as $k => $v) { // If we are the last day of the period if ($day == $lastday AND $v) return TRUE; // If we are at 80% usage if ($v/($allowance[$k] > 0 ? $allowance[$k] : 1) >= .8 AND $day%3 == 0) return TRUE; // If our average is greater than our allowance if ($day%5 == 0 AND ($v/$day > $allowance[$k]/$day)) return TRUE; } // If we get here, then we dont need to report usage. return FALSE; } /** * Get specific service details for use in other modules * For Example: Invoice * * @todo Make the rendered items configurable * @todo Change this method name, now that it is public */ public function _details($type) { switch ($type) { case 'invoice_detail_items': return array( _('Service Address')=>$this->service_address ? $this->display('service_address') : '>NotSet<', _('Contract Until')=>$this->contract_date_end(TRUE), ); break; default: return parent::$_details($type); } } /** * Return an array of the data used in a month by day */ private function get_traffic_data_daily($period=NULL,$bydate=FALSE) { $result = array(); 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)))); foreach ($t->find_all() as $to) { $day = (string)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] = (int)$v; } return $result; } /** * Calculate the total traffic used in a month */ private function get_traffic_data_month($period=NULL,$cache=0) { $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; return $result; } } ?>