BrightSide Workbench Full Report + Source Code
IncomeBook.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.financials.book;
20 
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;
29 import jxl.Workbook;
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;
49 
54 public class IncomeBook extends TreeSet<IncomeEntry> {
55 
56  public void download() {
57  String name = ElephantContext.getSiteName() + "_IncomeBook.xls";
58  Filedownload.save(getExcel(name), new MimetypesFileTypeMap().getContentType(name), name);
59  }
60 
61  public InputStream getExcel(String sheetName) {
62  try {
63  ByteArrayOutputStream baos = new ByteArrayOutputStream();
64  WritableWorkbook ww = Workbook.createWorkbook(baos);
65  WritableSheet ws = ww.createSheet(sheetName, 0);
66  writeSheet(ws);
67  ww.write();
68  ww.close();
69  baos.close();
70  return new ByteArrayInputStream(baos.toByteArray());
71  } catch (WriteException | IOException ex) {
72  WebLoggers.severe(this).exception(ex).log();
73  }
74  return null;
75  }
76 
77  private List<String> columns = List.of(
78  "Register", "DocNumber", "DocDate", "GID", "Customer",
79  "Tax%", "Taxable", "Tax", "Retention", "Total");
80 
81  private List<Long> docDefs = List.of(
82  1L, 67L);
83 
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));
88  }
89  for(IncomeEntry entry : this) {
90  countCols = 0;
91  countRows++;
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()));
102  }
103  }
104 
105  /* Utils */
106 
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)
111  .dao(dao.get())
112  .resultList(Document.class)
113  .forEach(d -> loadEntry(d));
114  }
115 
116  private void loadEntry(Document d) {
117  Contract c = d.getContract();
118  IContact contractor = c.getIContractor();
119  DocumentAmounts da = d.getAmounts();
120  double retained = 0;
121  for(AmountRetained ar : da.getRetentions()) {
122  retained += ar.getRetained();
123  }
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());
128  add(e);
129  } else {
130  IncomeEntry e = new IncomeEntry(d.getDocumentNumber(), contractor.getGlobalId(), contractor.getName(),
131  d.getDocumentDate(), at.getTax(), at.getTaxable(), at.getTaxAmount(), retained, at.getTotal());
132  add(e);
133  }
134  retained = 0;
135  }
136  }
137 
138  /* Dao */
139 
140  private final Cached<Dao> dao = Cached.instance(() -> new FinancialsPU());
141 
142  /* Factory */
143 
144  public static IncomeBook from(int year) {
145  return new IncomeBook(year);
146  }
147 
148  private final int year;
149 
150  private IncomeBook(int year) {
151  this.year = year;
152  load();
153  }
154 
155 }
InputStream getExcel(String sheetName)
Definition: IncomeBook.java:61
static IncomeBook from(int year)
static WebLoggers severe(Object entity)
Definition: WebLoggers.java:51
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29