BrightSide Workbench Full Report + Source Code
AccountReportItem.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.report;
19 
20 import java.util.ArrayList;
21 import java.util.List;
22 import org.turro.string.Strings;
23 import org.turro.elephant.db.WhereClause;
24 import org.turro.util.PhraseBuilder;
25 
30 public class AccountReportItem {
31 
32  private AccountReport report;
33  private AccountReportItem parent;
34  private List<AccountReportItem> children = new ArrayList<AccountReportItem>();
35  private String label;
36  private List<String> patterns = new ArrayList<String>();
37  private boolean showTotal;
38  private PeriodSet periods;
39  private ItemMode mode = null;
40  private boolean totalItem = false;
41 
42  public AccountReportItem(String label) {
43  this.label = label;
44  }
45 
46  public AccountReportItem addItem(String label) {
47  return addItem(new AccountReportItem(label));
48  }
49 
51  item.setReport(report);
52  item.setParent(this);
53  children.add(item);
54  return item;
55  }
56 
57  public void addPattern(String pattern) {
58  patterns.add(pattern);
59  }
60 
61  public List<AccountReportItem> getChildren() {
62  return children;
63  }
64 
65  public String getLabel() {
66  return label;
67  }
68 
69  public void setLabel(String label) {
70  this.label = label;
71  }
72 
73  public ItemMode getMode() {
74  return mode;
75  }
76 
77  public void setMode(ItemMode mode) {
78  this.mode = mode;
79  }
80 
82  return parent;
83  }
84 
85  public void setParent(AccountReportItem parent) {
86  this.parent = parent;
87  }
88 
89  public PeriodSet getPeriods() {
90  return periods;
91  }
92 
94  return report;
95  }
96 
97  public void setReport(AccountReport report) {
98  this.report = report;
99  }
100 
101  public boolean isShowTotal() {
102  return showTotal;
103  }
104 
105  public void setShowTotal(boolean showTotal) {
106  this.showTotal = showTotal;
107  }
108 
109  public boolean isTitleItem() {
110  return patterns == null || patterns.isEmpty();
111  }
112 
113  public boolean isTotalItem() {
114  return totalItem;
115  }
116 
117  public void setTotalItem(boolean totalItem) {
118  this.totalItem = totalItem;
119  if(totalItem) {
120  periods = new PeriodSet();
121  }
122  }
123 
124  public AccountReportItem getItem(String label) {
125  if(!Strings.isBlank(label)) {
126  for(AccountReportItem ari : children) {
127  if(label.equals(ari.getLabel())) {
128  return ari;
129  }
130  AccountReportItem ari2 = ari.getItem(label);
131  if(ari2 != null) {
132  return ari2;
133  }
134  }
135  }
136  return null;
137  }
138 
139  public PeriodSet loadData(PeriodSet existingPeriods) {
140  if(periods == null) {
141  periods = new PeriodSet();
142  if(!patterns.isEmpty()) {
143  String sqlQuery = getSQLQuery();
144  if(!sqlQuery.contains("in ()")) {
145  List l = getReport().getDao().getResultList(sqlQuery);
146  for(Object[] o : (List<Object[]>) l) {
147  AccountReportPeriod arp = new AccountReportPeriod((Integer) o[0], (Integer) o[1]);
148  arp.setValue((Double) o[2]);
149  arp.setItem(this);
150  periods.add(arp);
151  existingPeriods.add(arp);
152  }
153  }
154  }
155  for(AccountReportItem ari : children) {
156  ari.loadData(existingPeriods);
157  }
158  }
159  return periods;
160  }
161 
163  for(AccountReportPeriod period : getPeriods()) {
164  if(period.getYear() == accountReportPeriod.getYear() && period.getMonth() == accountReportPeriod.getMonth()) {
165  return period;
166  }
167  }
168  return null;
169  }
170 
171  public void regularizeMode(PeriodSet existingPeriods) {
172  for(AccountReportPeriod period : existingPeriods) {
173  AccountReportPeriod arp = new AccountReportPeriod(period.getYear(), period.getMonth());
174  arp.setItem(this);
175  periods.add(arp);
176  }
177  if(mode == ItemMode.BY_YEAR) {
178  double acumulated = 0;
179  int year = 0;
180  for(AccountReportPeriod period : getPeriods()) {
181  if(period.getYear() == year) {
182  acumulated += period.getValue();
183  } else {
184  acumulated = period.getValue();
185  year = period.getYear();
186  }
187  period.setValue(acumulated);
188  }
189  } else if(mode == ItemMode.BY_TAM) {
190  TamBuffer tam = new TamBuffer();
191  for(AccountReportPeriod period : getPeriods()) {
192  tam.add(period.getValue());
193  period.setValue(tam.getTotal());
194  }
195 
196  }
197  for(AccountReportItem ari : children) {
198  ari.regularizeMode(existingPeriods);
199  }
200  }
201 
203  AccountReportItem total = new AccountReportItem(label);
204  total.setMode(mode);
205  total.setParent(parent);
206  total.setReport(report);
207  total.setTotalItem(true);
208  for(AccountReportPeriod period : periods) {
209  AccountReportPeriod arp = new AccountReportPeriod(period.getYear(), period.getMonth());
210  arp.setItem(total);
211  arp.setValue(getAccumulatedFor(period));
212  total.getPeriods().add(arp);
213  }
214  return total;
215  }
216 
217  public double getAccumulatedFor(AccountReportPeriod period) {
218  AccountReportPeriod p = period == null ? null : getPeriodFor(period);
219  double accumulated = p == null ? 0.0 : p.getValue();
220  for(AccountReportItem item : children) {
221  accumulated += item.getAccumulatedFor(period);
222  }
223  return accumulated;
224  }
225 
226  private String getSQLQuery() {
227  WhereClause wc = new WhereClause();
228  wc.addClause("select year(re.register.registerDate), month(re.register.registerDate), sum(re.debit) - sum(re.credit)");
229  wc.addClause("from RegisterEntry re");
230  wc.addClause("where re.account.id in " + getInString());
231  wc.addClause("and re.register.exclude = FALSE");
232  wc.addClause("group by year(re.register.registerDate), month(re.register.registerDate)");
233  return wc.getClause();
234  }
235 
236  private String getInString() {
237  PhraseBuilder pb = new PhraseBuilder("(");
238  for(String account : getReport().getAccounts()) {
239  for(String regexp : patterns) {
240  if(account.matches(regexp)) {
241  pb.addWord("'" + account + "'");
242  pb.addPendingSeparator(",");
243  break;
244  }
245  }
246  }
247  pb.cancelSeparator();
248  pb.addWord(")");
249  return pb.toString();
250  }
251 
252 }
void regularizeMode(PeriodSet existingPeriods)
AccountReportItem addItem(AccountReportItem item)
PeriodSet loadData(PeriodSet existingPeriods)
AccountReportPeriod getPeriodFor(AccountReportPeriod accountReportPeriod)
double getAccumulatedFor(AccountReportPeriod period)