Bug 10933: (follow-up) fix the previous patch to work with master
[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
26 my (
27     $stylesheet,
28     $help,
29     $split,
30     $html,
31     $csv,
32     $ods,
33     $delimiter,
34     @letter_codes,
35     $send,
36 );
37
38 $send = 1;
39 GetOptions(
40     'h|help'  => \$help,
41     's|split' => \$split,
42     'html'    => \$html,
43     'csv'     => \$csv,
44     'ods'     => \$ods,
45     'd|delimiter:s' => \$delimiter,
46     'letter_code:s' => \@letter_codes,
47     'send!'         => \$send,
48 ) || pod2usage(1);
49
50 pod2usage(0) if $help;
51
52 my $output_directory = $ARGV[0];
53
54 if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
55     pod2usage({
56         -exitval => 1,
57         -msg => qq{\nError: You must specify a valid and writeable directory to dump the print notices in.\n},
58     });
59 }
60
61 # Default value is html
62 $html = 1 if not $html and not $csv and not $ods;
63
64 if ( $csv and @letter_codes != 1 ) {
65     pod2usage({
66         -exitval => 1,
67         -msg => qq{\nIt is not consistent to use --csv without one (and only one) letter_code\n},
68     });
69 }
70
71 if ( $ods and @letter_codes != 1 ) {
72     pod2usage({
73         -exitval => 1,
74         -msg => qq{\nIt is not consistent to use --ods without one (and only one) letter_code\n},
75     });
76 }
77
78 $delimiter ||= q|,|;
79
80 cronlogaction();
81
82 my $today        = C4::Dates->new();
83 my @all_messages = @{ GetPrintMessages() };
84
85 # Filter by letter_code
86 @all_messages = map {
87     my $letter_code = $_->{letter_code};
88     (
89         grep { /^$letter_code$/ } @letter_codes
90     ) ? $_ : ()
91 } @all_messages if @letter_codes;
92 exit unless @all_messages;
93
94 my ( $html_filenames, $csv_filenames, $ods_filenames );
95 $csv_filenames = print_notices({
96     messages => \@all_messages,
97     split => $split,
98     output_directory => $output_directory,
99     format => 'csv',
100 }) if $csv;
101
102 $ods_filenames = print_notices({
103     messages => \@all_messages,
104     split => $split,
105     output_directory => $output_directory,
106     format => 'ods',
107 }) if $ods;
108
109 if ( $html ) {
110     ## carriage return replaced by <br/> as output is html
111     foreach my $message (@all_messages) {
112         local $_ = $message->{'content'};
113         s/\n/<br \/>/g;
114         s/\r//g;
115         $message->{'content'} = $_;
116     }
117
118     $html_filenames = print_notices({
119         messages => \@all_messages,
120         split => $split,
121         output_directory => $output_directory,
122         format => 'html',
123     });
124 }
125
126 sub print_notices {
127     my ( $params ) = @_;
128
129     my $messages = $params->{messages};
130     my $split = $params->{split};
131     my $output_directory = $params->{output_directory};
132     my $format = $params->{format} // 'html';
133
134     die "Format $format is not known"
135         unless $format =~ m[^html$|^csv$|^ods$];
136
137     my ( @filenames, $messages_by_branch );
138
139     if ( $split ) {
140         foreach my $message (@$messages) {
141             push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
142         }
143     } else {
144         $messages_by_branch->{all_branches} = $messages;
145     }
146
147     while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
148         my $letter_codes = @letter_codes == 0 ? 'all' : join '_', @letter_codes;
149         my $filename = $split
150             ? "notices_$letter_codes-" . $today->output('iso') . "-$branchcode.$format"
151             : "notices_$letter_codes-" . $today->output('iso') . ".$format";
152         my $filepath = File::Spec->catdir( $output_directory, $filename );
153         if ( $format eq 'html' ) {
154             generate_html({
155                 messages => $branch_messages,
156                 filepath => $filepath,
157             });
158         } elsif ( $format eq 'csv' ) {
159             generate_csv ({
160                 messages => $branch_messages,
161                 filepath => $filepath,
162             });
163         } elsif ( $format eq 'ods' ) {
164             generate_ods ({
165                 messages => $branch_messages,
166                 filepath => $filepath,
167             });
168         }
169
170         if ( $send ) {
171             foreach my $message ( @$branch_messages ) {
172                 C4::Letters::_set_message_status(
173                     {
174                         message_id => $message->{'message_id'},
175                         status => 'sent'
176                     }
177                 );
178             }
179         }
180         push @filenames, $filename;
181     }
182     return \@filenames;
183 }
184
185 sub generate_html {
186     my ( $params ) = @_;
187     my $messages = $params->{messages};
188     my $filepath = $params->{filepath};
189
190     my $template =
191       C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
192         new CGI );
193
194     $template->param(
195         stylesheet => C4::Context->preference("NoticeCSS"),
196         today      => $today->output(),
197         messages   => $messages,
198     );
199
200     open my $OUTPUT, '>encoding(utf-8)', $filepath
201         or die "Could not open $filepath: $!";
202     print $OUTPUT $template->output;
203     close $OUTPUT;
204 }
205
206 sub generate_csv {
207     my ( $params ) = @_;
208     my $messages = $params->{messages};
209     my $filepath = $params->{filepath};
210
211     open my $OUTPUT, '>encoding(utf-8)', $filepath
212         or die "Could not open $filepath: $!";
213     my ( @csv_lines, $headers );
214     foreach my $message ( @$messages ) {
215         my @lines = split /\n/, $message->{content};
216         chomp for @lines;
217
218         # We don't have headers, get them
219         unless ( $headers ) {
220             $headers = $lines[0];
221             say $OUTPUT $headers;
222         }
223
224         shift @lines;
225         for my $line ( @lines ) {
226             next if $line =~ /^\s$/;
227             say $OUTPUT $line;
228         }
229     }
230 }
231
232 sub generate_ods {
233     my ( $params ) = @_;
234     my $messages = $params->{messages};
235     my $filepath = $params->{filepath};
236
237     use OpenOffice::OODoc;
238     my $tmpdir = dirname $filepath;
239     odfWorkingDirectory( $tmpdir );
240     my $container = odfContainer( $filepath, create => 'spreadsheet' );
241     my $doc = odfDocument (
242         container => $container,
243         part      => 'content'
244     );
245     my $table = $doc->getTable(0);
246
247     my @headers;
248     my ( $nb_rows, $nb_cols, $i ) = ( scalar(@$messages), 0, 0 );
249     foreach my $message ( @$messages ) {
250         my @lines = split /\n/, $message->{content};
251         chomp for @lines;
252
253         # We don't have headers, get them
254         unless ( @headers ) {
255             @headers = split $delimiter, $lines[0];
256
257             $nb_cols = @headers;
258             $doc->expandTable( $table, $nb_rows + 1, $nb_cols );
259             my $row = $doc->getRow( $table, 0 );
260             my $j = 0;
261             for my $header ( @headers ) {
262                 $doc->cellValue( $row, $j, Encode::encode( 'UTF8', $header ) );
263                 $j++;
264             }
265             $i = 1;
266         }
267
268         shift @lines; # remove headers
269         for my $line ( @lines ) {
270             my @row_data = split $delimiter, $line;
271             my $row = $doc->getRow( $table, $i );
272             # Note scalar(@$row_data) should be equal to $nb_cols
273             for ( my $j = 0 ; $j < scalar(@row_data) ; $j++ ) {
274                 my $value = Encode::encode( 'UTF8', $row_data[$j] );
275                 $doc->cellValue( $row, $j, $value );
276             }
277             $i++;
278         }
279     }
280     $doc->save();
281 }
282
283 =head1 NAME
284
285 gather_print_notices - Print waiting print notices
286
287 =head1 SYNOPSIS
288
289 gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-h|--help]
290
291 Will print all waiting print notices to the output_directory.
292
293 The generated filename will be notices-TODAY.[csv|html|ods] or notices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given.
294
295 =head1 OPTIONS
296
297 =over
298
299 =item B<output_directory>
300
301 Define the output directory where the files will be generated.
302
303 =item B<--send|--nosend>
304
305 After files have been generated, messages status is changed from 'pending' to
306 'sent'. This is the default action, without this parameter or with --send.
307 Using --nosend, the message status is not changed.
308
309 =item B<-s|--split>
310
311 Split messages into separate files by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods]
312
313 =item B<--html>
314
315 Generate the print notices in a html file (default is --html, if --csv and --ods are not given).
316
317 =item B<--csv>
318
319 Generate the print notices in a csv file.
320 If you use this parameter, the template should contain 2 lines.
321 The first one the csv headers and the second one the value list.
322
323 For example:
324 cardnumber:patron:email:item
325 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
326
327 You have to combine this option without one (and only one) letter_code.
328
329 =item B<--ods>
330
331 Generate the print notices in a ods file.
332
333 This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file.
334
335 =item B<--letter_code>
336
337 Filter print messages by letter_code.
338 Several letter_code parameters can be given.
339
340 =item B<-h|--help>
341
342 Print a brief help message
343
344 =back
345
346 =head1 AUTHOR
347
348 Jesse Weaver <pianohacker@gmail.com>
349
350 Jonathan Druart <jonathan.druart@biblibre.com>
351
352 =head1 COPYRIGHT
353
354 Copyright 2009 Jesse Weaver
355
356 Copyright 2014 BibLibre
357
358 =head1 LICENSE
359 This file is part of Koha.
360
361 Koha is free software; you can redistribute it and/or modify it
362 under the terms of the GNU General Public License as published by
363 the Free Software Foundation; either version 3 of the License, or
364 (at your option) any later version.
365
366 Koha is distributed in the hope that it will be useful, but
367 WITHOUT ANY WARRANTY; without even the implied warranty of
368 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
369 GNU General Public License for more details.
370
371 You should have received a copy of the GNU General Public License
372 along with Koha; if not, see <http://www.gnu.org/licenses>.
373
374 =cut