JIRA TASK:DRF-61
Override the default numbering of invoices based on categories
- Need to account separate Serial Numbers for the transaction record Invoice
- Invoice has three specific Prefixes based on Export, Local,and CounterSale.
- For each prefix need to account separate continuous Numbering System
We have created a custom list that lists the 3 categories :“Export”,”Local”, “Counter Sale”.
We have deployed a Userevent Script on creation of an invoice. On the creation of invoice, the script fetches the category for which invoice is created and sets the corresponding document numbering format.
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
/*************************************************************************************************************************
* CLIENTNAME:Diamond Roller Flour Mills Pvt. Ltd
* DRF - 61
* Serial Number Customization
* **********************************************************************************************************************
* Date : 03/03/2020
*
* Author: Jobin & Jismi IT Services LLP
* Script Description : This Script is to set the serial Number for the Invoice record based on the category
* Date created : 27/03/2020
*
* REVISION HISTORY
*
* Revision 1.0 ${03/03/2020} Rijoy: created
* Revision 1.1 ${30/03/2020} marg: created
*
**************************************************************************************************************************/
define(['N/file', 'N/http', 'N/https', 'N/record', 'N/runtime', 'N/search', 'N/task', 'N/xml', "N/render", 'N/encode', 'N/email'],
function(file, http, https, record, runtime, search, task, xml, render, encode, email) {
var main = {
beforeSubmit: function(scriptContext) {
log.debug("scriptContext", scriptContext.type);
if ( scriptContext.type == "create") {
var invoiceId = scriptContext.newRecord.id;
var records = scriptContext.newRecord;
var category = scriptContext.newRecord.getValue({
fieldId: 'custbody_category'
});
var getYear = scriptContext.newRecord.getValue({
fieldId: 'postingperiod'
});
var fiscal = main.getPeriod(getYear);
var lastTranId = main.findlastTran(category);
log.debug("lastTranId", lastTranId);
var newTranId = main.getTrandId(category, lastTranId, fiscal);
log.debug("newTranId", newTranId);
scriptContext.newRecord.setValue({
fieldId: 'tranid',
value: newTranId
});
}
},
//Function to get the prev invoice transaction filtered by category
findlastTran: function(category) {
var last_invoice;
var invoiceSearchObj = search.create({
type: "invoice",
filters: [
["type", "anyof", "CustInvc"],
"AND", ["custbody_category", "anyof", category],
"AND", ["mainline", "is", "T"]
],
columns: [
search.createColumn({
name: "internalid"
}),
search.createColumn({
name: "tranid",
label: "Document Number"
}),search.createColumn({
name: "datecreated",
sort: search.Sort.DESC,
label: "Date Created"
})
]
});
var searchResultCount = invoiceSearchObj.runPaged().count;
log.debug("invoiceSearchObj result count", searchResultCount);
if (searchResultCount > 0) {
invoiceSearchObj.run().each(function(result) {
last_invoice = result.getValue(invoiceSearchObj.columns[1]);
});
return last_invoice;
} else {
last_invoice = 0;
return last_invoice;
}
},
//Function to set the current financial year
getPeriod: function(getYear) {
var fieldLookUp = search.lookupFields({
type: search.Type.ACCOUNTING_PERIOD,
id: getYear,
columns: ['parent']
});
getYear = fieldLookUp.parent[0].text;
var Split = getYear.split(" ")
getYear = parseInt(Split[1]);
var fullYear = getYear + 1;
fullYear = fullYear.toString();
var fulllengthYear = fullYear.substring(fullYear.length, 2);
fulllengthYear = parseInt(fulllengthYear);
log.debug("fullYear", fullYear);
var fiscal = " "
fiscal = getYear + '-' + fulllengthYear;
log.debug("fiscal", fiscal);
return fiscal;
},
getTrandId: function(category, lastTranId, fiscal) {
log.debug("category", category);
if (category == 3) {
/*category counterSale*/
if (lastTranId == 0) {
return fiscal + "/A Mill/1";
}
log.debug("last cart counterSale", Number(lastTranId.split("/")[2]));
var serialNum = Number(lastTranId.split("/")[2]) + 1;
return fiscal + "/A Mill/" + serialNum;
} else if (category == 1) {
/*category Export*/
if (lastTranId == 0) {
return "EXP/INV/1"
}
log.debug("last cart Export", Number(lastTranId.split("/")[2]));
var serialNum = Number(lastTranId.split("/")[2]) + 1;
return "EXP/INV/" + serialNum;
} else if (category == 2) {
/* category Local*/
if (lastTranId == 0) {
return fiscal + "/" + "1"
}
log.debug("category Local prev", Number(lastTranId.split("/")[1]));
var serialNum = Number(lastTranId.split("/")[1]) + 1;
return fiscal + "/" + serialNum;
}
}
}
for (var key in main) {
if (typeof main[key] === 'function') {
main[key] = trycatch(main[key], key);
}
}
function trycatch(myfunction, key) {
return function() {
try {
return myfunction.apply(this, arguments);
} catch (e) {
log.debug("e in " + key, e);
}
}
};
return main;
});