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