2011-11-02 18:26:57 +00:00
#!/usr/bin/env php
< ? php
2012-05-09 10:01:42 +00:00
/**
2013-10-28 12:01:36 -07:00
* Observium
2012-05-09 10:01:42 +00:00
*
2013-10-28 12:01:36 -07:00
* This file is part of Observium .
2012-05-09 10:01:42 +00:00
*
2013-10-28 12:01:36 -07:00
* @ package observium
* @ subpackage billing
* @ author Adam Armstrong < adama @ memetic . org >
* @ copyright ( C ) 2006 - 2012 Adam Armstrong
2012-05-09 10:01:42 +00:00
*
*/
2011-12-06 22:17:37 +00:00
2012-04-08 10:44:41 +00:00
chdir ( dirname ( $argv [ 0 ]));
2011-11-02 18:26:57 +00:00
include ( " includes/defaults.inc.php " );
include ( " config.php " );
2012-05-09 10:01:42 +00:00
include ( " includes/definitions.inc.php " );
2011-11-02 18:26:57 +00:00
include ( " includes/functions.php " );
2011-12-07 14:18:46 +00:00
$options = getopt ( " r " );
2014-01-13 17:43:58 +00:00
if ( isset ( $options [ 'r' ])) { echo ( " Clearing history table. \n " ); dbQuery ( " TRUNCATE TABLE `bill_history` " ); }
2011-12-07 14:18:46 +00:00
2011-12-12 16:10:49 +00:00
foreach ( dbFetchRows ( " SELECT * FROM `bills` ORDER BY `bill_id` " ) as $bill )
2011-12-06 22:17:37 +00:00
{
echo ( str_pad ( $bill [ 'bill_id' ] . " " . $bill [ 'bill_name' ], 30 ) . " \n " );
$i = 0 ;
2011-12-07 18:33:27 +00:00
while ( $i <= 24 )
2011-11-02 18:26:57 +00:00
{
2011-12-06 22:17:37 +00:00
unset ( $class );
unset ( $rate_data );
$day_data = getDates ( $bill [ 'bill_day' ], $i );
$datefrom = $day_data [ '0' ];
$dateto = $day_data [ '1' ];
2011-12-06 22:26:47 +00:00
$check = dbFetchRow ( " SELECT * FROM `bill_history` WHERE bill_id = ? AND bill_datefrom = ? AND bill_dateto = ? LIMIT 1 " , array ( $bill [ 'bill_id' ], $datefrom , $dateto ));
2011-12-06 22:17:37 +00:00
$period = getPeriod ( $bill [ 'bill_id' ], $datefrom , $dateto );
$date_updated = str_replace ( " - " , " " , str_replace ( " : " , " " , str_replace ( " " , " " , $check [ 'updated' ])));
2012-04-06 15:19:43 +00:00
if ( $period > 0 && $dateto > $date_updated )
2011-12-06 22:17:37 +00:00
{
2011-11-02 18:26:57 +00:00
$rate_data = getRates ( $bill [ 'bill_id' ], $datefrom , $dateto );
$rate_95th = $rate_data [ 'rate_95th' ];
$dir_95th = $rate_data [ 'dir_95th' ];
$total_data = $rate_data [ 'total_data' ];
$rate_average = $rate_data [ 'rate_average' ];
if ( $bill [ 'bill_type' ] == " cdr " )
{
$type = " CDR " ;
2011-12-06 22:17:37 +00:00
$allowed = $bill [ 'bill_cdr' ];
$used = $rate_data [ 'rate_95th' ];
2011-12-12 12:16:57 +00:00
$allowed_text = format_si ( $allowed ) . " bps " ;
$used_text = format_si ( $used ) . " bps " ;
2011-12-07 14:18:46 +00:00
$overuse = $used - $allowed ;
$overuse = (( $overuse <= 0 ) ? " 0 " : $overuse );
$percent = round (( $rate_data [ 'rate_95th' ] / $bill [ 'bill_cdr' ]) * 100 , 2 );
2011-11-02 18:26:57 +00:00
} elseif ( $bill [ 'bill_type' ] == " quota " ) {
$type = " Quota " ;
2011-12-06 22:17:37 +00:00
$allowed = $bill [ 'bill_quota' ];
$used = $rate_data [ 'total_data' ];
2011-12-12 12:16:57 +00:00
$allowed_text = format_bytes_billing ( $allowed );
2012-04-06 15:19:43 +00:00
$used_text = format_bytes_billing ( $used );
2011-12-07 14:18:46 +00:00
$overuse = $used - $allowed ;
$overuse = (( $overuse <= 0 ) ? " 0 " : $overuse );
2011-12-06 22:17:37 +00:00
$percent = round (( $rate_data [ 'total_data' ] / $bill [ 'bill_quota' ]) * 100 , 2 );
2011-11-02 18:26:57 +00:00
}
2012-04-06 15:19:43 +00:00
2011-12-12 12:16:57 +00:00
echo ( strftime ( " %x @ %X " , strtotime ( $datefrom )) . " to " . strftime ( " %x @ %X " , strtotime ( $dateto )) . " " . str_pad ( $type , 8 ) . " " . str_pad ( $allowed_text , 10 ) . " " . str_pad ( $used_text , 10 ) . " " . $percent . " % " );
2011-11-02 18:26:57 +00:00
2012-04-06 15:19:43 +00:00
if ( $i == '0' )
2011-12-06 22:17:37 +00:00
{
$update = array ( 'rate_95th' => $rate_data [ 'rate_95th' ],
2011-11-05 14:55:08 +00:00
'rate_95th_in' => $rate_data [ 'rate_95th_in' ],
'rate_95th_out' => $rate_data [ 'rate_95th_out' ],
'dir_95th' => $rate_data [ 'dir_95th' ],
'total_data' => $rate_data [ 'total_data' ],
'total_data_in' => $rate_data [ 'total_data_in' ],
'total_data_out' => $rate_data [ 'total_data_out' ],
'rate_average' => $rate_data [ 'rate_average' ],
'rate_average_in' => $rate_data [ 'rate_average_in' ],
'rate_average_out' => $rate_data [ 'rate_average_out' ],
'bill_last_calc' => array ( 'NOW()' ) );
2011-11-02 18:26:57 +00:00
2011-12-06 22:17:37 +00:00
dbUpdate ( $update , 'bills' , '`bill_id` = ?' , array ( $bill [ 'bill_id' ]));
echo ( " Updated! " );
}
2012-04-06 15:19:43 +00:00
if ( $check [ 'bill_id' ] == $bill [ 'bill_id' ])
{
2011-12-06 22:17:37 +00:00
$update = array ( 'rate_95th' => $rate_data [ 'rate_95th' ],
'rate_95th_in' => $rate_data [ 'rate_95th_in' ],
'rate_95th_out' => $rate_data [ 'rate_95th_out' ],
'dir_95th' => $rate_data [ 'dir_95th' ],
'rate_average' => $rate_data [ 'rate_average' ],
'rate_average_in' => $rate_data [ 'rate_average_in' ],
'rate_average_out' => $rate_data [ 'rate_average_out' ],
'traf_total' => $rate_data [ 'total_data' ],
'traf_in' => $rate_data [ 'total_data_in' ],
'traf_out' => $rate_data [ 'total_data_out' ],
'bill_used' => $used ,
'bill_overuse' => $overuse ,
'bill_percent' => $percent ,
'updated' => array ( 'NOW()' ));
2011-12-12 16:10:49 +00:00
dbUpdate ( $update , 'bill_history' , '`bill_hist_id` = ?' , array ( $check [ 'bill_hist_id' ]));
2011-12-06 22:17:37 +00:00
echo ( " Updated history! " );
} else {
$update = array ( 'rate_95th' => $rate_data [ 'rate_95th' ],
2011-11-05 14:55:08 +00:00
'rate_95th_in' => $rate_data [ 'rate_95th_in' ],
'rate_95th_out' => $rate_data [ 'rate_95th_out' ],
'dir_95th' => $rate_data [ 'dir_95th' ],
2011-11-05 15:00:09 +00:00
'rate_average' => $rate_data [ 'rate_average' ],
'rate_average_in' => $rate_data [ 'rate_average_in' ],
'rate_average_out' => $rate_data [ 'rate_average_out' ],
2011-11-05 14:55:08 +00:00
'traf_total' => $rate_data [ 'total_data' ],
'traf_in' => $rate_data [ 'total_data_in' ],
'traf_out' => $rate_data [ 'total_data_out' ],
2011-12-06 22:17:37 +00:00
'bill_datefrom' => $datefrom ,
'bill_dateto' => $dateto ,
2011-11-05 14:55:08 +00:00
'bill_type' => $type ,
'bill_allowed' => $allowed ,
'bill_used' => $used ,
'bill_overuse' => $overuse ,
'bill_percent' => $percent ,
2012-04-06 15:19:43 +00:00
'bill_datefrom' => $datefrom ,
2011-12-06 22:17:37 +00:00
'bill_dateto' => $dateto ,
2011-11-05 14:55:08 +00:00
'bill_id' => $bill [ 'bill_id' ] );
2011-12-06 22:26:47 +00:00
dbInsert ( $update , 'bill_history' );
2011-12-06 22:17:37 +00:00
echo ( " Generated history! " );
2011-11-05 14:55:08 +00:00
}
2011-12-06 22:17:37 +00:00
echo ( " \n \n " );
}
$i ++ ;
2011-11-02 18:26:57 +00:00
}
2011-12-06 22:17:37 +00:00
}
2011-11-02 18:26:57 +00:00
?>