Bug 11678: QA follow-up - Fixing some typos in the documentation
[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 CGI qw( utf8 ); # NOT a CGI script, this is just to keep C4::Templates::gettemplate happy
13 use C4::Context;
14 use C4::Dates;
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 File::Basename qw( dirname );
24 use Koha::DateUtils;
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        = C4::Dates->new();
86 my @all_messages = @{ GetPrintMessages() };
87
88 # Filter by letter_code
89 @all_messages = map {
90     my $letter_code = $_->{letter_code};
91     (
92         grep { /^$letter_code$/ } @letter_codes
93     ) ? $_ : ()
94 } @all_messages if @letter_codes;
95 exit unless @all_messages;
96
97 my ( $html_filenames, $csv_filenames, $ods_filenames );
98 $csv_filenames = print_notices({
99     messages => \@all_messages,
100     split => $split,
101     output_directory => $output_directory,
102     format => 'csv',
103 }) if $csv;
104
105 $ods_filenames = print_notices({
106     messages => \@all_messages,
107     split => $split,
108     output_directory => $output_directory,
109     format => 'ods',
110 }) if $ods;
111
112 if ( $html ) {
113     ## carriage return replaced by <br/> as output is html
114     foreach my $message (@all_messages) {
115         local $_ = $message->{'content'};
116         s/\n/<br \/>/g;
117         s/\r//g;
118         $message->{'content'} = $_;
119     }
120
121     $html_filenames = print_notices({
122         messages => \@all_messages,
123         split => $split,
124         output_directory => $output_directory,
125         format => 'html',
126     });
127 }
128
129 if ( @emails ) {
130     my $files = {
131         html => $html_filenames,
132         csv  => $csv_filenames,
133         ods  => $ods_filenames,
134     };
135     for my $email ( @emails ) {
136         send_files({
137             directory => $output_directory,
138             files => $files,
139             to => $email,
140             from => C4::Context->preference('KohaAdminEmailAddress'), # Should be replaced if bug 8000 is pushed
141         });
142     }
143 }
144
145 sub print_notices {
146     my ( $params ) = @_;
147
148     my $messages = $params->{messages};
149     my $split = $params->{split};
150     my $output_directory = $params->{output_directory};
151     my $format = $params->{format} // 'html';
152
153     die "Format $format is not known"
154         unless $format =~ m[^html$|^csv$|^ods$];
155
156     my ( @filenames, $messages_by_branch );
157
158     if ( $split ) {
159         foreach my $message (@$messages) {
160             push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
161         }
162     } else {
163         $messages_by_branch->{all_branches} = $messages;
164     }
165
166     while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
167         my $letter_codes = @letter_codes == 0 ? 'all' : join '_', @letter_codes;
168         my $filename = $split
169             ? "notices_$letter_codes-" . $today->output('iso') . "-$branchcode.$format"
170             : "notices_$letter_codes-" . $today->output('iso') . ".$format";
171         my $filepath = File::Spec->catdir( $output_directory, $filename );
172         if ( $format eq 'html' ) {
173             generate_html({
174                 messages => $branch_messages,
175                 filepath => $filepath,
176             });
177         } elsif ( $format eq 'csv' ) {
178             generate_csv ({
179                 messages => $branch_messages,
180                 filepath => $filepath,
181             });
182         } elsif ( $format eq 'ods' ) {
183             generate_ods ({
184                 messages => $branch_messages,
185                 filepath => $filepath,
186             });
187         }
188
189         if ( $send ) {
190             foreach my $message ( @$branch_messages ) {
191                 C4::Letters::_set_message_status(
192                     {
193                         message_id => $message->{'message_id'},
194                         status => 'sent'
195                     }
196                 );
197             }
198         }
199         push @filenames, $filename;
200     }
201     return \@filenames;
202 }
203
204 sub generate_html {
205     my ( $params ) = @_;
206     my $messages = $params->{messages};
207     my $filepath = $params->{filepath};
208
209     my $template =
210       C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
211         new CGI );
212
213     $template->param(
214         stylesheet => C4::Context->preference("NoticeCSS"),
215         today      => $today->output(),
216         messages   => $messages,
217     );
218
219     open my $OUTPUT, '>encoding(utf-8)', $filepath
220         or die "Could not open $filepath: $!";
221     print $OUTPUT $template->output;
222     close $OUTPUT;
223 }
224
225 sub generate_csv {
226     my ( $params ) = @_;
227     my $messages = $params->{messages};
228     my $filepath = $params->{filepath};
229
230     open my $OUTPUT, '>encoding(utf-8)', $filepath
231         or die "Could not open $filepath: $!";
232     my ( @csv_lines, $headers );
233     foreach my $message ( @$messages ) {
234         my @lines = split /\n/, $message->{content};
235         chomp for @lines;
236
237         # We don't have headers, get them
238         unless ( $headers ) {
239             $headers = $lines[0];
240             say $OUTPUT $headers;
241         }
242
243         shift @lines;
244         for my $line ( @lines ) {
245             next if $line =~ /^\s$/;
246             say $OUTPUT $line;
247         }
248     }
249 }
250
251 sub generate_ods {
252     my ( $params ) = @_;
253     my $messages = $params->{messages};
254     my $filepath = $params->{filepath};
255
256     use OpenOffice::OODoc;
257     my $tmpdir = dirname $filepath;
258     odfWorkingDirectory( $tmpdir );
259     my $container = odfContainer( $filepath, create => 'spreadsheet' );
260     my $doc = odfDocument (
261         container => $container,
262         part      => 'content'
263     );
264     my $table = $doc->getTable(0);
265
266     my @headers;
267     my ( $nb_rows, $nb_cols, $i ) = ( scalar(@$messages), 0, 0 );
268     foreach my $message ( @$messages ) {
269         my @lines = split /\n/, $message->{content};
270         chomp for @lines;
271
272         # We don't have headers, get them
273         unless ( @headers ) {
274             @headers = split $delimiter, $lines[0];
275
276             $nb_cols = @headers;
277             $doc->expandTable( $table, $nb_rows + 1, $nb_cols );
278             my $row = $doc->getRow( $table, 0 );
279             my $j = 0;
280             for my $header ( @headers ) {
281                 $doc->cellValue( $row, $j, Encode::encode( 'UTF8', $header ) );
282                 $j++;
283             }
284             $i = 1;
285         }
286
287         shift @lines; # remove headers
288         for my $line ( @lines ) {
289             my @row_data = split $delimiter, $line;
290             my $row = $doc->getRow( $table, $i );
291             # Note scalar(@$row_data) should be equal to $nb_cols
292             for ( my $j = 0 ; $j < scalar(@row_data) ; $j++ ) {
293                 my $value = Encode::encode( 'UTF8', $row_data[$j] );
294                 $doc->cellValue( $row, $j, $value );
295             }
296             $i++;
297         }
298     }
299     $doc->save();
300 }
301
302 sub send_files {
303     my ( $params ) = @_;
304     my $directory = $params->{directory};
305     my $files = $params->{files};
306     my $to = $params->{to};
307     my $from = $params->{from};
308     return unless $to and $from;
309
310     my $mail = MIME::Lite->new(
311         From     => $from,
312         To       => $to,
313         Subject  => 'Print notices for ' . $today->output(),
314         Type     => 'multipart/mixed',
315     );
316
317     while ( my ( $type, $filenames ) = each %$files ) {
318         for my $filename ( @$filenames ) {
319             my $mimetype = $type eq 'html'
320                 ? 'text/html'
321                 : $type eq 'csv'
322                     ? 'text/csv'
323                     : $type eq 'ods'
324                         ? 'application/vnd.oasis.opendocument.spreadsheet'
325                         : undef;
326
327             next unless $mimetype;
328
329             my $filepath = File::Spec->catdir( $directory, $filename );
330
331             next unless $filepath or -f $filepath;
332
333             $mail->attach(
334               Type     => $mimetype,
335               Path     => $filepath,
336               Filename => $filename,
337               Encoding => 'base64',
338             );
339         }
340     }
341
342     $mail->send;
343 }
344
345 =head1 NAME
346
347 gather_print_notices - Print waiting print notices
348
349 =head1 SYNOPSIS
350
351 gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-e|--email=your_email@example.org] [-h|--help]
352
353 Will print all waiting print notices to the output_directory.
354
355 The generated filename will be notices-TODAY.[csv|html|ods] or notices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given.
356
357 =head1 OPTIONS
358
359 =over
360
361 =item B<output_directory>
362
363 Define the output directory where the files will be generated.
364
365 =item B<--send|--nosend>
366
367 After files have been generated, messages status is changed from 'pending' to
368 'sent'. This is the default action, without this parameter or with --send.
369 Using --nosend, the message status is not changed.
370
371 =item B<-s|--split>
372
373 Split messages into separate files by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods]
374
375 =item B<--html>
376
377 Generate the print notices in a html file (default is --html, if --csv and --ods are not given).
378
379 =item B<--csv>
380
381 Generate the print notices in a csv file.
382 If you use this parameter, the template should contain 2 lines.
383 The first one the csv headers and the second one the value list.
384
385 For example:
386 cardnumber:patron:email:item
387 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
388
389 You have to combine this option with one (and only one) letter_code.
390
391 =item B<--ods>
392
393 Generate the print notices in a ods file.
394
395 This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file.
396
397 =item B<--letter_code>
398
399 Filter print messages by letter_code.
400 Several letter_code parameters can be given.
401
402 =item B<-e|--email>
403
404 Repeatable.
405 E-mail address to send generated files to.
406
407 =item B<-h|--help>
408
409 Print a brief help message
410
411 =back
412
413 =head1 AUTHOR
414
415 Jesse Weaver <pianohacker@gmail.com>
416
417 Jonathan Druart <jonathan.druart@biblibre.com>
418
419 =head1 COPYRIGHT
420
421 Copyright 2009 Jesse Weaver
422
423 Copyright 2014 BibLibre
424
425 =head1 LICENSE
426 This file is part of Koha.
427
428 Koha is free software; you can redistribute it and/or modify it
429 under the terms of the GNU General Public License as published by
430 the Free Software Foundation; either version 3 of the License, or
431 (at your option) any later version.
432
433 Koha is distributed in the hope that it will be useful, but
434 WITHOUT ANY WARRANTY; without even the implied warranty of
435 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
436 GNU General Public License for more details.
437
438 You should have received a copy of the GNU General Public License
439 along with Koha; if not, see <http://www.gnu.org/licenses>.
440
441 =cut