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
23 use File::Basename qw( dirname );
45 'd|delimiter:s' => \$delimiter,
46 'letter_code:s' => \@letter_codes,
50 pod2usage(0) if $help;
52 my $output_directory = $ARGV[0];
54 if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
57 -msg => qq{\nError: You must specify a valid and writeable directory to dump the print notices in.\n},
61 # Default value is html
62 $html = 1 if not $html and not $csv and not $ods;
64 if ( $csv and @letter_codes != 1 ) {
67 -msg => qq{\nIt is not consistent to use --csv without one (and only one) letter_code\n},
71 if ( $ods and @letter_codes != 1 ) {
74 -msg => qq{\nIt is not consistent to use --ods without one (and only one) letter_code\n},
82 my $today = C4::Dates->new();
83 my @all_messages = @{ GetPrintMessages() };
85 # Filter by letter_code
87 my $letter_code = $_->{letter_code};
89 grep { /^$letter_code$/ } @letter_codes
91 } @all_messages if @letter_codes;
92 exit unless @all_messages;
94 my ( $html_filenames, $csv_filenames, $ods_filenames );
95 $csv_filenames = print_notices({
96 messages => \@all_messages,
98 output_directory => $output_directory,
102 $ods_filenames = print_notices({
103 messages => \@all_messages,
105 output_directory => $output_directory,
110 ## carriage return replaced by <br/> as output is html
111 foreach my $message (@all_messages) {
112 local $_ = $message->{'content'};
115 $message->{'content'} = $_;
118 $html_filenames = print_notices({
119 messages => \@all_messages,
121 output_directory => $output_directory,
129 my $messages = $params->{messages};
130 my $split = $params->{split};
131 my $output_directory = $params->{output_directory};
132 my $format = $params->{format} // 'html';
134 die "Format $format is not known"
135 unless $format =~ m[^html$|^csv$|^ods$];
137 my ( @filenames, $messages_by_branch );
140 foreach my $message (@$messages) {
141 push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
144 $messages_by_branch->{all_branches} = $messages;
147 while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
148 my $letter_codes = @letter_codes == 0 ? 'all' : join '_', @letter_codes;
149 my $filename = $split
150 ? "notices_$letter_codes-" . $today->output('iso') . "-$branchcode.$format"
151 : "notices_$letter_codes-" . $today->output('iso') . ".$format";
152 my $filepath = File::Spec->catdir( $output_directory, $filename );
153 if ( $format eq 'html' ) {
155 messages => $branch_messages,
156 filepath => $filepath,
158 } elsif ( $format eq 'csv' ) {
160 messages => $branch_messages,
161 filepath => $filepath,
163 } elsif ( $format eq 'ods' ) {
165 messages => $branch_messages,
166 filepath => $filepath,
171 foreach my $message ( @$branch_messages ) {
172 C4::Letters::_set_message_status(
174 message_id => $message->{'message_id'},
180 push @filenames, $filename;
187 my $messages = $params->{messages};
188 my $filepath = $params->{filepath};
191 C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
195 stylesheet => C4::Context->preference("NoticeCSS"),
196 today => $today->output(),
197 messages => $messages,
200 open my $OUTPUT, '>encoding(utf-8)', $filepath
201 or die "Could not open $filepath: $!";
202 print $OUTPUT $template->output;
208 my $messages = $params->{messages};
209 my $filepath = $params->{filepath};
211 open my $OUTPUT, '>encoding(utf-8)', $filepath
212 or die "Could not open $filepath: $!";
213 my ( @csv_lines, $headers );
214 foreach my $message ( @$messages ) {
215 my @lines = split /\n/, $message->{content};
218 # We don't have headers, get them
219 unless ( $headers ) {
220 $headers = $lines[0];
221 say $OUTPUT $headers;
225 for my $line ( @lines ) {
226 next if $line =~ /^\s$/;
234 my $messages = $params->{messages};
235 my $filepath = $params->{filepath};
237 use OpenOffice::OODoc;
238 my $tmpdir = dirname $filepath;
239 odfWorkingDirectory( $tmpdir );
240 my $container = odfContainer( $filepath, create => 'spreadsheet' );
241 my $doc = odfDocument (
242 container => $container,
245 my $table = $doc->getTable(0);
248 my ( $nb_rows, $nb_cols, $i ) = ( scalar(@$messages), 0, 0 );
249 foreach my $message ( @$messages ) {
250 my @lines = split /\n/, $message->{content};
253 # We don't have headers, get them
254 unless ( @headers ) {
255 @headers = split $delimiter, $lines[0];
258 $doc->expandTable( $table, $nb_rows + 1, $nb_cols );
259 my $row = $doc->getRow( $table, 0 );
261 for my $header ( @headers ) {
262 $doc->cellValue( $row, $j, Encode::encode( 'UTF8', $header ) );
268 shift @lines; # remove headers
269 for my $line ( @lines ) {
270 my @row_data = split $delimiter, $line;
271 my $row = $doc->getRow( $table, $i );
272 # Note scalar(@$row_data) should be equal to $nb_cols
273 for ( my $j = 0 ; $j < scalar(@row_data) ; $j++ ) {
274 my $value = Encode::encode( 'UTF8', $row_data[$j] );
275 $doc->cellValue( $row, $j, $value );
285 gather_print_notices - Print waiting print notices
289 gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-h|--help]
291 Will print all waiting print notices to the output_directory.
293 The generated filename will be notices-TODAY.[csv|html|ods] or notices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given.
299 =item B<output_directory>
301 Define the output directory where the files will be generated.
303 =item B<--send|--nosend>
305 After files have been generated, messages status is changed from 'pending' to
306 'sent'. This is the default action, without this parameter or with --send.
307 Using --nosend, the message status is not changed.
311 Split messages into separate files by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods]
315 Generate the print notices in a html file (default is --html, if --csv and --ods are not given).
319 Generate the print notices in a csv file.
320 If you use this parameter, the template should contain 2 lines.
321 The first one the csv headers and the second one the value list.
324 cardnumber:patron:email:item
325 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
327 You have to combine this option without one (and only one) letter_code.
331 Generate the print notices in a ods file.
333 This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file.
335 =item B<--letter_code>
337 Filter print messages by letter_code.
338 Several letter_code parameters can be given.
342 Print a brief help message
348 Jesse Weaver <pianohacker@gmail.com>
350 Jonathan Druart <jonathan.druart@biblibre.com>
354 Copyright 2009 Jesse Weaver
356 Copyright 2014 BibLibre
359 This file is part of Koha.
361 Koha is free software; you can redistribute it and/or modify it
362 under the terms of the GNU General Public License as published by
363 the Free Software Foundation; either version 3 of the License, or
364 (at your option) any later version.
366 Koha is distributed in the hope that it will be useful, but
367 WITHOUT ANY WARRANTY; without even the implied warranty of
368 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
369 GNU General Public License for more details.
371 You should have received a copy of the GNU General Public License
372 along with Koha; if not, see <http://www.gnu.org/licenses>.