From d7d0021f1300f4515ddc1075de92f52c297d8ae1 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Fri, 29 May 2020 20:02:55 +0000
Subject: [PATCH] Bug 25624: Add not equal, like, not like, and support for
 nulls to update_patron_category

To test:
1 - Run the script with no parameters and verify the help explains the parameters
2 - Try running with various fields, matching and not matching
3 - Test null values
4 - Test like values with wildcards
5 - Test like with the word null to find fields containing the word rather than being unset

Signed-off-by: Rhonda Kuiper <rkuiper@roundrocktexas.gov>
---
 misc/cronjobs/update_patrons_category.pl | 37 +++++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)

diff --git a/misc/cronjobs/update_patrons_category.pl b/misc/cronjobs/update_patrons_category.pl
index 3de4822b3d..f38fd92a93 100644
--- a/misc/cronjobs/update_patrons_category.pl
+++ b/misc/cronjobs/update_patrons_category.pl
@@ -56,6 +56,9 @@ Options:
    -rb=date --regbefore     update if registration date is before given date
    -ra=date --regafter      update if registration date is after a given date
    -d --dbfield name=value  where <name> is a column in the borrowers table, patrons will be updated if the field is equal to given <value>
+   -dn --notfield name=value  where <name> is a column in the borrowers table, patrons will be updated if the field is equal to given <value>
+   -dl --likefield name=value  where <name> is a column in the borrowers table, patrons will be updated if the field is equal to given <value>
+   -dnl --notlikefield name=value  where <name> is a column in the borrowers table, patrons will be updated if the field is equal to given <value>
    -v -verbose              verbose mode
    -c --confirm             commit changes to db, no action will be taken unless this switch is included
    -b --branch <branchname> only deal with patrons from this library/branch
@@ -121,12 +124,20 @@ Enter a date in ISO format YYYY-MM-DD and only patrons registered after this dat
 
 =item B<--field column=value | -d column=value>
 
-Use this flag to specify a column in the borrowers table and update only patrons whose value in that column matches the value supplied (repeatable)
+Use this flag to specify a column in the borrowers table and update only patrons whose value in that column equals the value supplied (repeatable)
 
 e.g.
 --field dateexpiry=2016-01-01
 will update all patrons who expired on that date, useful for schools etc.
 
+=item B<--notfield column=value | -dn column=value>
+
+Use this flag to specify a column in the borrowers table and update only patrons whose value in that column does not equal the value supplied (repeatable)
+
+e.g.
+--notfield email=NULL
+will update all patrons with no value for email
+
 =back
 
 =head1 DESCRIPTION
@@ -163,6 +174,9 @@ my $reg_bef;
 my $reg_aft;
 my $branch_lim;
 my %fields;
+my %not_fields;
+my %like_fields;
+my %not_like_fields;
 
 GetOptions(
     'help|?'          => \$help,
@@ -178,7 +192,10 @@ GetOptions(
     'rb|regbefore=s'  => \$reg_bef,
     'ra|regafter=s'   => \$reg_aft,
     'b|branch=s'      => \$branch_lim,
-    'd|field=s'       => \%fields
+    'd|field=s'       => \%fields,
+    'dn|notfield=s'   => \%not_fields,
+    'dl|likefield=s'       => \%like_fields,
+    'dnl|notlikefield=s'   => \%not_like_fields
 );
 
 pod2usage(1) if $help;
@@ -235,9 +252,23 @@ if ($verbose) {
 }
 
 while ( my ( $key, $value ) = each %fields ) {
-    $verbose and print "    Borrower column $key is equal to $value\n";
+    $verbose and print "    Borrower column $key is $value\n";
+    $value = undef if lc($value) eq 'null';
     $params{ "me." . $key } = $value;
 }
+while ( my ( $key, $value ) = each %not_fields ) {
+    $verbose and print "    Borrower column $key is not $value\n";
+    $value = undef if lc($value) eq 'null';
+    $params{ "me." . $key } = { '!=' => $value };
+}
+while ( my ( $key, $value ) = each %like_fields ) {
+    $verbose and print "    Borrower column $key is like $value\n";
+    $params{ "me." . $key } = { 'LIKE' => $value };
+}
+while ( my ( $key, $value ) = each %not_like_fields ) {
+    $verbose and print "    Borrower column $key is not like $value\n";
+    $params{ "me." . $key } = { 'NOT LIKE' => $value };
+}
 my $target_patrons = Koha::Patrons->search(\%params)->search_patrons_to_update_category(
     {
         from          => $fromcat,
-- 
2.11.0