From 62cdf14eb355175a6ca6bd2cd61ae901be14b2e9 Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Fri, 10 Jan 2014 15:50:34 +0100
Subject: [PATCH] Bug 11551: Allow to search on parent ordernumber in histsearch.pl

It is now possible to search on the ordernumber on the order search
page.

Also search on parent_ordernumber is possible. Like that, you are allow
to search children for a given ordernumber.

Test plan:
1/ create a basket and 1 order with at least 2 items.
2/ receive partialy the order (receive only 1 item).
3/ note that a new ordernumber is created for item not received.
4/ go on the order search form and search for the original ordernumber
without checking the new checkbox "Display children too." => only 1
order (the parent) is displayed.
5/ now check the checkbox and search again => the parent order is
displayed but children too.

Signed-off-by: remy juliette <juliette.levast@iepg.fr>
---
 C4/Acquisition.pm                                  |   13 +++++++++++++
 acqui/histsearch.pl                                |    6 ++++++
 .../prog/en/modules/acqui/histsearch.tt            |   10 ++++++++++
 3 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm
index 6446a07..a015d5d 100644
--- a/C4/Acquisition.pm
+++ b/C4/Acquisition.pm
@@ -2188,6 +2188,8 @@ sub GetHistory {
     my $orderstatus = $params{orderstatus};
     my $biblionumber = $params{biblionumber};
     my $get_canceled_order = $params{get_canceled_order} || 0;
+    my $ordernumber = $params{ordernumber};
+    my $search_children_too = $params{search_children_too} || 0;
 
     my @order_loop;
     my $total_qty         = 0;
@@ -2315,6 +2317,17 @@ sub GetHistory {
         push @query_params, "%$basketgroupname%";
     }
 
+    if ($ordernumber) {
+        $query .= " AND (aqorders.ordernumber = ? ";
+        push @query_params, $ordernumber;
+        if ($search_children_too) {
+            $query .= " OR aqorders.parent_ordernumber = ? ";
+            push @query_params, $ordernumber;
+        }
+        $query .= ") ";
+    }
+
+
     if ( C4::Context->preference("IndependentBranches") ) {
         unless ( C4::Context->IsSuperLibrarian() ) {
             $query .= " AND (borrowers.branchcode = ? OR borrowers.branchcode ='' ) ";
diff --git a/acqui/histsearch.pl b/acqui/histsearch.pl
index 4e152e9..bd1aa4c 100755
--- a/acqui/histsearch.pl
+++ b/acqui/histsearch.pl
@@ -74,6 +74,8 @@ my $from_placed_on          = C4::Dates->new($input->param('from'));
 my $to_placed_on            = C4::Dates->new($input->param('to'));
 my $budget                  = $input->param( 'budget' );
 my $orderstatus             = $input->param( 'orderstatus' );
+my $ordernumber             = $input->param( 'ordernumber' );
+my $search_children_too     = $input->param( 'search_children_too' );
 
 if ( not $input->param('from') ) {
     # FIXME Dirty but we can't sent a Date::Calc to C4::Dates ?
@@ -117,6 +119,8 @@ if ($do_search) {
         basketgroupname => $basketgroupname,
         budget => $budget,
         orderstatus => $orderstatus,
+        ordernumber => $ordernumber,
+        search_children_too => $search_children_too,
     );
 }
 
@@ -147,6 +151,8 @@ $template->param(
     basket                  => $basket,
     booksellerinvoicenumber => $booksellerinvoicenumber,
     basketgroupname         => $basketgroupname,
+    ordernumber             => $ordernumber,
+    search_children_too     => $search_children_too,
     from_placed_on          => $from_date,
     to_placed_on            => $to_date,
     orderstatus             => $orderstatus,
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/histsearch.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/histsearch.tt
index 56451ac..ffe7537 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/histsearch.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/histsearch.tt
@@ -47,6 +47,16 @@
             <label for="basketgroupname">Basket group:</label>
             <input type="text" name="basketgroupname" id="basketgroupname" value="[% basketgroupname %]" />
         </li>
+        <li>
+          <label for="ordernumber">Order no.:</label>
+          <input type="text" name="ordernumber" id="ordernumber" value="[% ordernumber %]" />
+          [% IF search_children_too %]
+            <input type="checkbox" name="search_children_too" id="search_children_too" value="1" checked="checked" />
+          [% ELSE %]
+            <input type="checkbox" name="search_children_too" id="search_children_too" value="1" />
+          [% END %]
+          <label class="yesno" for="search_children_too">Display children too.</label>
+        </li>
 
         <li>
             <label for="orderstatus">Order status: </label>
-- 
1.7.2.5