Bug 11603: Gather print notices - add a csv parameter
[koha.git] / misc / cronjobs / gather_print_notices.pl
1 #!/usr/bin/perl -w
2
3 use Modern::Perl;
4
5 BEGIN {
6     # find Koha's Perl modules
7     # test carefully before changing this
8     use FindBin;
9     eval { require "$FindBin::Bin/../kohalib.pl" };
10 }
11
12 use
13   CGI; # NOT a CGI script, this is just to keep C4::Templates::gettemplate happy
14 use C4::Context;
15 use C4::Dates;
16 use C4::Debug;
17 use C4::Letters;
18 use C4::Templates;
19 use File::Spec;
20 use Pod::Usage;
21 use Getopt::Long;
22 use C4::Log;
23
24 use Koha::DateUtils;
25
26 my ( $stylesheet, $help, $split, $html, $csv, @letter_codes );
27
28 GetOptions(
29     'h|help'  => \$help,
30     's|split' => \$split,
31     'html'    => \$html,
32     'csv'     => \$csv,
33     'letter_code:s' => \@letter_codes,
34 ) || pod2usage(1);
35
36 pod2usage(0) if $help;
37
38 my $output_directory = $ARGV[0];
39
40 if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
41     pod2usage({
42         -exitval => 1,
43         -msg => qq{\nError: You must specify a valid and writeable directory to dump the print notices in.\n},
44     });
45 }
46
47 # Default value is html
48 $html = 1 unless $html or $csv;
49
50 if ( $csv and @letter_codes != 1 ) {
51     pod2usage({
52         -exitval => 1,
53         -msg => qq{\nIt is not consistent to use --csv without one (and only one) letter_code\n},
54     });
55 }
56
57 cronlogaction();
58
59 my $today        = C4::Dates->new();
60 my @all_messages = @{ GetPrintMessages() };
61
62 # Filter by letter_code
63 @all_messages = map {
64     my $letter_code = $_->{letter_code};
65     (
66         grep { /^$letter_code$/ } @letter_codes
67     ) ? $_ : ()
68 } @all_messages;
69 exit unless @all_messages;
70
71 ## carriage return replaced by <br/> as output is html
72 foreach my $message (@all_messages) {
73     local $_ = $message->{'content'};
74     s/\n/<br \/>/g;
75     s/\r//g;
76     $message->{'content'} = $_;
77 }
78
79 print_notices_html({ messages => \@all_messages, split => $split })
80     if $html;
81
82 print_notices_csv({ messages => \@all_messages, split => $split })
83     if $csv;
84
85 sub print_notices_html {
86     my ( $params ) = @_;
87
88     my $messages = $params->{messages};
89     my $split = $params->{split};
90
91     my $messages_by_branch;
92     if ( $split ) {
93         foreach my $message (@$messages) {
94             push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
95         }
96     } else {
97         $messages_by_branch->{all_branches} = $messages;
98     }
99
100     while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
101         my $filename = $split
102             ? 'holdnotices-' . $today->output('iso') . "-$branchcode.html"
103             : 'holdnotices-' . $today->output('iso') . ".html";
104
105         my $template =
106           C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
107             new CGI );
108
109         $template->param(
110             stylesheet => C4::Context->preference("NoticeCSS"),
111             today      => $today->output(),
112             messages   => $branch_messages,
113         );
114
115         my $output_file = File::Spec->catdir( $output_directory, $filename )
116         open my $OUTPUT, '>', $output_file
117             or die "Could not open $output_file: $!";
118         print $OUTPUT $template->output;
119         close $OUTPUT;
120
121         foreach my $message ( @$branch_messages ) {
122             C4::Letters::_set_message_status(
123                 {
124                     message_id => $message->{'message_id'},
125                     status => 'sent'
126                 }
127             );
128             $message->{status} = 'sent';
129         }
130     }
131 }
132
133 sub print_notices_csv {
134     my ( $params ) = @_;
135
136     my $messages = $params->{messages};
137     my $split = $params->{split};
138
139     my $messages_by_branch;
140     if ( $split ) {
141         foreach my $message (@$messages) {
142             push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
143         }
144     } else {
145         $messages_by_branch->{all_branches} = $messages;
146     }
147
148     while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
149         my $filename = $split
150             ? 'holdnotices-' . $today->output('iso') . "-$branchcode.csv"
151             : 'holdnotices-' . $today->output('iso') . ".csv";
152
153         open my $OUTPUT, '>', File::Spec->catdir( $output_directory, $filename );
154         my ( @csv_lines, $headers );
155         foreach my $message ( @$branch_messages ) {
156             my @lines = split /\n/, $message->{content};
157
158             # We don't have headers, get them
159             unless ( $headers ) {
160                 $headers = $lines[0];
161                 chomp $headers;
162                 say $OUTPUT $headers;
163             }
164
165             shift @lines;
166             for my $line ( @lines ) {
167                 chomp $line;
168                 next if $line =~ /^\s$/;
169                 say $OUTPUT $line;
170             }
171
172             C4::Letters::_set_message_status(
173                 {
174                     message_id => $message->{'message_id'},
175                     status => 'sent'
176                 }
177             ) if $message->{status} ne 'sent';
178         }
179         close $OUTPUT;
180     }
181 }
182
183 =head1 NAME
184
185 gather_print_notices - Print waiting print notices
186
187 =head1 SYNOPSIS
188
189 gather_print_notices output_directory [-s|--split] [--html] [--csv] [--letter_code=LETTER_CODE] [-h|--help]
190
191 Will print all waiting print notices to the output_directory.
192
193 The generated filename will be holdnotices-TODAY.[csv|html] or holdnotices-TODAY-BRANCHCODE.[csv|html] if the --split parameter is given.
194
195 =head1 OPTIONS
196
197 =over
198
199 =item B<output_directory>
200
201 Define the output directory where the files will be generated.
202
203 =item B<-s|--split>
204
205 Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html]
206
207 =item B<--html>
208
209 Generate the print notices in a html file (default if --html and --csv are not given).
210
211 =item B<--csv>
212
213 Generate the print notices in a csv file.
214 If you use this parameter, the template should contain 2 lines.
215 The first one the the csv headers and the second one the value list.
216
217 For example:
218 cardnumber:patron:email:item
219 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
220
221 You have to combine this option without one (and only one) letter_code.
222
223 =item B<--letter_code>
224
225 Filter print messages by letter_code.
226 Several letter_code parameters can be given.
227
228 =item B<-h|--help>
229
230 Print a brief help message
231
232 =back
233
234 =head1 AUTHOR
235
236 Jesse Weaver <pianohacker@gmail.com>
237
238 Jonathan Druart <jonathan.druart@biblibre.com>
239
240 =head1 COPYRIGHT
241
242 Copyright 2009 Jesse Weaver
243
244 Copyright 2014 BibLibre
245
246 =head1 LICENSE
247 This file is part of Koha.
248
249 Koha is free software; you can redistribute it and/or modify it
250 under the terms of the GNU General Public License as published by
251 the Free Software Foundation; either version 3 of the License, or
252 (at your option) any later version.
253
254 Koha is distributed in the hope that it will be useful, but
255 WITHOUT ANY WARRANTY; without even the implied warranty of
256 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
257 GNU General Public License for more details.
258
259 You should have received a copy of the GNU General Public License
260 along with Koha; if not, see <http://www.gnu.org/licenses>.
261
262 =cut