Solution
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
/*******************************************************************
* UserEvent Script
****************************************************************
*
* Date: 30/12/2021
*
* Description: Encrypted QR Code update on invoice by using the subsidiary ,vat
*value,taxtotal,date etc
***************************************************************/
define(['N/encode', 'N/record', 'N/search'],
function (encode, record, search) {
let tagHex;
/**
* @param function to get the length of the parameters and convert the string to hex value
* @param returns hexadecimal value of string
*/
function getLength(lengthparam) {
try {
let length = lengthparam.length
//log.debug("length", length)
let hexString = length.toString(16)
if (hexString.length < 2) {
hexString = "0" + hexString;
}
//log.debug("hexString", hexString)
return hexString
} catch (e) {
log.debug("Error @function getLength", e);
}
}
/**
* @param function accepts UTF_8 format and returns hexadecimal value of input
* @param returns encoded string
*/
function encodingString(params) {
try {
let hexEncodedString = encode.convert({
string: params,
inputEncoding: encode.Encoding.UTF_8,
outputEncoding: encode.Encoding.HEX
});
//log.debug("hexEncodedString",hexEncodedString)
return (hexEncodedString)
} catch (e) {
log.debug("Error @fucction encoding string", e);
}
}
/**
* @param function to search subsidiary to get the seller name(arabic) if absent it returns name
* @param returns name
*/
function subsidiarySearch(sellerNameId) {
try {
let subsidiaryArray = [];
var subsidiarySearchObj = search.create({
type: "subsidiary",
filters:
[
["internalid", "anyof", sellerNameId]
],
columns:
[
search.createColumn({name: "taxidnum", label: "Tax ID"}),
search.createColumn({
name: "formulatext",
formula: "NVL2({custrecord_az_mn_sub_namearabic}, {custrecord_az_mn_sub_namearabic}, {name})",
label: "seller name"
})
]
});
let searchResultCount = subsidiarySearchObj.runPaged().count;
// log.debug("subsidiarySearchObj result count",searchResultCount);
subsidiarySearchObj.run().each(function (result) {
let vatNumber = result.getValue(result.columns[0]);
let sellerName = result.getValue(result.columns[1]);
let customRecordObj = {};
customRecordObj.vatNumber = vatNumber
customRecordObj.sellerName = sellerName
subsidiaryArray.push(customRecordObj);
return true;
});
return subsidiaryArray;
} catch (e) {
log.debug("Error @function subsidiary search", e);
}
}
/**
* @param function after submit to encode the qr code
* @param returns encoded qr code
*/
const afterSubmit = (scriptContext) => {
try {
let contextMode = scriptContext.type;
if (contextMode === scriptContext.UserEventType.CREATE || contextMode === scriptContext.UserEventType.EDIT || contextMode === scriptContext.UserEventType.COPY) {
let inputRecord = scriptContext.newRecord;
//get value of subsidiary
let sellerNameId = inputRecord.getValue({
fieldId: 'subsidiary'
});
//log.debug(" inputRecord", inputRecord)
let invoiceID = inputRecord.getValue({fieldId: 'id'});
//search for subsidiary record from invoice
let resultSet = subsidiarySearch(sellerNameId);
//fetching the seller/customer name
let customerName = resultSet[0].sellerName;
//get the length of the customer name
let custNameLength = getLength(customerName)
//search to encode customer name
let encodedCustName = encodingString(customerName)
//log.debug(" encodedCustName", encodedCustName)
//setting the tag value
tagHex = '01'
//concatinating the tag value hex value of length and encoded customer name
let sellerNameNew = tagHex.concat(custNameLength, encodedCustName)
//log.debug("sellerNameNew", sellerNameNew)
let vatNum = resultSet[0].vatNumber;
log.debug("vatNum", vatNum)
// if (vatNum != '' || vatNum != null || vatNum != undefined) {
let vatNumLength = getLength(vatNum)
//log.debug("vatNumLength", vatNumLength)
let encodedVatNum = encodingString(vatNum)
log.debug("encodedVatNum", encodedVatNum)
tagHex = '02'
let vatNumNew = tagHex.concat(vatNumLength, encodedVatNum)
// } else {
// let vatNumLength = 0
// let encodedVatNum = ''
// let vatNumNew = tagHex.concat(vatNumLength, encodedVatNum)
// }
if (vatNum == '' || vatNum == null || vatNum == undefined){
let vatNumLength = 0
let encodedVatNum = ''
let vatNumNew = tagHex.concat(vatNumLength, encodedVatNum)
}
log.debug("vatNumNew", vatNumNew)
let timeStamp = inputRecord.getValue({
fieldId: 'trandate'
});
log.debug("timeStamp", timeStamp)
let timeNew = timeStamp.toISOString().length;
log.debug("timeNew", timeNew)
let timeNew1 = timeStamp.toISOString();
log.debug("timeNew1", timeNew1)
let timStampLength = getLength(timeNew1)
log.debug("timStampLength", timStampLength)
let encodededTimeStamp = encodingString(timeNew1)
log.debug("encodededTimeStamp",encodededTimeStamp )
tagHex = '03'
let timeStampNew = tagHex.concat(timStampLength, encodededTimeStamp)
log.debug("timeStampNew ",timeStampNew)
let invoiceTotal = inputRecord.getValue({
fieldId: 'total'
});
let invoiceTotal1 = invoiceTotal.toFixed(2)
let invoiceTotalLength = getLength(invoiceTotal1)
let encodedStringInvoiceTotal = encodingString(invoiceTotal1)
// log.debug(" encodedStringInvoiceTotal", encodedStringInvoiceTotal)
tagHex = '04'
let invoiceTotalNew = tagHex.concat(invoiceTotalLength, encodedStringInvoiceTotal)
log.debug(" invoiceTotalNew", invoiceTotalNew)
let vatTotal = inputRecord.getValue({
fieldId: 'taxtotal'
});
let vatTotal1 = vatTotal.toFixed(2)
let vatTotalLength = getLength(vatTotal1)
let encodedStringVatTotal = encodingString(vatTotal1)
//log.debug("base64EncodedStringVatTotal",base64EncodedStringVatTotal)
tagHex = '05'
let vatTotalNew = tagHex.concat(vatTotalLength, encodedStringVatTotal)
//log.debug("vatTotalNew",vatTotalNew)
// concatinating the encoded seller name, vat number,time stamp,tax total and vat total
let encodedQrCode = sellerNameNew + vatNumNew + timeStampNew + invoiceTotalNew + vatTotalNew
log.debug("encodedQrCode",encodedQrCode)
//encoding the result to base 64 format
let base64EncodedString = encode.convert({
string: encodedQrCode,
inputEncoding: encode.Encoding.HEX,
outputEncoding: encode.Encoding.BASE_64
});
log.debug("base64EncodedString", base64EncodedString);
//setting the qr code value in the custom field
let otherId = record.submitFields({
type: record.Type.INVOICE,
id: invoiceID,
values: {
'custbody_jj_qr_code': base64EncodedString
}
});
}
} catch (e) {
log.debug("Error @after submit", e);
}
}
return {afterSubmit}
});