private String composeLine(InvoiceDTO invoice, InvoiceLineDTO invoiceLine, Integer userId) {
    StringBuffer line = new StringBuffer();
    ContactBL contact = new ContactBL();
    contact.set(userId);

    // cono
    line.append("\"" + emptyIfNull(contact.getEntity().getPostalCode()) + "\"");
    line.append(",");
    // custno
    line.append("\"" + userId + "\"");
    line.append(",");
    // naddrcode
    line.append("\"" + "000" + "\"");
    line.append(",");
    // lookupnm
    line.append("\"" + emptyIfNull(contact.getEntity().getOrganizationName()) + "\"");
    line.append(",");
    // totallineamt
    line.append("\"" + invoiceLine.getAmount() + "\"");
    line.append(",");
    // period
    line.append("\"" + new SimpleDateFormat("yyyyMM").format(invoice.getCreateDatetime()) + "\"");
    line.append(",");
    // name
    line.append("\"" + emptyIfNull(contact.getEntity().getOrganizationName()) + "\"");
    line.append(",");
    // deliveryaddr
    line.append("\"" + emptyIfNull(contact.getEntity().getAddress1()) + "\"");
    line.append(",");
    // city
    line.append("\"" + emptyIfNull(contact.getEntity().getCity()) + "\"");
    line.append(",");
    // state
    line.append("\"" + emptyIfNull(contact.getEntity().getStateProvince()) + "\"");
    line.append(",");
    // zip5
    line.append("\"" + emptyIfNull(contact.getEntity().getPostalCode()) + "\"");
    line.append(",");
    // totdue - round to two decimals
    line.append("\"" + new UserBL().getBalance(userId).round(new MathContext(2)) + "\"");
    line.append(",");
    // qty
    line.append("\"" + invoiceLine.getQuantity() + "\"");
    line.append(",");
    // description
    line.append("\"" + invoiceLine.getDescription() + "\"");
    line.append(",");
    // invoiceno
    line.append("\"" + invoice.getNumber() + "\"");
    line.append(",");
    // custstatus
    line.append("\"" + "TRUE" + "\"");

    LOG.debug("Line to export:" + line);
    return line.toString();
  }
  private LineItem getLineItem(
      Integer itemId, InvoiceLineDTO invoiceLine, String uniqueTrackingCode, Integer userId)
      throws TaskException {
    // Get the meta field names
    String secondaryZipCodeExtensionFieldname =
        getParameter(SECONDARY_ZIP_CODE_EXTN_FIELDNAME, "Secondary Zip code extension");
    String secondaryZipCodeFieldname =
        getParameter(SECONDARY_ZIP_CODE_FIELDNAME, "Secondary Zip code");
    String billingZipCodeFieldname =
        getParameter(BILLING_ZIP_CODE_FIELDNAME, "Billing Zip code extension");
    String regulatoryCodeFieldname = getParameter(REGULATORY_CODE_FIELDNAME, "Regulatory Code");
    String salesTypeCodeFieldname = getParameter(SALES_TYPE_CODE_FIELDNAME, "Sales Type Code");
    String taxExemptionCodeFieldname =
        getParameter(TAX_EXEMPTION_CODE_FIELDNAME, "Tax exemption code");
    String transactionTypeCodeFieldname =
        getParameter(TRANSACTION_TYPE_CODE_FIELDNAME, "Transaction Type Code");

    LineItem lineItem = new LineItem();
    lineItem.setBillToNumber(""); // TODO: need to be addressed ?
    String customerNumber = null;
    List<NewInvoiceContext.OrderContext> orders = invoice.getOrders();
    // We need to get the fresh item from the database because
    // the item in the invoiceLine doesn't yet contain meta fields.
    ItemDTO item = new ItemDAS().find(itemId);
    OrderDTO orderDTO = null;
    UserDTO invoiceToUser = null;
    for (NewInvoiceContext.OrderContext orderCtx : orders) {
      if (orderCtx.order.getId().intValue() == invoiceLine.getOrder().getId()) {
        orderDTO = orderCtx.order;
        break;
      }
    }

    if (null == orderDTO) {
      orderDTO = orders.get(0).order;
    }

    invoiceToUser = new UserDAS().find(userId);
    customerNumber = invoiceToUser.getCustomer().getId() + "";

    lineItem.setCustomerNumber(customerNumber);
    lineItem.setInvoiceNumber("JB" + uniqueTrackingCode);
    lineItem.setLineNumber(""); // TODO: need to be addressed ?
    lineItem.setOrigNumber(""); // TODO: need to be addressed ?

    MetaFieldValue<String> p2PPlus4 =
        invoiceToUser.getCustomer().getMetaField(secondaryZipCodeExtensionFieldname);
    if (p2PPlus4 != null) {
      lineItem.setP2PPlus4(p2PPlus4.getValue());
    } else {
      lineItem.setP2PPlus4("");
    }

    MetaFieldValue<String> p2PZipcode =
        invoiceToUser.getCustomer().getMetaField(secondaryZipCodeFieldname);
    if (p2PZipcode != null) {
      lineItem.setP2PZipcode(p2PZipcode.getValue());
    } else {
      lineItem.setP2PZipcode("");
    }

    MetaFieldValue<String> plus4 =
        invoiceToUser.getCustomer().getMetaField(billingZipCodeFieldname);
    if (plus4 != null) {
      lineItem.setPlus4(plus4.getValue());
    } else {
      lineItem.setPlus4("");
    }

    LOG.debug("Meta fields: p2PPlus4: %s, p2PZipcode: %s, plus4:%s", p2PPlus4, p2PZipcode, plus4);

    MetaFieldValue<String> regulatoryCode = null;
    regulatoryCode = item.getMetaField(regulatoryCodeFieldname);
    if (regulatoryCode == null
        || regulatoryCode.getValue() == null
        || regulatoryCode.getValue().isEmpty()) {
      lineItem.setRegulatoryCode("00");
    } else {
      lineItem.setRegulatoryCode(regulatoryCode.getValue());
    }

    lineItem.setRevenue(invoiceLine.getAmount().floatValue());

    MetaFieldValue<String> salesTypeCode = orderDTO.getMetaField(salesTypeCodeFieldname);
    if (salesTypeCode == null
        || salesTypeCode.getValue() == null
        || salesTypeCode.getValue().isEmpty()) {
      lineItem.setSalesTypeCode("R");
    } else {
      lineItem.setSalesTypeCode(salesTypeCode.getValue());
    }

    lineItem.setSeconds(
        invoiceLine.getQuantity() != null ? invoiceLine.getQuantity().intValue() : 0);
    List<String> taxExemptionCodeList = new ArrayList<String>();
    // First get the tax exemption code from the customer
    MetaFieldValue<String> taxExemptionCode =
        invoiceToUser.getCustomer().getMetaField(taxExemptionCodeFieldname);
    LOG.debug("Tax exemption code from customer: %s", taxExemptionCode);
    if (!(taxExemptionCode != null
        && taxExemptionCode.getValue() != null
        && !taxExemptionCode.getValue().isEmpty())) {
      taxExemptionCode = item.getMetaField(taxExemptionCodeFieldname);
      LOG.debug("Tax exemption code from product: %s", taxExemptionCode);
    }
    if (taxExemptionCode == null) {
      LOG.debug("Setting tax exemption code to be 00");
      taxExemptionCodeList.add("00");
    } else {
      taxExemptionCodeList.add(taxExemptionCode.getValue());
    }
    LOG.debug(
        "Meta fields: regulatoryCode: %s, salesTypeCode: %s, taxExemptionCode: %s",
        regulatoryCode, salesTypeCode, taxExemptionCode);
    lineItem.setTaxExemptionCodeList(taxExemptionCodeList);
    lineItem.setTaxIncludedCode("0");

    lineItem.setTermNumber("");

    // TODO: Need to check if trans date will be current date or based on data year and data month ?
    lineItem.setTransDate("07-10-2012");

    MetaFieldValue<String> transTypeCode = null;
    transTypeCode = item.getMetaField(transactionTypeCodeFieldname);

    if (transTypeCode == null
        || transTypeCode.getValue() == null
        || transTypeCode.getValue().isEmpty()) {
      throw new SessionInternalError(
          "No valid transaction type code found on the product",
          new String[] {"ItemDTOEx,transTypeCode,no.valid.transactionTypeCode.on.product"});
    }
    lineItem.setTransTypeCode(transTypeCode.getValue());
    lineItem.setUnits(invoiceLine.getQuantity() != null ? invoiceLine.getQuantity().intValue() : 0);
    lineItem.setUnitType("00");

    if (invoiceToUser.getContact().getPostalCode() != null
        && plus4 != null
        && plus4.getValue() != null
        && !plus4.getValue().isEmpty()) {
      lineItem.setZipcode(invoiceToUser.getContact().getPostalCode());
      lineItem.setTaxSitusRule("05");
    } else if (invoiceToUser.getContact().getPostalCode() != null
        && (plus4 == null || plus4.getValue() == null || plus4.getValue().isEmpty())) {
      lineItem.setZipcode(invoiceToUser.getContact().getPostalCode());
      lineItem.setPlus4("0000");
      lineItem.setTaxSitusRule("05");
    }
    return lineItem;
  }
Ejemplo n.º 3
0
  public static InvoiceWS getWS(InvoiceDTO i) {
    if (i == null) {
      return null;
    }
    InvoiceWS retValue = new InvoiceWS();
    retValue.setId(i.getId());
    retValue.setCreateDateTime(i.getCreateDatetime());
    retValue.setCreateTimeStamp(i.getCreateTimestamp());
    retValue.setLastReminder(i.getLastReminder());
    retValue.setDueDate(i.getDueDate());
    retValue.setTotal(i.getTotal());
    retValue.setToProcess(i.getToProcess());
    retValue.setStatusId(i.getInvoiceStatus().getId());
    retValue.setBalance(i.getBalance());
    retValue.setCarriedBalance(i.getCarriedBalance());
    retValue.setInProcessPayment(i.getInProcessPayment());
    retValue.setDeleted(i.getDeleted());
    retValue.setPaymentAttempts(i.getPaymentAttempts());
    retValue.setIsReview(i.getIsReview());
    retValue.setCurrencyId(i.getCurrency().getId());
    retValue.setCustomerNotes(i.getCustomerNotes());
    retValue.setNumber(i.getPublicNumber());
    retValue.setOverdueStep(i.getOverdueStep());
    retValue.setUserId(i.getBaseUser().getId());

    Integer delegatedInvoiceId = i.getInvoice() == null ? null : i.getInvoice().getId();
    Integer userId = i.getBaseUser().getId();
    Integer payments[] = new Integer[i.getPaymentMap().size()];
    com.sapienter.jbilling.server.entity.InvoiceLineDTO invoiceLines[] =
        new com.sapienter.jbilling.server.entity.InvoiceLineDTO[i.getInvoiceLines().size()];
    Integer orders[] = new Integer[i.getOrderProcesses().size()];

    int f;
    f = 0;
    for (PaymentInvoiceMapDTO p : i.getPaymentMap()) {
      payments[f++] = p.getPayment().getId();
    }
    f = 0;
    for (OrderProcessDTO orderP : i.getOrderProcesses()) {
      orders[f++] = orderP.getPurchaseOrder().getId();
    }
    f = 0;
    for (InvoiceLineDTO line : i.getInvoiceLines()) {
      invoiceLines[f++] =
          new com.sapienter.jbilling.server.entity.InvoiceLineDTO(
              line.getId(),
              line.getDescription(),
              line.getAmount(),
              line.getPrice(),
              line.getQuantity(),
              line.getDeleted(),
              line.getItem() == null ? null : line.getItem().getId(),
              line.getSourceUserId(),
              line.getIsPercentage());
    }

    retValue.setDelegatedInvoiceId(delegatedInvoiceId);
    retValue.setUserId(userId);
    retValue.setPayments(payments);
    retValue.setInvoiceLines(invoiceLines);
    retValue.setOrders(orders);

    retValue.setMetaFields(MetaFieldBL.convertMetaFieldsToWS(new UserBL().getEntityId(userId), i));

    return retValue;
  }
Ejemplo n.º 4
0
  public void createLines(NewInvoiceDTO newInvoice) {
    Collection invoiceLines = invoice.getInvoiceLines();

    // Now create all the invoice lines, from the lines in the DTO
    // put there by the invoice composition pluggable tasks
    InvoiceLineDAS invoiceLineDas = new InvoiceLineDAS();

    // get the result DTO lines
    Iterator dueInvoiceLines = newInvoice.getResultLines().iterator();
    // go over the DTO lines, creating one invoice line for each

    // #2196 - GET Invoice Rounding Preference for entity entityId
    PreferenceBL pref = new PreferenceBL();
    Integer entityId = newInvoice.getEntityId();
    if (null == entityId) {
      entityId = newInvoice.getBaseUser().getEntity().getId();
    }

    int decimals = Constants.BIGDECIMAL_SCALE;
    try {
      pref.set(entityId, Constants.PREFERENCE_INVOICE_DECIMALS);
      decimals = pref.getInt();
    } catch (EmptyResultDataAccessException e) {
      // ignore
    }
    // #2196

    while (dueInvoiceLines.hasNext()) {
      InvoiceLineDTO lineToAdd = (InvoiceLineDTO) dueInvoiceLines.next();
      // define if the line is a percentage or not
      lineToAdd.setIsPercentage(0);
      if (lineToAdd.getItem() != null) {
        try {
          ItemBL item = new ItemBL(lineToAdd.getItem());
          if (item.getEntity().getPercentage() != null) {
            lineToAdd.setIsPercentage(1);
          }
        } catch (SessionInternalError e) {
          LOG.error("Could not find item to create invoice line " + lineToAdd.getItem().getId());
        }
      }

      // #2196 - Use Invoice Rounding Preference to round Invoice Lines
      if (null != lineToAdd.getAmount()) {
        lineToAdd.setAmount(lineToAdd.getAmount().setScale(decimals, Constants.BIGDECIMAL_ROUND));
      }
      // #2196

      // create the database row
      InvoiceLineDTO newLine =
          invoiceLineDas.create(
              lineToAdd.getDescription(),
              lineToAdd.getAmount(),
              lineToAdd.getQuantity(),
              lineToAdd.getPrice(),
              lineToAdd.getTypeId(),
              lineToAdd.getItem(),
              lineToAdd.getSourceUserId(),
              lineToAdd.getIsPercentage());

      // update the invoice-lines relationship
      newLine.setInvoice(invoice);
      invoiceLines.add(newLine);
    }
    getHome().save(invoice);
    EventManager.process(new NewInvoiceEvent(invoice));
  }