From b10274584b2741e3e2d66d54a5cd0f80af3a7eab Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Tue, 25 May 2021 18:46:37 +0000
Subject: [PATCH] Bug 28456: Add WHERE option to membership_expiry cronjob

Some libraries have a large amount of accounts and want to be able to limit how many email reminders they send.
Adding a where statement will allow for flexibility

To test:
1 - Set some borrowers to expire soon
2 - Set MembershipExpiryDaysNotice to 10
3 - perl misc/cronjobs/membership_expiry.pl -c -v -n --before 100 --after 100
4 - Note the patrons that yo uadjusted show up
5 - Limit by various patron fields, e.g.
    perl misc/cronjobs/membership_expiry.pl -c -v -n --before 100 --after 100 --where="lastseen IS NULL"
    perl misc/cronjobs/membership_expiry.pl -c -v -n --before 100 --after 100 --where="lastseen IS NOT NULL"
    perl misc/cronjobs/membership_expiry.pl -c -v -n --before 100 --after 100 --where="surname LIKE '%a%'"
    perl misc/cronjobs/membership_expiry.pl -c -v -n --before 100 --after 100 --where="surname NOT LIKE '%a%'"
6 - Confirm expected results
7 - perl misc/cronjobs/membership_expiry.pl
8 - Confirm the help message makes sense

Signed-off-by: Azucena Aguayo <azucena.aguayo@gmail.com>
---
 misc/cronjobs/membership_expiry.pl | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/misc/cronjobs/membership_expiry.pl b/misc/cronjobs/membership_expiry.pl
index 5038ce1f9f..c0451c9004 100755
--- a/misc/cronjobs/membership_expiry.pl
+++ b/misc/cronjobs/membership_expiry.pl
@@ -29,6 +29,18 @@ or, in crontab:
 
 0 1 * * * membership_expiry.pl -c
 
+Options:
+   --help                   brief help message
+   --man                    full documentation
+   --where <conditions>     where clause to add to the query
+   -v -verbose              verbose mode
+   -n --nomail              if supplied messages will be output to STDOUT and not sent
+   -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
+   --before=X               inclue patrons expiring a number of days BEFORE the date set by the preference
+   --after=X                inclue patrons expiring a number of days AFTER  the date set by the preference
+   -l --letter <lettercode> use a specific notice rather than the default
+
 =head1 DESCRIPTION
 
 This script sends membership expiry reminder notices to patrons.
@@ -75,15 +87,24 @@ the date set by the preference.
 Optional parameter to extend the selection with a number of days AFTER
 the date set by the preference.
 
+=item B<-where>
+
+Use this option to specify a condition built with columns from the borrowers table
+
+e.g.
+--where 'lastseen IS NOT NULL'
+will only notify patrons who have been seen.
+
 =item B<-letter>
 
-Optional parameter to use another notice than the default one.
+Optional parameter to use another notice than the default: MEMBERSHIP_EXPIRY
 
 =back
 
 =head1 CONFIGURATION
 
-The content of the messages is configured in Tools -> Notices and slips. Use the MEMBERSHIP_EXPIRY notice.
+The content of the messages is configured in Tools -> Notices and slips. Use the MEMBERSHIP_EXPIRY notice or
+supply another via the parameters.
 
 Typically, messages are prepared for each patron when the memberships are going to expire.
 
@@ -142,6 +163,7 @@ my $man     = 0;
 my $before  = 0;
 my $after   = 0;
 my ( $branch, $letter_type );
+my @where;
 
 GetOptions(
     'help|?'         => \$help,
@@ -153,6 +175,7 @@ GetOptions(
     'before:i'       => \$before,
     'after:i'        => \$after,
     'letter:s'       => \$letter_type,
+    'where=s'        => \@where,
 ) or pod2usage(2);
 
 pod2usage( -verbose => 2 ) if $man;
@@ -177,6 +200,10 @@ my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires(
         after  => $after,
     }
 );
+
+my $where_literal = join ' AND ', @where;
+$upcoming_mem_expires = $upcoming_mem_expires->search( \$where_literal ) if @where;
+
 warn 'found ' . $upcoming_mem_expires->count . ' soon expiring members'
     if $verbose;
 
-- 
2.11.0