6b64a76f90fa95082ecb3b6c1d477dd56d332828
[koha-equinox.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 CGI qw( utf8 ); # NOT a CGI script, this is just to keep C4::Templates::gettemplate happy
13 use Koha::Script -cron;
14 use C4::Context;
15 use C4::Debug;
16 use C4::Letters;
17 use C4::Templates;
18 use File::Spec;
19 use Pod::Usage;
20 use Getopt::Long;
21 use C4::Log;
22
23 use Koha::DateUtils;
24 use Koha::Util::OpenDocument;
25 use MIME::Lite;
26
27 my (
28     $stylesheet,
29     $help,
30     $split,
31     $html,
32     $csv,
33     $ods,
34     $delimiter,
35     @letter_codes,
36     $send,
37     @emails,
38 );
39
40 $send = 1;
41 GetOptions(
42     'h|help'  => \$help,
43     's|split' => \$split,
44     'html'    => \$html,
45     'csv'     => \$csv,
46     'ods'     => \$ods,
47     'd|delimiter:s' => \$delimiter,
48     'letter_code:s' => \@letter_codes,
49     'send!'         => \$send,
50     'e|email:s'     => \@emails,
51 ) || pod2usage(1);
52
53 pod2usage(0) if $help;
54
55 my $output_directory = $ARGV[0];
56
57 if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
58     pod2usage({
59         -exitval => 1,
60         -msg => qq{\nError: You must specify a valid and writeable directory to dump the print notices in.\n},
61     });
62 }
63
64 # Default value is html
65 $html = 1 if not $html and not $csv and not $ods;
66
67 if ( $csv and @letter_codes != 1 ) {
68     pod2usage({
69         -exitval => 1,
70         -msg => qq{\nIt is not consistent to use --csv without one (and only one) letter_code\n},
71     });
72 }
73
74 if ( $ods and @letter_codes != 1 ) {
75     pod2usage({
76         -exitval => 1,
77         -msg => qq{\nIt is not consistent to use --ods without one (and only one) letter_code\n},
78     });
79 }
80
81 $delimiter ||= q|,|;
82
83 cronlogaction();
84
85 my $today_iso     = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } ) ;
86 my $today_syspref = output_pref( { dt => dt_from_string, dateonly => 1 } );
87
88 my @all_messages = @{ GetPrintMessages() };
89
90 # Filter by letter_code
91 @all_messages = map {
92     my $letter_code = $_->{letter_code};
93     (
94         grep { /^$letter_code$/ } @letter_codes
95     ) ? $_ : ()
96 } @all_messages if @letter_codes;
97 exit unless @all_messages;
98
99 my ( $html_filenames, $csv_filenames, $ods_filenames );
100 $csv_filenames = print_notices({
101     messages => \@all_messages,
102     split => $split,
103     output_directory => $output_directory,
104     format => 'csv',
105 }) if $csv;
106
107 $ods_filenames = print_notices({
108     messages => \@all_messages,
109     split => $split,
110     output_directory => $output_directory,
111     format => 'ods',
112 }) if $ods;
113
114 if ( $html ) {
115     ## carriage return replaced by <br/> as output is html
116     foreach my $message (@all_messages) {
117         local $_ = $message->{'content'};
118         s/\n/<br \/>/g;
119         s/\r//g;
120         $message->{'content'} = $_;
121     }
122
123     $html_filenames = print_notices({
124         messages => \@all_messages,
125         split => $split,
126         output_directory => $output_directory,
127         format => 'html',
128     });
129 }
130
131 if ( @emails ) {
132     my $files = {
133         html => $html_filenames,
134         csv  => $csv_filenames,
135         ods  => $ods_filenames,
136     };
137     for my $email ( @emails ) {
138         send_files({
139             directory => $output_directory,
140             files => $files,
141             to => $email,
142             from => C4::Context->preference('KohaAdminEmailAddress'), # Should be replaced if bug 8000 is pushed
143         });
144     }
145 }
146
147 sub print_notices {
148     my ( $params ) = @_;
149
150     my $messages = $params->{messages};
151     my $split = $params->{split};
152     my $output_directory = $params->{output_directory};
153     my $format = $params->{format} // 'html';
154
155     die "Format $format is not known"
156         unless $format =~ m[^html$|^csv$|^ods$];
157
158     my ( @filenames, $messages_by_branch );
159
160     if ( $split ) {
161         foreach my $message (@$messages) {
162             push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
163         }
164     } else {
165         $messages_by_branch->{all_branches} = $messages;
166     }
167
168     while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
169         my $letter_codes = @letter_codes == 0 ? 'all' : join '_', @letter_codes;
170         my $filename = $split
171             ? "notices_$letter_codes-" . $today_iso . "-$branchcode.$format"
172             : "notices_$letter_codes-" . $today_iso . ".$format";
173         my $filepath = File::Spec->catdir( $output_directory, $filename );
174         if ( $format eq 'html' ) {
175             generate_html({
176                 messages => $branch_messages,
177                 filepath => $filepath,
178             });
179         } elsif ( $format eq 'csv' ) {
180             generate_csv ({
181                 messages => $branch_messages,
182                 filepath => $filepath,
183             });
184         } elsif ( $format eq 'ods' ) {
185             _generate_ods ({
186                 messages => $branch_messages,
187                 filepath => $filepath,
188             });
189         }
190
191         if ( $send ) {
192             foreach my $message ( @$branch_messages ) {
193                 C4::Letters::_set_message_status(
194                     {
195                         message_id => $message->{'message_id'},
196                         status => 'sent'
197                     }
198                 );
199             }
200         }
201         push @filenames, $filename;
202     }
203     return \@filenames;
204 }
205
206 sub generate_html {
207     my ( $params ) = @_;
208     my $messages = $params->{messages};
209     my $filepath = $params->{filepath};
210
211     my $template =
212       C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
213         new CGI );
214
215     $template->param(
216         stylesheet => C4::Context->preference("NoticeCSS"),
217         today      => $today_syspref,
218         messages   => $messages,
219     );
220
221     open my $OUTPUT, '>encoding(utf-8)', $filepath
222         or die "Could not open $filepath: $!";
223     print $OUTPUT $template->output;
224     close $OUTPUT;
225 }
226
227 sub generate_csv {
228     my ( $params ) = @_;
229     my $messages = $params->{messages};
230     my $filepath = $params->{filepath};
231
232     open my $OUTPUT, '>encoding(utf-8)', $filepath
233         or die "Could not open $filepath: $!";
234     my ( @csv_lines, $headers );
235     foreach my $message ( @$messages ) {
236         my @lines = split /\n/, $message->{content};
237         chomp for @lines;
238
239         # We don't have headers, get them
240         unless ( $headers ) {
241             $headers = $lines[0];
242             say $OUTPUT $headers;
243         }
244
245         shift @lines;
246         for my $line ( @lines ) {
247             next if $line =~ /^\s$/;
248             say $OUTPUT $line;
249         }
250     }
251 }
252
253 sub _generate_ods {
254     my ( $params ) = @_;
255     my $messages = $params->{messages};
256     my $ods_filepath = $params->{filepath};
257
258     # Prepare sheet
259     my $ods_content;
260     my $has_headers;
261     foreach my $message ( @$messages ) {
262         my @message_lines = split /\n/, $message->{content};
263         chomp for @message_lines;
264         # Get headers from first message
265         if ($has_headers) {
266             shift @message_lines;
267         } else {
268             $has_headers = 1;
269         }
270         foreach my $message_line ( @message_lines ) {
271             my @content_row;
272             my @message_cells = split $delimiter, $message_line;
273             foreach ( @message_cells ) {
274                 push @content_row, Encode::encode( 'UTF8', $_ );
275             }
276             push @$ods_content, \@content_row;
277         }
278     }
279
280     # Process
281     generate_ods($ods_filepath, $ods_content);
282 }
283
284 sub send_files {
285     my ( $params ) = @_;
286     my $directory = $params->{directory};
287     my $files = $params->{files};
288     my $to = $params->{to};
289     my $from = $params->{from};
290     return unless $to and $from;
291
292     my $mail = MIME::Lite->new(
293         From     => $from,
294         To       => $to,
295         Subject  => 'Print notices for ' . $today_syspref,
296         Type     => 'multipart/mixed',
297     );
298
299     while ( my ( $type, $filenames ) = each %$files ) {
300         for my $filename ( @$filenames ) {
301             my $mimetype = $type eq 'html'
302                 ? 'text/html'
303                 : $type eq 'csv'
304                     ? 'text/csv'
305                     : $type eq 'ods'
306                         ? 'application/vnd.oasis.opendocument.spreadsheet'
307                         : undef;
308
309             next unless $mimetype;
310
311             my $filepath = File::Spec->catdir( $directory, $filename );
312
313             next unless $filepath or -f $filepath;
314
315             $mail->attach(
316               Type     => $mimetype,
317               Path     => $filepath,
318               Filename => $filename,
319               Encoding => 'base64',
320             );
321         }
322     }
323
324     $mail->send;
325 }
326
327 =head1 NAME
328
329 gather_print_notices - Print waiting print notices
330
331 =head1 SYNOPSIS
332
333 gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-e|--email=your_email@example.org] [-h|--help]
334
335 Will print all waiting print notices to the output_directory.
336
337 The generated filename will be notices-TODAY.[csv|html|ods] or notices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given.
338
339 =head1 OPTIONS
340
341 =over
342
343 =item B<output_directory>
344
345 Define the output directory where the files will be generated.
346
347 =item B<--send|--nosend>
348
349 After files have been generated, messages status is changed from 'pending' to
350 'sent'. This is the default action, without this parameter or with --send.
351 Using --nosend, the message status is not changed.
352
353 =item B<-s|--split>
354
355 Split messages into separate files by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods]
356
357 =item B<--html>
358
359 Generate the print notices in a html file (default is --html, if --csv and --ods are not given).
360
361 =item B<--csv>
362
363 Generate the print notices in a csv file.
364 If you use this parameter, the template should contain 2 lines.
365 The first one the csv headers and the second one the value list.
366
367 For example:
368 cardnumber:patron:email:item
369 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
370
371 You have to combine this option with one (and only one) letter_code.
372
373 =item B<--ods>
374
375 Generate the print notices in a ods file.
376
377 This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file.
378
379 =item B<--letter_code>
380
381 Filter print messages by letter_code.
382 Several letter_code parameters can be given.
383
384 =item B<-e|--email>
385
386 Repeatable.
387 E-mail address to send generated files to.
388
389 =item B<-h|--help>
390
391 Print a brief help message
392
393 =back
394
395 =head1 AUTHOR
396
397 Jesse Weaver <pianohacker@gmail.com>
398
399 Jonathan Druart <jonathan.druart@biblibre.com>
400
401 =head1 COPYRIGHT
402
403 Copyright 2009 Jesse Weaver
404
405 Copyright 2014 BibLibre
406
407 =head1 LICENSE
408 This file is part of Koha.
409
410 Koha is free software; you can redistribute it and/or modify it
411 under the terms of the GNU General Public License as published by
412 the Free Software Foundation; either version 3 of the License, or
413 (at your option) any later version.
414
415 Koha is distributed in the hope that it will be useful, but
416 WITHOUT ANY WARRANTY; without even the implied warranty of
417 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
418 GNU General Public License for more details.
419
420 You should have received a copy of the GNU General Public License
421 along with Koha; if not, see <http://www.gnu.org/licenses>.
422
423 =cut