From 315fb717c116c805ac5dfd1272a87b5b1285ddd6 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 and follwoing comments on IRC by putti
http://irc.koha-community.org/koha/2015-11-06#i_1752803
(misc/cronjobs/gather_print_notices.pl is using still C4::Dates, line 84)
6.11.2015 /mv
---
 misc/cronjobs/batch_anonymise.pl                       |    1 -
 misc/cronjobs/cleanup_database.pl                      |    1 -
 misc/cronjobs/gather_print_notices.pl                  |   13 +++++++------
 misc/cronjobs/overdue_notices.pl                       |   12 +++++++-----
 misc/cronjobs/serialsUpdate.pl                         |    4 ++--
 misc/cronjobs/staticfines.pl                           |   15 +++++++--------
 misc/cronjobs/thirdparty/TalkingTech_itiva_outbound.pl |    6 +++---
 7 files changed, 26 insertions(+), 26 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..cb1aecf 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;
@@ -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({
@@ -212,7 +213,7 @@ sub generate_html {
 
     $template->param(
         stylesheet => C4::Context->preference("NoticeCSS"),
-        today      => $today->output(),
+        today      => $today_syspref,
         messages   => $messages,
     );
 
@@ -310,7 +311,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',
     );
 
diff --git a/misc/cronjobs/overdue_notices.pl b/misc/cronjobs/overdue_notices.pl
index 2f9fa68..3cc25f9 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;
diff --git a/misc/cronjobs/serialsUpdate.pl b/misc/cronjobs/serialsUpdate.pl
index 9cc3014..aae1e56 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