19 package org.turro.financials.report;
22 import java.io.IOException;
23 import java.io.Writer;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.TreeSet;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import java.util.stream.Stream;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.db.WhereClause;
34 import org.turro.elephant.impl.util.FileUtil;
35 import org.turro.financials.db.FinancialsPU;
36 import org.turro.financials.entity.RegisterEntry;
37 import org.turro.reflection.JSONSerializer;
46 private TreeSet<ReportRow> rows;
47 private TreeSet<ReportColumn> columns;
48 private transient HashMap<String, Double> values;
49 private transient HashMap<String, HashMap<String, Double>> sum;
50 private transient File file;
61 if(rows ==
null) rows =
new TreeSet<>();
66 if(columns ==
null) columns =
new TreeSet<>();
72 Double accumulate = values.get(generateKey(row, column));
73 if(accumulate !=
null) {
74 return formatAndAddSum(row, column, accumulate);
82 Double total = sum.get(group).get(generateKey(
null, column));
83 sum.get(group).put(generateKey(
null, column), 0.0);
85 return row.
isNegate() ? -total : total;
95 public void fill(Date start, Date end) {
96 if(values !=
null) values.clear();
97 if(sum !=
null) sum.clear();
99 wc.
addClause(
"select e from RegisterEntry e");
100 wc.
addClause(
"where e.register.registerDate >= :start");
101 wc.
addClause(
"and e.register.registerDate <= :end");
105 entries.forEach((e) -> {
110 if(sum ==
null) sum =
new HashMap<>();
113 sum.put(row.getGroup(),
new HashMap<>());
119 return values !=
null && !values.isEmpty();
122 private void setFile(File file) {
126 private void addEntry(RegisterEntry e) {
127 rows.forEach((row) -> {
128 if(!row.isSum() && row.check(e)) {
129 columns.forEach((column) -> {
130 if(column.check(e)) {
131 addValue(e, row, column);
138 private String generateKey(ReportRow row, ReportColumn column) {
139 return row ==
null ?
"" + column.hashCode() : row.hashCode() +
"|" + column.hashCode();
142 private void addValue(RegisterEntry e, ReportRow row, ReportColumn column) {
143 if(values ==
null) values =
new HashMap<>();
145 switch(row.getSumType()) {
147 value = e.getDebit() - e.getCredit();
150 value = e.getDebit();
153 value = e.getCredit();
156 Double accumulate = values.get(generateKey(row, column));
157 if(accumulate ==
null) {
162 values.put(generateKey(row, column), accumulate);
165 private Double formatAndAddSum(ReportRow row, ReportColumn column, Double value) {
166 value = row.isNegate() ? -value : value;
167 for(HashMap<String, Double> sm : sum.values()) {
168 Double total = sm.get(generateKey(
null, column));
174 sm.put(generateKey(
null, column), total);
181 private static final String dveRoot =
"/WEB-INF/elephant/reports/financials";
186 List<ReportBody> reports =
new ArrayList<>();
187 for(File file : root.listFiles()) {
188 if(file.isFile() && !file.isHidden()) {
199 JSONSerializer ser =
new JSONSerializer(
true);
202 }
catch (IOException ex) {
214 private static void save(
ReportBody body, String name) {
216 JSONSerializer ser =
new JSONSerializer(body,
true);
217 writer.write(ser.toJson());
218 }
catch (IOException ex) {
219 Logger.getLogger(ReportBody.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
223 private static String fileName(String name) {
224 return ElephantContext.getRealPath(dveRoot) +
"/" + name +
".json";
229 private static void setTest(ReportBody body) {
230 ReportRow row =
new ReportRow(0,
"Test row",
new String[] {
"[67].*"}, ReportSumType.BALANCE,
false);
231 body.getRows().add(row);
232 ReportColumn column =
new ReportColumn(0,
"Test column",
new String[] {
".*"});
233 body.getColumns().add(column);
static String getRealPath(String path)
static String logMsg(String msg)
void addClause(String clause)
void addNamedValue(String name, Object value)
static OutputStreamWriter getFileWriter(String folder, String file)
static String getContent(File file)
Double getValue(ReportRow row, ReportColumn column)
void fill(Date start, Date end)
TreeSet< ReportRow > getRows()
static ReportBody load(File file)
TreeSet< ReportColumn > getColumns()
Double getSum(ReportRow row, ReportColumn column, String group)
static List< ReportBody > reports()