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 void createXMLInvoice(Invoice invoice, File billingRundir) throws BusinessException { try { boolean entreprise = invoice.getProvider().isEntreprise(); DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element invoiceTag = doc.createElement("invoice"); Element header = doc.createElement("header"); invoiceTag.setAttribute("number", "XXXXXXX"); invoiceTag.setAttribute("id", invoice.getId().toString()); invoiceTag.setAttribute( "customerId", invoice.getBillingAccount().getCustomerAccount().getCustomer().getCode() + ""); invoiceTag.setAttribute( "customerAccountCode", invoice.getBillingAccount().getCustomerAccount().getCode() != null ? invoice.getBillingAccount().getCustomerAccount().getCode() : ""); BillingCycle billingCycle = invoice.getBillingRun().getBillingCycle(); invoiceTag.setAttribute( "templateName", billingCycle != null && billingCycle.getBillingTemplateName() != null ? billingCycle.getBillingTemplateName() : "default"); doc.appendChild(invoiceTag); invoiceTag.appendChild(header); Customer customer = invoice.getBillingAccount().getCustomerAccount().getCustomer(); Element customerTag = doc.createElement("customer"); customerTag.setAttribute("id", customer.getId() + ""); customerTag.setAttribute("code", customer.getCode() + ""); customerTag.setAttribute( "externalRef1", customer.getExternalRef1() != null ? customer.getExternalRef1() : ""); customerTag.setAttribute( "externalRef2", customer.getExternalRef2() != null ? customer.getExternalRef2() : ""); header.appendChild(customerTag); addNameAndAdress(customer, doc, customerTag); CustomerAccount customerAccount = invoice.getBillingAccount().getCustomerAccount(); Element customerAccountTag = doc.createElement("customerAccount"); customerAccountTag.setAttribute("id", customerAccount.getId() + ""); customerAccountTag.setAttribute("code", customerAccount.getCode() + ""); customerAccountTag.setAttribute("description", customerAccount.getDescription() + ""); customerAccountTag.setAttribute( "externalRef1", customerAccount.getExternalRef1() != null ? customerAccount.getExternalRef1() : ""); customerAccountTag.setAttribute( "externalRef2", customerAccount.getExternalRef2() != null ? customerAccount.getExternalRef2() : ""); EntityManager em = MeveoPersistence.getEntityManager(); Query billingQuery = em.createQuery( "select si from ServiceInstance si join si.subscription s join s.userAccount ua join ua.billingAccount ba join ba.customerAccount ca where ca.id = :customerAccountId"); billingQuery.setParameter("customerAccountId", customerAccount.getId()); List<ServiceInstance> services = (List<ServiceInstance>) billingQuery.getResultList(); boolean terminated = isAllServiceInstancesTerminated(services); customerAccountTag.setAttribute("accountTerminated", terminated + ""); header.appendChild(customerAccountTag); addNameAndAdress(customerAccount, doc, customerAccountTag); addproviderContact(customerAccount, doc, customerAccountTag); BillingAccount billingAccount = invoice.getBillingAccount(); Element billingAccountTag = doc.createElement("billingAccount"); if (billingCycle == null) { billingCycle = billingAccount.getBillingCycle(); } String billingCycleCode = billingCycle != null ? billingCycle.getCode() + "" : ""; billingAccountTag.setAttribute("billingCycleCode", billingCycleCode); String billingAccountId = billingAccount.getId() + ""; String billingAccountCode = billingAccount.getCode() + ""; billingAccountTag.setAttribute("id", billingAccountId); billingAccountTag.setAttribute("code", billingAccountCode); billingAccountTag.setAttribute("description", billingAccount.getDescription() + ""); billingAccountTag.setAttribute( "externalRef1", billingAccount.getExternalRef1() != null ? billingAccount.getExternalRef1() : ""); billingAccountTag.setAttribute( "externalRef2", billingAccount.getExternalRef2() != null ? billingAccount.getExternalRef2() : ""); header.appendChild(billingAccountTag); if (billingAccount.getName() != null && billingAccount.getName().getTitle() != null) { Element company = doc.createElement("company"); Text companyTxt = doc.createTextNode(billingAccount.getName().getTitle().getIsCompany() + ""); billingAccountTag.appendChild(companyTxt); } Element email = doc.createElement("email"); Text emailTxt = doc.createTextNode(billingAccount.getEmail() != null ? billingAccount.getEmail() : ""); email.appendChild(emailTxt); billingAccountTag.appendChild(email); addNameAndAdress(billingAccount, doc, billingAccountTag); addPaymentInfo(billingAccount, doc, billingAccountTag); Element invoiceDate = doc.createElement("invoiceDate"); Text invoiceDateTxt = doc.createTextNode( DateUtils.formatDateWithPattern(invoice.getInvoiceDate(), "dd/MM/yyyy")); invoiceDate.appendChild(invoiceDateTxt); header.appendChild(invoiceDate); Element dueDate = doc.createElement("dueDate"); Text dueDateTxt = doc.createTextNode(DateUtils.formatDateWithPattern(invoice.getDueDate(), dueDateFormat)); dueDate.appendChild(dueDateTxt); header.appendChild(dueDate); addHeaderCategories(invoice, doc, header); Element amount = doc.createElement("amount"); invoiceTag.appendChild(amount); Element currency = doc.createElement("currency"); Text currencyTxt = doc.createTextNode("EUR"); currency.appendChild(currencyTxt); amount.appendChild(currency); Element amountWithoutTax = doc.createElement("amountWithoutTax"); Text amountWithoutTaxTxt = doc.createTextNode(round(invoice.getAmountWithoutTax())); amountWithoutTax.appendChild(amountWithoutTaxTxt); amount.appendChild(amountWithoutTax); Element amountWithTax = doc.createElement("amountWithTax"); Text amountWithTaxTxt = doc.createTextNode(round(invoice.getAmountWithTax())); amountWithTax.appendChild(amountWithTaxTxt); amount.appendChild(amountWithTax); BigDecimal balance = computeBalance( invoice.getBillingAccount().getCustomerAccount().getCode(), invoice.getDueDate()); if (balance == null) { throw new BusinessException("account balance calculation failed"); } BigDecimal netToPay = BigDecimal.ZERO; if (entreprise) { netToPay = invoice.getAmountWithTax(); } else { netToPay = invoice.getAmountWithTax().add(balance); } Element balanceElement = doc.createElement("balance"); Text balanceTxt = doc.createTextNode(round(balance)); balanceElement.appendChild(balanceTxt); amount.appendChild(balanceElement); Element netToPayElement = doc.createElement("netToPay"); Text netToPayTxt = doc.createTextNode(round(netToPay)); netToPayElement.appendChild(netToPayTxt); amount.appendChild(netToPayElement); addTaxes(invoice, doc, amount); Element detail = doc.createElement("detail"); invoiceTag.appendChild(detail); addUserAccounts(invoice, doc, detail); TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); // create string from xml tree DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult( billingRundir + File.separator + invoice.getTemporaryInvoiceNumber() + ".xml"); logger.info("source=" + source.toString()); trans.transform(source, result); } catch (Exception e) { e.printStackTrace(); throw new BusinessException(e.getMessage()); } }
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); }
private void addTaxes(Invoice invoice, Document doc, Element parent) { Element taxes = doc.createElement("taxes"); taxes.setAttribute("total", round(invoice.getAmountTax())); parent.appendChild(taxes); Map<Long, TaxInvoiceAgregate> taxInvoiceAgregateMap = new HashMap<Long, TaxInvoiceAgregate>(); for (InvoiceAgregate invoiceAgregate : invoice.getInvoiceAgregates()) { if (invoiceAgregate instanceof TaxInvoiceAgregate) { TaxInvoiceAgregate taxInvoiceAgregate = (TaxInvoiceAgregate) invoiceAgregate; TaxInvoiceAgregate taxAgregate = null; if (taxInvoiceAgregateMap.containsKey(taxInvoiceAgregate.getTax().getId())) { taxAgregate = taxInvoiceAgregateMap.get(taxInvoiceAgregate.getTax().getId()); taxAgregate.setAmountTax( taxAgregate.getAmountTax().add(taxInvoiceAgregate.getAmountTax())); taxAgregate.setAmountWithoutTax( taxAgregate.getAmountWithoutTax().add(taxInvoiceAgregate.getAmountWithoutTax())); } else { taxAgregate = new TaxInvoiceAgregate(); taxAgregate.setTaxPercent(taxInvoiceAgregate.getTaxPercent()); taxAgregate.setTax(taxInvoiceAgregate.getTax()); taxAgregate.setAmountTax(taxInvoiceAgregate.getAmountTax()); taxAgregate.setAmountWithoutTax(taxInvoiceAgregate.getAmountWithoutTax()); taxInvoiceAgregateMap.put(taxInvoiceAgregate.getTax().getId(), taxAgregate); } } } int taxId = 0; for (TaxInvoiceAgregate taxInvoiceAgregate : taxInvoiceAgregateMap.values()) { Element tax = doc.createElement("tax"); tax.setAttribute("id", ++taxId + ""); tax.setAttribute("code", taxInvoiceAgregate.getTax().getCode() + ""); Element taxName = doc.createElement("name"); Text taxNameTxt = doc.createTextNode( taxInvoiceAgregate.getTax() != null ? (taxInvoiceAgregate.getTax().getDescription() != null ? taxInvoiceAgregate.getTax().getDescription() : "") : ""); taxName.appendChild(taxNameTxt); tax.appendChild(taxName); Element percent = doc.createElement("percent"); Text percentTxt = doc.createTextNode(round(taxInvoiceAgregate.getTaxPercent())); percent.appendChild(percentTxt); tax.appendChild(percent); Element taxAmount = doc.createElement("amount"); Text amountTxt = doc.createTextNode(round(taxInvoiceAgregate.getAmountTax())); taxAmount.appendChild(amountTxt); tax.appendChild(taxAmount); Element amountHT = doc.createElement("amountHT"); Text amountHTTxt = doc.createTextNode(round(taxInvoiceAgregate.getAmountWithoutTax())); amountHT.appendChild(amountHTTxt); tax.appendChild(amountHT); taxes.appendChild(tax); } }
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); } } } } } }