18 package org.turro.financials.sheets;
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
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;
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;
55 private File sheetFile;
59 private List<WritableCell> clearCells;
64 this.sheetFile = sheetFile;
67 this.year =
new CheckDate(date).getYear();
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);
85 for(WritableCell cell : clearCells) {
86 sheet.addCell(
new Label(cell.getColumn(), cell.getRow(),
""));
93 return new ByteArrayInputStream(baos.toByteArray());
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)) {
103 }
else if(
"PATRIMONIO NETO Y PASIVO".equalsIgnoreCase(value)) {
105 }
else if(
"CUENTA PERDIDAS Y GANACIAS %year".equalsIgnoreCase(value)) {
108 processValue(value, sheet, row, col);
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;
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;
129 if(value.contains(
"%")) {
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);
143 private double getAmount(
int year, String accounts) {
144 if(accounts ==
null)
return 0;
146 String accs[] = accounts.split(
",");
147 for(String acc : accs) {
148 amount += getValue(year, acc.trim());
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);
159 if(account.endsWith(
"*")) {
160 account = account.substring(0, account.length() - 1);
163 if(account.startsWith(
"(")) {
164 account = account.substring(1, account.length() - 1);
191 if(account.equals(
"129")) {
192 return results(year);
194 CheckDate used =
new CheckDate(date);
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 <= ? " +
202 "and re.account.id like '" + account +
"%' " +
203 "and r.exclude = false",
205 year, used.getDate(), view
207 if(obj instanceof Double) {
208 Double d = (Double) obj;
210 if((d < 0 && currentType.equals(MajorAccountType.MAJOR_PASSIVE)) ||
211 (d > 0 && currentType.equals(MajorAccountType.MAJOR_ACTIVE))) {
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",
231 Integer.valueOf(exercise)
233 return !list.isEmpty();
236 private double results(
int exercise) {
237 CheckDate used =
new CheckDate(date);
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 <= ? " +
247 "and (re.account.id like '6%' or re.account.id like '7%')" +
248 "and r.exclude = false",
250 Integer.valueOf(exercise), used.getDate(), view
252 if(obj instanceof Double) {
253 return ((Double) obj) + pending(exercise);
256 return result129(exercise);
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) = ? " +
267 "and re.account.id like '129%' " +
268 "and r.exclude = false",
270 Integer.valueOf(exercise), view
272 if(obj instanceof Double) {
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) = ? " +
284 "and re.account.id like '129%' " +
285 "and r.exclude = true",
287 Integer.valueOf(exercise), view
289 if(obj instanceof Double) {
ReportSheet(File sheetFile, Date date, RegisterView view)
Object getSingleResult(WhereClause wc)