ff35e086c93a03bec8b421847967d77026437bdf
[koha.git] / misc / maintenance / borrowers-force-messaging-defaults
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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) = @_;
43
44     $since = '0000-00-00' if (!$since);
45     print $since;
46
47     my $dbh = C4::Context->dbh;
48     $dbh->{AutoCommit} = 0;
49
50     if ( $doit && $truncate ) {
51         my $sth = $dbh->prepare("TRUNCATE borrower_message_preferences");
52         $sth->execute();
53     }
54
55     my $sth = $dbh->prepare("SELECT borrowernumber, categorycode FROM borrowers WHERE dateenrolled >= ?");
56     $sth->execute($since);
57     while ( my ($borrowernumber, $categorycode) = $sth->fetchrow ) {
58         print "$borrowernumber: $categorycode\n";
59         next unless $doit;
60         C4::Members::Messaging::SetMessagingPreferencesFromDefaults( {
61             borrowernumber => $borrowernumber,
62             categorycode   => $categorycode,
63         } );
64     }
65     $dbh->commit();
66 }
67
68
69 my ($doit, $truncate, $since, $help);
70 my $result = GetOptions(
71     'doit'     => \$doit,
72     'truncate' => \$truncate,
73     'since:s'  => \$since,
74     'help|h'   => \$help,
75 );
76
77 usage() if $help;
78
79 force_borrower_messaging_defaults( $doit, $truncate, $since );
80
81 =head1 NAME
82
83 force-borrower-messaging-defaults
84
85 =head1 SYNOPSIS
86
87   force-borrower-messaging-defaults 
88   force-borrower-messaging-defaults --help
89   force-borrower-messaging-defaults --doit
90   force-borrower-messaging-defaults --doit --truncate
91
92 =head1 DESCRIPTION
93
94 If the EnhancedMessagingPreferences syspref is enabled after borrowers have
95 been created in the DB, those borrowers won't have messaging transport
96 preferences default values as defined for their borrower category. So you would
97 have to modify each borrower one by one if you would like to send them 'Hold
98 Filled' notice for example.
99
100 This script create transport preferences for all existing borrowers and set
101 them to default values defined for the category they belong to.
102
103 =over 8
104
105 =item B<--help>
106
107 Prints this help
108
109 =item B<--doit>
110
111 Process actually the borrowers.
112
113 =item B<--truncate>
114
115 Truncate all borrowers transport preferences before (re-)creating them. It
116 affects borrower_message_preferences table.
117
118 =back
119
120 =cut
121