Jira Code: PS 4
This task is to calculate a Net profit on the sales order. We have to calculate total, COG, Estimated market fee, Shipping cost, Estimated packaging costs, NET Profit, NET Profit %. It would have to be after the fulfillment record is created, so when the status changes to pending billing.
User event script
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
* @ScriptID customscript_ps4_jj_ue_set_net_profit_so
* @SCriptTitle PS 4 JJ UE Set Net Profit in SO.js
* @Description This is to set Net Profit, %Net Profit, COG,Estimated marketPlace,Shipping cost,Packaging cost in SO
* @AppliedTO IF
* @CreatedBy AJ 15/12/2018
* @Modified AJ 19/12/2018 for can you change to use subtotal instead of total
* @Revised AJ 28/03/2019 Changed ID => custbody_channel to ==>custbody_fa_channel
* @RelatedTask PS-4
*/
define(['N/record', 'N/search'],
/**
* @param {record} record
* @param {search} search
*/
function(record, search) {
/**
* Function definition to be triggered before record is loaded.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {string} scriptContext.type - Trigger type
* @param {Form} scriptContext.form - Current form
* @Since 2015.2
*/
function beforeLoad(scriptContext) {
}
/**
* Function definition to be triggered before record is loaded.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type
* @Since 2015.2
*/
function beforeSubmit(scriptContext) {
}
/**
* Function definition to be triggered before record is loaded.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type
* @Since 2015.2
*/
function afterSubmit(scriptContext) {
try{
if(scriptContext.type=='create')
{
var totalWeight=0,totalCost=0,estimatedMarketPlace,shipCostSO;
// to get the current record
var IFrec = scriptContext.newRecord;
// to get the Created SO
var SO_ID = IFrec.getValue({
fieldId:'createdfrom'
});
// to get the shipping cost
var IFShipCost = IFrec.getValue({
fieldId:'shippingcost'
});
// to set COG
//load SO
var SORecord = record.load({
type:'salesorder',
id:SO_ID
});
// to get the status
var status = SORecord.getText({
fieldId:'status'
});
if(status=='Pending Billing')
{
// to get TOTAL ==>custbodymarketplacetotal
var Total = SORecord.getValue({
fieldId:'custbodymarketplacetotal'
});
/***
* AJ MODIFIED 19/12/2018
*/
var subTotal = SORecord.getValue({
fieldId:'subtotal'
});
/***
* AJ MODIFIED 19/12/2018 ENDS
*/
// to get total lineNum
var lineNum = SORecord.getLineCount({
sublistId:'item'
});
if(lineNum>0)
{
for(var i=0;i<lineNum;i++)
{
// to get the cost(Amount)
var lineAmount = SORecord.getSublistValue({
sublistId:'item',
fieldId:'amount',
line:i
});
// to get qty
var qty = SORecord.getSublistValue({
sublistId:'item',
fieldId:'quantity',
line:i
});
// to get item type
var itemType = SORecord.getSublistValue({
sublistId:'item',
fieldId:'itemtype',
line:i
});
// to get the item
var item = SORecord.getSublistValue({
sublistId:'item',
fieldId:'item',
line:i
});
// to calculte weight
// if item type is grp
if(itemType=='Group')
{
var SingleItem =getweightOfSingleQtyFromItemGRP(item);
}
else
{
//var weightOfSingleQty = getweightOfSingleQtyFromItem(item);
var SingleItem = getweightOfSingleQtyFromItem(item);
}
if(qty)
// calculate total weight
totalWeight = parseFloat(totalWeight)+parseFloat(SingleItem.weight*qty);
// to calculate total cost
if(SingleItem.cost)
totalCost=parseFloat(totalCost)+parseFloat(SingleItem.cost*qty);
}
}
// to set COG
SORecord.setValue({
fieldId:'custbodymarketplaceitemcost',
value:totalCost
});
// to set Estimated PAckaging cost
if(totalWeight>=1)
{
var estPackCost =0.75;
}
else
{
var estPackCost =0.25;
}
SORecord.setValue({
fieldId:'custbodymarketplaceboxfee',
value:estPackCost
});
// to get custbody_chanel
var channel = SORecord.getText({
fieldId:'custbody_fa_channel'
});
if(channel=='GrouponGoods'||channel=='GrouponGoods-Groupon-IPS')
{
// set shipping cost
shipCostSO=0;
SORecord.setValue({
fieldId:'custbodymarketplaceshipcost',
value:0
});
// to set Estimated Market place
estimatedMarketPlace= parseFloat(0.04 *subTotal);
}
else{
shipCostSO=IFShipCost;
// set shipping cost
SORecord.setValue({
fieldId:'custbodymarketplaceshipcost',
value:IFShipCost
});
if(channel=='Amazon'||channel=='eBay'||channel=='Jet'||channel=='Walmart')
{
estimatedMarketPlace= parseFloat(0.15*subTotal);
}
else if(channel=='Bluestem')
{
estimatedMarketPlace= parseFloat(0.03*subTotal);
}
}
// set Estimated MarketPlace
SORecord.setValue({
fieldId:'custbodymarketplacefees',
value:estimatedMarketPlace
});
// to calculate Net Profit
//Total-COG-Estimated PAckaging cost-shippingcost -Estimated Market place
var netProfit = parseFloat(checkif(subTotal))-parseFloat(checkif(totalCost))-parseFloat(checkif(estPackCost))-parseFloat(checkif(shipCostSO))-parseFloat(checkif(estimatedMarketPlace));
// to set netProfit
SORecord.setValue({
fieldId:'custbodymarketplaceprofit',
value:(netProfit).toFixed(2)
});
// % net Profit
if(subTotal!=0)
var percent_netProfit = parseFloat((checkif(netProfit*100))/subTotal);
else
var percent_netProfit = parseFloat((checkif(netProfit*100))/1);
SORecord.setValue({
fieldId:'custbodymarketplaceprofitpercent',
value:(percent_netProfit).toFixed(2)
});
// save record
SORecord.save();
}
}
}catch(e)
{
log.debug("Err@ FN afterSubmit ",e.message);
log.error("Err@ FN =",e);
}
}
/**
* To get weight of Item Grp
*/
function getweightOfSingleQtyFromItemGRP(item)
{
try{
var itemObj={};
itemObj.cost=0;
// to create the search
var itemgroupSearchObj = search.create({
type: "itemgroup",
filters:
[
["type","anyof","Group"],
"AND",
["internalidnumber","equalto",item]
],
columns:
[
search.createColumn({
name: "formulanumeric",
summary: "SUM",
formula: "{memberquantity}*{memberitem.weight}",
label: "Formula (Numeric)"
}),
search.createColumn({
name: "formulanumeric",
summary: "SUM",
formula: "{memberquantity}*{memberitem.cost}",
label: "Formula (Numeric)"
})
]
});
var searchResultCount = itemgroupSearchObj.runPaged().count;
var searchResult = itemgroupSearchObj.run().getRange({
start:0,
end:1
});
if(searchResultCount>0)
{
var columns = itemgroupSearchObj.columns;
itemObj.weight = searchResult[0].getValue({
name: "formulanumeric",
summary: "SUM",
formula: "{memberquantity}*{memberitem.weight}",
label: "Formula (Numeric)"
});
}
return itemObj;
}catch(e)
{
log.debug("Err@ FN getweightOfSingleQtyFromItemGRP ",e.message);
log.error("Err@ FN =",e);
}
}
/**
* To get the individual weight
*/
function getweightOfSingleQtyFromItem(item)
{
try{
var itemObj={};
// search for item weight
var itemSearchObj = search.create({
type: "item",
filters:
[
["internalidnumber","equalto",item]
],
columns:
[
search.createColumn({name: "weight", label: "Weight"}),
search.createColumn({name: "weightunit", label: "Weight Units"}),
search.createColumn({name: "cost", label: "Purchase Price"})
]
});
var searchResultCount = itemSearchObj.runPaged().count;
var searchResult = itemSearchObj.run().getRange({
start:0,
end:1
});
if(searchResultCount>0)
{
itemObj.weight = searchResult[0].getValue({
name: "weight"
});
itemObj.cost = searchResult[0].getValue({
name: "cost"
});
}
}catch(e)
{
log.debug("Err@ FN ",e.message);
log.error("Err@ FN =",e);
}
return itemObj;
}
function checkif(item) {
if (item == "" ||item == null || item == undefined || item =='- None -'||item =="-") {
return "0";
} else {
return item;
}
}
return {
beforeLoad: beforeLoad,
beforeSubmit: beforeSubmit,
afterSubmit: afterSubmit
};
});