/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/currentRecord', 'N/record', 'N/search'],
/**
* @param{currentRecord} currentRecord
* @param{record} record
* @param{search} search
*/
function (currentRecord, record, search) {
/**
* Function to check the RMA is created from an Invoice record
* @param currentRMARecord
* @returns {boolean}
*/
function checkRMAFromInvoice(createdFrom) {
try {
var transactionSearchObj = search.create({
type: "transaction",
filters:
[
["internalid","anyof",createdFrom],
"AND",
["mainline","is","T"]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({name: "recordtype", label: "Record Type"})
]
});
var searchResultCount = transactionSearchObj.runPaged().count;
var flag = 0;
if(searchResultCount > 0)
{
transactionSearchObj.run().each(function(result){
var recordType = result.getValue({
name: "recordtype", label: "Record Type"
})
console.log("recordType",recordType)
if(recordType == 'invoice'){
flag= flag + 1
return true
}
});
if(flag > 0)
{
return true
}
}
else
{
return false
}
}
catch(err)
{
console.log("error@checkRMAFromInvoice",err)
}
}
/**
* Function for check for parameters
* @param parameter
* @returns {boolean}
*/
const checkForParameter = function checkForParameter(parameter) {
if (parameter !== "" && parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== " " && parameter !== 'false') {
return true;
}
}
/**
* Function to get the items in the Invoice Record
* @param createdFrom is internal id of invoice
* @returns {*[]}
*/
function getInvoiceItems(createdFrom) {
try {
var invoiceSearchObj = search.create({
type: "invoice",
filters:
[
["type", "anyof", "CustInvc"],
"AND",
["internalid","anyof",createdFrom],
"AND",
["taxline", "is", "F"],
"AND",
["mainline", "is", "F"],
"AND",
["cogs","is","F"]
],
columns:
[
search.createColumn({
name: "internalid",
join: "item",
sort: search.Sort.ASC,
label: "Internal ID"
}),
search.createColumn({
name: "itemid",
join: "item",
label: "Name"
}),
search.createColumn({name: "lineuniquekey", label: "Line Unique Key"})
]
});
var searchResultCount = invoiceSearchObj.runPaged().count;
var itemArray = []
if (searchResultCount > 0) {
invoiceSearchObj.run().each(function (result) {
// var itemObj={}
itemArray.push(result.getValue({
name: "internalid",
join: "item",
sort: search.Sort.ASC,
label: "Internal ID"
}))
return true;
});
return itemArray;
} else {
return []
}
} catch (err) {
console.log("error@getInvoiceItems", err)
return []
}
}
/**
* Function to check the type of the item
* @param itemID is the item id
* @returns {boolean|*}
*/
function checkItemType(itemID)
{
try
{
var itemSearchObj = search.create({
type: "item",
filters:
[
["internalid","anyof",itemID],
"AND",
["isinactive","is","F"]
],
columns:
[
search.createColumn({
name: "itemid",
sort: search.Sort.ASC,
label: "Name"
}),
search.createColumn({name: "type", label: "Type"}),
search.createColumn({name: "internalid", label: "Internal ID"})
]
});
var searchResultCount = itemSearchObj.runPaged().count;
var itemType
if(searchResultCount > 0){
itemSearchObj.run().each(function(result){
itemType = result.getValue({
name: "type", label: "Type"
})
return true;
});
return itemType
}
else
{
return false
}
}
catch(err)
{
console.log("error@checkItemType",err)
return false
}
}
/**
* Function to be executed after page is initialized.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
*
* @since 2015.2
*/
function pageInit(scriptContext) {
}
/**
* Function to be executed when field is changed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
* @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
*
* @since 2015.2
*/
function fieldChanged(scriptContext) {
try {
var currentRec = scriptContext.currentRecord;
var sublistName = scriptContext.sublistId;
var sublistFieldName = scriptContext.fieldId;
var createdFrom = currentRec.getValue({
fieldId: 'createdfrom'
});
var invoiceItems;
var lineItem = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
});
var amount = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'amount'
});
if (sublistName === 'item' && sublistFieldName === 'amount') {
if (checkForParameter(createdFrom)) {
var isCreatedFromInvoice = checkRMAFromInvoice(createdFrom)
console.log("isCreatedFromInvoice**",isCreatedFromInvoice)
if (isCreatedFromInvoice == true) {
if (lineItem && lineItem == '671') {
if ((Math.sign(amount) == '1')) {
alert("Please enter a negative value for the amount.")
return false;
} else {
return true
}
} else {
return true
}
} else {
return true
}
}
}
} catch (err) {
console.log("error@fieldChanged", err)
}
}
/**
* Validation function to be executed when field is changed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
* @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
*
* @returns {boolean} Return true if field is valid
*
* @since 2015.2
*/
function validateField(scriptContext) {
try {
var currentRec = scriptContext.currentRecord;
var sublistName = scriptContext.sublistId;
var sublistFieldName = scriptContext.fieldId;
var createdFrom = currentRec.getValue({
fieldId: 'createdfrom'
});
var invoiceItems;
var lineItem = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
});
var lineUniqueKey = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'lineuniquekey'
});
var lineNumber = currentRec.findSublistLineWithValue({
sublistId: 'item',
fieldId: 'item',
value: lineItem
});
if (sublistName === 'item' && sublistFieldName === 'item') {
console.log("1st if...............")
if (checkForParameter(createdFrom)) {
console.log("2nd if...............")
var isCreatedFromInvoice = checkRMAFromInvoice(createdFrom);
console.log("isCreatedFromInvoice",isCreatedFromInvoice)
if (isCreatedFromInvoice == true) {
console.log("3rd if...............")
invoiceItems = getInvoiceItems(createdFrom)
console.log("invoiceItems",invoiceItems)
if (invoiceItems.length > 0) {
if ((lineItem == '671') || (invoiceItems.includes(lineItem))) {
console.log("items in invoice or it is restock item")
} else {
alert("You dont have permission to choose other items in the RMA record")
return false;
}
}
if(lineNumber != '-1' && lineItem != '671')
{
alert("This item has already been added.")
return false;
}
else
{
return true
}
} else {
return true
}
}
}
return true
} catch (err) {
console.log("error@validateField", err)
}
}
/**
* Validation function to be executed when sublist line is committed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
*
* @returns {boolean} Return true if sublist line is valid
*
* @since 2015.2
*/
function validateLine(scriptContext) {
try {
var currentRec = scriptContext.currentRecord;
var sublistName = scriptContext.sublistId;
var sublistFieldName = scriptContext.fieldId;
var createdFrom = currentRec.getValue({
fieldId: 'createdfrom'
});
if (sublistName === 'item') {
if (checkForParameter(createdFrom)) {
var isCreatedFromInvoice = checkRMAFromInvoice(createdFrom)
console.log("isCreatedFromInvoice", isCreatedFromInvoice)
if (isCreatedFromInvoice == true) {
console.log("craeted from invoice");
var lineItem = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
});
var amount = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'amount'
});
if (lineItem && lineItem == '671') {
if ((Math.sign(amount) == '1')) {
alert("Please enter a negative value for the amount.")
return false;
} else {
return true
}
} else {
return true
}
} else {
return true
}
}
}
return true
} catch (err) {
console.log("error@ValidateLine", err)
}
}
/**
* Validation function to be executed when record is deleted.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
*
* @returns {boolean} Return true if sublist line is valid
*
* @since 2015.2
*/
function validateDelete(scriptContext) {
try
{
var currentRec = scriptContext.currentRecord;
var createdFrom = currentRec.getValue({
fieldId: 'createdfrom'
});
if (checkForParameter(createdFrom)) {
var isCreatedFromInvoice = checkRMAFromInvoice(createdFrom)
if (isCreatedFromInvoice == true) {
var currentLineItem = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
});
var lineCount = currentRec.getLineCount({
sublistId:'item'
})
console.log("lineCount",lineCount)
var currentLines = currentRec.getCurrentSublistIndex({
sublistId: 'item'
});
console.log("currentLines",currentLines)
if(checkForParameter(currentLineItem))
{
var checkDiscountItem = checkItemType(currentLineItem)
console.log("checkDiscountItem",checkDiscountItem);
if(checkDiscountItem == 'Discount' && currentLines > 0)
{
var previousItem = currentRec.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line:currentLines -1
});
if(checkForParameter(previousItem))
{
var inventoryItemCheck = checkItemType(previousItem)
console.log("inventoryItemCheck",inventoryItemCheck)
if(inventoryItemCheck == 'InvtPart')
{
alert("The item cannot be deleted until the related item is removed.")
return false
}
else
{
console.log("can remove item")
}
}
}
}
// if(checkForParameter(currentLineItem))
// {
// var isDiscountItem = checkItemType(currentLineItem)
// if(isDiscountItem == 'Discount')
// {
// for( var i = lineCount -1 ; i >= 0 ; i--)
// {
// console.log("i...",i)
// var item = currentRec.getSublistValue({
// sublistId:'item',
// fieldId:'item',
// line:i
// })
// itemsArray.push(item)
// }
// console.log("itemsArray",itemsArray)
// }
// else
// {
// console.log("not discount item")
// }
//
// }
}
}
return true
}
catch(err)
{
console.log("error@validateDelete",err)
}
}
return {
pageInit: pageInit,
fieldChanged: fieldChanged,
// postSourcing: postSourcing,
// sublistChanged: sublistChanged,
// lineInit: lineInit,
validateField: validateField,
validateLine: validateLine,
// validateInsert: validateInsert,
validateDelete: validateDelete,
};
});