billing improvements from codekiller.

git-svn-id: http://www.observium.org/svn/observer/trunk@2715 61d68cd4-352d-0410-923a-c4978735b2b8
This commit is contained in:
Adam Amstrong
2011-11-05 14:55:08 +00:00
parent f3de72be89
commit f7eb392298
9 changed files with 508 additions and 147 deletions

View File

@@ -8,9 +8,13 @@ include("includes/functions.php");
foreach (dbFetchRows("SELECT * FROM `bills` ORDER BY `bill_name`") as $bill)
{
unset($class);
unset($rate_data);
$day_data = getDates($bill['bill_day']);
$datefrom = $day_data['0'];
$dateto = $day_data['1'];
$datefrom_lp = $day_data['2'];
$dateto_lp = $day_data['3'];
$check_lp = dbFetchRow("SELECT * FROM `bill_history` WHERE bill_id = ? AND bill_datefrom = ? AND bill_dateto = ? LIMIT 1", array($bill['bill_id'], $datefrom_lp, $dateto_lp));
$rate_data = getRates($bill['bill_id'],$datefrom,$dateto);
$rate_95th = $rate_data['rate_95th'];
$dir_95th = $rate_data['dir_95th'];
@@ -25,32 +29,82 @@ include("includes/functions.php");
$percent = round(($rate_data['rate_95th'] / $bill['bill_cdr']) * 100,2);
} elseif ($bill['bill_type'] == "quota") {
$type = "Quota";
$allowed = formatStorage($bill['bill_gb']* 1024 * 1024 * 1024);
$allowed = formatStorage($bill['bill_gb'] * 1024 * 1024 * 1024);
$used = formatStorage($rate_data['total_data'] * 1024 * 1024);
$percent = round(($rate_data['total_data'] / ($bill['bill_gb'] * 1024)) * 100,2);
}
echo(str_pad($bill['bill_id']." ".$bill['bill_name'], 30)." ".str_pad($type,8)." ".str_pad($allowed,10)." ".str_pad($used,10)." ".$percent."%");
echo(str_pad($bill['bill_id']." ".$bill['bill_name'], 30)." ".str_pad($type,8)." ".str_pad($allowed,10)." ".str_pad($used,10)." ".$percent."%");
$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'],
'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()') );
$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'],
'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()') );
dbUpdate($update, 'bills', '`bill_id` = ?', array($bill['bill_id']));
# print_r($rate_data);
echo("\n");
dbUpdate($update, 'bills', '`bill_id` = ?', array($bill['bill_id']));
if (empty($check_lp['id'])) {
unset($rate_data);
$rate_data = getRates($bill['bill_id'],$datefrom_lp,$dateto_lp);
$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'];
//$total_data = formatStorage($rate_data['total_data'] * 1024 * 1024);
//$total_data_in = formatStorage($rate_data['total_data_in'] * 1024 * 1024);
//$total_data_out = formatStorage($rate_data['total_data_out'] * 1024 * 1024);
//$rate_average = $rate_data['rate_average'];
if ($bill['bill_type'] == "cdr")
{
$type = "CDR";
//$allowed = formatRates($bill['bill_cdr'] * 1000);
//$used = formatRates($rate_data['rate_95th'] * 1000);
//$overuse = formatRates(($rate_data['rate_95th'] - $bill['bill_cdr'])* 1000);
$allowed = $bill['bill_cdr'];
$used = $rate_data['rate_95th'];
$overuse = $used - $allowed;
$overuse = (($overuse <= 0) ? "0" : $overuse);
$percent = round(($rate_data['rate_95th'] / $bill['bill_cdr']) * 100,2);
} elseif ($bill['bill_type'] == "quota") {
$type = "Quota";
//$allowed = formatStorage($bill['bill_gb'] * 1024 * 1024 * 1024);
//$used = formatStorage($rate_data['total_data'] * 1024 * 1024);
//$overuse = formatStorage(($rate_data['total_data'] - ($bill['bill_gb'] * 1024)) * 1024 * 1024);
$allowed = $bill['bill_gb'] * 1024;
$used = $rate_data['total_data'];
$overuse = $used - $allowed;
$overuse = (($overuse <= 0) ? "0" : $overuse);
$percent = round(($rate_data['total_data'] / ($bill['bill_gb'] * 1024)) * 100,2);
}
$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'],
'traf_total' => $rate_data['total_data'],
'traf_in' => $rate_data['total_data_in'],
'traf_out' => $rate_data['total_data_out'],
'bill_datefrom' => $datefrom_lp,
'bill_dateto' => $dateto_lp,
'bill_type' => $type,
'bill_allowed' => $allowed,
'bill_used' => $used,
'bill_overuse' => $overuse,
'bill_percent' => $percent,
'bill_id' => $bill['bill_id'] );
# print_r($update);
dbInsert($update, 'bill_history');
echo(" * Generated historical data from ".strftime("%x @ %X", strtotime($datefrom_lp))." to ".strftime("%x @ %X", strtotime($dateto_lp))."\n");
}
#print_r($rate_data);
echo("\n");
}
?>

View File

@@ -1163,3 +1163,31 @@ CREATE TABLE IF NOT EXISTS `vrfs` (
PRIMARY KEY (`vrf_id`),
KEY `device_id` (`device_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Tabel structuur voor tabel `bill_history`
--
CREATE TABLE IF NOT EXISTS `bill_history` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bill_id` int(11) NOT NULL,
`bill_datefrom` datetime NOT NULL,
`bill_dateto` datetime NOT NULL,
`bill_type` text NOT NULL,
`bill_allowed` int(11) NOT NULL,
`bill_used` int(11) NOT NULL,
`bill_overuse` int(11) NOT NULL,
`bill_percent` DECIMAL(5,2) NOT NULL,
`rate_95th_in` int(11) NOT NULL,
`rate_95th_out` int(11) NOT NULL,
`rate_95th` int(11) NOT NULL,
`dir_95th` varchar(3) NOT NULL,
`traf_in` int(11) NOT NULL,
`traf_out` int(11) NOT NULL,
`traf_total` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `bill_id` (`bill_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

View File

@@ -19,3 +19,6 @@ ALTER TABLE `bills` ADD `rate_average_in` int(11) NOT NULL;
ALTER TABLE `bills` ADD `rate_average_out` int(11) NOT NULL;
ALTER TABLE `bills` ADD `rate_average` int(11) NOT NULL;
ALTER TABLE `bills` ADD `bill_last_calc` datetime NOT NULL;
CREATE TABLE IF NOT EXISTS `bill_history` ( `id` int(11) NOT NULL AUTO_INCREMENT, `bill_id` int(11) NOT NULL, `bill_datefrom` datetime NOT NULL, `bill_dateto` datetime NOT NULL, `bill_type` text NOT NULL, `bill_allowed` int(11) NOT NULL, `bill_used` int(11) NOT NULL, `bill_overuse` int(11) NOT NULL, `bill_percent` decimal(5,2) NOT NULL, `rate_95th_in` int(11) NOT NULL, `rate_95th_out` int(11) NOT NULL, `rate_95th` int(11) NOT NULL, `dir_95th` varchar(3) NOT NULL, `traf_in` int(11) NOT NULL, `traf_out` int(11) NOT NULL, `traf_total` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `bill_id` (`bill_id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

View File

@@ -9,7 +9,8 @@ if ($_SESSION['userlevel'] == "10")
if (bill_permitted($bill_id))
{
$bill_data = dbFetchRow("SELECT * FROM bills WHERE bill_id = ?", array($bill_id));
$bill_data = dbFetchRow("SELECT * FROM bills WHERE bill_id = ? LIMIT 1", array($bill_id));
$bill_name = $bill_data['bill_name'];
$today = str_replace("-", "", dbFetchCell("SELECT CURDATE()"));
$yesterday = str_replace("-", "", dbFetchCell("SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY)"));
@@ -68,12 +69,20 @@ if (bill_permitted($bill_id))
$unix_prev_from = dbFetchCell("SELECT UNIX_TIMESTAMP('$lastfrom')");
$unix_prev_to = dbFetchCell("SELECT UNIX_TIMESTAMP('$lastto')");
# Speeds up loading for other included pages by setting it before progessing of mysql data!
echo("<font face=\"Verdana, Arial, Sans-Serif\"><h2>
Bill : " . $bill_name . "</h2>");
print_optionbar_start();
echo("<span style='font-weight: bold;'>Bill</span> &#187; ");
if (!$_GET['optb']) { $_GET['optb'] = "details"; }
if ($_GET['opta'] == "admin_history") { $_GET['optb'] = $_GET['opta']; }
if ($_GET['optb'] == "basic") { echo("<span class='pagemenu-selected'>"); }
echo("<a href='bill/".$bill_id."/basic/'>Quick Graphs</a>");
@@ -85,8 +94,19 @@ if (bill_permitted($bill_id))
echo("<a href='bill/".$bill_id."/details/'>Accurate Graphs</a>");
if ($_GET['optb'] == "details") { echo("</span>"); }
echo(" | ");
if ($_GET['optb'] == "history") { echo("<span class='pagemenu-selected'>"); }
echo("<a href='bill/".$bill_id."/history/'>Historical Usage</a>");
if ($_GET['optb'] == "history") { echo("</span>"); }
if ($_SESSION['userlevel'] == "10")
{
# echo(" | ");
# if ($_GET['optb'] == "admin_history") { echo("<span class='pagemenu-selected'>"); }
# echo("<a href='bills/history/'>Previous Billing Period</a>");
# if ($_GET['optb'] == "admin_history") { echo("</span>"); }
echo(" | ");
if ($_GET['optb'] == "edit") { echo("<span class='pagemenu-selected'>"); }
echo("<a href='bill/".$bill_id."/edit/'>Edit</a>");
@@ -98,9 +118,10 @@ if (bill_permitted($bill_id))
if ($_GET['optb'] == "delete") { echo("</span>"); }
}
print_optionbar_end();
echo("<div style='font-weight: bold; float: right;'><a href='bills/'>Back to Bills</a></div>");
# echo("<table width=715 border=0 cellspace=0 cellpadding=0><tr><td>");
print_optionbar_end();
if ($_GET['optb'] == "edit" && $_SESSION['userlevel'] == "10")
{
@@ -110,127 +131,188 @@ if (bill_permitted($bill_id))
{
include("pages/bill/delete.inc.php");
}
elseif ($_GET['optb'] == "details" || $_GET['optb'] == "basic")
elseif ($_GET['optb'] == "history")
{
echo("<h3>Billed Ports</h3>");
$ports = dbFetchRows("SELECT * FROM `bill_ports` AS B, `ports` AS P, `devices` AS D
WHERE B.bill_id = ? AND P.interface_id = B.port_id
AND D.device_id = P.device_id", array($bill_id));
foreach ($ports as $port)
{
echo(generate_port_link($port) . " on " . generate_device_link($port) . "<br />");
include("pages/bill/history.inc.php");
}
elseif ($_GET['optb'] == "details" || $_GET['optb'] == "basic") {
echo("<h3>Bill Summary</h3>");
$bill_data = dbFetchRow("SELECT * FROM bills WHERE bill_id = ?", array($bill_id));
if ($bill_data['bill_type'] == "quota")
{
// The Customer is billed based on a pre-paid quota
$today = str_replace("-", "", dbFetchCell("SELECT CURDATE()"));
$tomorrow = str_replace("-", "", dbFetchCell("SELECT DATE_ADD(CURDATE(), INTERVAL 1 DAY)"));
$last_month = str_replace("-", "", dbFetchCell("SELECT DATE_SUB(CURDATE(), INTERVAL 1 MONTH)"));
echo("<h4>Quota Bill</h4>");
$rightnow = $today . date(His);
$before = $yesterday . date(His);
$lastmonth = $last_month . date(His);
$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 . "
<br />Transferred ".formatStorage($total_data * 1024 * 1024)." of ".formatStorage($bill_data['bill_gb'] * 1024 * 1024 * 1024)." (".$percent."%)
<br />Average rate " . formatRates($rate_average * 1000));
# $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;
$background = get_percentage_colours($percent);
$right_background = $background['right'];
$left_background = $background['left'];
if ($paidrate < 1000000) { $paidrate_text = $paid_kb . "Kbps is the CDR."; }
if ($paidrate >= 1000000) { $paidrate_text = $paid_mb . "Mbps is the CDR."; }
echo("<p>".print_percentage_bar (350, 20, $perc, NULL, "ffffff", $left_background, $percent . "%", "ffffff", $right_background)."</p>");
$day_data = getDates($dayofmonth);
$type="&amp;ave=yes";
}
elseif ($bill_data['bill_type'] == "cdr")
{
// The customer is billed based on a CDR with 95th%ile overage
$datefrom = $day_data['0'];
$dateto = $day_data['1'];
echo("<h4>CDR / 95th Bill</h4>");
$lastfrom = $day_data['2'];
$lastto = $day_data['3'];
$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);
# $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'];
$rate_95th = $bill_data['rate_95th'];
$dir_95th = $bill_data['dir_95th'];
$total_data = $bill_data['total_data'];
$rate_average = $bill_data['rate_average'];
$percent = round(($rate_95th) / $cdr * 100, 2);
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";
}
$type="&amp;95th=yes";
$fromtext = dbFetchCell("SELECT DATE_FORMAT($datefrom, '%M %D %Y')");
$totext = dbFetchCell("SELECT DATE_FORMAT($dateto, '%M %D %Y')");
$unixfrom = dbFetchCell("SELECT UNIX_TIMESTAMP('$datefrom')");
$unixto = dbFetchCell("SELECT UNIX_TIMESTAMP('$dateto')");
echo("<strong>" . $fromtext . " to " . $totext . "</strong>
<br />Measured ".$rate_95th."$unit of ".$cdr."$unit (".$percent."%)");
$unix_prev_from = dbFetchCell("SELECT UNIX_TIMESTAMP('$lastfrom')");
$unix_prev_to = dbFetchCell("SELECT UNIX_TIMESTAMP('$lastto')");
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("<h3>Billed Ports</h3>");
echo("<p>".print_percentage_bar (350, 20, $perc, NULL, "ffffff", $left_background, $percent . "%", "ffffff", $right_background)."</p>");
$ports = dbFetchRows("SELECT * FROM `bill_ports` AS B, `ports` AS P, `devices` AS D
WHERE B.bill_id = ? AND P.interface_id = B.port_id
AND D.device_id = P.device_id", array($bill_id));
# echo("<p>Billing Period : " . $fromtext . " to " . $totext . "<br />
# " . $paidrate_text . " <br />
# " . $total_data . "MB transfered in the current billing cycle. <br />
# " . $rate_average . "Kbps Average during the current billing cycle. </p>
# <font face=\"Trebuchet MS, Verdana, Arial, Sans-Serif\" color=" . $bill_color . "><B>" . $rate_95th . "Kbps @ 95th Percentile.</b> (" . $dir_95th . ") (" . $bill_text . ")</font>
# </td><td><img src=\"images/billing-key.png\"></td></tr></table>
# <br />");
foreach ($ports as $port)
{
echo(generate_port_link($port) . " on " . generate_device_link($port) . "<br />");
}
}
echo("<h3>Bill Summary</h3>");
$lastmonth = dbFetchCell("SELECT UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MONTH))");
$yesterday = dbFetchCell("SELECT UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))");
$rightnow = date(U);
if ($bill_data['bill_type'] == "quota")
{
// The Customer is billed based on a pre-paid quota
if ($_GET['optb'] == "details") {
echo("<h4>Quota Bill</h4>");
$bi = "<img src='billing-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$bi = $bi . "&amp;from=" . $unixfrom . "&amp;to=" . $unixto;
$bi = $bi . "&amp;x=800&amp;y=250";
$bi = $bi . "$type'>";
$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 . "
<br />Transferred ".formatStorage($total_data * 1024 * 1024)." of ".formatStorage($bill_data['bill_gb'] * 1024 * 1024 * 1024)." (".$percent."%)
<br />Average rate " . formatRates($rate_average * 1000));
$li = "<img src='billing-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$li = $li . "&amp;from=" . $unix_prev_from . "&amp;to=" . $unix_prev_to . "&amp;x=800&amp;y=250";
$li = $li . "$type'>";
$background = get_percentage_colours($percent);
$right_background = $background['right'];
$left_background = $background['left'];
$di = "<img src='billing-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$di = $di . "&amp;from=" . $config['time']['day'] . "&amp;to=" . $config['time']['now'] . "&amp;x=800&amp;y=250";
$di = $di . "$type'>";
echo("<p>".print_percentage_bar (350, 20, $perc, NULL, "ffffff", $left_background, $percent . "%", "ffffff", $right_background)."</p>");
$mi = "<img src='billing-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$mi = $mi . "&amp;from=" . $lastmonth . "&amp;to=" . $rightnow . "&amp;x=800&amp;y=250";
$mi = $mi . "$type'>";
$type="&amp;ave=yes";
}
elseif ($bill_data['bill_type'] == "cdr")
{
// The customer is billed based on a CDR with 95th%ile overage
} else {
echo("<h4>CDR / 95th Bill</h4>");
$bi = "<img src='graph.php?type=bill_bits&amp;id=" . $bill_id;
$bi .= "&amp;from=" . $unixfrom . "&amp;to=" . $unixto;
$bi .= "&amp;width=715&amp;height=200&amp;total=1'>";
$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);
$li = "<img src='graph.php?type=bill_bits&amp;id=" . $bill_id;
$li .= "&amp;from=" . $unix_prev_from . "&amp;to=" . $unix_prev_to;
$li .= "&amp;width=715&amp;height=200&amp;total=1'>";
$percent = round(($rate_95th) / $cdr * 100, 2);
$di = "<img src='graph.php?type=bill_bits&amp;id=" . $bill_id;
$di .= "&amp;from=" . $config['time']['day'] . "&amp;to=" . $config['time']['now'];
$di .= "&amp;width=715&amp;height=200&amp;total=1'>";
$type="&amp;95th=yes";
$mi = "<img src='graph.php?type=bill_bits&amp;id=" . $bill_id;
$mi .= "&amp;from=" . $lastmonth . "&amp;to=" . $rightnow;
$mi .= "&amp;width=715&amp;height=200&amp;total=1'>";
echo("<strong>" . $fromtext . " to " . $totext . "</strong>
<br />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'; }
if ($null)
{
echo("
echo("<p>".print_percentage_bar (350, 20, $perc, NULL, "ffffff", $left_background, $percent . "%", "ffffff", $right_background)."</p>");
# echo("<p>Billing Period : " . $fromtext . " to " . $totext . "<br />
# " . $paidrate_text . " <br />
# " . $total_data . "MB transfered in the current billing cycle. <br />
# " . $rate_average . "Kbps Average during the current billing cycle. </p>
# <font face=\"Trebuchet MS, Verdana, Arial, Sans-Serif\" color=" . $bill_color . "><B>" . $rate_95th . "Kbps @ 95th Percentile.</b> (" . $dir_95th . ") (" . $bill_text . ")</font>
# </td><td><img src=\"images/billing-key.png\"></td></tr></table>
# <br />");
}
$lastmonth = dbFetchCell("SELECT UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MONTH))");
$yesterday = dbFetchCell("SELECT UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))");
$rightnow = date(U);
if ($_GET['optb'] == "details") {
$bi = "<img src='billing-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$bi = $bi . "&amp;from=" . $unixfrom . "&amp;to=" . $unixto;
$bi = $bi . "&amp;x=800&amp;y=250";
$bi = $bi . "$type'>";
$li = "<img src='billing-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$li = $li . "&amp;from=" . $unix_prev_from . "&amp;to=" . $unix_prev_to . "&amp;x=800&amp;y=250";
$li = $li . "$type'>";
$di = "<img src='billing-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$di = $di . "&amp;from=" . $config['time']['day'] . "&amp;to=" . $config['time']['now'] . "&amp;x=800&amp;y=250";
$di = $di . "$type'>";
$mi = "<img src='billing-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$mi = $mi . "&amp;from=" . $lastmonth . "&amp;to=" . $rightnow . "&amp;x=800&amp;y=250";
$mi = $mi . "$type'>";
} else {
$bi = "<img src='graph.php?type=bill_bits&amp;id=" . $bill_id;
$bi .= "&amp;from=" . $unixfrom . "&amp;to=" . $unixto;
$bi .= "&amp;width=715&amp;height=200&amp;total=1'>";
$li = "<img src='graph.php?type=bill_bits&amp;id=" . $bill_id;
$li .= "&amp;from=" . $unix_prev_from . "&amp;to=" . $unix_prev_to;
$li .= "&amp;width=715&amp;height=200&amp;total=1'>";
$di = "<img src='graph.php?type=bill_bits&amp;id=" . $bill_id;
$di .= "&amp;from=" . $config['time']['day'] . "&amp;to=" . $config['time']['now'];
$di .= "&amp;width=715&amp;height=200&amp;total=1'>";
$mi = "<img src='graph.php?type=bill_bits&amp;id=" . $bill_id;
$mi .= "&amp;from=" . $lastmonth . "&amp;to=" . $rightnow;
$mi .= "&amp;width=715&amp;height=200&amp;total=1'>";
}
if ($null)
{
echo("
<script type='text/javascript' src='js/calendarDateInput.js'>
</script>
@@ -251,32 +333,32 @@ if (bill_permitted($bill_id))
");
}
}
if ($_GET['all'])
{
$ai = "<img src=\"billing-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$ai .= "&amp;from=0&amp;to=" . $rightnow;
$ai .= "&amp;x=715&amp;y=250";
$ai .= "&amp;count=60\">";
echo("<h3>Entire Data View</h3>$ai");
}
elseif ($_GET['custom'])
{
$cg = "<img src=\"billing-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$cg .= "&amp;from=" . $_GET['fromdate'] . "000000&amp;to=" . $_GET['todate'] . "235959";
$cg .= "&amp;x=715&amp;y=250";
$cg .= "&amp;count=60\">";
echo("<h3>Custom Graph</h3>$cg");
}
else
{
echo("<h3>Billing View</h3>$bi");
echo("<h3>Previous Bill View</h3>$li");
echo("<h3>24 Hour View</h3>$di");
echo("<h3>Monthly View</h3>$mi");
# echo("<br /><a href=\"rate.php?" . $_SERVER['QUERY_STRING'] . "&amp;all=yes\">Graph All Data (SLOW)</a>");
}
if ($_GET['all'])
{
$ai = "<img src=\"billing-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$ai .= "&amp;from=0&amp;to=" . $rightnow;
$ai .= "&amp;x=715&amp;y=250";
$ai .= "&amp;count=60\">";
echo("<h3>Entire Data View</h3>$ai");
}
elseif ($_GET['custom'])
{
$cg = "<img src=\"billing-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$cg .= "&amp;from=" . $_GET['fromdate'] . "000000&amp;to=" . $_GET['todate'] . "235959";
$cg .= "&amp;x=715&amp;y=250";
$cg .= "&amp;count=60\">";
echo("<h3>Custom Graph</h3>$cg");
}
else
{
echo("<h3>Billing View</h3>$bi");
echo("<h3>Previous Bill View</h3>$li");
echo("<h3>24 Hour View</h3>$di");
echo("<h3>Monthly View</h3>$mi");
# echo("<br /><a href=\"rate.php?" . $_SERVER['QUERY_STRING'] . "&amp;all=yes\">Graph All Data (SLOW)</a>");
}
} # End if details
}
else

View File

@@ -8,6 +8,7 @@ if ($_POST['action'] == "delete_bill" && $_POST['confirm'] == "confirm")
dbDelete('port_out_measurements', '`port_id` = ?', array($port_data['bill_id']));
}
dbDelete('bill_history', '`bill_id` = ?', array($bill_id));
dbDelete('bill_ports', '`bill_id` = ?', array($bill_id));
dbDelete('bill_data', '`bill_id` = ?', array($bill_id));
dbDelete('bill_perms', '`bill_id` = ?', array($bill_id));

View File

@@ -0,0 +1,73 @@
<?php
$pagetitle[] = "Historical Usage";
$i=0;
echo("<table border=0 cellspacing=0 cellpadding=5 class=devicetable width=100%>
<tr style=\"font-weight: bold; \">
<td width=\"7\"></td>
<td width=\"220\">Period</td>
<td>Type</td>
<td>Allowed</td>
<td>Inbound</td>
<td>Outbound</td>
<td>Total</td>
<td>95 percentile</td>
<td style=\"text-align: center;\">Overusage</td>
<td width=\"250\"></td>
</tr>");
foreach (dbFetchRows("SELECT * FROM `bill_history` WHERE `bill_id` = ? ORDER BY `bill_datefrom` AND `bill_dateto` DESC LIMIT 24", array($bill_id)) as $history)
{
if (bill_permitted($history['bill_id']))
{
unset($class);
$datefrom = $history['bill_datefrom'];
$dateto = $history['bill_dateto'];
$type = $history['bill_type'];
$percent = $history['bill_percent'];
$dir_95th = $history['dir_95th'];
$rate_95th = formatRates($history['rate_95th'] * 1000);
$total_data = formatStorage($history['traf_total'] * 1024 * 1024);
$background = get_percentage_colours($percent);
$row_colour = ((!is_integer($i/2)) ? $list_colour_a : $list_colour_b);
if ($type == "CDR")
{
$allowed = formatRates($history['bill_allowed'] * 1000);
$used = formatRates($history['rate_95th'] * 1000);
$in = formatRates($history['rate_95th_in'] * 1000);
$out = formatRates($history['rate_95th_out'] * 1000);
$overuse = (($history['bill_overuse'] <= 0) ? "-" : "<span style=\"color: #".$background['left']."; font-weight: bold;\">".formatRates($history['bill_overuse'] * 1000)."</span>");
} elseif ($type == "Quota") {
$allowed = formatStorage($history['bill_allowed'] * 1024 * 1024);
$used = formatStorage($history['total_data'] * 1024 * 1024);
$in = formatStorage($history['traf_in'] * 1024 * 1024);
$out = formatStorage($history['traf_out'] * 1024 * 1024);
$overuse = (($history['bill_overuse'] <= 0) ? "-" : "<span style=\"color: #".$background['left']."; font-weight: bold;\">".formatStorage($history['bill_overuse'] * 1024 * 1024)."</span>");
}
$total_data = (($type == "Quota") ? "<b>".$total_data."</b>" : $total_data);
$rate_95th = (($type == "CDR") ? "<b>".$rate_95th."</b>" : $rate_95th);
echo("
<tr style=\"background: $row_colour;\">
<td></td>
<td><span style=\"font-weight: bold;\" class=\"interface\">from ".strftime("%x", strtotime($datefrom))." to ".strftime("%x", strtotime($dateto))."</span></td>
<td>$type</td>
<td>$allowed</td>
<td>$in</td>
<td>$out</td>
<td>$total_data</td>
<td>$rate_95th</td>
<td style=\"text-align: center;\">$overuse</td>
<td>".print_percentage_bar (250, 20, $perc, NULL, "ffffff", $background['left'], $percent . "%", "ffffff", $background['right'])."</td>
</tr>");
$i++;
} ### PERMITTED
}
echo("</table>");
?>

View File

@@ -0,0 +1,78 @@
<?php
$pagetitle[] = "Previous Billing Period";
$i=1;
echo("<table border=0 cellspacing=0 cellpadding=5 class=devicetable width=100%>
<tr style=\"font-weight: bold; border-bottom: ".$list_highlight." 1px solid;\">
<td width=\"7\"></td>
<td width=\"250\">Billing name</td>
<td>Type</td>
<td>Allowed</td>
<td>Inbound</td>
<td>Outbound</td>
<td>Total</td>
<td>95 percentile</td>
<td style=\"text-align: center;\">Overusage</td>
<td width=\"250\"></td>
</tr>");
foreach (dbFetchRows("SELECT * FROM `bills` ORDER BY `bill_name`") as $bill)
{
if (bill_permitted($bill['bill_id']))
{
$day_data = getDates($bill['bill_day']);
$datefrom = $day_data['2'];
$dateto = $day_data['3'];
// foreach (dbFetchRows("SELECT * FROM `bill_history` ORDER BY `bill_datefrom` AND `bill_dateto` DESC LIMIT 24") as $history)
foreach (dbFetchRows("SELECT * FROM `bill_history` WHERE `bill_id` = ? AND `bill_datefrom` = ? AND `bill_dateto` = ? ORDER BY `bill_datefrom` AND `bill_dateto` LIMIT 1", array($bill['bill_id'], $datefrom, $dateto)) as $history)
{
unset($class);
$type = $history['bill_type'];
$percent = $history['bill_percent'];
$dir_95th = $history['dir_95th'];
$rate_95th = formatRates($history['rate_95th'] * 1000);
$total_data = formatStorage($history['traf_total'] * 1024 * 1024);
$background = get_percentage_colours($percent);
$row_colour = ((!is_integer($i/2)) ? $list_colour_a : $list_colour_b);
if ($type == "CDR")
{
$allowed = formatRates($history['bill_allowed'] * 1000);
$used = formatRates($history['rate_95th'] * 1000);
$in = formatRates($history['rate_95th_in'] * 1000);
$out = formatRates($history['rate_95th_out'] * 1000);
$overuse = (($history['bill_overuse'] <= 0) ? "-" : "<span style=\"color: #".$background['left']."; font-weight: bold;\">".formatRates($history['bill_overuse'] * 1000)."</span>");
} elseif ($type == "Quota") {
$allowed = formatStorage($history['bill_allowed'] * 1024 * 1024);
$used = formatStorage($history['total_data'] * 1024 * 1024);
$in = formatStorage($history['traf_in'] * 1024 * 1024);
$out = formatStorage($history['traf_out'] * 1024 * 1024);
$overuse = (($history['bill_overuse'] <= 0) ? "-" : "<span style=\"color: #".$background['left']."; font-weight: bold;\">".formatStorage($history['bill_overuse'] * 1024 * 1024)."</span>");
}
$total_data = (($type == "Quota") ? "<b>".$total_data."</b>" : $total_data);
$rate_95th = (($type == "CDR") ? "<b>".$rate_95th."</b>" : $rate_95th);
echo("
<tr style=\"background: $row_colour;\">
<td></td>
<td><a href=\"bill/".$bill['bill_id']."/\"><span style=\"font-weight: bold;\" class=\"interface\">".$bill['bill_name']."</a></span><br />from ".strftime("%x", strtotime($datefrom))." to ".strftime("%x", strtotime($dateto))."</td>
<td>$type</td>
<td>$allowed</td>
<td>$in</td>
<td>$out</td>
<td>$total_data</td>
<td>$rate_95th</td>
<td style=\"text-align: center;\">$overuse</td>
<td>".print_percentage_bar (250, 20, $perc, NULL, "ffffff", $background['left'], $percent . "%", "ffffff", $background['right'])."</td>
</tr>");
$i++;
} ### PERMITTED
}
}
echo("</table>");
?>

View File

@@ -15,9 +15,15 @@ if ($_POST['addbill'] == "yes")
$pagetitle[] = "Billing";
echo("<meta http-equiv='refresh' content='10000'>");
if ($_GET['opta'] == "add")
if ($_GET['opta'] == "history")
{
include("pages/bill/search.inc.php");
include("pages/bill/pmonth.inc.php");
}
elseif ($_GET['opta'] == "add")
{
?>
@@ -70,6 +76,8 @@ if ($_GET['opta'] == "add")
} else {
include("pages/bill/search.inc.php");
/*
print_optionbar_start('40');
?>
@@ -104,6 +112,9 @@ if ($_GET['opta'] == "add")
<td>
<input type=submit class=submit value=Search>
</td>
<td width='170' style='font-weight:bold; font-size: 12px;'>
<a href='bills/history/'><img src="images/16/information.png" align=absmiddle alt="Previous Billing Period"> Previous Billing Period</a>
</td>
<td width='80' style='font-weight:bold; font-size: 12px;'>
<a href='bills/add/'><img src="images/16/add.png" align=absmiddle alt="Add"> Add Bill</a>
</td>
@@ -114,9 +125,21 @@ if ($_GET['opta'] == "add")
<?php
print_optionbar_end();
*/
echo("<table border=0 cellspacing=0 cellpadding=5 class=devicetable width=100%>");
$i=1;
$i=0;
echo("<table border=0 cellspacing=0 cellpadding=5 class=devicetable width=100%>
<tr style=\"font-weight: bold; \">
<td width=\"7\"></td>
<td width=\"250\">Billing name</td>
<td></td>
<td>Type</td>
<td>Allowed</td>
<td>Used</td>
<td style=\"text-align: center;\">Overusage</td>
<td width=\"250\"></td>
<td width=\"60\"></td>
</tr>");
foreach (dbFetchRows("SELECT * FROM `bills` ORDER BY `bill_name`") as $bill)
{
if (bill_permitted($bill['bill_id']))
@@ -126,7 +149,7 @@ if ($_GET['opta'] == "add")
$datefrom = $day_data['0'];
$dateto = $day_data['1'];
# $rate_data = getRates($bill['bill_id'],$datefrom,$dateto);
$rate_data = $bill;
$rate_data = $bill;
$rate_95th = $rate_data['rate_95th'];
$dir_95th = $rate_data['dir_95th'];
$total_data = $rate_data['total_data'];
@@ -138,28 +161,34 @@ if ($_GET['opta'] == "add")
$allowed = formatRates($bill['bill_cdr'] * 1000);
$used = formatRates($rate_data['rate_95th'] * 1000);
$percent = round(($rate_data['rate_95th'] / $bill['bill_cdr']) * 100,2);
$background = get_percentage_colours($percent);
$overuse = $rate_data['rate_95th'] - $bill['bill_cdr'];
$overuse = (($overuse <= 0) ? "-" : "<span style=\"color: #".$background['left']."; font-weight: bold;\">".formatRates($overuse * 1000)."</span>");
} elseif ($bill['bill_type'] == "quota") {
$type = "Quota";
$allowed = formatStorage($bill['bill_gb']* 1024 * 1024 * 1024);
$used = formatStorage($rate_data['total_data'] * 1024 * 1024);
$percent = round(($rate_data['total_data'] / ($bill['bill_gb'] * 1024)) * 100,2);
$background = get_percentage_colours($percent);
$overuse = $rate_data['total_data'] - ($bill['bill_gb'] * 1024);
$overuse = (($overuse <= 0) ? "-" : "<span style=\"color: #".$background['left']."; font-weight: bold;\">".formatStorage($overuse * 1024 * 1024)."</span>");
}
$background = get_percentage_colours($percent);
$right_background = $background['right'];
$left_background = $background['left'];
if (!is_integer($i/2)) { $row_colour = $list_colour_a; } else { $row_colour = $list_colour_b; }
echo("
<tr bgcolor='$row_colour'>
<td width='7'></td>
<td width='250'><a href='bill/".$bill['bill_id']."/'><span style='font-weight: bold;' class=interface>".$bill['bill_name']."</span></a></td>
<td></td>
<td><a href='bill/".$bill['bill_id']."/'><span style='font-weight: bold;' class=interface>".$bill['bill_name']."</span></a><br />".strftime("%F", strtotime($datefrom))." to ".strftime("%F", strtotime($dateto))."</td>
<td>$notes</td>
<td>$type</td>
<td>$allowed</td>
<td>$used</td>
<td width=370>".print_percentage_bar (350, 20, $perc, NULL, "ffffff", $background['left'], $percent . "%", "ffffff", $background['right'])."</td>
<td width=60><a href='bill/".$bill['bill_id']."/edit/'><img src='images/16/wrench.png' align=absmiddle alt='Edit'> Edit</a></td>
<td style=\"text-align: center;\">$overuse</td>
<td>".print_percentage_bar (250, 20, $perc, NULL, "ffffff", $background['left'], $percent . "%", "ffffff", $background['right'])."</td>
<td><a href='bill/".$bill['bill_id']."/edit/'><img src='images/16/wrench.png' align=absmiddle alt='Edit'> Edit</a></td>
</tr>
");

View File

@@ -171,8 +171,13 @@ function getRates($bill_id,$datefrom,$dateto)
$a_95th = dbFetchColumn($q_95_sql);
$m_95th = $a_95th[$measurement_95th];
$mtot = getTotal($bill_id,$datefrom,$dateto);
$ptot = getPeriod($bill_id,$datefrom,$dateto);
$sum_data = getSum($bill_id,$datefrom,$dateto);
# $mtot = getTotal($bill_id,$datefrom,$dateto);
$mtot = $sum_data['total'];
$mtot_in = $sum_data['inbound'];
$mtot_out = $sum_data['outbound'];
# $ptot = getPeriod($bill_id,$datefrom,$dateto);
$ptot = $sum_data['period'];
$data['rate_95th_in'] = get95thIn($bill_id,$datefrom,$dateto);
$data['rate_95th_out'] = get95thOut($bill_id,$datefrom,$dateto);
@@ -187,6 +192,8 @@ function getRates($bill_id,$datefrom,$dateto)
}
$data['total_data'] = round($mtot / 1000 / 1000, 2);
$data['total_data_in'] = round($mtot_in / 1000 / 1000, 2);
$data['total_data_out'] = round($mtot_out / 1000 / 1000, 2);
$data['rate_average'] = round($mtot / $ptot / 1000 * 8, 2);
# print_r($data);
@@ -200,6 +207,12 @@ function getTotal($bill_id,$datefrom,$dateto)
return($mtot);
}
function getSum($bill_id,$datefrom,$dateto)
{
$sum = dbFetchRow("SELECT SUM(period) as period, SUM(delta) as total, SUM(in_delta) as inbound, SUM(out_delta) as outbound FROM bill_data WHERE bill_id = '".mres($bill_id)."' AND timestamp > '".mres($datefrom)."' AND timestamp <= '".mres($dateto)."'");
return($sum);
}
function getPeriod($bill_id,$datefrom,$dateto)
{
$ptot = dbFetchCell("SELECT SUM(period) FROM bill_data WHERE bill_id = '".mres($bill_id)."' AND timestamp > '".mres($datefrom)."' AND timestamp <= '".mres($dateto)."'");