Bug 7607: (follow-up) Address OPAC and limits
[koha.git] / misc / maintenance / borrowers-force-messaging-defaults.pl
blobc446ca0f1c7c13eb57bc1889e3b58e01a65977ed
1 #!/usr/bin/perl
3 # Copyright (C) 2011 Tamil s.a.r.l.
5 # This file is part of Koha.
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.
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.
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>.
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" };
29 use Koha::Script;
30 use C4::Context;
31 use C4::Members::Messaging;
32 use Getopt::Long;
33 use Pod::Usage;
36 sub usage {
37 pod2usage( -verbose => 2 );
38 exit;
42 sub force_borrower_messaging_defaults {
43 my ($doit, $since, $not_expired, $no_overwrite, $category ) = @_;
45 $since = '0000-00-00' if (!$since);
46 print "Since: $since\n";
48 my $dbh = C4::Context->dbh;
49 $dbh->{AutoCommit} = 0;
51 my $sql =
52 q|SELECT DISTINCT bo.borrowernumber, bo.categorycode FROM borrowers bo
53 LEFT JOIN borrower_message_preferences mp USING (borrowernumber)
54 WHERE bo.dateenrolled >= ?|;
55 if ($not_expired) {
56 $sql .= " AND bo.dateexpiry >= NOW()"
58 if( $no_overwrite ) {
59 $sql .= " AND mp.borrowernumber IS NULL";
61 $sql .= " AND bo.categorycode = ?" if $category;
62 my $sth = $dbh->prepare($sql);
63 $sth->execute($since, $category || () );
64 my $cnt = 0;
65 while ( my ($borrowernumber, $categorycode) = $sth->fetchrow ) {
66 print "$borrowernumber: $categorycode\n";
67 next unless $doit;
68 C4::Members::Messaging::SetMessagingPreferencesFromDefaults( {
69 borrowernumber => $borrowernumber,
70 categorycode => $categorycode,
71 } );
72 $cnt++;
74 $dbh->commit();
75 print "Total borrowers updated: $cnt\n" if $doit;
79 my ( $doit, $since, $help, $not_expired, $no_overwrite, $category );
80 my $result = GetOptions(
81 'doit' => \$doit,
82 'since:s' => \$since,
83 'not-expired' => \$not_expired,
84 'no-overwrite' => \$no_overwrite,
85 'category:s' => \$category,
86 'help|h' => \$help,
89 usage() if $help;
91 force_borrower_messaging_defaults( $doit, $since, $not_expired, $no_overwrite, $category );
93 =head1 NAME
95 borrowers-force-messaging-defaults.pl
97 =head1 SYNOPSIS
99 borrowers-force-messaging-defaults.pl
100 borrowers-force-messaging-defaults.pl --help
101 borrowers-force-messaging-defaults.pl --doit
102 borrowers-force-messaging-defaults.pl --doit --not-expired
103 borrowers-force-messaging-defaults.pl --doit --category PT
105 =head1 DESCRIPTION
107 If the EnhancedMessagingPreferences syspref is enabled after borrowers have
108 been created in the DB, those borrowers won't have messaging transport
109 preferences default values as defined for their borrower category. So you would
110 have to modify each borrower one by one if you would like to send them 'Hold
111 Filled' notice for example.
113 This script creates/overwrites messaging preferences for all borrowers and sets
114 them to default values defined for the category they belong to (unless you
115 use the options -not-expired or -no-overwrite to update a subset).
117 =over 8
119 =item B<--help>
121 Prints this help
123 =item B<--doit>
125 Actually update the borrowers.
127 =item B<--not-expired>
129 Will only update active borrowers (borrowers who didn't pass their expiration date).
131 =item B<--no-overwrite>
133 Will only update patrons without messaging preferences and skip patrons that
134 already set their preferences.
136 =item B<--category>
138 Will only update patrons in the category specified.
140 =back
142 =cut