GL Plugin script to modify GL of payment based on the account selected.

REQUIREMENT

Client needs to implement a functionality, where user will have a dropdown menu to select the type of discount account, where they want to direct the discount amount entered on each invoices applied in the payment record. Currently, their customers (retailers) give them all sort of discounts but it is always defaults to the same discount account is XXXX. 

SOLUTION

Set the GL Plugin set up in the account, add the GL plugin script, and configure it to the specific record to modify the GL lines.

/**
 * @NScriptType plugintypeimpl
 */

function customizeGlImpact(
    transactionRecord,
    standardLines,
    customLines,
    book
  ) {
    try {
   
      var rectype = transactionRecord.getRecordType();
  
      var recid = transactionRecord.getId();
      var customer = transactionRecord.getFieldValue("customer");
  
      var applyLinesResult = iterateApplyLines(transactionRecord);

      if(!(applyLinesResult.length == 1 && (applyLinesResult)[0]['accountIds'] == '101')){

      var linecount = standardLines.getCount();

      if (linecount == 0) return;

      var discAmt = 0;
  
      for (var j = 0; j < linecount; j++) {
        var accountId = standardLines.getLine(j).getAccountId();
  
        if (accountId == "101") {
          var creditAmount = standardLines.getLine(j).getCreditAmount();

          if (creditAmount > 0) {
            discAmt = creditAmount;
          }
  
          var debitAmount = standardLines.getLine(j).getDebitAmount();

          if (debitAmount > 0) {
            discAmt = debitAmount;

            var newLine1 = customLines.addNewLine();

            newLine1.setAccountId(101);

            newLine1.setCreditAmount(debitAmount);
            newLine1.setEntityId(Number(customer));
            newLine1.setMemo("Test Reversing");
  
            for (var count = 0; count < applyLinesResult.length; count++) {
              var newLine2 = customLines.addNewLine();

              newLine2.setDebitAmount(applyLinesResult[count].amounts);
              newLine2.setEntityId(Number(customer));
              newLine2.setAccountId(Number(applyLinesResult[count].accountIds));
              newLine1.setMemo("Test ");
            }
          }
        }
      }
    }
 
    } catch (e) {
      try {
        var err_title = "Unexpected error";
        var err_description = "";
        if (e) {
          if (e instanceof nlobjError) {
            err_description =
              err_description + " " + e.getCode() + "|" + e.getDetails();
          } else {
            err_description = err_description + " " + e.toString();
          }
        }

      } catch (ex) {
        nlapiLogExecution("ERROR", "Error performing error logging");
      }
    }
  
    /**
     * Funciton to iterate over the apply lines in payment record
     * @param {*} transactionRecord 
     * @returns array of account and amount
     */
    function iterateApplyLines(transactionRecord) {
      try {
        var accountArray = [];
        for (var i = 1; i <= transactionRecord.getLineItemCount("apply"); i++) {
          var apply = transactionRecord.getLineItemValue("apply", "apply", i);
          nlapiLogExecution("DEBUG", "apply", apply);
  
          if (apply === "T") {
            var discAmmount = transactionRecord.getLineItemValue(
              "apply",
              "disc",
              i
            );
            nlapiLogExecution("DEBUG", "discAmmount", discAmmount);
            if (discAmmount > 0) {
              var invoiceRef = transactionRecord.getLineItemValue(
                "apply",
                "doc",
                i
              );
  
              var accountId = nlapiLookupField(
                "invoice",
                Number(invoiceRef),
                "custbody_jj_disc_acc_tgus576",
                false
              );
              var accValueObj = {};
              var acc=''
              if(checkForParameter(accountId)){
                 acc=accountId
              }else{
                acc =101
              }
              accValueObj.accountIds = acc;
              accValueObj.amounts = discAmmount;
           
              accountArray.push(accValueObj);
            }
          }
        }
  
        var outputArray = [];
  
        for (var i = 0; i < accountArray.length; i++) {
          var currentObj = accountArray[i];
          var found = false;
  
          for (var j = 0; j < outputArray.length; j++) {
            if (outputArray[j].accountIds === currentObj.accountIds) {
              outputArray[j].amounts = (
                parseFloat(outputArray[j].amounts) +
                parseFloat(currentObj.amounts)
              ).toFixed(2);
              found = true;
              break;
            }
          }
  
          if (!found) {
            outputArray.push({
              accountIds: currentObj.accountIds,
              amounts: currentObj.amounts,
            });
          }
        }

        return outputArray;
      } catch (err) {
        try {
          var err_title = "Unexpected error";
          var err_description = "";
          if (e) {
            if (e instanceof nlobjError) {
              err_description =
                err_description + " " + e.getCode() + "|" + e.getDetails();
            } else {
              err_description = err_description + " " + e.toString();
            }
          }

        } catch (ex) {
          nlapiLogExecution("ERROR", "Error performing error logging");
        }
      }
    }

       /**
       * @description Check whether the given parameter argument has value on it or is it empty.
       * ie, To check whether a value exists in parameter
       * @param {*} parameter parameter which contains/references some values
       * @param {*} parameterName name of the parameter, not mandatory
       * @returns {Boolean} true if there exist a value, else false
       */
       function checkForParameter(parameter) {
        if (
            parameter !== "" &&
            parameter !== null &&
            parameter !== undefined &&
            parameter !== false &&
            parameter !== "null" &&
            parameter !== "undefined" &&
            parameter !== " " &&
            parameter !== "false"
        ) {
            return true;
        }
    }
  }
  

Leave a comment

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