Create json file to upload in Eway bill portal

JJira Code: ME-76 Create eway bill JSON

The e-Way Bill system provides a provision of an offline method to generate the multiple e-way bills in one-go by the tax payers or transporters. This can be done by uploading a JSON file in the e way portal. The JSON file includes information from Netsuite Invoice. A button is placed in invoice clicking on which downloads a JSON file which is in the specified format to be uploaded to an e-way portal.

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * @NModuleScope SameAccount
 */
/**
 * Script Description: JSON Creation 
 */
/*******************************************************************************
 * * ME * *
 * **************************************************************************
 * Date:2/11/18 
 * Script name: ME JJ SL Generate Eway bill
 * Script id: customscript_jj_sl_generate_ewaybill
 * 

 ******************************************************************************/
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 invoiceSearchObj = search.create({
                type: "invoice",
                filters: [
                    ["type", "anyof", "CustInvc"], "AND", ["internalidnumber", "equalto", internalId], "AND", ["mainline", "is", "T"]
                ],
                columns: [
                    search.createColumn({
                        name: "subsidiarytaxregnum",
                        label: "Subsidiary Tax Registration Number"
                    }), search.createColumn({
                        name: "custbody_jj_sub_supply_type",
                        label: "Sub Supply Type"
                    }), search.createColumn({
                        name: "tranid",
                        label: "Document Number"
                    }), search.createColumn({
                        name: "trandate",
                        sort: search.Sort.ASC,
                        label: "Date"
                    }), search.createColumn({
                        name: "custbody_jj_transaction_type",
                        label: "Transaction Type"
                    }), search.createColumn({
                        name: "entitytaxregnum",
                        label: "Entity Tax Registration Number"
                    }), search.createColumn({
                        name: "entity",
                        label: "Name"
                    }), search.createColumn({
                        name: "shipaddress1",
                        label: "Shipping Address 1"
                    }), search.createColumn({
                        name: "shipaddress2",
                        label: "Shipping Address 2"
                    }), search.createColumn({
                        name: "shipcity",
                        label: "Shipping City"
                    }), search.createColumn({
                        name: "shipstate",
                        label: "Shipping State/Province"
                    }), search.createColumn({
                        name: "shipzip",
                        label: "Shipping Zip"
                    }), search.createColumn({
                        name: "custbody_in_place_of_supply",
                        label: "Place of Supply"
                    }), search.createColumn({
                        name: "total",
                        label: "Amount (Transaction Total)"
                    }), search.createColumn({
                        name: "custbody_str_transport_mode",
                        label: "Mode of transport"
                    }), search.createColumn({
                        name: "custbody_jj_dist_of_transport",
                        label: "Distance of transportation"
                    }), search.createColumn({
                        name: "custbody_jj_transporter_name",
                        label: "Transporter Name"
                    }), search.createColumn({
                        name: "custbody_jj_transporter_id",
                        label: "Transporter Id"
                    }), search.createColumn({
                        name: "custbody_jj_transporter_doc_date",
                        label: "Transporter Doc Date"
                    }), search.createColumn({
                        name: "custbody_jj_transporter_doc_number",
                        label: "Transporter Doc Number"
                    }), search.createColumn({
                        name: "custbody_jj_vehicle_number",
                        label: "Vehicle Number"
                    }), search.createColumn({
                        name: "custbody_jj_vehicle_type",
                        label: "Vehicle Type"
                    }), search.createColumn({
                        name: "companyname",
                        join: "customer",
                        label: "Company Name"
                    }),
                    search.createColumn({
                        name: "custbody_jj_document_type",
                        label: "Document Type"
                    }),
                    search.createColumn({
                        name: "custbody_jj_mode_of_transport",
                        label: "Mode of Transportation"
                    })
                ]
            });
            var result = invoiceSearchObj.run().getRange({
                start: 0,
                end: 1
            });
            var eway_details = {};
            eway_details.userGstin = result[0].getValue({
                name: 'subsidiarytaxregnum'
            });
            eway_details.supplyType = "O";
            eway_details.subSupplyType = Number(result[0].getValue({
                name: 'custbody_jj_sub_supply_type'
            }));
            eway_details.docType = result[0].getText({
                name: 'custbody_jj_document_type'
            });
            //log.debug("eway_details.docType", eway_details.docType);
            if (eway_details.docType == 'Tax Invoice') {
                eway_details.docType = 'INV';
            } else if (eway_details.docType == 'Bill of Supply') {
                eway_details.docType = 'BIL';
            } else {
                eway_details.docType = 'OTH';
            }
            eway_details.docNo = result[0].getValue({
                name: 'tranid'
            });
            eway_details.docDate = result[0].getValue({
                name: 'trandate',
                sort: search.Sort.ASC
            });
            eway_details.transType = Number(result[0].getValue({
                name: 'custbody_jj_transaction_type'
            }));
            eway_details.fromGstin = result[0].getValue({
                name: 'subsidiarytaxregnum'
            });
            // company info
            var companyInfo = config.load({
                type: config.Type.COMPANY_INFORMATION
            });
            //log.debug("companyInfo",companyInfo);
            eway_details.fromTrdName = companyInfo.getValue({
                fieldId: "legalname"
            });
            eway_details.fromAddr1 = '1826/II/23 New Amarnath Building';
            eway_details.fromAddr2 = 'Bhagirath Palace';
            eway_details.fromPlace = 'Chandni Chowk';
            eway_details.fromPincode = 110006;
            eway_details.fromStateCode = 7;
            eway_details.actualFromStateCode = 7;
            eway_details.toGstin = result[0].getValue({
                name: 'entitytaxregnum'
            });
            eway_details.toTrdName = result[0].getValue({
                name: "companyname",
                join: "customer"
            });
            eway_details.toAddr1 = result[0].getValue({
                name: "shipaddress1"
            });
            eway_details.toAddr2 = result[0].getValue({
                name: "shipaddress2"
            });
            eway_details.toPlace = result[0].getValue({
                name: "shipcity"
            });
            eway_details.toStateCode = result[0].getValue({
                name: "shipstate"
            });
            //log.debug("eway_details.toStateCode",eway_details.toStateCode)
            eway_details.toStateCode = eway_details.toStateCode.split("-");
            eway_details.toStateCode = Number(eway_details.toStateCode[0]);
            eway_details.actualToStateCode = eway_details.toStateCode;
            eway_details.toPincode = Number(result[0].getValue({
                name: "shipzip"
            }));
            eway_details.totInvValue = Number(result[0].getValue({
                name: "total"
            }));


            // get tax details
            if (eway_details.toStateCode == eway_details.fromStateCode) {
                eway_details.cgstValue = Number(objRecord.getValue({
                    fieldId: 'taxtotal3'
                }));
                log.debug("cgst", eway_details.cgstValue)
                eway_details.sgstValue = Number(objRecord.getValue({
                    fieldId: 'taxtotal5'
                }));
                log.debug("eway_details.cgstValue", eway_details.cgstValue)
                eway_details.igstValue = 0;
            } else {
                eway_details.igstValue = Number(objRecord.getValue({
                    fieldId: 'taxtotal2'
                }));
                log.debug("igstValue", eway_details.igstValue)
                eway_details.cgstValue = 0;
                eway_details.sgstValue = 0;
            }





            eway_details.transMode = result[0].getValue({
                name: "custbody_str_transport_mode"
            });
            eway_details.transDistance = Number(result[0].getValue({
                name: "custbody_jj_dist_of_transport"
            }));
            eway_details.transporterName = result[0].getValue({
                name: "custbody_jj_transporter_name"
            });
            eway_details.transporterId = result[0].getValue({
                name: "custbody_jj_transporter_id"
            });
            eway_details.transDocNo = result[0].getValue({
                name: "custbody_jj_transporter_doc_number"
            });
            eway_details.transDocDate = result[0].getValue({
                name: "custbody_jj_transporter_doc_date"
            });
            eway_details.vehicleNo = result[0].getValue({
                name: "custbody_jj_vehicle_number"
            });
            eway_details.vehicleType = result[0].getText({
                name: "custbody_jj_vehicle_type"
            });
            if (eway_details.vehicleType == 'Regular') {
                eway_details.vehicleType = 'R';
            } else if (eway_details.vehicleType == 'Over Dimensional Cargo') {
                eway_details.vehicleType = 'O';
            } else {
                eway_details.vehicleType = '';
            }


            eway_details.transMode = Number(result[0].getValue({
                name: "custbody_jj_mode_of_transport"
            }));
            var numLines = objRecord.getLineCount({
                sublistId: 'item'
            });
            var itemList = [];
            var itemData = {};
            for (var i = 0; i < numLines; i++) {
                itemData.productName = objRecord.getSublistText({
                    sublistId: 'item',
                    fieldId: 'item',
                    line: i
                });
                itemData.productDesc = objRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'description',
                    line: i
                });
                itemData.hsnCode = Number(objRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'custcol_in_hsn_code',
                    line: i
                }));

                var fieldLookUp = search.lookupFields({
                    type: 'customrecord_in_gst_hsn_code_for_service',
                    id: itemData.hsnCode,
                    columns: ['custrecord_in_gst_hsn_code']
                });

                itemData.hsnCode = Number(fieldLookUp.custrecord_in_gst_hsn_code);

                itemData.quantity = Number(objRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'quantity',
                    line: i
                }));
                itemData.qtyUnit = objRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'units_display',
                    line: i
                });
                if (itemData.qtyUnit == 'ea') {
                    itemData.qtyUnit = 'UNT';
                } else if (itemData.qtyUnit == 'm') {
                    itemData.qtyUnit = 'MTR';
                } else {
                    itemData.qtyUnit = ' ';
                }

                itemData.taxableAmount = objRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'amount',
                    line: i
                });
                itemList.push(itemData);
            }





            var billLists = [];
            eway_details.itemList = itemList;
            billLists.push(eway_details);
            //billLists.push(itemList);

            var eway_json = {};
            eway_json.version = '1.0.1118';
            eway_json.billLists = billLists
            log.debug("eway_json", eway_json);



            var fileObj = file.create({
                name: 'EwayBill' + eway_details.docNo + ".json",
                fileType: file.Type.JSON,
                encoding: file.Encoding.UTF8,
                contents: JSON.stringify(eway_json)
            });
            context.response.writeFile(fileObj);
            // context.response.writeFile(fileObj.getContents());
        } catch (e) {
            log.debug({
                title: e.name,
                details: e.message
            });
        }
    }
    return {
        onRequest: onRequest
    };
});

Leave a comment

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