From b22f56f3ed070021b91e924ce442eb869fb455c3 Mon Sep 17 00:00:00 2001
From: Maxime Pelletier <maxime.pelletier@libeo.com>
Date: Tue, 3 Jul 2012 17:21:48 -0400
Subject: [PATCH] =?UTF-8?q?Fix=20l'alerte=20ajax=20dans=20la=20cr=C3=A9ation=20d'achat=20lorsque=20le=20montant=20d=C3=A9passe=20celui=20du=20budget.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 koha-tmpl/intranet-tmpl/prog/en/js/acq.js          |   38 +++++++------------
 .../prog/en/modules/acqui/neworderempty.tt         |   14 ++++---
 2 files changed, 22 insertions(+), 30 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/js/acq.js b/koha-tmpl/intranet-tmpl/prog/en/js/acq.js
index 70e988e..b108ac4 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/js/acq.js
+++ b/koha-tmpl/intranet-tmpl/prog/en/js/acq.js
@@ -783,30 +783,20 @@ for (i=1;i<=2;i++) {
 
 //USED BY NEWORDEREMPTY.PL
 function totalExceedsBudget(budgetId, total) {
-
-    var xmlhttp = null;
-    xmlhttp = new XMLHttpRequest();
-    if ( typeof xmlhttp.overrideMimeType != 'undefined') {
-        xmlhttp.overrideMimeType('text/xml');
-    }
-
-    var url = '../acqui/check_budget_total.pl?budget_id=' + budgetId + "&total=" + total;
-    xmlhttp.open('GET', url, false);
-    xmlhttp.send(null);
-
-    xmlhttp.onreadystatechange = function() {
-        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
-
-            actTotal = eval ( xmlhttp.responseText );
-
-            if (  Math.abs(actTotal) < Math.abs(total)  ) {
-            // if budget is to low :(
-                return true ;
-            } else {
-                return false;
-            }
-        }
-    }
+    budgetTotal = 0;
+    $.ajax({
+        url: "../acqui/check_budget_total.pl",
+        async:false,
+        method: "post",
+        data: {budget_id : budgetId, total : total},
+        success: function(actTotal) {
+            actTotal = Math.abs(actTotal);
+            exceeds = (actTotal < Math.abs(total));
+            if (exceeds)
+                budgetTotal = actTotal;
+         }
+    });
+    return budgetTotal;
 }
 
 
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 4cd6b49..fa0b374 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt
@@ -56,12 +56,6 @@ function Check(ff) {
                     _alertString += "\n- " + _("Total must be a number");
     }
 
-    if (totalExceedsBudget(ff.budget_id.value, ff.total.value  )  ) {
-        ok=1;
-        _alertString += "\n- " + _("Order total (") + ff.total.value +
-            _(") exceeds budget available (") + actTotal+")";
-    }
-
     if ( ff.field_value ) {
         var empty_item_mandatory = 0;
         for (i = 0; i < ff.field_value.length; i++) {
@@ -87,6 +81,14 @@ function Check(ff) {
         [% END %]
         return false;
     }
+    else {
+        if (actTotal = totalExceedsBudget(ff.budget_id.value, ff.total.value  )  ) {
+            stringWarning = _("Order total (") + ff.total.value +
+                _(") exceeds budget available (") + actTotal+")\n";
+            stringWarning += _("Are you sure you want do save this order?");
+            return confirm(stringWarning);
+        }
+    }
 
     [% IF (AcqCreateItemOrdering) %]
         if(check_additem('[% UniqueItemFields %]') == false) {
-- 
1.7.2.5