Bug 5610 Script to force borr transport defaults messaging
[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) = @_;
43
44     my $dbh = C4::Context->dbh;
45     $dbh->{AutoCommit} = 0;
46
47     if ( $doit && $truncate ) {
48         my $sth = $dbh->prepare("TRUNCATE borrower_message_preferences");
49         $sth->execute();
50     }
51
52     my $sth = $dbh->prepare("SELECT borrowernumber, categorycode FROM borrowers");
53     $sth->execute();
54     while ( my ($borrowernumber, $categorycode) = $sth->fetchrow ) {
55         print "$borrowernumber: $categorycode\n";
56         next unless $doit;
57         C4::Members::Messaging::SetMessagingPreferencesFromDefaults( {
58             borrowernumber => $borrowernumber,
59             categorycode   => $categorycode,
60         } );
61     }
62     $dbh->commit();
63 }
64
65
66 my ($doit, $truncate, $help);
67 my $result = GetOptions(
68     'doit'     => \$doit,
69     'truncate' => \$truncate,
70     'help|h'   => \$help,
71 );
72
73 usage() if $help;
74
75 force_borrower_messaging_defaults( $doit, $truncate);
76
77 =head1 NAME
78
79 force-borrower-messaging-defaults
80
81 =head1 SYNOPSIS
82
83   force-borrower-messaging-defaults 
84   force-borrower-messaging-defaults --help
85   force-borrower-messaging-defaults --doit
86   force-borrower-messaging-defaults --doit --truncate
87
88 =head1 DESCRIPTION
89
90 If the EnhancedMessagingPreferences syspref is enabled after borrowers have
91 been created in the DB, those borrowers won't have messaging transport
92 preferences default values as defined for their borrower category. So you would
93 have to modify each borrower one by one if you would like to send them 'Hold
94 Filled' notice for example.
95
96 This script create transport preferences for all existing borrowers and set
97 them to default values defined for the category they belong to.
98
99 =over 8
100
101 =item B<--help>
102
103 Prints this help
104
105 =item B<--doit>
106
107 Process actually the borrowers.
108
109 =item B<--truncate>
110
111 Truncate all borrowers transport preferences before (re-)creating them. It
112 affects borrower_message_preferences table.
113
114 =back
115
116 =cut
117