5 use CGI; # NOT a CGI script, this is just to keep C4::Templates::gettemplate happy
6 use Koha::Script -cron;
8 use C4::Letters qw( GetPrintMessages );
11 use Pod::Usage qw( pod2usage );
12 use Getopt::Long qw( GetOptions );
13 use C4::Log qw( cronlogaction );
15 use Koha::DateUtils qw( dt_from_string output_pref );
17 use Koha::Util::OpenDocument qw( generate_ods );
38 'd|delimiter:s' => \$delimiter,
39 'letter_code:s' => \@letter_codes,
41 'e|email:s' => \@emails,
44 pod2usage(0) if $help;
46 my $output_directory = $ARGV[0];
48 if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
51 -msg => qq{\nError: You must specify a valid and writeable directory to dump the print notices in.\n},
55 # Default value is html
56 $html = 1 if not $html and not $csv and not $ods;
58 if ( $csv and @letter_codes != 1 ) {
61 -msg => qq{\nIt is not consistent to use --csv without one (and only one) letter_code\n},
65 if ( $ods and @letter_codes != 1 ) {
68 -msg => qq{\nIt is not consistent to use --ods without one (and only one) letter_code\n},
76 my $today_iso = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } ) ;
77 my $today_syspref = output_pref( { dt => dt_from_string, dateonly => 1 } );
79 my @all_messages = @{ GetPrintMessages() };
81 # Filter by letter_code
83 my $letter_code = $_->{letter_code};
85 grep { $_ eq $letter_code } @letter_codes
87 } @all_messages if @letter_codes;
88 exit unless @all_messages;
90 my ( $html_filenames, $csv_filenames, $ods_filenames );
91 $csv_filenames = print_notices({
92 messages => \@all_messages,
94 output_directory => $output_directory,
98 $ods_filenames = print_notices({
99 messages => \@all_messages,
101 output_directory => $output_directory,
106 ## carriage return replaced by <br/> as output is html
107 foreach my $message (@all_messages) {
108 local $_ = $message->{'content'};
111 $message->{'content'} = $_;
114 $html_filenames = print_notices({
115 messages => \@all_messages,
117 output_directory => $output_directory,
124 html => $html_filenames,
125 csv => $csv_filenames,
126 ods => $ods_filenames,
128 for my $email ( @emails ) {
130 directory => $output_directory,
133 from => C4::Context->preference('KohaAdminEmailAddress'), # Should be replaced if bug 8000 is pushed
141 my $messages = $params->{messages};
142 my $split = $params->{split};
143 my $output_directory = $params->{output_directory};
144 my $format = $params->{format} // 'html';
146 die "Format $format is not known"
147 unless $format =~ m[^html$|^csv$|^ods$];
149 my ( @filenames, $messages_by_branch );
152 foreach my $message (@$messages) {
153 push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
156 $messages_by_branch->{all_branches} = $messages;
159 while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
160 my $letter_codes = @letter_codes == 0 ? 'all' : join '_', @letter_codes;
161 my $filename = $split
162 ? "notices_$letter_codes-" . $today_iso . "-$branchcode.$format"
163 : "notices_$letter_codes-" . $today_iso . ".$format";
164 my $filepath = File::Spec->catdir( $output_directory, $filename );
165 if ( $format eq 'html' ) {
167 messages => $branch_messages,
168 filepath => $filepath,
170 } elsif ( $format eq 'csv' ) {
172 messages => $branch_messages,
173 filepath => $filepath,
175 } elsif ( $format eq 'ods' ) {
177 messages => $branch_messages,
178 filepath => $filepath,
183 foreach my $message ( @$branch_messages ) {
184 C4::Letters::_set_message_status(
186 message_id => $message->{'message_id'},
192 push @filenames, $filename;
199 my $messages = $params->{messages};
200 my $filepath = $params->{filepath};
203 C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
207 stylesheet => C4::Context->preference("NoticeCSS"),
208 today => $today_syspref,
209 messages => $messages,
212 open my $OUTPUT, '>encoding(utf-8)', $filepath
213 or die "Could not open $filepath: $!";
214 print $OUTPUT $template->output;
220 my $messages = $params->{messages};
221 my $filepath = $params->{filepath};
223 open my $OUTPUT, '>encoding(utf-8)', $filepath
224 or die "Could not open $filepath: $!";
226 foreach my $message ( @$messages ) {
227 my @lines = split /\n/, $message->{content};
230 # We don't have headers, get them
231 unless ( $headers ) {
232 $headers = $lines[0];
233 say $OUTPUT $headers;
237 for my $line ( @lines ) {
238 next if $line =~ /^\s$/;
246 my $messages = $params->{messages};
247 my $ods_filepath = $params->{filepath};
252 foreach my $message ( @$messages ) {
253 my @message_lines = split /\n/, $message->{content};
254 chomp for @message_lines;
255 # Get headers from first message
257 shift @message_lines;
261 foreach my $message_line ( @message_lines ) {
263 my @message_cells = split $delimiter, $message_line;
264 foreach ( @message_cells ) {
265 push @content_row, Encode::encode( 'UTF8', $_ );
267 push @$ods_content, \@content_row;
272 generate_ods($ods_filepath, $ods_content);
277 my $directory = $params->{directory};
278 my $files = $params->{files};
279 my $to = $params->{to};
280 my $from = $params->{from};
281 return unless $to and $from;
283 my $email = Koha::Email->create(
287 subject => 'Print notices for ' . $today_syspref,
291 while ( my ( $type, $filenames ) = each %$files ) {
292 for my $filename ( @$filenames ) {
293 my $mimetype = $type eq 'html'
298 ? 'application/vnd.oasis.opendocument.spreadsheet'
301 next unless $mimetype;
303 my $filepath = File::Spec->catfile( $directory, $filename );
305 next unless $filepath or -f $filepath;
309 content_type => $mimetype,
311 disposition => 'attachment',
321 gather_print_notices - Print waiting print notices
325 gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-e|--email=your_email@example.org] [-h|--help]
327 Will print all waiting print notices to the output_directory.
329 The generated filename will be notices-TODAY.[csv|html|ods] or notices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given.
335 =item B<output_directory>
337 Define the output directory where the files will be generated.
339 =item B<--send|--nosend>
341 After files have been generated, messages status is changed from 'pending' to
342 'sent'. This is the default action, without this parameter or with --send.
343 Using --nosend, the message status is not changed.
347 Split messages into separate files by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods]
351 Generate the print notices in a html file (default is --html, if --csv and --ods are not given).
355 Generate the print notices in a csv file.
356 If you use this parameter, the template should contain 2 lines.
357 The first one the csv headers and the second one the value list.
360 cardnumber:patron:email:item
361 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
363 You have to combine this option with one (and only one) letter_code.
367 Generate the print notices in a ods file.
369 This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file.
371 =item B<--letter_code>
373 Filter print messages by letter_code.
374 Several letter_code parameters can be given.
379 E-mail address to send generated files to.
383 Print a brief help message
389 Jesse Weaver <pianohacker@gmail.com>
391 Jonathan Druart <jonathan.druart@biblibre.com>
395 Copyright 2009 Jesse Weaver
397 Copyright 2014 BibLibre
400 This file is part of Koha.
402 Koha is free software; you can redistribute it and/or modify it
403 under the terms of the GNU General Public License as published by
404 the Free Software Foundation; either version 3 of the License, or
405 (at your option) any later version.
407 Koha is distributed in the hope that it will be useful, but
408 WITHOUT ANY WARRANTY; without even the implied warranty of
409 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
410 GNU General Public License for more details.
412 You should have received a copy of the GNU General Public License
413 along with Koha; if not, see <http://www.gnu.org/licenses>.