OSB enhancements to date
This commit is contained in:
101
modules/service/classes/model/service.php
Normal file
101
modules/service/classes/model/service.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class supports Services
|
||||
*
|
||||
* @package OSB
|
||||
* @subpackage Service
|
||||
* @category Models
|
||||
* @author Deon George
|
||||
* @copyright (c) 2010 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class Model_Service extends ORMOSB {
|
||||
// Relationships
|
||||
protected $_has_many = array(
|
||||
'invoice'=>array('through'=>'invoice_item'),
|
||||
'adsl_plan'=>array('through'=>'service__adsl'),
|
||||
);
|
||||
protected $_has_one = array(
|
||||
'service_adsl'=>array(),
|
||||
);
|
||||
protected $_belongs_to = array(
|
||||
'product'=>array(),
|
||||
'account'=>array(),
|
||||
);
|
||||
|
||||
protected $_formats = array(
|
||||
'active'=>array('StaticList_YesNo::display'=>array()),
|
||||
'date_next_invoice'=>array('Config::date'=>array()),
|
||||
'recur_schedule'=>array('StaticList_RecurSchedule::display'=>array()),
|
||||
'price'=>array(
|
||||
'Tax::add'=>array(),
|
||||
'Currency::display'=>array(),
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* Display the service number
|
||||
*/
|
||||
public function svcnum() {
|
||||
return sprintf('%05s',$this->id);
|
||||
}
|
||||
|
||||
// Nothing to directly display on invoices for this module.
|
||||
public function invoice_display() {
|
||||
if ($this->sku)
|
||||
return sprintf('%s: %s',_('Service'),$this->sku);
|
||||
else
|
||||
return '';
|
||||
}
|
||||
|
||||
public function name() {
|
||||
return $this->product->product_translate->find()->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find invoices associated with this service
|
||||
*/
|
||||
public function invoices() {
|
||||
$return = array();
|
||||
|
||||
foreach ($this->invoice->distinct('id')->find_all() as $invoice) {
|
||||
$return[$invoice->id]['due'] = $invoice->due();
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find invoices currently outstanding associated with this service
|
||||
*/
|
||||
public function invoices_due() {
|
||||
$return = array();
|
||||
|
||||
foreach ($this->invoices() as $id => $invoice)
|
||||
if ($invoice['due'])
|
||||
array_push($return,$invoice);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the total of invoices due for this service
|
||||
*/
|
||||
public function invoices_due_total() {
|
||||
$total = 0;
|
||||
foreach ($this->invoices_due() as $invoice)
|
||||
$total += $invoice['due'];
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
// @todo To implement
|
||||
/**
|
||||
* Calculate the tax for this item
|
||||
*/
|
||||
public function tax() {
|
||||
return $this->price * .1;
|
||||
}
|
||||
}
|
||||
?>
|
328
modules/service/classes/model/service/adsl.php
Normal file
328
modules/service/classes/model/service/adsl.php
Normal file
@@ -0,0 +1,328 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class supports Services
|
||||
*
|
||||
* @package OSB
|
||||
* @subpackage ADSL
|
||||
* @category Models
|
||||
* @author Deon George
|
||||
* @copyright (c) 2010 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class Model_Service_ADSL extends ORMOSB {
|
||||
protected $_table_name = 'service__adsl';
|
||||
protected $_updated_column = FALSE;
|
||||
|
||||
// Relationships
|
||||
protected $_belongs_to = array(
|
||||
'adsl_plan'=>array('foreign_key'=>'adsl_plan_id'),
|
||||
'service'=>array(),
|
||||
);
|
||||
|
||||
/**
|
||||
* Return the IP Address for the service
|
||||
*/
|
||||
public function ipaddress() {
|
||||
return $this->ipaddress ? $this->ipaddress : _('Dynamic');
|
||||
}
|
||||
|
||||
public function contract_date_start() {
|
||||
//@todo Use the system configured date format
|
||||
return date('Y-m-d',$this->service_connect_date);
|
||||
}
|
||||
|
||||
public function contract_date_end() {
|
||||
//@todo Use the system configured date format
|
||||
return date('Y-m-d',strtotime(sprintf('+%s months',$this->adsl_plan->contract_term),$this->service_connect_date));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the total traffic used in a month
|
||||
*/
|
||||
private function get_traffic_data_month($period=NULL) {
|
||||
$return = array();
|
||||
|
||||
foreach ($this->get_traffic_data_daily($period,TRUE) as $tdata)
|
||||
foreach ($tdata as $k => $v)
|
||||
if (isset($return[$k]))
|
||||
$return[$k] += $v;
|
||||
else
|
||||
$return[$k] = $v;
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of the data used in a month by day
|
||||
*/
|
||||
public function get_traffic_data_daily($period=NULL,$bydate=FALSE) {
|
||||
$cacheable = TRUE;
|
||||
if (is_null($period))
|
||||
$period = strtotime('yesterday');
|
||||
|
||||
$cache = $this->service_id.date('M-Y',$period).$bydate;
|
||||
|
||||
// @todo This cache needs to be improved, so that we cache the query regardless of bydate setting
|
||||
if ($cacheable AND $return = Cache::instance(Config::cachetype())->get($cache))
|
||||
return $return;
|
||||
|
||||
$return = array();
|
||||
|
||||
$to = ORM::factory('service_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))));
|
||||
|
||||
foreach ($to->find_all() as $traffic) {
|
||||
// Roll up the charges according to the configuration
|
||||
$data = ADSL::allowance(array(
|
||||
'base_down_peak'=>is_null($this->adsl_plan->base_down_peak) ? NULL : $traffic->down_peak,
|
||||
'base_down_offpeak'=>is_null($this->adsl_plan->base_down_offpeak) ? NULL : $traffic->down_offpeak,
|
||||
'base_up_peak'=>is_null($this->adsl_plan->base_up_peak) ? NULL : $traffic->up_peak,
|
||||
'base_up_offpeak'=>is_null($this->adsl_plan->base_up_offpeak) ? NULL : $traffic->up_offpeak,
|
||||
'extra_down_peak'=>$this->adsl_plan->extra_down_peak,
|
||||
'extra_down_offpeak'=>$this->adsl_plan->extra_down_offpeak,
|
||||
'extra_up_peak'=>$this->adsl_plan->extra_up_peak,
|
||||
'extra_up_offpeak'=>$this->adsl_plan->extra_up_offpeak,
|
||||
));
|
||||
|
||||
$day = date('d',strtotime($traffic->date));
|
||||
if ($bydate)
|
||||
$return[$day] = $data;
|
||||
else
|
||||
foreach ($data as $k => $v)
|
||||
$return[$k][$day] = $v;
|
||||
}
|
||||
|
||||
// Cache our data
|
||||
// @todo Our cache time should be a configuration parameter
|
||||
Cache::instance(Config::cachetype())->set($cache,$return,43200);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of the data used in a year by month
|
||||
*/
|
||||
public function get_traffic_data_monthly($period=NULL,$bydate=FALSE) {
|
||||
$cacheable = FALSE;
|
||||
if (is_null($period))
|
||||
$period = strtotime('yesterday');
|
||||
|
||||
$cache = $this->service_id.date('M-Y',$period).$bydate.__METHOD__;
|
||||
|
||||
// @todo This cache needs to be improved, so that we cache the query regardless of bydate setting
|
||||
if ($cacheable AND $return = Cache::instance(Config::cachetype())->get($cache))
|
||||
return $return;
|
||||
|
||||
$return = array();
|
||||
|
||||
$to = ORM::factory('service_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 ($to->find_all() as $traffic) {
|
||||
// Roll up the charges according to the configuration
|
||||
$data = ADSL::allowance(array(
|
||||
'base_down_peak'=>is_null($this->adsl_plan->base_down_peak) ? NULL : $traffic->down_peak,
|
||||
'base_down_offpeak'=>is_null($this->adsl_plan->base_down_offpeak) ? NULL : $traffic->down_offpeak,
|
||||
'base_up_peak'=>is_null($this->adsl_plan->base_up_peak) ? NULL : $traffic->up_peak,
|
||||
'base_up_offpeak'=>is_null($this->adsl_plan->base_up_offpeak) ? NULL : $traffic->up_offpeak,
|
||||
'extra_down_peak'=>$this->adsl_plan->extra_down_peak,
|
||||
'extra_down_offpeak'=>$this->adsl_plan->extra_down_offpeak,
|
||||
'extra_up_peak'=>$this->adsl_plan->extra_up_peak,
|
||||
'extra_up_offpeak'=>$this->adsl_plan->extra_up_offpeak,
|
||||
));
|
||||
|
||||
if ($bydate)
|
||||
$return[$traffic->month] = $data;
|
||||
else
|
||||
foreach ($data as $k => $v)
|
||||
$return[$k][$traffic->month] = $v;
|
||||
}
|
||||
|
||||
// Cache our data
|
||||
// @todo Our cache time should be a configuration parameter
|
||||
Cache::instance(Config::cachetype())->set($cache,$return,43200);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
public function traffic_month($month,$string=TRUE) {
|
||||
return $string ? implode('/',$this->get_traffic_data_month($month)) :
|
||||
$this->get_traffic_data_month($month);
|
||||
}
|
||||
|
||||
public function traffic_lastmonth($string=TRUE) {
|
||||
return $this->traffic_month(strtotime('last month'),$string);
|
||||
}
|
||||
|
||||
public function traffic_thismonth($string=TRUE) {
|
||||
return $this->traffic_month(strtotime('yesterday'),$string);
|
||||
}
|
||||
|
||||
public function traffic_lastmonth_exceed($all=FALSE) {
|
||||
$return = array();
|
||||
|
||||
foreach ($this->traffic_month(strtotime('last month'),FALSE) as $k => $v) {
|
||||
// We shouldnt need to eval for nulls, since the traffic calc does that
|
||||
if ($all OR ($v > $this->adsl_plan->$k)) {
|
||||
$return[$k]['allowance'] = $this->adsl_plan->$k;
|
||||
$return[$k]['used'] = $v;
|
||||
$return[$k]['shaped'] = (! empty($this->adsl_plan->extra_shaped) AND $this->adsl_plan->extra_shaped AND $v > $this->adsl_plan->$k) ? TRUE : FALSE;
|
||||
$return[$k]['excess'] = (! empty($this->adsl_plan->extra_charged) AND $this->adsl_plan->extra_charged AND $v > $this->adsl_plan->$k) ? $v-$this->adsl_plan->$k : 0;
|
||||
$return[$k]['rate'] = $this->adsl_plan->{ADSL::map($k)};
|
||||
$return[$k]['charge'] = ceil(($return[$k]['excess'])/1000)*$return[$k]['rate'];
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
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',
|
||||
);
|
||||
|
||||
$return = array();
|
||||
$allowance = $this->adsl_plan->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('m',$period),$day,date('Y',$period));
|
||||
$daysleft = date('d',strtotime('last day ',$date))-$day;
|
||||
|
||||
$google = GoogleChart::factory('vertical_bar');
|
||||
$google->title = sprintf('DSL traffic usage as at %s',Config::date($date));
|
||||
|
||||
foreach ($traffic_data as $k => $details)
|
||||
$google->series(array(
|
||||
'title'=>array((isset($friendly[$k]) ? $friendly[$k] : $k)),
|
||||
'axis'=>'x',
|
||||
'data'=>array((isset($friendly[$k]) ? $friendly[$k] : $k)=>$traffic_data[$k])));
|
||||
|
||||
// Work out comulative numbers
|
||||
foreach ($traffic_data as $k => $details)
|
||||
$google->series(array(
|
||||
'title'=>array((isset($friendly['cumulative_'.$k]) ? $friendly['cumulative_'.$k] : 'cumulative_'.$k)),
|
||||
'axis'=>'r',
|
||||
'data'=>array((isset($friendly['cumulative_'.$k]) ? $friendly['cumulative_'.$k] : 'cumulative_'.$k)=>$this->cumulative($traffic_data['cumulative_'.$k]))));
|
||||
|
||||
foreach ($array as $item) {
|
||||
switch ($item) {
|
||||
case 'MONTH_GRAPH': $value = (string)$google; break;
|
||||
case 'MONTH_TABLE': $value = $google->html_table(FALSE,array(
|
||||
'table'=>'style="border: 1px solid #bebcb7; padding: 5px 5px; background: none repeat scroll 0% 0% #f8f7f5;"',
|
||||
)); 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 = '';
|
||||
}
|
||||
|
||||
$return[$item] = $value;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will take an array of numbers and change it into a cumulative array
|
||||
*/
|
||||
public 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() {
|
||||
$allowance = $this->adsl_plan->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;
|
||||
$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 == 3)
|
||||
return TRUE;
|
||||
|
||||
// If our average is greater than our allowance
|
||||
if ($day%5 == 5 AND ($v/$day > $allowance[$k]/$day))
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// If we get here, then we dont need to report usage.
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
?>
|
23
modules/service/classes/model/service/adsl/traffic.php
Normal file
23
modules/service/classes/model/service/adsl/traffic.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class supports Services Traffic for ADSL
|
||||
*
|
||||
* @package OSB
|
||||
* @subpackage Service
|
||||
* @category Models
|
||||
* @author Deon George
|
||||
* @copyright (c) 2010 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class Model_Service_ADSL_Traffic extends ORMOSB {
|
||||
protected $_table_name = 'service__adsl_traffic';
|
||||
protected $_primary_key = 'service';
|
||||
protected $_callbacks = array(
|
||||
'site_id'=>array('set_site_id'),
|
||||
);
|
||||
|
||||
protected $_created_column = FALSE;
|
||||
protected $_updated_column = FALSE;
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user