From 49db15c09003e2c1e5539b1609a6dc5967afd9dc Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Thu, 12 Oct 2017 15:36:10 +0200
Subject: [PATCH] Bug 19454: Add -category parameter to
 borrowers-force-messaging-defaults.pl
Content-Type: text/plain; charset=utf-8

Instead of updating patrons over all patron categories, it would be
helpful if we could filter on a specified category.

Test plan:
[1] Select two patrons A and B in say categories C1 and C2.
[2] Change the msg prefs for A and B away from defaults.
[3] Run borrowers-force-messaging-defaults.pl -doit -cat C1
    Verify that patron A changed and patron B did not.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
---
 misc/maintenance/borrowers-force-messaging-defaults.pl | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/misc/maintenance/borrowers-force-messaging-defaults.pl b/misc/maintenance/borrowers-force-messaging-defaults.pl
index 8951990..e9745f9 100755
--- a/misc/maintenance/borrowers-force-messaging-defaults.pl
+++ b/misc/maintenance/borrowers-force-messaging-defaults.pl
@@ -39,7 +39,7 @@ sub usage {
 
 
 sub force_borrower_messaging_defaults {
-    my ($doit, $since, $not_expired, $no_overwrite) = @_;
+    my ($doit, $since, $not_expired, $no_overwrite, $category ) = @_;
 
     $since = '0000-00-00' if (!$since);
     print "Since: $since\n";
@@ -54,8 +54,9 @@ sub force_borrower_messaging_defaults {
     if( $no_overwrite ) {
         $sql .= " AND (SELECT COUNT(*) FROM borrower_message_preferences mp WHERE mp.borrowernumber=bo.borrowernumber) = 0"
     }
+    $sql .= " AND categorycode = ?" if $category;
     my $sth = $dbh->prepare($sql);
-    $sth->execute($since);
+    $sth->execute($since, $category || () );
     while ( my ($borrowernumber, $categorycode) = $sth->fetchrow ) {
         print "$borrowernumber: $categorycode\n";
         next unless $doit;
@@ -68,18 +69,19 @@ sub force_borrower_messaging_defaults {
 }
 
 
-my ( $doit, $since, $help, $not_expired, $no_overwrite );
+my ( $doit, $since, $help, $not_expired, $no_overwrite, $category );
 my $result = GetOptions(
     'doit'        => \$doit,
     'since:s'     => \$since,
     'not-expired' => \$not_expired,
     'no_overwrite'  => \$no_overwrite,
+    'category:s'  => \$category,
     'help|h'      => \$help,
 );
 
 usage() if $help;
 
-force_borrower_messaging_defaults( $doit, $since, $not_expired, $no_overwrite );
+force_borrower_messaging_defaults( $doit, $since, $not_expired, $no_overwrite, $category );
 
 =head1 NAME
 
@@ -91,6 +93,7 @@ borrowers-force-messaging-defaults.pl
   borrowers-force-messaging-defaults.pl --help
   borrowers-force-messaging-defaults.pl --doit
   borrowers-force-messaging-defaults.pl --doit --not-expired
+  borrowers-force-messaging-defaults.pl --doit -category PT
 
 =head1 DESCRIPTION
 
@@ -102,7 +105,7 @@ Filled' notice for example.
 
 This script creates/overwrites messaging preferences for all borrowers and sets
 them to default values defined for the category they belong to (unless you
-use the options -not-expired or -no_overwrite to update a subset).
+use the options -not-expired, -no_overwrite or -category to update a subset).
 
 =over 8
 
@@ -123,6 +126,10 @@ Will only update active borrowers (borrowers who didn't pass their expiration da
 Will only update patrons without messaging preferences and skip patrons that
 already set their preferences.
 
+=item B<-category>
+
+Will only update patrons in the category specified.
+
 =back
 
 =cut
-- 
2.1.4