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
Raw Permalink Normal View History

2013-10-10 02:44:53 +00:00
<?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 {
2013-10-10 02:44:53 +00:00
protected $_table_name = 'service__adsl_traffic';
protected $_primary_key = 'service';
protected $_disable_wild_select = TRUE;
protected $_created_column = FALSE;
protected $_updated_column = FALSE;
2013-10-11 04:02:56 +00:00
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',
);
2013-10-10 02:44:53 +00:00
public function rules() {
$result = parent::rules();
// We don use the "ID" field.
unset($result['id']);
return $result;
}
2013-11-15 05:25:45 +00:00
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;
2013-10-10 02:44:53 +00:00
}
/**
* Find the last traffic record
*/
public function find_last() {
return $this->order_by('date','DESC')->order_by('time','DESC')->find();
}
2013-10-10 02:44:53 +00:00
}
?>