From af0b365f9e9244c41aaa4257bad17f24135b7e45 Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Tue, 25 Feb 2014 14:13:58 +0100
Subject: [PATCH] Bug 11870: Add more informations to histsearch.pl results

- Basket group closed date and delivery place (in basketgroup column)
- Additional order fields (one column for each additional field)

Depends on bugs 11844 and 11708
---
 C4/Acquisition.pm                                  |    2 ++
 acqui/histsearch.pl                                |   21 ++++++++++++++++++
 .../prog/en/modules/acqui/histsearch.tt            |   23 ++++++++++++++++++--
 3 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm
index b848c73..640c2c4 100644
--- a/C4/Acquisition.pm
+++ b/C4/Acquisition.pm
@@ -2197,6 +2197,8 @@ sub GetHistory {
             aqbasket.basketname,
             aqbasket.basketgroupid,
             aqbasketgroups.name as groupname,
+            aqbasketgroups.closeddate as groupcloseddate,
+            IF(aqbasketgroups.freedeliveryplace != '', aqbasketgroups.freedeliveryplace, aqbasketgroups.deliveryplace) as groupdeliveryplace,
             aqbooksellers.name,
             aqbasket.creationdate,
             aqorders.datereceived,
diff --git a/acqui/histsearch.pl b/acqui/histsearch.pl
index 4e152e9..3b46162 100755
--- a/acqui/histsearch.pl
+++ b/acqui/histsearch.pl
@@ -59,6 +59,7 @@ use C4::Dates;
 use C4::Debug;
 use C4::Branch;
 use C4::Koha;
+use Koha::AdditionalField;
 
 my $input = new CGI;
 my $title                   = $input->param( 'title');
@@ -118,6 +119,26 @@ if ($do_search) {
         budget => $budget,
         orderstatus => $orderstatus,
     );
+    my $additional_fields = Koha::AdditionalField->all({
+        tablename => 'aqorders',
+    });
+    foreach my $af (@$additional_fields) {
+        if (!$af->{authorised_value_category} && $af->{marcfield}) {
+            # Try to retrieve authorised values category from MARC framework
+            my ($field, $subfield) = split /\$/, $af->{marcfield};
+            my $category = C4::Koha::GetAuthValCodeFromField($field, $subfield);
+            if ($category) {
+                $af->{authorised_value_category} = $category;
+            }
+        }
+    }
+    my $af_values = Koha::AdditionalField->fetch_all_values({
+        tablename => 'aqorders',
+    });
+    $template->param(
+        'additional_fields' => $additional_fields,
+        'additional_fields_values' => $af_values,
+    );
 }
 
 my $from_date = $from_placed_on ? $from_placed_on->output('syspref') : undef;
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..ffd7273 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/histsearch.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/histsearch.tt
@@ -1,4 +1,5 @@
 [% USE KohaDates %]
+[% USE AuthorisedValues %]
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; Acquisitions &rsaquo; [% IF ( order_loop ) %]Orders search &rsaquo; Search results[% ELSE %]Order search[% END %]</title>
 <link rel="stylesheet" type="text/css" href="[% themelang %]/css/datatables.css" />
@@ -134,6 +135,9 @@
                 <th>Pending order</th>
 				<th>Unit cost</th>
                 <th>Fund</th>
+                [% FOREACH field IN additional_fields %]
+                  <th>[% field.name %]</th>
+                [% END %]
 			</tr>
             </thead>
             <tbody>
@@ -156,8 +160,12 @@
                     <td>
                         [% IF ( order.basketgroupid ) %]
                             [% order.groupname %] (<a href="basketgroup.pl?booksellerid=[% order.id %]">[% order.basketgroupid %]</a>)
-                        [% ELSE %]
-                            &nbsp;
+                            [% IF order.groupcloseddate %]
+                              <br />Closed on [% order.groupcloseddate |$KohaDates %].
+                            [% END %]
+                            [% IF order.groupdeliveryplace %]
+                              <br />Delivery place: [% order.groupdeliveryplace %]
+                            [% END %]
                         [% END %]
                     </td>
                     <td>[% IF ( order.invoicenumber ) %]
@@ -181,6 +189,17 @@
                     <td>[% order.quantity %]</td>
                     <td>[% order.ecost %]</td>
                     <td>[% order.budget_name %]</td>
+                    [% FOREACH field IN additional_fields %]
+                      [% value = '' %]
+                      [% IF additional_fields_values.exists(order.ordernumber) %]
+                        [% values = additional_fields_values.item(order.ordernumber) %]
+                        [% value = values.item(field.name) %]
+                        [% IF field.authorised_value_category %]
+                          [% value = AuthorisedValues.GetByCode(field.authorised_value_category, value) %]
+                        [% END %]
+                      [% END %]
+                      <td>[% value %]</td>
+                    [% END %]
                 </tr>
             [% END %]
             </tbody>
-- 
1.7.10.4