From 3334893d78170f7bfdb8a1ee0186c6623d45aee6 Mon Sep 17 00:00:00 2001
From: Robert Anderson <randerson@lbsd.net>
Date: Wed, 30 Jun 2010 14:46:18 +0000
Subject: [PATCH] Renamed $userName to $username Use built in DateTime methods
 Added SMAdminDepletedOn functionality and columns

---
 database/core.tsql                            |  2 +
 .../modules/system/mod_config_sql_topups.pm   | 60 ++++++++-----------
 2 files changed, 27 insertions(+), 35 deletions(-)

diff --git a/database/core.tsql b/database/core.tsql
index 9ae65665..a179dca9 100644
--- a/database/core.tsql
+++ b/database/core.tsql
@@ -168,6 +168,7 @@ CREATE TABLE @PREFIX@topups (
 	Value			@INT_UNSIGNED@,
 
 	Depleted		SMALLINT NOT NULL DEFAULT '0',
+	SMAdminDepletedOn	DATETIME,
 
 	FOREIGN KEY (UserID) REFERENCES @PREFIX@users(ID)
 ) @CREATE_TABLE_SUFFIX@;
@@ -183,6 +184,7 @@ CREATE TABLE @PREFIX@topups_summary (
 	Balance			@INT_UNSIGNED@,
 
 	Depleted		SMALLINT NOT NULL DEFAULT '0',
+	SMAdminDepletedOn	DATETIME,
 
 	FOREIGN KEY (TopupID) REFERENCES @PREFIX@topups(ID)
 ) @CREATE_TABLE_SUFFIX@;
diff --git a/smradius/modules/system/mod_config_sql_topups.pm b/smradius/modules/system/mod_config_sql_topups.pm
index c5ed1c6d..4dec37a3 100644
--- a/smradius/modules/system/mod_config_sql_topups.pm
+++ b/smradius/modules/system/mod_config_sql_topups.pm
@@ -299,39 +299,27 @@ sub cleanup
 
 	# The datetime now
 	my $now = DateTime->now->set_time_zone($server->{'smradius'}->{'event_timezone'});
-	# Make datetime
+
+	# This month..
 	my $thisMonth = DateTime->new( year => $now->year, month => $now->month, day => 1 );
 
-	# Get begin date of last month
-	my ($prevYear,$prevMonth);
-	if ($now->month == 1) {
-		$prevYear = $now->year - 1;
-		$prevMonth = 12;
-	} else {
-		$prevYear = $now->year;
-		$prevMonth = $now->month - 1;
-	}
-	my $lastMonth = DateTime->new( year => $prevYear, month => $prevMonth, day => 1 );
+	# Last month..
+	my $lastMonth = $thisMonth->clone()->subtract( months => 1 );
 	my $prevPeriodKey = $lastMonth->strftime("%Y-%m");
 
-	# Get begin date of next month
-	my ($folYear,$folMonth);
-	if ($now->month == 12) {
-		$folYear = $now->year + 1;
-		$folMonth = 1;
-	} else {
-		$folYear = $now->year;
-		$folMonth = $now->month + 1;
-	}
-	my $nextMonth = DateTime->new( year => $folYear, month => $folMonth, day => 1 );
+	# Next month..
+	my $nextMonth = $thisMonth->clone()->add( months => 1 );
 	my $unix_nextMonth = $nextMonth->epoch();
 
+	# Get a timestamp for this user
+	my $depletedTimestamp = $now->strftime('%Y-%m-%d %H:%M:%S');
+
 	# Start of multiple queries
 	DBBegin();
 
 	# Loop through users
 	foreach my $userID (keys %users) {
-		my $userName = $users{$userID};
+		my $username = $users{$userID};
 
 		# TODO - in future we must be more dynamic, we may not be using SQL accunting
 
@@ -349,7 +337,7 @@ sub cleanup
 				PeriodKey = ?
 				AND Username = ?
 			',
-			$prevPeriodKey,$userName
+			$prevPeriodKey,$username
 		);
 
 		if (!$sth) {
@@ -438,11 +426,11 @@ sub cleanup
 						if (defined($row->{'Value'}) && $row->{'Value'} =~ /^[\d]+$/) {
 							$capRecord{'TrafficLimit'} = $row->{'Value'};
 						} else {
-							$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => SMRadius-Capping-Traffic-Limit value invalid for user '".$userName."'");
+							$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => SMRadius-Capping-Traffic-Limit value invalid for user '".$username."'");
 						}
 					} else {
 						$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => Incorrect '".$row->{'Name'}."' operator '"
-								.$row->{'Operator'}."' used  for user '".$userName."'");
+								.$row->{'Operator'}."' used  for user '".$username."'");
 					}
 				}
 				if ($row->{'Name'} eq 'SMRadius-Capping-Uptime-Limit') {
@@ -450,11 +438,11 @@ sub cleanup
 						if (defined($row->{'Value'}) && $row->{'Value'} =~ /^[\d]+$/) {
 							$capRecord{'UptimeLimit'} = $row->{'Value'};
 						} else {
-							$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => SMRadius-Capping-Uptime-Limit value invalid for user '".$userName."'");
+							$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => SMRadius-Capping-Uptime-Limit value invalid for user '".$username."'");
 						}
 					} else {
 						$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => Incorrect '".$row->{'Name'}."' operator '"
-								.$row->{'Operator'}."' used  for user '".$userName."'");
+								.$row->{'Operator'}."' used  for user '".$username."'");
 					}
 				}
 			}
@@ -494,11 +482,11 @@ sub cleanup
 						if (defined($row->{'Value'}) && $row->{'Value'} =~ /^[\d]+$/) {
 							$capRecord{'TrafficLimit'} = $row->{'Value'};
 						} else {
-							$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => SMRadius-Capping-Traffic-Limit value invalid for user '".$userName."'");
+							$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => SMRadius-Capping-Traffic-Limit value invalid for user '".$username."'");
 						}
 					} else {
 						$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => Incorrect '".$row->{'Name'}."' operator '"
-								.$row->{'Operator'}."' used  for user '".$userName."'");
+								.$row->{'Operator'}."' used  for user '".$username."'");
 					}
 				}
 				if ($row->{'Name'} eq 'SMRadius-Capping-Uptime-Limit') {
@@ -506,11 +494,11 @@ sub cleanup
 						if (defined($row->{'Value'}) && $row->{'Value'} =~ /^[\d]+$/) {
 							$capRecord{'UptimeLimit'} = $row->{'Value'};
 						} else {
-							$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => SMRadius-Capping-Uptime-Limit value invalid for user '".$userName."'");
+							$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => SMRadius-Capping-Uptime-Limit value invalid for user '".$username."'");
 						}
 					} else {
 						$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => Incorrect '".$row->{'Name'}."' operator '"
-								.$row->{'Operator'}."' used  for user '".$userName."'");
+								.$row->{'Operator'}."' used  for user '".$username."'");
 					}
 				}
 			}
@@ -909,11 +897,12 @@ sub cleanup
 				UPDATE
 					@TP@topups
 				SET
-					Depleted = 1
+					Depleted = 1,
+					SMAdminDepletedOn = ?
 				WHERE
 					ID = ?
 				',
-				$topupID
+				$depletedTimestamp,$topupID
 			);
 			if (!$sth) {
 				$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => Failed to update topups: ".
@@ -929,11 +918,12 @@ sub cleanup
 				UPDATE
 					@TP@topups_summary
 				SET
-					Depleted = 1
+					Depleted = 1,
+					SMAdminDepletedOn = ?
 				WHERE
 					TopupID = ?
 				',
-				$topupID
+				$depletedTimestamp,$topupID
 			);
 			if (!$sth) {
 				$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => Failed to update topups_summary: ".
-- 
GitLab