From 59649883cdd9e1f1e6ed9550985fab1e7baf9574 Mon Sep 17 00:00:00 2001
From: Fridolyn SOMERS <fridolyn.somers@biblibre.com>
Date: Mon, 13 Aug 2012 13:55:11 +0200
Subject: [PATCH] [SIGNED-OFF] Bug 8629: update of statistics fields on fund selection in order creation/edition
Content-Type: text/plain; charset="utf-8"

Signed-off-by: Marc Veron <veron@veron.ch>

Followed test plan in comment #1
Behaves as described in the test plan.
Useful feature!
---
 acqui/addorderiso2709.pl                           |    6 --
 acqui/fetch_sort_dropbox.pl                        |   54 ++++++++++++-------
 acqui/neworderempty.pl                             |    4 --
 koha-tmpl/intranet-tmpl/prog/en/js/acq.js          |   10 +++-
 .../prog/en/modules/acqui/addorderiso2709.tt       |   28 ++++++-----
 .../prog/en/modules/acqui/neworderempty.tt         |    7 ++-
 6 files changed, 61 insertions(+), 48 deletions(-)

diff --git a/acqui/addorderiso2709.pl b/acqui/addorderiso2709.pl
index 002fbf5..b1b4910 100755
--- a/acqui/addorderiso2709.pl
+++ b/acqui/addorderiso2709.pl
@@ -298,10 +298,7 @@ if ($budget) {    # its a mod ..
     }
 } elsif ( scalar(@$budgets) ) {
     $CGIsort1 = GetAuthvalueDropbox(  @$budgets[0]->{'sort1_authcat'}, '' );
-} else {
-    $CGIsort1 = GetAuthvalueDropbox(  '', '' );
 }
-
 # if CGIsort is successfully fetched, the use it
 # else - failback to plain input-field
 if ($CGIsort1) {
@@ -317,10 +314,7 @@ if ($budget) {
     }
 } elsif ( scalar(@$budgets) ) {
     $CGIsort2 = GetAuthvalueDropbox(  @$budgets[0]->{sort2_authcat}, '' );
-} else {
-    $CGIsort2 = GetAuthvalueDropbox( '', '' );
 }
-
 if ($CGIsort2) {
     $template->param( CGIsort2 => $CGIsort2 );
 } else {
diff --git a/acqui/fetch_sort_dropbox.pl b/acqui/fetch_sort_dropbox.pl
index 2dfeb81..ce47fb7 100755
--- a/acqui/fetch_sort_dropbox.pl
+++ b/acqui/fetch_sort_dropbox.pl
@@ -54,40 +54,54 @@ Sort number. 1 or 2 for the moment.
 my $input = new CGI;
 
 my $budget_id = $input->param('budget_id');
-my $sort_id   = $input->param('sort');
+my $sort_nb   = $input->param('sort');
+die "sort parameter can only be 1 or 2" unless ($sort_nb == 1 || $sort_nb == 2);
 
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {   template_name   => "acqui/ajax.tmpl",
         query           => $input,
         type            => "intranet",
         authnotrequired => 0,
-        flagsrequired => {editcatalogue => 'edit_catalogue'},
+        flagsrequired => {acquisition => 'order_manage'},
         debug => 0,
     }
 );
 
-my $budget = GetBudget($budget_id);
-my $dropbox_values = GetAuthvalueDropbox( $budget->{'sort'.$sort_id.'_authcat'}, '' );
+my $ret_html;
+my $name = 'sort'.$sort_nb;
+my $authcat_field = 'sort'.$sort_nb.'_authcat';
 
-my @authorised_values;
-my %authorised_lib;
+my $budget = GetBudget($budget_id);
 
-foreach ( @$dropbox_values) {
-    push @authorised_values, $_->{value};
-    $authorised_lib{$_->{value}} = $_->{label};
-}
+if ( $budget && $budget->{$authcat_field} ) {
+    # with custom  Asort* planning values
+    my $dropbox_values = GetAuthvalueDropbox( $budget->{$authcat_field}, '' );
 
-my $budget_authvalue_dropbox = CGI::scrolling_list(
-    -values   => \@authorised_values,
-    -labels   => \%authorised_lib,
-    -default  => $authorised_values[0],
-);
+    my @authorised_values;
+    my %authorised_lib;
+    my $default_value;
 
+    foreach ( @$dropbox_values) {
+        push @authorised_values, $_->{value};
+        $authorised_lib{$_->{value}} = $_->{label};
+        $default_value = $_->{value} if $_->{'default'};
+    }
 
-# strip off select tags
-$budget_authvalue_dropbox =~ s/^\<select.*?\"\>//;
-$budget_authvalue_dropbox =~ s/\<\/select\>$//;
-chomp $budget_authvalue_dropbox;
+    $ret_html = CGI::scrolling_list(
+        -values   => \@authorised_values,
+        -labels   => \%authorised_lib,
+        -default  => $default_value,
+        -override => 1,
+        -size     => 1,
+        -multiple => 0,
+        -name     => $name,
+        -id       => $name,
+    );
+
+} else {
+    # free input
+    $ret_html = '<input type="text" size="20" name="'.$name.'" id="'.$name.'" />';
+}
 
-$template->param( return => $budget_authvalue_dropbox );
+$template->param( 'return' => $ret_html );
 output_html_with_http_headers $input, $cookie, $template->output;
diff --git a/acqui/neworderempty.pl b/acqui/neworderempty.pl
index a461a1b..4a3eb49 100755
--- a/acqui/neworderempty.pl
+++ b/acqui/neworderempty.pl
@@ -281,8 +281,6 @@ if ($budget) {    # its a mod ..
     }
 } elsif(@{$budgets}){
     $CGIsort1 = GetAuthvalueDropbox(  @$budgets[0]->{'sort1_authcat'}, '' );
-}else{
-    $CGIsort1 = GetAuthvalueDropbox( '', '' );
 }
 
 # if CGIsort is successfully fetched, the use it
@@ -300,8 +298,6 @@ if ($budget) {
     }
 } elsif(@{$budgets}) {
     $CGIsort2 = GetAuthvalueDropbox(  @$budgets[0]->{sort2_authcat}, '' );
-}else{
-    $CGIsort2 = GetAuthvalueDropbox( '', '' );
 }
 
 if ($CGIsort2) {
diff --git a/koha-tmpl/intranet-tmpl/prog/en/js/acq.js b/koha-tmpl/intranet-tmpl/prog/en/js/acq.js
index 2efeba0..028dae4 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/js/acq.js
+++ b/koha-tmpl/intranet-tmpl/prog/en/js/acq.js
@@ -744,7 +744,7 @@ function fetchSortDropbox(f) {
 
 for (i=1;i<=2;i++) {
 
-    var sort_dropbox = document.getElementById('sort'+i);
+    var sort_zone = document.getElementById('sort'+i+'_zone');
     var url = '../acqui/fetch_sort_dropbox.pl?sort='+i+'&budget_id='+budgetId;
 
     var xmlhttp = null;
@@ -764,7 +764,13 @@ for (i=1;i<=2;i++) {
         }
     };
     // rc =  eval ( xmlhttp.responseText );
-    sort_dropbox.innerHTML  =  xmlhttp.responseText;
+    var retRootType = xmlhttp.responseXML.firstChild.nodeName;
+    var existingInputs = sort_zone.getElementsByTagName('input');
+    if (existingInputs.length > 0 && retRootType == 'input') {
+        // when sort is already an input, do not override to preseve value
+        return;
+    }
+    sort_zone.innerHTML = xmlhttp.responseText;
 }
 }
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/addorderiso2709.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/addorderiso2709.tt
index 7a7e72a..71ae462 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/addorderiso2709.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/addorderiso2709.tt
@@ -188,7 +188,7 @@
                         </li>
                         <li><div class="hint">The 2 following fields are available for your own usage. They can be useful for statistical purposes</div>
                             <label for="sort1">Planning value1: </label>
-
+                            <span id="sort1_zone">
                             [% IF CGIsort1 %]
                                 <select id="sort1" size="1" name="sort1">
                                 [% FOREACH sort_opt IN CGIsort1 %]
@@ -202,23 +202,25 @@
                             [% ELSE %]
                                 <input type="text" id="sort1" size="20" name="sort1" value="[% sort1 %]" />
                             [% END %]
+                            </span>
                         </li>
                         <li>
                             <label for="sort2">Planning value2: </label>
-
-                        [% IF CGIsort2 %]
-                            <select id="sort2" size="1" name="sort1">
-                            [% FOREACH sort_opt IN CGIsort2 %]
-                                [% IF sort_opt.default %]
-                                    <option value="[% sort_opt.id %]" selected="selected">[% sort_opt.label %]</option>
-                                [% ELSE %]
-                                    <option value="[% sort_opt.id %]">[% sort_opt.label %]</option>
+                            <span id="sort2_zone">
+                            [% IF CGIsort2 %]
+                                <select id="sort2" size="1" name="sort1">
+                                [% FOREACH sort_opt IN CGIsort2 %]
+                                    [% IF sort_opt.default %]
+                                        <option value="[% sort_opt.id %]" selected="selected">[% sort_opt.label %]</option>
+                                    [% ELSE %]
+                                        <option value="[% sort_opt.id %]">[% sort_opt.label %]</option>
+                                    [% END %]
                                 [% END %]
+                                </select>
+                            [% ELSE %]
+                                 <input type="text" id="sort2" size="20" name="sort2" value="[% sort2 %]" />
                             [% END %]
-                            </select>
-                        [% ELSE %]
-                             <input type="text" id="sort2" size="20" name="sort2" value="[% sort2 %]" />
-                        [% END %]
+                            </span>
                         </li>
                         <li>
                             
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt
index 566318f..0195dd8 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt
@@ -508,7 +508,7 @@ $(document).ready(function()
             </li>
             <li><div class="hint">The 2 following fields are available for your own usage. They can be useful for statistical purposes</div>
                 <label for="sort1">Statistic 1: </label>
-
+                <span id="sort1_zone">
                 [% IF CGIsort1 %]
                     <select id="sort1" size="1" name="sort1">
                     [% FOREACH sort_opt IN CGIsort1 %]
@@ -520,13 +520,13 @@ $(document).ready(function()
                     [% END %]
                     </select>
                 [% ELSE %]
-
                     <input type="text" id="sort1" size="20" name="sort1" value="[% sort1 %]" />
                 [% END %]
+                </span>
             </li>
             <li>
                 <label for="sort2">Statistic 2: </label>
-
+                <span id="sort2_zone">
                 [% IF CGIsort2 %]
                     <select id="sort2" size="1" name="sort2">
                     [% FOREACH sort_opt IN CGIsort2 %]
@@ -540,6 +540,7 @@ $(document).ready(function()
                 [% ELSE %]
                     <input type="text" id="sort2" size="20" name="sort2" value="[% sort2 %]" />
                 [% END %]
+                </span>
             </li>
 </ol>
     </fieldset>
-- 
1.7.2.5