From b3feec0c4fb99877194593b857302f77127a605b Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@biblibre.com>
Date: Tue, 14 Aug 2012 17:26:46 +0200
Subject: [PATCH] BUG 8652: Add a default value for the lateorders

- By default, the date from value is the today's date
- Replace C4::Dates with Koha::DateUtils

To test:
Check the page displays the late orders by default.
Add values for 'date from' and/or 'date to' and/or delay.
The date interval is based on the estimated delivery date and the delay
param is based on the closed date.
---
 C4/Acquisition.pm   |   13 ++++++++-----
 acqui/lateorders.pl |   39 +++++++++++++++++++++++++++++++--------
 2 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm
index 9678ffc..cbd1292 100644
--- a/C4/Acquisition.pm
+++ b/C4/Acquisition.pm
@@ -1671,16 +1671,19 @@ sub GetLateOrders {
         $from .= ' AND borrowers.branchcode LIKE ? ';
         push @query_params, $branch;
     }
+
+    if ( defined $estimateddeliverydatefrom or defined $estimateddeliverydateto ) {
+        $from .= ' AND aqbooksellers.deliverytime IS NOT NULL ';
+    }
     if ( defined $estimateddeliverydatefrom ) {
-        $from .= '
-            AND aqbooksellers.deliverytime IS NOT NULL
-            AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) >= ?';
+        $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) >= ?';
         push @query_params, $estimateddeliverydatefrom;
     }
-    if ( defined $estimateddeliverydatefrom and defined $estimateddeliverydateto ) {
+    if ( defined $estimateddeliverydateto ) {
         $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= ?';
         push @query_params, $estimateddeliverydateto;
-    } elsif ( defined $estimateddeliverydatefrom ) {
+    }
+    if ( defined $estimateddeliverydatefrom and not defined $estimateddeliverydateto ) {
         $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= CAST(now() AS date)';
     }
     if (C4::Context->preference("IndependantBranches")
diff --git a/acqui/lateorders.pl b/acqui/lateorders.pl
index 3c66eb8..8fd8e84 100755
--- a/acqui/lateorders.pl
+++ b/acqui/lateorders.pl
@@ -43,8 +43,7 @@ To know on which branch this script have to display late order.
 
 =cut
 
-use strict;
-use warnings;
+use Modern::Perl;
 use CGI;
 use C4::Bookseller qw( GetBooksellersWithLateOrders );
 use C4::Auth;
@@ -54,6 +53,7 @@ use C4::Context;
 use C4::Acquisition;
 use C4::Letters;
 use C4::Branch; # GetBranches
+use Koha::DateUtils;
 
 my $input = new CGI;
 my ($template, $loggedinuser, $cookie) = get_template_and_user({
@@ -66,9 +66,32 @@ my ($template, $loggedinuser, $cookie) = get_template_and_user({
 });
 
 my $booksellerid = $input->param('booksellerid') || undef; # we don't want "" or 0
-my $delay      = $input->param('delay');
+my $delay        = $input->param('delay');
+
+# Get the "date from" param
 my $estimateddeliverydatefrom = $input->param('estimateddeliverydatefrom');
-my $estimateddeliverydateto   = $input->param('estimateddeliverydateto');
+# construct the "date from" in an iso format
+my $estimateddeliverydatefrom_iso = $estimateddeliverydatefrom
+    ? dt_from_string($estimateddeliverydatefrom, "iso")
+    : undef;
+# Get the "date to" param. If it is not defined and $delay is not defined too, it is the today's date.
+my $estimateddeliverydateto = $input->param('estimateddeliverydateto');
+$estimateddeliverydateto = $estimateddeliverydateto
+    ? dt_from_string($estimateddeliverydateto)
+    : ( not defined $delay ) ? dt_from_string() : undef;
+# construct the "date to" in an iso format
+my $estimateddeliverydateto_iso = $estimateddeliverydateto ? dt_from_string($estimateddeliverydateto, "iso") : dt_from_string(undef, "iso");
+
+# Format the output of "date from" and "date to"
+if ( $estimateddeliverydatefrom ) {
+    $estimateddeliverydatefrom = output_pref( $estimateddeliverydatefrom );
+    $estimateddeliverydatefrom =~ s/\s.*//;
+}
+if ( $estimateddeliverydateto ) {
+    $estimateddeliverydateto = output_pref( $estimateddeliverydateto );
+    $estimateddeliverydateto =~ s/\s.*//;
+}
+
 my $branch     = $input->param('branch');
 my $op         = $input->param('op');
 
@@ -98,8 +121,8 @@ if ($op and $op eq "send_alert"){
 my %supplierlist = GetBooksellersWithLateOrders(
     $delay,
     $branch,
-    C4::Dates->new($estimateddeliverydatefrom)->output("iso"),
-    C4::Dates->new($estimateddeliverydateto)->output("iso")
+    $estimateddeliverydatefrom_iso,
+    $estimateddeliverydateto_iso,
 );
 
 my (@sloopy);	# supplier loop
@@ -117,8 +140,8 @@ my @lateorders = GetLateOrders(
     $delay,
     $booksellerid,
     $branch,
-    C4::Dates->new($estimateddeliverydatefrom)->output("iso"),
-    C4::Dates->new($estimateddeliverydateto)->output("iso")
+    $estimateddeliverydatefrom_iso,
+    $estimateddeliverydateto_iso,
 );
 
 my $total;
-- 
1.7.7.3