Skip to content
Snippets Groups Projects
Commit d838b678 authored by Robert Anderson's avatar Robert Anderson
Browse files

Simplified topup query

Results for admin interface are now in line with user interface
parent 624035b5
No related branches found
No related tags found
No related merge requests found
......@@ -41,12 +41,12 @@ function getAdminUserLogsSummary($params) {
# Get user attributes
$res = DBSelect("
SELECT
@TP@user_attributes.Name,
@TP@user_attributes.Value
Name,
Value
FROM
@TP@user_attributes
WHERE
@TP@user_attributes.UserID = ?",
UserID = ?",
array($params[0]['ID'])
);
......@@ -91,8 +91,9 @@ function getAdminUserLogsSummary($params) {
@TP@topups_summary.TopupID = @TP@topups.ID
AND @TP@topups.UserID = ?
AND @TP@topups_summary.PeriodKey = ?
AND @TP@topups_summary.Depleted = 0
ORDER BY
@TP@topups.Timestamp",
@TP@topups.Timestamp ASC",
array($params[0]['ID'],$periodKey->format('Y-m'))
);
......@@ -107,8 +108,8 @@ function getAdminUserLogsSummary($params) {
while ($row = $res->fetchObject()) {
$topups[$i] = array();
$topups[$i]['Type'] = $row->type;
$topups[$i]['Limit'] = $row->balance;
$topups[$i]['OriginalLimit'] = $row->value;
$topups[$i]['CurrentLimit'] = $row->balance;
$topups[$i]['Limit'] = $row->value;
$i++;
}
......@@ -121,11 +122,12 @@ function getAdminUserLogsSummary($params) {
FROM
@TP@topups
WHERE
@TP@topups.UserID = ?
AND @TP@topups.ValidFrom = ?
AND @TP@topups.ValidTo >= ?
UserID = ?
AND ValidFrom = ?
AND ValidTo >= ?
AND Depleted = 0
ORDER BY
@TP@topups.Timestamp",
Timestamp ASC",
array($params[0]['ID'],$periodKey->format('Y-m-d'),$periodKeyEnd->format('Y-m-d'))
);
......@@ -175,15 +177,87 @@ function getAdminUserLogsSummary($params) {
$resultArray['uptimeUsage'] += $row->totalsessiontime;
}
# Excess usage
$excessTraffic = 0;
if ($trafficCap == -1) {
$excessTraffic = $resultArray['trafficUsage'];
} else {
$excessTraffic = $resultArray['trafficUsage'] > $trafficCap ? ($resultArray['trafficUsage'] - $trafficCap) : 0;
}
$excessUptime = 0;
if ($uptimeCap == -1) {
$excessUptime = $resultArray['uptimeUsage'];
} else {
$excessUptime = $resultArray['uptimeUsage'] > $uptimeCap ? ($resultArray['uptimeUsage'] - $uptimeCap) : 0;
}
# 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'];
# Topup not currently in use
if ($excessTraffic <= 0) {
$resultArray['trafficUsage'] += isset($topupItem['CurrentLimit']) ? ($topupItem['Limit'] - $topupItem['CurrentLimit']) : 0;
# Set total available topups
$resultArray['trafficTopups'] += $topupItem['Limit'];
# Topup currently in use
} elseif (!isset($topupItem['CurrentLimit']) && $excessTraffic < $topupItem['Limit']) {
# Set total available topups
$resultArray['trafficTopups'] += $topupItem['Limit'];
# If we hit this topup then all the rest of them are available
$excessTraffic = 0;
} elseif (isset($topupItem['CurrentLimit']) && $excessTraffic < $topupItem['CurrentLimit']) {
$resultArray['trafficUsage'] += ($topupItem['Limit'] - $topupItem['CurrentLimit']);
# Set total available topups
$resultArray['trafficTopups'] += $topupItem['Limit'];
# If we hit this topup then all the rest of them are available
$excessTraffic = 0;
# Topup has been used up
} else {
$resultArray['trafficUsage'] += isset($topupItem['CurrentLimit']) ? ($topupItem['Limit'] - $topupItem['CurrentLimit']) : 0;
$resultArray['trafficTopups'] += $topupItem['Limit'];
# Subtract this topup from excessTraffic usage
$excessTraffic -= isset($topupItem['CurrentLimit']) ? $topupItem['CurrentLimit'] : $topupItem['Limit'];
}
}
if ($topupItem['Type'] == 2) {
$resultArray['uptimeTopups'] += $topupItem['Limit'];
# Topup not currently in use
if ($excessUptime <= 0) {
$resultArray['uptimeUsage'] += isset($topupItem['CurrentLimit']) ? ($topupItem['Limit'] - $topupItem['CurrentLimit']) : 0;
# Set total available topups
$resultArray['uptimeTopups'] += $topupItem['Limit'];
# Topup currently in use
} elseif (!isset($topupItem['CurrentLimit']) && $excessUptime < $topupItem['Limit']) {
# Set total available topups
$resultArray['uptimeTopups'] += $topupItem['Limit'];
# If we hit this topup then all the rest of them are available
$excessUptime = 0;
} elseif (isset($topupItem['CurrentLimit']) && $excessUptime < $topupItem['CurrentLimit']) {
$resultArray['uptimeUsage'] += ($topupItem['Limit'] - $topupItem['CurrentLimit']);
# Set total available topups
$resultArray['uptimeTopups'] += $topupItem['Limit'];
# If we hit this topup then all the rest of them are available
$excessUptime = 0;
# Topup has been used up
} else {
$resultArray['uptimeUsage'] += isset($topupItem['CurrentLimit']) ? ($topupItem['Limit'] - $topupItem['CurrentLimit']) : 0;
$resultArray['uptimeTopups'] += $topupItem['Limit'];
# Subtract this topup from excessUptime usage
$excessUptime -= isset($topupItem['CurrentLimit']) ? $topupItem['CurrentLimit'] : $topupItem['Limit'];
}
}
}
......
......@@ -41,12 +41,12 @@ function getWiSPUserLogsSummary($params) {
# Get user attributes
$res = DBSelect("
SELECT
@TP@user_attributes.Name,
@TP@user_attributes.Value
Name,
Value
FROM
@TP@user_attributes
WHERE
@TP@user_attributes.UserID = ?",
UserID = ?",
array($params[0]['ID'])
);
......@@ -91,8 +91,9 @@ function getWiSPUserLogsSummary($params) {
@TP@topups_summary.TopupID = @TP@topups.ID
AND @TP@topups.UserID = ?
AND @TP@topups_summary.PeriodKey = ?
AND @TP@topups_summary.Depleted = 0
ORDER BY
@TP@topups.Timestamp",
@TP@topups.Timestamp ASC",
array($params[0]['ID'],$periodKey->format('Y-m'))
);
......@@ -107,8 +108,8 @@ function getWiSPUserLogsSummary($params) {
while ($row = $res->fetchObject()) {
$topups[$i] = array();
$topups[$i]['Type'] = $row->type;
$topups[$i]['Limit'] = $row->balance;
$topups[$i]['OriginalLimit'] = $row->value;
$topups[$i]['CurrentLimit'] = $row->balance;
$topups[$i]['Limit'] = $row->value;
$i++;
}
......@@ -121,11 +122,12 @@ function getWiSPUserLogsSummary($params) {
FROM
@TP@topups
WHERE
@TP@topups.UserID = ?
AND @TP@topups.ValidFrom = ?
AND @TP@topups.ValidTo >= ?
UserID = ?
AND ValidFrom = ?
AND ValidTo >= ?
AND Depleted = 0
ORDER BY
@TP@topups.Timestamp",
Timestamp ASC",
array($params[0]['ID'],$periodKey->format('Y-m-d'),$periodKeyEnd->format('Y-m-d'))
);
......@@ -175,15 +177,87 @@ function getWiSPUserLogsSummary($params) {
$resultArray['uptimeUsage'] += $row->totalsessiontime;
}
# Excess usage
$excessTraffic = 0;
if ($trafficCap == -1) {
$excessTraffic = $resultArray['trafficUsage'];
} else {
$excessTraffic = $resultArray['trafficUsage'] > $trafficCap ? ($resultArray['trafficUsage'] - $trafficCap) : 0;
}
$excessUptime = 0;
if ($uptimeCap == -1) {
$excessUptime = $resultArray['uptimeUsage'];
} else {
$excessUptime = $resultArray['uptimeUsage'] > $uptimeCap ? ($resultArray['uptimeUsage'] - $uptimeCap) : 0;
}
# 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'];
# Topup not currently in use
if ($excessTraffic <= 0) {
$resultArray['trafficUsage'] += isset($topupItem['CurrentLimit']) ? ($topupItem['Limit'] - $topupItem['CurrentLimit']) : 0;
# Set total available topups
$resultArray['trafficTopups'] += $topupItem['Limit'];
# Topup currently in use
} elseif (!isset($topupItem['CurrentLimit']) && $excessTraffic < $topupItem['Limit']) {
# Set total available topups
$resultArray['trafficTopups'] += $topupItem['Limit'];
# If we hit this topup then all the rest of them are available
$excessTraffic = 0;
} elseif (isset($topupItem['CurrentLimit']) && $excessTraffic < $topupItem['CurrentLimit']) {
$resultArray['trafficUsage'] += ($topupItem['Limit'] - $topupItem['CurrentLimit']);
# Set total available topups
$resultArray['trafficTopups'] += $topupItem['Limit'];
# If we hit this topup then all the rest of them are available
$excessTraffic = 0;
# Topup has been used up
} else {
$resultArray['trafficUsage'] += isset($topupItem['CurrentLimit']) ? ($topupItem['Limit'] - $topupItem['CurrentLimit']) : 0;
$resultArray['trafficTopups'] += $topupItem['Limit'];
# Subtract this topup from excessTraffic usage
$excessTraffic -= isset($topupItem['CurrentLimit']) ? $topupItem['CurrentLimit'] : $topupItem['Limit'];
}
}
if ($topupItem['Type'] == 2) {
$resultArray['uptimeTopups'] += $topupItem['Limit'];
# Topup not currently in use
if ($excessUptime <= 0) {
$resultArray['uptimeUsage'] += isset($topupItem['CurrentLimit']) ? ($topupItem['Limit'] - $topupItem['CurrentLimit']) : 0;
# Set total available topups
$resultArray['uptimeTopups'] += $topupItem['Limit'];
# Topup currently in use
} elseif (!isset($topupItem['CurrentLimit']) && $excessUptime < $topupItem['Limit']) {
# Set total available topups
$resultArray['uptimeTopups'] += $topupItem['Limit'];
# If we hit this topup then all the rest of them are available
$excessUptime = 0;
} elseif (isset($topupItem['CurrentLimit']) && $excessUptime < $topupItem['CurrentLimit']) {
$resultArray['uptimeUsage'] += ($topupItem['Limit'] - $topupItem['CurrentLimit']);
# Set total available topups
$resultArray['uptimeTopups'] += $topupItem['Limit'];
# If we hit this topup then all the rest of them are available
$excessUptime = 0;
# Topup has been used up
} else {
$resultArray['uptimeUsage'] += isset($topupItem['CurrentLimit']) ? ($topupItem['Limit'] - $topupItem['CurrentLimit']) : 0;
$resultArray['uptimeTopups'] += $topupItem['Limit'];
# Subtract this topup from excessUptime usage
$excessUptime -= isset($topupItem['CurrentLimit']) ? $topupItem['CurrentLimit'] : $topupItem['Limit'];
}
}
}
......
......@@ -63,13 +63,13 @@ function displayDetails() {
SELECT
SUM(AcctSessionTime) / 60 AS AcctSessionTime,
SUM(AcctInputOctets) / 1024 / 1024 +
SUM(AcctInputGigawords) * 4096 AS AcctInputTraffic,
SUM(AcctInputGigawords) * 4096 +
SUM(AcctOutputOctets) / 1024 / 1024 +
SUM(AcctOutputGigawords) * 4096 AS AcctOutputTraffic
SUM(AcctOutputGigawords) * 4096 AS TotalTraffic
FROM
${DB_TABLE_PREFIX}accounting
WHERE
Username = ".$db->quote($_SESSION['username'])."
Username = ".$db->quote($username)."
AND
PeriodKey = ".$db->quote($currentMonth)."
";
......@@ -85,15 +85,10 @@ function displayDetails() {
# Pull in row
$row = $res->fetchObject();
# Traffic in
if (isset($row->acctinputtraffic) && $row->acctinputtraffic > 0) {
$totalTraffic += $row->acctinputtraffic;
}
# Traffic out
if (isset($row->acctoutputtraffic) && $row->acctoutputtraffic > 0) {
$totalTraffic += $row->acctoutputtraffic;
# Traffic
if (isset($row->totaltraffic) && $row->totaltraffic > 0) {
$totalTraffic += $row->totaltraffic;
}
# Uptime
if (isset($row->acctsessiontime) && $row->acctsessiontime > 0) {
$totalUptime += $row->acctsessiontime;
......@@ -167,7 +162,7 @@ function displayDetails() {
AND ${DB_TABLE_PREFIX}topups_summary.PeriodKey = ".$db->quote($currentMonth)."
AND ${DB_TABLE_PREFIX}topups_summary.Depleted = 0
ORDER BY
${DB_TABLE_PREFIX}topups.Timestamp
${DB_TABLE_PREFIX}topups.Timestamp ASC
";
$res = $db->query($sql);
if (!(is_object($res))) {
......@@ -204,7 +199,7 @@ function displayDetails() {
AND ValidTo >= ".$db->quote($now)."
AND Depleted = 0
ORDER BY
Timestamp
Timestamp ASC
";
$res = $db->query($sql);
if (!(is_object($res))) {
......@@ -223,7 +218,8 @@ function displayDetails() {
}
# Calculate topup usage for prepaid and normal users
if (!($trafficCap === "Unlimited")) {
$totalTrafficTopupsAvail = 0;
if (!(is_numeric($trafficCap) && $trafficCap == 0)) {
# Excess usage
$excess = 0;
......@@ -234,7 +230,6 @@ function displayDetails() {
}
# Loop through all valid topups
$totalTrafficTopupsAvail = 0;
$trafficRows = array();
$i = 0;
foreach ($topups as $topup) {
......@@ -242,6 +237,11 @@ function displayDetails() {
# Traffic topups
if ($topup['Type'] == 1) {
# Note this usage from previous topups as well
if (isset($topup['CurrentLimit'])) {
$totalTraffic += ($topup['Limit'] - $topup['CurrentLimit']);
}
# Topup not currently in use
if ($excess <= 0) {
$trafficRows[$i] = array();
......@@ -290,6 +290,11 @@ function displayDetails() {
# Set total available topups
$totalTrafficTopupsAvail += $topup['Limit'];
# Set current topup
$currentTrafficTopup = array();
$currentTrafficTopup['Used'] = ($topup['Limit'] - $topup['CurrentLimit']) + $excess;
$currentTrafficTopup['Cap'] = $topup['Limit'];
# If we hit this topup then all the rest of them are available
$excess = 0;
......@@ -304,6 +309,9 @@ function displayDetails() {
$trafficRows[$i]['ValidFrom'] = $topup['ValidFrom'];
$trafficRows[$i]['Expires'] = $topup['Expires'];
# Set total available topups
$totalTrafficTopupsAvail += $topup['Limit'];
# Subtract this topup from excess usage
$excess -= $topup['Limit'];
......@@ -314,7 +322,8 @@ function displayDetails() {
}
# Calculate topup usage for prepaid and normal users
if (!($uptimeCap === "Unlimited")) {
$totalUptimeTopupsAvail = 0;
if (!(is_numeric($uptimeCap) && $uptimeCap == 0)) {
# Excess usage
$excess = 0;
......@@ -325,7 +334,6 @@ function displayDetails() {
}
# Loop through all valid topups
$totalUptimeTopupsAvail = 0;
$uptimeRows = array();
$i = 0;
foreach ($topups as $topup) {
......@@ -333,6 +341,11 @@ function displayDetails() {
# Uptime topups
if ($topup['Type'] == 2) {
# Note this usage from previous topups as well
if (isset($topup['CurrentLimit'])) {
$totalUptime += ($topup['Limit'] - $topup['CurrentLimit']);
}
# Topup not currently in use
if ($excess <= 0) {
$uptimeRows[$i] = array();
......@@ -381,6 +394,11 @@ function displayDetails() {
# Set total available topups
$totalUptimeTopupsAvail += $topup['Limit'];
# Set current topup
$currentUptimeTopup = array();
$currentUptimeTopup['Used'] = ($topup['Limit'] - $topup['CurrentLimit']) + $excess;
$currentUptimeTopup['Cap'] = $topup['Limit'];
# If we hit this topup then all the rest of them are available
$excess = 0;
......@@ -395,6 +413,9 @@ function displayDetails() {
$uptimeRows[$i]['ValidFrom'] = $topup['ValidFrom'];
$uptimeRows[$i]['Expires'] = $topup['Expires'];
# Set total available topups
$totalUptimeTopupsAvail += $topup['Limit'];
# Subtract this topup from excess usage
$excess -= $topup['Limit'];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment