It comprises all the actions defined for the quotation(USER EVENT) disabling the sub tabs if the status is order lost or cancel or if any sales order is created from the quotation and Not Administrator Role. Added different button based on the different conditions. Disabling the increment field based on some conditions.
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
/**
* Script Description: This script is for setting a button in the Quotation record
*/
/*******************************************************************************
* * UMAR WSR * *
* **************************************************************************
* Date:23/2/18
* Script name: NetU UE Quote Btns (Corresponding client is Netu_cs Quote_btn_Action)
* Script id: customscript_netu_ue_quote_btns
* Deployment id: customdeploy_netu_ue_quote_btns
* Applied to: Quotation
* Revised on 31/5/18 for add a new button quotation button when profit greater than zero.
* Revised on 4/6/18 for disabling increment field based on some conditions.
* Revised on 7/6/18 for canceling the other sub-jobs with a reason code
******************************************************************************/
define(['N/record', 'N/ui/serverWidget', 'N/search', 'N/runtime', 'N/format', 'N/currency', 'N/url', 'N/https'],
function(record, serverWidget, search, runtime, format, currency, url, https) {
/**
* 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) {
try {
var CustRec = scriptContext.form;
//Getting record id
var id = scriptContext.newRecord.id; //scriptContext.request.parameters.id;
var quotedtls = search.lookupFields({
type: record.Type.ESTIMATE,
id: id,
columns: ['custbody_netu_subjob_status', 'customform']
});
var quoteSt = null;
if (quotedtls.custbody_netu_subjob_status != null) {
quoteSt = quotedtls.custbody_netu_subjob_status[0].value;
}
//Getting The role of current User
var userRole = runtime.getCurrentUser().role;
//Set a button and Provide the action for the button by calling the function in client script
if (scriptContext.type == 'view') {
//Loading Client Script
CustRec.clientScriptFileId = 1700;
//Getting the Record Id
var recId = scriptContext.request.parameters.id;
var quotationDtls = search.lookupFields({
type: record.Type.ESTIMATE,
id: recId,
columns: ['custbody_netu_subjob_status', 'custbody_netu_offer_sent_date', 'custbody_netu_confirmation_date', 'custbody_netu_profit_amount', 'custbody_netu_offer_sent_date']
});
var st = null;
if (quotationDtls.custbody_netu_profit_amount != null) {
profit = quotationDtls.custbody_netu_profit_amount;
if (profit > 0) {
var quotationBtn = CustRec.addButton({
id: 'custpage_customer_quotation_btn',
label: 'Quotation',
functionName: 'sendQuotation'
});
}
}
var offerSend=null;
var confirmDate=null;
if (quotationDtls.custbody_netu_offer_sent_date != null) {
offerSend = quotationDtls.custbody_netu_offer_sent_date;
}
if (quotationDtls.custbody_netu_confirmation_date != null) {
confirmDate = quotationDtls.custbody_netu_confirmation_date;
}
if (quotationDtls.custbody_netu_subjob_status != null) {
st = quotationDtls.custbody_netu_subjob_status[0].value;
}
var buttonCOC = CustRec.addButton({
id: 'custpage_customer_confirm_btn',
label: 'Customer Order Confirmed',
functionName: 'CustomerOrderConfirmed'
});
buttonCOC.isDisabled = true;
//Button is disabled if status is Quotation
if ((st == 5) && (offerSend != '') && ((confirmDate == '') || (confirmDate < offerSend))) {
buttonCOC.isDisabled = false;
}
if(confirmDate)
{
quotationBtn.isDisabled = true;
}
//Searching for any Open SO for this quotation
var salesorderSearchObj = search.create({
type: "salesorder",
filters: [
["type", "anyof", "SalesOrd"],
"AND",
["status", "noneof", "SalesOrd:H"],
"AND",
["createdfrom", "anyof", recId],
"AND",
["mainline", "is", "T"]
],
columns: [
"mainline",
search.createColumn({
name: "trandate",
sort: search.Sort.ASC
}),
"type",
"tranid",
"entity"
]
});
var searchResultCount = salesorderSearchObj.runPaged().count;
if ((searchResultCount > 0) || (confirmDate == '')) {
CustRec.removeButton({
id: 'createsalesord'
});
}
//remove the buttons if the status is subjob cancelled or unable to quote or order lost.
if (((quoteSt == 10) || (quoteSt == 4) || (quoteSt == 20))) {
buttonCOC.isDisabled = true;
CustRec.removeButton({
id: 'createsalesord'
});
CustRec.removeButton({
id: 'createcashsale'
});
CustRec.removeButton({
id: 'createinvoice'
});
}
var cancelSubjob = CustRec.addButton({
id: 'custpage_cancel_sunbjob_btn',
label: 'Cancel subjob',
functionName: 'cancelSubjob'
});
}
if (scriptContext.type == scriptContext.UserEventType.EDIT) {
//Loading Client Script
CustRec.clientScriptFileId = 1700;
var currentReco = scriptContext.newRecord;
var id = currentReco.id;
//Searching for any Open SO for this quotation
var salesorderSearchObj = search.create({
type: "salesorder",
filters: [
["type", "anyof", "SalesOrd"],
"AND",
["createdfrom", "anyof", id],
"AND",
["mainline", "is", "T"]
],
columns: [
"mainline",
search.createColumn({
name: "trandate",
sort: search.Sort.ASC
}),
"type",
"tranid",
"entity"
]
});
var searchResultCount = salesorderSearchObj.runPaged().count;
//removing edit button on anothor roles once a SO is created
if (userRole != 3) {
if ((searchResultCount > 0) || ((quoteSt == 10) || (quoteSt == 4) || (quoteSt == 20))) {
//disabling the sub tabs if the status is order lost or cancel or if any sales order is craeted from the quotatation and Not Administrator Role.
var jobField1 = CustRec.addField({
id: 'custpage_jobid',
type: 'INLINEHTML',
label: 'Job Id'
});
var html = "<script>jQuery( document ).ready(function() { jQuery('#div__body').css('pointer-events', 'none'); });</script>"
jobField1.defaultValue = html;
}
}
var getComm = currentReco.getText({
fieldId: 'custbody_netu_location_principal'
});
if (!getComm) {
var vendor = currentReco.getValue({
fieldId: 'custbody_netu_vendor'
});
//checking the vendor principle location
var principleLocation = search.lookupFields({
type: search.Type.VENDOR,
id: vendor,
columns: ['custentity_netu_principal_location', 'custentity_netu_commission_type', 'custentity_netu_discount_perc']
});
//getting the Vendor Discount
var vendid = principleLocation.custentity_netu_discount_perc;
var vendor_discount = null;
if ((vendid == "") || (vendid == null) || (vendid == undefined)) {
var alertDisc = 'Vendor discount is not defined';
} else {
var vendper = vendid.split("%");
vendor_discount = vendper[0];
}
//Getting the Location of main Job
var mainjobLocation = search.lookupFields({
type: record.Type.ESTIMATE,
id: id,
columns: ['location']
});
var alertMainLocation = '';
var alertVendorLocation = '';
var locationFlag = 0;
var fixCommission = null;
var jobLocationDtls = mainjobLocation.location;
var mainLocation = null;
if (jobLocationDtls == null || jobLocationDtls == "" || jobLocationDtls == undefined) {
alertMainLocation = 'Location is not defined';
} else {
//log.debug('jobLocationDtls',jobLocationDtls);
mainLocation = jobLocationDtls[0].value;
var location = principleLocation.custentity_netu_principal_location[0];
var prinLocation = null;
if (location == null || location == "" || location == undefined) {
alertVendorLocation = 'Principal location is not defined for the vendor at the time of creation';
} else {
//log.debug('principleLocation11',location);
var length = principleLocation.custentity_netu_principal_location.length;
//Checking whether the Vendor location and Main job location are same
for (var k = 0; k < length; k++) {
prinLocation = principleLocation.custentity_netu_principal_location[k].value;
if (mainLocation == prinLocation) {
locationFlag = 1;
log.debug('locationFlag', locationFlag);
break;
}
}
//If locations are same gettimg the Commission
if (locationFlag == 1) {
//getting the current class
var itemClass = currentReco.getValue({
fieldId: 'class'
});
//Searching the vendor commission from the vendor budget and fixed commission list.
var custrecord_netu_vendor_class_commissionObj = search.create({
type: "customrecord_netu_vendor_budget",
filters: [
["custrecord_netu_class", "anyof", itemClass],
"AND",
["custrecord_netu_vendor_budget_record", "anyof", vendor]
],
columns: [
"custrecord_netu_vendor_class_commission"
]
});
var vendorCommissionResultCount = custrecord_netu_vendor_class_commissionObj.runPaged().count;
log.debug('vendorCommissionResultCount', vendorCommissionResultCount);
if (vendorCommissionResultCount != 0) {
var vendorFixedResults = custrecord_netu_vendor_class_commissionObj.run().getRange({ //start:0,
start: 0,
end: 1
});
log.debug('vendorFixedResults', vendorFixedResults);
fixedCommi = vendorFixedResults[0].getValue({
name: 'custrecord_netu_vendor_class_commission'
});
var fixedCom = fixedCommi.split("%");
fixCommission = fixedCom[0];
log.debug('fixCommission', fixCommission);
}
}
}
}
var commObj = '{ "vendor_discount" : ' + vendor_discount + ',"alertMainLocation" : "' + alertMainLocation + '","alertVendorLocation" : "' + alertVendorLocation + '","locationFlag" : ' + locationFlag + ',"fixCommission" : ' + fixCommission + '}';
log.debug('commObj', commObj);
currentReco.setText({
fieldId: 'custbody_netu_location_principal',
text: commObj
});
}
}
var currRecord = scriptContext.newRecord;
var subjob = currRecord.getValue({
fieldId: 'custbody_netu_subjob_type'
});
var CustomRecord = scriptContext.form;
// refer to the client script Quote UI
CustomRecord.clientScriptFileId = 1700;
if ((scriptContext.type == 'edit') && (subjob == 1)) {
// to add button for add optional/free/ additional item
CustomRecord.addButton({
id: 'custpage_button',
label: 'Set optional items',
functionName: 'addOptional'
});
}
} catch (e) {
log.debug({
title: e.name,
details: e.message
});
}
}
function beforeSubmit(scriptContext) {
try {
if (scriptContext.type == 'edit') {
var currentRec = scriptContext.newRecord;
var vendCurrency = currentRec.getText({
fieldId: 'currency'
});
var defCurrency = currentRec.getText({
fieldId: 'custbody_netu_default_currency'
});
var customer = currentRec.getValue({
fieldId: 'entity'
});
if (defCurrency == '' || defCurrency == null || defCurrency == undefined) {} else {
var custResult = search.lookupFields({
type: search.Type.CUSTOMER,
id: customer,
columns: ['custentity_netu_equivalent_amount']
});
var equiAmnt = custResult.custentity_netu_equivalent_amount;
if ((equiAmnt) && (defCurrency != vendCurrency)) {
var netAmnt = currentRec.getValue({
fieldId: 'custbody_netu_sales_net_amnt_charges_b'
});
var today = new Date();
var athens = format.format({ value: today, type: format.Type.DATETIME, timezone: format.Timezone.EUROPE_ISTANBUL })
var todayDate = athens.split(" ");
var currentdate = todayDate[0];
var excRate = currency.exchangeRate({
source: 'EUR',
target: defCurrency,
date: new Date(currentdate)
//date: currentdate
});
var custTotal = parseFloat(excRate) * parseFloat(netAmnt);
var custTotalFix = custTotal.toFixed(2);
currentRec.setValue({
fieldId: 'custbody_netu_default_curr_exch_rate',
value: excRate
});
currentRec.setValue({
fieldId: 'custbody_default_currency_total',
value: custTotalFix
});
} else {
currentRec.setValue({
fieldId: 'custbody_default_currency_total',
value: null
});
}
}
}
} catch (e) {
log.debug({
title: e.name,
details: e.message
});
}
}
function afterSubmit(scriptContext) {
try {
var st = 20;
//adding discount,freight,packing as line item for the GLimpact
var objRecord = scriptContext.newRecord;
var id = objRecord.id;
var base_gross_amnt = objRecord.getValue({
fieldId: 'custbody_netu_cost_gross_amount_base'
});
if ((base_gross_amnt != "") && (base_gross_amnt != null) && (base_gross_amnt != undefined)) {
var currentRec = record.load({
type: record.Type.ESTIMATE,
id: id,
isDynamic: true,
});
//getting the line count
var count = currentRec.getLineCount({
sublistId: 'item'
});
//getting the taxcode
var taxCode = currentRec.getSublistValue({
sublistId: 'item',
fieldId: 'taxcode',
line: 0
});
var getFields = ['custbody_netu_sales_discount_amount', 'custbody_netu_freight', 'custbody_netu_packing_charges',
'custbody_netu_cost_discount_amount', 'custbody_netu_v_freight', 'custbody_netu_v_packing_charges'
];
var setValues = [];
for (var k = 0; k < 6; k++) {
setValues[k] = currentRec.getValue({
fieldId: getFields[k]
});
setValues[k] = checkif(setValues[k]);
}
var salesValues = [-setValues[0], setValues[1], setValues[2]];
var purchaseValues = [-setValues[3], setValues[4], setValues[5]];
log.debug({
title: 'salesValues',
details: salesValues + ',' + purchaseValues
});
//checking weather any item line has an income id.
for (var n = 0; n < count; n++) {
var itemidIncome = currentRec.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_netu_incomeid',
line: n
});
if (itemidIncome == '3') {
var countGl = 1;
}
}
if (!countGl) {
var classtyp = currentRec.getValue({
fieldId: 'class'
});
log.debug('classtyp',classtyp);
var customrecord_netu_class_revenue_mapSearchObj = search.create({
type: "customrecord_netu_class_revenue_map",
filters: [
[["custrecord_netu_packing","is","T"],"OR",["custrecord_netu_freight","is","T"],"OR",["custrecord_netu_customer_discount","is","T"]],
"AND",
["custrecord_netu_map_class.internalidnumber","equalto",classtyp]
],
columns: [
search.createColumn({ name: "internalid", label: "Internal ID" }),
search.createColumn({ name: "custrecord_netu_item_code", label: "Item Code" }),
search.createColumn({
name: "custrecord_netu_packing",
sort: search.Sort.ASC,
label: "Packing"
}),
search.createColumn({
name: "custrecord_netu_freight",
sort: search.Sort.ASC,
label: "Freight"
}),
search.createColumn({
name: "custrecord_netu_customer_discount",
sort: search.Sort.ASC,
label: "Customer Discount"
}),
search.createColumn({
name: "internalid",
join: "CUSTRECORD_NETU_ITEM_CODE",
label: "Internal ID"
})
]
});
var classRevenueCountin = customrecord_netu_class_revenue_mapSearchObj.runPaged().count;
log.debug('classRevenueCountin',classRevenueCountin);
if (classRevenueCountin > 0) {
var classRevenueCountinResults = customrecord_netu_class_revenue_mapSearchObj.run().getRange({ //start:0,
start: 0,
end: 5
});
}
//console.log('classRevenueCountin',classRevenueCountin);
var incomeAcctids = [];
//var incomeDisplayName=[];
for (var j = 0; j < classRevenueCountin; j++) {
incomeAcctidValues = classRevenueCountinResults[j].getValue({
name: "internalid",
join: "CUSTRECORD_NETU_ITEM_CODE"
});
incomeAcctids.push(incomeAcctidValues);
}
var k = 3;
for (var i = count; i < count + 3; i++) {
k--;
var line = currentRec.insertLine({
sublistId: 'item',
line: i
});
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: parseFloat(incomeAcctids[k]),
ignoreFieldChange: true
});
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'amount',
value: salesValues[k],
ignoreFieldChange: true
});
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_netu_purch_price',
value: 0,
ignoreFieldChange: true
});
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_netu_purch_gross_amount',
value: purchaseValues[k],
ignoreFieldChange: true
});
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'taxcode',
value: taxCode,
ignoreFieldChange: true
});
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_netu_incomeid',
value: k + 1,
ignoreFieldChange: true
});
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: null,
ignoreFieldChange: true
});
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'units',
value: null,
ignoreFieldChange: true
});
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_netu_incomeid',
value: k + 1,
ignoreFieldChange: true
});
currentRec.commitLine({
sublistId: 'item'
});
}
var recordId = currentRec.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
}
}
} catch (e) {
log.debug({
title: e.name,
details: e.message
});
}
}
function checkif(singleitem) {
if (singleitem == "" || singleitem == null || singleitem == undefined) {
return 0;
} else {
return singleitem;
}
}
return {
beforeLoad: beforeLoad,
beforeSubmit: beforeSubmit,
afterSubmit: afterSubmit
};
});