6 # find Koha's Perl modules
7 # test carefully before changing this
9 eval { require "$FindBin::Bin/../kohalib.pl" };
13 CGI; # NOT a CGI script, this is just to keep C4::Templates::gettemplate happy
24 use File::Basename qw( dirname );
27 my ( $stylesheet, $help, $split, $html, $csv, $ods, $delimiter, @letter_codes );
35 'd|delimiter:s' => \$delimiter,
36 'letter_code:s' => \@letter_codes,
39 pod2usage(0) if $help;
41 my $output_directory = $ARGV[0];
43 if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
46 -msg => qq{\nError: You must specify a valid and writeable directory to dump the print notices in.\n},
50 # Default value is html
51 $html = 1 if not $html and not $csv and not $ods;
53 if ( $csv and @letter_codes != 1 ) {
56 -msg => qq{\nIt is not consistent to use --csv without one (and only one) letter_code\n},
60 if ( $ods and @letter_codes != 1 ) {
63 -msg => qq{\nIt is not consistent to use --ods without one (and only one) letter_code\n},
71 my $today = C4::Dates->new();
72 my @all_messages = @{ GetPrintMessages() };
74 # Filter by letter_code
76 my $letter_code = $_->{letter_code};
78 grep { /^$letter_code$/ } @letter_codes
81 exit unless @all_messages;
83 my ( $html_filenames, $csv_filenames, $ods_filenames );
84 $csv_filenames = print_notices({
85 messages => \@all_messages,
87 output_directory => $output_directory,
91 $ods_filenames = print_notices({
92 messages => \@all_messages,
94 output_directory => $output_directory,
99 ## carriage return replaced by <br/> as output is html
100 foreach my $message (@all_messages) {
101 local $_ = $message->{'content'};
104 $message->{'content'} = $_;
107 $html_filenames = print_notices({
108 messages => \@all_messages,
110 output_directory => $output_directory,
118 my $messages = $params->{messages};
119 my $split = $params->{split};
120 my $output_directory = $params->{output_directory};
121 my $format = $params->{format} // 'html';
123 die "Format $format is not known"
124 unless $format =~ m[^html$|^csv$|^ods$];
126 my ( @filenames, $messages_by_branch );
129 foreach my $message (@$messages) {
130 push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
133 $messages_by_branch->{all_branches} = $messages;
136 while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
137 my $letter_code = @letter_codes == 1 ? $letter_codes[0] : 'hold';
138 my $filename = $split
139 ? "notices_$letter_code-" . $today->output('iso') . "-$branchcode.$format"
140 : "notices_$letter_code-" . $today->output('iso') . ".$format";
141 my $filepath = File::Spec->catdir( $output_directory, $filename );
142 if ( $format eq 'html' ) {
144 messages => $branch_messages,
145 filepath => $filepath,
147 } elsif ( $format eq 'csv' ) {
149 messages => $branch_messages,
150 filepath => $filepath,
152 } elsif ( $format eq 'ods' ) {
154 messages => $branch_messages,
155 filepath => $filepath,
159 foreach my $message ( @$branch_messages ) {
160 C4::Letters::_set_message_status(
162 message_id => $message->{'message_id'},
167 push @filenames, $filename;
174 my $messages = $params->{messages};
175 my $filepath = $params->{filepath};
178 C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
182 stylesheet => C4::Context->preference("NoticeCSS"),
183 today => $today->output(),
184 messages => $messages,
187 open my $OUTPUT, '>', $filepath
188 or die "Could not open $filepath: $!";
189 print $OUTPUT $template->output;
195 my $messages = $params->{messages};
196 my $filepath = $params->{filepath};
198 open my $OUTPUT, '>', $filepath
199 or die "Could not open $filepath: $!";
200 my ( @csv_lines, $headers );
201 foreach my $message ( @$messages ) {
202 my @lines = split /\n/, $message->{content};
205 # We don't have headers, get them
206 unless ( $headers ) {
207 $headers = $lines[0];
208 say $OUTPUT Encode::encode( 'UTF8', $headers );
212 for my $line ( @lines ) {
213 next if $line =~ /^\s$/;
214 say $OUTPUT Encode::encode( 'UTF8', $line );
221 my $messages = $params->{messages};
222 my $filepath = $params->{filepath};
224 use OpenOffice::OODoc;
225 my $tmpdir = dirname $filepath;
226 odfWorkingDirectory( $tmpdir );
227 my $container = odfContainer( $filepath, create => 'spreadsheet' );
228 my $doc = odfDocument (
229 container => $container,
232 my $table = $doc->getTable(0);
235 my ( $nb_rows, $nb_cols ) = ( scalar(@$messages), 0 );
236 foreach my $message ( @$messages ) {
237 my @lines = split /\n/, $message->{content};
240 # We don't have headers, get them
241 unless ( @headers ) {
242 @headers = split $delimiter, $lines[0];
245 $doc->expandTable( $table, $nb_rows + 1, $nb_cols );
246 my $row = $doc->getRow( $table, 0 );
248 for my $header ( @headers ) {
249 $doc->cellValue( $row, $j, Encode::encode( 'UTF8', $header ) );
254 shift @lines; # remove headers
256 for my $line ( @lines ) {
257 my $row_data = split $delimiter, $line;
258 my $row = $doc->getRow( $table, $i );
259 # Note scalar(@$row_data) should be equal to $nb_cols
260 for ( my $j = 0 ; $j < scalar(@$row_data) ; $j++ ) {
261 my $value = Encode::encode( 'UTF8', $row_data->[$j] );
262 $doc->cellValue( $row, $j, $value );
272 gather_print_notices - Print waiting print notices
276 gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-h|--help]
278 Will print all waiting print notices to the output_directory.
280 The generated filename will be holdnotices-TODAY.[csv|html|ods] or holdnotices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given.
286 =item B<output_directory>
288 Define the output directory where the files will be generated.
292 Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods]
296 Generate the print notices in a html file (default if --html, --csv and ods are not given).
300 Generate the print notices in a csv file.
301 If you use this parameter, the template should contain 2 lines.
302 The first one the csv headers and the second one the value list.
305 cardnumber:patron:email:item
306 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
308 You have to combine this option without one (and only one) letter_code.
312 Generate the print notices in a ods file.
314 This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file.
316 =item B<--letter_code>
318 Filter print messages by letter_code.
319 Several letter_code parameters can be given.
323 Print a brief help message
329 Jesse Weaver <pianohacker@gmail.com>
331 Jonathan Druart <jonathan.druart@biblibre.com>
335 Copyright 2009 Jesse Weaver
337 Copyright 2014 BibLibre
340 This file is part of Koha.
342 Koha is free software; you can redistribute it and/or modify it
343 under the terms of the GNU General Public License as published by
344 the Free Software Foundation; either version 3 of the License, or
345 (at your option) any later version.
347 Koha is distributed in the hope that it will be useful, but
348 WITHOUT ANY WARRANTY; without even the implied warranty of
349 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
350 GNU General Public License for more details.
352 You should have received a copy of the GNU General Public License
353 along with Koha; if not, see <http://www.gnu.org/licenses>.