From 3906bf52c954356e936c5838af4c673b1df2b9d6 Mon Sep 17 00:00:00 2001
From: Robert Anderson <randerson@lbsd.net>
Date: Mon, 12 Oct 2009 07:27:22 +0000
Subject: [PATCH] Use periodKey for fetching summaries Show depleted topups

---
 webgui/ajax.php                               |  20 +-
 .../include/ajax/functions/AdminUserLogs.php  | 193 +++++-------------
 .../include/ajax/functions/WiSPUserLogs.php   | 191 +++++------------
 webgui/js/app/windows/AdminUserLogs.js        |  92 +++------
 webgui/js/app/windows/WiSPUserLogs.js         |  94 +++------
 5 files changed, 174 insertions(+), 416 deletions(-)

diff --git a/webgui/ajax.php b/webgui/ajax.php
index 783d6da2..057969bd 100644
--- a/webgui/ajax.php
+++ b/webgui/ajax.php
@@ -366,12 +366,10 @@
 			$res = new json_response;
 			$res->addField('uptimeCap','int');
 			$res->addField('trafficCap','int');
-			$res->addField('trafficCurrentTopupUsed','int');
-			$res->addField('trafficCurrentTopupCap','int');
-			$res->addField('uptimeCurrentTopupUsed','int');
-			$res->addField('uptimeCurrentTopupCap','int');
-			$res->addField('trafficTopupRemaining','int');
-			$res->addField('uptimeTopupRemaining','int');
+			$res->addField('trafficUsage','int');
+			$res->addField('uptimeUsage','int');
+			$res->addField('trafficTopups','int');
+			$res->addField('uptimeTopups','int');
 			$res->parseHash($rawData);
 			$res->setDatasetSize($numResults);
 
@@ -387,12 +385,10 @@
 			$res = new json_response;
 			$res->addField('uptimeCap','int');
 			$res->addField('trafficCap','int');
-			$res->addField('trafficCurrentTopupUsed','int');
-			$res->addField('trafficCurrentTopupCap','int');
-			$res->addField('uptimeCurrentTopupUsed','int');
-			$res->addField('uptimeCurrentTopupCap','int');
-			$res->addField('trafficTopupRemaining','int');
-			$res->addField('uptimeTopupRemaining','int');
+			$res->addField('trafficUsage','int');
+			$res->addField('uptimeUsage','int');
+			$res->addField('trafficTopups','int');
+			$res->addField('uptimeTopups','int');
 			$res->parseHash($rawData);
 			$res->setDatasetSize($numResults);
 
diff --git a/webgui/include/ajax/functions/AdminUserLogs.php b/webgui/include/ajax/functions/AdminUserLogs.php
index 54ef3feb..97971750 100644
--- a/webgui/include/ajax/functions/AdminUserLogs.php
+++ b/webgui/include/ajax/functions/AdminUserLogs.php
@@ -6,6 +6,39 @@ include_once("include/db.php");
 # Return user logs summary
 function getAdminUserLogsSummary($params) {
 
+	# Get group attributes
+	# fixme - user might be member of multiple groups
+	$res = DBSelect("
+			SELECT
+				group_attributes.Name,
+				group_attributes.Value
+			FROM
+				group_attributes, users_to_groups, groups
+			WHERE
+				group_attributes.GroupID = groups.ID
+				AND groups.ID = users_to_groups.GroupID
+				AND users_to_groups.UserID = ?",
+				array($params[0]['ID'])
+	);
+
+	# Return if error
+	if (!is_object($res)) {
+		return $res;
+	}
+
+	# Fetch uptime and traffic limits, if not found, this is prepaid account.. use -1 as we need int
+	$trafficCap = -1;
+	$uptimeCap = -1;
+	while ($row = $res->fetchObject()) {
+		if ($row->name == 'SMRadius-Capping-Traffic-Limit') {
+			$trafficCap = (int)$row->value;
+		}
+		if ($row->name == 'SMRadius-Capping-Uptime-Limit') {
+			$uptimeCap = (int)$row->value;
+		}
+	}
+
+	# Get user attributes
 	$res = DBSelect("
 			SELECT
 				user_attributes.Name,
@@ -22,12 +55,7 @@ function getAdminUserLogsSummary($params) {
 		return $res;
 	}
 
-	# Array of results
-	$resultArray = array();
-
-	# Fetch uptime and traffic limits, if not  found, this is prepaid account.. use -1 as we need int
-	$trafficCap = -1;
-	$uptimeCap = -1;
+	# Fetch uptime and traffic limits, if not found, this is prepaid account.. use -1 as we need int
 	while ($row = $res->fetchObject()) {
 		if ($row->name == 'SMRadius-Capping-Traffic-Limit') {
 			$trafficCap = (int)$row->value;
@@ -37,12 +65,13 @@ function getAdminUserLogsSummary($params) {
 		}
 	}
 
-	# Add cap type / amount to result
+	# Add caps to result
+	$resultArray = array();
 	$resultArray['trafficCap'] = $trafficCap;
 	$resultArray['uptimeCap'] = $uptimeCap;
 
 	# Dates we want to use to search search
-	$periodKey = new DateTime($params[0]['PeriodKey']);
+	$periodKey = new DateTime($params[0]['PeriodKey']."-01");
 
 	# Return if error
 	if (!is_object($periodKey)) {
@@ -62,7 +91,6 @@ function getAdminUserLogsSummary($params) {
 			topups_summary.TopupID = topups.ID
 			AND topups.UserID = ?
 			AND topups_summary.PeriodKey = ?
-			AND topups.Depleted = 0
 		ORDER BY
 			topups.Timestamp",
 			array($params[0]['ID'],$periodKey->format('Y-m'))
@@ -85,6 +113,8 @@ function getAdminUserLogsSummary($params) {
 	}
 
 	# Fetch user uptime and traffic topups
+	$periodKeyEnd = new DateTime($periodKey->format('Y-m-d'));
+	$periodKeyEnd->modify("+1 month");
 	$res = DBSelect("
 		SELECT
 			Value, Type
@@ -92,12 +122,11 @@ function getAdminUserLogsSummary($params) {
 			topups
 		WHERE
 			topups.UserID = ?
-			AND topups.ValidFrom <= ?
+			AND topups.ValidFrom = ?
 			AND topups.ValidTo >= ?
-			AND topups.Depleted = 0
 		ORDER BY
 			topups.Timestamp",
-			array($params[0]['ID'],$periodKey->format('Y-m-d'),$periodKey->format('Y-m-d'))
+			array($params[0]['ID'],$periodKey->format('Y-m-d'),$periodKeyEnd->format('Y-m-d'))
 	);
 
 	# Return if error
@@ -140,26 +169,22 @@ function getAdminUserLogsSummary($params) {
 
 		# Traffic in
 		$inputDataItem = 0;
-
 		if (isset($row->acctinputoctets) && $row->acctinputoctets > 0) {
 			$inputDataItem += ($row->acctinputoctets / 1024) / 1024;
 		}
 		if (isset($row->acctinputgigawords) && $row->acctinputgigawords > 0) {
 			$inputDataItem += ($row->acctinputgigawords * 4096);
 		}
-
 		$totalTraffic += $inputDataItem;
 
 		# Traffic out
 		$outputDataItem = 0;
-
 		if (isset($row->acctoutputoctets) && $row->acctoutputoctets > 0) {
-			$outputDataItem += ($row->acctoutputoctets / 1024) / 1024;
+			$outputDataItem += ($row->acctoutputoctets / 1026) / 1024;
 		}
 		if (isset($row->acctoutputgigawords) && $row->acctoutputgigawords > 0) {
 			$outputDataItem += ($row->acctoutputgigawords * 4096);
 		}
-
 		$totalTraffic += $outputDataItem;
 
 		# Uptime
@@ -169,134 +194,28 @@ function getAdminUserLogsSummary($params) {
 		}
 
 		$totalUptime += $sessionTimeItem;
-		# Round up
-		$totalUptime = ceil($totalUptime / 60);
 	}
 
-	# Set excess traffic usage
-	$excessTraffic = 0;
-	if (is_numeric($trafficCap) && $trafficCap > 0) {
-		$excessTraffic += $totalTraffic - $trafficCap;
-	} else {
-		$excessTraffic += $totalTraffic;
-	}
+	# Round up usage
+	$totalTraffic = (int)ceil($totalTraffic);
+	$totalUptime = (int)ceil($totalUptime / 60);
 
-	# Set excess uptime usage
-	$excessUptime = 0;
-	if (is_numeric($uptimeCap) && $uptimeCap > 0) {
-		$excessUptime += $totalUptime - $uptimeCap;
-	} else {
-		$excessUptime += $totalUptime;
-	}
+	# Add usage to our return array
+	$resultArray['trafficUsage'] = $totalTraffic;
+	$resultArray['uptimeUsage'] = $totalUptime;
 
-	$currentTrafficTopup = array();
-	$topupTrafficRemaining = 0;
-	# Loop through traffic topups and check for current topup, total topups not being used
-	if (is_string($trafficCap) || $trafficCap != 0) {
-		$i = 0;
-		# User is using traffic from topups
-		if ($excessTraffic > 0) {
-			foreach ($topups as $topupItem) {
-				if ($topupItem['Type'] == 1) {
-					if ($excessTraffic <= 0) {
-						$topupTrafficRemaining += $topupItem['Limit'];
-						next($topupItem);
-					} elseif ($excessTraffic >= $topupItem['Limit']) {
-						$excessTraffic -= $topupItem['Limit'];
-					} else {
-						if (isset($topupItem['OriginalLimit'])) {
-							$currentTrafficTopup['Cap'] = $topupItem['OriginalLimit'];
-						} else {
-							$currentTrafficTopup['Cap'] = $topupItem['Limit'];
-						}
-						$currentTrafficTopup['Used'] = $excessTraffic;
-						$excessTraffic -= $topupItem['Limit'];
-					}
-				}
-			}
-		# User has not used traffic topups yet
-		} else {
-			foreach ($topups as $topupItem) {
-				if ($topupItem['Type'] == 1) {
-					if ($i == 0) {
-						if (isset($topupItem['OriginalLimit'])) {
-							$currentTrafficTopup['Cap'] = $topupItem['OriginalLimit'];
-						} else {
-							$currentTrafficTopup['Cap'] = $topupItem['Limit'];
-						}
-						$i = 1;
-							$currentTrafficTopup['Used'] = 0;
-					} else {
-						$topupTrafficRemaining += $topupItem['Limit'];
-					}
-				}
-			}
+	# Loop through topups and add to return array
+	$resultArray['trafficTopups'] = 0;
+	$resultArray['uptimeTopups'] = 0;
+	foreach ($topups as $topupItem) {
+		if ($topupItem['Type'] == 1) {
+			$resultArray['trafficTopups'] += $topupItem['Limit'];
 		}
-	}
-
-	$currentUptimeTopup = array();
-	$topupUptimeRemaining = 0;
-	# Loop through uptime topups and check for current topup, total topups not being used
-	if (is_string($uptimeCap) || $uptimeCap != 0) {
-		$i = 0;
-		# User is using uptime from topups
-		if ($excessUptime > 0) {
-			foreach ($topups as $topupItem) {
-				if ($topupItem['Type'] == 2) {
-					if ($excessUptime <= 0) {
-						$topupUptimeRemaining += $topupItem['Limit'];
-						next($topupItem);
-					} elseif ($excessUptime >= $topupItem['Limit']) {
-						$excessUptime -= $topupItem['Limit'];
-					} else {
-						if (isset($topupItem['OriginalLimit'])) {
-							$currentUptimeTopup['Cap'] = $topupItem['OriginalLimit'];
-						} else {
-							$currentUptimeTopup['Cap'] = $topupItem['Limit'];
-						}
-						$currentUptimeTopup['Used'] = $excessUptime;
-						$excessUptime -= $topupItem['Limit'];
-					}
-				}
-			}
-		# User has not used uptime topups yet
-		} else {
-			foreach ($topups as $topupItem) {
-				if ($topupItem['Type'] == 2) {
-					if ($i == 0) {
-						if (isset($topupItem['OriginalLimit'])) {
-							$currentUptimeTopup['Cap'] = $topupItem['OriginalLimit'];
-						} else {
-							$currentUptimeTopup['Cap'] = $topupItem['Limit'];
-						}
-						$i = 1;
-							$currentUptimeTopup['Used'] = 0;
-					} else {
-						$topupUptimeRemaining += $topupItem['Limit'];
-					}
-				}
-			}
+		if ($topupItem['Type'] == 2) {
+			$resultArray['uptimeTopups'] += $topupItem['Limit'];
 		}
 	}
 
-	# Traffic..
-	$resultArray['trafficCurrentTopupUsed'] = -1;
-	$resultArray['trafficCurrentTopupCap'] = -1;
-	if (count($currentTrafficTopup) > 0) {
-		$resultArray['trafficCurrentTopupUsed'] = $currentTrafficTopup['Used'];
-		$resultArray['trafficCurrentTopupCap'] = (int)$currentTrafficTopup['Cap'];
-	}
-	$resultArray['trafficTopupRemaining'] = $topupTrafficRemaining;
-
-	# Uptime..
-	$resultArray['uptimeCurrentTopupUsed'] = -1;
-	$resultArray['uptimeCurrentTopupCap'] = -1;
-	if (count($currentUptimeTopup) > 0) {
-		$resultArray['uptimeCurrentTopupUsed'] = $currentUptimeTopup['Used'];
-		$resultArray['uptimeCurrentTopupCap'] = (int)$currentUptimeTopup['Cap'];
-	}
-	$resultArray['uptimeTopupRemaining'] = $topupUptimeRemaining;
-
 	# Return results
 	return array($resultArray, 1);
 }
diff --git a/webgui/include/ajax/functions/WiSPUserLogs.php b/webgui/include/ajax/functions/WiSPUserLogs.php
index d9822eee..125c8cbc 100644
--- a/webgui/include/ajax/functions/WiSPUserLogs.php
+++ b/webgui/include/ajax/functions/WiSPUserLogs.php
@@ -6,6 +6,39 @@ include_once("include/db.php");
 # Return user logs summary
 function getWiSPUserLogsSummary($params) {
 
+	# Get group attributes
+	# fixme - user might be member of multiple groups
+	$res = DBSelect("
+			SELECT
+				group_attributes.Name,
+				group_attributes.Value
+			FROM
+				group_attributes, users_to_groups, groups
+			WHERE
+				group_attributes.GroupID = groups.ID
+				AND groups.ID = users_to_groups.GroupID
+				AND users_to_groups.UserID = ?",
+				array($params[0]['ID'])
+	);
+
+	# Return if error
+	if (!is_object($res)) {
+		return $res;
+	}
+
+	# Fetch uptime and traffic limits, if not found, this is prepaid account.. use -1 as we need int
+	$trafficCap = -1;
+	$uptimeCap = -1;
+	while ($row = $res->fetchObject()) {
+		if ($row->name == 'SMRadius-Capping-Traffic-Limit') {
+			$trafficCap = (int)$row->value;
+		}
+		if ($row->name == 'SMRadius-Capping-Uptime-Limit') {
+			$uptimeCap = (int)$row->value;
+		}
+	}
+
+	# Get user attributes
 	$res = DBSelect("
 			SELECT
 				user_attributes.Name,
@@ -22,12 +55,7 @@ function getWiSPUserLogsSummary($params) {
 		return $res;
 	}
 
-	# Array of results
-	$resultArray = array();
-
-	# Fetch uptime and traffic limits, if not  found, this is prepaid account.. use -1 as we need int
-	$trafficCap = -1;
-	$uptimeCap = -1;
+	# Fetch uptime and traffic limits, if not found, this is prepaid account.. use -1 as we need int
 	while ($row = $res->fetchObject()) {
 		if ($row->name == 'SMRadius-Capping-Traffic-Limit') {
 			$trafficCap = (int)$row->value;
@@ -37,12 +65,13 @@ function getWiSPUserLogsSummary($params) {
 		}
 	}
 
-	# Add cap type / amount to result
+	# Add caps to result
+	$resultArray = array();
 	$resultArray['trafficCap'] = $trafficCap;
 	$resultArray['uptimeCap'] = $uptimeCap;
 
 	# Dates we want to use to search search
-	$periodKey = new DateTime($params[0]['PeriodKey']);
+	$periodKey = new DateTime($params[0]['PeriodKey']."-01");
 
 	# Return if error
 	if (!is_object($periodKey)) {
@@ -62,7 +91,6 @@ function getWiSPUserLogsSummary($params) {
 			topups_summary.TopupID = topups.ID
 			AND topups.UserID = ?
 			AND topups_summary.PeriodKey = ?
-			AND topups.Depleted = 0
 		ORDER BY
 			topups.Timestamp",
 			array($params[0]['ID'],$periodKey->format('Y-m'))
@@ -85,6 +113,8 @@ function getWiSPUserLogsSummary($params) {
 	}
 
 	# Fetch user uptime and traffic topups
+	$periodKeyEnd = new DateTime($periodKey->format('Y-m-d'));
+	$periodKeyEnd->modify("+1 month");
 	$res = DBSelect("
 		SELECT
 			Value, Type
@@ -92,12 +122,11 @@ function getWiSPUserLogsSummary($params) {
 			topups
 		WHERE
 			topups.UserID = ?
-			AND topups.ValidFrom <= ?
+			AND topups.ValidFrom = ?
 			AND topups.ValidTo >= ?
-			AND topups.Depleted = 0
 		ORDER BY
 			topups.Timestamp",
-			array($params[0]['ID'],$periodKey->format('Y-m-d'),$periodKey->format('Y-m-d'))
+			array($params[0]['ID'],$periodKey->format('Y-m-d'),$periodKeyEnd->format('Y-m-d'))
 	);
 
 	# Return if error
@@ -140,26 +169,22 @@ function getWiSPUserLogsSummary($params) {
 
 		# Traffic in
 		$inputDataItem = 0;
-
 		if (isset($row->acctinputoctets) && $row->acctinputoctets > 0) {
 			$inputDataItem += ($row->acctinputoctets / 1024) / 1024;
 		}
 		if (isset($row->acctinputgigawords) && $row->acctinputgigawords > 0) {
 			$inputDataItem += ($row->acctinputgigawords * 4096);
 		}
-
 		$totalTraffic += $inputDataItem;
 
 		# Traffic out
 		$outputDataItem = 0;
-
 		if (isset($row->acctoutputoctets) && $row->acctoutputoctets > 0) {
 			$outputDataItem += ($row->acctoutputoctets / 1024) / 1024;
 		}
 		if (isset($row->acctoutputgigawords) && $row->acctoutputgigawords > 0) {
 			$outputDataItem += ($row->acctoutputgigawords * 4096);
 		}
-
 		$totalTraffic += $outputDataItem;
 
 		# Uptime
@@ -169,134 +194,28 @@ function getWiSPUserLogsSummary($params) {
 		}
 
 		$totalUptime += $sessionTimeItem;
-		# Round up
-		$totalUptime = ceil($totalUptime / 60);
 	}
 
-	# Set excess traffic usage
-	$excessTraffic = 0;
-	if (is_numeric($trafficCap) && $trafficCap > 0) {
-		$excessTraffic += $totalTraffic - $trafficCap;
-	} else {
-		$excessTraffic += $totalTraffic;
-	}
+	# Round up usage
+	$totalTraffic = (int)ceil($totalTraffic);
+	$totalUptime = (int)ceil($totalUptime / 60);
 
-	# Set excess uptime usage
-	$excessUptime = 0;
-	if (is_numeric($uptimeCap) && $uptimeCap > 0) {
-		$excessUptime += $totalUptime - $uptimeCap;
-	} else {
-		$excessUptime += $totalUptime;
-	}
+	# Add usage to our return array
+	$resultArray['trafficUsage'] = $totalTraffic;
+	$resultArray['uptimeUsage'] = $totalUptime;
 
-	$currentTrafficTopup = array();
-	$topupTrafficRemaining = 0;
-	# Loop through traffic topups and check for current topup, total topups not being used
-	if (is_string($trafficCap) || $trafficCap != 0) {
-		$i = 0;
-		# User is using traffic from topups
-		if ($excessTraffic > 0) {
-			foreach ($topups as $topupItem) {
-				if ($topupItem['Type'] == 1) {
-					if ($excessTraffic <= 0) {
-						$topupTrafficRemaining += $topupItem['Limit'];
-						next($topupItem);
-					} elseif ($excessTraffic >= $topupItem['Limit']) {
-						$excessTraffic -= $topupItem['Limit'];
-					} else {
-						if (isset($topupItem['OriginalLimit'])) {
-							$currentTrafficTopup['Cap'] = $topupItem['OriginalLimit'];
-						} else {
-							$currentTrafficTopup['Cap'] = $topupItem['Limit'];
-						}
-						$currentTrafficTopup['Used'] = $excessTraffic;
-						$excessTraffic -= $topupItem['Limit'];
-					}
-				}
-			}
-		# User has not used traffic topups yet
-		} else {
-			foreach ($topups as $topupItem) {
-				if ($topupItem['Type'] == 1) {
-					if ($i == 0) {
-						if (isset($topupItem['OriginalLimit'])) {
-							$currentTrafficTopup['Cap'] = $topupItem['OriginalLimit'];
-						} else {
-							$currentTrafficTopup['Cap'] = $topupItem['Limit'];
-						}
-						$i = 1;
-							$currentTrafficTopup['Used'] = 0;
-					} else {
-						$topupTrafficRemaining += $topupItem['Limit'];
-					}
-				}
-			}
+	# Loop through topups and add to return array
+	$resultArray['trafficTopups'] = 0;
+	$resultArray['uptimeTopups'] = 0;
+	foreach ($topups as $topupItem) {
+		if ($topupItem['Type'] == 1) {
+			$resultArray['trafficTopups'] += $topupItem['Limit'];
 		}
-	}
-
-	$currentUptimeTopup = array();
-	$topupUptimeRemaining = 0;
-	# Loop through uptime topups and check for current topup, total topups not being used
-	if (is_string($uptimeCap) || $uptimeCap != 0) {
-		$i = 0;
-		# User is using uptime from topups
-		if ($excessUptime > 0) {
-			foreach ($topups as $topupItem) {
-				if ($topupItem['Type'] == 2) {
-					if ($excessUptime <= 0) {
-						$topupUptimeRemaining += $topupItem['Limit'];
-						next($topupItem);
-					} elseif ($excessUptime >= $topupItem['Limit']) {
-						$excessUptime -= $topupItem['Limit'];
-					} else {
-						if (isset($topupItem['OriginalLimit'])) {
-							$currentUptimeTopup['Cap'] = $topupItem['OriginalLimit'];
-						} else {
-							$currentUptimeTopup['Cap'] = $topupItem['Limit'];
-						}
-						$currentUptimeTopup['Used'] = $excessUptime;
-						$excessUptime -= $topupItem['Limit'];
-					}
-				}
-			}
-		# User has not used uptime topups yet
-		} else {
-			foreach ($topups as $topupItem) {
-				if ($topupItem['Type'] == 2) {
-					if ($i == 0) {
-						if (isset($topupItem['OriginalLimit'])) {
-							$currentUptimeTopup['Cap'] = $topupItem['OriginalLimit'];
-						} else {
-							$currentUptimeTopup['Cap'] = $topupItem['Limit'];
-						}
-						$i = 1;
-							$currentUptimeTopup['Used'] = 0;
-					} else {
-						$topupUptimeRemaining += $topupItem['Limit'];
-					}
-				}
-			}
+		if ($topupItem['Type'] == 2) {
+			$resultArray['uptimeTopups'] += $topupItem['Limit'];
 		}
 	}
 
-	# Traffic..
-	$resultArray['trafficCurrentTopupUsed'] = -1;
-	$resultArray['trafficCurrentTopupCap'] = -1;
-	if (count($currentTrafficTopup) > 0) {
-		$resultArray['trafficCurrentTopupUsed'] = $currentTrafficTopup['Used'];
-		$resultArray['trafficCurrentTopupCap'] = (int)$currentTrafficTopup['Cap'];
-	}
-	$resultArray['trafficTopupRemaining'] = $topupTrafficRemaining;
-
-	# Uptime..
-	$resultArray['uptimeCurrentTopupUsed'] = -1;
-	$resultArray['uptimeCurrentTopupCap'] = -1;
-	if (count($currentUptimeTopup) > 0) {
-		$resultArray['uptimeCurrentTopupUsed'] = $currentUptimeTopup['Used'];
-		$resultArray['uptimeCurrentTopupCap'] = (int)$currentUptimeTopup['Cap'];
-	}
-	$resultArray['uptimeTopupRemaining'] = $topupUptimeRemaining;
-
 	# Return results
 	return array($resultArray, 1);
 }
diff --git a/webgui/js/app/windows/AdminUserLogs.js b/webgui/js/app/windows/AdminUserLogs.js
index e5aa790b..1ccb49d7 100644
--- a/webgui/js/app/windows/AdminUserLogs.js
+++ b/webgui/js/app/windows/AdminUserLogs.js
@@ -254,10 +254,8 @@ function showAdminUserLogsWindow(id) {
 	var store = Ext.getCmp(adminUserLogsWindow.gridPanelID).getStore();
 
 	store.on('load',function() {
-		var inputTotal = store.sum('AcctInputMbyte');
-		var outputTotal = store.sum('AcctOutputMbyte');
-		var uptimeTotal = store.sum('AcctSessionTime');
 
+		// Fetch periodKey from form
 		var periodKeyField = (Ext.getCmp(formPeriodKeyID)).getValue();
 
 		// Mask parent window
@@ -267,7 +265,7 @@ function showAdminUserLogsWindow(id) {
 			adminUserLogsWindow,
 			{
 				params: {
-					From: periodKeyField,
+					PeriodKey: periodKeyField,
 					ID: id,
 					SOAPUsername: globalConfig.soap.username,
 					SOAPPassword: globalConfig.soap.password,
@@ -280,58 +278,17 @@ function showAdminUserLogsWindow(id) {
 				customSuccess: function (result) {
 					response = Ext.decode(result.responseText);
 
-					// Traffic variables
+					// Caps
 					var trafficCap = response.data.trafficCap; // value of -1: prepaid
-					
-					var trafficCurrentTopupUsed = response.data.trafficCurrentTopupUsed; // value of -1: no current topup
-					var trafficCurrentTopupCap = response.data.trafficCurrentTopupCap; // value of -1: no current topup
-					var trafficTopupRemaining = response.data.trafficTopupRemaining;
-
-					// Uptime variables
 					var uptimeCap = response.data.uptimeCap; // value of -1: prepaid
+					
+					// Usage
+					var trafficUsage = response.data.trafficUsage;
+					var uptimeUsage = response.data.uptimeUsage;
 
-					var uptimeCurrentTopupUsed = response.data.uptimeCurrentTopupUsed; // value of -1: no current topup
-					var uptimeCurrentTopupCap = response.data.uptimeCurrentTopupCap; // value of -1: no current topup
-					var uptimeTopupRemaining = response.data.uptimeTopupRemaining;
-
-					// Total up traffic
-					var trafficTotalAllowed;
-					var validTrafficTopups;
-					if (trafficCurrentTopupCap > 0) {
-						validTrafficTopups = trafficCurrentTopupCap;
-						validTrafficTopups += trafficTopupRemaining;
-					} else {
-						validTrafficTopups = trafficTopupRemaining;
-					}
-
-					if (trafficCap < 0) {
-						trafficTotalAllowed = validTrafficTopups;
-					} else {
-						trafficTotalAllowed = trafficCap + validTrafficTopups;
-					}
-
-					// Traffic usage
-					var trafficUsage = inputTotal + outputTotal;
-
-					// Total up uptime
-					var uptimeTotalAllowed;
-					var validUptimeTopups;
-					if (uptimeCurrentTopupCap > 0) {
-						validUptimeTopups = uptimeCurrentTopupCap;
-						validUptimeTopups += uptimeTopupRemaining;
-					} else {
-						validUptimeTopups = uptimeTopupRemaining;
-					}
-
-					if (uptimeCap < 0) {
-						uptimeTotalAllowed = validUptimeTopups;
-					} else {
-						uptimeTotalAllowed = uptimeCap + validUptimeTopups;
-					}
-
-					// Get summary field
-					var form = Ext.getCmp(summaryFormID);
-					var summaryTotal = Ext.getCmp(summaryTotalID);
+					// Topups
+					var trafficTopups = response.data.trafficTopups;
+					var uptimeTopups = response.data.uptimeTopups;
 
 					// Format string before printing
 					var trafficString = '';
@@ -340,16 +297,17 @@ function showAdminUserLogsWindow(id) {
 						trafficCap = 'Prepaid';
 						trafficString += sprintf('               Traffic\nCap: %s MB Topup: %d MB\n'+
 								'Usage: %d/%d MB\n=====================================\n',
-								trafficCap,validTrafficTopups,trafficUsage,trafficTotalAllowed);
+								trafficCap,trafficTopups,trafficUsage,trafficTopups);
 					// Uncapped traffic
 					} else if (trafficCap == 0) {
-						trafficString += sprintf('               Traffic\nCap: Uncapped Used: %d\n=====================================n',
+						trafficString += sprintf('               Traffic\nCap: Uncapped Used: %d MB\n=====================================\n',
 								trafficUsage);
 					// Capped traffic
 					} else {
+						var combinedTrafficCap = trafficCap + trafficTopups;
 						trafficString += sprintf('               Traffic\nCap: %d MB Topup: %d MB\n'+
 								'Usage: %d/%d MB\n=====================================\n',
-								trafficCap,validTrafficTopups,trafficUsage,trafficTotalAllowed);
+								trafficCap,trafficTopups,trafficUsage,combinedTrafficCap);
 					}
 
 					// Format string before printing
@@ -357,21 +315,25 @@ function showAdminUserLogsWindow(id) {
 					// Prepaid uptime
 					if (uptimeCap == -1) {
 						uptimeCap = 'Prepaid';
-						uptimeString += sprintf('               Uptime\nCap: %s MB Topup: %d MB\n'+
-								'Usage: %d/%d MB',
-								uptimeCap,validUptimeTopups,uptimeTotal,uptimeTotalAllowed);
+						uptimeString += sprintf('               Uptime\nCap: %s Min Topup: %d Min\n'+
+								'Usage: %d/%d Min',
+								uptimeCap,uptimeTopups,uptimeUsage,uptimeTopups);
 					// Uncapped uptime
 					} else if (uptimeCap == 0) {
-						uptimeString += sprintf('               Uptime\nCap: Uncapped Used: %d',
-								uptimeTotal);
+						uptimeString += sprintf('               Uptime\nCap: Uncapped Used: %d Min',
+								uptimeUsage);
 					// Capped uptime
 					} else {
-						uptimeString += sprintf('               Uptime\nCap: %d MB Topup: %d MB\n'+
-								'Usage: %d/%d MB',
-								uptimeCap,validUptimeTopups,uptimeTotal,uptimeTotalAllowed);
+						var combinedUptimeCap = uptimeCap + uptimeTopups;
+						uptimeString += sprintf('               Uptime\nCap: %d Min Topup: %d Min\n'+
+								'Usage: %d/%d Min',
+								uptimeCap,uptimeTopups,uptimeUsage,combinedUptimeCap);
 					}
 
-					summaryTotal.setValue(trafficString+uptimeString);
+					// Get summary field
+					var form = Ext.getCmp(summaryFormID);
+					var summaryField = Ext.getCmp(summaryTotalID);
+					summaryField.setValue(trafficString+uptimeString);
 				},
 				failure: function (result) {
 					Ext.MessageBox.alert('Failed', 'Couldn\'t fetch data: '+result.date);
diff --git a/webgui/js/app/windows/WiSPUserLogs.js b/webgui/js/app/windows/WiSPUserLogs.js
index 766388f2..04519ad3 100644
--- a/webgui/js/app/windows/WiSPUserLogs.js
+++ b/webgui/js/app/windows/WiSPUserLogs.js
@@ -226,9 +226,9 @@ function showWiSPUserLogsWindow(id) {
 			filters: [
 				{type: 'numeric', dataIndex: 'ID'},
 				{
-					type: 'date',  
+					type: 'date',
 					format: 'Y-m-d H:i:s',
-					dataIndex: 'EventTimestamp', 
+					dataIndex: 'EventTimestamp',
 					value: {
 						after: firstOfMonth,
 						before: firstOfNext
@@ -254,10 +254,8 @@ function showWiSPUserLogsWindow(id) {
 	var store = Ext.getCmp(wispUserLogsWindow.gridPanelID).getStore();
 
 	store.on('load',function() {
-		var inputTotal = store.sum('AcctInputMbyte');
-		var outputTotal = store.sum('AcctOutputMbyte');
-		var uptimeTotal = store.sum('AcctSessionTime');
 
+		// Fetch periodKey from form
 		var periodKeyField = (Ext.getCmp(formPeriodKeyID)).getValue();
 
 		// Mask parent window
@@ -280,58 +278,17 @@ function showWiSPUserLogsWindow(id) {
 				customSuccess: function (result) {
 					response = Ext.decode(result.responseText);
 
-					// Traffic variables
+					// Caps
 					var trafficCap = response.data.trafficCap; // value of -1: prepaid
-					
-					var trafficCurrentTopupUsed = response.data.trafficCurrentTopupUsed; // value of -1: no current topup
-					var trafficCurrentTopupCap = response.data.trafficCurrentTopupCap; // value of -1: no current topup
-					var trafficTopupRemaining = response.data.trafficTopupRemaining;
-
-					// Uptime variables
 					var uptimeCap = response.data.uptimeCap; // value of -1: prepaid
+					
+					// Usage
+					var trafficUsage = response.data.trafficUsage;
+					var uptimeUsage = response.data.uptimeUsage;
 
-					var uptimeCurrentTopupUsed = response.data.uptimeCurrentTopupUsed; // value of -1: no current topup
-					var uptimeCurrentTopupCap = response.data.uptimeCurrentTopupCap; // value of -1: no current topup
-					var uptimeTopupRemaining = response.data.uptimeTopupRemaining;
-
-					// Total up traffic
-					var trafficTotalAllowed;
-					var validTrafficTopups;
-					if (trafficCurrentTopupCap > 0) {
-						validTrafficTopups = trafficCurrentTopupCap;
-						validTrafficTopups += trafficTopupRemaining;
-					} else {
-						validTrafficTopups = trafficTopupRemaining;
-					}
-
-					if (trafficCap < 0) {
-						trafficTotalAllowed = validTrafficTopups;
-					} else {
-						trafficTotalAllowed = trafficCap + validTrafficTopups;
-					}
-
-					// Traffic usage
-					var trafficUsage = inputTotal + outputTotal;
-
-					// Total up uptime
-					var uptimeTotalAllowed;
-					var validUptimeTopups;
-					if (uptimeCurrentTopupCap > 0) {
-						validUptimeTopups = uptimeCurrentTopupCap;
-						validUptimeTopups += uptimeTopupRemaining;
-					} else {
-						validUptimeTopups = uptimeTopupRemaining;
-					}
-
-					if (uptimeCap < 0) {
-						uptimeTotalAllowed = validUptimeTopups;
-					} else {
-						uptimeTotalAllowed = uptimeCap + validUptimeTopups;
-					}
-
-					// Get summary field
-					var form = Ext.getCmp(summaryFormID);
-					var summaryTotal = Ext.getCmp(summaryTotalID);
+					// Topups
+					var trafficTopups = response.data.trafficTopups;
+					var uptimeTopups = response.data.uptimeTopups;
 
 					// Format string before printing
 					var trafficString = '';
@@ -340,16 +297,17 @@ function showWiSPUserLogsWindow(id) {
 						trafficCap = 'Prepaid';
 						trafficString += sprintf('               Traffic\nCap: %s MB Topup: %d MB\n'+
 								'Usage: %d/%d MB\n=====================================\n',
-								trafficCap,validTrafficTopups,trafficUsage,trafficTotalAllowed);
+								trafficCap,trafficTopups,trafficUsage,trafficTopups);
 					// Uncapped traffic
 					} else if (trafficCap == 0) {
-						trafficString += sprintf('               Traffic\nCap: Uncapped Used: %d\n=====================================n',
+						trafficString += sprintf('               Traffic\nCap: Uncapped Used: %d MB\n=====================================\n',
 								trafficUsage);
 					// Capped traffic
 					} else {
+						var combinedTrafficCap = trafficCap + trafficTopups;
 						trafficString += sprintf('               Traffic\nCap: %d MB Topup: %d MB\n'+
 								'Usage: %d/%d MB\n=====================================\n',
-								trafficCap,validTrafficTopups,trafficUsage,trafficTotalAllowed);
+								trafficCap,trafficTopups,trafficUsage,combinedTrafficCap);
 					}
 
 					// Format string before printing
@@ -357,21 +315,25 @@ function showWiSPUserLogsWindow(id) {
 					// Prepaid uptime
 					if (uptimeCap == -1) {
 						uptimeCap = 'Prepaid';
-						uptimeString += sprintf('               Uptime\nCap: %s MB Topup: %d MB\n'+
-								'Usage: %d/%d MB',
-								uptimeCap,validUptimeTopups,uptimeTotal,uptimeTotalAllowed);
+						uptimeString += sprintf('               Uptime\nCap: %s Min Topup: %d Min\n'+
+								'Usage: %d/%d Min',
+								uptimeCap,uptimeTopups,uptimeUsage,uptimeTopups);
 					// Uncapped uptime
 					} else if (uptimeCap == 0) {
-						uptimeString += sprintf('               Uptime\nCap: Uncapped Used: %d',
-								uptimeTotal);
+						uptimeString += sprintf('               Uptime\nCap: Uncapped Used: %d Min',
+								uptimeUsage);
 					// Capped uptime
 					} else {
-						uptimeString += sprintf('               Uptime\nCap: %d MB Topup: %d MB\n'+
-								'Usage: %d/%d MB',
-								uptimeCap,validUptimeTopups,uptimeTotal,uptimeTotalAllowed);
+						var combinedUptimeCap = uptimeCap + uptimeTopups;
+						uptimeString += sprintf('               Uptime\nCap: %d Min Topup: %d Min\n'+
+								'Usage: %d/%d Min',
+								uptimeCap,uptimeTopups,uptimeUsage,combinedUptimeCap);
 					}
 
-					summaryTotal.setValue(trafficString+uptimeString);
+					// Get summary field
+					var form = Ext.getCmp(summaryFormID);
+					var summaryField = Ext.getCmp(summaryTotalID);
+					summaryField.setValue(trafficString+uptimeString);
 				},
 				failure: function (result) {
 					Ext.MessageBox.alert('Failed', 'Couldn\'t fetch data: '+result.date);
-- 
GitLab