0e30596879c0bc9a8f5d3fd58f94aebd5506d4de
[srvgit] / 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 Koha::Script;
30 use C4::Context;
31 use C4::Members::Messaging;
32 use Getopt::Long;
33 use Pod::Usage;
34
35
36 sub usage {
37     pod2usage( -verbose => 2 );
38     exit;
39 }
40
41
42 sub force_borrower_messaging_defaults {
43     my ($doit, $since, $not_expired, $no_overwrite, $category ) = @_;
44
45     print "Since: $since\n" if $since;
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 1|;
54
55     if ( $since ) {
56         $sql .= " AND bo.dateenrolled >= ?";
57     }
58     if ($not_expired) {
59         $sql .= " AND bo.dateexpiry >= NOW()"
60     }
61     if( $no_overwrite ) {
62         $sql .= " AND mp.borrowernumber IS NULL";
63     }
64     $sql .= " AND bo.categorycode = ?" if $category;
65     my $sth = $dbh->prepare($sql);
66     $sth->execute($since || (), $category || () );
67     my $cnt = 0;
68     while ( my ($borrowernumber, $categorycode) = $sth->fetchrow ) {
69         print "$borrowernumber: $categorycode\n";
70         next unless $doit;
71         C4::Members::Messaging::SetMessagingPreferencesFromDefaults( {
72             borrowernumber => $borrowernumber,
73             categorycode   => $categorycode,
74         } );
75         $cnt++;
76     }
77     $dbh->commit();
78     print "Total borrowers updated: $cnt\n" if $doit;
79 }
80
81
82 my ( $doit, $since, $help, $not_expired, $no_overwrite, $category );
83 my $result = GetOptions(
84     'doit'        => \$doit,
85     'since:s'     => \$since,
86     'not-expired' => \$not_expired,
87     'no-overwrite'  => \$no_overwrite,
88     'category:s'  => \$category,
89     'help|h'      => \$help,
90 );
91
92 usage() if $help;
93
94 force_borrower_messaging_defaults( $doit, $since, $not_expired, $no_overwrite, $category );
95
96 =head1 NAME
97
98 borrowers-force-messaging-defaults.pl
99
100 =head1 SYNOPSIS
101
102   borrowers-force-messaging-defaults.pl
103   borrowers-force-messaging-defaults.pl --help
104   borrowers-force-messaging-defaults.pl --doit
105   borrowers-force-messaging-defaults.pl --doit --not-expired
106   borrowers-force-messaging-defaults.pl --doit --category PT
107
108 =head1 DESCRIPTION
109
110 If the EnhancedMessagingPreferences syspref is enabled after borrowers have
111 been created in the DB, those borrowers won't have messaging transport
112 preferences default values as defined for their borrower category. So you would
113 have to modify each borrower one by one if you would like to send them 'Hold
114 Filled' notice for example.
115
116 This script creates/overwrites messaging preferences for all borrowers and sets
117 them to default values defined for the category they belong to (unless you
118 use the options -not-expired or -no-overwrite to update a subset).
119
120 =over 8
121
122 =item B<--help>
123
124 Prints this help
125
126 =item B<--doit>
127
128 Actually update the borrowers.
129
130 =item B<--not-expired>
131
132 Will only update active borrowers (borrowers who didn't pass their expiration date).
133
134 =item B<--no-overwrite>
135
136 Will only update patrons without messaging preferences and skip patrons that
137 already set their preferences.
138
139 =item B<--category>
140
141 Will only update patrons in the category specified.
142
143 =back
144
145 =cut