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 );
26 my ( $stylesheet, $help, $split, $html, $csv, $ods, $delimiter, @letter_codes );
34 'd|delimiter:s' => \$delimiter,
35 'letter_code:s' => \@letter_codes,
38 pod2usage(0) if $help;
40 my $output_directory = $ARGV[0];
42 if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
45 -msg => qq{\nError: You must specify a valid and writeable directory to dump the print notices in.\n},
49 # Default value is html
50 $html = 1 if not $html and not $csv and not $ods;
52 if ( $csv and @letter_codes != 1 ) {
55 -msg => qq{\nIt is not consistent to use --csv without one (and only one) letter_code\n},
59 if ( $ods and @letter_codes != 1 ) {
62 -msg => qq{\nIt is not consistent to use --ods without one (and only one) letter_code\n},
70 my $today = C4::Dates->new();
71 my @all_messages = @{ GetPrintMessages() };
73 # Filter by letter_code
75 my $letter_code = $_->{letter_code};
77 grep { /^$letter_code$/ } @letter_codes
80 exit unless @all_messages;
82 my ( $html_filenames, $csv_filenames, $ods_filenames );
83 $csv_filenames = print_notices({
84 messages => \@all_messages,
86 output_directory => $output_directory,
90 $ods_filenames = print_notices({
91 messages => \@all_messages,
93 output_directory => $output_directory,
98 ## carriage return replaced by <br/> as output is html
99 foreach my $message (@all_messages) {
100 local $_ = $message->{'content'};
103 $message->{'content'} = $_;
106 $html_filenames = print_notices({
107 messages => \@all_messages,
109 output_directory => $output_directory,
117 my $messages = $params->{messages};
118 my $split = $params->{split};
119 my $output_directory = $params->{output_directory};
120 my $format = $params->{format} // 'html';
122 die "Format $format is not known"
123 unless $format =~ m[^html$|^csv$|^ods$];
125 my ( @filenames, $messages_by_branch );
128 foreach my $message (@$messages) {
129 push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
132 $messages_by_branch->{all_branches} = $messages;
135 while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
136 my $letter_code = @letter_codes == 1 ? $letter_codes[0] : 'hold';
137 my $filename = $split
138 ? "notices_$letter_code-" . $today->output('iso') . "-$branchcode.$format"
139 : "notices_$letter_code-" . $today->output('iso') . ".$format";
140 my $filepath = File::Spec->catdir( $output_directory, $filename );
141 if ( $format eq 'html' ) {
143 messages => $branch_messages,
144 filepath => $filepath,
146 } elsif ( $format eq 'csv' ) {
148 messages => $branch_messages,
149 filepath => $filepath,
151 } elsif ( $format eq 'ods' ) {
153 messages => $branch_messages,
154 filepath => $filepath,
158 foreach my $message ( @$branch_messages ) {
159 C4::Letters::_set_message_status(
161 message_id => $message->{'message_id'},
166 push @filenames, $filename;
173 my $messages = $params->{messages};
174 my $filepath = $params->{filepath};
177 C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
181 stylesheet => C4::Context->preference("NoticeCSS"),
182 today => $today->output(),
183 messages => $messages,
186 open my $OUTPUT, '>encoding(utf-8)', $filepath
187 or die "Could not open $filepath: $!";
188 print $OUTPUT $template->output;
194 my $messages = $params->{messages};
195 my $filepath = $params->{filepath};
197 open my $OUTPUT, '>encoding(utf-8)', $filepath
198 or die "Could not open $filepath: $!";
199 my ( @csv_lines, $headers );
200 foreach my $message ( @$messages ) {
201 my @lines = split /\n/, $message->{content};
204 # We don't have headers, get them
205 unless ( $headers ) {
206 $headers = $lines[0];
207 say $OUTPUT $headers;
211 for my $line ( @lines ) {
212 next if $line =~ /^\s$/;
220 my $messages = $params->{messages};
221 my $filepath = $params->{filepath};
223 use OpenOffice::OODoc;
224 my $tmpdir = dirname $filepath;
225 odfWorkingDirectory( $tmpdir );
226 my $container = odfContainer( $filepath, create => 'spreadsheet' );
227 my $doc = odfDocument (
228 container => $container,
231 my $table = $doc->getTable(0);
234 my ( $nb_rows, $nb_cols ) = ( scalar(@$messages), 0 );
235 foreach my $message ( @$messages ) {
236 my @lines = split /\n/, $message->{content};
239 # We don't have headers, get them
240 unless ( @headers ) {
241 @headers = split $delimiter, $lines[0];
244 $doc->expandTable( $table, $nb_rows + 1, $nb_cols );
245 my $row = $doc->getRow( $table, 0 );
247 for my $header ( @headers ) {
248 $doc->cellValue( $row, $j, Encode::encode( 'UTF8', $header ) );
253 shift @lines; # remove headers
255 for my $line ( @lines ) {
256 my @row_data = split $delimiter, $line;
257 my $row = $doc->getRow( $table, $i );
258 # Note scalar(@$row_data) should be equal to $nb_cols
259 for ( my $j = 0 ; $j < scalar(@row_data) ; $j++ ) {
260 my $value = Encode::encode( 'UTF8', $row_data[$j] );
261 $doc->cellValue( $row, $j, $value );
271 gather_print_notices - Print waiting print notices
275 gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-h|--help]
277 Will print all waiting print notices to the output_directory.
279 The generated filename will be holdnotices-TODAY.[csv|html|ods] or holdnotices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given.
285 =item B<output_directory>
287 Define the output directory where the files will be generated.
291 Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods]
295 Generate the print notices in a html file (default if --html, --csv and ods are not given).
299 Generate the print notices in a csv file.
300 If you use this parameter, the template should contain 2 lines.
301 The first one the csv headers and the second one the value list.
304 cardnumber:patron:email:item
305 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
307 You have to combine this option without one (and only one) letter_code.
311 Generate the print notices in a ods file.
313 This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file.
315 =item B<--letter_code>
317 Filter print messages by letter_code.
318 Several letter_code parameters can be given.
322 Print a brief help message
328 Jesse Weaver <pianohacker@gmail.com>
330 Jonathan Druart <jonathan.druart@biblibre.com>
334 Copyright 2009 Jesse Weaver
336 Copyright 2014 BibLibre
339 This file is part of Koha.
341 Koha is free software; you can redistribute it and/or modify it
342 under the terms of the GNU General Public License as published by
343 the Free Software Foundation; either version 3 of the License, or
344 (at your option) any later version.
346 Koha is distributed in the hope that it will be useful, but
347 WITHOUT ANY WARRANTY; without even the implied warranty of
348 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
349 GNU General Public License for more details.
351 You should have received a copy of the GNU General Public License
352 along with Koha; if not, see <http://www.gnu.org/licenses>.