public BankFileTaskResponse call() throws Exception { logger.info("Starting BankFileTask tasks..."); File dir = new File(BayadConfig.getBankFileSourceFilesDirectory()); if (!dir.exists()) { dir.mkdirs(); } logger.info( "Import bankFile SourceFilesDirectory:" + BayadConfig.getBankFileSourceFilesDirectory()); BankFileImportHisto bankFileImportHisto = new BankFileImportHisto(); bankFileImportHisto.setExecutionDate(new Date()); ImportFileFiltre filtre = new ImportFileFiltre("*", BayadConfig.getBankFileFileExtensions()); File[] listFile = dir.listFiles(filtre); int nbBankFileFiles = (listFile == null ? 0 : listFile.length); int nbBankFileImported = 0, nbBankFileError = 0; logger.info("Import BankFile job " + nbBankFileFiles + " file to import"); Provider provider = null; CsvBuilder errorOut = new CsvBuilder(":", false); List<String> fileNames = new ArrayList<String>(); for (int i = 0; i < listFile.length; i++) { File currentFile = null; try { ImportBankFile importBankFile = new ImportBankFile(); logger.info("Import BankFile job " + listFile[i].getName() + " in progres"); fileNames.add(listFile[i].getName()); currentFile = FileUtils.addExtension(listFile[i], BayadConfig.getInvoicesFileProcessingExtension()); provider = importBankFile.execute(currentFile); FileUtils.moveFile( BayadConfig.getBankFileAcceptedFilesDirectory(), currentFile, listFile[i].getName()); logger.info("Import BankFile job " + listFile[i].getName() + " done"); nbBankFileImported++; } catch (Exception iie) { iie.printStackTrace(); logger.info("Import BankFile job " + listFile[i].getName() + " failed"); FileUtils.moveFile( BayadConfig.getBankFileRejectedFilesDirectory(), currentFile, listFile[i].getName()); nbBankFileError++; errorOut.appendValue(listFile[i].getName()).appendValue(iie.getMessage()).startNewLine(); } } if (!errorOut.isEmpty()) { errorOut.toFile( BayadConfig.getBankFileErrorDirectory() + File.separator + BayadConfig.getBankFileErrorFilePrefix() + DateUtils.formatDateWithPattern( new Date(), BayadConfig.getBankFileErrorFileExtension())); } bankFileImportHisto.setLinesRead(nbBankFileFiles); bankFileImportHisto.setLinesInserted(nbBankFileImported); bankFileImportHisto.setLinesRejected(nbBankFileError); EntityManager em = MeveoPersistence.getEntityManager(); em.getTransaction().begin(); em.persist(bankFileImportHisto); em.getTransaction().commit(); BankFileTaskResponse bankFileTaskResponse = new BankFileTaskResponse(); bankFileTaskResponse.setFileNames(fileNames); bankFileTaskResponse.setNbImported(nbBankFileImported); bankFileTaskResponse.setNbRejected(nbBankFileError); return bankFileTaskResponse; }
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()); } }