diff --git a/entity/Prod2prod_entity/Prod2prod_entity.aod b/entity/Prod2prod_entity/Prod2prod_entity.aod index 2cc4ae001df0fc3f0c49ca346d6ff4fdab3b273c..ead47cfc2b7ce1a9f304350248ad64201065a2a0 100644 --- a/entity/Prod2prod_entity/Prod2prod_entity.aod +++ b/entity/Prod2prod_entity/Prod2prod_entity.aod @@ -124,16 +124,6 @@ <title>Unit</title> <state>READONLY</state> </entityField> - <entityField> - <name>currentPurchasePrice</name> - <title>Curr. purchase price</title> - <state>READONLY</state> - </entityField> - <entityField> - <name>currentSalesPrice</name> - <title>Curr. sales price</title> - <state>READONLY</state> - </entityField> <entityField> <name>PARENT_ID</name> </entityField> @@ -144,6 +134,16 @@ <name>PICTURE</name> <contentType>IMAGE</contentType> </entityField> + <entityField> + <name>currentSalesPrice</name> + <title>Curr. sales price</title> + <state>READONLY</state> + </entityField> + <entityField> + <name>currentPurchasePrice</name> + <title>Curr. purchase price</title> + <state>READONLY</state> + </entityField> </entityFields> <recordContainers> <jDitoRecordContainer> diff --git a/entity/Prod2prod_entity/entityfields/source_id/onValueChange.js b/entity/Prod2prod_entity/entityfields/source_id/onValueChange.js index 498192c6dc9e096ec2f225e2c5bad1234bc17305..6b7340b99f92da214cf9f70f511e441c2d15ec55 100644 --- a/entity/Prod2prod_entity/entityfields/source_id/onValueChange.js +++ b/entity/Prod2prod_entity/entityfields/source_id/onValueChange.js @@ -1,3 +1,5 @@ +import("system.eMath"); +import("Util_lib"); import("system.text"); import("Product_lib"); import("KeywordRegistry_basic"); @@ -6,25 +8,187 @@ import("Sql_lib"); import("system.vars"); import("system.neon"); -var productId = vars.get("$local.value"); +if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW || vars.get("$sys.recordstate") == neon.OPERATINGSTATE_EDIT) +{ + var buyPrice = ""; + var sellPrice = ""; + var productId = vars.get("$local.value"); + + var unit = newSelect(KeywordUtils.getResolvedTitleSqlPart($KeywordRegistry.quantityUnit(), "PRODUCT.UNIT")) + .from("PRODUCT") + .where("PRODUCT.PRODUCTID", productId) + .cell(); + var data = ""; + var purchasePrice = ProductUtils.getCurrentProductPrice(productId, "PP", true); + if (purchasePrice.length > 0) + { + purchasePrice = text.formatDouble(purchasePrice[0], "#,##0.00", true) + " " + purchasePrice[1]; + } + else + { + let calcPrices = _calcPrices(); + buyPrice = calcPrices["buyPrice"]; + sellPrice = calcPrices["sellPrice"]; + purchasePrice = buyPrice; + } -var unit = newSelect(KeywordUtils.getResolvedTitleSqlPart($KeywordRegistry.quantityUnit(), "PRODUCT.UNIT")) - .from("PRODUCT") - .where("PRODUCT.PRODUCTID", productId) - .cell(); + var salesPrice = ProductUtils.getCurrentProductPrice(productId, "SP", true); + if (salesPrice.length > 0) + { + salesPrice = text.formatDouble(salesPrice[0], "#,##0.00", true) + " " + salesPrice[1]; + } + else + { + if(Utils.isNullOrEmptyString(data)) + { + let calcPrices = _calcPrices(); + buyPrice = calcPrices["buyPrice"]; + sellPrice = calcPrices["sellPrice"]; + } + salesPrice = sellPrice; + } + + neon.setFieldValue("$field.unit", unit); + neon.setFieldValue("$field.currentPurchasePrice", purchasePrice); + neon.setFieldValue("$field.currentSalesPrice", salesPrice); +} -var purchasePrice = ProductUtils.getCurrentProductPrice(productId, "PP", true); -if (purchasePrice.length > 0) +/* +* Recursive function that retuns the childs of a product parts list +* +* @param {String} pId - uid value +* +* @param {String} pProductId product id +* +* @param {Integer} pMaxRecursion max recursion +* +* @return {Array} Childs + */ +function _getChilds(pId, pProductId, pMaxRecursion) { - purchasePrice = text.formatDouble(purchasePrice[0], "#,##0.00", true) + " " + purchasePrice[1]; + if(pMaxRecursion == 0) + { + return []; + } + var items = prod2prod.filter(function(p2p) { + return p2p[5] == pProductId; + }); + var itemChilds = []; + items.forEach(function(p2p) { + p2p[2] = pId; + itemChilds.push(_getChilds(p2p[0], p2p[3], pMaxRecursion - 1)); + }); + return Array.prototype.concat.apply(items, itemChilds); } -var salesPrice = ProductUtils.getCurrentProductPrice(productId, "SP", true); -if (salesPrice.length > 0) +/* +* Caclulates the prace of pProdId by adding up the child prices +* +* @param {String} pProdId - uid value +* +* @param {String} pProductAndChildProducts product id +* +* @param {Integer} pPpSp buy/sell possible values: "PP", "SP" +* +* @return {String} Price + */ +function _calculatePriceFromChildren(pProdId, pProductAndChildProducts, pPpSp) { - salesPrice = text.formatDouble(salesPrice[0], "#,##0.00", true) + " " + salesPrice[1]; + var children = pProductAndChildProducts[pProdId]; + var price = ""; + if(children != undefined) + { + for (let i = 0; i < children.length; i++) + { + let childProdId = children[i]; + childPrice = productPrices[childProdId][pPpSp]; + if(childPrice && Utils.isNotNullOrEmptyString(childPrice)) + { + + if(price == "") + { + price = 0; + } + price = eMath.addDec(price, childPrice); + } + } + } + return price; } -neon.setFieldValue("$field.unit", unit); -neon.setFieldValue("$field.currentPurchasePrice", purchasePrice); -neon.setFieldValue("$field.currentSalesPrice", salesPrice); \ No newline at end of file +/* +* Caclulates the buy and sales prices for the current product +* +* @return {Object} Object with buyPrice and sellPrice. + */ +function _calcPrices() +{ + var sqlMask = new SqlMaskingUtils(); + prod2prod = newSelect([ + "PROD2PROD.PROD2PRODID", // UID.value + "PROD2PROD.PROD2PRODID", // PROD2PRODID.value + "''", // PARENT_ID.value + "PROD2PROD.SOURCE_ID", // SOURCE_ID.value + "PRODUCT.PRODUCTNAME", // SOURCE_ID.displayValue + "PROD2PROD.DEST_ID", // DEST_ID.value + ]).from("PROD2PROD") + .join("PRODUCT", "PRODUCT.PRODUCTID = PROD2PROD.SOURCE_ID") + .where("PROD2PROD.SOURCE_ID", vars.get("$local.value")) + .or("PROD2PROD.DEST_ID", vars.get("$local.value")) + .table(); + + data = _getChilds(null, vars.get("$local.value"), 20) + var productAndChildProducts = {}; + var productPrices = {}; + + for(var i = data.length - 1; i >= 0; i--)//loop backwards + { + var productId = data[i][3]; + var prodId = data[i][0]; + var parentId = data[i][2]; + + if(!productAndChildProducts.hasOwnProperty(parentId)) + { + productAndChildProducts[parentId] = [prodId]; + } + else + { + productAndChildProducts[parentId].push(prodId); + } + + productPrices[prodId] = {}; + + var purchasePrice = ProductUtils.getCurrentProductPrice(productId, "PP", true); + productPrices[prodId]["PP"] = purchasePrice.length == 0 ? _calculatePriceFromChildren(prodId, productAndChildProducts, "PP") : purchasePrice[0]; + + var salesPrice = ProductUtils.getCurrentProductPrice(productId, "SP", true); + productPrices[prodId]["SP"] = salesPrice.length == 0 ? _calculatePriceFromChildren(prodId, productAndChildProducts, "SP") : salesPrice[0]; + + } + + for (i = 0; i < productAndChildProducts[null].length; i++) + { + var currentProdId = productAndChildProducts[null][i]; + + if(Utils.isNotNullOrEmptyString(productPrices[currentProdId]["PP"])) + { + buyPrice = eMath.addDec(buyPrice, productPrices[currentProdId]["PP"]); + } + if(Utils.isNotNullOrEmptyString(productPrices[currentProdId]["SP"])) + { + sellPrice = eMath.addDec(sellPrice, productPrices[currentProdId]["SP"]); + } + } + buyPrice =( + Utils.isNullOrEmptyString(buyPrice) ? "" : + text.formatDouble(buyPrice, "#,##0.00", true) + " " + "Euro" + ); + + sellPrice =( + Utils.isNullOrEmptyString(sellPrice) ? "" : + text.formatDouble(sellPrice, "#,##0.00", true) + " " + "Euro" + ); + + return {"buyPrice": buyPrice + , "sellPrice": sellPrice}; +} \ No newline at end of file diff --git a/entity/Prod2prod_entity/recordcontainers/jdito/contentProcess.js b/entity/Prod2prod_entity/recordcontainers/jdito/contentProcess.js index 9c60cc28a8c3905a4c3419520d44bdb5ac1ea277..42e34ece3d422f06fee8f846dc94ebeb54cb88e4 100644 --- a/entity/Prod2prod_entity/recordcontainers/jdito/contentProcess.js +++ b/entity/Prod2prod_entity/recordcontainers/jdito/contentProcess.js @@ -1,3 +1,5 @@ +import("system.eMath"); +import("Util_lib"); import("Product_lib"); import("system.text"); import("system.neon"); @@ -31,23 +33,68 @@ var query = newSelect([ .join("PRODUCT", "PRODUCT.PRODUCTID = PROD2PROD.SOURCE_ID"); function _returnData(data) { - for(let i = 0; i < data.length; i++) + + var productAndChildProducts = {}; + var productPrices = {}; + function _calculatePriceFromChildren(pProdId, pParentId, pPpSp) + { + var children = productAndChildProducts[pProdId]; + var price = ""; + if(children != undefined) + { + for (let i = 0; i < children.length; i++) + { + let childProdId = children[i]; + childPrice = productPrices[childProdId][pPpSp]; + if(childPrice && Utils.isNotNullOrEmptyString(childPrice)) + { + + if(price == "") + { + price = 0; + } + price = eMath.addDec(price, childPrice); + } + } + } + return price; + } + + for(var i = data.length - 1; i >= 0; i--)//loop backwards { var productId = data[i][3]; + var prodId = data[i][0]; + var parentId = data[i][2]; + + if(!productAndChildProducts.hasOwnProperty(parentId)) + { + productAndChildProducts[parentId] = [prodId]; + } + else + { + productAndChildProducts[parentId].push(prodId); + } + + productPrices[prodId] = {}; - // currentPurchasePrice.value var purchasePrice = ProductUtils.getCurrentProductPrice(productId, "PP", true); + productPrices[prodId]["PP"] = purchasePrice.length == 0 ? _calculatePriceFromChildren(prodId, parentId, "PP") : purchasePrice[0]; + + var salesPrice = ProductUtils.getCurrentProductPrice(productId, "SP", true); + productPrices[prodId]["SP"] = salesPrice.length == 0 ? _calculatePriceFromChildren(prodId, parentId, "SP") : salesPrice[0]; + + // currentPurchasePrice.value data[i].push( - purchasePrice.length == 0 ? "" : - text.formatDouble(purchasePrice[0], "#,##0.00", true) + " " + purchasePrice[1] + Utils.isNullOrEmptyString(productPrices[prodId]["PP"]) ? "" : + text.formatDouble(productPrices[prodId]["PP"], "#,##0.00", true) + " " + "Euro" ); // currentSalesPrice.value - var salesPrice = ProductUtils.getCurrentProductPrice(productId, "SP", true); data[i].push( - salesPrice.length == 0 ? "" : - text.formatDouble(salesPrice[0], "#,##0.00", true) + " " + salesPrice[1] + Utils.isNullOrEmptyString(productPrices[prodId]["SP"]) ? "" : + text.formatDouble(productPrices[prodId]["SP"], "#,##0.00", true) + " " + "Euro" ); + } result.object(data); } @@ -76,4 +123,4 @@ else return Array.prototype.concat.apply(items, itemChilds); } _returnData(_getChilds(null, vars.get("$param.ProductId_param"), 20)); -} +} \ No newline at end of file