From d5ce5d831b14ff3758c88870a3a5c6c167fa3768 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 22 Jan 2014 17:11:38 +0100 Subject: [PATCH] Bug 11603: Gather print notices - add a ods parameter This patch adds: - the ability to generate an ods file From now you are able to generate a ods file for print notices. You would like to generate a csv file and not a html file. Test plan: - same as previous patch but test the following parameters: perl misc/cronjobs/gather_print_notices.pl /tmp/test --ods --letter_code=OVERDUE -d=: you should get an error because csv2ods is not installed. Follow the installation instructions and try again the command. A ods file should be generated in your /tmp/test directory. --- misc/cronjobs/gather_print_notices.pl | 151 +++++++++++++++++++++++++++------ 1 file changed, 127 insertions(+), 24 deletions(-) diff --git a/misc/cronjobs/gather_print_notices.pl b/misc/cronjobs/gather_print_notices.pl index 6591dd2..032b0f6 100755 --- a/misc/cronjobs/gather_print_notices.pl +++ b/misc/cronjobs/gather_print_notices.pl @@ -19,15 +19,18 @@ use C4::Templates; use File::Spec; use Pod::Usage; use Getopt::Long; +use File::Temp qw( tempdir ); use Koha::DateUtils; -my ( $stylesheet, $help, $split, $html, $csv, @letter_codes ); +my ( $stylesheet, $help, $split, $html, $csv, $ods, $delimiter, @letter_codes ); GetOptions( 'h|help' => \$help, 's|split' => \$split, 'html' => \$html, 'csv' => \$csv, + 'ods' => \$ods, + 'd|delimiter:s' => \$delimiter, 'letter_code:s' => \@letter_codes, ) || pod2usage(1); @@ -52,6 +55,32 @@ if ( $csv and @letter_codes != 1 ) { }); } +if ( $ods and @letter_codes != 1 ) { + pod2usage({ + -exitval => 1, + -msg => qq{\nIt is not consistent to use --ods without one (and only one) letter_code\n}, + }); +} + +my $csv2ods_cmd; +if ( $ods ) { + $csv2ods_cmd = qx|which csv2ods|; + chomp $csv2ods_cmd; + if ( not $csv2ods_cmd or not -f $csv2ods_cmd ) { + my $installation_instructions = qq| +Please have a look at https://code.google.com/p/scone-fu. + +Installation instructions: +git clone https://code.google.com/p/scone-fu/ +cd scone-fu/code/python/odfpy-0.9.3/ +sudo python setup.py install\n|; + say qq{\ncsv2ods seems not to be installed.\n$installation_instructions\n}; + exit 1; + } + + $delimiter ||= q|,|; +} + my $today = C4::Dates->new(); my @all_messages = @{ GetPrintMessages() }; @@ -72,17 +101,32 @@ foreach my $message (@all_messages) { $message->{'content'} = $_; } -print_notices_html({ messages => \@all_messages, split => $split }) - if $html; +print_notices_html({ + messages => \@all_messages, + split => $split, + output_directory => $output_directory, +}) if $html; + +print_notices_csv({ + messages => \@all_messages, + split => $split, + output_directory => $output_directory, +}) if $csv; -print_notices_csv({ messages => \@all_messages, split => $split }) - if $csv; +print_notices_ods({ + messages => \@all_messages, + split => $split, + output_directory => $output_directory, +}) if $ods; sub print_notices_html { my ( $params ) = @_; my $messages = $params->{messages}; my $split = $params->{split}; + my $output_directory = $params->{output_directory}; + my $set_status = $params->{set_status} // 1; + my @filenames; my $messages_by_branch; if ( $split ) { @@ -114,16 +158,19 @@ sub print_notices_html { print $OUTPUT $template->output; close $OUTPUT; - foreach my $message ( @$branch_messages ) { - C4::Letters::_set_message_status( - { - message_id => $message->{'message_id'}, - status => 'sent' - } - ); - $message->{status} = 'sent'; + if ( $set_status ) { + foreach my $message ( @$branch_messages ) { + C4::Letters::_set_message_status( + { + message_id => $message->{'message_id'}, + status => 'sent' + } + ); + } } + push @filenames, $filename; } + return \@filenames; } sub print_notices_csv { @@ -131,6 +178,9 @@ sub print_notices_csv { my $messages = $params->{messages}; my $split = $params->{split}; + my $output_directory = $params->{output_directory}; + my $set_status = $params->{set_status} // 1; + my @filenames; my $messages_by_branch; if ( $split ) { @@ -165,15 +215,62 @@ sub print_notices_csv { say $OUTPUT $line; } - C4::Letters::_set_message_status( - { - message_id => $message->{'message_id'}, - status => 'sent' - } - ) if $message->{status} ne 'sent'; + if ( $set_status ) { + C4::Letters::_set_message_status( + { + message_id => $message->{'message_id'}, + status => 'sent' + } + ); + } } close $OUTPUT; + push @filenames, $filename; + } + return \@filenames; +} + +sub print_notices_ods { + my ( $params ) = @_; + + my $messages = $params->{messages}; + my $split = $params->{split}; + my $output_directory = $params->{output_directory}; + my $set_status = $params->{set_status} // 1; + my @filenames; + + my $messages_by_branch; + if ( $split ) { + foreach my $message (@$messages) { + push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message ); + } + } else { + $messages_by_branch->{all_branches} = $messages; + } + + while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) { + my $filename = $split + ? 'holdnotices-' . $today->output('iso') . "-$branchcode.ods" + : 'holdnotices-' . $today->output('iso') . ".ods"; + + my $tmpdir = File::Temp->newdir(); + + my ( $csv_filename ) = @{ print_notices_csv({ + messages => $branch_messages, + output_directory => $tmpdir, + set_status => 0, + }) }; + + my $ods_output_filepath = File::Spec->catdir( $output_directory, $filename ); + my $csv_filepath = File::Spec->catdir( $tmpdir, $csv_filename ); + + qx| + $csv2ods_cmd -i $csv_filepath -o $ods_output_filepath -d $delimiter + |; + + push @filenames, $filename; } + return \@filenames; } =head1 NAME @@ -182,11 +279,11 @@ gather_print_notices - Print waiting print notices =head1 SYNOPSIS -gather_print_notices output_directory [-s|--split] [--html] [--csv] [--letter_code=LETTER_CODE] [-h|--help] +gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-h|--help] Will print all waiting print notices to the output_directory. -The generated filename will be holdnotices-TODAY.[csv|html] or holdnotices-TODAY-BRANCHCODE.[csv|html] if the --split parameter is given. +The generated filename will be holdnotices-TODAY.[csv|html|ods] or holdnotices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given. =head1 OPTIONS @@ -198,17 +295,17 @@ Define the output directory where the files will be generated. =item B<-s|--split> -Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html] +Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods] =item B<--html> -Generate the print notices in a html file (default if --html and --csv are not given). +Generate the print notices in a html file (default if --html, --csv and ods are not given). =item B<--csv> Generate the print notices in a csv file. If you use this parameter, the template should contain 2 lines. -The first one the the csv headers and the second one the value list. +The first one the csv headers and the second one the value list. For example: cardnumber:patron:email:item @@ -216,6 +313,12 @@ cardnumber:patron:email:item You have to combine this option without one (and only one) letter_code. +=item B<--ods> + +Generate the print notices in a ods file. + +This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file. + =item B<--letter_code> Filter print messages by letter_code. -- 1.7.10.4