Exemplo n.º 1
0
  private void addHeaderCategories(Invoice invoice, Document doc, Element parent) {
    boolean entreprise = invoice.getProvider().isEntreprise();
    LinkedHashMap<String, XMLInvoiceHeaderCategoryDTO> headerCategories =
        new LinkedHashMap<String, XMLInvoiceHeaderCategoryDTO>();
    List<CategoryInvoiceAgregate> categoryInvoiceAgregates =
        new ArrayList<CategoryInvoiceAgregate>();
    for (InvoiceAgregate invoiceAgregate : invoice.getInvoiceAgregates()) {
      if (invoiceAgregate instanceof CategoryInvoiceAgregate) {
        CategoryInvoiceAgregate categoryInvoiceAgregate = (CategoryInvoiceAgregate) invoiceAgregate;
        categoryInvoiceAgregates.add(categoryInvoiceAgregate);
      }
    }
    Collections.sort(
        categoryInvoiceAgregates,
        new Comparator<CategoryInvoiceAgregate>() {
          public int compare(CategoryInvoiceAgregate c0, CategoryInvoiceAgregate c1) {
            if (c0.getInvoiceCategory() != null
                && c1.getInvoiceCategory() != null
                && c0.getInvoiceCategory().getSortIndex() != null
                && c1.getInvoiceCategory().getSortIndex() != null) {
              return c0.getInvoiceCategory()
                  .getSortIndex()
                  .compareTo(c1.getInvoiceCategory().getSortIndex());
            }
            return 0;
          }
        });

    for (CategoryInvoiceAgregate categoryInvoiceAgregate : categoryInvoiceAgregates) {
      InvoiceCategory invoiceCategory = categoryInvoiceAgregate.getInvoiceCategory();
      System.out.println("invoiceCategory:::" + invoiceCategory.getDescription());
      XMLInvoiceHeaderCategoryDTO headerCat = null;
      if (headerCategories.containsKey(invoiceCategory.getCode())) {
        headerCat = headerCategories.get(invoiceCategory.getCode());
        headerCat.addAmountWithoutTax(categoryInvoiceAgregate.getAmountWithoutTax());
        headerCat.addAmountWithTax(categoryInvoiceAgregate.getAmountWithTax());
      } else {
        headerCat = new XMLInvoiceHeaderCategoryDTO();
        headerCat.setDescription(invoiceCategory.getDescription());
        headerCat.setCode(invoiceCategory.getCode());
        headerCat.setAmountWithoutTax(categoryInvoiceAgregate.getAmountWithoutTax());
        headerCat.setAmountWithTax(categoryInvoiceAgregate.getAmountWithTax());
        headerCategories.put(invoiceCategory.getCode(), headerCat);
      }
      if (entreprise) {
        Set<SubCategoryInvoiceAgregate> subCategoryInvoiceAgregates =
            categoryInvoiceAgregate.getSubCategoryInvoiceAgregates();

        for (SubCategoryInvoiceAgregate subCatInvoiceAgregate : subCategoryInvoiceAgregates) {
          List<RatedTransaction> transactions = subCatInvoiceAgregate.getRatedtransactions();

          logger.info(
              "subCatInvoiceAgregate code="
                  + subCatInvoiceAgregate.getId()
                  + ",transactions="
                  + subCatInvoiceAgregate.getRatedtransactions().size());

          Collections.sort(
              transactions,
              new Comparator<RatedTransaction>() {
                public int compare(RatedTransaction c0, RatedTransaction c1) {
                  if (c0.getChargeApplication() != null && c1.getChargeApplication() != null) {
                    return c0.getChargeApplication()
                        .getId()
                        .compareTo(c1.getChargeApplication().getId());
                  }
                  return 0;
                }
              });
          Map<String, RatedTransaction> headerRatedTransactions = headerCat.getRatedtransactions();
          for (RatedTransaction ratedTrnsaction : transactions) {
            BigDecimal transactionAmountWithTax = ratedTrnsaction.getAmount1WithTax();
            if (transactionAmountWithTax == null
                || transactionAmountWithTax.equals(BigDecimal.ZERO)) {
              continue;
            }
            RatedTransaction headerRatedTransaction = null;
            logger.info(
                "headerRatedTransaction id="
                    + ratedTrnsaction.getId()
                    + ",code="
                    + ratedTrnsaction.getUsageCode()
                    + ",Amount1WithoutTax="
                    + ratedTrnsaction.getAmount1WithoutTax());

            if (headerRatedTransactions.containsKey(ratedTrnsaction.getUsageCode())) {
              headerRatedTransaction = headerRatedTransactions.get(ratedTrnsaction.getUsageCode());
              headerRatedTransaction.setAmount1WithoutTax(
                  headerRatedTransaction
                      .getAmount1WithoutTax()
                      .add(ratedTrnsaction.getAmount1WithoutTax()));
              headerRatedTransaction.setAmount1WithTax(
                  headerRatedTransaction.getAmount1WithTax().add(transactionAmountWithTax));

            } else {
              headerRatedTransaction = new RatedTransaction();
              headerRatedTransaction.setUsageCode(ratedTrnsaction.getUsageCode());
              headerRatedTransaction.setDescription(
                  ratedTrnsaction.getChargeApplication() != null
                      ? ratedTrnsaction.getChargeApplication().getDescription()
                      : "");
              headerRatedTransaction.setAmount1WithoutTax(ratedTrnsaction.getAmount1WithoutTax());
              headerRatedTransaction.setAmount1WithTax(ratedTrnsaction.getAmount1WithTax());
              headerRatedTransaction.setTaxPercent(ratedTrnsaction.getTaxPercent());
              headerRatedTransactions.put(ratedTrnsaction.getUsageCode(), headerRatedTransaction);
            }
            logger.info(
                "addHeaderCategories headerRatedTransaction amoutHT="
                    + headerRatedTransaction.getAmount1WithoutTax());
          }

          logger.info(
              "addHeaderCategories headerRatedTransactions.size=" + headerRatedTransactions.size());
          logger.info(
              "addHeaderCategories headerCat.getRatedtransactions().size="
                  + headerCat.getRatedtransactions().size());
        }
      }
    }
    addHeaderCategories(headerCategories, doc, parent, entreprise);
  }
Exemplo n.º 2
0
  public static void addCategories(
      UserAccount userAccount,
      Invoice invoice,
      Document doc,
      Element parent,
      boolean generateSubCat) {

    Element categories = doc.createElement("categories");
    parent.appendChild(categories);
    boolean entreprise = invoice.getProvider().isEntreprise();
    List<CategoryInvoiceAgregate> categoryInvoiceAgregates =
        new ArrayList<CategoryInvoiceAgregate>();
    for (InvoiceAgregate invoiceAgregate : invoice.getInvoiceAgregates()) {
      if (invoiceAgregate.getUserAccount().getId() == userAccount.getId()) {
        if (invoiceAgregate instanceof CategoryInvoiceAgregate) {
          CategoryInvoiceAgregate categoryInvoiceAgregate =
              (CategoryInvoiceAgregate) invoiceAgregate;

          categoryInvoiceAgregates.add(categoryInvoiceAgregate);
        }
      }
    }
    Collections.sort(
        categoryInvoiceAgregates,
        new Comparator<CategoryInvoiceAgregate>() {
          public int compare(CategoryInvoiceAgregate c0, CategoryInvoiceAgregate c1) {
            if (c0.getInvoiceCategory() != null
                && c1.getInvoiceCategory() != null
                && c0.getInvoiceCategory().getSortIndex() != null
                && c1.getInvoiceCategory().getSortIndex() != null) {
              return c0.getInvoiceCategory()
                  .getSortIndex()
                  .compareTo(c1.getInvoiceCategory().getSortIndex());
            }
            return 0;
          }
        });
    for (CategoryInvoiceAgregate categoryInvoiceAgregate : categoryInvoiceAgregates) {
      InvoiceCategory invoiceCategory = categoryInvoiceAgregate.getInvoiceCategory();
      Element category = doc.createElement("category");
      category.setAttribute(
          "label",
          invoiceCategory != null && invoiceCategory.getDescription() != null
              ? invoiceCategory.getDescription()
              : "");
      category.setAttribute(
          "code",
          invoiceCategory != null && invoiceCategory.getCode() != null
              ? invoiceCategory.getCode()
              : "");
      categories.appendChild(category);

      Element amountWithoutTax = doc.createElement("amountWithoutTax");
      Text amountWithoutTaxTxt =
          doc.createTextNode(round(categoryInvoiceAgregate.getAmountWithoutTax()));
      amountWithoutTax.appendChild(amountWithoutTaxTxt);
      category.appendChild(amountWithoutTax);

      Element amountWithTax = doc.createElement("amountWithTax");
      Text amountWithTaxTxt = doc.createTextNode(round(categoryInvoiceAgregate.getAmountWithTax()));
      amountWithTax.appendChild(amountWithTaxTxt);
      category.appendChild(amountWithTax);

      if (generateSubCat) {
        Element subCategories = doc.createElement("subCategories");
        category.appendChild(subCategories);
        Set<SubCategoryInvoiceAgregate> subCategoryInvoiceAgregates =
            categoryInvoiceAgregate.getSubCategoryInvoiceAgregates();

        for (SubCategoryInvoiceAgregate subCatInvoiceAgregate : subCategoryInvoiceAgregates) {
          InvoiceSubCategory invoiceSubCat = subCatInvoiceAgregate.getInvoiceSubCategory();
          List<RatedTransaction> transactions = subCatInvoiceAgregate.getRatedtransactions();

          boolean createSubCatElement = false;
          for (RatedTransaction ratedTrnsaction : transactions) {
            BigDecimal transactionAmount =
                entreprise
                    ? ratedTrnsaction.getAmount1WithTax()
                    : ratedTrnsaction.getAmount2WithoutTax();
            if (transactionAmount != null && !transactionAmount.equals(BigDecimal.ZERO)) {
              createSubCatElement = true;
              break;
            }
          }
          if (!createSubCatElement) {
            continue;
          }

          Element subCategory = doc.createElement("subCategory");
          subCategories.appendChild(subCategory);
          subCategory.setAttribute(
              "label", invoiceSubCat != null ? invoiceSubCat.getDescription() + "" : "");

          Collections.sort(
              transactions,
              new Comparator<RatedTransaction>() {
                public int compare(RatedTransaction c0, RatedTransaction c1) {
                  if (c0.getChargeApplication() != null && c1.getChargeApplication() != null) {
                    return c0.getChargeApplication()
                        .getId()
                        .compareTo(c1.getChargeApplication().getId());
                  }
                  return 0;
                }
              });

          for (RatedTransaction ratedTrnsaction : transactions) {
            BigDecimal transactionAmount =
                entreprise
                    ? ratedTrnsaction.getAmount1WithTax()
                    : ratedTrnsaction.getAmount2WithoutTax();
            if (transactionAmount != null && !transactionAmount.equals(BigDecimal.ZERO)) {

              Element line = doc.createElement("line");
              line.setAttribute(
                  "code",
                  ratedTrnsaction.getUsageCode() != null ? ratedTrnsaction.getUsageCode() : "");
              line.setAttribute("taxPercent", round(ratedTrnsaction.getTaxPercent()));

              Element lebel = doc.createElement("label");
              Text lebelTxt =
                  doc.createTextNode(
                      ratedTrnsaction.getDescription() != null
                          ? ratedTrnsaction.getDescription()
                          : "");
              lebel.appendChild(lebelTxt);
              line.appendChild(lebel);

              Element lineAmountWithoutTax = doc.createElement("amountWithoutTax");
              Text lineAmountWithoutTaxTxt =
                  doc.createTextNode(round(ratedTrnsaction.getAmount1WithoutTax()));
              lineAmountWithoutTax.appendChild(lineAmountWithoutTaxTxt);
              line.appendChild(lineAmountWithoutTax);

              Element lineAmountWithTax = doc.createElement("amountWithTax");
              Text lineAmountWithTaxTxt =
                  doc.createTextNode(
                      round(
                          entreprise
                              ? ratedTrnsaction.getAmount1WithTax()
                              : ratedTrnsaction.getAmount2WithoutTax()));
              lineAmountWithTax.appendChild(lineAmountWithTaxTxt);
              line.appendChild(lineAmountWithTax);

              Element unitPrice1 = doc.createElement("unitPrice1");
              Text unitPrice1Txt = doc.createTextNode(round(ratedTrnsaction.getUnitPrice1()));
              unitPrice1.appendChild(unitPrice1Txt);
              line.appendChild(unitPrice1);

              Element unitPrice2 = doc.createElement("unitPrice2");
              Text unitPrice2Txt = doc.createTextNode(round(ratedTrnsaction.getUnitPrice2()));
              unitPrice2.appendChild(unitPrice2Txt);
              line.appendChild(unitPrice2);

              Element quantity = doc.createElement("quantity");
              Text quantityTxt =
                  doc.createTextNode(
                      ratedTrnsaction.getUsageQuantity() != null
                          ? ratedTrnsaction.getUsageQuantity() + ""
                          : "");
              quantity.appendChild(quantityTxt);
              line.appendChild(quantity);

              subCategory.appendChild(line);
            }
          }
        }
      }
    }
  }