2016-08-18 20:28:22 -05:00
< ? php
// Calculate filters
2023-04-21 19:52:47 -05:00
use LibreNMS\Billing ;
2022-08-24 14:01:54 -05:00
use LibreNMS\Util\Number ;
2018-03-25 22:50:09 +02:00
$prev = ! empty ( $vars [ 'period' ]) && ( $vars [ 'period' ] == 'prev' );
2016-08-18 20:28:22 -05:00
$wheres = [];
$param = [];
if ( isset ( $searchPhrase ) && ! empty ( $searchPhrase )) {
$wheres [] = 'bills.bill_name LIKE ?' ;
$param [] = " % $searchPhrase % " ;
}
2018-03-25 22:50:09 +02:00
if ( ! empty ( $vars [ 'bill_type' ])) {
2016-08-18 20:28:22 -05:00
if ( $prev ) {
$wheres [] = 'bill_history.bill_type = ?' ;
} else {
$wheres [] = 'bill_type = ?' ;
}
2018-03-25 22:50:09 +02:00
$param [] = $vars [ 'bill_type' ];
2016-08-18 20:28:22 -05:00
}
2018-03-25 22:50:09 +02:00
if ( ! empty ( $vars [ 'state' ])) {
if ( $vars [ 'state' ] === 'under' ) {
2016-08-18 20:28:22 -05:00
if ( $prev ) {
$wheres [] = " ((bill_history.bill_type = 'cdr' AND bill_history.rate_95th <= bill_history.bill_allowed) OR (bill_history.bill_type = 'quota' AND bill_history.traf_total <= bill_history.bill_allowed)) " ;
} else {
$wheres [] = " ((bill_type = 'cdr' AND rate_95th <= bill_cdr) OR (bill_type = 'quota' AND total_data <= bill_quota)) " ;
}
2018-03-25 22:50:09 +02:00
} elseif ( $vars [ 'state' ] === 'over' ) {
2016-08-18 20:28:22 -05:00
if ( $prev ) {
$wheres [] = " ((bill_history.bill_type = 'cdr' AND bill_history.rate_95th > bill_history.bill_allowed) OR (bill_history.bill_type = 'quota' AND bill_history.traf_total > bill_allowed)) " ;
} else {
$wheres [] = " ((bill_type = 'cdr' AND rate_95th > bill_cdr) OR (bill_type = 'quota' AND total_data > bill_quota)) " ;
}
}
}
2018-04-07 15:55:28 -05:00
2016-08-18 20:28:22 -05:00
if ( $prev ) {
$select = 'SELECT bills.bill_name, bills.bill_notes, bill_history.*, bill_history.traf_total as total_data, bill_history.traf_in as total_data_in, bill_history.traf_out as total_data_out ' ;
$query = ' FROM `bills`
INNER JOIN ( SELECT bill_id , MAX ( bill_hist_id ) AS bill_hist_id FROM bill_history WHERE bill_dateto < NOW () AND bill_dateto > subdate ( NOW (), 40 ) GROUP BY bill_id ) qLastBills ON bills . bill_id = qLastBills . bill_id
INNER JOIN bill_history ON qLastBills . bill_hist_id = bill_history . bill_hist_id
' ;
} else {
$select = " SELECT bills.*,
IF ( bills . bill_type = 'CDR' , bill_cdr , bill_quota ) AS bill_allowed
" ;
$query = " FROM `bills` \n " ;
}
// Permissions check
2019-08-05 14:16:05 -05:00
if ( ! Auth :: user () -> hasGlobalRead ()) {
2016-08-18 20:28:22 -05:00
$query .= ' INNER JOIN `bill_perms` AS `BP` ON `bills`.`bill_id` = `BP`.`bill_id` ' ;
$wheres [] = '`BP`.`user_id`=?' ;
2019-08-05 14:16:05 -05:00
$param [] = Auth :: id ();
2016-08-18 20:28:22 -05:00
}
if ( sizeof ( $wheres ) > 0 ) {
$query .= 'WHERE ' . implode ( ' AND ' , $wheres ) . " \n " ;
}
$orderby = 'ORDER BY bills.bill_name' ;
$total = dbFetchCell ( " SELECT COUNT(bills.bill_id) $query " , $param );
$sql = " $select
$query " ;
if ( ! isset ( $sort ) || empty ( $sort )) {
$sort = 'bills.bill_name' ;
}
$sql .= " \n ORDER BY $sort " ;
if ( isset ( $current )) {
2023-03-13 22:32:22 +01:00
$limit_low = (( $current * $rowCount ) - $rowCount );
2016-08-18 20:28:22 -05:00
$limit_high = $rowCount ;
}
if ( $rowCount != - 1 ) {
$sql .= " LIMIT $limit_low , $limit_high " ;
}
foreach ( dbFetchRows ( $sql , $param ) as $bill ) {
if ( $prev ) {
$datefrom = $bill [ 'bill_datefrom' ];
$dateto = $bill [ 'bill_dateto' ];
} else {
2023-04-21 19:52:47 -05:00
$day_data = Billing :: getDates ( $bill [ 'bill_day' ], 0 );
2016-08-18 20:28:22 -05:00
$datefrom = $day_data [ '0' ];
$dateto = $day_data [ '1' ];
}
2022-08-24 14:01:54 -05:00
$rate_95th = Number :: formatSi ( $bill [ 'rate_95th' ], 2 , 3 , '' ) . 'bps' ;
2016-08-18 20:28:22 -05:00
$dir_95th = $bill [ 'dir_95th' ];
2023-04-21 19:52:47 -05:00
$total_data = Billing :: formatBytes ( $bill [ 'total_data' ]);
2016-08-18 20:28:22 -05:00
$rate_average = $bill [ 'rate_average' ];
2021-03-28 17:25:30 -05:00
$url = \LibreNMS\Util\Url :: generate ([ 'page' => 'bill' , 'bill_id' => $bill [ 'bill_id' ]]);
2022-08-24 14:01:54 -05:00
$used95th = Number :: formatSi ( $bill [ 'rate_95th' ], 2 , 3 , '' ) . 'bps' ;
2022-10-17 02:04:22 -05:00
$notes = htmlentities ( $bill [ 'bill_notes' ]);
2018-04-07 15:55:28 -05:00
2016-08-18 20:28:22 -05:00
if ( $prev ) {
$percent = $bill [ 'bill_percent' ];
$overuse = $bill [ 'bill_overuse' ];
} else {
}
if ( strtolower ( $bill [ 'bill_type' ]) == 'cdr' ) {
$type = 'CDR' ;
2022-08-24 14:01:54 -05:00
$allowed = Number :: formatSi ( $bill [ 'bill_allowed' ], 2 , 3 , '' ) . 'bps' ;
$in = Number :: formatSi ( $bill [ 'rate_95th_in' ], 2 , 3 , '' ) . 'bps' ;
$out = Number :: formatSi ( $bill [ 'rate_95th_out' ], 2 , 3 , '' ) . 'bps' ;
2016-08-18 20:28:22 -05:00
if ( ! $prev ) {
2022-08-24 14:01:54 -05:00
$percent = Number :: calculatePercent ( $bill [ 'rate_95th' ], $bill [ 'bill_allowed' ]);
2016-08-18 20:28:22 -05:00
$overuse = ( $bill [ 'rate_95th' ] - $bill [ 'bill_allowed' ]);
}
2018-04-07 15:55:28 -05:00
2022-08-24 14:01:54 -05:00
$overuse_formatted = Number :: formatSi ( $overuse , 2 , 3 , '' ) . 'bps' ;
2016-08-18 20:28:22 -05:00
$used = $rate_95th ;
$tmp_used = $bill [ 'rate_95th' ];
$rate_95th = " <b> $rate_95th </b> " ;
} elseif ( strtolower ( $bill [ 'bill_type' ]) == 'quota' ) {
$type = 'Quota' ;
2023-04-21 19:52:47 -05:00
$allowed = Billing :: formatBytes ( $bill [ 'bill_allowed' ]);
2016-09-20 16:23:15 +01:00
if ( ! empty ( $prev )) {
2023-04-21 19:52:47 -05:00
$in = Billing :: formatBytes ( $bill [ 'traf_in' ]);
$out = Billing :: formatBytes ( $bill [ 'traf_out' ]);
2016-09-20 16:23:15 +01:00
} else {
2023-04-21 19:52:47 -05:00
$in = Billing :: formatBytes ( $bill [ 'total_data_in' ]);
$out = Billing :: formatBytes ( $bill [ 'total_data_out' ]);
2016-09-20 16:23:15 +01:00
}
2016-08-18 20:28:22 -05:00
if ( ! $prev ) {
2022-08-24 14:01:54 -05:00
$percent = Number :: calculatePercent ( $bill [ 'total_data' ], $bill [ 'bill_allowed' ]);
2016-08-18 20:28:22 -05:00
$overuse = ( $bill [ 'total_data' ] - $bill [ 'bill_allowed' ]);
}
2018-04-07 15:55:28 -05:00
2023-04-21 19:52:47 -05:00
$overuse_formatted = Billing :: formatBytes ( $overuse );
2016-08-18 20:28:22 -05:00
$used = $total_data ;
$tmp_used = $bill [ 'total_data' ];
$total_data = " <b> $total_data </b> " ;
}
2021-12-06 09:12:24 -06:00
$background = \LibreNMS\Util\Color :: percentage ( $percent , null );
2016-08-18 20:28:22 -05:00
$right_background = $background [ 'right' ];
$left_background = $background [ 'left' ];
$overuse_formatted = (( $overuse <= 0 ) ? '-' : " <span style='color: # ${ background['left'] } ; font-weight: bold;'> $overuse_formatted </span> " );
2018-04-07 15:55:28 -05:00
2022-10-17 02:04:22 -05:00
$bill_name = " <a href=' $url '><span style='font-weight: bold;' class='interface'> " . htmlentities ( $bill [ 'bill_name' ]) . '</span></a><br />' .
2022-09-09 14:08:06 +02:00
date ( 'Y-m-d' , strtotime ( $datefrom )) . ' to ' . date ( 'Y-m-d' , strtotime ( $dateto ));
2016-08-18 20:28:22 -05:00
$bar = print_percentage_bar ( 250 , 20 , $percent , null , 'ffffff' , $background [ 'left' ], $percent . '%' , 'ffffff' , $background [ 'right' ]);
$actions = '' ;
2018-04-07 15:55:28 -05:00
2019-08-05 14:16:05 -05:00
if ( ! $prev && Auth :: user () -> hasGlobalAdmin ()) {
2021-03-28 17:25:30 -05:00
$actions .= " <a href=' " . \LibreNMS\Util\Url :: generate ([ 'page' => 'bill' , 'bill_id' => $bill [ 'bill_id' ], 'view' => 'edit' ]) .
2017-01-21 13:24:05 +02:00
" '><i class='fa fa-pencil fa-lg icon-theme' title='Edit' aria-hidden='true'></i> Edit</a> " ;
2016-08-18 20:28:22 -05:00
}
2019-07-24 06:47:05 +02:00
if ( strtolower ( $bill [ 'bill_type' ]) == 'cdr' ) {
2023-04-21 19:52:47 -05:00
$predicted = Number :: formatSi ( Billing :: getPredictedUsage ( $bill [ 'bill_day' ], $tmp_used ), 2 , 3 , '' ) . 'bps' ;
2019-07-24 06:47:05 +02:00
} elseif ( strtolower ( $bill [ 'bill_type' ]) == 'quota' ) {
2023-04-21 19:52:47 -05:00
$predicted = Billing :: formatBytes ( Billing :: getPredictedUsage ( $bill [ 'bill_day' ], $tmp_used ));
2019-07-24 06:47:05 +02:00
}
2018-04-07 15:55:28 -05:00
2016-08-18 20:28:22 -05:00
$response [] = [
'bill_name' => $bill_name ,
'notes' => $notes ,
'bill_type' => $type ,
'bill_allowed' => $allowed ,
'total_data_in' => $in ,
'total_data_out' => $out ,
'total_data' => $total_data ,
'rate_95th' => $rate_95th ,
'used' => $used ,
'overusage' => $overuse_formatted ,
'predicted' => $predicted ,
'graph' => $bar ,
'actions' => $actions ,
];
}
$output = [ 'current' => $current , 'rowCount' => $rowCount , 'rows' => $response , 'total' => $total ];
2021-03-04 07:55:41 -06:00
echo json_encode ( $output , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );