From bdcb66f32a8e277023c7fe94f2e240f5f418a93a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marc=20V=C3=A9ron?= <veron@veron.ch>
Date: Mon, 5 Oct 2015 18:20:57 +0200
Subject: [PATCH] Bug 14960 - Remove C4::Dates from files in misc/cronjobs

This patch removes C4::Dates from following files:

- misc/cronjobs/batch_anonymise.pl
- misc/cronjobs/cleanup_database.pl
- misc/cronjobs/gather_print_notices.pl
- misc/cronjobs/serialsUpdate.pl
- misc/cronjobs/thirdparty/TalkingTech_itiva_outbound.pl
-  misc/cronjobs/overdue_notices.pl
-  misc/cronjobs/staticfines.pl

To test:
- Carefully review code changes.
- Verify that the cronjobs behave as before.

Amended to make patch apply ( 04.11.2015 / mv   :-)  )
---
 misc/cronjobs/batch_anonymise.pl                   |    1 -
 misc/cronjobs/cleanup_database.pl                  |    1 -
 misc/cronjobs/gather_print_notices.pl              |    1 -
 misc/cronjobs/overdue_notices.pl                   |   81 ++++++++++++++++++--
 misc/cronjobs/serialsUpdate.pl                     |    4 +-
 misc/cronjobs/staticfines.pl                       |   15 ++--
 .../thirdparty/TalkingTech_itiva_outbound.pl       |    6 +-
 7 files changed, 88 insertions(+), 21 deletions(-)

diff --git a/misc/cronjobs/batch_anonymise.pl b/misc/cronjobs/batch_anonymise.pl
index 45868dc..c90842f 100755
--- a/misc/cronjobs/batch_anonymise.pl
+++ b/misc/cronjobs/batch_anonymise.pl
@@ -31,7 +31,6 @@ BEGIN {
 
 use C4::Context;
 use C4::Circulation;
-use C4::Dates;
 use Date::Calc qw(
   Today
   Add_Delta_Days
diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl
index e882b4b..7d1bfc1 100755
--- a/misc/cronjobs/cleanup_database.pl
+++ b/misc/cronjobs/cleanup_database.pl
@@ -35,7 +35,6 @@ BEGIN {
 }
 
 use C4::Context;
-use C4::Dates;
 use C4::Search;
 use C4::Search::History;
 use Getopt::Long;
diff --git a/misc/cronjobs/gather_print_notices.pl b/misc/cronjobs/gather_print_notices.pl
index 825aee2..0458406 100755
--- a/misc/cronjobs/gather_print_notices.pl
+++ b/misc/cronjobs/gather_print_notices.pl
@@ -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;
diff --git a/misc/cronjobs/overdue_notices.pl b/misc/cronjobs/overdue_notices.pl
index 2f9fa68..2f8dae6 100755
--- a/misc/cronjobs/overdue_notices.pl
+++ b/misc/cronjobs/overdue_notices.pl
@@ -38,7 +38,6 @@ use DateTime;
 use DateTime::Duration;
 
 use C4::Context;
-use C4::Dates qw/format_date/;
 use C4::Debug;
 use C4::Letters;
 use C4::Overdues qw(GetFine GetOverdueMessageTransportTypes);
@@ -527,7 +526,7 @@ END_SQL
             my $borrowernumber;
             while ( my $data = $sth->fetchrow_hashref ) {
 
-                next unless ( DateTime->compare( $date_to_run,  dt_from_string($data->{date_due})) ) == 1;
+                next unless ( DateTime->compare( $date_to_run, dt_from_string($data->{date_due})) ) == 1;
 
                 # check the borrower has at least one item that matches
                 my $days_between;
@@ -536,12 +535,12 @@ END_SQL
                     my $calendar =
                       Koha::Calendar->new( branchcode => $branchcode );
                     $days_between =
-                      $calendar->days_between(  dt_from_string($data->{date_due}),
+                      $calendar->days_between( dt_from_string($data->{date_due}),
                         $date_to_run );
                 }
                 else {
                     $days_between =
-                      $date_to_run->delta_days(  dt_from_string($data->{date_due}) );
+                      $date_to_run->delta_days( dt_from_string($data->{date_due}) );
                 }
                 $days_between = $days_between->in_units('days');
                 if ($triggered) {
@@ -657,7 +656,10 @@ END_SQL
                       last;
                     }
                     $j++;
-                    my @item_info = map { $_ =~ /^date|date$/ ? format_date( $item_info->{$_} ) : $item_info->{$_} || '' } @item_content_fields;
+                    my @item_info = map { $_ =~ /^date|date$/ ?
+                                           eval { output_pref( { dt => dt_from_string( $item_info->{$_} ), dateonly => 1 } ); }
+                                           :
+                                           $item_info->{$_} || '' } @item_content_fields;
                     $titles .= join("\t", @item_info) . "\n";
                     $itemcount++;
                     push @items, $item_info;
@@ -829,6 +831,75 @@ if ( defined $htmlfilename ) {
 
 These methods are internal to the operation of overdue_notices.pl.
 
+=head2 parse_letter
+
+parses the letter template, replacing the placeholders with data
+specific to this patron, biblio, or item
+
+named parameters:
+  letter - required hashref
+  borrowernumber - required integer
+  substitute - optional hashref of other key/value pairs that should
+    be substituted in the letter content
+
+returns the C<letter> hashref, with the content updated to reflect the
+substituted keys and values.
+
+
+=cut
+
+sub parse_letter {
+    my $params = shift;
+    foreach my $required (qw( letter_code borrowernumber )) {
+        return unless ( exists $params->{$required} && $params->{$required} );
+    }
+
+    my $substitute = $params->{'substitute'} || {};
+    $substitute->{today} ||= output_pref( { dt => dt_from_string , dateonly => 1 } );
+
+    my %tables = ( 'borrowers' => $params->{'borrowernumber'} );
+    if ( my $p = $params->{'branchcode'} ) {
+        $tables{'branches'} = $p;
+    }
+
+    my $currencies = GetCurrency();
+    my $currency_format;
+    $currency_format = $currencies->{currency} if defined($currencies);
+
+    my @item_tables;
+    if ( my $i = $params->{'items'} ) {
+        my $item_format = '';
+        foreach my $item (@$i) {
+            my $fine = GetFine($item->{'itemnumber'}, $params->{'borrowernumber'});
+            if ( !$item_format and defined $params->{'letter'}->{'content'} ) {
+                $params->{'letter'}->{'content'} =~ m/(<item>.*<\/item>)/;
+                $item_format = $1;
+            }
+
+            $item->{'fine'} = currency_format($currency_format, "$fine", FMT_SYMBOL);
+            # if active currency isn't correct ISO code fallback to sprintf
+            $item->{'fine'} = sprintf('%.2f', $fine) unless $item->{'fine'};
+
+            push @item_tables, {
+                'biblio' => $item->{'biblionumber'},
+                'biblioitems' => $item->{'biblionumber'},
+                'items' => $item,
+                'issues' => $item->{'itemnumber'},
+            };
+        }
+    }
+
+    return C4::Letters::GetPreparedLetter (
+        module => 'circulation',
+        letter_code => $params->{'letter_code'},
+        branchcode => $params->{'branchcode'},
+        tables => \%tables,
+        substitute => $substitute,
+        repeat => { item => \@item_tables },
+        message_transport_type => $params->{message_transport_type},
+    );
+}
+
 =head2 prepare_letter_for_printing
 
 returns a string of text appropriate for printing in the event that an
diff --git a/misc/cronjobs/serialsUpdate.pl b/misc/cronjobs/serialsUpdate.pl
index fedf064..b648554 100755
--- a/misc/cronjobs/serialsUpdate.pl
+++ b/misc/cronjobs/serialsUpdate.pl
@@ -29,10 +29,10 @@ BEGIN {
 }
 
 use C4::Context;
-use C4::Dates qw/format_date format_date_in_iso/;
 use C4::Debug;
 use C4::Serials;
 use C4::Log;
+use Koha::DateUtils;
 
 use Date::Calc qw/Date_to_Days check_date/;
 use Getopt::Long;
@@ -119,7 +119,7 @@ while ( my $issue = $sth->fetchrow_hashref ) {
 
     if ( $subscription && $publisheddate && $publisheddate ne "0000-00-00" ) {
         my $nextpublisheddate = GetNextDate( $subscription, $publisheddate );
-        my $today = format_date_in_iso( C4::Dates->new()->output() );
+        my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
 
         if ( $nextpublisheddate && $today ) {
             my ( $year,  $month,  $day )  = split( /-/, $nextpublisheddate );
diff --git a/misc/cronjobs/staticfines.pl b/misc/cronjobs/staticfines.pl
index eb5284c..de92c65 100755
--- a/misc/cronjobs/staticfines.pl
+++ b/misc/cronjobs/staticfines.pl
@@ -103,7 +103,7 @@ foreach (@pcategories) {
 }
 
 use vars qw(@borrower_fields @item_fields @other_fields);
-use vars qw($fldir $libname $control $mode $delim $dbname $today $today_iso $today_days);
+use vars qw($fldir $libname $control $mode $delim $dbname $today_iso $today_days);
 use vars qw($filename);
 
 CHECK {
@@ -135,8 +135,7 @@ if (defined $borrowernumberlimit) {
 }
 my $overdueItemsCounted = 0;
 my %calendars           = ();
-$today      = C4::Dates->new();
-$today_iso  = $today->output('iso');
+$today_iso  = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } );
 my ($tyear, $tmonth, $tday) = split( /-/, $today_iso );
 $today_days = Date_to_Days( $tyear, $tmonth, $tday );
 
@@ -145,14 +144,14 @@ for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) {
     my $datedue;
     my $datedue_days;
     eval {
-    $datedue = C4::Dates->new( $data->[$i]->{'date_due'}, 'iso' );
-    $datedue_days = Date_to_Days( split( /-/, $datedue->output('iso') ) );
+    $datedue = eval { output_pref( { dt => dt_from_string( $data->[$i]->{'date_due'} ), dateonly => 1, dateformat => 'iso' } ); };
+    $datedue_days = Date_to_Days( split( /-/, $datedue ) );
     };
     if ($@) {
     warn "Error on date for borrower " . $data->[$i]->{'borrowernumber'} .  ": $@date_due: " . $data->[$i]->{'date_due'} . "\ndatedue_days: " . $datedue_days . "\nSkipping";
     next;
     }
-    my $due_str = $datedue->output();
+    my $due_str = eval { output_pref( { dt => dt_from_string( $datedue ), dateonly => 1 } ); };
     unless ( defined $data->[$i]->{'borrowernumber'} ) {
         print STDERR "ERROR in Getoverdues line $i: issues.borrowernumber IS NULL.  Repair 'issues' table now!  Skipping record.\n";
         next;    # Note: this doesn't solve everything.  After NULL borrowernumber, multiple issues w/ real borrowernumbers can pile up.
@@ -192,8 +191,8 @@ for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) {
         $data->[$i],
         $borrower->{'categorycode'},
         $branchcode,
-        dt_from_string($datedue->output('iso')),
-        dt_from_string($today->output('iso')),
+        dt_from_string( $datedue ),
+        dt_from_string( $today_iso ),
     );
 
     # Reassign fine's amount if specified in command-line
diff --git a/misc/cronjobs/thirdparty/TalkingTech_itiva_outbound.pl b/misc/cronjobs/thirdparty/TalkingTech_itiva_outbound.pl
index d4063d4..18e9f86 100755
--- a/misc/cronjobs/thirdparty/TalkingTech_itiva_outbound.pl
+++ b/misc/cronjobs/thirdparty/TalkingTech_itiva_outbound.pl
@@ -36,8 +36,8 @@ use C4::Context;
 use C4::Items;
 use C4::Letters;
 use C4::Overdues;
-use C4::Dates;
 use C4::Calendar;
+use Koha::DateUtils;
 
 sub usage {
     pod2usage( -verbose => 2 );
@@ -124,8 +124,8 @@ foreach my $type (@types) {
     }
 
     foreach my $issues (@loop) {
-        my $date = C4::Dates->new( $issues->{'date_due'}, 'iso' );
-        my $due_date = $date->output('metric');
+        my $date_dt = dt_from_string ( $issues->{'date_due'} );
+        my $due_date = output_pref( { dt => $date_dt, dateonly => 1, dateformat =>'metric' } );
 
         my $letter = C4::Letters::GetPreparedLetter(
             module      => $module,
-- 
1.7.10.4