This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/adsl/classes/Service/Traffic/Adsl.php

167 lines
4.4 KiB
PHP
Raw Normal View History

2013-10-10 02:44:53 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class will take care of ADSL Traffic.
*
* @package ADSL
* @category Service
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Service_Traffic_Adsl {
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));
$so = ORM::factory('ADSL_Supplier')
->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) {
$sc = Kohana::classname(get_called_class().'_'.$supplier);
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() {
if (Minion_CLI::options('verbose'))
echo ' - Last: '.date('Y-m-d',strtotime($this->last_update().'+1 day'))."\n";
$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'))) {
if (Minion_CLI::options('verbose'))
echo " - Date: $querydate\n";
$goodfetch = false;
// @todo log this fetch in a "log"
// Supplier specific output
// Data returned should be in MB's
$data = $this->getdata($querydate);
if (Minion_CLI::options('verbose'))
print_r($data);
if (! $this->fetchresult) {
echo 'Bad fetch'.get_class($this);
break;
}
$traffic = ORM::factory('Service_Plugin_Adsl_Traffic');
foreach ($data as $item) {
$traffic->values($item,array_keys($item));
$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() {
$date = strtotime('last month');
// @todo need a way to find out services that have traffic charges dynamically.
foreach ($this->so->services() as $so) {
if ($charge = $so->plugin()->traffic_lastmonth_exceed(FALSE,$date)) {
foreach ($charge as $metric => $details) {
$co = ORM::factory('Charge');
$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(
sprintf('ADSL Service==%s',$so->plugin()->service_number),
sprintf('Allowance==%s',$details['allowance']),
sprintf('Metric==%s',$metric),
sprintf('Used==%s',$details['used']),
sprintf('Month==%s',date('Y-m',$date)),
));
$co->check();
$co->save();
}
}
}
}
public function alert_traffic() {
$et = Email_Template::instance('adsl_traffic_notice');
foreach ($this->so->services() as $so) {
if (! $so->plugin()->report_traffic())
continue;
// Get our variable data
$et->to = array('account'=>array($so->account_id));
$et->variables = $so->plugin()->template_variables($et->variables());
$et->send();
}
}
}
?>