protected BigDecimal getTotalRevenue(NewInvoiceContext invoice) {

    // calculate TOTAL to include result lines
    invoice.calculateTotal();
    BigDecimal invoiceAmountSum = invoice.getTotal();

    // Remove CARRIED BALANCE from tax calculation to avoid double taxation
    LOG.debug("Carried balance is %s", invoice.getCarriedBalance());
    if (null != invoice.getCarriedBalance()) {
      invoiceAmountSum = invoiceAmountSum.subtract(invoice.getCarriedBalance());
    }

    // Remove TAX ITEMS from Invoice to avoid calculating tax on tax
    for (int i = 0; i < invoice.getResultLines().size(); i++) {
      InvoiceLineDTO invoiceLine = (InvoiceLineDTO) invoice.getResultLines().get(i);
      if (null != invoiceLine.getInvoiceLineType()
          && invoiceLine.getInvoiceLineType().getId() == ServerConstants.INVOICE_LINE_TYPE_TAX) {
        invoiceAmountSum = invoiceAmountSum.subtract(invoiceLine.getAmount());
      }
    }

    return invoiceAmountSum;
  }
  /**
   * Creates a Suretax request from inputs from the invoice.
   *
   * @param invoice Invoice for which tax lines need to be calculated.
   * @return Returns instance of com.sapienter.jbilling.client.suretax.request.SuretaxRequest
   * @throws TaskException
   */
  private SuretaxRequest getAssembledRequest(NewInvoiceContext invoice, Integer userId)
      throws TaskException {
    // Construct a suretax request to get the tax lines.
    SuretaxRequest suretaxRequest = new SuretaxRequest();

    // Get the pluggable task parameters here.
    String clientNumber = getParameter(CLIENT_NUMBER, "");
    String validationKey = getParameter(VALIDATION_KEY, "");
    String responseGroup = getParameter(RESPONSE_GROUP, "03");
    String responseType = getParameter(RESPONSE_TYPE, "D");
    String numberOfDecimals = getParameter(NUMBER_OF_DECIMAL, "2");
    Integer dataYear = null;
    Integer dataMonth = null;
    try {
      dataYear = getParameter(DATA_YEAR, Calendar.getInstance().get(Calendar.YEAR));
      dataMonth = getParameter(DATA_MONTH, Calendar.getInstance().get(Calendar.MONTH) + 1);
    } catch (PluggableTaskException e) {
      LOG.debug("Exception while retrieving Data Year or Data Month");
    }

    suretaxRequest.setClientNumber(clientNumber);
    suretaxRequest.setValidationKey(validationKey);
    String uniqueTrackingCode = System.currentTimeMillis() + "";
    suretaxRequest.setClientTracking(uniqueTrackingCode);
    suretaxRequest.setDataMonth(dataMonth.toString());
    suretaxRequest.setDataYear(dataYear.toString());
    suretaxRequest.setIndustryExemption("");
    suretaxRequest.setBusinessUnit("");
    suretaxRequest.setResponseGroup(responseGroup);
    suretaxRequest.setResponseType(responseType + numberOfDecimals);
    suretaxRequest.setReturnFileCode("0");
    suretaxRequest.setTotalRevenue(getTotalRevenue(invoice).floatValue());

    List<LineItem> itemList = new ArrayList<LineItem>();
    for (InvoiceLineDTO invoiceLine : (List<InvoiceLineDTO>) invoice.getResultLines()) {

      if (invoiceLine.getInvoiceLineType().getId()
          != ServerConstants.INVOICE_LINE_TYPE_TAX.intValue()) {

        LOG.debug("Populating itemlist for invoice line: %s", invoiceLine);

        itemList.add(
            getLineItem(invoiceLine.getItem().getId(), invoiceLine, uniqueTrackingCode, userId));
      }
    }
    suretaxRequest.setItemList(itemList);
    return suretaxRequest;
  }