6 # find Koha's Perl modules
7 # test carefully before changing this
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
22 use File::Basename qw( dirname );
46 'd|delimiter:s' => \$delimiter,
47 'letter_code:s' => \@letter_codes,
49 'e|email:s' => \@emails,
52 pod2usage(0) if $help;
54 my $output_directory = $ARGV[0];
56 if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
59 -msg => qq{\nError: You must specify a valid and writeable directory to dump the print notices in.\n},
63 # Default value is html
64 $html = 1 if not $html and not $csv and not $ods;
66 if ( $csv and @letter_codes != 1 ) {
69 -msg => qq{\nIt is not consistent to use --csv without one (and only one) letter_code\n},
73 if ( $ods and @letter_codes != 1 ) {
76 -msg => qq{\nIt is not consistent to use --ods without one (and only one) letter_code\n},
84 my $today_iso = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } ) ;
85 my $today_syspref = output_pref( { dt => dt_from_string, dateonly => 1 } );
87 my @all_messages = @{ GetPrintMessages() };
89 # Filter by letter_code
91 my $letter_code = $_->{letter_code};
93 grep { /^$letter_code$/ } @letter_codes
95 } @all_messages if @letter_codes;
96 exit unless @all_messages;
98 my ( $html_filenames, $csv_filenames, $ods_filenames );
99 $csv_filenames = print_notices({
100 messages => \@all_messages,
102 output_directory => $output_directory,
106 $ods_filenames = print_notices({
107 messages => \@all_messages,
109 output_directory => $output_directory,
114 ## carriage return replaced by <br/> as output is html
115 foreach my $message (@all_messages) {
116 local $_ = $message->{'content'};
119 $message->{'content'} = $_;
122 $html_filenames = print_notices({
123 messages => \@all_messages,
125 output_directory => $output_directory,
132 html => $html_filenames,
133 csv => $csv_filenames,
134 ods => $ods_filenames,
136 for my $email ( @emails ) {
138 directory => $output_directory,
141 from => C4::Context->preference('KohaAdminEmailAddress'), # Should be replaced if bug 8000 is pushed
149 my $messages = $params->{messages};
150 my $split = $params->{split};
151 my $output_directory = $params->{output_directory};
152 my $format = $params->{format} // 'html';
154 die "Format $format is not known"
155 unless $format =~ m[^html$|^csv$|^ods$];
157 my ( @filenames, $messages_by_branch );
160 foreach my $message (@$messages) {
161 push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
164 $messages_by_branch->{all_branches} = $messages;
167 while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
168 my $letter_codes = @letter_codes == 0 ? 'all' : join '_', @letter_codes;
169 my $filename = $split
170 ? "notices_$letter_codes-" . $today_iso . "-$branchcode.$format"
171 : "notices_$letter_codes-" . $today_iso . ".$format";
172 my $filepath = File::Spec->catdir( $output_directory, $filename );
173 if ( $format eq 'html' ) {
175 messages => $branch_messages,
176 filepath => $filepath,
178 } elsif ( $format eq 'csv' ) {
180 messages => $branch_messages,
181 filepath => $filepath,
183 } elsif ( $format eq 'ods' ) {
185 messages => $branch_messages,
186 filepath => $filepath,
191 foreach my $message ( @$branch_messages ) {
192 C4::Letters::_set_message_status(
194 message_id => $message->{'message_id'},
200 push @filenames, $filename;
207 my $messages = $params->{messages};
208 my $filepath = $params->{filepath};
211 C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
215 stylesheet => C4::Context->preference("NoticeCSS"),
216 today => $today_syspref,
217 messages => $messages,
220 open my $OUTPUT, '>encoding(utf-8)', $filepath
221 or die "Could not open $filepath: $!";
222 print $OUTPUT $template->output;
228 my $messages = $params->{messages};
229 my $filepath = $params->{filepath};
231 open my $OUTPUT, '>encoding(utf-8)', $filepath
232 or die "Could not open $filepath: $!";
233 my ( @csv_lines, $headers );
234 foreach my $message ( @$messages ) {
235 my @lines = split /\n/, $message->{content};
238 # We don't have headers, get them
239 unless ( $headers ) {
240 $headers = $lines[0];
241 say $OUTPUT $headers;
245 for my $line ( @lines ) {
246 next if $line =~ /^\s$/;
254 my $messages = $params->{messages};
255 my $filepath = $params->{filepath};
258 use OpenOffice::OODoc;
259 my $tmpdir = dirname $filepath;
260 odfWorkingDirectory( $tmpdir );
261 my $doc = odfDocument( file => $filepath, create => 'spreadsheet' );
267 foreach my $message ( @$messages ) {
268 my @lines = split /\n/, $message->{content};
271 # We don't have headers, get them
272 unless ( @headers ) {
273 @headers = split $delimiter, $lines[0];
274 my $nb_cols = scalar @headers;
275 my $nb_rows = scalar @$messages;
276 my $sheet = $doc->expandTable( 0, $nb_rows + 1, $nb_cols );
277 @rows = $doc->getTableRows($sheet);
282 for my $header ( @headers ) {
283 $doc->cellValue( $row, $j, Encode::encode( 'UTF8', $header ) );
290 shift @lines; # remove headers
291 for my $line ( @lines ) {
292 my @row_data = split $delimiter, $line;
294 # Note scalar(@$row_data) should be equal to $nb_cols
295 for ( my $j = 0 ; $j < scalar(@row_data) ; $j++ ) {
296 my $value = Encode::encode( 'UTF8', $row_data[$j] );
297 $doc->cellValue( $row, $j, $value );
307 my $directory = $params->{directory};
308 my $files = $params->{files};
309 my $to = $params->{to};
310 my $from = $params->{from};
311 return unless $to and $from;
313 my $mail = MIME::Lite->new(
316 Subject => 'Print notices for ' . $today_syspref,
317 Type => 'multipart/mixed',
320 while ( my ( $type, $filenames ) = each %$files ) {
321 for my $filename ( @$filenames ) {
322 my $mimetype = $type eq 'html'
327 ? 'application/vnd.oasis.opendocument.spreadsheet'
330 next unless $mimetype;
332 my $filepath = File::Spec->catdir( $directory, $filename );
334 next unless $filepath or -f $filepath;
339 Filename => $filename,
340 Encoding => 'base64',
350 gather_print_notices - Print waiting print notices
354 gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-e|--email=your_email@example.org] [-h|--help]
356 Will print all waiting print notices to the output_directory.
358 The generated filename will be notices-TODAY.[csv|html|ods] or notices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given.
364 =item B<output_directory>
366 Define the output directory where the files will be generated.
368 =item B<--send|--nosend>
370 After files have been generated, messages status is changed from 'pending' to
371 'sent'. This is the default action, without this parameter or with --send.
372 Using --nosend, the message status is not changed.
376 Split messages into separate files by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods]
380 Generate the print notices in a html file (default is --html, if --csv and --ods are not given).
384 Generate the print notices in a csv file.
385 If you use this parameter, the template should contain 2 lines.
386 The first one the csv headers and the second one the value list.
389 cardnumber:patron:email:item
390 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
392 You have to combine this option with one (and only one) letter_code.
396 Generate the print notices in a ods file.
398 This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file.
400 =item B<--letter_code>
402 Filter print messages by letter_code.
403 Several letter_code parameters can be given.
408 E-mail address to send generated files to.
412 Print a brief help message
418 Jesse Weaver <pianohacker@gmail.com>
420 Jonathan Druart <jonathan.druart@biblibre.com>
424 Copyright 2009 Jesse Weaver
426 Copyright 2014 BibLibre
429 This file is part of Koha.
431 Koha is free software; you can redistribute it and/or modify it
432 under the terms of the GNU General Public License as published by
433 the Free Software Foundation; either version 3 of the License, or
434 (at your option) any later version.
436 Koha is distributed in the hope that it will be useful, but
437 WITHOUT ANY WARRANTY; without even the implied warranty of
438 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
439 GNU General Public License for more details.
441 You should have received a copy of the GNU General Public License
442 along with Koha; if not, see <http://www.gnu.org/licenses>.