Convert table to bootgrid

This commit is contained in:
Richard Lawley
2016-03-13 21:01:58 +00:00
parent c35afb1e1c
commit cbb8699fb8
4 changed files with 243 additions and 242 deletions

View File

@@ -0,0 +1,165 @@
<?php
// Calculate filters
$prev = !empty($_POST['period']) && ($_POST['period'] == 'prev');
$wheres = array();
$param = array();
if (isset($searchPhrase) && !empty($searchPhrase)) {
$wheres[] = 'bills.bill_name LIKE ?';
$param[] = "%$searchPhrase%";
}
if (!empty($_POST['bill_type'])) {
if ($prev) {
$wheres[] = 'bill_history.bill_type = ?';
}
else {
$wheres[] = 'bill_type = ?';
}
$param[] = $_POST['bill_type'];
}
if (!empty($_POST['state'])) {
if ($_POST['state'] === 'under') {
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))";
}
}
else if ($_POST['state'] === 'over') {
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))";
}
}
}
if ($prev) {
$select = "SELECT bills.bill_name, 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
if (is_admin() === false && is_read() === false) {
$query .= ' INNER JOIN `bill_perms` AS `BP` ON `bills`.`bill_id` = `BP`.`bill_id` ';
$wheres[] = '`BP`.`user_id`=?';
$param[] = $_SESSION['user_id'];
}
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 .= "\nORDER BY $sort";
if (isset($current)) {
$limit_low = (($current * $rowCount) - ($rowCount));
$limit_high = $rowCount;
}
if ($rowCount != -1) {
$sql .= " LIMIT $limit_low,$limit_high";
}
foreach (dbFetchRows($sql, $param) as $bill) {
$day_data = getDates($bill['bill_day']);
$datefrom = $day_data['0'];
$dateto = $day_data['1'];
$rate_95th = format_si($bill['rate_95th']) . 'bps';
$dir_95th = $bill['dir_95th'];
$total_data = format_bytes_billing($bill['total_data']);
$rate_average = $bill['rate_average'];
$url = generate_url(array('page' => 'bill', 'bill_id' => $bill['bill_id']));
$used95th = format_si($bill['rate_95th']).'bps';
if ($prev) {
$percent = $bill['bill_percent'];
$overuse = $bill['bill_overuse'];
}
else {
}
if (strtolower($bill['bill_type']) == 'cdr') {
$type = 'CDR';
$allowed = format_si($bill['bill_allowed']).'bps';
$in = format_si($bill['rate_95th_in']).'bps';
$out = format_si($bill['rate_95th_out']).'bps';
if (!$prev) {
$percent = round((($bill['rate_95th'] / $bill['bill_allowed']) * 100), 2);
$overuse = ($bill['rate_95th'] - $bill['bill_allowed']);
}
$overuse_formatted = format_si($overuse).'bps';
$used = $rate_95th;
$rate_95th = "<b>$rate_95th</b>";
}
else if (strtolower($bill['bill_type']) == 'quota') {
$type = 'Quota';
$allowed = format_bytes_billing($bill['bill_allowed']);
$in = format_bytes_billing($bill['traf_in']);
$out = format_bytes_billing($bill['traf_out']);
if (!$prev) {
$percent = round((($bill['total_data'] / ($bill['bill_allowed'])) * 100), 2);
$overuse = ($bill['total_data'] - $bill['bill_allowed']);
}
$overuse_formatted = format_bytes_billing($overuse);
$used = $total_data;
$total_data = "<b>$total_data</b>";
}
$background = get_percentage_colours($percent);
$right_background = $background['right'];
$left_background = $background['left'];
$overuse_formatted = (($overuse <= 0) ? '-' : "<span style='color: #${background['left']}; font-weight: bold;'>$overuse_formatted</span>");
$bill_name = "<a href='$url'><span style='font-weight: bold;' class='interface'>${bill['bill_name']}</span></a><br />" .
strftime('%F', strtotime($datefrom)) . " to " . strftime('%F', strtotime($dateto));
$bar = print_percentage_bar(250, 20, $percent, null, 'ffffff', $background['left'], $percent.'%', 'ffffff', $background['right']);
$actions = "";
if (!$prev && is_admin()) {
$actions .= "<a href='" . generate_url(array('page' => 'bill', 'bill_id' => $bill['bill_id'], 'view' => 'edit')) .
"'><img src='images/16/wrench.png' align=absmiddle alt='Edit'> Edit</a> ";
}
$response[] = array(
'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,
'graph' => $bar,
'actions' => $actions
);
}
$output = array('current' => $current, 'rowCount' => $rowCount, 'rows' => $response, 'total' => $total);
echo _json_encode($output);

View File

@@ -87,116 +87,90 @@ $pagetitle[] = 'Billing';
echo "<meta http-equiv='refresh' content='10000'>";
if ($vars['view'] == 'history') {
include 'pages/bills/search.inc.php';
include 'pages/bills/pmonth.inc.php';
}
else {
include 'pages/bills/search.inc.php';
include 'includes/modal/new_bill.inc.php';
include 'includes/modal/new_bill.inc.php';
?>
<table class="table table-striped">
<div class="panel panel-default panel-condensed">
<div class="table-responsive">
<table class="table table-hover" id="bills-list">
<thead>
<th>Billing name</th>
<th></th>
<th>Type</th>
<th>Allowed</th>
<th>Used</th>
<th>Overusage</th>
<th></th>
<th></th>
<th data-column-id="bill_name">Billing name</th>
<th data-column-id="notes" data-sortable="false"></th>
<th data-column-id="bill_type">Type</th>
<th data-column-id="bill_allowed" data-align="right">Allowed</th>
<th data-column-id="total_data_in" data-align="right">Inbound</th>
<th data-column-id="total_data_out" data-align="right">Outbound</th>
<th data-column-id="total_data" data-align="right">Total</th>
<th data-column-id="rate_95th" data-align="right">95th Percentile</th>
<th data-column-id="overusage" data-sortable="false" data-align="center">Overusage</th>
<th data-column-id="graph" data-sortable="false"></th>
<th data-column-id="actions" data-sortable="false"></th>
</thead>
<tbody>
<?php
$wheres = array();
$params = array();
if (!empty($_GET['search'])) {
$wheres[] = 'bills.bill_name LIKE ?';
$params[] = '%'.$_GET['search'].'%';
}
if (!empty($_GET['bill_type'])) {
$wheres[] = 'bill_type = ?';
$params[] = $_GET['bill_type'];
}
if ($_GET['state'] === 'under') {
$wheres[] = "((bill_type = 'cdr' AND rate_95th <= bill_cdr) OR (bill_type = 'quota' AND total_data <= bill_quota))";
} else if ($_GET['state'] === 'over') {
$wheres[] = "((bill_type = 'cdr' AND rate_95th > bill_cdr) OR (bill_type = 'quota' AND total_data > bill_quota))";
}
$query = 'SELECT *
FROM `bills`
';
if (sizeof($wheres) > 0) {
$query .= 'WHERE ' . implode(' AND ', $wheres) . "\n";
}
$query .= 'ORDER BY bills.bill_name';
foreach (dbFetchRows($query, $params) as $bill) {
if (bill_permitted($bill['bill_id'])) {
unset($class);
$day_data = getDates($bill['bill_day']);
$datefrom = $day_data['0'];
$dateto = $day_data['1'];
$rate_data = $bill;
$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';
$allowed = format_si($bill['bill_cdr']).'bps';
$used = format_si($rate_data['rate_95th']).'bps';
$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;">'.format_si($overuse).'bps</span>');
}
else if ($bill['bill_type'] == 'quota') {
$type = 'Quota';
$allowed = format_bytes_billing($bill['bill_quota']);
$used = format_bytes_billing($rate_data['total_data']);
$percent = round((($rate_data['total_data'] / ($bill['bill_quota'])) * 100), 2);
$background = get_percentage_colours($percent);
$overuse = ($rate_data['total_data'] - $bill['bill_quota']);
$overuse = (($overuse <= 0) ? '-' : '<span style="color: #'.$background['left'].'; font-weight: bold;">'.format_bytes_billing($overuse).'</span>');
}
$right_background = $background['right'];
$left_background = $background['left'];
?>
<tr>
<td>
<a href='<?php echo generate_url(array('page' => 'bill', 'bill_id' => $bill['bill_id'])) ?>'><span style='font-weight: bold;' class=interface><?php echo $bill['bill_name'] ?></span></a>
<br />
<?php echo strftime('%F', strtotime($datefrom)) ?> to <?php echo strftime('%F', strtotime($dateto)) ?>
</td>
<td><?php echo $notes ?></td>
<td><?php echo $type ?></td>
<td><?php echo $allowed ?></td>
<td><?php echo $used ?></td>
<td style="text-align: center;"><?php echo $overuse ?></td>
<td><?php echo print_percentage_bar(250, 20, $percent, null, 'ffffff', $background['left'], $percent.'%', 'ffffff', $background['right'])?></td>
<td>
<?php if ($_SESSION['userlevel'] >= 10) { ?>
<a href='<?php echo generate_url(array('page' => 'bill', 'bill_id' => $bill['bill_id'], 'view' => 'edit')) ?>'><img src='images/16/wrench.png' align=absmiddle alt='Edit'> Edit</a>
<?php } ?>
</td>
</tr>
<?php }
}?>
</tbody>
</table>
</div>
</div>
<script type="text/html" id="table-header">
<div id="{{ctx.id}}" class="{{css.header}}">
<div class="row">
<div class="col-sm-4">
<?php if ($_SESSION['userlevel'] >= 10) { ?>
<button type="button" class="btn btn-default btn-sm" data-toggle="modal" data-target="#create-bill"><i class="fa fa-plus"></i> Create Bill</button>
<?php } ?>
</div>
<div class="col-sm-8 actionBar">
<span class="form-inline" id="table-filters">
<fieldset class="form-group">
<select name='period' id='period' class="form-control input-sm">
<option value=''>Current Billing Period</option>
<option value='prev'>Previous Billing Period</option>
</select>
<select name='bill_type' id='bill_type' class="form-control input-sm">
<option value=''>All Types</option>
<option value='cdr' <?php if ($_GET['bill_type'] === 'cdr') { echo 'selected'; } ?>>CDR</option>
<option value='quota' <?php if ($_GET['bill_type'] === 'quota') { echo 'selected'; } ?>>Quota</option>
</select>
<select name='state' id='state' class="form-control input-sm">
<option value=''>All States</option>
<option value='under' <?php if ($_GET['state'] === 'under') { echo 'selected'; } ?>>Under Quota</option>
<option value='over' <?php if ($_GET['state'] === 'over') { echo 'selected'; } ?>>Over Quota</option>
</select>
</fieldset>
</span>
<p class="{{css.search}}"></p>
<p class="{{css.actions}}"></p>
</div>
</div>
</div>
</script>
<script type="text/javascript">
var grid = $('#bills-list').bootgrid({
ajax: true,
templates: {
header: $('#table-header').html()
},
columnSelection: false,
rowCount: [10,20,50,100,-1],
post: function() {
return {
id: 'bills',
bill_type: $('select#bill_type').val(),
state: $('select#state').val(),
period: $('select#period').val()
};
},
url: "/ajax_table.php"
}).on("loaded.rs.jquery.bootgrid", function() {
});
$('#table-filters select').on('change', function() { grid.bootgrid('reload'); });
<?php
if ($vars['view'] == 'add') {
?>
<script type="text/javascript">
$(function() {
$('#create-bill').modal('show');
});
</script>
<?php
}
}
?>
</script>

View File

@@ -1,98 +0,0 @@
<?php
$no_refresh = true;
$pagetitle[] = 'Previous Billing Period';
echo '<table class="table table-condensed table-striped">
<thead>
<tr>
<th>Billing name</th>
<th>Type</th>
<th>Allowed</th>
<th>Inbound</th>
<th>Outbound</th>
<th>Total</th>
<th>95 percentile</th>
<th>Overusage</th>
<th></th>
</tr>
</thead>
<tbody>';
$wheres = array();
$params = array();
if (!empty($_GET['search'])) {
$wheres[] = 'bills.bill_name LIKE ?';
$params[] = '%'.$_GET['search'].'%';
}
if (!empty($_GET['bill_type'])) {
$wheres[] = 'bill_history.bill_type = ?';
$params[] = $_GET['bill_type'];
}
if ($_GET['state'] === 'under') {
$wheres[] = 'bill_history.bill_overuse = 0';
} else if ($_GET['state'] === 'over') {
$wheres[] = 'bill_history.bill_overuse > 0';
}
$query = 'SELECT bills.bill_name, bill_history.*
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
';
if (sizeof($wheres) > 0) {
$query .= 'WHERE ' . implode(' AND ', $wheres) . "\n";
}
$query .= 'ORDER BY bills.bill_name';
foreach (dbFetchRows($query, $params) as $bill) {
if (bill_permitted($bill['bill_id'])) {
$datefrom = $bill['bill_datefrom'];
$dateto = $bill['bill_dateto'];
unset($class);
$type = $bill['bill_type'];
$percent = $bill['bill_percent'];
$dir_95th = $bill['dir_95th'];
$rate_95th = format_si($bill['rate_95th']).'bps';
$total_data = format_bytes_billing($bill['traf_total']);
$background = get_percentage_colours($percent);
if ($type == 'CDR') {
$allowed = format_si($bill['bill_allowed']).'bps';
$used = format_si($bill['rate_95th']).'bps';
$in = format_si($bill['rate_95th_in']).'bps';
$out = format_si($bill['rate_95th_out']).'bps';
$overuse = (($bill['bill_overuse'] <= 0) ? '-' : '<span style="color: #'.$background['left'].'; font-weight: bold;">'.format_si($bill['bill_overuse']).'bps</span>');
}
else if ($type == 'Quota') {
$allowed = format_bytes_billing($bill['bill_allowed']);
$used = format_bytes_billing($bill['total_data']);
$in = format_bytes_billing($bill['traf_in']);
$out = format_bytes_billing($bill['traf_out']);
$overuse = (($bill['bill_overuse'] <= 0) ? '-' : '<span style="color: #'.$background['left'].'; font-weight: bold;">'.format_bytes_billing($bill['bill_overuse']).'</span>');
}
$total_data = (($type == 'Quota') ? '<b>'.$total_data.'</b>' : $total_data);
$rate_95th = (($type == 'CDR') ? '<b>'.$rate_95th.'</b>' : $rate_95th);
echo "
<tr>
<td><a href=\"".generate_url(array('page' => 'bill', 'bill_id' => $bill['bill_id'], 'view' => 'history', detail => $bill['bill_hist_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, $percent, null, 'ffffff', $background['left'], $percent.'%', 'ffffff', $background['right']).'</td>
</tr>';
}//end if
}//end foreach
echo '</tbody>
</table>';

View File

@@ -1,40 +0,0 @@
<?php
print_optionbar_start('40');
?>
<form method='get' action='' class="form-inline" role="form">
<fieldset class="form-group">
Bills
<input type="text" name="search" id="search" class="form-control input-sm" value="<?php echo $_GET['search']; ?>" />
<select name='bill_type' id='bill_type' class="form-control input-sm">
<option value=''>All Types</option>
<option value='cdr' <?php if ($_GET['bill_type'] === 'cdr') { echo 'selected'; } ?>>CDR</option>
<option value='quota' <?php if ($_GET['bill_type'] === 'quota') { echo 'selected'; } ?>>Quota</option>
</select>
<select name='state' id='state' class="form-control input-sm">
<option value=''>All States</option>
<option value='under' <?php if ($_GET['state'] === 'under') { echo 'selected'; } ?>>Under Quota</option>
<option value='over' <?php if ($_GET['state'] === 'over') { echo 'selected'; } ?>>Over Quota</option>
</select>
<button type="submit" class="btn btn-default input-sm">Search</button>
</fieldset>
<div class="form-group pull-right">
<?php
if ($vars['view'] == 'history') {
echo '<a class="btn btn-default btn-sm" href="bills/"><i class="fa fa-clock-o"></i> Current Billing Period</a>';
}
else {
echo '<a class="btn btn-default btn-sm" href="bills/view=history/"><i class="fa fa-history"></i> Previous Billing Period</a>';
}
?>
<?php if ($_SESSION['userlevel'] >= 10) { ?>
<button type="button" class="btn btn-default btn-sm" data-toggle="modal" data-target="#create-bill"><i class="fa fa-plus"></i> Create Bill</button>
<?php } ?>
</div>
</form>
<?php
print_optionbar_end();