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/Model/Service/Plugin/Adsl/Traffic.php

127 lines
3.0 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports Services Traffic for ADSL
*
* @package ADSL
* @category Models
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Service_Plugin_Adsl_Traffic extends ORM {
protected $_table_name = 'service__adsl_traffic';
protected $_primary_key = 'service';
protected $_disable_wild_select = TRUE;
protected $_created_column = FALSE;
protected $_updated_column = FALSE;
protected $_has_one = array(
'plan'=>array('model'=>'Service_Plugin_Adsl','foreign_key'=>'service_username','far_key'=>'service'),
);
public static $metrics = array(
'down_peak'=>'base_down_peak',
'down_offpeak'=>'base_down_offpeak',
'up_peak'=>'base_up_peak',
'up_offpeak'=>'base_up_offpeak',
'peer'=>'base_down_peak',
'internal'=>'base_down_offpeak',
);
private $_friendly = array(
'base_up_peak'=>'UP Peak',
'base_up_offpeak'=>'UP Offpeak',
'base_down_peak'=>'DOWN Peak',
'base_down_offpeak'=>'DOWN Offpeak',
'base_peer'=>'Peer',
'base_internal'=>'Internal',
);
public function rules() {
$result = parent::rules();
// We don use the "ID" field.
unset($result['id']);
return $result;
}
public function date() {
return strtotime($this->date);
}
public function friendly($name) {
return isset($this->_friendly[$name]) ? $this->_friendly[$name] : $name;
}
public function save(Validation $validation = NULL) {
// If all our values are zero/NULL, we'll ignore if our previous one was.
if ($this->total() !== 0)
return parent::save($validation);
$l = ORM::factory($this->_object_name)->where('service','=',$this->service)->where('supplier_id','=',$this->supplier_id)->find_last();
if ($l->total() !== 0)
return parent::save($validation);
// Fake our save, since the previous value is 0.
$this->_loaded = $this->_saved = TRUE;
$this->_changed = array();
$this->_original_values = $this->_object;
return $this;
}
/**
* Generate the select statements to SUM up the traffic metrics that we use
*/
public function selectsummetric() {
foreach (Model_Service_Plugin_Adsl_Traffic::$metrics as $metric=>$v)
$this->select(array("sum(".$metric.")",$metric));
return $this;
}
/**
* Total up the traffic into the metrics we use
*/
public function total() {
$result = 0;
foreach (array_keys(Model_Service_Plugin_Adsl_Traffic::$metrics) as $metric) {
if (is_null($this->$metric))
$this->$metric = 0;
$result += $this->$metric;
}
return $result;
}
/**
* Collapse our traffic data into an array as per $this->_metric
*/
public function traffic_data() {
$result = array();
foreach (Model_Service_Plugin_Adsl_Traffic::$metrics as $metric=>$v) {
if (! isset($result[$v]))
$result[$v] = 0;
$result[$v] += $this->{$metric};
}
return $result;
}
/**
* Find the last traffic record
*/
public function find_last() {
return $this->order_by('date','DESC')->order_by('time','DESC')->find();
}
}
?>