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