To set amount in words

Jira code: ME-101

To set the total amount in words when a tax invoice, debit note, credit memo is saved.

User event script

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 * CLIENTNAME:MANGALAM
 * ME 101 -Automatically set Amount in words 
 *************************************************************************
 * Date : 08/03/2019
 *
 * Author: Jobin & Jismi IT Services LLP
 *
 * REVISION HISTORY
 *
 * Revision 1.0 ${08/013/2019} aj : created
 * 
 *
 ******************************************************************************/

define(['N/record', 'N/search','N/file'],
		/**
		 * @param {record} record
		 * @param {search} search
		 */
		function(record, search,file) {

	/**
	 * 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) {

	}

	/**
	 * Function definition to be triggered before record is loaded.
	 *
	 * @param {Object} scriptContext
	 * @param {Record} scriptContext.newRecord - New record
	 * @param {Record} scriptContext.oldRecord - Old record
	 * @param {string} scriptContext.type - Trigger type
	 * @Since 2015.2
	 */
	function afterSubmit(scriptContext) {
		try{

			var curRec = scriptContext.newRecord;
			var curRecType = scriptContext.newRecord.type;
			var curRecId = scriptContext.newRecord.id;
			var totalObj = search.lookupFields({
				type :curRecType,
				id : curRecId,
				columns : [ 'total' ]
			});


			var total  = totalObj["total"];
          
			if(curRecType=='creditmemo'|| curRecType=='vendorcredit')
			{
				total=total*-1;
			}
			//function to call the words fn
			var amountInWords = findAmountInWords(total);
			 
			
			// to set amount in words
			var id=record.submitFields({
				type:curRecType,
				id:curRecId,
				values:{
					'custbody_amount_in_wrds':amountInWords
				}
			})

		}catch(e)
		{
			log.debug("Err@ FN beforeSubmit",e);
			log.error("Err@ beforeSubmit FN ",e);

		}

	}


	/************************************
	 * to find amount in words fn
	 ************************************/
	function findAmountInWords(num)
	{
		try{
			var a = ['','One ','Two ','Three ','Four ', 'Five ','Six ','Seven ','Eight ','Nine ','Ten ','Eleven ','Twelve ','Thirteen ','Fourteen ','Fifteen ','Sixteen ','Seventeen ','Eighteen ','Nineteen '];
			var b = ['', '', 'Twenty','Thirty','Forty','Fifty', 'Sixty','Seventy','Eighty','Ninety'];

			var paisa= (num.toString()).split(".")[1];


          

			num =(num.toString()).split(".")[0];



			if ((num = num.toString()).length > 9) return 'overflow';
			n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
			if (!n) return; var str = '';
			str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
			str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '';
			str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
			str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
			str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'Rupees ' : '';
			
			if(paisa!=null &&paisa!=undefined && paisa!='null' && paisa!=" " && paisa!="" && paisa!=00 && paisa !='00')
			{


				m = ('000000000' + paisa).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
				if (!m) return; var str1 = '';
				str1 += (m[1] != 0) ? (a[Number(m[1])] || b[m[1][0]] + ' ' + a[m[1][1]]) + 'crore ' : '';
				str1 += (m[2] != 0) ? (a[Number(m[2])] || b[m[2][0]] + ' ' + a[m[2][1]]) + 'lakh ' : '';
				str1 += (m[3] != 0) ? (a[Number(m[3])] || b[m[3][0]] + ' ' + a[m[3][1]]) + 'thousand ' : '';
				str1 += (m[4] != 0) ? (a[Number(m[4])] || b[m[4][0]] + ' ' + a[m[4][1]]) + 'hundred ' : '';
				str1 += (m[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(m[5])] || b[m[5][0]] + ' ' + a[m[5][1]]) + 'Paisa Only' : '';
			}else if(paisa==00||paisa==null ||paisa==undefined || paisa=='null' || paisa==" " || paisa=="" ||paisa=='00'){
				var str1='Only';


					return str+str1;


		}catch(e)
		{
			log.debug("Err@ FN findAmountInWords",e);
			log.error("Err@ findAmountInWords FN ",e);

		}
	}

	return {

		afterSubmit: afterSubmit
	};

});

Leave a comment

Your email address will not be published. Required fields are marked *