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 );
18 use Koha::SMTP::Servers;
32 my $command_line_options = join(" ",@ARGV);
41 'd|delimiter:s' => \$delimiter,
42 'letter_code:s' => \@letter_codes,
44 'e|email:s' => \@emails,
47 pod2usage(0) if $help;
49 my $output_directory = $ARGV[0];
51 if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
54 -msg => qq{\nError: You must specify a valid and writeable directory to dump the print notices in.\n},
58 # Default value is html
59 $html = 1 if not $html and not $csv and not $ods;
61 if ( $csv and @letter_codes != 1 ) {
64 -msg => qq{\nIt is not consistent to use --csv without one (and only one) letter_code\n},
68 if ( $ods and @letter_codes != 1 ) {
71 -msg => qq{\nIt is not consistent to use --ods without one (and only one) letter_code\n},
77 cronlogaction({ info => $command_line_options });
79 my $today_iso = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } ) ;
80 my $today_syspref = output_pref( { dt => dt_from_string, dateonly => 1 } );
82 my @all_messages = @{ GetPrintMessages() };
84 # Filter by letter_code
86 my $letter_code = $_->{letter_code};
88 grep { $_ eq $letter_code } @letter_codes
90 } @all_messages if @letter_codes;
91 exit unless @all_messages;
93 my ( $html_filenames, $csv_filenames, $ods_filenames );
94 $csv_filenames = print_notices({
95 messages => \@all_messages,
97 output_directory => $output_directory,
101 $ods_filenames = print_notices({
102 messages => \@all_messages,
104 output_directory => $output_directory,
109 $html_filenames = print_notices({
110 messages => \@all_messages,
112 output_directory => $output_directory,
119 html => $html_filenames,
120 csv => $csv_filenames,
121 ods => $ods_filenames,
124 my $transport = Koha::SMTP::Servers->get_default->transport;
126 for my $email ( @emails ) {
129 directory => $output_directory,
131 from => C4::Context->preference('KohaAdminEmailAddress'), # Should be replaced if bug 8000 is pushed
133 transport => $transport,
139 cronlogaction({ action => 'End', info => "COMPLETED" });
144 my $messages = $params->{messages};
145 my $split = $params->{split};
146 my $output_directory = $params->{output_directory};
147 my $format = $params->{format} // 'html';
149 die "Format $format is not known"
150 unless $format =~ m[^html$|^csv$|^ods$];
152 my ( @filenames, $messages_by_branch );
155 foreach my $message (@$messages) {
156 push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
159 $messages_by_branch->{all_branches} = $messages;
162 while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
163 my $letter_codes = @letter_codes == 0 ? 'all' : join '_', @letter_codes;
164 my $filename = $split
165 ? "notices_$letter_codes-" . $today_iso . "-$branchcode.$format"
166 : "notices_$letter_codes-" . $today_iso . ".$format";
167 my $filepath = File::Spec->catdir( $output_directory, $filename );
168 if ( $format eq 'html' ) {
170 messages => $branch_messages,
171 filepath => $filepath,
173 } elsif ( $format eq 'csv' ) {
175 messages => $branch_messages,
176 filepath => $filepath,
178 } elsif ( $format eq 'ods' ) {
180 messages => $branch_messages,
181 filepath => $filepath,
186 foreach my $message ( @$branch_messages ) {
187 C4::Letters::_set_message_status(
189 message_id => $message->{'message_id'},
195 push @filenames, $filename;
202 my $messages = $params->{messages};
203 my $filepath = $params->{filepath};
206 C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
209 foreach my $message (@$messages) {
210 $message->{is_html} = $message->{content_type} && $message->{content_type} =~ /html/i;
214 stylesheet => C4::Context->preference("NoticeCSS"),
215 today => $today_syspref,
216 messages => $messages,
219 open my $OUTPUT, '>encoding(utf-8)', $filepath
220 or die "Could not open $filepath: $!";
221 print $OUTPUT $template->output;
227 my $messages = $params->{messages};
228 my $filepath = $params->{filepath};
230 open my $OUTPUT, '>encoding(utf-8)', $filepath
231 or die "Could not open $filepath: $!";
233 foreach my $message ( @$messages ) {
234 my @lines = split /\n/, $message->{content};
237 # We don't have headers, get them
238 unless ( $headers ) {
239 $headers = $lines[0];
240 say $OUTPUT $headers;
244 for my $line ( @lines ) {
245 next if $line =~ /^\s$/;
253 my $messages = $params->{messages};
254 my $ods_filepath = $params->{filepath};
259 foreach my $message ( @$messages ) {
260 my @message_lines = split /\n/, $message->{content};
261 chomp for @message_lines;
262 # Get headers from first message
264 shift @message_lines;
268 foreach my $message_line ( @message_lines ) {
270 my @message_cells = split $delimiter, $message_line;
271 foreach ( @message_cells ) {
272 push @content_row, Encode::encode( 'UTF8', $_ );
274 push @$ods_content, \@content_row;
279 generate_ods($ods_filepath, $ods_content);
284 my $directory = $params->{directory};
285 my $files = $params->{files};
286 my $to = $params->{to};
287 my $from = $params->{from};
288 my $transport = $params->{transport};
290 return unless $to and $from and $transport;
292 my $email = Koha::Email->create(
296 subject => 'Print notices for ' . $today_syspref,
300 while ( my ( $type, $filenames ) = each %$files ) {
301 for my $filename ( @$filenames ) {
302 my $mimetype = $type eq 'html'
307 ? 'application/vnd.oasis.opendocument.spreadsheet'
310 next unless $mimetype;
312 my $filepath = File::Spec->catfile( $directory, $filename );
314 next unless $filepath or -f $filepath;
318 content_type => $mimetype,
321 disposition => 'attachment',
326 $email->send_or_die( { transport => $transport } );
332 gather_print_notices - Print waiting print notices
336 gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-e|--email=your_email@example.org] [-h|--help]
338 Will print all waiting print notices to the output_directory.
340 The generated filename will be notices-TODAY.[csv|html|ods] or notices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given.
346 =item B<output_directory>
348 Define the output directory where the files will be generated.
350 =item B<--send|--nosend>
352 After files have been generated, messages status is changed from 'pending' to
353 'sent'. This is the default action, without this parameter or with --send.
354 Using --nosend, the message status is not changed.
358 Split messages into separate files by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods]
362 Generate the print notices in a html file (default is --html, if --csv and --ods are not given).
366 Generate the print notices in a csv file.
367 If you use this parameter, the template should contain 2 lines.
368 The first one the csv headers and the second one the value list.
371 cardnumber:patron:email:item
372 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
374 You have to combine this option with one (and only one) letter_code.
378 Generate the print notices in a ods file.
380 This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file.
382 =item B<--letter_code>
384 Filter print messages by letter_code.
385 Several letter_code parameters can be given.
390 E-mail address to send generated files to.
394 Print a brief help message
400 Jesse Weaver <pianohacker@gmail.com>
402 Jonathan Druart <jonathan.druart@biblibre.com>
406 Copyright 2009 Jesse Weaver
408 Copyright 2014 BibLibre
411 This file is part of Koha.
413 Koha is free software; you can redistribute it and/or modify it
414 under the terms of the GNU General Public License as published by
415 the Free Software Foundation; either version 3 of the License, or
416 (at your option) any later version.
418 Koha is distributed in the hope that it will be useful, but
419 WITHOUT ANY WARRANTY; without even the implied warranty of
420 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
421 GNU General Public License for more details.
423 You should have received a copy of the GNU General Public License
424 along with Koha; if not, see <http://www.gnu.org/licenses>.