19 package org.turro.financials.book;
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.List;
26 import java.util.TreeSet;
27 import javax.activation.MimetypesFileTypeMap;
29 import jxl.write.DateTime;
30 import jxl.write.Label;
31 import jxl.write.WritableSheet;
32 import jxl.write.WritableWorkbook;
33 import jxl.write.WriteException;
34 import org.turro.time.Dates;
35 import org.turro.elephant.context.ElephantContext;
36 import org.turro.financials.db.FinancialsPU;
37 import org.turro.financials.entity.Contract;
38 import org.turro.financials.entity.Document;
39 import org.turro.financials.model.document.AmountTaxable;
40 import org.turro.financials.model.document.DocumentAmounts;
41 import org.turro.jpa.Dao;
42 import org.turro.log.WebLoggers;
43 import org.turro.plugin.contacts.IContact;
44 import org.turro.sql.SqlClause;
45 import org.turro.util.Cached;
46 import org.zkoss.zul.Filedownload;
56 Filedownload.save(
getExcel(name),
new MimetypesFileTypeMap().getContentType(name), name);
59 public InputStream
getExcel(String sheetName) {
61 ByteArrayOutputStream baos =
new ByteArrayOutputStream();
62 WritableWorkbook ww = Workbook.createWorkbook(baos);
63 WritableSheet ws = ww.createSheet(sheetName, 0);
68 return new ByteArrayInputStream(baos.toByteArray());
69 }
catch (WriteException | IOException ex) {
75 private List<String> columns = List.of(
76 "Register",
"DocNumber",
"ReceiptDate",
"DocDate",
"GID",
"Provider",
77 "Type",
"Tax%",
"Taxable",
"Tax",
"Total");
79 private List<Long> docDefs = List.of(
80 2L, 10L, 12L, 15L, 51L);
82 private void writeSheet(WritableSheet ws)
throws WriteException {
83 int countCols = 0, countRows = 0;
84 for(String s : columns) {
85 ws.addCell(
new Label(countCols++, countRows, s));
87 for(ExpensesEntry entry :
this) {
90 ws.addCell(
new jxl.write.Number(countCols++, countRows, countRows));
91 ws.addCell(
new Label(countCols++, countRows, entry.getDocNumber()));
92 ws.addCell(
new DateTime(countCols++, countRows, Dates.fromGTM(entry.getReceiptDate()).get()));
93 ws.addCell(
new DateTime(countCols++, countRows, Dates.fromGTM(entry.getDocDate()).get()));
94 ws.addCell(
new Label(countCols++, countRows, entry.getGlobalIdentifier()));
95 ws.addCell(
new Label(countCols++, countRows, entry.getProvider()));
96 ws.addCell(
new Label(countCols++, countRows, entry.getType()));
97 ws.addCell(
new jxl.write.Number(countCols++, countRows, entry.getTax()));
98 ws.addCell(
new jxl.write.Number(countCols++, countRows, entry.getTaxable()));
99 ws.addCell(
new jxl.write.Number(countCols++, countRows, entry.getTaxAmount()));
100 ws.addCell(
new jxl.write.Number(countCols++, countRows, entry.getTotal()));
106 private void load() {
107 SqlClause.select(
"d").from(
"Document d")
108 .where().equal(
"year(d.receiptDate)", year)
109 .and().in(
"d.documentDefinition.id", docDefs)
111 .resultList(Document.class)
112 .forEach(d -> loadEntry(d));
115 private void loadEntry(Document d) {
116 Contract c = d.getContract();
117 IContact contractor = c.getIContractor();
118 DocumentAmounts da = d.getAmounts();
119 for(AmountTaxable at : da.getTaxables()) {
120 ExpensesEntry e =
new ExpensesEntry(d.getDocumentNumber(), contractor.getGlobalId(), contractor.getName(),
121 c.getOperatingModifier().toString(), d.getReceiptDate(), d.getDocumentDate(),
122 at.getTax(), at.getTaxable(), at.getTaxAmount(), at.getTotal());
129 private final Cached<Dao> dao = Cached.instance(() ->
new FinancialsPU());
137 private final int year;
static String getSiteName()
static ExpensesBook from(int year)
InputStream getExcel(String sheetName)
static WebLoggers severe(Object entity)
WebLoggers exception(Throwable throwable)