Translation updates for Koha 17.11.10
[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 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, $since, $not_expired) = @_;
43
44     $since = '0000-00-00' if (!$since);
45     print "Since: $since\n";
46
47     my $dbh = C4::Context->dbh;
48     $dbh->{AutoCommit} = 0;
49
50     my $sql = "SELECT borrowernumber, categorycode FROM borrowers WHERE dateenrolled >= ?";
51     if ($not_expired) {
52         $sql .= " AND dateexpiry >= NOW()"
53     }
54     my $sth = $dbh->prepare($sql);
55     $sth->execute($since);
56     while ( my ($borrowernumber, $categorycode) = $sth->fetchrow ) {
57         print "$borrowernumber: $categorycode\n";
58         next unless $doit;
59         C4::Members::Messaging::SetMessagingPreferencesFromDefaults( {
60             borrowernumber => $borrowernumber,
61             categorycode   => $categorycode,
62         } );
63     }
64     $dbh->commit();
65 }
66
67
68 my ($doit, $since, $help, $not_expired);
69 my $result = GetOptions(
70     'doit'        => \$doit,
71     'since:s'     => \$since,
72     'not-expired' => \$not_expired,
73     'help|h'      => \$help,
74 );
75
76 usage() if $help;
77
78 force_borrower_messaging_defaults( $doit, $since, $not_expired );
79
80 =head1 NAME
81
82 borrowers-force-messaging-defaults.pl
83
84 =head1 SYNOPSIS
85
86   borrowers-force-messaging-defaults.pl
87   borrowers-force-messaging-defaults.pl --help
88   borrowers-force-messaging-defaults.pl --doit
89   borrowers-force-messaging-defaults.pl --doit --not-expired
90
91 =head1 DESCRIPTION
92
93 If the EnhancedMessagingPreferences syspref is enabled after borrowers have
94 been created in the DB, those borrowers won't have messaging transport
95 preferences default values as defined for their borrower category. So you would
96 have to modify each borrower one by one if you would like to send them 'Hold
97 Filled' notice for example.
98
99 This script create transport preferences for all existing borrowers and set
100 them to default values defined for the category they belong to.
101
102 =over 8
103
104 =item B<--help>
105
106 Prints this help
107
108 =item B<--doit>
109
110 Actually update the borrowers.
111
112 =item B<--not-expired>
113
114 Will only update active borrowers (borrowers who didn't pass their expiration date).
115
116 =back
117
118 =cut