Create a user event script that will set the summary box on before load
/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/*************************************************************************************
 ***********
 * Mohd Ali Mohideen Supermarket LLC-UAE-NS Software project
 *
 * MAMS-123
 *
 *
 *************************************************************************************
 ***********
 *
 *
 * Author: Jobin and Jismi IT Services LLP
 *
 * Date Created : 16-March-2023
 *
 * Description: This script is used for setting the summary box in the item receipt
 *
 * REVISION HISTORY
 *
 * @version 1.0 MAMS-123 : 16-March-2023 : Created the initial build by JJ0177
 *
 *
 *************************************************************************************
 **********/
define(['N/record', 'N/search'],
    /**
     * @param{record} record
     * @param{search} search
     */
    (record, search) => {
        /**
         * @description the function to create the summary box
         * @param {number} values - Values for creating the summary
         * @return html - retun the html of summary box
         */
        function createSummary(values) {
            try {
                log.debug('inside createsummary');
                let html = '<style>' +
                    'table.newtotallingtable caption {\n' +
                    '    display: table-caption !important;\n' +
                    '    margin-bottom: 10px;\n' +
                    '    font-weight: bold;\n' +
                    '    color: white;\n' +
                    '    font-size: 12px !important;\n' +
                    '    padding: 4px 0px 4px 8px;\n' +
                    '}' +
                    'table.newtotallingtable caption {\n' +
                    '    background-color: #607799;\n' +
                    '}' +
                    'caption, th {\n' +
                    '    text-align: left;\n' +
                    '}' +
                    '</style>';
                html += '<span class="bgmd totallingbg" style="display:inline-block; position:relative;left: 0px; padding: 10px 25px; margin-bottom:5px;">';
                html += '<table class="newtotallingtable" cellspacing="2" cellpadding="0px" border="0px" style="padding: 5px;\n' +
                    '    width: 217px;"><caption style="display: none;" >Summary</caption><tbody><td>';
                html += '<div class="uir-field-wrapper" data-field-type="currency"><span id="subtotal_fs_lbl_uir_label" class="smalltextnolink uir-label "><span id="subtotal_fs_lbl" class="smalltextnolink" style="color: #262626 !important; font-size: 12px; padding-bottom:10px;">'
                html += 'SUBTOTAL</td>';
                html += '<td  style="color: #262626 !important; font-size: 13px; padding-bottom:10px;" align="right" id="subtotal">';
                if (values.mode === 'create' || values.mode === 'edit') {
                    html += values.subTotal;
                } else {
                    html += '<b>' + values.subTotal + '</b>';
                }
                html += '</td><td></td></tr>'
                html += '<tr><td  style="color: #262626 !important; font-size: 12px;" align="left">TAX TOTAL</td><td align="right"   style="font-size: 13px; color: #262626 !important;">';
                if (values.mode === 'create' || values.mode === 'edit') {
                    html += values.taxTotal;
                } else {
                    html += '<b>' + values.taxTotal + '</b>';
                }
                html += '</td><td></td></tr>'
                html += '<tr><td colspan="2"><hr style="width:200px;margin: fill"/></td></tr>'
                html += '<tr><td  style="color: #262626 !important; font-size: 12px;" align="left">TOTAL</td><td align="right"   style="font-size: 13px; color: #262626 !important;">';
                if (values.mode === 'create' || values.mode === 'edit') {
                    html += values.Total;
                } else {
                    html += '<b>' + values.Total + '</b>';
                }
                html += '</td></tr>';
                html += '</table></div>';
                return '<div id="custom_summary">' + html + '</div>';
            } catch (e) {
                log.debug({
                    title: e.name,
                    details: e
                });
                return false
            }
        }
        /**
         * @description function to fetch the number with two decimal point with out rounding the number
         * @param {number} number - input number for setting the two decimal point
         * @param {number} fixed - number up to decimal place we want to set the number.
         * @return number - the number with two decimal point
         */
        function toFixed(number, fixed) {
            try {
                let regExp = new RegExp('^-?\\d+(?:\.\\d{0,' + (fixed || -1) + '})?');
                return number.toString().match(regExp)[0];
            } catch (e) {
                log.error("error @ precesion function", e)
            }
            return false
        }
        /**
         * @description search function to fetch the tax percent from the purchase order record
         * @param {number} poRecordsId - purchase order internal id
         * @return itemObj - object containing tax percentage
         */
        function purchaseOrderDetails(poRecordsId) {
            try {
                let purchaseorderSearchObj = search.create({
                    type: "purchaseorder",
                    filters:
                        [
                            ["type", "anyof", "PurchOrd"],
                            "AND",
                            ["internalid", "anyof", poRecordsId],
                            "AND",
                            ["mainline", "is", "F"],
                            "AND",
                            ["cogs", "is", "F"],
                            "AND",
                            ["shipping", "is", "F"]
                        ],
                    columns:
                        [
                            search.createColumn({name: "line", label: "Line ID"}),
                            search.createColumn({
                                name: "rate",
                                join: "taxItem",
                                label: "Rate"
                            })
                        ]
                });
                let searchResultCount = purchaseorderSearchObj.runPaged().count;
                let itemObj = {}
                if (searchResultCount > 0) {
                    purchaseorderSearchObj.run().each(function (result) {
                        let memberLine = result.getValue({
                            name: "line",
                            label: "Line ID"
                        })
                        let memberTax = result.getValue({
                            name: "rate",
                            join: "taxItem",
                            label: "Rate"
                        })
                        itemObj[memberLine] = memberTax;
                        return true
                    });
                    return itemObj;
                }
            } catch (e) {
                log.error("error @ search function", e)
            }
        }
        /**
         * Defines the function definition that is executed before record is loaded.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @param {Form} scriptContext.form - Current form
         * @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
         * @since 2015.2
         */
        const beforeLoad = (scriptContext) => {
            try {
                let taxTotal = 0;
                let amountTotal = 0
                let total = 0
                /**
                 * @description fetch the client script when the record is opened in view or edit
                 * @param values - Values for creating the summary
                 */
                if (scriptContext.type === 'view' || scriptContext.type === 'edit') {
                   // scriptContext.form.clientScriptModulePath = '/SuiteScripts/Jobin and Jismi IT Services/Summary Box Setup MAMS-123/jj_cs_ir_custom_summarybox mams-123.js';//SB
                    scriptContext.form.clientScriptModulePath = '/SuiteScripts/Jobin and Jismi IT Services/Summary Box Setup MAMS-123/jj_cs_ir_custom_summarybox mams-123.js';
                }
                let mode = scriptContext.type;
                let newRecord = scriptContext.newRecord;
                let poRecordsId = scriptContext.newRecord.getValue({
                    fieldId: 'createdfrom'
                })
                let poItemDetails = purchaseOrderDetails(poRecordsId)
                let lineCount = newRecord.getLineCount({
                    sublistId: 'item'
                });
                /**
                 * @Description loop through the item lines to fetch the rate, quantity and taxtotal
                 * */
                for (let i = 0; i < lineCount; i++) {
                    let itemIsReceive = newRecord.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'itemreceive',
                        line: i
                    });
                    /**
                     * @Description  check whether the isreceive checkbox is checked or not
                     * */
                    if (itemIsReceive) {
                        let irOrderLine = newRecord.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'orderline',
                            line: i
                        });
                        let lineArray = [];
                        lineArray.push(irOrderLine)
                        let itemQuantity = newRecord.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'quantity',
                            line: i
                        });
                        let itemRate = newRecord.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'rate',
                            line: i
                        });
                        let amount = Number(itemQuantity) * Number(itemRate)
                        if (amount != '')
                            amount = amount.toFixed(2)
                        var tax = poItemDetails[irOrderLine];
                        tax = toFixed(tax, 2)
                        let taxamount = Number(amount) * Number(tax / 100)
                        taxamount = taxamount.toFixed(2)
                        amountTotal = Number(amountTotal) + Number(amount);
                        amountTotal = amountTotal.toFixed(2)
                        taxTotal = Number(taxTotal) + Number(taxamount)
                        taxTotal = taxTotal.toFixed(2)
                    }
                }
                total = Number(amountTotal) + Number(taxTotal)
                total = total.toFixed(2)
                /**
                 * @Description  create the values for setting the summary
                 * */
                let values = {subTotal: amountTotal, taxTotal: taxTotal, Total: total, mode: mode};
                log.debug("values", values)
                /**
                 * @Description  call the createSummary() function to create the summary
                 * */
                let summary = createSummary(values);
                newRecord.setValue({
                    fieldId: 'custbody_jj_actual_summarybox',
                    value: summary
                });
            } catch (e) {
                log.error("error @main function", e)
            }
        }
        return {beforeLoad}
    });Create a client script that will update the summary box values on editing the rate or quantity in the item receipt record.
/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
/*************************************************************************************
 ***********
 * Mohd Ali Mohideen Supermarket LLC-UAE-NS Software project
 *
 * MAMS-123
 *
 *
 *************************************************************************************
 ***********
 *
 *
 * Author: Jobin and Jismi IT Services LLP
 *
 * Date Created : 16-March-2023
 *
 * Description: This script is used for setting the summary box in the item receipt
 *
 * REVISION HISTORY
 *
 * @version 1.0 MAMS-123 : 16-March-2023 : Created the initial build by JJ0177
 *
 *
 *************************************************************************************
 **********/
define(['N/record', 'N/search'],
    /**
     * @param{record} record
     * @param{search} search
     */
    function (record, search) {
        /**
         * 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 {
                if (scriptContext.fieldId === 'quantity' || scriptContext.fieldId === 'rate' || scriptContext.fieldId === 'itemreceive') {
                    getSummaryDetails(scriptContext.currentRecord);
                }
            } catch (e) {
                log.debug({
                    title: e.name,
                    details: e
                });
            }
        }
        /**
         * Function to be executed after sublist is inserted, removed, or edited.
         *
         * @param {Object} scriptContext
         * @param {Record} scriptContext.currentRecord - Current form record
         * @param {string} scriptContext.sublistId - Sublist name
         *
         * @since 2015.2
         */
        function sublistChanged(scriptContext) {
            try {
                if (scriptContext.sublistId == 'item') {
                    getSummaryDetails(scriptContext.currentRecord);
                }
            } catch (e) {
                log.debug({
                    title: e.name,
                    details: e
                });
            }
        }
        /**
         * @description the search functcion to fetch tax percent from the item receipt.
         * @param {record} newIRRecord - record
         */
        function getSummaryDetails(newIRRecord) {
            try {
                var taxTotal = 0;
                var amountTotal = 0
                var total = 0
                var newRecord = newIRRecord;
                var poRecordsId = newRecord.getValue({
                    fieldId: 'createdfrom'
                })
                var poItemDetails = purchaseOrderDetails(poRecordsId)
                var lineCount = newRecord.getLineCount({
                    sublistId: 'item'
                });
                /**
                 * @Description loop through the item lines to fetch the rate, quantity and taxtotal
                 * */
                for (var i = 0; i < lineCount; i++) {
                    var itemIsReceive = newRecord.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'itemreceive',
                        line: i
                    });
                    /**
                     * @Description  check whether the isreceive checkbox is checked or not
                     * */
                    if (itemIsReceive) {
                        var irOrderLine = newRecord.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'orderline',
                            line: i
                        });
                        var lineArray = [];
                        lineArray.push(irOrderLine)
                        var itemQuantity = newRecord.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'quantity',
                            line: i
                        });
                        var itemRate = newRecord.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'rate',
                            line: i
                        });
                        var amount = Number(itemQuantity) * Number(itemRate)
                        if (amount != '')
                            amount = amount.toFixed(2)
                        log.debug("amount", amount)
                        var tax = poItemDetails[irOrderLine];
                        tax = toFixed(tax, 2)
                        var taxamount = Number(amount) * Number(tax / 100)
                        taxamount = taxamount.toFixed(2)
                        amountTotal = Number(amountTotal) + Number(amount);
                        amountTotal = amountTotal.toFixed(2)
                        log.debug("amountTotal", amountTotal)
                        taxTotal = Number(taxTotal) + Number(taxamount)
                        taxTotal = taxTotal.toFixed(2)
                    }
                }
                total = Number(amountTotal) + Number(taxTotal)
                total = total.toFixed(2)
                /**
                 * @Description  create the values for setting the summary
                 * */
                var values = {subTotal: amountTotal, taxTotal: taxTotal, Total: total};
                /**
                 * @Description  call the createSummary() function to create the summary
                 * */
                var summary = createSummary(values);
                document.getElementById("custom_summary").innerHTML = summary;
            } catch (e) {
                console.log("Error @getSummaryDetails", e);
                return false;
            }
        }
        /**
         * @description the fuctcion to create the summary box
         * @param {number} values - Values for creating the summary
         * @return html - html corresponding to the summary
         */
        function createSummary(values) {
            try {
                log.debug('inside createsummary');
                /**
                 * @Description  style the summary box using html code
                 * */
                var html = '<style>' +
                    'table.newtotallingtable caption {\n' +
                    '    display: table-caption !important;\n' +
                    '    margin-bottom: 10px;\n' +
                    '    font-weight: bold;\n' +
                    '    color: white;\n' +
                    '    font-size: 12px !important;\n' +
                    '    padding: 4px 0px 4px 8px;\n' +
                    '}' +
                    'table.newtotallingtable caption {\n' +
                    '    background-color: #607799;\n' +
                    '}' +
                    'caption, th {\n' +
                    '    text-align: left;\n' +
                    '}' +
                    '</style>';
                html += '<span class="bgmd totallingbg" style="display:inline-block; position:relative;left: 0px; padding: 10px 25px; margin-bottom:5px;">';
                html += '<table class="newtotallingtable" cellspacing="2" cellpadding="0px" border="0px" style="padding: 5px;\n' +
                    '    width: 217px;"><caption style="display: none;" >Summary</caption>' +
                    '<tbody style="border: 1px black solid">' +
                    '<td><div class="uir-field-wrapper" data-field-type="currency"><span id="subtotal_fs_lbl_uir_label" class="smalltextnolink uir-label "><span id="subtotal_fs_lbl" class="smalltextnolink" style="color: #262626 !important; font-size: 12px; padding-bottom:10px;">'
                html += 'SUBTOTAL</td>';
                html += '<td  style="color: #262626 !important; font-size: 13px; padding-bottom:10px;" align="right" id="subtotal">';
                html += values.subTotal + '</b></td>' +
                    '<td></td>' +
                    '</tr>';
                html += '<tr>' +
                    '<td  style="color: #262626 !important; font-size: 12px;" align="left">TAX TOTAL</td>' +
                    '<td align="right"   style="font-size: 13px; color: #262626 !important;">';
                html += values.taxTotal + '</td>' +
                    '<td></td>' +
                    '</tr>';
                html += '<tr>' +
                    '<td colspan="2"><hr style="width:200px;margin: fill"/></td>' +
                    '</tr>'
                html += '<tr>' +
                    '<td  style="color: #262626 !important; font-size: 12px;" align="left">TOTAL</td>' +
                    '<td align="right"   style="font-size: 13px; color: #262626 !important;">';
                html += values.Total + '</td>' +
                    '</tr>';
                html += '</table></div>';
                return '<div id="custom_summary">' + html + '</div>';
            } catch (e) {
                log.debug({
                    title: e.name,
                    details: e
                });
                return false;
            }
        }
        /**
         * @description function to fetch the number with two decimal point with out rounding the number
         * @param {number} number - input number for setting the two decimal point
         * @param {number} fixed - number up to decimal place we want to set the number.
         * @return number
         */
        function toFixed(num, fixed) {
            try {
                var re = new RegExp('^-?\\d+(?:\.\\d{0,' + (fixed || -1) + '})?');
                return num.toString().match(re)[0];
            } catch (e) {
                log.debug("error @ precesion function", e);
                return false;
            }
        }
        /**
         * @description search function to fetch the tax percent from the purchase order record
         * @param {number} poRecordsId - purchase order internal id
         * @return itemObj - object containing tax percentage
         */
        function purchaseOrderDetails(poRecordsId) {
            try {
                var purchaseorderSearchObj = search.create({
                    type: "purchaseorder",
                    filters:
                        [
                            ["type", "anyof", "PurchOrd"],
                            "AND",
                            ["internalid", "anyof", poRecordsId],
                            "AND",
                            ["mainline", "is", "F"],
                            "AND",
                            ["cogs", "is", "F"],
                            "AND",
                            ["shipping", "is", "F"]
                        ],
                    columns:
                        [
                            search.createColumn({name: "line", label: "Line ID"}),
                            search.createColumn({
                                name: "rate",
                                join: "taxItem",
                                label: "Rate"
                            })
                        ]
                });
                var searchResultCount = purchaseorderSearchObj.runPaged().count;
                var itemObj = {}
                if (searchResultCount > 0) {
                    purchaseorderSearchObj.run().each(function (result) {
                        var memberLine = result.getValue({
                            name: "line",
                            label: "Line ID"
                        })
                        var memberTax = result.getValue({
                            name: "rate",
                            join: "taxItem",
                            label: "Rate"
                        })
                        itemObj[memberLine] = memberTax;
                        return true
                    });
                    return itemObj;
                }
            } catch (e) {
                log.debug("error @ search function", e)
            }
        }
        return {
            fieldChanged: fieldChanged,
            sublistChanged:sublistChanged
        };
    });