Custom GL Plugin to modify GL of invoice

Jira Code: CNET-4

The Custom GL Lines Plug-in modifies the general ledger impact of standard and custom transactions. The Custom GL Lines Plug-in can set and read custom segment values from custom and standard lines, and from transaction records. This script modifies the GL of invoice.

/**
 * Module Description
 * 
 * Version Date Author Remarks 1.00 21 Nov 2017 Jobin and Jismi IT services LLP
 * 
 */
function customizeGlImpact(transactionRecord, standardLines, customLines, book) {
    try {
        var customer = transactionRecord.getFieldValue('entity');
        nlapiLogExecution('DEBUG', 'customer', customer);
        try {
            // to get the country of the customer
            var billCountry;
            var count = transactionRecord.getLineItemCount('iladdrbook');
            for (var i = 1; i <= count; i++) {
                // to get the default shipping box
                var defaultBill = transactionRecord.getLineItemValue(
                    'iladdrbook', 'iladdrisdefaultbill', i);

                if (defaultBill == 'T') {
                    billCountry = transactionRecord.getLineItemValue(
                        'iladdrbook', 'iladdrshipcountry', i);
                }
            }

            nlapiLogExecution('DEBUG', 'billCountry', billCountry);
        } catch (e) {
            nlapiLogExecution('DEBUG', 'Get Country', e.message);
        }

        var recordid = transactionRecord.id;
        nlapiLogExecution('DEBUG', 'recordid', recordid);



        var gl_accountsSearch = nlapiSearchRecord("customrecord_cnet4_gl_accounts", null, [],
            [
                new nlobjSearchColumn("custrecord_domestic_account_id", null, null),
                new nlobjSearchColumn("custrecord_domestic_account_name", null, null),
                new nlobjSearchColumn("custrecord_international_account_id", null, null),
                new nlobjSearchColumn("custrecord_international_account_name", null, null),
                new nlobjSearchColumn("custrecord_sales_tr_account_id", null, null),
                new nlobjSearchColumn("custrecord_sales_tr_account_name", null, null)
            ]
        );

        var columns = gl_accountsSearch[0].getAllColumns();
        var sales_tr_account = gl_accountsSearch[0].getValue(columns[4]);
        var domestic_account = gl_accountsSearch[0].getValue(columns[0]);
        var international_account = gl_accountsSearch[0].getValue(columns[2]);

        nlapiLogExecution('DEBUG', 'sales_tr_account', sales_tr_account);
        sales_tr_account = parseInt(sales_tr_account);
        domestic_account = parseInt(domestic_account);
        international_account = parseInt(international_account);
        // get items with parents
        try {

            var invoiceSearch = nlapiSearchRecord("invoice", null,
                [
                    ["type", "anyof", "CustInvc"],
                    "AND",
                    ["internalid", "is", recordid],
                    "AND",
                    ["item.internalidnumber", "greaterthan", "0"],
                    "AND",
                    ["item.type", "anyof", "Service", "Assembly", "InvtPart", "Group", "Kit", "NonInvtPart"],
                    "AND",
                    ["amount", "greaterthan", "0.00"],
                    "AND",
                    ["item.parent", "noneof", "@NONE@"],
                    "AND",
                    ["item.account", "anyof", sales_tr_account]
                ],
                [
                    new nlobjSearchColumn("tranid", null, "GROUP"),
                    new nlobjSearchColumn("parent", "item", "GROUP").setSort(false),
                    new nlobjSearchColumn("amount", null, "SUM")
                ]
            );



            if ((invoiceSearch != null) && (invoiceSearch != undefined) &&
                (invoiceSearch != '')) {
                for (var z = 0; z < invoiceSearch.length; z++) {
                    var columns = invoiceSearch[z].getAllColumns();
                    var amount = invoiceSearch[z].getValue(columns[2]);
                    // var parent = searchResults[z].getText(columns [1]);
                    var parent = invoiceSearch[z].getValue(columns[1]);

                    var parentname = nlapiLookupField('item', parent,
                        'displayname');
                    var newLine = customLines.addNewLine();
                    newLine.setDebitAmount(amount);
                    newLine.setAccountId(sales_tr_account);
                    newLine.setMemo(parentname);
                    // newLine.setEntityId(customer);

                    var newLine = customLines.addNewLine();
                    newLine.setCreditAmount(amount);
                    try {
                        if (billCountry === 'TR') {
                            newLine.setAccountId(domestic_account);
                        } else {
                            newLine.setAccountId(international_account);
                        }

                    } catch (e) {
                        nlapiLogExecution('DEBUG', 'item with parent country',
                            e.message);
                    }

                    newLine.setMemo(parentname);
                }
            }

        } catch (e) {
            nlapiLogExecution('DEBUG', 'item with parent', e.message);
        }

        // get items without parent
        try {

            var invoiceSearch1 = nlapiSearchRecord("invoice", null,
                [
                    ["type", "anyof", "CustInvc"],
                    "AND",
                    ["internalid", "is", recordid],
                    "AND",
                    ["item.internalidnumber", "greaterthan", "0"],
                    "AND",
                    ["item.type", "anyof", "Service", "Assembly", "InvtPart", "Group", "Kit", "NonInvtPart"],
                    "AND",
                    ["item.parent", "anyof", "@NONE@"],
                    "AND",
                    ["amount", "notlessthanorequalto", "0.00"],
                    "AND",
                    ["item.account", "anyof", sales_tr_account]
                ],
                [
                    new nlobjSearchColumn("tranid", null, "GROUP"),
                    new nlobjSearchColumn("item", null, "GROUP").setSort(false),
                    new nlobjSearchColumn("amount", null, "SUM")
                ]
            );


            if ((invoiceSearch1 != null) && (invoiceSearch1 != undefined) &&
                (invoiceSearch1 != '')) {
                for (var j = 0; j < invoiceSearch1.length; j++) {
                    var columns = invoiceSearch1[j].getAllColumns();
                    var amount = invoiceSearch1[j].getValue(columns[2]);
                    // var parent = searchResults[z].getText(columns [1]);
                    var parent = invoiceSearch1[j].getValue(columns[1]);

                    var parentname = nlapiLookupField('item', parent,
                        'displayname');

                    var newLine = customLines.addNewLine();
                    newLine.setDebitAmount(amount);
                    newLine.setAccountId(sales_tr_account);
                    newLine.setMemo(parentname);

                    var newLine = customLines.addNewLine();
                    newLine.setCreditAmount(amount);
                    try {
                        if (billCountry === 'TR') {
                            newLine.setAccountId(domestic_account);
                        } else {
                            newLine.setAccountId(international_account);
                        }

                    } catch (e) {
                        nlapiLogExecution('DEBUG',
                            'item without parent country', e.message);
                    }

                    newLine.setMemo(parentname);
                }
            }

        } catch (e) {
            nlapiLogExecution('DEBUG', 'item without parent', e.message);
        }



    } catch (e) {
        nlapiLogExecution('DEBUG', 'custom GL', e.message);
    }

}

Leave a comment

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