ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / misc / cronjobs / gather_print_notices.pl
index a511326..ca99635 100755 (executable)
@@ -11,7 +11,6 @@ BEGIN {
 
 use CGI qw( utf8 ); # NOT a CGI script, this is just to keep C4::Templates::gettemplate happy
 use C4::Context;
-use C4::Dates;
 use C4::Debug;
 use C4::Letters;
 use C4::Templates;
@@ -20,8 +19,8 @@ use Pod::Usage;
 use Getopt::Long;
 use C4::Log;
 
-use File::Basename qw( dirname );
 use Koha::DateUtils;
+use Koha::Util::OpenDocument;
 use MIME::Lite;
 
 my (
@@ -82,7 +81,9 @@ $delimiter ||= q|,|;
 
 cronlogaction();
 
-my $today        = C4::Dates->new();
+my $today_iso     = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } ) ;
+my $today_syspref = output_pref( { dt => dt_from_string, dateonly => 1 } );
+
 my @all_messages = @{ GetPrintMessages() };
 
 # Filter by letter_code
@@ -166,8 +167,8 @@ sub print_notices {
     while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
         my $letter_codes = @letter_codes == 0 ? 'all' : join '_', @letter_codes;
         my $filename = $split
-            ? "notices_$letter_codes-" . $today->output('iso') . "-$branchcode.$format"
-            : "notices_$letter_codes-" . $today->output('iso') . ".$format";
+            ? "notices_$letter_codes-" . $today_iso . "-$branchcode.$format"
+            : "notices_$letter_codes-" . $today_iso . ".$format";
         my $filepath = File::Spec->catdir( $output_directory, $filename );
         if ( $format eq 'html' ) {
             generate_html({
@@ -180,7 +181,7 @@ sub print_notices {
                 filepath => $filepath,
             });
         } elsif ( $format eq 'ods' ) {
-            generate_ods ({
+            _generate_ods ({
                 messages => $branch_messages,
                 filepath => $filepath,
             });
@@ -212,7 +213,7 @@ sub generate_html {
 
     $template->param(
         stylesheet => C4::Context->preference("NoticeCSS"),
-        today      => $today->output(),
+        today      => $today_syspref,
         messages   => $messages,
     );
 
@@ -248,55 +249,35 @@ sub generate_csv {
     }
 }
 
-sub generate_ods {
+sub _generate_ods {
     my ( $params ) = @_;
     my $messages = $params->{messages};
-    my $filepath = $params->{filepath};
-
-    use OpenOffice::OODoc;
-    my $tmpdir = dirname $filepath;
-    odfWorkingDirectory( $tmpdir );
-    my $container = odfContainer( $filepath, create => 'spreadsheet' );
-    my $doc = odfDocument (
-        container => $container,
-        part      => 'content'
-    );
-    my $table = $doc->getTable(0);
+    my $ods_filepath = $params->{filepath};
 
-    my @headers;
-    my ( $nb_rows, $nb_cols, $i ) = ( scalar(@$messages), 0, 0 );
+    # Prepare sheet
+    my $ods_content;
+    my $has_headers;
     foreach my $message ( @$messages ) {
-        my @lines = split /\n/, $message->{content};
-        chomp for @lines;
-
-        # We don't have headers, get them
-        unless ( @headers ) {
-            @headers = split $delimiter, $lines[0];
-
-            $nb_cols = @headers;
-            $doc->expandTable( $table, $nb_rows + 1, $nb_cols );
-            my $row = $doc->getRow( $table, 0 );
-            my $j = 0;
-            for my $header ( @headers ) {
-                $doc->cellValue( $row, $j, Encode::encode( 'UTF8', $header ) );
-                $j++;
-            }
-            $i = 1;
+        my @message_lines = split /\n/, $message->{content};
+        chomp for @message_lines;
+        # Get headers from first message
+        if ($has_headers) {
+            shift @message_lines;
+        } else {
+            $has_headers = 1;
         }
-
-        shift @lines; # remove headers
-        for my $line ( @lines ) {
-            my @row_data = split $delimiter, $line;
-            my $row = $doc->getRow( $table, $i );
-            # Note scalar(@$row_data) should be equal to $nb_cols
-            for ( my $j = 0 ; $j < scalar(@row_data) ; $j++ ) {
-                my $value = Encode::encode( 'UTF8', $row_data[$j] );
-                $doc->cellValue( $row, $j, $value );
+        foreach my $message_line ( @message_lines ) {
+            my @content_row;
+            my @message_cells = split $delimiter, $message_line;
+            foreach ( @message_cells ) {
+                push @content_row, Encode::encode( 'UTF8', $_ );
             }
-            $i++;
+            push @$ods_content, \@content_row;
         }
     }
-    $doc->save();
+
+    # Process
+    generate_ods($ods_filepath, $ods_content);
 }
 
 sub send_files {
@@ -310,7 +291,7 @@ sub send_files {
     my $mail = MIME::Lite->new(
         From     => $from,
         To       => $to,
-        Subject  => 'Print notices for ' . $today->output(),
+        Subject  => 'Print notices for ' . $today_syspref,
         Type     => 'multipart/mixed',
     );
 
@@ -386,7 +367,7 @@ For example:
 cardnumber:patron:email:item
 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
 
-You have to combine this option without one (and only one) letter_code.
+You have to combine this option with one (and only one) letter_code.
 
 =item B<--ods>
 
@@ -401,7 +382,8 @@ Several letter_code parameters can be given.
 
 =item B<-e|--email>
 
-E-mail address to send generated files.
+Repeatable.
+E-mail address to send generated files to.
 
 =item B<-h|--help>