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