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