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()); } }
public static void addPaymentInfo(BillingAccount billingAccount, Document doc, Element parent) { Element paymentMethod = doc.createElement("paymentMethod"); parent.appendChild(paymentMethod); paymentMethod.setAttribute("type", billingAccount.getPaymentMethod().toString()); Element bankCoordinates = doc.createElement("bankCoordinates"); Element bankCode = doc.createElement("bankCode"); Element branchCode = doc.createElement("branchCode"); Element accountNumber = doc.createElement("accountNumber"); Element accountOwner = doc.createElement("accountOwner"); Element key = doc.createElement("key"); Element iban = doc.createElement("IBAN"); bankCoordinates.appendChild(bankCode); bankCoordinates.appendChild(branchCode); bankCoordinates.appendChild(accountNumber); bankCoordinates.appendChild(accountOwner); bankCoordinates.appendChild(key); bankCoordinates.appendChild(iban); paymentMethod.appendChild(bankCoordinates); if (billingAccount.getBankCoordinates() != null && billingAccount.getBankCoordinates().getBankCode() != null) { Text bankCodeTxt = doc.createTextNode( billingAccount.getBankCoordinates().getBankCode() != null ? billingAccount.getBankCoordinates().getBankCode() : ""); bankCode.appendChild(bankCodeTxt); Text branchCodeTxt = doc.createTextNode( billingAccount.getBankCoordinates().getBranchCode() != null ? billingAccount.getBankCoordinates().getBranchCode() : ""); branchCode.appendChild(branchCodeTxt); Text accountNumberTxt = doc.createTextNode( billingAccount.getBankCoordinates().getAccountNumber() != null ? billingAccount.getBankCoordinates().getAccountNumber() : ""); accountNumber.appendChild(accountNumberTxt); Text accountOwnerTxt = doc.createTextNode( billingAccount.getBankCoordinates().getAccountOwner() != null ? billingAccount.getBankCoordinates().getAccountOwner() : ""); accountOwner.appendChild(accountOwnerTxt); Text keyTxt = doc.createTextNode( billingAccount.getBankCoordinates().getKey() != null ? billingAccount.getBankCoordinates().getKey() : ""); key.appendChild(keyTxt); if (billingAccount.getBankCoordinates().getIban() != null) { Text ibanTxt = doc.createTextNode( billingAccount.getBankCoordinates().getIban() != null ? billingAccount.getBankCoordinates().getIban() : ""); iban.appendChild(ibanTxt); } } }