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 ## carriage return replaced by <br/> as output is html
110 foreach my $message (@all_messages) {
111 local $_ = $message->{'content'};
114 $message->{'content'} = $_;
117 $html_filenames = print_notices({
118 messages => \@all_messages,
120 output_directory => $output_directory,
127 html => $html_filenames,
128 csv => $csv_filenames,
129 ods => $ods_filenames,
132 my $transport = Koha::SMTP::Servers->get_default->transport;
134 for my $email ( @emails ) {
137 directory => $output_directory,
139 from => C4::Context->preference('KohaAdminEmailAddress'), # Should be replaced if bug 8000 is pushed
141 transport => $transport,
147 cronlogaction({ action => 'End', info => "COMPLETED" });
152 my $messages = $params->{messages};
153 my $split = $params->{split};
154 my $output_directory = $params->{output_directory};
155 my $format = $params->{format} // 'html';
157 die "Format $format is not known"
158 unless $format =~ m[^html$|^csv$|^ods$];
160 my ( @filenames, $messages_by_branch );
163 foreach my $message (@$messages) {
164 push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
167 $messages_by_branch->{all_branches} = $messages;
170 while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
171 my $letter_codes = @letter_codes == 0 ? 'all' : join '_', @letter_codes;
172 my $filename = $split
173 ? "notices_$letter_codes-" . $today_iso . "-$branchcode.$format"
174 : "notices_$letter_codes-" . $today_iso . ".$format";
175 my $filepath = File::Spec->catdir( $output_directory, $filename );
176 if ( $format eq 'html' ) {
178 messages => $branch_messages,
179 filepath => $filepath,
181 } elsif ( $format eq 'csv' ) {
183 messages => $branch_messages,
184 filepath => $filepath,
186 } elsif ( $format eq 'ods' ) {
188 messages => $branch_messages,
189 filepath => $filepath,
194 foreach my $message ( @$branch_messages ) {
195 C4::Letters::_set_message_status(
197 message_id => $message->{'message_id'},
203 push @filenames, $filename;
210 my $messages = $params->{messages};
211 my $filepath = $params->{filepath};
214 C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
218 stylesheet => C4::Context->preference("NoticeCSS"),
219 today => $today_syspref,
220 messages => $messages,
223 open my $OUTPUT, '>encoding(utf-8)', $filepath
224 or die "Could not open $filepath: $!";
225 print $OUTPUT $template->output;
231 my $messages = $params->{messages};
232 my $filepath = $params->{filepath};
234 open my $OUTPUT, '>encoding(utf-8)', $filepath
235 or die "Could not open $filepath: $!";
237 foreach my $message ( @$messages ) {
238 my @lines = split /\n/, $message->{content};
241 # We don't have headers, get them
242 unless ( $headers ) {
243 $headers = $lines[0];
244 say $OUTPUT $headers;
248 for my $line ( @lines ) {
249 next if $line =~ /^\s$/;
257 my $messages = $params->{messages};
258 my $ods_filepath = $params->{filepath};
263 foreach my $message ( @$messages ) {
264 my @message_lines = split /\n/, $message->{content};
265 chomp for @message_lines;
266 # Get headers from first message
268 shift @message_lines;
272 foreach my $message_line ( @message_lines ) {
274 my @message_cells = split $delimiter, $message_line;
275 foreach ( @message_cells ) {
276 push @content_row, Encode::encode( 'UTF8', $_ );
278 push @$ods_content, \@content_row;
283 generate_ods($ods_filepath, $ods_content);
288 my $directory = $params->{directory};
289 my $files = $params->{files};
290 my $to = $params->{to};
291 my $from = $params->{from};
292 my $transport = $params->{transport};
294 return unless $to and $from and $transport;
296 my $email = Koha::Email->create(
300 subject => 'Print notices for ' . $today_syspref,
304 while ( my ( $type, $filenames ) = each %$files ) {
305 for my $filename ( @$filenames ) {
306 my $mimetype = $type eq 'html'
311 ? 'application/vnd.oasis.opendocument.spreadsheet'
314 next unless $mimetype;
316 my $filepath = File::Spec->catfile( $directory, $filename );
318 next unless $filepath or -f $filepath;
322 content_type => $mimetype,
325 disposition => 'attachment',
330 $email->send_or_die( { transport => $transport } );
336 gather_print_notices - Print waiting print notices
340 gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-e|--email=your_email@example.org] [-h|--help]
342 Will print all waiting print notices to the output_directory.
344 The generated filename will be notices-TODAY.[csv|html|ods] or notices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given.
350 =item B<output_directory>
352 Define the output directory where the files will be generated.
354 =item B<--send|--nosend>
356 After files have been generated, messages status is changed from 'pending' to
357 'sent'. This is the default action, without this parameter or with --send.
358 Using --nosend, the message status is not changed.
362 Split messages into separate files by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods]
366 Generate the print notices in a html file (default is --html, if --csv and --ods are not given).
370 Generate the print notices in a csv file.
371 If you use this parameter, the template should contain 2 lines.
372 The first one the csv headers and the second one the value list.
375 cardnumber:patron:email:item
376 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
378 You have to combine this option with one (and only one) letter_code.
382 Generate the print notices in a ods file.
384 This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file.
386 =item B<--letter_code>
388 Filter print messages by letter_code.
389 Several letter_code parameters can be given.
394 E-mail address to send generated files to.
398 Print a brief help message
404 Jesse Weaver <pianohacker@gmail.com>
406 Jonathan Druart <jonathan.druart@biblibre.com>
410 Copyright 2009 Jesse Weaver
412 Copyright 2014 BibLibre
415 This file is part of Koha.
417 Koha is free software; you can redistribute it and/or modify it
418 under the terms of the GNU General Public License as published by
419 the Free Software Foundation; either version 3 of the License, or
420 (at your option) any later version.
422 Koha is distributed in the hope that it will be useful, but
423 WITHOUT ANY WARRANTY; without even the implied warranty of
424 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
425 GNU General Public License for more details.
427 You should have received a copy of the GNU General Public License
428 along with Koha; if not, see <http://www.gnu.org/licenses>.