Converting Invoice Amount to Arabic Words in NetSuite: A Comprehensive Guide

Overview: NetSuite allows users to create customizations using SuiteScript to enhance business operations. One such customization is the conversion of invoice amounts into Arabic words for better localization and user experience. This article explains how to implement this functionality using a User Event Script in SuiteScript 2.1, enabling automatic conversion of the invoice total to its Arabic currency representation upon invoice creation or editing.

Script Overview:

This User Event Script triggers after a Sales Invoice is created or edited. It takes the invoice total and converts it to Arabic words (including both main and small currency units) before storing the result in a custom field.

Key Functions:

  1. convertToArabicCurrency: This function converts a numeric amount into its Arabic word representation with the corresponding main and small currency units.
javascript

Copy

Edit
function convertToArabicCurrency(number, mainCurrency, smallCurrency) {
    try {
        let integerPart = Math.floor(number); 
        let decimalPart = Math.round((number - integerPart) * 100);

        let words = convertToArabicWords(integerPart) + ` ${mainCurrency} `;

        if (decimalPart > 0) {
            words += ' و ' + convertToArabicWords(decimalPart) + ` ${smallCurrency} `;
        }

        return words.trim();
    } catch (e) {
        console.error('error @convertToArabicCurrency', e);
        return null;
    }
}
  1. How it works:
  • It separates the integer and decimal parts of the amount.
  • Converts both parts into Arabic words using the convertToArabicWords function.
  • Adds the main currency and small currency units (e.g., “Rupees” and “Paisa”) accordingly.
  1. convertToArabicWords: This function takes a number and converts it to its Arabic word representation.
javascript

Copy

Edit
function convertToArabicWords(number) {
    try {
        const arabicNumbers = [
            '', 'واحد', 'اثنان', 'ثلاثة', 'أربعة', 'خمسة', 'ستة', 'سبعة', 'ثمانية', 'تسعة',
            'عشرة', 'أحد عشر', 'اثنا عشر', 'ثلاثة عشر', 'أربعة عشر', 'خمسة عشر', 'ستة عشر',
            'سبعة عشر', 'ثمانية عشر', 'تسعة عشر'
        ];
        const tens = ['', '', 'عشرون', 'ثلاثون', 'أربعون', 'خمسون', 'ستون', 'سبعون', 'ثمانون', 'تسعون'];
        const hundreds = ['', 'مائة', 'مائتان', 'ثلاثمائة', 'أربعمائة', 'خمسمائة', 'ستمائة', 'سبعمائة', 'ثمانمائة', 'تسعمائة'];

        let words = '';

        if (number >= 1000000) {
            let millions = Math.floor(number / 1000000);
            words += convertToArabicWords(millions) + (millions === 2 ? ' مليونان ' : millions > 2 && millions < 11 ? ' ملايين ' : ' مليون ');
            number %= 1000000;
            if (number > 0) words += 'و ';
        }

        if (number >= 1000) {
            let thousands = Math.floor(number / 1000);
            words += convertToArabicWords(thousands) + (thousands === 2 ? ' ألفان ' : thousands > 2 && thousands < 11 ? ' آلاف ' : ' ألف ');
            number %= 1000;
            if (number > 0) words += 'و ';
        }

        if (number >= 100) {
            words += hundreds[Math.floor(number / 100)] + ' ';
            number %= 100;
            if (number > 0) words += 'و ';
        }

        if (number >= 20) {
            words += tens[Math.floor(number / 10)] + ' ';
            number %= 10;
            if (number > 0) words += 'و ';
        }

        if (number > 0) {
            words += arabicNumbers[number] + ' ';
        }

        return words.trim();
    } catch (e) {
        console.error('error @convertToArabicWords', e);
    }
}
  1. How it works:
  • Arabic Numbers: An array maps numbers from 1 to 19 to their Arabic word equivalents.
  • Tens and Hundreds: Arrays for tens and hundreds allow for constructing larger numbers in Arabic.
  • The function recursively breaks down the number into its components (millions, thousands, hundreds, tens, ones) and converts each part to Arabic words.
  1. afterSubmit Trigger: The afterSubmit function is triggered when a Sales Invoice is created or edited. It retrieves the total amount and the corresponding currencies, calls the conversion functions, and stores the result in a custom field (custbody64).
javascript

Copy

Edit
const afterSubmit = (scriptContext) => {
    try {
        if (scriptContext.type === scriptContext.UserEventType.CREATE || 
            scriptContext.type === scriptContext.UserEventType.EDIT) {
            
            let invoiceRec = scriptContext.newRecord;
            let recId = invoiceRec.id;
            let totalAmount = invoiceRec.getValue({ fieldId: 'total' });
            let mainCurrencyId = invoiceRec.getValue({ fieldId: 'currency' });
            let smallCurrencyId = invoiceRec.getValue({ fieldId: 'custbody_smallcurrency' });
            
            let mainCurrencyLookUp = search.lookupFields({
                type: search.Type.CURRENCY,
                id: mainCurrencyId,
                columns: ['name']
            });
            let mainCurrency = mainCurrencyLookUp.name;
            
            let smallCurrencyLookUp = search.lookupFields({
                type: 'customlist_minicurr',
                id: smallCurrencyId,
                columns: ['name']
            });
            let smallCurrency = smallCurrencyLookUp.name;

            if (!totalAmount) return;

            let amountInWords = convertToArabicCurrency(totalAmount, mainCurrency, smallCurrency);
            amountInWords += " فقط";
            record.submitFields({
                type: record.Type.INVOICE,
                id: recId,
                values: {
                    custbody64: amountInWords
                },
                options: {
                    enableSourcing: false,
                    ignoreMandatoryFields: true
                }
            });
        }
    } catch (e) {
        log.error('error @afterSubmit', e);
    }
}
  1. How it works:
  • Currency Lookup: The script looks up both the main currency and small currency to fetch their names.
  • The invoice total is passed to the convertToArabicCurrency function to get the Arabic equivalent.
  • The result is stored in the custbody64 field for further use.

Benefits of This Script:

  1. Localization: Provides a localized experience by converting the invoice amount into Arabic words.
  2. Automation: Automates the process of generating invoice amounts in words, reducing manual errors.
  3. Customization: Easily customizable to handle other currencies or different custom fields for various regions.

Conclusion:

This SuiteScript 2.1 User Event Script helps businesses working in Arabic-speaking regions to automate the conversion of invoice amounts into Arabic currency words. This adds to the professionalism and compliance with regional norms, improving the invoicing process for users and customers alike.

Leave a comment

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