More updates to ADSL module

This commit is contained in:
Deon George 2013-11-15 16:25:45 +11:00
parent f80fcdc4c1
commit c66f2f14c4
12 changed files with 203 additions and 193 deletions

View File

@ -40,8 +40,8 @@ class Controller_Admin_Adsl extends Controller_Adsl {
->jssort('adsl') ->jssort('adsl')
->columns(array( ->columns(array(
'id'=>'ID', 'id'=>'ID',
'adsl_supplier_plan->name()'=>'Plan', 'supplier_plan->name()'=>'Plan',
'adsl_supplier_plan->display("status")'=>'Avail', 'supplier_plan->display("status")'=>'Avail',
'base_down_peak'=>'PD', 'base_down_peak'=>'PD',
'base_down_offpeak'=>'OPD', 'base_down_offpeak'=>'OPD',
'base_up_peak'=>'PU', 'base_up_peak'=>'PU',

View File

@ -115,7 +115,7 @@ class Model_Product_Plugin_Adsl extends Model_Product_Plugin {
* *
* @param array Traffic Used in each metric. * @param array Traffic Used in each metric.
* @param bool Whether the output should be a string * @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 * @param int Divide the numbers
*/ */
public function allowance(array $data=array(),$format=FALSE,$over=FALSE,$divide=0) { public function allowance(array $data=array(),$format=FALSE,$over=FALSE,$divide=0) {

View File

@ -103,6 +103,19 @@ $(document).ready(function() {
return $format ? Config::date($x) : $x; 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 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 * 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.'); 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 * Calculate the Excess Traffic Charges
*/ */
@ -298,6 +385,48 @@ $(document).ready(function() {
return $x ? array_pop($x) : 0; 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 * Render a table of traffic
*/ */
@ -323,10 +452,6 @@ $(document).ready(function() {
* Search for services matching a term * Search for services matching a term
*/ */
public function list_autocomplete($term,$index,$value,array $label,array $limit=array(),array $options=NULL) { 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(); $ao = Auth::instance()->get_user();
$options['key'] = 'id'; $options['key'] = 'id';
@ -335,130 +460,14 @@ $(document).ready(function() {
->join('service') ->join('service')
->on('service.id','=',$this->_table_name.'.service_id') ->on('service.id','=',$this->_table_name.'.service_id')
->where('service.account_id','IN',$ao->RTM->customers($ao->RTM)) ->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); 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 * Get specific service details for use in other modules
* For Example: Invoice * For Example: Invoice
@ -478,47 +487,5 @@ $(document).ready(function() {
return parent::$_details($type); 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;
}
} }
?> ?>

View File

@ -48,6 +48,10 @@ class Model_Service_Plugin_Adsl_Traffic extends ORM_OSB {
return $result; return $result;
} }
public function date() {
return strtotime($this->date);
}
public function friendly($name) { public function friendly($name) {
return isset($this->_friendly[$name]) ? $this->_friendly[$name] : $name; return isset($this->_friendly[$name]) ? $this->_friendly[$name] : $name;
} }

View File

@ -105,20 +105,5 @@ abstract class Service_Traffic_Adsl {
$this->aso->save(); $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();
}
}
} }
?> ?>

View File

@ -9,14 +9,35 @@
* @copyright (c) 2009-2013 Open Source Billing * @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html * @license http://dev.osbill.net/license.html
*/ */
class Task_Adsl_Trafficalert extends Task_Adsl_Trafficget { class Task_Adsl_Trafficalert extends Minion_Task {
protected function _execute(array $params) { protected $_options = array(
foreach ($this->_traffic_suppliers(TRUE) as $aso) { 'verbose'=>FALSE,
if ($params['verbose']) );
echo $aso->name."\n";
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));
} }
} }
?> ?>

View File

@ -9,7 +9,7 @@
* @copyright (c) 2009-2013 Open Source Billing * @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html * @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( protected $_options = array(
'verbose'=>FALSE, 'verbose'=>FALSE,
); );

View File

@ -35,7 +35,7 @@
<tr class="plan-features"> <tr class="plan-features">
<th>Speed</th> <th>Speed</th>
<?php $c=0; foreach ($o->products() as $po) : ?> <?php $c=0; foreach ($o->products() as $po) : ?>
<td><?php echo $po->plugin()->adsl_supplier_plan->display('speed'); ?></td> <td><?php echo $po->plugin()->supplier_plan->display('speed'); ?></td>
<?php endforeach ?> <?php endforeach ?>
</tr> </tr>

View File

@ -34,7 +34,7 @@
<div class="plan-features"> <div class="plan-features">
<ul> <ul>
<li><span class="note">$</span><strong><?php echo Currency::display($po->price(0,1,'price_setup',TRUE)); ?></strong> Connection</li> <li><span class="note">$</span><strong><?php echo Currency::display($po->price(0,1,'price_setup',TRUE)); ?></strong> Connection</li>
<li><strong><?php echo $po->plugin()->adsl_supplier_plan->display('speed'); ?></strong> Speed</li> <li><strong><?php echo $po->plugin()->supplier_plan->display('speed'); ?></strong> Speed</li>
<li><strong><?php echo $po->plugin()->base_down_peak/1000; ?></strong>GB Peak Downloads</li> <li><strong><?php echo $po->plugin()->base_down_peak/1000; ?></strong>GB Peak Downloads</li>
<?php if ($po->plugin()->base_down_offpeak) : ?> <?php if ($po->plugin()->base_down_offpeak) : ?>
<li><strong><?php echo $po->plugin()->base_down_offpeak/1000; ?></strong>GB OffPeak Downloads</li> <li><strong><?php echo $po->plugin()->base_down_offpeak/1000; ?></strong>GB OffPeak Downloads</li>

View File

@ -12,6 +12,7 @@
class Model_Charge extends ORM_OSB { class Model_Charge extends ORM_OSB {
protected $_belongs_to = array( protected $_belongs_to = array(
'account'=>array(), 'account'=>array(),
'service'=>array(),
); );
protected $_has_one = array( protected $_has_one = array(
'invoice_item'=>array('far_key'=>'id'), 'invoice_item'=>array('far_key'=>'id'),
@ -46,6 +47,36 @@ class Model_Charge extends ORM_OSB {
), ),
); );
/**
* Temporarily override our parent call to reformat old data
*/
public function __get($column) {
switch ($column) {
case 'attributes':
$x = parent::__get($column);
if (is_string($this->_object[$column]) AND preg_match('/==/',$this->_object[$column])) {
$x = explode("\n",$this->_object[$column]);
foreach ($x as $k=>$v) {
if (preg_match('/==/',$x[$k]))
$x[$k] = preg_replace('/==\s*/',':',$v);
if (preg_replace('/^ADSL Service:/',"",$x[$k]) == $this->service->plugin()->service_number)
unset($x[$k]);
}
sort($x);
}
$this->{$column} = $x;
return $x;
default: return parent::__get($column);
}
}
/** /**
* Render some details for specific calls, eg: invoice * Render some details for specific calls, eg: invoice
*/ */

View File

@ -26,6 +26,7 @@ class Controller_User_Invoice extends Controller_Invoice {
$imo = $io->invoice_memo; $imo = $io->invoice_memo;
$imo->invoice_id = $io->id; $imo->invoice_id = $io->id;
$imo->account_id = $this->ao->id; $imo->account_id = $this->ao->id;
$imo->type = 'download';
$imo->memo = 'Invoice Downloaded.'; $imo->memo = 'Invoice Downloaded.';
$imo->save(); $imo->save();

View File

@ -41,6 +41,7 @@ class Controller_User_SSL extends Controller_SSL {
$smo = $so->service_memo; $smo = $so->service_memo;
$smo->service_id = $so->id; $smo->service_id = $so->id;
$smo->account_id = $this->ao->id; $smo->account_id = $this->ao->id;
$smo->type = 'download';
$smo->memo = sprintf('SSL Certificate %s Downloaded.',$so->plugin()->serial()); $smo->memo = sprintf('SSL Certificate %s Downloaded.',$so->plugin()->serial());
$smo->save(); $smo->save();