From 4ca90fd71df1283e1e087b4f68c3f353ad69a7f0 Mon Sep 17 00:00:00 2001
From: Robert Anderson <randerson@lbsd.net>
Date: Tue, 25 Aug 2009 07:17:51 +0000
Subject: [PATCH] Update accounting summaries if required Updated TODO

---
 TODO                                          |   2 +-
 .../modules/accounting/mod_accounting_sql.pm  | 114 +++++++++++++-----
 2 files changed, 88 insertions(+), 28 deletions(-)

diff --git a/TODO b/TODO
index 850c988d..cef75ed8 100644
--- a/TODO
+++ b/TODO
@@ -7,4 +7,4 @@ usage related queries:
 * Use Math module to perform calculations
 
 smadmin:
-* Update or possibly ignore entries instead of deleting
+* Update or possibly ignore entries instead of deleting (topup summaries)
diff --git a/smradius/modules/accounting/mod_accounting_sql.pm b/smradius/modules/accounting/mod_accounting_sql.pm
index 1e19f891..28c1a15e 100644
--- a/smradius/modules/accounting/mod_accounting_sql.pm
+++ b/smradius/modules/accounting/mod_accounting_sql.pm
@@ -683,37 +683,97 @@ sub cleanup
 
 		my @insertArray;
 		for (my $i = 0; $i < $index; $i++) {
-			@insertArray = (
-				$allRecords[$i]->{'Username'},
+
+			# Check if this record exists
+			my $sth = DBSelect('
+				SELECT
+					COUNT(*) as rowCount
+				FROM
+					@TP@accounting
+				WHERE
+					PeriodKey = ?
+					AND Username = ?
+				',
 				$allRecords[$i]->{'PeriodKey'},
-				$allRecords[$i]->{'SessionTime'},
-				$allRecords[$i]->{'InputOctets'},
-				$allRecords[$i]->{'InputGigawords'},
-				$allRecords[$i]->{'OutputOctets'},
-				$allRecords[$i]->{'OutputGigawords'}
+				$allRecords[$i]->{'Username'}
 			);
 
-			@dbDoParams = ('
-				INSERT INTO
-					@TP@accounting_summary
-				(
-					Username,
-					PeriodKey,
-					AcctSessionTime,
-					AcctInputOctets,
-					AcctInputGigawords,
-					AcctOutputOctets,
-					AcctOutputGigawords
-				)
-				VALUES
-					(?,?,?,?,?,?,?)
-				',
-				@insertArray
+			if (!$sth) {
+				$server->log(LOG_ERR,"[MOD_ACCOUNTING_SQL] Cleanup => Failed to check for existing record: ".
+						awitpt::db::dblayer::Error());
+				return;
+			}
+
+			my $recordCheck = $sth->fetchrow_hashref();
+			$recordCheck = hashifyLCtoMC(
+				$recordCheck,
+				qw(rowCount)
 			);
 
-			if ($sth) {
-				# Do query
-				$sth = DBDo(@dbDoParams);
+			if (defined($recordCheck->{'rowCount'}) && $recordCheck->{'rowCount'} > 0) {
+				@insertArray = (
+					$allRecords[$i]->{'SessionTime'},
+					$allRecords[$i]->{'InputOctets'},
+					$allRecords[$i]->{'InputGigawords'},
+					$allRecords[$i]->{'OutputOctets'},
+					$allRecords[$i]->{'OutputGigawords'},
+					$allRecords[$i]->{'Username'},
+					$allRecords[$i]->{'PeriodKey'}
+				);
+
+				@dbDoParams = ('
+					UPDATE
+						@TP@accounting_summary
+					SET
+						AcctSessionTime = ?,
+						AcctInputOctets = ?,
+						AcctInputGigawords = ?,
+						AcctOutputOctets = ?,
+						AcctOutputGigawords = ?
+					WHERE
+						Username = ?
+						AND	PeriodKey = ?
+					',
+					@insertArray
+				);
+
+				if ($sth) {
+					# Do query
+					$sth = DBDo(@dbDoParams);
+				}
+			} else {
+				@insertArray = (
+					$allRecords[$i]->{'Username'},
+					$allRecords[$i]->{'PeriodKey'},
+					$allRecords[$i]->{'SessionTime'},
+					$allRecords[$i]->{'InputOctets'},
+					$allRecords[$i]->{'InputGigawords'},
+					$allRecords[$i]->{'OutputOctets'},
+					$allRecords[$i]->{'OutputGigawords'}
+				);
+
+				@dbDoParams = ('
+					INSERT INTO
+						@TP@accounting_summary
+					(
+						Username,
+						PeriodKey,
+						AcctSessionTime,
+						AcctInputOctets,
+						AcctInputGigawords,
+						AcctOutputOctets,
+						AcctOutputGigawords
+					)
+					VALUES
+						(?,?,?,?,?,?,?)
+					',
+					@insertArray
+				);
+
+				if ($sth) {
+					# Do query
+					$sth = DBDo(@dbDoParams);
+				}
 			}
 		}
 	}
@@ -721,7 +781,7 @@ sub cleanup
 	# Rollback with error if failed
 	if (!$sth) {
 		DBRollback();
-		$server->log(LOG_ERR,"[MOD_ACCOUNTING_SQL] Cleanup => Failed to insert accounting record: ".
+		$server->log(LOG_ERR,"[MOD_ACCOUNTING_SQL] Cleanup => Failed to insert or update accounting record: ".
 				awitpt::db::dblayer::Error());
 		return;
 	}
-- 
GitLab