2010-11-29 22:41:08 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class will take care of ADSL Traffic.
|
|
|
|
*
|
|
|
|
* @package OSB
|
|
|
|
* @subpackage Service
|
|
|
|
* @category Helpers
|
|
|
|
* @author Deon George
|
|
|
|
* @copyright (c) 2010 Deon George
|
|
|
|
* @license http://dev.leenooks.net/license.html
|
|
|
|
*/
|
2013-01-11 10:51:37 +00:00
|
|
|
class Service_Traffic_Adsl {
|
2010-11-29 22:41:08 +00:00
|
|
|
protected $so;
|
|
|
|
protected $today;
|
|
|
|
protected $fetchresult = NULL;
|
|
|
|
protected $curlopts = array(
|
|
|
|
CURLOPT_CONNECTTIMEOUT => 60,
|
|
|
|
CURLOPT_FAILONERROR => TRUE,
|
|
|
|
CURLOPT_FOLLOWLOCATION => FALSE,
|
|
|
|
CURLOPT_HEADER => FALSE,
|
|
|
|
CURLOPT_HTTPPROXYTUNNEL => FALSE,
|
|
|
|
CURLOPT_RETURNTRANSFER => TRUE,
|
|
|
|
CURLOPT_TIMEOUT => 30,
|
|
|
|
CURLOPT_SSL_VERIFYHOST => FALSE,
|
|
|
|
CURLOPT_SSL_VERIFYPEER => FALSE,
|
|
|
|
CURLOPT_VERBOSE => FALSE,
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup this class. We need to get our supplier details out of the database.
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
// Our DB record must be the suffix of this class name
|
|
|
|
$supplier = preg_replace('/^'.get_parent_class($this).'_/','',get_class($this));
|
|
|
|
|
2012-11-09 23:13:57 +00:00
|
|
|
$so = ORM::factory('Adsl_Supplier')
|
2010-11-29 22:41:08 +00:00
|
|
|
->where('name','=',$supplier)
|
|
|
|
->find();
|
|
|
|
|
|
|
|
if (! $so->loaded())
|
|
|
|
throw new Kohana_Exception('Supplier :supplier not defined in the database',array(':supplier'=>$supplier));
|
|
|
|
|
|
|
|
$this->so = $so;
|
|
|
|
$this->today = date('Y-m-d',strtotime('yesterday'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of this class
|
|
|
|
*
|
|
|
|
* @return HeadImage
|
|
|
|
*/
|
|
|
|
public static function instance($supplier) {
|
2013-01-11 10:51:37 +00:00
|
|
|
$sc = sprintf('%s_%s',get_called_class(),ucwords(strtolower($supplier)));
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
if (! class_exists($sc))
|
|
|
|
throw new Kohana_Exception('Class doesnt exist for :supplier',array(':supplier'=>$supplier));
|
|
|
|
else
|
|
|
|
return new $sc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the last date we obtained the stats.
|
|
|
|
*/
|
|
|
|
private function last_update() {
|
|
|
|
return $this->so->stats_lastupdate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Traffic data from supplier
|
|
|
|
*/
|
|
|
|
public function update_traffic() {
|
2012-11-09 23:13:57 +00:00
|
|
|
if (Minion_CLI::options('verbose'))
|
2012-02-22 08:15:46 +00:00
|
|
|
echo ' - Last: '.date('Y-m-d',strtotime($this->last_update().'+1 day'))."\n";
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
$alreadyrun = FALSE;
|
|
|
|
for ($querydate=date('Y-m-d',strtotime($this->last_update().'+1 day'));
|
|
|
|
$querydate<=$this->today;
|
|
|
|
$querydate=date('Y-m-d',strtotime($querydate.'+1 day'))) {
|
|
|
|
|
2012-11-09 23:13:57 +00:00
|
|
|
if (Minion_CLI::options('verbose'))
|
2012-02-22 08:15:46 +00:00
|
|
|
echo " - Date: $querydate\n";
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
$goodfetch = false;
|
|
|
|
// @todo log this fetch in a "log"
|
|
|
|
|
|
|
|
// Supplier specific output
|
|
|
|
// Data returned should be in MB's
|
|
|
|
$data = $this->getdata($querydate);
|
|
|
|
|
2012-11-09 23:13:57 +00:00
|
|
|
if (Minion_CLI::options('verbose'))
|
2012-02-22 08:15:46 +00:00
|
|
|
print_r($data);
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
if (! $this->fetchresult) {
|
|
|
|
echo 'Bad fetch'.get_class($this);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-09 23:13:57 +00:00
|
|
|
$traffic = ORM::factory('Service_Plugin_Adsl_Traffic');
|
2010-11-29 22:41:08 +00:00
|
|
|
foreach ($data as $item) {
|
2011-08-03 14:24:38 +00:00
|
|
|
$traffic->values($item,array_keys($item));
|
2010-11-29 22:41:08 +00:00
|
|
|
$traffic->supplier_id = $this->so->id;
|
|
|
|
|
|
|
|
if ($traffic->check())
|
|
|
|
$traffic->save();
|
|
|
|
|
|
|
|
if (! $traffic->saved())
|
|
|
|
throw new Kohana_Exception('Unable to save traffic record');
|
|
|
|
|
|
|
|
$traffic->clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->so->stats_lastupdate = $this->today;
|
|
|
|
$this->so->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function charge_excess_traffic() {
|
2011-08-02 06:48:41 +00:00
|
|
|
$date = strtotime('last month');
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
// @todo need a way to find out services that have traffic charges dynamically.
|
|
|
|
foreach ($this->so->services() as $so) {
|
2011-09-28 06:46:22 +00:00
|
|
|
if ($charge = $so->plugin()->traffic_lastmonth_exceed(FALSE,$date)) {
|
2010-11-29 22:41:08 +00:00
|
|
|
foreach ($charge as $metric => $details) {
|
2012-11-09 23:13:57 +00:00
|
|
|
$co = ORM::factory('Charge');
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
$co->status = 0;
|
|
|
|
$co->sweep_type = 6;
|
|
|
|
$co->account_id = $so->account_id;
|
|
|
|
$co->service_id = $so->id;
|
|
|
|
$co->amount = $details['rate'];
|
|
|
|
// @todo This needs to be calculated.
|
|
|
|
$co->taxable = TRUE;
|
|
|
|
$co->quantity = ceil($details['excess']/1000);
|
|
|
|
$co->description = _('Excess Traffic');
|
|
|
|
// @todo This need to be improved = strtotime function should be the one used in the function call
|
|
|
|
$co->attributes = implode("\n",array(
|
2011-09-28 06:46:22 +00:00
|
|
|
sprintf('ADSL Service==%s',$so->plugin()->service_number),
|
2010-11-29 22:41:08 +00:00
|
|
|
sprintf('Allowance==%s',$details['allowance']),
|
|
|
|
sprintf('Metric==%s',$metric),
|
|
|
|
sprintf('Used==%s',$details['used']),
|
2011-08-02 06:48:41 +00:00
|
|
|
sprintf('Month==%s',date('Y-m',$date)),
|
2010-11-29 22:41:08 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$co->check();
|
|
|
|
$co->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function alert_traffic() {
|
2011-08-26 02:01:45 +00:00
|
|
|
$et = Email_Template::instance('adsl_traffic_notice');
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
foreach ($this->so->services() as $so) {
|
2011-09-28 06:46:22 +00:00
|
|
|
if (! $so->plugin()->report_traffic())
|
2010-11-29 22:41:08 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get our variable data
|
2011-08-25 23:21:39 +00:00
|
|
|
$et->to = array('account'=>array($so->account_id));
|
2011-09-28 06:46:22 +00:00
|
|
|
$et->variables = $so->plugin()->template_variables($et->variables());
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
$et->send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|