/** Exports the results to a pdf */ public void exportPdf() { try { Customer c = accountController.getCustomer(); DateFormat format = new SimpleDateFormat("dd.MM.yyyy"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfPCell cell; Document document = new Document(); PdfWriter.getInstance(document, baos); Font defaultFont = new Font(Font.FontFamily.HELVETICA, 10); Font headerFont = new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD); document.open(); document.add( new Paragraph( "Umsatzanzeige für " + c.getFirstName() + " " + c.getFamilyName() + ", erstellt am " + format.format(new Date()) + " für Konto " + acc, defaultFont)); // document.add(new Paragraph("Zweiter Paragraph")); PdfPTable table = new PdfPTable(new float[] {1f, 4f, 1f}); table.setWidthPercentage(100f); table.setSpacingBefore(20f); cell = new PdfPCell(new Phrase("Datum", headerFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setPadding(4f); cell.setGrayFill(0.9f); table.addCell(cell); cell = new PdfPCell(new Phrase("Umsatzinformation", headerFont)); cell.setPadding(4f); cell.setGrayFill(0.9f); table.addCell(cell); cell = new PdfPCell(new Phrase("Euro", headerFont)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setPadding(4f); cell.setGrayFill(0.9f); table.addCell(cell); if (transactions != null) for (Transaction transaction : transactions) { cell = new PdfPCell(new Phrase(format.format(transaction.getDate()), defaultFont)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setBorder(Rectangle.BOTTOM); cell.setPadding(4f); table.addCell(cell); if (transaction.getFromAccount().toString().equals(acc)) { cell = new PdfPCell( new Phrase( "AN : " + transaction.getToAccount() + ", " + transaction.getPaymentReason(), defaultFont)); } else { cell = new PdfPCell( new Phrase( "VON: " + (transaction.getFromAccount().getAccountID() == BankSettings.bankAccountID ? "E-BANK-SYSTEM" : transaction.getFromAccount()) + ", " + transaction.getPaymentReason(), defaultFont)); } cell.setBorder(Rectangle.BOTTOM); cell.setPadding(4f); table.addCell(cell); cell = new PdfPCell( new Phrase( transaction.getAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString(), defaultFont)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setBorder(Rectangle.BOTTOM); cell.setPadding(4f); table.addCell(cell); } document.add(table); document.close(); FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); response.setContentType("application/pdf"); response.setContentLength(baos.size()); response.setHeader("Content-disposition", "inline;filename=\"umsaetze.pdf\""); OutputStream out = response.getOutputStream(); baos.writeTo(out); out.flush(); response.flushBuffer(); context.responseComplete(); } catch (Exception e) { e.printStackTrace(); } }
/** exports the results to a excel file */ public void exportExcel() { try { FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment; filename=\"umsaetze.xls\";"); OutputStream out = response.getOutputStream(); WorkbookSettings ws = new WorkbookSettings(); ws.setLocale(new Locale("de", "DE")); WritableWorkbook workbook = Workbook.createWorkbook(out, ws); WritableSheet sheet = workbook.createSheet("Sheet1", 0); sheet.addCell(new Label(0, 0, "Datum")); sheet.addCell(new Label(1, 0, "Umssatzinformation")); sheet.addCell(new Label(2, 0, "EURO")); DateFormat format = new SimpleDateFormat("dd.MM.yyyy"); int row = 1; if (transactions != null) for (Transaction transaction : transactions) { sheet.addCell(new Label(0, row, format.format(transaction.getDate()))); if (transaction.getFromAccount().toString().equals(acc)) { sheet.addCell( new Label( 1, row, "AN :" + transaction.getToAccount().toString() + ", " + transaction.getPaymentReason())); } else { sheet.addCell( new Label( 1, row, "VON:" + (transaction.getFromAccount().getAccountID() == BankSettings.bankAccountID ? "E-BANK-SYSTEM" : transaction.getFromAccount()) + ", " + transaction.getPaymentReason())); } sheet.addCell( new Label( 2, row, transaction.getAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString())); row++; } workbook.write(); workbook.close(); response.flushBuffer(); context.responseComplete(); } catch (Exception e) { e.printStackTrace(); } }
/** * build the {@link Transaction} list and filter also for {@link #acc}, {@link * #paymentReason},{@link #from} and {@link #until} * * @return the filtered {@link Transaction} list */ public List<Transaction> calculateTransactions() { List<Account> accounts = accountController.getAccounts(); if (accounts == null) return null; Account fromAcc = null; for (Account account : accounts) { if (account.toString().equals(acc)) { fromAcc = account; break; } } List<Transaction> result = new ArrayList<Transaction>(); List<Transaction> transactions; try { transactions = Database.getInstance().getTransactions(fromAcc); } catch (Throwable e) { return null; } if (transactions == null) return null; Pattern payment = null; if (paymentReason != null) { payment = Pattern.compile(".*" + Pattern.quote(paymentReason.toLowerCase()) + ".*"); } if (from == null && until == null) result = transactions; if (from != null && until == null) for (Transaction transaction : transactions) if (transaction.getDate().after(from)) result.add(transaction); if (from != null && until != null) for (Transaction transaction : transactions) if (transaction.getDate().after(from) && transaction.getDate().before(until)) result.add(transaction); if (from == null && until != null) for (Transaction transaction : transactions) if (transaction.getDate().before(until)) result.add(transaction); List<Transaction> endResult = new LinkedList<Transaction>(); if (paymentReason == null) { endResult = result; } else { for (Transaction transaction : result) { if (payment.matcher(transaction.getPaymentReason().toLowerCase()).matches()) { endResult.add(transaction); } } } // for (Transaction transaction : endResult) { // if(transaction.getFromAccount().getAccountID() == fromAcc.getAccountID()){ // if(transaction.getAmount().floatValue() > 0) // transaction.setAmount(transaction.getAmount().negate()); // } // } return endResult; }