Convert Amount into Thai Words

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
 define(['N/record','N/search'],

 (record,search) => {


         /**
          * Defines the function definition that is executed after record is submitted.
          * @param {Object} scriptContext
          * @param {Record} scriptContext.newRecord - New record
          * @param {Record} scriptContext.oldRecord - Old record
          * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
          * @since 2015.2
          */
         const afterSubmit = (scriptContext) => {

                 try{

                         var recId = scriptContext.newRecord.id;
                         var recType = scriptContext.newRecord.type;
                         var amount;
                         var amountToThai,amountToThai2;
                         var poRec = record.load({
                                 type: recType,
                                 id: recId,
                                 isDynamic: false
                         }) ;
                         var  amount = poRec.getValue({
                            fieldId:'total'
                        });
                         log.debug('PO record',poRec);
                         
                         

                         //set ศูนย์(Zero) if the Total is Zero
                         if(amount==0){
                                 amountToThai = 'ศูนย์'
                         }
                         //convert Total to Thai Words
                         else{
                                 amountToThai = convertToThai(amount);
                         }
                         //set the thai words to the custom field on the customer payment
                         poRec.setValue({
                                 fieldId:'custbody_total_amount_in_thai',
                                 value:amountToThai
                         })
                        
                         var saveID = poRec.save({
                                 enableSourcing: true,
                                 ignoreMandatoryFields: true
                         });

                 }
                 catch (e) {
                         log.error('error@saveRecord',e.message);
                 }

         }
         function convertToThai(num, suffix){
                 try {
                         //log.debug('in convert fn');
                         //log.debug('amount', num);

                         'use strict';

                         if (typeof suffix === 'undefined') {
                                 suffix = 'บาทถ้วน';
                         }

                         num = num || 0;
                         num = num.toString().replace(/[, ]/g, ''); // remove commas, spaces

                         if (isNaN(num) || (Math.round(parseFloat(num) * 100) / 100) === 0) {
                                 return 'ศูนย์บาทถ้วน';
                         } else {

                                 var t = ['', 'สิบ', 'ร้อย', 'พัน', 'หมื่น', 'แสน', 'ล้าน'],
                                     n = ['', 'หนึ่ง', 'สอง', 'สาม', 'สี่', 'ห้า', 'หก', 'เจ็ด', 'แปด', 'เก้า'],
                                     len,
                                     digit,
                                     text = '',
                                     parts,
                                     i;

                                 if (num.indexOf('.') > -1) { // have decimal

                                         /*
                                          * precision-hack
                                          * more accurate than parseFloat the whole number
                                          */

                                         parts = num.toString().split('.');

                                         num = parts[0];
                                         parts[1] = parseFloat('0.' + parts[1]);
                                         parts[1] = (Math.round(parts[1] * 100) / 100).toString(); // more accurate than toFixed(2)
                                         parts = parts[1].split('.');

                                         if (parts.length > 1 && parts[1].length === 1) {
                                                 parts[1] = parts[1].toString() + '0';
                                         }

                                         num = parseInt(num, 10) + parseInt(parts[0], 10);
                                         //log.debug('num',num)


                                         /*
                                          * end - precision-hack
                                          */
                                         text = num ? convertToThai(num) : '';

                                         if (parseInt(parts[1], 10) > 0) {
                                             //log.debug('text-part1',text)
                                                 text = text.replace('ถ้วน', '') + convertToThai(parts[1], 'สตางค์');
                                                 //log.debug('text-parts',text)
                                         }

                                         return text;

                                 } else {

                                         if (num.length > 7) { // more than (or equal to) 10 millions

                                                 var overflow = num.substring(0, num.length - 6);
                                                 var remains = num.slice(-6);
                                                 return convertToThai(overflow).replace('บาทถ้วน', 'ล้าน') + convertToThai(remains).replace('ศูนย์', '');

                                         } else {

                                                 len = num.length;
                                                 //log.debug('length',len)
                                                 for (i = 0; i < len; i = i + 1) {
                                                         digit = parseInt(num.charAt(i), 10);
                                                         //log.debug('digit',digit)
                                                         if (digit > 0) {
                                                                 if (len > 2 && i === len - 1 && digit === 1 && suffix !== 'สตางค์') {
                                                                         text += 'หนึ่ง' + t[len - 1 - i];
                                                                 } else {
                                                                         text += n[digit] + t[len - 1 - i];
                                                                 }
                                                         }
                                                         log.debug('text',text+'_'+i)
                                                 }

                                                 // grammar correction
                                                 text = text.replace('หนึ่งสิบ', 'สิบ');
                                                 text = text.replace('สองสิบ', 'ยี่สิบ');
                                                 text = text.replace('สิบหนึ่ง', 'สิบเอ็ด');

                                                 return text + suffix;
                                         }

                                 }

                         }


                 } catch (e) {
                         log.error('error@ThaiconvertFuction', e.message);
                 }
         }

         return {afterSubmit}

 });

Leave a comment

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