BrightSide Workbench Full Report + Source Code
StatementSet.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.account.logic;
19 
20 import java.util.*;
21 import org.amic.util.date.CheckDate;
22 import org.turro.financials.db.FinancialsPU;
23 import org.turro.financials.entity.RegisterEntry;
24 import org.turro.financials.entity.RegisterView;
25 import org.turro.jpa.Dao;
26 import org.turro.validate.ValidatedStatus;
27 
32 public class StatementSet extends TreeSet<StatementEntry> {
33 
34  private Date fromDate, toDate;
35  private String accountSearch;
36  private RegisterView view;
38 
39  public StatementSet() {
40  initiateValues();
41  }
42 
43  public StatementSet(double initialDebit, double initialCredit, Collection<? extends StatementEntry> c) {
44  super(c);
45  add(new StatementFirst(initialDebit, initialCredit));
46  checkBalance();
47  }
48 
49  public String getAccountSearch() {
50  return accountSearch;
51  }
52 
53  public void setAccountSearch(String accountSearch) {
54  this.accountSearch = accountSearch;
55  }
56 
57  public Date getFromDate() {
58  return fromDate;
59  }
60 
61  public void setFromDate(Date fromDate) {
62  this.fromDate = fromDate;
63  }
64 
65  public Date getToDate() {
66  return toDate;
67  }
68 
69  public void setToDate(Date toDate) {
70  this.toDate = toDate;
71  }
72 
74  return validated;
75  }
76 
77  public void setValidated(ValidatedStatus validated) {
78  this.validated = validated;
79  }
80 
81  public RegisterView getView() {
82  return view;
83  }
84 
85  public void setView(RegisterView view) {
86  this.view = view;
87  }
88 
89  public void previous() {
90  long millis = toDate.getTime() - fromDate.getTime();
91  toDate.setTime(fromDate.getTime());
92  fromDate.setTime(toDate.getTime() - millis);
93  }
94 
95  public void next() {
96  long millis = toDate.getTime() - fromDate.getTime();
97  fromDate.setTime(toDate.getTime());
98  toDate.setTime(fromDate.getTime() + millis);
99  }
100 
101  public void refreshData() {
102  clear();
103  if(accountSearch == null) return;
104  Dao dao = new FinancialsPU();
105  StatementFirst sf = new StatementFirst();
106  sf.setAccountSearch(accountSearch);
107  sf.setFromDate(fromDate);
108  sf.setView(view);
109  sf.refreshData(dao);
110  add(sf);
111  addEntries(dao);
112  checkBalance();
113  }
114 
115  public void addBank() {
116  if(!hasBank()) {
117  addAll(BankAccounts.getMovements(accountSearch, fromDate, toDate));
118  }
119  }
120 
121  private boolean hasBank() {
122  for(StatementEntry se : this) {
123  if(se instanceof BankEntry) {
124  return true;
125  }
126  }
127  return false;
128  }
129 
130  private void checkBalance() {
131  Iterator<StatementEntry> it = iterator();
132  double balance = 0;
133  while(it.hasNext()) {
134  StatementEntry se = it.next();
135  if(!(se instanceof BankEntry)) {
136  se.setInheritedBalance(balance);
137  balance = se.getBalance();
138  }
139  }
140  }
141 
142  private void addEntries(Dao dao) {
143  Object[] pars = view != null ?
144  new Object[] {
145  accountSearch.replaceAll("\\*", "%").replaceAll("\\.", "%"),
146  fromDate,
147  toDate,
148  view
149  } :
150  new Object[] {
151  accountSearch.replaceAll("\\*", "%").replaceAll("\\.", "%"),
152  fromDate,
153  toDate
154  };
155  List<RegisterEntry> l = (List<RegisterEntry>) dao.getResultList(
156  "select distinct entry " +
157  "from RegisterEntry as entry " +
158  "where entry.account.id like ? " +
159  "and entry.register.registerDate >= ? " +
160  "and entry.register.registerDate <= ? " +
161  (ValidatedStatus.VALIDATED_NOT.equals(validated) ?
162  "and entry.conciliated = FALSE " : "") +
163  (ValidatedStatus.VALIDATED_YES.equals(validated) ?
164  "and entry.conciliated = TRUE " : "") +
165  (view != null ? "and entry.register.view = ?" : ""),
166  pars, 400);
167  for(RegisterEntry re : l) {
168  add(new StatementEntry(re));
169  }
170  }
171 
172  private void initiateValues() {
173  toDate = new Date();
174  fromDate = new CheckDate(toDate).addMonths(-1).getDate();
175  view = null;
176  }
177 
178 }
static Collection< StatementEntry > getMovements(String account, Date start, Date end)
StatementSet(double initialDebit, double initialCredit, Collection<? extends StatementEntry > c)
void setValidated(ValidatedStatus validated)