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