BrightSide Workbench Full Report + Source Code
ExpensesBook.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 javax.activation.MimetypesFileTypeMap;
28 import jxl.Workbook;
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;
47 
52 public class ExpensesBook extends TreeSet<ExpensesEntry> {
53 
54  public void download() {
55  String name = ElephantContext.getSiteName() + "_ExpensesBook.xls";
56  Filedownload.save(getExcel(name), new MimetypesFileTypeMap().getContentType(name), name);
57  }
58 
59  public InputStream getExcel(String sheetName) {
60  try {
61  ByteArrayOutputStream baos = new ByteArrayOutputStream();
62  WritableWorkbook ww = Workbook.createWorkbook(baos);
63  WritableSheet ws = ww.createSheet(sheetName, 0);
64  writeSheet(ws);
65  ww.write();
66  ww.close();
67  baos.close();
68  return new ByteArrayInputStream(baos.toByteArray());
69  } catch (WriteException | IOException ex) {
70  WebLoggers.severe(this).exception(ex).log();
71  }
72  return null;
73  }
74 
75  private List<String> columns = List.of(
76  "Register", "DocNumber", "ReceiptDate", "DocDate", "GID", "Provider",
77  "Type", "Tax%", "Taxable", "Tax", "Total");
78 
79  private List<Long> docDefs = List.of(
80  2L, 10L, 12L, 15L, 51L);
81 
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));
86  }
87  for(ExpensesEntry entry : this) {
88  countCols = 0;
89  countRows++;
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()));
101  }
102  }
103 
104  /* Utils */
105 
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)
110  .dao(dao.get())
111  .resultList(Document.class)
112  .forEach(d -> loadEntry(d));
113  }
114 
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());
123  add(e);
124  }
125  }
126 
127  /* Dao */
128 
129  private final Cached<Dao> dao = Cached.instance(() -> new FinancialsPU());
130 
131  /* Factory */
132 
133  public static ExpensesBook from(int year) {
134  return new ExpensesBook(year);
135  }
136 
137  private final int year;
138 
139  private ExpensesBook(int year) {
140  this.year = year;
141  load();
142  }
143 
144 }
static ExpensesBook from(int year)
InputStream getExcel(String sheetName)
static WebLoggers severe(Object entity)
Definition: WebLoggers.java:51
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29