Skip to content
Snippets Groups Projects
Commit 22e4ff88 authored by Sebastian Pongratz's avatar Sebastian Pongratz :ping_pong:
Browse files

Merge branch 'sales_2001136_Prod2ProdCalculatePricesFromPartsList' into '2021.2'

Sales 2001136 prod2 prod calculate prices from parts list

See merge request xrm/basic!1506
parents dad8adb6 e56174de
No related branches found
No related tags found
No related merge requests found
......@@ -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>
......
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
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment