Bug 11603: (follow-up) pod
[koha.git] / misc / cronjobs / gather_print_notices.pl
blob0e8cee36949a59ebad500cb6fb6aa1511001674b
1 #!/usr/bin/perl -w
3 use Modern::Perl;
5 BEGIN {
6 # find Koha's Perl modules
7 # test carefully before changing this
8 use FindBin;
9 eval { require "$FindBin::Bin/../kohalib.pl" };
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;
23 use File::Basename qw( dirname );
24 use Koha::DateUtils;
26 my (
27 $stylesheet,
28 $help,
29 $split,
30 $html,
31 $csv,
32 $ods,
33 $delimiter,
34 @letter_codes,
35 $send,
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);
50 pod2usage(0) if $help;
52 my $output_directory = $ARGV[0];
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 });
61 # Default value is html
62 $html = 1 if not $html and not $csv and not $ods;
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 });
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 });
78 $delimiter ||= q|,|;
80 cronlogaction();
82 my $today = C4::Dates->new();
83 my @all_messages = @{ GetPrintMessages() };
85 # Filter by letter_code
86 @all_messages = map {
87 my $letter_code = $_->{letter_code};
89 grep { /^$letter_code$/ } @letter_codes
90 ) ? $_ : ()
91 } @all_messages if @letter_codes;
92 exit unless @all_messages;
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;
102 $ods_filenames = print_notices({
103 messages => \@all_messages,
104 split => $split,
105 output_directory => $output_directory,
106 format => 'ods',
107 }) if $ods;
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'} = $_;
118 $html_filenames = print_notices({
119 messages => \@all_messages,
120 split => $split,
121 output_directory => $output_directory,
122 format => 'html',
126 sub print_notices {
127 my ( $params ) = @_;
129 my $messages = $params->{messages};
130 my $split = $params->{split};
131 my $output_directory = $params->{output_directory};
132 my $format = $params->{format} // 'html';
134 die "Format $format is not known"
135 unless $format =~ m[^html$|^csv$|^ods$];
137 my ( @filenames, $messages_by_branch );
139 if ( $split ) {
140 foreach my $message (@$messages) {
141 push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
143 } else {
144 $messages_by_branch->{all_branches} = $messages;
147 while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
148 my $letter_code = @letter_codes == 1 ? $letter_codes[0] : 'hold';
149 my $filename = $split
150 ? "notices_$letter_code-" . $today->output('iso') . "-$branchcode.$format"
151 : "notices_$letter_code-" . $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,
158 } elsif ( $format eq 'csv' ) {
159 generate_csv ({
160 messages => $branch_messages,
161 filepath => $filepath,
163 } elsif ( $format eq 'ods' ) {
164 generate_ods ({
165 messages => $branch_messages,
166 filepath => $filepath,
170 if ( $send ) {
171 foreach my $message ( @$branch_messages ) {
172 C4::Letters::_set_message_status(
174 message_id => $message->{'message_id'},
175 status => 'sent'
180 push @filenames, $filename;
182 return \@filenames;
185 sub generate_html {
186 my ( $params ) = @_;
187 my $messages = $params->{messages};
188 my $filepath = $params->{filepath};
190 my $template =
191 C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
192 new CGI );
194 $template->param(
195 stylesheet => C4::Context->preference("NoticeCSS"),
196 today => $today->output(),
197 messages => $messages,
200 open my $OUTPUT, '>encoding(utf-8)', $filepath
201 or die "Could not open $filepath: $!";
202 print $OUTPUT $template->output;
203 close $OUTPUT;
206 sub generate_csv {
207 my ( $params ) = @_;
208 my $messages = $params->{messages};
209 my $filepath = $params->{filepath};
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;
218 # We don't have headers, get them
219 unless ( $headers ) {
220 $headers = $lines[0];
221 say $OUTPUT $headers;
224 shift @lines;
225 for my $line ( @lines ) {
226 next if $line =~ /^\s$/;
227 say $OUTPUT $line;
232 sub generate_ods {
233 my ( $params ) = @_;
234 my $messages = $params->{messages};
235 my $filepath = $params->{filepath};
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'
245 my $table = $doc->getTable(0);
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;
253 # We don't have headers, get them
254 unless ( @headers ) {
255 @headers = split $delimiter, $lines[0];
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++;
265 $i = 1;
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 );
277 $i++;
280 $doc->save();
283 =head1 NAME
285 gather_print_notices - Print waiting print notices
287 =head1 SYNOPSIS
289 gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-h|--help]
291 Will print all waiting print notices to the output_directory.
293 The generated filename will be notices-TODAY.[csv|html|ods] or notices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given.
295 =head1 OPTIONS
297 =over
299 =item B<output_directory>
301 Define the output directory where the files will be generated.
303 =item B<--send|--nosend>
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.
309 =item B<-s|--split>
311 Split messages into separate files by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods]
313 =item B<--html>
315 Generate the print notices in a html file (default is --html, if --csv and --ods are not given).
317 =item B<--csv>
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.
323 For example:
324 cardnumber:patron:email:item
325 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
327 You have to combine this option without one (and only one) letter_code.
329 =item B<--ods>
331 Generate the print notices in a ods file.
333 This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file.
335 =item B<--letter_code>
337 Filter print messages by letter_code.
338 Several letter_code parameters can be given.
340 =item B<-h|--help>
342 Print a brief help message
344 =back
346 =head1 AUTHOR
348 Jesse Weaver <pianohacker@gmail.com>
350 Jonathan Druart <jonathan.druart@biblibre.com>
352 =head1 COPYRIGHT
354 Copyright 2009 Jesse Weaver
356 Copyright 2014 BibLibre
358 =head1 LICENSE
359 This file is part of Koha.
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.
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.
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>.
374 =cut