diff --git a/webgui/include/ajax/functions/AdminUserLogs.php b/webgui/include/ajax/functions/AdminUserLogs.php index 2e192fe2e731aa2c999e12de4d26a028b11e4454..5f6587df6fca96d4f7f66eb76fc1e11013c03cce 100644 --- a/webgui/include/ajax/functions/AdminUserLogs.php +++ b/webgui/include/ajax/functions/AdminUserLogs.php @@ -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']; + } } } diff --git a/webgui/include/ajax/functions/WiSPUserLogs.php b/webgui/include/ajax/functions/WiSPUserLogs.php index 32c386653ac0e2a1f8e069cab6a585e48b8febd3..dfa292b1ffe57d46e013c8f6ff6f14e3fbe30fd3 100644 --- a/webgui/include/ajax/functions/WiSPUserLogs.php +++ b/webgui/include/ajax/functions/WiSPUserLogs.php @@ -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']; + } } } diff --git a/webui/user/index.php b/webui/user/index.php index 04b1552f9166733d14a8dd4ecadbb7f29c72a51a..213e814b7633574ca2635a2af5ccd2b47a9afba3 100644 --- a/webui/user/index.php +++ b/webui/user/index.php @@ -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'];