Bug 19451: (QA follow-up) Replace weird subquery
[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, $truncate, $since, $not_expired, $no_overwrite) = @_;
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     if ( $doit && $truncate ) {
51         $dbh->do(q|SET FOREIGN_KEY_CHECKS = 0|);
52         $dbh->do(q|TRUNCATE borrower_message_transport_preferences|);
53         $dbh->do(q|TRUNCATE borrower_message_preferences|);
54         $dbh->do(q|SET FOREIGN_KEY_CHECKS = 1|);
55     }
56
57     my $sql =
58 q|SELECT DISTINCT bo.borrowernumber, bo.categorycode FROM borrowers bo
59 LEFT JOIN borrower_message_preferences mp USING (borrowernumber)
60 WHERE bo.dateenrolled >= ?|;
61     if ($not_expired) {
62         $sql .= " AND bo.dateexpiry >= NOW()"
63     }
64     if( $no_overwrite ) {
65         $sql .= " AND mp.borrowernumber IS NULL";
66     }
67     my $sth = $dbh->prepare($sql);
68     $sth->execute($since);
69     while ( my ($borrowernumber, $categorycode) = $sth->fetchrow ) {
70         print "$borrowernumber: $categorycode\n";
71         next unless $doit;
72         C4::Members::Messaging::SetMessagingPreferencesFromDefaults( {
73             borrowernumber => $borrowernumber,
74             categorycode   => $categorycode,
75         } );
76     }
77     $dbh->commit();
78 }
79
80
81 my ( $doit, $truncate, $since, $help, $not_expired, $no_overwrite );
82 my $result = GetOptions(
83     'doit'        => \$doit,
84     'truncate'    => \$truncate,
85     'since:s'     => \$since,
86     'not-expired' => \$not_expired,
87     'no-overwrite'  => \$no_overwrite,
88     'help|h'      => \$help,
89 );
90
91 usage() if $help;
92
93 force_borrower_messaging_defaults( $doit, $truncate, $since, $not_expired, $no_overwrite );
94
95 =head1 NAME
96
97 borrowers-force-messaging-defaults.pl
98
99 =head1 SYNOPSIS
100
101   borrowers-force-messaging-defaults.pl
102   borrowers-force-messaging-defaults.pl --help
103   borrowers-force-messaging-defaults.pl --doit
104   borrowers-force-messaging-defaults.pl --doit --truncate
105   borrowers-force-messaging-defaults.pl --doit --not-expired
106
107 =head1 DESCRIPTION
108
109 If the EnhancedMessagingPreferences syspref is enabled after borrowers have
110 been created in the DB, those borrowers won't have messaging transport
111 preferences default values as defined for their borrower category. So you would
112 have to modify each borrower one by one if you would like to send them 'Hold
113 Filled' notice for example.
114
115 This script creates/overwrites messaging preferences for all borrowers and sets
116 them to default values defined for the category they belong to (unless you
117 use the options -not-expired or -no-overwrite to update a subset).
118
119 =over 8
120
121 =item B<--help>
122
123 Prints this help
124
125 =item B<--doit>
126
127 Actually update the borrowers.
128
129 =item B<--truncate>
130
131 Truncate all borrowers transport preferences before (re-)creating them. It
132 affects borrower_message_preferences table.
133
134 =item B<--not-expired>
135
136 Will only update active borrowers (borrowers who didn't pass their expiration date).
137
138 =item B<--no-overwrite>
139
140 Will only update patrons without messaging preferences and skip patrons that
141 already set their preferences.
142
143 =back
144
145 =cut