Print only selected items of Item group in an invoice

ME-118 Integrated Item PDF customization scripting

Created a suitelet script to include combining of individual components of an item group without printing the entire group components.This print is based on the custom requirements of the client. The item substitution scenario of client is included in the script.Also, the print shows only selected items which are selected by the user while creating invoice. The hsn code which is to be printed is cut short into numeric digits and the data from the custom fields in the invoice are also included in the print.

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * @NModuleScope SameAccount
 */
/**
 * Script Description: PDF Creation 
 */
/*******************************************************************************
 * * ME * *
 * **************************************************************************
 * Date:13/3/19
 * Script name: ME JJ SL Print Invoice
 * Script id: customscript_jj_sl_print_invoice
 * 
 ******************************************************************************/
define(['N/file', 'N/render', 'N/record', 'N/search', 'N/config', 'N/file', 'N/encode'], function(file, render, record, search, config, file, encode) {
    /**
     * Definition of the Suitelet script trigger point.
     *
     * @param {Object} context
     * @param {ServerRequest} context.request - Encapsulation of the incoming request
     * @param {ServerResponse} context.response - Encapsulation of the Suitelet response
     * @Since 2015.2
     */
    function onRequest(context) {
        try {
            var internalId = context.request.parameters.intenalId;
            var objRecord = record.load({
                type: "invoice",
                id: internalId
            });


            var items = [];
            var length = objRecord.getLineCount({ sublistId: 'item' });
            //log.debug("line", length)
            var itemgroup_count = 0;
            for (var i = 0; i < length; i++) {
                var itemtype = objRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'itemtype',
                    line: i
                });
                if (itemtype == 'Group')
                    itemgroup_count++;
            }


            if (itemgroup_count > 0) {
                log.debug("integrated item")
                var myFile = render.create();

                var template = 112;
                myFile.setTemplateById(template);


                myFile.addRecord({
                    templateName: 'record',
                    record: objRecord
                });


                var quantity, amount, rate, grossamt, taxref, hsncode;
                for (var i = 0; i < length; i++) {
                    var itemtype = objRecord.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'itemtype',
                        line: i
                    });
                    //log.debug("itemtype", itemtype + "i");

                    if (itemtype == 'Group') {
                        quantity = objRecord.getSublistValue({ sublistId: 'item', fieldId: 'quantity', line: i });
                        var component_items = [];
                        for (var j = i + 1; j < length; j++) {
                            var itemtype_new = objRecord.getSublistValue({
                                sublistId: 'item',
                                fieldId: 'itemtype',
                                line: j
                            });
                            if (itemtype_new == 'EndGroup') {
                                amount = objRecord.getSublistValue({ sublistId: 'item', fieldId: 'amount', line: j });
                                rate = parseFloat(amount / quantity).toFixed(2);
                                grossamt = objRecord.getSublistValue({ sublistId: 'item', fieldId: 'grossamt', line: j });

                                break;
                            } else {

                                // get hsn code of component item
                                hsncode = objRecord.getSublistText({ sublistId: 'item', fieldId: 'custcol_in_hsn_code', line: j });

                                taxref = objRecord.getSublistValue({ sublistId: 'item', fieldId: 'taxdetailsreference', line: j });

                                var show_item = objRecord.getSublistValue({ sublistId: 'item', fieldId: 'custcol_jj_me71_show_item', line: j });
                                if (show_item != true)
                                    continue;
                                var is_substitute = objRecord.getSublistValue({ sublistId: 'item', fieldId: 'custcol_jj_substitute_item', line: j });
                                if (is_substitute == true)
                                    component_items.push(objRecord.getSublistValue({ sublistId: 'item', fieldId: 'custcol_jj_me102_substitute_item', line: j }));
                                else
                                    component_items.push(objRecord.getSublistText({ sublistId: 'item', fieldId: 'item', line: j }));
                            }
                        }

                        var component_arr = ' ';
                        for (var k = 0; k < component_items.length; k++) {
                            if (k == 0)
                                component_arr = component_items[k];

                            else
                                component_arr = component_arr + ',' + component_items[k];
                        }
                        //log.debug("component_arr", component_arr);
                        /*items.push({
                           components: component_arr});*/
                        if ((hsncode != '') && (hsncode != null) && (hsncode != undefined)) {
                            var lineNumber = objRecord.findSublistLineWithValue({
                                sublistId: 'taxdetails',
                                fieldId: 'taxdetailsreference',
                                value: taxref
                            });
                            log.debug("lineNumber", lineNumber)
                            var taxrate = objRecord.getSublistText({ sublistId: 'taxdetails', fieldId: 'taxrate', line: lineNumber });
                            var taxrate1 = objRecord.getSublistValue({ sublistId: 'taxdetails', fieldId: 'taxrate', line: lineNumber });
                            var taxamount = parseFloat((amount * taxrate1) / 100).toFixed(2);

                            var hsncode_arr = hsncode.split(" ");
                            //log.debug("hsncode_arr",hsncode_arr)
                            hsncode = hsncode_arr[0];
                        }






                        items.push({
                            item: objRecord.getSublistText({ sublistId: 'item', fieldId: 'item', line: i }),
                            description: objRecord.getSublistValue({ sublistId: 'item', fieldId: 'description', line: i }),
                            quantity: objRecord.getSublistValue({ sublistId: 'item', fieldId: 'quantity', line: i }),
                            unit: 'each',
                            hsncode: hsncode,
                            taxrate: taxrate,
                            taxamount: taxamount,
                            rate: rate,
                            amount: amount,
                            grossamt: grossamt,
                            components: component_arr
                        });
                        log.debug("component_items", component_items);

                        i = j;

                    } else if (itemtype == 'EndGroup') {
                        continue;
                    } else {

                        var hsncode = objRecord.getSublistText({ sublistId: 'item', fieldId: 'custcol_in_hsn_code', line: i });
                        var taxrate;
                        if ((hsncode != '') && (hsncode != null) && (hsncode != undefined)) {
                            var hsncode_arr = hsncode.split(" ");
                            //log.debug("hsncode_arr",hsncode_arr)
                            hsncode = hsncode_arr[0];
                            var taxref = objRecord.getSublistValue({ sublistId: 'item', fieldId: 'taxdetailsreference', line: i });
                            var lineNumber = objRecord.findSublistLineWithValue({
                                sublistId: 'taxdetails',
                                fieldId: 'taxdetailsreference',
                                value: taxref
                            });

                            taxrate = objRecord.getSublistText({ sublistId: 'taxdetails', fieldId: 'taxrate', line: lineNumber });
                        }


                        items.push({
                            item: objRecord.getSublistText({ sublistId: 'item', fieldId: 'item', line: i }),
                            description: objRecord.getSublistValue({ sublistId: 'item', fieldId: 'description', line: i }),
                            quantity: objRecord.getSublistValue({ sublistId: 'item', fieldId: 'quantity', line: i }),
                            unit: objRecord.getSublistValue({ sublistId: 'item', fieldId: 'units_display', line: i }),
                            hsncode: hsncode,
                            taxrate: taxrate,
                            taxamount: objRecord.getSublistValue({ sublistId: 'item', fieldId: 'taxamount', line: i }),
                            grossamt: objRecord.getSublistValue({ sublistId: 'item', fieldId: 'grossamt', line: i }),
                            rate: objRecord.getSublistValue({ sublistId: 'item', fieldId: 'rate', line: i }),
                            amount: objRecord.getSublistValue({ sublistId: 'item', fieldId: 'amount', line: i })
                        });
                    }

                }

                log.debug("items", items)



                myFile.addCustomDataSource({
                    format: render.DataSource.JSON,
                    alias: "items",
                    data: JSON.stringify({ items: items })
                });
                var invoicePdf = myFile.renderAsPdf();
                context.response.writeFile(invoicePdf, true);


            } else {
                log.debug("normal itm")
                var transactionFile = render.transaction({
                    entityId: Number(internalId),
                    printMode: render.PrintMode.PDF,
                    formId: 105,
                    inCustLocale: true
                });

                context.response.writeFile(transactionFile, true);



            }
        } catch (er) {
            log.debug('error', er)
        }
    }
    return {
        onRequest: onRequest
    };
});

Leave a comment

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