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