BrightSide Workbench Full Report + Source Code
ReportSheet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.financials.sheets;
19 
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.text.SimpleDateFormat;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import jxl.CellType;
32 import jxl.Workbook;
33 import jxl.WorkbookSettings;
34 import jxl.read.biff.BiffException;
35 import jxl.write.Label;
36 import jxl.write.WritableCell;
37 import jxl.write.WritableSheet;
38 import jxl.write.WritableWorkbook;
39 import jxl.write.WriteException;
40 import org.amic.util.date.CheckDate;
41 import org.turro.string.Strings;
42 import org.turro.elephant.context.ElephantContext;
43 import org.turro.financials.db.FinancialsPU;
44 import org.turro.financials.entity.MajorAccountType;
45 import org.turro.financials.entity.Register;
46 import org.turro.financials.entity.RegisterView;
47 import org.turro.jpa.Dao;
48 
53 public class ReportSheet {
54 
55  private File sheetFile;
56  private Date date;
57  private int year;
58  private RegisterView view;
59  private List<WritableCell> clearCells;
60  private Dao dao;
61  private MajorAccountType currentType = MajorAccountType.MAJOR_NONE;
62 
63  public ReportSheet(File sheetFile, Date date, RegisterView view) {
64  this.sheetFile = sheetFile;
65  this.date = date;
66  this.view = view;
67  this.year = new CheckDate(date).getYear();
68  dao = new FinancialsPU();
69  }
70 
71  public InputStream getExcel() throws IOException, BiffException, WriteException {
72  clearCells = new ArrayList<WritableCell>();
73  ByteArrayOutputStream baos = new ByteArrayOutputStream();
74  WorkbookSettings wsWrite = new WorkbookSettings();
75  wsWrite.setEncoding("UTF-8");
76  WorkbookSettings wsRead = new WorkbookSettings();
77  wsRead.setEncoding("ISO-8859-1");
78  WritableWorkbook workbook = Workbook.createWorkbook(baos, Workbook.getWorkbook(sheetFile, wsRead), wsWrite);
79  for(WritableSheet sheet : workbook.getSheets()) {
80  for(int row = 0; row < sheet.getRows(); row++) {
81  for(int col = 0; col < sheet.getColumns(); col++) {
82  processCell(sheet, row, col);
83  }
84  }
85  for(WritableCell cell : clearCells) {
86  sheet.addCell(new Label(cell.getColumn(), cell.getRow(), ""));
87  }
88  clearCells.clear();
89  }
90  workbook.write();
91  workbook.close();
92  baos.close();
93  return new ByteArrayInputStream(baos.toByteArray());
94  }
95 
96  private void processCell(WritableSheet sheet, int row, int col) {
97  WritableCell cell = sheet.getWritableCell(col, row);
98  if(cell.getType() == CellType.LABEL) {
99  Label l = (Label) cell;
100  String value = l.getString().trim();
101  if("ACTIVO".equalsIgnoreCase(value)) {
102  currentType = MajorAccountType.MAJOR_ACTIVE;
103  } else if("PATRIMONIO NETO Y PASIVO".equalsIgnoreCase(value)) {
104  currentType = MajorAccountType.MAJOR_PASSIVE;
105  } else if("CUENTA PERDIDAS Y GANACIAS %year".equalsIgnoreCase(value)) {
106  currentType = MajorAccountType.MAJOR_EXPLOITATION;
107  }
108  processValue(value, sheet, row, col);
109  }
110  }
111 
112  private void processValue(String value, WritableSheet sheet, int row, int col) {
113  if(value.contains("%year")) {
114  value = value.replaceAll("\\%year", year + "");
115  WritableCell cell = sheet.getWritableCell(col, row);
116  if(cell.getType() == CellType.LABEL) {
117  Label l = (Label) cell;
118  l.setString(value);
119  }
120  }
121  if(value.contains("%date")) {
122  value = value.replaceAll("\\%date", SimpleDateFormat.getDateInstance().format(date));
123  WritableCell cell = sheet.getWritableCell(col, row);
124  if(cell.getType() == CellType.LABEL) {
125  Label l = (Label) cell;
126  l.setString(value);
127  }
128  }
129  if(value.contains("%")) {
130  try {
131  int yearOp = Integer.valueOf(Strings.extract(value, "\\%y([\\-0-9]+)"));
132  int cellExpr = Integer.valueOf(Strings.extract(value, "v([\\-0-9]+)"));
133  WritableCell cell = sheet.getWritableCell(cellExpr, row);
134  clearCells.add(cell);
135  double amount = getAmount(year + yearOp, cell.getContents());
136  sheet.addCell(new jxl.write.Number(col, row, amount));
137  } catch (WriteException ex) {
138  Logger.getLogger(ReportSheet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
139  }
140  }
141  }
142 
143  private double getAmount(int year, String accounts) {
144  if(accounts == null) return 0;
145  double amount = 0;
146  String accs[] = accounts.split(",");
147  for(String acc : accs) {
148  amount += getValue(year, acc.trim());
149  }
150  return amount;
151  }
152 
153  private double getValue(int year, String account) {
154  boolean negative = false, anysign = false, asSign = false;
155  if(account.endsWith("+")) {
156  account = account.substring(0, account.length() - 1);
157  asSign = true;
158  }
159  if(account.endsWith("*")) {
160  account = account.substring(0, account.length() - 1);
161  anysign = true;
162  }
163  if(account.startsWith("(")) {
164  account = account.substring(1, account.length() - 1);
165  negative = true;
166  }
167 // if(currentType.equals(MajorAccountType.MAJOR_PASSIVE)) {
168 // dao.executeUpdate(
169 // "update MajorAccount " +
170 // "set type = ? " +
171 // "where type is null and account like '" + account + "%'",
172 // new Object[] {
173 // currentType
174 // });
175 // dao.executeUpdate(
176 // "update MajorAccount " +
177 // "set type = ? " +
178 // "where type = ? and account like '" + account + "%'",
179 // new Object[] {
180 // MajorAccountType.MAJOR_BOTH, MajorAccountType.MAJOR_ACTIVE
181 // });
182 // } else {
183 // dao.executeUpdate(
184 // "update MajorAccount " +
185 // "set type = ? " +
186 // "where type is null and account like '" + account + "%'",
187 // new Object[] {
188 // currentType
189 // });
190 // }
191  if(account.equals("129")) {
192  return results(year);
193  } else {
194  CheckDate used = new CheckDate(date);
195  used.setYear(year);
196  Object obj = dao.getSingleResult(
197  "select sum(re.debit)-sum(re.credit) from RegisterEntry re " +
198  "join re.register r " +
199  "where year(r.registerDate) = ? " +
200  "and r.registerDate <= ? " +
201  "and r.view = ? " +
202  "and re.account.id like '" + account + "%' " +
203  "and r.exclude = false",
204  new Object[] {
205  year, used.getDate(), view
206  });
207  if(obj instanceof Double) {
208  Double d = (Double) obj;
209  if(asSign) {
210  if((d < 0 && currentType.equals(MajorAccountType.MAJOR_PASSIVE)) ||
211  (d > 0 && currentType.equals(MajorAccountType.MAJOR_ACTIVE))) {
212  return d;
213  }
214  } else {
215  return d;
216  }
217  }
218  return 0;
219  }
220  }
221 
222  private boolean existClosing(int exercise) {
223  Dao dao = new FinancialsPU();
224  List<Register> list = dao.getResultList(
225  "select reg from Register reg " +
226  "where year(reg.registerDate) = ? " +
227  "and month(reg.registerDate) = 12 " +
228  "and reg.exclude = TRUE and reg.closing = TRUE " +
229  "and reg.document is null",
230  new Object[] {
231  Integer.valueOf(exercise)
232  });
233  return !list.isEmpty();
234  }
235 
236  private double results(int exercise) {
237  CheckDate used = new CheckDate(date);
238  used.setYear(year);
239  Object obj;
240  if(!existClosing(exercise)) {
241  obj = dao.getSingleResult(
242  "select sum(re.debit)-sum(re.credit) from RegisterEntry re " +
243  "join re.register r " +
244  "where year(r.registerDate) = ? " +
245  "and r.registerDate <= ? " +
246  "and r.view = ? " +
247  "and (re.account.id like '6%' or re.account.id like '7%')" +
248  "and r.exclude = false",
249  new Object[] {
250  Integer.valueOf(exercise), used.getDate(), view
251  });
252  if(obj instanceof Double) {
253  return ((Double) obj) + pending(exercise);
254  }
255  } else {
256  return result129(exercise);
257  }
258  return 0;
259  }
260 
261  private double pending(int exercise) {
262  Object obj = dao.getSingleResult(
263  "select sum(re.debit)-sum(re.credit) from RegisterEntry re " +
264  "join re.register r " +
265  "where year(r.registerDate) = ? " +
266  "and r.view = ? " +
267  "and re.account.id like '129%' " +
268  "and r.exclude = false",
269  new Object[] {
270  Integer.valueOf(exercise), view
271  });
272  if(obj instanceof Double) {
273  return (Double) obj;
274  }
275  return 0;
276  }
277 
278  private double result129(int exercise) {
279  Object obj = dao.getSingleResult(
280  "select -sum(re.credit) from RegisterEntry re " +
281  "join re.register r " +
282  "where year(r.registerDate) = ? " +
283  "and r.view = ? " +
284  "and re.account.id like '129%' " +
285  "and r.exclude = true",
286  new Object[] {
287  Integer.valueOf(exercise), view
288  });
289  if(obj instanceof Double) {
290  return (Double) obj;
291  }
292  return 0;
293  }
294 
295 }
ReportSheet(File sheetFile, Date date, RegisterView view)
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380