David Durst
05/02/2019, 8:43 AMfunction ItemCostData(itemid, cost, margin) {
this.itemid = itemid;
this.cost = cost;
this.margin = margin;
this.price = function() {
return (this.cost/((100-this.margin)/100)).toFixed(3);
};
}
function map(context) {
try {
var data = JSON.parse(context.value);
var ispreferredvendor = data.values['formulanumeric'];
if (ispreferredvendor == '1') {
var vendorcost = data.values["vendorcost"];
var group = data.values["custitem_price_matrix_group"].value;
var costdata = new ItemCostData(
data.id,
vendorcost,
getMargin(group, vendorcost)
);
log.debug({'title':'Map Debug', 'details': 'Item ID: '+costdata.itemid+' Cost: '+costdata.cost+' Margin: '+costdata.margin})
context.write(data.id, costdata);
}
} catch(ex) {
log.error({ title: 'map: error mapping data', details: ex });
}
}
function reduce(context) {
try {
log.debug({'title':'Context Value','details': context.value})
var costdata = JSON.parse(context.value);
log.debug({title: 'ItemId: '+costdata.itemid, details: 'Margin: '+costdata.margin+' Vendor Cost: '+costdata.cost+' Price: '+costdata.price()});
context.write(costdata.itemid, setItemPrice(costdata));
} catch(ex) {
log.error({ title: 'map: error mapping data', details: ex });
}
}