Skip to content
Snippets Groups Projects
onDBUpdate.js 2.30 KiB
import("system.eMath");
import("system.entities");
import("system.vars");
import("system.db");
import("system.neon");
import("Order_lib");
import("Sql_lib");
    
//this process get's executed for every child of this orderItem since we use writeEntiy, so we use the param to make sure we don't execute it for the children
if(vars.getString("$param.IgnoreOnUpdateProcess_param") != "true")
{
    var newQuanitity = parseFloat(vars.get("$field.QUANTITY"));
    var oldQuantity = parseFloat(vars.get("$local.initialRowdata")["SALESORDERITEM.QUANTITY"]);
    var orderItemId = vars.get("$field.SALESORDERITEMID");
    if(newQuanitity != oldQuantity) //quantity changed -> change quantities of the childitems accordingly
    {
        var multiplier = newQuanitity/oldQuantity;

        var loadConfig = entities.createConfigForLoadingRows().entity("Orderitem_entity").addParameter("OrderId_param", vars.get("$field.SALESORDER_ID")).fields(["SALESORDERITEMID", "ASSIGNEDTO", "PRODUCT_ID", "QUANTITY"])

        var rows = entities.getRows(loadConfig);
        var potentialAsignees = {};
        var orderItemsToUpdate = {};
        var statements = [];
        var stop = false;
        while(stop == false)//we have too loop for all the rows for each row that needs updating, since those are also pontially asignees
        {
            stop = true;
            for(var orderitem in rows)//loop trough all the rows and build orderItemsToUpdate
            {
                if(!(rows[orderitem]["SALESORDERITEMID"] in orderItemsToUpdate) &&(rows[orderitem]["ASSIGNEDTO"] == orderItemId || rows[orderitem]["ASSIGNEDTO"] in potentialAsignees))
                {
                    statements.push(
                        newWhere("SALESORDERITEM.SALESORDERITEMID", rows[orderitem]["SALESORDERITEMID"]).buildUpdateStatement({
                            "QUANTITY": parseInt(rows[orderitem]["QUANTITY"])*multiplier
                        })
                    );
                    orderItemsToUpdate[rows[orderitem]["SALESORDERITEMID"]] = parseInt(rows[orderitem]["QUANTITY"])*multiplier;
                    potentialAsignees[rows[orderitem]["SALESORDERITEMID"]] = "";
                        
                    stop = false;
                }
            }
        }
        db.execute(statements);// no write entity -> performance reason
    }
}