From c66f2f14c459689a8f4ebda4391409dc73f98aac Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 15 Nov 2013 16:25:45 +1100 Subject: [PATCH] More updates to ADSL module --- .../adsl/classes/Controller/Admin/Adsl.php | 4 +- .../classes/Model/Product/Plugin/Adsl.php | 2 +- .../classes/Model/Service/Plugin/Adsl.php | 299 ++++++++---------- .../Model/Service/Plugin/Adsl/Traffic.php | 4 + modules/adsl/classes/Service/Traffic/Adsl.php | 15 - .../adsl/classes/Task/Adsl/Trafficalert.php | 33 +- .../adsl/classes/Task/Adsl/Trafficcharge.php | 2 +- .../category/list/adslcompare-large.php | 2 +- .../product/category/list/adslcompare.php | 2 +- modules/charge/classes/Model/Charge.php | 31 ++ .../classes/Controller/User/Invoice.php | 1 + modules/ssl/classes/Controller/User/Ssl.php | 1 + 12 files changed, 203 insertions(+), 193 deletions(-) diff --git a/modules/adsl/classes/Controller/Admin/Adsl.php b/modules/adsl/classes/Controller/Admin/Adsl.php index 722202e8..720873c4 100644 --- a/modules/adsl/classes/Controller/Admin/Adsl.php +++ b/modules/adsl/classes/Controller/Admin/Adsl.php @@ -40,8 +40,8 @@ class Controller_Admin_Adsl extends Controller_Adsl { ->jssort('adsl') ->columns(array( 'id'=>'ID', - 'adsl_supplier_plan->name()'=>'Plan', - 'adsl_supplier_plan->display("status")'=>'Avail', + 'supplier_plan->name()'=>'Plan', + 'supplier_plan->display("status")'=>'Avail', 'base_down_peak'=>'PD', 'base_down_offpeak'=>'OPD', 'base_up_peak'=>'PU', diff --git a/modules/adsl/classes/Model/Product/Plugin/Adsl.php b/modules/adsl/classes/Model/Product/Plugin/Adsl.php index 12ead200..4a73dbb5 100644 --- a/modules/adsl/classes/Model/Product/Plugin/Adsl.php +++ b/modules/adsl/classes/Model/Product/Plugin/Adsl.php @@ -115,7 +115,7 @@ class Model_Product_Plugin_Adsl extends Model_Product_Plugin { * * @param array Traffic Used in each metric. * @param bool Whether the output should be a string - * @param bool Display the over alloance numbers + * @param bool Display the over allowance numbers * @param int Divide the numbers */ public function allowance(array $data=array(),$format=FALSE,$over=FALSE,$divide=0) { diff --git a/modules/adsl/classes/Model/Service/Plugin/Adsl.php b/modules/adsl/classes/Model/Service/Plugin/Adsl.php index 91b904c9..343e6fe3 100644 --- a/modules/adsl/classes/Model/Service/Plugin/Adsl.php +++ b/modules/adsl/classes/Model/Service/Plugin/Adsl.php @@ -103,6 +103,19 @@ $(document).ready(function() { return $format ? Config::date($x) : $x; } + /** + * 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; + } + /** * 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 @@ -229,6 +242,80 @@ $(document).ready(function() { throw new Kohana_Exception('This function hasnt been written yet.'); } + /** + * 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') + ->title(sprintf('DSL traffic usage as at %s',Config::date($data['last']))); + + 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; + case 'USAGE_DATE': $value = Config::date($data['last']); break; + case 'USER_NAME': $value = $this->service->account->name(); break; + default: + $value = ''; + } + + $result[$item] = $value; + } + + return $result; + } + /** * Calculate the Excess Traffic Charges */ @@ -298,6 +385,48 @@ $(document).ready(function() { return $x ? array_pop($x) : 0; } + /** + * 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(); + $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 (! $result['used']) + return array(); + +$result['day'] = 3; + // 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 + 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 + 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 */ @@ -323,10 +452,6 @@ $(document).ready(function() { * 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'; @@ -335,130 +460,14 @@ $(document).ready(function() { ->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.'%'); + ->where_open() + ->and_where($this->_table_name.'.service_number','like','%'.$term.'%') + ->or_where($this->_table_name.'.service_address','like','%'.$term.'%') + ->where_close(); 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 @@ -478,47 +487,5 @@ $(document).ready(function() { 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; - } } ?> diff --git a/modules/adsl/classes/Model/Service/Plugin/Adsl/Traffic.php b/modules/adsl/classes/Model/Service/Plugin/Adsl/Traffic.php index 1635a387..59f0fdb1 100644 --- a/modules/adsl/classes/Model/Service/Plugin/Adsl/Traffic.php +++ b/modules/adsl/classes/Model/Service/Plugin/Adsl/Traffic.php @@ -48,6 +48,10 @@ class Model_Service_Plugin_Adsl_Traffic extends ORM_OSB { return $result; } + public function date() { + return strtotime($this->date); + } + public function friendly($name) { return isset($this->_friendly[$name]) ? $this->_friendly[$name] : $name; } diff --git a/modules/adsl/classes/Service/Traffic/Adsl.php b/modules/adsl/classes/Service/Traffic/Adsl.php index 644db4c3..6b0989fb 100644 --- a/modules/adsl/classes/Service/Traffic/Adsl.php +++ b/modules/adsl/classes/Service/Traffic/Adsl.php @@ -105,20 +105,5 @@ abstract class Service_Traffic_Adsl { $this->aso->save(); } } - - public function alert_traffic() { - foreach ($this->aso->services() as $so) { - if (! $so->plugin()->report_traffic()) - continue; - - $et = Email_Template::instance('adsl_traffic_notice'); - - // Get our variable data - $et->to = array('account'=>array($so->account_id)); - $et->variables = $so->plugin()->template_variables($et->variables()); - - $et->send(); - } - } } ?> diff --git a/modules/adsl/classes/Task/Adsl/Trafficalert.php b/modules/adsl/classes/Task/Adsl/Trafficalert.php index ad791b93..3b0662d6 100644 --- a/modules/adsl/classes/Task/Adsl/Trafficalert.php +++ b/modules/adsl/classes/Task/Adsl/Trafficalert.php @@ -9,14 +9,35 @@ * @copyright (c) 2009-2013 Open Source Billing * @license http://dev.osbill.net/license.html */ -class Task_Adsl_Trafficalert extends Task_Adsl_Trafficget { - protected function _execute(array $params) { - foreach ($this->_traffic_suppliers(TRUE) as $aso) { - if ($params['verbose']) - echo $aso->name."\n"; +class Task_Adsl_Trafficalert extends Minion_Task { + protected $_options = array( + 'verbose'=>FALSE, + ); - Service_Traffic_Adsl::instance($aso->name)->alert_traffic(); + protected function _execute(array $params) { + $date = strtotime('last month'); + $c = array(); + + // @todo Pick up services that are no longer active, but were inactive < 30 days ago. + foreach (ORM::factory('Service')->list_byplugin('ADSL') as $so) { + if ($params['verbose']) + echo $so->service_name()."\n"; + + if (! $data=$so->plugin()->traffic_report()) + continue; + + $et = Email_Template::instance('adsl_traffic_notice'); + + // Get our variable data + $et->to = array('account'=>array($so->account_id)); + $et->variables = $so->plugin()->template_variables($et->variables(),$data); + + $et->send(); + + array_push($c,$so->id); } + + return sprintf('%s alerts sent (%s)',count($c),join('|',$c)); } } ?> diff --git a/modules/adsl/classes/Task/Adsl/Trafficcharge.php b/modules/adsl/classes/Task/Adsl/Trafficcharge.php index c1b822e9..3b56709b 100644 --- a/modules/adsl/classes/Task/Adsl/Trafficcharge.php +++ b/modules/adsl/classes/Task/Adsl/Trafficcharge.php @@ -9,7 +9,7 @@ * @copyright (c) 2009-2013 Open Source Billing * @license http://dev.osbill.net/license.html */ -class Task_Adsl_Trafficcharge extends Task_Adsl_Trafficget { +class Task_Adsl_Trafficcharge extends Minion_Task { protected $_options = array( 'verbose'=>FALSE, ); diff --git a/modules/adsl/views/product/category/list/adslcompare-large.php b/modules/adsl/views/product/category/list/adslcompare-large.php index 45d87e67..a45540ba 100644 --- a/modules/adsl/views/product/category/list/adslcompare-large.php +++ b/modules/adsl/views/product/category/list/adslcompare-large.php @@ -35,7 +35,7 @@ Speed products() as $po) : ?> - plugin()->adsl_supplier_plan->display('speed'); ?> + plugin()->supplier_plan->display('speed'); ?> diff --git a/modules/adsl/views/product/category/list/adslcompare.php b/modules/adsl/views/product/category/list/adslcompare.php index 41081b39..a3561781 100644 --- a/modules/adsl/views/product/category/list/adslcompare.php +++ b/modules/adsl/views/product/category/list/adslcompare.php @@ -34,7 +34,7 @@