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

Renamed $userName to $username

Use built in DateTime methods
Added SMAdminDepletedOn functionality and columns
parent 67f10471
No related branches found
No related tags found
No related merge requests found
...@@ -168,6 +168,7 @@ CREATE TABLE @PREFIX@topups ( ...@@ -168,6 +168,7 @@ CREATE TABLE @PREFIX@topups (
Value @INT_UNSIGNED@, Value @INT_UNSIGNED@,
Depleted SMALLINT NOT NULL DEFAULT '0', Depleted SMALLINT NOT NULL DEFAULT '0',
SMAdminDepletedOn DATETIME,
FOREIGN KEY (UserID) REFERENCES @PREFIX@users(ID) FOREIGN KEY (UserID) REFERENCES @PREFIX@users(ID)
) @CREATE_TABLE_SUFFIX@; ) @CREATE_TABLE_SUFFIX@;
...@@ -183,6 +184,7 @@ CREATE TABLE @PREFIX@topups_summary ( ...@@ -183,6 +184,7 @@ CREATE TABLE @PREFIX@topups_summary (
Balance @INT_UNSIGNED@, Balance @INT_UNSIGNED@,
Depleted SMALLINT NOT NULL DEFAULT '0', Depleted SMALLINT NOT NULL DEFAULT '0',
SMAdminDepletedOn DATETIME,
FOREIGN KEY (TopupID) REFERENCES @PREFIX@topups(ID) FOREIGN KEY (TopupID) REFERENCES @PREFIX@topups(ID)
) @CREATE_TABLE_SUFFIX@; ) @CREATE_TABLE_SUFFIX@;
......
...@@ -299,39 +299,27 @@ sub cleanup ...@@ -299,39 +299,27 @@ sub cleanup
# The datetime now # The datetime now
my $now = DateTime->now->set_time_zone($server->{'smradius'}->{'event_timezone'}); 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 ); my $thisMonth = DateTime->new( year => $now->year, month => $now->month, day => 1 );
# Get begin date of last month # Last month..
my ($prevYear,$prevMonth); my $lastMonth = $thisMonth->clone()->subtract( months => 1 );
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 );
my $prevPeriodKey = $lastMonth->strftime("%Y-%m"); my $prevPeriodKey = $lastMonth->strftime("%Y-%m");
# Get begin date of next month # Next month..
my ($folYear,$folMonth); my $nextMonth = $thisMonth->clone()->add( months => 1 );
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 );
my $unix_nextMonth = $nextMonth->epoch(); 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 # Start of multiple queries
DBBegin(); DBBegin();
# Loop through users # Loop through users
foreach my $userID (keys %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 # TODO - in future we must be more dynamic, we may not be using SQL accunting
...@@ -349,7 +337,7 @@ sub cleanup ...@@ -349,7 +337,7 @@ sub cleanup
PeriodKey = ? PeriodKey = ?
AND Username = ? AND Username = ?
', ',
$prevPeriodKey,$userName $prevPeriodKey,$username
); );
if (!$sth) { if (!$sth) {
...@@ -438,11 +426,11 @@ sub cleanup ...@@ -438,11 +426,11 @@ sub cleanup
if (defined($row->{'Value'}) && $row->{'Value'} =~ /^[\d]+$/) { if (defined($row->{'Value'}) && $row->{'Value'} =~ /^[\d]+$/) {
$capRecord{'TrafficLimit'} = $row->{'Value'}; $capRecord{'TrafficLimit'} = $row->{'Value'};
} else { } 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 { } else {
$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => Incorrect '".$row->{'Name'}."' operator '" $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') { if ($row->{'Name'} eq 'SMRadius-Capping-Uptime-Limit') {
...@@ -450,11 +438,11 @@ sub cleanup ...@@ -450,11 +438,11 @@ sub cleanup
if (defined($row->{'Value'}) && $row->{'Value'} =~ /^[\d]+$/) { if (defined($row->{'Value'}) && $row->{'Value'} =~ /^[\d]+$/) {
$capRecord{'UptimeLimit'} = $row->{'Value'}; $capRecord{'UptimeLimit'} = $row->{'Value'};
} else { } 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 { } else {
$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => Incorrect '".$row->{'Name'}."' operator '" $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 ...@@ -494,11 +482,11 @@ sub cleanup
if (defined($row->{'Value'}) && $row->{'Value'} =~ /^[\d]+$/) { if (defined($row->{'Value'}) && $row->{'Value'} =~ /^[\d]+$/) {
$capRecord{'TrafficLimit'} = $row->{'Value'}; $capRecord{'TrafficLimit'} = $row->{'Value'};
} else { } 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 { } else {
$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => Incorrect '".$row->{'Name'}."' operator '" $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') { if ($row->{'Name'} eq 'SMRadius-Capping-Uptime-Limit') {
...@@ -506,11 +494,11 @@ sub cleanup ...@@ -506,11 +494,11 @@ sub cleanup
if (defined($row->{'Value'}) && $row->{'Value'} =~ /^[\d]+$/) { if (defined($row->{'Value'}) && $row->{'Value'} =~ /^[\d]+$/) {
$capRecord{'UptimeLimit'} = $row->{'Value'}; $capRecord{'UptimeLimit'} = $row->{'Value'};
} else { } 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 { } else {
$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => Incorrect '".$row->{'Name'}."' operator '" $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 ...@@ -909,11 +897,12 @@ sub cleanup
UPDATE UPDATE
@TP@topups @TP@topups
SET SET
Depleted = 1 Depleted = 1,
SMAdminDepletedOn = ?
WHERE WHERE
ID = ? ID = ?
', ',
$topupID $depletedTimestamp,$topupID
); );
if (!$sth) { if (!$sth) {
$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => Failed to update topups: ". $server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => Failed to update topups: ".
...@@ -929,11 +918,12 @@ sub cleanup ...@@ -929,11 +918,12 @@ sub cleanup
UPDATE UPDATE
@TP@topups_summary @TP@topups_summary
SET SET
Depleted = 1 Depleted = 1,
SMAdminDepletedOn = ?
WHERE WHERE
TopupID = ? TopupID = ?
', ',
$topupID $depletedTimestamp,$topupID
); );
if (!$sth) { if (!$sth) {
$server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => Failed to update topups_summary: ". $server->log(LOG_ERR,"[MOD_CONFIG_SQL_TOPUPS] Cleanup => Failed to update topups_summary: ".
......
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