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