/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
* Version: 1.0
*/
define(['N/record', 'N/search', 'N/ui/serverWidget', 'N/runtime'],
/**
* @param {record} record
*/
function (record, search, serverWidget, runtime) {
const _PAY_OPT_PAYPAL = "225";
const _PAY_OPT_CHECK = "2";
const _PAY_OPT_WIRE_TRANSFER = "243";
const _TERMS_AMAZON = "7";
const _TERMS_WALMART = "8"
const _TERMS_CHECK_OVER_UNDER_PAYMENT = "9";
const _CASH_SALE = "Cash Sale";
const _UNAPPROVED_PAYMENT = "Unapproved Payment";
const _DEPOSITED = "Deposited";
const _NOT_DEPOSITED = "Not Deposited";
const _CC_NOT_PAID = "1";
const _PAYPAL = "2";
const _CHECK_NOT_PAID = "3";
const _WIRE_TRANSFER_NOT_PAID = "4";
const _AMAZON = "5";
const _WALMART = "6";
const _PAID_BY_CHECK = "7";
const _REFUND_AMAZON = "8";
const _REFUND_WALMART = "9";
const _REFUNDED = "10";
const _REFUND_PAYPAL = "11";
const _CANCELED = "12";
const _CC_PROCESSING_ERROR = "13";
const _PAYPAL_PAID = "14";
const _PAID_BY_WIRE_TRANSFER = "15";
//const _PAID_BY_CHECK = "16";
const _PAID_BY_CC = "17";
const _REFUND = "18"
const _PARTIAL_REFUND = "19"
const RETURN_REASONS = ["24", "19", "16", "36", "37"];
const ROLE_BF_CSREP = 1038
const ROLE_BF_CSTRAINEE = 1070
function beforeLoad(context) {
try {
var objUser = runtime.getCurrentUser();
var intRole = objUser.role
var form = context.form
if (intRole != ROLE_BF_CSREP && intRole != ROLE_BF_CSTRAINEE) return
var objOriginalTerms = form.getField({
id: 'terms'
})
var arrTermOptions = objOriginalTerms.getSelectOptions()
var objTerms = form.addField({
id: 'custpage_cd_terms',
label: 'Terms',
type: 'select',
container: 'billingtab'
}).updateLayoutType({
layoutType: serverWidget.FieldLayoutType.OUTSIDE
});
objTerms.addSelectOption({
value: '',
text: ''
})
arrTermOptions.forEach(function (element) {
if (element.text != 'Amazon' && element.text != 'WalMart') {
objTerms.addSelectOption({
value: element.value,
text: element.text
})
}
})
objOriginalTerms.updateDisplayType({
displayType: 'hidden'
})
} catch (e) {
log.error('Error', e)
}
}
function afterSubmit(context) {
try {
if (context.type === context.UserEventType.CREATE || context.type === context.UserEventType.EDIT) {
var stRec = record.load({ type: record.Type.SALES_ORDER, id: context.newRecord.id });
log.debug("soId", context.newRecord.id)
assessPaymentStatus(stRec);
}
} catch (err) {
log.error("ERR", JSON.stringify(err))
}
}
function assessPaymentStatus(stRec) {
var terms = stRec.getValue({ fieldId: "terms" })
var paymentoption = stRec.getValue({ fieldId: "paymentoption" });
var hasCashSaleData = getItemStatusInCashSales(stRec);
var hasCashRefundData = getItemStatusInCashRefund(stRec);
// Arnold Xavi Update Start
log.debug("paymentoption", paymentoption)
var paymentOptionValue = stRec.getText({ fieldId: "paymentoption" })
log.debug("paymentOptionValue", paymentOptionValue)
var paymentMethod = stRec.getValue({ fieldId: "paymentmethod" })
log.debug("paymentMethod", paymentMethod)
var brainTreeFlag = 0
if (paymentOptionValue) {
var indexValue = paymentOptionValue.indexOf("BT -");
if (indexValue != -1) {
brainTreeFlag = 1
log.debug("Payment Option Contains BT- ")
}
else if (paymentMethod == 19 || paymentMethod == 20 || paymentMethod == 21 || paymentMethod == 22) {
brainTreeFlag = 1
log.debug("Payment Method Contains BT- ")
}
else {
brainTreeFlag = 0
log.debug("Payment Option Does Not Contains BT- ");
}
}
log.debug("brainTreeFlag", brainTreeFlag)
// Arnold Xavi Update End
var cashSaleData = hasCashSaleData[0];
var isCashSaleDataAvailable = hasCashSaleData[1];
var cashRefundData = hasCashRefundData[0];
var isCashRefundDataAvailable = hasCashRefundData[1];
var _CREDITCARD = false;
if (paymentoption) {
var searchObj = search.create({
type: "paymentoption",
filters: [
["internalid", "anyof", paymentoption],
"AND",
["paymentinstrumenttype", "anyof", [1, 3, 4]]
]
});
if (searchObj.runPaged().count) {
_CREDITCARD = true
}
}
log.debug("cashSaleData", JSON.stringify(cashSaleData))
log.debug("isCashSaleDataAvailable", isCashSaleDataAvailable)
log.debug("cashRefundData", JSON.stringify(cashRefundData))
log.debug("isCashRefundDataAvailable", isCashRefundDataAvailable)
var itemLines = stRec.getLineCount({ sublistId: "item" });
for (var index = 0; index < itemLines; index++) {
var soItem = stRec.getSublistValue({ sublistId: "item", fieldId: "item", line: index });
if (terms === _TERMS_AMAZON && cashRefundData.hasOwnProperty(soItem)) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _REFUND_AMAZON })
} else if (terms === _TERMS_WALMART && cashRefundData.hasOwnProperty(soItem)) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _REFUND_WALMART })
// Refunded
} else if ((_CREDITCARD || brainTreeFlag == 1) && cashRefundData.hasOwnProperty(soItem)) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _REFUNDED })
log.debug("cashRefundData", cashRefundData.hasOwnProperty(soItem))
// Refunded
} else if (paymentoption === _PAY_OPT_PAYPAL && cashRefundData.hasOwnProperty(soItem)) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _REFUND_PAYPAL })
} else if (isChecked(stRec.getSublistValue({ sublistId: "item", fieldId: "isclosed", line: index }))) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _CANCELED })
// CC Not Paid
} else if ((_CREDITCARD || brainTreeFlag == 1) && !cashSaleData.hasOwnProperty(soItem)) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _CC_NOT_PAID })
log.debug("cashSaleDatahasOwnPropertysoItem", (cashSaleData.hasOwnProperty(soItem)))
// CC Not Paid
} else if (paymentoption === _PAY_OPT_PAYPAL && !cashSaleData.hasOwnProperty(soItem)) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _PAYPAL })
} else if (paymentoption === _PAY_OPT_CHECK && !cashSaleData.hasOwnProperty(soItem)) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _CHECK_NOT_PAID })
} else if (paymentoption === _PAY_OPT_WIRE_TRANSFER && !cashSaleData.hasOwnProperty(soItem)) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _WIRE_TRANSFER_NOT_PAID })
} else if (terms === _TERMS_AMAZON) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _AMAZON })
} else if (terms === _TERMS_WALMART) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _WALMART })
} else if (terms === _TERMS_CHECK_OVER_UNDER_PAYMENT) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _PAID_BY_CHECK })
// CC Processing Error
} else if ((_CREDITCARD || brainTreeFlag == 1) && cashSaleData.hasOwnProperty(soItem) >= 0 && cashSaleData[soItem] === _UNAPPROVED_PAYMENT) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _CC_PROCESSING_ERROR })
// CC Processing Error
} else if (paymentoption === _PAY_OPT_PAYPAL && cashSaleData.hasOwnProperty(soItem) && (cashSaleData[soItem] === _DEPOSITED || cashSaleData[soItem] === _NOT_DEPOSITED)) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _PAYPAL_PAID })
} else if (paymentoption === _PAY_OPT_WIRE_TRANSFER && cashSaleData.hasOwnProperty(soItem) && (cashSaleData[soItem] === _DEPOSITED || cashSaleData[soItem] === _NOT_DEPOSITED)) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _PAID_BY_WIRE_TRANSFER })
} else if (paymentoption === _PAY_OPT_CHECK && cashSaleData.hasOwnProperty(soItem) && (cashSaleData[soItem] === _DEPOSITED || cashSaleData[soItem] === _NOT_DEPOSITED)) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _PAID_BY_CHECK })
// Paid by CC
} else if ((_CREDITCARD || brainTreeFlag == 1) && cashSaleData.hasOwnProperty(soItem) && (cashSaleData[soItem] === _DEPOSITED || cashSaleData[soItem] === _NOT_DEPOSITED)) {
stRec.setSublistValue({ sublistId: "item", fieldId: "custcol_cd_payment_status", line: index, value: _PAID_BY_CC })
// Paid by CC
} else {
//do nothing
}
}
stRec.save({ ignoreMandatoryFields: true });
}
function isChecked(fieldValue) {
var arrTrueValues = ['T', 't', 'true', 'True', 'TRUE', true]
if (arrTrueValues.indexOf(fieldValue) >= 0) {
return true;
}
return false;
}
function getItemStatusInCashSales(stRec) {
var obj = {};
var cashsaleSearchObj = search.create({
type: record.Type.CASH_SALE,
filters:
[
["createdfrom.internalidnumber", "equalto", stRec.id],
"AND",
["type", "anyof", "CashSale"],
"AND",
["voided", "is", "F"],
"AND",
["mainline", "is", "F"],
"AND",
["shipping", "is", "F"],
"AND",
["cogs", "is", "F"],
"AND",
["taxline", "is", "F"]
],
columns:
[
search.createColumn({ name: "item", label: "Item" }),
search.createColumn({ name: "statusref", label: "Status" })
]
});
cashsaleSearchObj.run().each(function (res) {
obj[res.getValue({ name: "item" })] = res.getText({ name: "statusref" });
return true;
});
return [obj, Object.keys(obj).length ? true : false];
}
function getItemStatusInCashRefund(stRec) {
var obj = {};
var cashrefundSearchObj = search.create({
type: record.Type.RETURN_AUTHORIZATION,
filters: [
["type", "anyof", "RtnAuth"],
"AND",
["createdfrom.createdfrom", "anyof", stRec.id],
"AND",
["status", "anyof", "RtnAuth:G"],
"AND",
["mainline", "is", "F"],
"AND",
["taxline", "is", "F"],
"AND",
["cogs", "is", "F"],
"AND",
["shipping", "is", "F"]
],
columns: [
search.createColumn({ name: "item", label: "Item" })
]
});
cashrefundSearchObj.run().each(function (res) {
obj[res.getValue({ name: "item" })] = res.getValue({ name: "item" });
return true;
});
return [obj, Object.keys(obj).length ? true : false];
}
return {
beforeLoad: beforeLoad,
afterSubmit: afterSubmit
}
}
);