From 536b67ec34a1674543be74263b769756a314a26c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marc=20V=C3=A9ron?= <veron@veron.ch>
Date: Thu, 8 Oct 2015 23:02:30 +0200
Subject: [PATCH] Bug 14985: Remove C4::Dates from files in folder C4/*.pm
 (part one)

This patch removes C4::Dates from following files in folder C4:

- C4/Members.pm
- C4/Reserves.pm
- C4/Search.pm
- C4/Utils/DataTables.pm
- C4/Utils/DataTables/Members.pm
- C4/VirtualShelves/Page.pm

To test:
 -run tests as appropriate,
- have a close look at the code changes
- try to find regressions

http://bugs.koha-community.org/show_bug.cgi?id=14985

Signed-off-by: Brendan Gallagher <brendan@bywatersolutions.com>

Bug 14985: (followup) Remove eval if dates come from database

This patch removes some evals from date-formatting where the dates come
from the database.

See comments #7 - #9

Additionaly, C4/VirtualShelves/Page.pm is removed from the patches (obsolete).

Bug 14985: (followup) Remove C4::Dates from C4/Overdues.pm

Ths patch removes a stray C4::Dates from C4/Overdues.pm

- To test got to a patron who has overdues
  (Home > Circulation > Checkouts > [Patron])
- Print overdues

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
---
 C4/Members.pm                  | 13 ++++++-------
 C4/Overdues.pm                 |  3 ++-
 C4/Reserves.pm                 | 16 +++++++++-------
 C4/Search.pm                   |  4 ++--
 C4/Utils/DataTables.pm         |  4 ++--
 C4/Utils/DataTables/Members.pm |  2 +-
 6 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/C4/Members.pm b/C4/Members.pm
index 5f71e89..2616598 100644
--- a/C4/Members.pm
+++ b/C4/Members.pm
@@ -23,7 +23,6 @@ package C4::Members;
 use strict;
 #use warnings; FIXME - Bug 2505
 use C4::Context;
-use C4::Dates qw(format_date_in_iso format_date);
 use String::Random qw( random_string );
 use Date::Calc qw/Today Add_Delta_YM check_date Date_to_Days/;
 use C4::Log; # logaction
@@ -738,12 +737,12 @@ sub AddMember {
 
     # add expiration date if it isn't already there
     unless ( $data{'dateexpiry'} ) {
-        $data{'dateexpiry'} = GetExpiryDate( $data{'categorycode'}, C4::Dates->new()->output("iso") );
+        $data{'dateexpiry'} = GetExpiryDate( $data{'categorycode'}, output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } ) );
     }
 
     # add enrollment date if it isn't already there
     unless ( $data{'dateenrolled'} ) {
-        $data{'dateenrolled'} = C4::Dates->new()->output("iso");
+        $data{'dateenrolled'} = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } );
     }
 
     my $patron_category = $schema->resultset('Category')->find( $data{'categorycode'} );
@@ -1862,8 +1861,9 @@ sub ExtendMemberSubscriptionTo {
     my $borrower = GetMember('borrowernumber'=>$borrowerid);
     unless ($date){
       $date = (C4::Context->preference('BorrowerRenewalPeriodBase') eq 'dateexpiry') ?
-                                        C4::Dates->new($borrower->{'dateexpiry'}, 'iso')->output("iso") :
-                                        C4::Dates->new()->output("iso");
+                                        eval { output_pref( { dt => dt_from_string( $borrower->{'dateexpiry'}  ), dateonly => 1, dateformat => 'iso' } ); }
+                                        :
+                                        output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } );
       $date = GetExpiryDate( $borrower->{'categorycode'}, $date );
     }
     my $sth = $dbh->do(<<EOF);
@@ -2261,8 +2261,7 @@ sub GetMessages {
     my @results;
 
     while ( my $data = $sth->fetchrow_hashref ) {
-        my $d = C4::Dates->new( $data->{message_date}, 'iso' );
-        $data->{message_date_formatted} = $d->output;
+        $data->{message_date_formatted} = output_pref( { dt => dt_from_string( $data->{message_date} ), dateonly => 1, dateformat => 'iso' } );
         push @results, $data;
     }
     return \@results;
diff --git a/C4/Overdues.pm b/C4/Overdues.pm
index 0fc6232..72c0704 100644
--- a/C4/Overdues.pm
+++ b/C4/Overdues.pm
@@ -33,6 +33,7 @@ use C4::Accounts;
 use C4::Log; # logaction
 use C4::Debug;
 use C4::Budgets qw(GetCurrency);
+use Koha::DateUtils;
 
 use vars qw($VERSION @ISA @EXPORT);
 
@@ -978,7 +979,7 @@ sub parse_overdues_letter {
     }
 
     my $substitute = $params->{'substitute'} || {};
-    $substitute->{today} ||= C4::Dates->new()->output("syspref");
+    $substitute->{today} ||= output_pref( { dt => dt_from_string, dateonly => 1} );
 
     my %tables = ( 'borrowers' => $params->{'borrowernumber'} );
     if ( my $p = $params->{'branchcode'} ) {
diff --git a/C4/Reserves.pm b/C4/Reserves.pm
index 4b10ff9..29a0aa5 100644
--- a/C4/Reserves.pm
+++ b/C4/Reserves.pm
@@ -35,7 +35,6 @@ use C4::Members::Messaging;
 use C4::Members qw();
 use C4::Letters;
 use C4::Branch qw( GetBranchDetail );
-use C4::Dates qw( format_date_in_iso );
 
 use Koha::DateUtils;
 use Koha::Calendar;
@@ -166,11 +165,13 @@ sub AddReserve {
 
     my $dbh     = C4::Context->dbh;
 
-    $resdate = format_date_in_iso( $resdate ) if ( $resdate );
-    $resdate = C4::Dates->today( 'iso' ) unless ( $resdate );
+    $resdate = eval { output_pref( { dt => dt_from_string( $resdate ), dateonly => 1, dateformat => 'iso' }); }
+               if ( $resdate );
+    $resdate = eval { output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' }); }
+               unless ( $resdate );
 
     if ($expdate) {
-        $expdate = format_date_in_iso( $expdate );
+        $expdate = eval { output_pref( { dt => dt_from_string( $expdate), dateonly => 1, dateformat => 'iso' } ); };
     } else {
         undef $expdate; # make reserves.expirationdate default to null rather than '0000-00-00'
     }
@@ -1162,7 +1163,7 @@ sub ModReserve {
 
         if ( defined( $suspend_until ) ) {
             if ( $suspend_until ) {
-                $suspend_until = C4::Dates->new( $suspend_until )->output("iso");
+                $suspend_until = eval { output_pref( { dt => dt_from_string( $suspend_until ), dateonly => 1 }); };
                 $dbh->do("UPDATE reserves SET suspend = 1, suspend_until = ? WHERE reserve_id = ?", undef, ( $suspend_until, $reserve_id ) );
             } else {
                 $dbh->do("UPDATE reserves SET suspend_until = NULL WHERE reserve_id = ?", undef, ( $reserve_id ) );
@@ -1655,7 +1656,8 @@ sub SuspendAll {
     my $suspend_until  = $params{'suspend_until'}  || undef;
     my $suspend        = defined( $params{'suspend'} ) ? $params{'suspend'} :  1;
 
-    $suspend_until = C4::Dates->new( $suspend_until )->output("iso") if ( defined( $suspend_until ) );
+    $suspend_until = eval { output_pref( { dt => dt_from_string( $suspend_until), dateonly => 1, dateformat => 'iso' } ); }
+                     if ( defined( $suspend_until ) );
 
     return unless ( $borrowernumber || $biblionumber );
 
@@ -1976,7 +1978,7 @@ sub _koha_notify_reserve {
             'reserves'  => $reserve,
             'items', $reserve->{'itemnumber'},
         },
-        substitute => { today => C4::Dates->new()->output() },
+        substitute => { today => eval { output_pref( { dt => dt_from_string, dateonly => 1 } ); } },
     );
 
     my $notification_sent = 0; #Keeping track if a Hold_filled message is sent. If no message can be sent, then default to a print message.
diff --git a/C4/Search.pm b/C4/Search.pm
index 11b4909..b895993 100644
--- a/C4/Search.pm
+++ b/C4/Search.pm
@@ -21,10 +21,10 @@ require Exporter;
 use C4::Context;
 use C4::Biblio;    # GetMarcFromKohaField, GetBiblioData
 use C4::Koha;      # getFacets
+use Koha::DateUtils;
 use Lingua::Stem;
 use C4::Search::PazPar2;
 use XML::Simple;
-use C4::Dates qw(format_date);
 use C4::Members qw(GetHideLostItemsPreference);
 use C4::XSLT;
 use C4::Branch;
@@ -2094,7 +2094,7 @@ sub searchResults {
             {
                 $onloan_count++;
                 my $key = $prefix . $item->{onloan} . $item->{barcode};
-                $onloan_items->{$key}->{due_date} = format_date( $item->{onloan} );
+                $onloan_items->{$key}->{due_date} = output_pref( { dt => dt_from_string( $item->{onloan} ), dateonly => 1 } );
                 $onloan_items->{$key}->{count}++ if $item->{$hbranch};
                 $onloan_items->{$key}->{branchname}     = $item->{branchname};
                 $onloan_items->{$key}->{location}       = $shelflocations->{ $item->{location} };
diff --git a/C4/Utils/DataTables.pm b/C4/Utils/DataTables.pm
index 26af6c5..2a0d998 100644
--- a/C4/Utils/DataTables.pm
+++ b/C4/Utils/DataTables.pm
@@ -249,11 +249,11 @@ sub dt_build_query_dates {
     my @params;
     if ( $datefrom ) {
         $query .= " AND $field >= ? ";
-        push @params, C4::Dates->new($datefrom)->output('iso');
+        push @params, eval { output_pref( { dt => dt_from_string( $datefrom ), dateonly => 1, dateformat => 'iso' } ); };
     }
     if ( $dateto ) {
         $query .= " AND $field <= ? ";
-        push @params, C4::Dates->new($dateto)->output('iso');
+        push @params, eval { output_pref( { dt => dt_from_string( $dateto ), dateonly => 1, dateformat => 'iso' } ); };
     }
     return ( $query, \@params );
 }
diff --git a/C4/Utils/DataTables/Members.pm b/C4/Utils/DataTables/Members.pm
index 4240fa0..5e0b812 100644
--- a/C4/Utils/DataTables/Members.pm
+++ b/C4/Utils/DataTables/Members.pm
@@ -142,7 +142,7 @@ sub search {
         ($patron->{overdues}, $patron->{issues}, $patron->{fines}) =
             GetMemberIssuesAndFines($patron->{borrowernumber});
         if($patron->{dateexpiry} and $patron->{dateexpiry} ne '0000-00-00') {
-            $patron->{dateexpiry} = C4::Dates->new($patron->{dateexpiry}, "iso")->output();
+            $patron->{dateexpiry} = output_pref( { dt => dt_from_string( $patron->{dateexpiry}), dateonly => 1, dateformat => 'iso' } );
         } else {
             $patron->{dateexpiry} = '';
         }
-- 
2.1.0