public static void addUserAccounts(Invoice invoice, Document doc, Element parent) { Element userAccounts = doc.createElement("userAccounts"); parent.appendChild(userAccounts); BillingAccount billingAccount = invoice.getBillingAccount(); for (UserAccount userAccount : billingAccount.getUsersAccounts()) { Element userAccountTag = doc.createElement("userAccount"); userAccountTag.setAttribute("id", userAccount.getId() + ""); userAccountTag.setAttribute( "code", userAccount.getCode() != null ? userAccount.getCode() : ""); userAccountTag.setAttribute( "description", userAccount.getDescription() != null ? userAccount.getDescription() : ""); userAccounts.appendChild(userAccountTag); addNameAndAdress(userAccount, doc, userAccountTag); addCategories(userAccount, invoice, doc, userAccountTag, true); } }
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); } } } } } }