From 40dba752defba233f903fae7b5bbfd44d0446246 Mon Sep 17 00:00:00 2001 From: Paul Derscheid Date: Thu, 29 Jun 2023 10:16:53 +0000 Subject: [PATCH] Bug 4437: acq.js uses XMLHttpRequest() directly; should use jQuery I was too lazy to resolve the merge conflicts so I just rewrote the previous patch. The functions should be functionally equivalent to the ones using XMLHttpRequest, so they should still work as intended in their consumers. I deleted the getAuthValueDropbox function as I couldn't find a single reference in the entire codebase. Correct me if I'm wrong. I also ran the configured formatter on the whole file to clean up the mess. Actual changes in implementation are just in these functions - totalExceedsBudget - budgetExceedsParent - checkBudgetParent We could pontentially follow this up by making async requests instead to prevent blocking behaviour. But this would mean that we need to change the consumers as well. To test: Testing budgetExceedsParent (without patch): 1) Go to Home > Administration > Budgets administration 2) Create a Budget 'Testbudget', give it a value of 100, make it active 3) Add a fund 'Testfund 1', give it a value of 90, save 4) Add a fund 'Testfund 2', give it a value of 20 (exceeds budget), save Result OK: Message box ------------------------------------------------------- Form not submitted because of the following problem(s) ------------------------------------------------------- - Fund amount exceeds period allocation ------------------------------------------------------- Testing budgetExceedsParent (with patch): a) Clear browser cache, restart memcached, plack b) Make sure page is reloaded, repeat step 4) above Result: Not OK, no message box, form saves, period allocated is 100, total available is 110 (should not) 5) Test the other consumers in a similar manor. 6) Sign off. --- koha-tmpl/intranet-tmpl/prog/js/acq.js | 461 +++++++++++++------------ 1 file changed, 233 insertions(+), 228 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/acq.js b/koha-tmpl/intranet-tmpl/prog/js/acq.js index 89e1ad1fc4..be865fd737 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/acq.js +++ b/koha-tmpl/intranet-tmpl/prog/js/acq.js @@ -1,37 +1,42 @@ /* global confirmDelete */ -/* exported uncheckbox isNotNull isNull isNum log calcTotalRow autoFillRow messenger updateCosts calcNewsuggTotal getAuthValueDropbox totalExceedsBudget budgetExceedsParent checkBudgetParent hideColumn showColumn showAllColumns hideAllColumns */ +/* exported uncheckbox isNotNull isNull isNum log calcTotalRow autoFillRow messenger updateCosts calcNewsuggTotal totalExceedsBudget budgetExceedsParent checkBudgetParent hideColumn showColumn showAllColumns hideAllColumns */ //======================================================================= //input validation: // acqui/uncertainprice.tmpl uses this function uncheckbox(form, field) { - var price = new Number(form.elements['price' + field].value); + var price = new Number(form.elements["price" + field].value); var tmpprice = ""; - var errmsg = __("ERROR: Price is not a valid number, please check the price and try again!"); + var errmsg = __( + "ERROR: Price is not a valid number, please check the price and try again!" + ); if (isNaN(price)) { alert(errmsg); - for(var i=0; i 0) { return false; } @@ -39,30 +44,33 @@ function isNull(f,noalert) { } //Function returns false if v is not a number (if maybenull is 0, it also returns an error if the number is 0) -function isNum(v,maybenull) { +function isNum(v, maybenull) { var n = new Number(v.value); if (isNaN(n)) { return false; } - if (maybenull==0 && v.value=='') { - - - return false; + if (maybenull == 0 && v.value == "") { + return false; } return true; } - //a logging function (a bit buggy, might open millions of log pages when initializing, but works fine after... function log(message) { if (!log.window_ || log.window_.closed) { - var win = window.open("", null, "width=400,height=200," + - "scrollbars=yes,resizable=yes,status=no," + - "location=no,menubar=no,toolbar=no"); + var win = window.open( + "", + null, + "width=400,height=200," + + "scrollbars=yes,resizable=yes,status=no," + + "location=no,menubar=no,toolbar=no" + ); if (!win) return; var doc = win.document; - doc.write("Debug Log" + - ""); + doc.write( + "Debug Log" + + "" + ); doc.close(); log.window_ = win; } @@ -72,127 +80,137 @@ function log(message) { } //======================================================================= -function getElementsByClass( searchClass, domNode, tagName) { +function getElementsByClass(searchClass, domNode, tagName) { if (domNode == null) domNode = document; - if (tagName == null) tagName = '*'; + if (tagName == null) tagName = "*"; var el = new Array(); var tags = domNode.getElementsByTagName(tagName); - var tcl = " "+searchClass+" "; - for(i=0,j=0; i 0 ) { - remainingTotal.style.color = 'green'; - } else { // if its negative, make it red.. - remainingTotal.style.color = 'red'; + cell.value = cc.toFixed(2); // TIDYME... + remainingNew = Math.abs(budgetTotal) - remainingNew; + + if (remainingNew == 0) { + remainingTotal.style.color = "black"; + } else if (remainingNew > 0) { + remainingTotal.style.color = "green"; + } else { + // if its negative, make it red.. + remainingTotal.style.color = "red"; } - remainingTotal.textContent = remainingNew.toFixed(2) ; + remainingTotal.textContent = remainingNew.toFixed(2); } function autoFillRow(bud_id) { - - var remainingTotal = document.getElementById("budget_est_"+bud_id); - var remainingNew = new Number; - var budgetTotal = document.getElementById("budget_tot_"+bud_id ).textContent; - var arr = getElementsByClass("plan_entry_" + bud_id); - - budgetTotal = budgetTotal.replace(/\,/, ""); - var qty = new Number; -// get the totals + var remainingTotal = document.getElementById("budget_est_" + bud_id); + var remainingNew = new Number(); + var budgetTotal = document.getElementById( + "budget_tot_" + bud_id + ).textContent; + var arr = getElementsByClass("plan_entry_" + bud_id); + + budgetTotal = budgetTotal.replace(/\,/, ""); + var qty = new Number(); + // get the totals var novalueArr = new Array(); - for ( var i=0, len=arr.length; i


"); + win.document.write( + "


" + ); win.document.write(etc); - win.document.write("

"); + win.document.write( + "
" + ); win.document.write("
"); } - //======================================================================= // NEXT BLOCK IS USED BY NEWORDERBEMPTY -function updateCosts(){ +function updateCosts() { var quantity = new Number($("#quantity").val()); var discount = new Number($("#discount").val()); - var listprice = new Number($("#listprice").val()); + var listprice = new Number($("#listprice").val()); var currcode = new String($("#currency").val()); - var exchangerate = new Number($("#currency_rate_"+currcode).val()); - var gst_on=false; + var exchangerate = new Number($("#currency_rate_" + currcode).val()); + var gst_on = false; - var rrp = new Number(listprice*exchangerate); - var rep = new Number(listprice*exchangerate); + var rrp = new Number(listprice * exchangerate); + var rep = new Number(listprice * exchangerate); var ecost = rrp; - if ( 100-discount != 100 ) { //Prevent rounding issues if no discount - ecost = new Number(Math.floor(rrp * (100 - discount )) / 100); + if (100 - discount != 100) { + //Prevent rounding issues if no discount + ecost = new Number(Math.floor(rrp * (100 - discount)) / 100); } - var total = new Number( ecost * quantity); + var total = new Number(ecost * quantity); $("#rrp").val(rrp.toFixed(2)); $("#replacementprice").val(rep.toFixed(2)); $("#ecost").val(ecost.toFixed(2)); @@ -204,188 +222,175 @@ function updateCosts(){ // Calculates total amount in a suggestion -function calcNewsuggTotal(){ +function calcNewsuggTotal() { //collect values - var quantity = Number(document.getElementById('quantity').value); - var currcode = String(document.getElementById('currency').value); - var price = Number(document.getElementById('price').value); - var exchangerate = Number(document.getElementById('currency_rate_'+currcode).value); + var quantity = Number(document.getElementById("quantity").value); + var currcode = String(document.getElementById("currency").value); + var price = Number(document.getElementById("price").value); + var exchangerate = Number( + document.getElementById("currency_rate_" + currcode).value + ); - var total = Number(quantity*price*exchangerate); + var total = Number(quantity * price * exchangerate); - document.getElementById('total').value = total.toFixed(2); - document.getElementById('price').value = price.toFixed(2); + document.getElementById("total").value = total.toFixed(2); + document.getElementById("price").value = price.toFixed(2); return true; } -function getAuthValueDropbox( name, cat, destination, selected ) { - if ( typeof(selected) == 'undefined' ) { - selected = ""; - } - if (cat == null || cat == "") { - $(destination).replaceWith(' ' ); - return; - } - $.ajax({ - url: "/cgi-bin/koha/acqui/ajax-getauthvaluedropbox.pl", - data: { - name: name, - category: cat, - default: selected - }, - async: false, - success: function(data){ - if(data === "0"){ - $(destination).replaceWith(' ' ); - }else{ - $(destination).replaceWith(data); - } - } - }); -} - //USED BY NEWORDEREMPTY.PL function totalExceedsBudget(budgetId, total) { + var url = + "../acqui/check_budget_total.pl?budget_id=" + + budgetId + + "&total=" + + 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 ; + $.ajax({ + url: url, + type: "GET", + async: false, + dataType: "text", + success: function(response) { + var actTotal = eval(response); + if (Math.abs(actTotal) < Math.abs(total)) { + // if budget is too low :( + return true; } else { return false; } - } - } + }, + }); } - //USED BY AQBUDGETS.TMPL function budgetExceedsParent(budgetTotal, budgetId, newBudgetParent, periodID) { - - - var xmlhttp = null; - xmlhttp = new XMLHttpRequest(); - if ( typeof xmlhttp.overrideMimeType != 'undefined') { - xmlhttp.overrideMimeType('text/xml'); + var url = + "../admin/check_parent_total.pl?total=" + + budgetTotal + + "&period_id=" + + periodID; + + if (budgetId) { + url += "&budget_id=" + budgetId; + } + if (newBudgetParent) { + url += "&parent_id=" + newBudgetParent; } -// make the call... yawn -// var url = '../admin/check_parent_total.pl?budget_id=' + budgetId + '&parent_id=' + newBudgetParent + "&total=" + budgetTotal + "&period_id="+ periodID ; - - - var url = '../admin/check_parent_total.pl?total=' + budgetTotal + "&period_id="+ periodID ; - -if (budgetId ) { url += '&budget_id=' + budgetId }; -if ( newBudgetParent ) { url += '&parent_id=' + newBudgetParent}; - - - xmlhttp.open('GET', url, false); - xmlhttp.send(null); - - xmlhttp.onreadystatechange = function() { - if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { - // stupid JS... - } else { - // wait for the call to complete - } - }; + var result; - var result = eval ( xmlhttp.responseText ); + $.ajax({ + url: url, + type: "GET", + async: false, + dataType: "text", + success: function(response) { + result = eval(response); + }, + error: function(error) { + console.error(error); + throw error; + }, + }); - if (result == '1') { - return "- " + __("Fund amount exceeds parent allocation") + "\n"; - } else if (result == '2') { - return "- " + __("Fund amount exceeds period allocation") + "\n"; - } else { - return false; + if (result == "1") { + return "- " + __("Fund amount exceeds parent allocation") + "\n"; + } else if (result == "2") { + return "- " + __("Fund amount exceeds period allocation") + "\n"; + } else { + return false; } } - - - //USED BY AQBUDGETS.TMPL function checkBudgetParent(budgetId, newBudgetParent) { - var xmlhttp = null; - xmlhttp = new XMLHttpRequest(); - if ( typeof xmlhttp.overrideMimeType != 'undefined') { - xmlhttp.overrideMimeType('text/xml'); - } - - var url = '../admin/check_budget_parent.pl?budget_id=' + budgetId + '&new_parent=' + newBudgetParent; - xmlhttp.open('GET', url, false); - xmlhttp.send(null); + var url = + "../admin/check_budget_parent.pl?budget_id=" + + budgetId + + "&new_parent=" + + newBudgetParent; + var result; - xmlhttp.onreadystatechange = function() { - if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { - // do something with the results - } else { - // wait for the call to complete - } - }; - - var result = eval ( xmlhttp.responseText ); + $.ajax({ + url: url, + type: "GET", + async: false, + dataType: "text", + success: function(response) { + result = eval(response); + }, + error: function(error) { + console.error(error); + throw error; + }, + }); - if (result == '1') { - return "- " + __("New budget-parent is beneath budget") + "\n"; -// } else if (result == '2') { -// return "- New budget-parent has insufficent funds\n"; -// } else { -// return false; + if (result == "1") { + return "- " + __("New budget-parent is beneath budget") + "\n"; } } function hideColumn(num) { - $("#hideall,#showall").prop("checked", false).parent().removeClass("selected"); - $("#"+num).parent().removeClass("selected"); - var hide = Number(num.replace("col","")) + 2; + $("#hideall,#showall") + .prop("checked", false) + .parent() + .removeClass("selected"); + $("#" + num) + .parent() + .removeClass("selected"); + var hide = Number(num.replace("col", "")) + 2; // hide header and cells matching the index - $("#plan td:nth-child("+hide+"),#plan th:nth-child("+hide+")").toggle(); + $( + "#plan td:nth-child(" + hide + "),#plan th:nth-child(" + hide + ")" + ).toggle(); } -function showColumn(num){ +function showColumn(num) { $("#hideall").prop("checked", false).parent().removeClass("selected"); - $("#"+num).parent().addClass("selected"); + $("#" + num) + .parent() + .addClass("selected"); // set the index of the table column to hide - show = Number(num.replace("col","")) + 2; + show = Number(num.replace("col", "")) + 2; // hide header and cells matching the index - $("#plan td:nth-child("+show+"),#plan th:nth-child("+show+")").toggle(); + $( + "#plan td:nth-child(" + show + "),#plan th:nth-child(" + show + ")" + ).toggle(); } -function showAllColumns(){ - $("#selections").find("input:checkbox").each(function () { - $(this).prop("checked", true); - }); +function showAllColumns() { + $("#selections") + .find("input:checkbox") + .each(function() { + $(this).prop("checked", true); + }); $("#selections span").addClass("selected"); $("#plan td:nth-child(2),#plan tr th:nth-child(2)").nextAll().show(); $("#hideall").prop("checked", false).parent().removeClass("selected"); } -function hideAllColumns(){ +function hideAllColumns() { var allCols = $("#plan th").length; - $("#selections").find("input:checkbox").each(function () { - $(this).prop("checked", false); - }); + $("#selections") + .find("input:checkbox") + .each(function() { + $(this).prop("checked", false); + }); $("#selections span").removeClass("selected"); - $("#plan td:nth-child(2),#plan th:nth-child(2)").nextUntil("th:nth-child("+(allCols-1)+"),td:nth-child("+(allCols-1)+")").hide(); // hide all but the last two columns + $("#plan td:nth-child(2),#plan th:nth-child(2)") + .nextUntil( + "th:nth-child(" + + (allCols - 1) + + "),td:nth-child(" + + (allCols - 1) + + ")" + ) + .hide(); // hide all but the last two columns $("#hideall").prop("checked", true).parent().addClass("selected"); } -$(document).ready(function(){ - $("#deletesupplier").on("click", function(){ - confirmDelete(__("Are you sure you want to delete this vendor?") ); +$(document).ready(function() { + $("#deletesupplier").on("click", function() { + confirmDelete(__("Are you sure you want to delete this vendor?")); }); }); -- 2.30.2