From dacdaf7267cfc4e43a4a1f436938ddc61cc1f358 Mon Sep 17 00:00:00 2001 From: Adam Amstrong Date: Sat, 31 Jul 2010 21:08:35 +0000 Subject: [PATCH] billing system fixes (make moar sexypants) git-svn-id: http://www.observium.org/svn/observer/trunk@1592 61d68cd4-352d-0410-923a-c4978735b2b8 --- html/billing-graph.php | 15 +- html/includes/authenticate.inc.php | 2 + html/includes/functions.inc.php | 20 +- html/includes/graphs/bill/auth.inc.php | 17 + html/includes/graphs/bill/bits.inc.php | 28 ++ html/index.php | 4 - html/pages/bill.inc.php | 427 ++++++++++++++----------- html/pages/bills.inc.php | 18 +- 8 files changed, 324 insertions(+), 207 deletions(-) create mode 100644 html/includes/graphs/bill/auth.inc.php create mode 100644 html/includes/graphs/bill/bits.inc.php diff --git a/html/billing-graph.php b/html/billing-graph.php index e607b2e7f2..cff1bc1847 100644 --- a/html/billing-graph.php +++ b/html/billing-graph.php @@ -11,14 +11,16 @@ if($_GET['debug']) { include("../includes/defaults.inc.php"); include("../config.php"); include("../includes/functions.php"); +include("includes/functions.inc.php"); include("includes/authenticate.inc.php"); if(!$_SESSION['authenticated']) { echo("unauthenticated"); exit; } require("includes/jpgraph/src/jpgraph.php"); include("includes/jpgraph/src/jpgraph_line.php"); include("includes/jpgraph/src/jpgraph_utils.inc.php"); -if($_GET['bill_id']){ - if(testPassword($_GET['bill_id'],$_GET['bill_code']) == "1") { +if(is_numeric($_GET['bill_id'])){ + if(bill_permitted($_GET['bill_id'])) + { $bill_id = $_GET['bill_id']; } else { echo("Unauthorised Access Prohibited."); @@ -76,7 +78,6 @@ $i = '0'; #$start = mysql_result(mysql_query("SELECT *, UNIX_TIMESTAMP(timestamp) AS formatted_date FROM bill_data WHERE bill_id = $bill_id AND timestamp >=$datefrom AND timestamp <= $dateto ORDER BY timestamp ASC LIMIT 0,1"),0); #$end = mysql_result(mysql_query("SELECT *, UNIX_TIMESTAMP(timestamp) AS formatted_date FROM bill_data WHERE bill_id = $bill_id AND timestamp >=$datefrom AND timestamp <= $dateto ORDER BY timestamp DESC LIMIT 0,1"),0); - $dur = $end - $start; $sql = "SELECT *, UNIX_TIMESTAMP(timestamp) AS formatted_date FROM bill_data WHERE bill_id = $bill_id AND timestamp >= $datefrom AND timestamp <= $dateto ORDER BY timestamp ASC"; @@ -96,7 +97,7 @@ while($row = mysql_fetch_array($data)) #@$data[] = $in_value + $out_value; #@$in_data[] = $in_value; #@$out_data[] = $out_value; - #@$ticks[] = $timestamp; +# @$ticks[] = $timestamp; #@$per_data[] = $rate_95th / 1000; #@$ave_data[] = $rate_average / 1000; @@ -111,7 +112,7 @@ while($row = mysql_fetch_array($data)) $out_data[$i] = round($iter_out * 8 / $iter_period / $div, 2); $in_data[$i] = round($iter_in * 8 / $iter_period / $div, 2); $tot_data[$i] = $out_data[$i] + $in_data[$i]; -# $ticks[$i] = date('M j g:ia', $timestamp); + $ticks[$i] = date('M j g:ia', $timestamp); $ticks[$i] = $timestamp; if($dur < 172800) { @@ -170,10 +171,10 @@ $graph->title->Set("$graph_name"); $graph->title->SetFont(FF_FONT2,FS_BOLD,10); $graph->xaxis->SetFont(FF_FONT1,FS_BOLD); -$graph->xaxis->SetTickLabels($ticks); +#$graph->xaxis->SetTickLabels($ticks); if(count($tickPositions) > 24) { - $graph->xaxis->SetTextLabelInterval(3); + $graph->xaxis->SetTextLabelInterval(6); }elseif(count($tickPositions) > 12) { $graph->xaxis->SetTextLabelInterval(2); } diff --git a/html/includes/authenticate.inc.php b/html/includes/authenticate.inc.php index 6b0b127ec6..14f08acdfc 100644 --- a/html/includes/authenticate.inc.php +++ b/html/includes/authenticate.inc.php @@ -74,6 +74,8 @@ if (isset($_SESSION['username'])) setcookie("username", $_SESSION['username'], time()+60*60*24*100, "/"); setcookie("password", $_SESSION['password'], time()+60*60*24*100, "/"); } + $permissions = permissions_cache($_SESSION['user_id']); + } elseif (isset($_SESSION['username'])) { diff --git a/html/includes/functions.inc.php b/html/includes/functions.inc.php index a8b2af26ee..c0c26f5bec 100644 --- a/html/includes/functions.inc.php +++ b/html/includes/functions.inc.php @@ -125,7 +125,8 @@ function print_graph_popup($graph_array) -function permissions_cache($user_id) { +function permissions_cache($user_id) +{ $permissions = array(); $query = mysql_query("SELECT * FROM devices_perms WHERE user_id = '".$user_id."'"); while($device = mysql_fetch_assoc($query)) { @@ -135,9 +136,26 @@ function permissions_cache($user_id) { while($port = mysql_fetch_assoc($query)) { $permissions['port'][$port['interface_id']] = 1; } + $query = mysql_query("SELECT * FROM bill_perms WHERE user_id = '".$user_id."'"); + while($bill = mysql_fetch_assoc($query)) { + $permissions['bill'][$bill['bill_id']] = 1; + } return $permissions; } +function bill_permitted($bill_id) +{ + global $_SESSION; global $permissions; + if ($_SESSION['userlevel'] >= "5") { + $allowed = TRUE; + } elseif ( $permissions['bill'][$bill_id]) { + $allowed = TRUE; + } else { + $allowed = FALSE; + } + return $allowed; + +} function interfacepermitted($interface_id, $device_id = NULL) { global $_SESSION; global $permissions; diff --git a/html/includes/graphs/bill/auth.inc.php b/html/includes/graphs/bill/auth.inc.php new file mode 100644 index 0000000000..eeda409131 --- /dev/null +++ b/html/includes/graphs/bill/auth.inc.php @@ -0,0 +1,17 @@ + diff --git a/html/includes/graphs/bill/bits.inc.php b/html/includes/graphs/bill/bits.inc.php new file mode 100644 index 0000000000..180e9229f5 --- /dev/null +++ b/html/includes/graphs/bill/bits.inc.php @@ -0,0 +1,28 @@ + diff --git a/html/index.php b/html/index.php index 1db387b6ed..ec635ed547 100755 --- a/html/index.php +++ b/html/index.php @@ -17,10 +17,6 @@ if(strpos($_SERVER['REQUEST_URI'], "debug")) { include("../includes/functions.php"); include("includes/functions.inc.php"); include("includes/authenticate.inc.php"); - if($_SESSION['authenticated']) { - # Load permissions used my devicepermitted() and interfacepermitted() - $permissions = permissions_cache($_SESSION['user_id']); - } $start = utime(); $now = time(); diff --git a/html/pages/bill.inc.php b/html/pages/bill.inc.php index 23d214a390..ab2db92d52 100644 --- a/html/pages/bill.inc.php +++ b/html/pages/bill.inc.php @@ -1,202 +1,245 @@ = 1000000) { $paidrate_text = $paid_mb . "Mbps is the CDR."; } + + $day_data = getDates($dayofmonth); + $datefrom = $day_data['0']; + $dateto = $day_data['1']; + $rate_data = getRates($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 ($rate_95th > $paid_kb) { + $over = $rate_95th - $paid_kb; + $bill_text = $over . "Kbit excess."; + $bill_color = "#cc0000"; + } else { + $under = $paid_kb - $rate_95th; + $bill_text = $under . "Kbit headroom."; + $bill_color = "#0000cc"; + } + + $fromtext = mysql_result(mysql_query("SELECT DATE_FORMAT($datefrom, '%M %D %Y')"), 0); + $totext = mysql_result(mysql_query("SELECT DATE_FORMAT($dateto, '%M %D %Y')"), 0); + $unixfrom = mysql_result(mysql_query("SELECT UNIX_TIMESTAMP('$datefrom')"), 0); + $unixto = mysql_result(mysql_query("SELECT UNIX_TIMESTAMP('$dateto')"), 0); + + echo("

+ " . $bill_name . "

"); + + print_optionbar_start(); + + if(!$_GET['optb']) { $_GET['optb'] = "details"; } + + if($_GET['optb'] == "details") { echo(""); } + echo("Details"); + if($_GET['optb'] == "details") { echo(""); } + + echo(" | "); + if($_GET['optb'] == "edit") { echo(""); } + echo("Edit"); + if($_GET['optb'] == "edit") { echo(""); } + + print_optionbar_end(); + + echo("
"); + + + if($_GET['optb'] == "edit") { + + include("pages/bill/edit.php"); + + }elseif($_GET['optb'] == "details") { + + + echo("

Billed Ports

"); + + $ports = mysql_query("SELECT * FROM `bill_ports` AS B, `ports` AS P, `devices` AS D + WHERE B.bill_id = '".$bill_id."' AND P.interface_id = B.port_id + AND D.device_id = P.device_id"); + + while ($port = mysql_fetch_array($ports)) { + + echo(generateiflink($port) . " on " . generatedevicelink($port) . "
"); + + } + + echo("

Bill Summary

"); + + if($bill_data['bill_type'] == "quota") { + + // The Customer is billed based on a pre-paid quota + + echo("

Quota Bill

"); + + $percent = round(($total_data / 1024) / $bill_data['bill_gb'] * 100, 2); + $unit = "MB"; + $total_data = round($total_data, 2); + echo("Billing Period from " . $fromtext . " to " . $totext . " +
Transferred ".formatStorage($total_data * 1024 * 1024)." of ".formatStorage($bill_data['bill_gb'] * 1024 * 1024 * 1024)." (".$percent."%) +
Average rate " . formatRates($rate_average * 1000)); + + if ($percent > 100) { $perc = "100"; } else { $perc = $percent; } + if($perc > '90') { $left_background='c4323f'; $right_background='C96A73'; + } elseif($perc > '75') { $left_background='bf5d5b'; $right_background='d39392'; + } elseif($perc > '50') { $left_background='bf875b'; $right_background='d3ae92'; + } elseif($perc > '25') { $left_background='5b93bf'; $right_background='92b7d3'; + } else { $left_background='9abf5b'; $right_background='bbd392'; } + + echo("

".print_percentage_bar (350, 20, $perc, NULL, "ffffff", $left_background, $percent . "%", "ffffff", $right_background)."

"); + + $type="&ave=yes"; + + + } elseif($bill_data['bill_type'] == "cdr") { + + // The customer is billed based on a CDR with 95th%ile overage + + echo("

CDR / 95th Bill

"); + + $unit = "kbps"; + $cdr = $bill_data['bill_cdr']; + if($rate_95th > "1000") { $rate_95th = $rate_95th / 1000; $cdr = $cdr / 1000; $unit = "Mbps"; } + if($rate_95th > "1000") { $rate_95th = $rate_95th / 1000; $cdr = $cdr / 1000; $unit = "Gps"; } + $rate_95th = round($rate_95th, 2); + + $percent = round(($rate_95th) / $cdr * 100, 2); + + $type="&95th=yes"; + + echo("" . $fromtext . " to " . $totext . " +
Measured ".$rate_95th."$unit of ".$cdr."$unit (".$percent."%)"); + + if ($percent > 100) { $perc = "100"; } else { $perc = $percent; } + if($perc > '90') { $left_background='c4323f'; $right_background='C96A73'; + } elseif($perc > '75') { $left_background='bf5d5b'; $right_background='d39392'; + } elseif($perc > '50') { $left_background='bf875b'; $right_background='d3ae92'; + } elseif($perc > '25') { $left_background='5b93bf'; $right_background='92b7d3'; + } else { $left_background='9abf5b'; $right_background='bbd392'; } + + echo("

".print_percentage_bar (350, 20, $perc, NULL, "ffffff", $left_background, $percent . "%", "ffffff", $right_background)."

"); + + + # echo("

Billing Period : " . $fromtext . " to " . $totext . "
+ # " . $paidrate_text . "
+ # " . $total_data . "MB transfered in the current billing cycle.
+ # " . $rate_average . "Kbps Average during the current billing cycle.

+ # " . $rate_95th . "Kbps @ 95th Percentile. (" . $dir_95th . ") (" . $bill_text . ") + #
+ #
"); + + } + + echo(""); + +# $bi = ""; + + $bi = ""; + + + $lastmonth = mysql_result(mysql_query("SELECT UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MONTH))"), 0); + $yesterday = mysql_result(mysql_query("SELECT UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))"), 0); + $rightnow = date(U); + +# $di = ""; + + $di = ""; + + +# $mi = ""; + + $mi = ""; -$bi_q = mysql_query("SELECT * FROM bills WHERE bill_id = $bill_id"); -$bill_data = mysql_fetch_array($bi_q); + if($null) { + + echo(" + + +
+ + + + + + From: + + + To: + + + +
+ + "); + + } + + if ($_GET[all]) { + $ai = ""; + echo("

Entire Data View

$ai"); + } elseif ($_GET[custom]) { + $cg = ""; + echo("

Custom Graph

$cg"); + } else { + echo("

Billing View

$bi

24 Hour View

$di"); + echo("

Monthly View

$mi"); +# echo("
Graph All Data (SLOW)"); + } + + } # End if details -$today = str_replace("-", "", mysql_result(mysql_query("SELECT CURDATE()"), 0)); -$yesterday = str_replace("-", "", mysql_result(mysql_query("SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY)"), 0)); -$tomorrow = str_replace("-", "", mysql_result(mysql_query("SELECT DATE_ADD(CURDATE(), INTERVAL 1 DAY)"), 0)); -$last_month = str_replace("-", "", mysql_result(mysql_query("SELECT DATE_SUB(CURDATE(), INTERVAL 1 MONTH)"), 0)); - -$rightnow = $today . date(His); -$before = $yesterday . date(His); -$lastmonth = $last_month . date(His); - -$bill_name = $bill_data['bill_name']; -$dayofmonth = $bill_data['bill_day']; -$paidrate = $bill_data['bill_paid_rate']; -$paid_kb = $paidrate / 1000; -$paid_mb = $paid_kb / 1000; - -if ($paidrate < 1000000) { $paidrate_text = $paid_kb . "Kbps is the CDR."; } -if ($paidrate >= 1000000) { $paidrate_text = $paid_mb . "Mbps is the CDR."; } - -$day_data = getDates($dayofmonth); -$datefrom = $day_data['0']; -$dateto = $day_data['1']; -$rate_data = getRates($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 ($rate_95th > $paid_kb) { - $over = $rate_95th - $paid_kb; - $bill_text = $over . "Kbit excess."; - $bill_color = "#cc0000"; } else { - $under = $paid_kb - $rate_95th; - $bill_text = $under . "Kbit headroom."; - $bill_color = "#0000cc"; -} -$fromtext = mysql_result(mysql_query("SELECT DATE_FORMAT($datefrom, '%M %D %Y')"), 0); -$totext = mysql_result(mysql_query("SELECT DATE_FORMAT($dateto, '%M %D %Y')"), 0); -$unixfrom = mysql_result(mysql_query("SELECT UNIX_TIMESTAMP('$datefrom')"), 0); -$unixto = mysql_result(mysql_query("SELECT UNIX_TIMESTAMP('$dateto')"), 0); - -echo("

-" . $bill_name . "

"); - -print_optionbar_start(); - -if(!$_GET['optb']) { $_GET['optb'] = "details"; } - -if($_GET['optb'] == "details") { echo(""); } -echo("Details"); -if($_GET['optb'] == "details") { echo(""); } - -echo(" | "); -if($_GET['optb'] == "edit") { echo(""); } -echo("Edit"); -if($_GET['optb'] == "edit") { echo(""); } - -print_optionbar_end(); - -echo("
"); - - -if($_GET['optb'] == "edit") { - - include("pages/bill/edit.php"); - -}elseif($_GET['optb'] == "details") { - - -echo("

Billed Ports

"); - -$ports = mysql_query("SELECT * FROM interfaces AS I, devices AS D, bill_ports as B WHERE B.bill_id = '$bill_id' AND B.port_id = I.interface_id AND I.device_id = D.device_id"); - -while ($port = mysql_fetch_array($ports)) { - - echo(generateiflink($port) . " on " . generatedevicelink($port) . "
"); + include("includes/error-no-perm.inc.php"); } - -echo("

Bill Summary

"); - -if($bill_data['bill_type'] == "quota") { - - // The Customer is billed based on a pre-paid quota - - $percent = round(($total_data / 1024) / $bill_data['bill_gb'] * 100, 2); - $unit = "MB"; - $total_data = round($total_data, 2); - echo("Billing Period from " . $fromtext . " to " . $totext . " -
Transferred ".formatStorage($total_data * 1024 * 1024)." of ".formatStorage($bill_data['bill_gb'] * 1024 * 1024 * 1024)." (".$percent."%) -
Average rate " . formatRates($rate_average * 1000)); - if ($percent > 100) { $percent = "100"; } - echo("

"); - - $type="&ave=yes"; - - -} elseif($bill_data['bill_type'] == "cdr") { - - // The customer is billed based on a CDR with 95th%ile overage - - $unit = "kbps"; - $cdr = $bill_data['bill_cdr']; - if($rate_95th > "1000") { $rate_95th = $rate_95th / 1000; $cdr = $cdr / 1000; $unit = "Mbps"; } - if($rate_95th > "1000") { $rate_95th = $rate_95th / 1000; $cdr = $cdr / 1000; $unit = "Gps"; } - $rate_95th = round($rate_95th, 2); - - $percent = round(($rate_95th) / $cdr * 100, 2); - - $type="&95th=yes"; - - - echo("" . $fromtext . " to " . $totext . " -
Measured ".$rate_95th."$unit of ".$cdr."$unit (".$percent."%)"); - if ($percent > 100) { $percent = "100"; } - echo("

"); - -# echo("

Billing Period : " . $fromtext . " to " . $totext . "
-# " . $paidrate_text . "
-# " . $total_data . "MB transfered in the current billing cycle.
-# " . $rate_average . "Kbps Average during the current billing cycle.

-# " . $rate_95th . "Kbps @ 95th Percentile. (" . $dir_95th . ") (" . $bill_text . ") -#
-#
"); - -} - -echo(""); - -$bi = ""; - -$lastmonth = mysql_result(mysql_query("SELECT UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MONTH))"), 0); -$yesterday = mysql_result(mysql_query("SELECT UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))"), 0); -$rightnow = date(U); - -$di = ""; - -$mi = ""; - -if($null) { - -echo(" - - -
- - - - - - From: - - - To: - - - -
- -"); - -} - - if ($_GET[all]) { - $ai = ""; - echo("

Entire Data View

$ai"); - } elseif ($_GET[custom]) { - $cg = ""; - echo("

Custom Graph

$cg"); - } else { - echo("

Billing View

$bi

24 Hour View

$di"); - #echo("

Monthly View

$li"); - #echo("
Graph All Data (SLOW)"); - } - -} # End if details - + ?> - + + diff --git a/html/pages/bills.inc.php b/html/pages/bills.inc.php index 288ede9b80..6f50b69c02 100644 --- a/html/pages/bills.inc.php +++ b/html/pages/bills.inc.php @@ -127,6 +127,10 @@ print_optionbar_end(); echo(""); $i=1; while($bill = mysql_fetch_array($query)) { + #echo("
");
+   #print_r($permissions);
+   #echo("
"); + if(bill_permitted($bill['bill_id'])) { unset($class); $day_data = getDates($bill['bill_day']); $datefrom = $day_data['0']; @@ -148,6 +152,14 @@ print_optionbar_end(); $used = formatStorage($rate_data['total_data'] * 1024 * 1024); $percent = round(($rate_data['total_data'] / ($bill['bill_gb'] * 1024)) * 100,2); } + + if ($percent > 100) { $perc = "100"; } else { $perc = $percent; } + if($perc > '90') { $left_background='c4323f'; $right_background='C96A73'; + } elseif($perc > '75') { $left_background='bf5d5b'; $right_background='d39392'; + } elseif($perc > '50') { $left_background='bf875b'; $right_background='d3ae92'; + } elseif($perc > '25') { $left_background='5b93bf'; $right_background='92b7d3'; + } else { $left_background='9abf5b'; $right_background='bbd392'; } + if(!is_integer($i/2)) { $row_colour = $list_colour_a; } else { $row_colour = $list_colour_b; } echo(" @@ -157,12 +169,12 @@ print_optionbar_end(); - - - + + "); $i++; + } ### PERMITTED } echo("
$type $allowed $used $percent%Edit Edit".print_percentage_bar (350, 20, $perc, NULL, "ffffff", $left_background, $percent . "%", "ffffff", $right_background)."Edit Edit
"); }