From 7551f3fe14d3f6483912cfdc4fe7afa90c716ac3 Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Wed, 8 May 2019 12:53:15 +0100
Subject: [PATCH] Bug 22610: (follow-up) Catch non-standard SIP codes

---
 .../data/mysql/atomicupdate/bug_22610.perl    | 29 +++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/installer/data/mysql/atomicupdate/bug_22610.perl b/installer/data/mysql/atomicupdate/bug_22610.perl
index ea122a18c5..a788d2f462 100644
--- a/installer/data/mysql/atomicupdate/bug_22610.perl
+++ b/installer/data/mysql/atomicupdate/bug_22610.perl
@@ -40,6 +40,35 @@ if ( CheckVersion($DBversion) ) {
           accounttype = 'Pay02';
     });
 
+    my $sth = $dbh->prepare( qq{SELECT * FROM accountlines WHERE accounttype LIKE "Pay%" AND accounttype != "Pay" } );
+    $sth->execute();
+    my $seen = {};
+    while (my $row = $sth->fetchrow_hashref) {
+        my $type = $row->{accounttype};
+        my $sipcode = $type;
+        $sipcode =~ s/Pay/SIP/g;
+        unless ($seen->{$sipcode}) {
+            $dbh->do(qq{
+                INSERT INTO
+                  authorised_values (category,authorised_value,lib)
+                VALUES
+                  ('PAYMENT_TYPE',"$sipcode",'Unrecognised SIP2 payment type')
+            });
+    
+             $dbh->do(qq{
+                UPDATE
+                  accountlines
+                SET
+                  accounttype  = 'Pay',
+                  payment_type = "$sipcode"
+                WHERE
+                  accounttype = "$type";
+            });
+    
+            $seen->{$sipcode} = 1;
+        }
+    }
+
     SetVersion($DBversion);
     print "Upgrade to $DBversion done (Bug 22610 - Fix accounttypes for SIP2 payments)\n";
 }
-- 
2.20.1