Bug 19454: Add -category parameter to borrowers-force-messaging-defaults.pl
[koha.git] / misc / maintenance / borrowers-force-messaging-defaults.pl
1 #!/usr/bin/perl
2 #
3 # Copyright (C) 2011 Tamil s.a.r.l.
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22 BEGIN {
23     # find Koha's Perl modules
24     # test carefully before changing this
25     use FindBin;
26     eval { require "$FindBin::Bin/../kohalib.pl" };
27 }
28
29 use C4::Context;
30 use C4::Members::Messaging;
31 use Getopt::Long;
32 use Pod::Usage;
33
34
35 sub usage {
36     pod2usage( -verbose => 2 );
37     exit;
38 }
39
40
41 sub force_borrower_messaging_defaults {
42     my ($doit, $since, $not_expired, $no_overwrite, $category ) = @_;
43
44     $since = '0000-00-00' if (!$since);
45     print "Since: $since\n";
46
47     my $dbh = C4::Context->dbh;
48     $dbh->{AutoCommit} = 0;
49
50     my $sql =
51 q|SELECT DISTINCT bo.borrowernumber, bo.categorycode FROM borrowers bo
52 LEFT JOIN borrower_message_preferences mp USING (borrowernumber)
53 WHERE bo.dateenrolled >= ?|;
54     if ($not_expired) {
55         $sql .= " AND bo.dateexpiry >= NOW()"
56     }
57     if( $no_overwrite ) {
58         $sql .= " AND mp.borrowernumber IS NULL";
59     }
60     $sql .= " AND categorycode = ?" if $category;
61     my $sth = $dbh->prepare($sql);
62     $sth->execute($since, $category || () );
63     while ( my ($borrowernumber, $categorycode) = $sth->fetchrow ) {
64         print "$borrowernumber: $categorycode\n";
65         next unless $doit;
66         C4::Members::Messaging::SetMessagingPreferencesFromDefaults( {
67             borrowernumber => $borrowernumber,
68             categorycode   => $categorycode,
69         } );
70     }
71     $dbh->commit();
72 }
73
74
75 my ( $doit, $since, $help, $not_expired, $no_overwrite, $category );
76 my $result = GetOptions(
77     'doit'        => \$doit,
78     'since:s'     => \$since,
79     'not-expired' => \$not_expired,
80     'no-overwrite'  => \$no_overwrite,
81     'category:s'  => \$category,
82     'help|h'      => \$help,
83 );
84
85 usage() if $help;
86
87 force_borrower_messaging_defaults( $doit, $since, $not_expired, $no_overwrite, $category );
88
89 =head1 NAME
90
91 borrowers-force-messaging-defaults.pl
92
93 =head1 SYNOPSIS
94
95   borrowers-force-messaging-defaults.pl
96   borrowers-force-messaging-defaults.pl --help
97   borrowers-force-messaging-defaults.pl --doit
98   borrowers-force-messaging-defaults.pl --doit --not-expired
99   borrowers-force-messaging-defaults.pl --doit --category PT
100
101 =head1 DESCRIPTION
102
103 If the EnhancedMessagingPreferences syspref is enabled after borrowers have
104 been created in the DB, those borrowers won't have messaging transport
105 preferences default values as defined for their borrower category. So you would
106 have to modify each borrower one by one if you would like to send them 'Hold
107 Filled' notice for example.
108
109 This script creates/overwrites messaging preferences for all borrowers and sets
110 them to default values defined for the category they belong to (unless you
111 use the options -not-expired or -no-overwrite to update a subset).
112
113 =over 8
114
115 =item B<--help>
116
117 Prints this help
118
119 =item B<--doit>
120
121 Actually update the borrowers.
122
123 =item B<--not-expired>
124
125 Will only update active borrowers (borrowers who didn't pass their expiration date).
126
127 =item B<--no-overwrite>
128
129 Will only update patrons without messaging preferences and skip patrons that
130 already set their preferences.
131
132 =item B<--category>
133
134 Will only update patrons in the category specified.
135
136 =back
137
138 =cut