Share ideas, ask questions, and get feedback about developing for ProcessMaker
Forum rules: Please post new questions under "Developing processes & programming" for ProcessMaker 2 or 3.
By azalea45
#784302
Dear All,

I have a stock issuing process. As part of the process the stock room does not always have the stock on hand. I created a simple script to calculate if there is a stock shortfall. Depending on if there is a shortfall, it will determine how the process continues. The items ordered are in a grid "Stock_Grid". The grid contains 4 fields "Module", "ConfirmedQuantity" which are brought in from the previous step. "PackedQuantity" is entered by the Stock Room Packer.

If there is no stock shortfall then the system must place a hyphen (-) in the text box. If there is a shortfall, the amount of the shortfall must be calculated and appear in the ShortPacked field. At the same time there is a hidden field called "SecretSum" that must be increased by the shortfall. "SecretSum" is the field that the form will use for routing.

Where did I go wrong? Irrespective of what I do this function doesn't seem to work
Code: Select all
function CalculateShortStock() {

  var nRows = Number_Rows_Grid("Stock_Grid", "Module");

  var Ssum = 0;

  for (var i = 1; i <= nRows; i++) {
    getGridField("Stock_Grid", i, "ShortPacked").value = 0;
    
    LineVal = parseFloat(getGridField("Stock_Grid", i, "ConfirmedQuantity").value) - parseFloat(getGridField("Stock_Grid", i, "PackedQuantity").value);
    
    getGridField("Stock_Grid", i, "ShortPacked").value = LineVal;
    Ssum += parseFloat(getGridField("Stock_Grid", i, "ConfirmedQuantity").value);
    If getGridField("Stock_Grid", i, "ShortPacked").value = 0 
      {
        getGridField("Stock_Grid", i, "ShortPacked").value = "-"
      }
  }
  getField("SecretSum").value = Ssum;

};
document.getElementById("Stock_Grid").onchange= CalculateShortStock; //execute when any value in the grid changes
CalculateShortStock(); //execute when DynaForm loads
Please help!!!!!!
By snosov
#784530
Ok. The first issue that jumps out at me is the way you set up onchange event -- it is incorrect. You will need to set up an event for each row on load. Then, if you allow add/delete rows, you must create/ change the onchange listener. Here is an example of my code I use for order packing where I an employee scans or types an item barcode and it gets added to a grid and then is validated against the order:
Code: Select all
function calculate() {
  var iRow = parseInt(this.id.match(/\]\[(\d+)\]\[/)[1]);
  var qty_unpacked  = parseInt(getGridField("OrderSummaryGrid", iRow, "qty_unpacked").value);
  var qty_in_box = parseInt(getGridField("OrderSummaryGrid", iRow, "qty_in_box").value);
  var qty_remaining = qty_unpacked - qty_in_box;
  
  if (qty_remaining < 0) {
    setFocusById("OrderSummaryGrid][" + iRow + "][qty_in_box");
    var row = getRow("OrderSummaryGrid][" + iRow + "][product_id");
    highlightRow(row, "red");
    G.highLight(getField("OrderSummaryGrid][" + iRow + "][qty_remaining"));
    //G.alert("You packed more books than ordered.", "Error");
  } else if (qty_remaining == 0) {
    setFocusById("product_bar_code");
    var row = getRow("OrderSummaryGrid][" + iRow + "][product_id");
    highlightRow(row, "green");
    getField("OrderSummaryGrid][" + iRow + "][qty_remaining").style.backgroundColor = "green"; 
  } else {
    setFocusById("product_bar_code");
    var row = getRow("OrderSummaryGrid][" + iRow + "][product_id");
    highlightRow(row, "yellow");
    getField("OrderSummaryGrid][" + iRow + "][qty_remaining").style.backgroundColor = "yellow"; 
  }
  getGridField("OrderSummaryGrid", iRow, "qty_remaining").value = qty_remaining;
  
}
//set event handler for all existing rows in the grid:
var totalRows = Number_Rows_Grid("OrderSummaryGrid", "product_id");
for (var i = 1; i <= totalRows; i++) {
   getGridField("OrderSummaryGrid", i, "qty_in_box").onchange = calculate;
}


getField("product_bar_code").onchange = processItem;


function processItem() { 
  var barCode = getValue(getField("product_bar_code"));
  var totalRows = Number_Rows_Grid("OrderSummaryGrid", "product_id");
  var found = false;
  //var gridBarCode = getGridField("ScanItemsGrid", iRow, "bar_code").value;
  //var gridItemQuantity = getGridField("OrderSummaryGrid", iRow, "item_quantity").value;
  
  var barCodePieces = barCode.split("-");
  var orderId = barCodePieces[0].toUpperCase() + "-" + barCodePieces[1].toUpperCase();
  var checkOrderId = getValue(getField("full_order_id"));
  var productId = barCodePieces[2].toUpperCase();
  
  if (checkOrderId != orderId) {
    G.alert("Product " + orderId + "-" + productId +" does not belong with order " + checkOrderId + ".", "Error");
    changeValue("product_bar_code", "");
    setFocusById("product_bar_code");
    //return 0;
  } else {
    for (var i = 1; i <= totalRows; i++) {
      if (getGridField("OrderSummaryGrid", i, "product_id").value == productId) {
        found = true;
        var iRow = i;
      }
    }
    
    if (!found) {
      G.alert("Product " + productId + " is not part of this order.", "Error");
      changeValue("product_bar_code", "");
      setFocusById("product_bar_code");
      //return 0;
    } else {
      //var qty_in_box = parseInt(getGridField("OrderSummaryGrid", iRow, "qty_in_box").value);
      //getGridField("OrderSummaryGrid", iRow, "qty_in_box").value = qty_in_box + 1;
      
      var row = getRow("OrderSummaryGrid][" + iRow + "][product_id");
      //highlightRow(row, "yellow");
      changeValue("product_bar_code", "");
      setFocusById("OrderSummaryGrid][" + iRow + "][qty_in_box");
    }
  }
}


var dynaformOnload = setOnLoad;

function setOnLoad() {
  calculate;
  var totalRows = Number_Rows_Grid("OrderSummaryGrid", "product_id");
  for (var iRow = 1; iRow <= totalRows; iRow++) {
    var qty_remaining = parseInt(getGridField("OrderSummaryGrid", iRow, "qty_remaining").value);
    if (qty_remaining > 0) {
      getField("OrderSummaryGrid][" + iRow + "][qty_remaining").style.backgroundColor = "yellow"; 
    } else if (qty_remaining == 0) {
      getField("OrderSummaryGrid][" + iRow + "][qty_remaining").style.backgroundColor = "green"; 
    } else {
      G.highLight(getField("OrderSummaryGrid][" + iRow + "][qty_remaining")); 
    }
  }
  changeValue("product_bar_code", "");
  setFocusById("OrderSummaryGrid][" + iRow + "][qty_in_box");
}

getField("product_bar_code").form.onsubmit = checkOnSubmit;
  
function checkOnSubmit() {
  var totalRows = Number_Rows_Grid("OrderSummaryGrid", "product_id");
  for (var iRow = 1; iRow <= totalRows; iRow++) {
    var qty_remaining = parseInt(getGridField("OrderSummaryGrid", iRow, "qty_remaining").value);
    if (qty_remaining < 0) {
      G.alert("You are trying to ship more than was ordered.", "Error");
      return false;
    } else {
      return true;
    }
  }
}
Notice this:
//set event handler for all existing rows in the grid:
var totalRows = Number_Rows_Grid("OrderSummaryGrid", "product_id");
for (var i = 1; i <= totalRows; i++) {
getGridField("OrderSummaryGrid", i, "qty_in_box").onchange = calculate;
}


This code will also give you a good idea how to manage presentation of the interface with row highlighting...

Enjoy

To take Cenforce 120mg tablet, follow the guidelin[…]

ICO software script is a pre-made program for crea[…]

A crypto exchange script is a pre-designed softwar[…]

So I recently bought an addmotor Ebike which of co[…]