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 java.util.stream.Stream;
28 import javax.activation.MimetypesFileTypeMap;
30 import jxl.write.DateTime;
31 import jxl.write.Label;
32 import jxl.write.WritableSheet;
33 import jxl.write.WritableWorkbook;
34 import jxl.write.WriteException;
35 import org.turro.time.Dates;
36 import org.turro.elephant.context.ElephantContext;
37 import org.turro.financials.db.FinancialsPU;
38 import org.turro.financials.entity.Contract;
39 import org.turro.financials.entity.Document;
40 import org.turro.financials.model.document.AmountRetained;
41 import org.turro.financials.model.document.AmountTaxable;
42 import org.turro.financials.model.document.DocumentAmounts;
43 import org.turro.jpa.Dao;
44 import org.turro.log.WebLoggers;
45 import org.turro.plugin.contacts.IContact;
46 import org.turro.sql.SqlClause;
47 import org.turro.util.Cached;
48 import org.zkoss.zul.Filedownload;
58 Filedownload.save(
getExcel(name),
new MimetypesFileTypeMap().getContentType(name), name);
61 public InputStream
getExcel(String sheetName) {
63 ByteArrayOutputStream baos =
new ByteArrayOutputStream();
64 WritableWorkbook ww = Workbook.createWorkbook(baos);
65 WritableSheet ws = ww.createSheet(sheetName, 0);
70 return new ByteArrayInputStream(baos.toByteArray());
71 }
catch (WriteException | IOException ex) {
77 private List<String> columns = List.of(
78 "Register",
"DocNumber",
"DocDate",
"GID",
"Customer",
79 "Tax%",
"Taxable",
"Tax",
"Retention",
"Total");
81 private List<Long> docDefs = List.of(
84 private void writeSheet(WritableSheet ws)
throws WriteException {
85 int countCols = 0, countRows = 0;
86 for(String s : columns) {
87 ws.addCell(
new Label(countCols++, countRows, s));
89 for(IncomeEntry entry :
this) {
92 ws.addCell(
new jxl.write.Number(countCols++, countRows, countRows));
93 ws.addCell(
new Label(countCols++, countRows, entry.getDocNumber()));
94 ws.addCell(
new DateTime(countCols++, countRows, Dates.fromGTM(entry.getDocDate()).get()));
95 ws.addCell(
new Label(countCols++, countRows, entry.getGlobalIdentifier()));
96 ws.addCell(
new Label(countCols++, countRows, entry.getCustomer()));
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.getRetention()));
101 ws.addCell(
new jxl.write.Number(countCols++, countRows, entry.getTotal()));
107 private void load() {
108 SqlClause.select(
"d").from(
"Document d")
109 .where().equal(
"year(d.documentDate)", year)
110 .and().in(
"d.documentDefinition.id", docDefs)
112 .resultList(Document.class)
113 .forEach(d -> loadEntry(d));
116 private void loadEntry(Document d) {
117 Contract c = d.getContract();
118 IContact contractor = c.getIContractor();
119 DocumentAmounts da = d.getAmounts();
121 for(AmountRetained ar : da.getRetentions()) {
122 retained += ar.getRetained();
124 for(AmountTaxable at : da.getTaxables()) {
125 if(d.getDocumentDefinition().getId() == 67L) {
126 IncomeEntry e =
new IncomeEntry(d.getDocumentNumber(),
"", d.getDocumentDefinition().getName(),
127 d.getDocumentDate(), at.getTax(), at.getTaxable(), at.getTaxAmount(), retained, at.getTotal());
130 IncomeEntry e =
new IncomeEntry(d.getDocumentNumber(), contractor.getGlobalId(), contractor.getName(),
131 d.getDocumentDate(), at.getTax(), at.getTaxable(), at.getTaxAmount(), retained, at.getTotal());
140 private final Cached<Dao> dao = Cached.instance(() ->
new FinancialsPU());
148 private final int year;
static String getSiteName()
InputStream getExcel(String sheetName)
static IncomeBook from(int year)
static WebLoggers severe(Object entity)
WebLoggers exception(Throwable throwable)