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