BrightSide Workbench Full Report + Source Code
BalanceFilter.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.register;
19 
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.List;
24 import org.amic.util.date.CheckDate;
25 import org.turro.contacts.Contact;
26 import org.turro.financials.db.FinancialsPU;
27 import org.turro.financials.entity.*;
28 import org.turro.financials.view.ViewSets;
29 import org.turro.i18n.I_;
30 
35 public class BalanceFilter {
36 
37  private ViewSets viewSets;
38  private Date fromDate, toDate;
39  private Contract contract;
40  private Contact contact;
41  private Department department;
42  private Service service;
43  private Headquarters headquarters;
44  private Company company;
45  private List<Long> contractListIds;
46  private int breakdown = 4;
47 
48  public BalanceFilter() {
49  fromDate = new CheckDate().setDay(1).setMonth(1).getDate();
50  toDate = new Date();
51  viewSets = new ViewSets();
52  }
53 
54  public int getBreakdown() {
55  return breakdown;
56  }
57 
58  public void setBreakdown(int breakdown) {
59  this.breakdown = breakdown;
60  }
61 
62  public boolean hasOneGroup() {
63  return !(viewSets.getGroupA().isEmpty());
64  }
65 
66  public boolean hasTwoGroup() {
67  return !(viewSets.getGroupA().isEmpty()) &&
68  !(viewSets.getGroupB().isEmpty());
69  }
70 
71  public Date getFromDate() {
72  return fromDate;
73  }
74 
75  public void setFromDate(Date fromDate) {
76  this.fromDate = fromDate;
77  }
78 
79  public Date getToDate() {
80  return toDate;
81  }
82 
83  public void setToDate(Date toDate) {
84  this.toDate = toDate;
85  }
86 
87  public ViewSets getViewSets() {
88  return viewSets;
89  }
90 
91  public void setViewSets(ViewSets viewSets) {
92  this.viewSets = viewSets;
93  }
94 
95  public Company getCompany() {
96  return company;
97  }
98 
99  public void setCompany(Company company) {
100  clearValues();
101  this.company = company;
102  }
103 
104  public Contact getContact() {
105  return contact;
106  }
107 
108  public void setContact(Contact contact) {
109  clearValues();
110  this.contact = contact;
111  }
112 
114  return contract;
115  }
116 
117  public void setContract(Contract contract) {
118  clearValues();
119  this.contract = contract;
120  }
121 
123  return department;
124  }
125 
126  public void setDepartment(Department department) {
127  clearValues();
128  this.department = department;
129  }
130 
132  return headquarters;
133  }
134 
135  public void setHeadquarters(Headquarters headquarters) {
136  clearValues();
137  this.headquarters = headquarters;
138  }
139 
140  public Service getService() {
141  return service;
142  }
143 
144  public void setService(Service service) {
145  clearValues();
146  this.service = service;
147  }
148 
149  public String getFilterString() {
150  if(company != null) {
151  return I_.get("Company") + ": " + company.getName();
152  } else if(contact != null) {
153  return I_.get("Contact") + ": " + contact.getName();
154  } else if(contract != null) {
155  return I_.get("Contract") + ": " + contract.getFullDescription();
156  } else if(department != null) {
157  return I_.get("Department") + ": " + department.getFullName();
158  } else if(headquarters != null) {
159  return I_.get("Headquarters") + ": " + headquarters.getName();
160  } else if(service != null) {
161  return I_.get("Service") + ": " + service.getFullName();
162  }
163  return "";
164  }
165 
166  public String getFullFilterString() {
167  return viewSets.getStringFilter() + " - " + getFilterString();
168  }
169 
170  public Double[] getAmountsA(String account) {
171  return getAmounts(account, viewSets.getSqlWhereA("entry.register.view"));
172  }
173 
174  public Double[] getAmountsB(String account) {
175  return getAmounts(account, viewSets.getSqlWhereB("entry.register.view"));
176  }
177 
178  public Double[] getAmounts(String account, String viewFilter) {
179  Object[] pars = new Object[] {
180  account + "%",
181  fromDate,
182  toDate
183  };
184  Object[] o = (Object[]) new FinancialsPU().getSingleResult(
185  "select sum(round(entry.debit, 4) + 0.0), sum(round(entry.credit, 4) + 0.0) " +
186  "from RegisterEntry as entry " +
187  "where entry.account.id like ? " +
188  getInClause("entry.account.id") +
189  "and entry.register.registerDate >= ? " +
190  "and entry.register.registerDate <= ? " +
191  "and entry.register.exclude = FALSE " +
192  viewFilter,
193  pars);
194  return new Double[] {
195  (Double) (o[0] == null ? 0.0 : o[0]),
196  (Double) (o[1] == null ? 0.0 : o[1])
197  };
198  }
199 
200  public List<MajorAccount> getMajorAccounts(String parent) {
201  Object[] pars = new Object[] {
202  fromDate,
203  toDate
204  };
205  if(parent == null) {
206  parent = "";
207  }
208  parent += "_";
209  List<MajorAccount> l = new FinancialsPU().getResultList(
210  "select m from MajorAccount as m " +
211  "where m.account like '" + parent + "' " +
212  "and exists ( " +
213  " select re from RegisterEntry as re " +
214  " where re.account.id like concat(m.account, '%') " +
215  getInClause("re.account.id") +
216  " and re.register.registerDate >= ? " +
217  " and re.register.registerDate <= ? " +
218  viewSets.getSqlWhereAB("re.register.view") +
219  ") " +
220  "order by m.account",
221  pars);
222  return l;
223  }
224 
225  public List<Account> getAccounts(String parent) {
226  Object[] pars = new Object[] {
227  fromDate,
228  toDate
229  };
230  return new FinancialsPU().getResultList(
231  "select a from Account as a " +
232  "where exists ( " +
233  " select ma from MajorAccount as ma " +
234  " where ma.account = '" + parent + "' " +
235  " and a.id like concat(ma.account, '_%') " +
236  " and not exists ( " +
237  " select ma2 from MajorAccount as ma2 " +
238  " where ma2.account like concat(substring(a.id, 1, length(ma.account) + 1), '%') " +
239  " ) " +
240  ") " +
241  "and exists ( " +
242  " select re from RegisterEntry as re " +
243  " where re.account.id = a.id " +
244  getInClause("re.account.id") +
245  " and re.register.registerDate >= ? " +
246  " and re.register.registerDate <= ? " +
247  viewSets.getSqlWhereAB("re.register.view") +
248  ") " +
249  "order by a.id",
250  pars);
251  }
252 
253  public Collection<AccountRI> getAccountRIs() {
254  Object[] pars = new Object[] {
255  fromDate,
256  toDate
257  };
258  return new AccountRIAdapter(new FinancialsPU().getResultList(
259  "select a from Account as a " +
260  "where exists ( " +
261  " select re from RegisterEntry as re " +
262  " where re.account.id = a.id " +
263  getInClause("re.account.id") +
264  " and re.register.registerDate >= ? " +
265  " and re.register.registerDate <= ? " +
266  viewSets.getSqlWhereAB("re.register.view") +
267  ") " +
268  "order by a.id",
269  pars)).getRIs(this);
270  }
271 
272  private void clearValues() {
273  contract = null;
274  contact = null;
275  department = null;
276  service = null;
277  headquarters = null;
278  company = null;
279  }
280 
281  private String getInClause(String field) {
282  contractListIds = new ArrayList<Long>();
283  boolean done = false;
284  if(company != null) {
285  done = true;
286  fillCompanyContracts(company);
287  } else if(contact != null) {
288  done = true;
289  fillContactContracts(contact);
290  } else if(contract != null) {
291  done = true;
292  contractListIds.add(contract.getId());
293  } else if(department != null) {
294  done = true;
295  fillDepartmentContracts(department);
296  } else if(headquarters != null) {
297  done = true;
298  fillHeadquartersContracts(headquarters);
299  } else if(service != null) {
300  done = true;
301  fillServiceContracts(service);
302  }
303  StringBuilder sb = new StringBuilder("");
304  if(contractListIds.size() > 0) {
305  sb.append(" and substring(" + field + ",5,10)");
306  sb.append(" in (");
307  String sep = "";
308  for(Long l : contractListIds) {
309  sb.append(sep);
310  sb.append(l);
311  sep = ",";
312  }
313  sb.append(") ");
314  sb.append(" and " + field + " not like '477%'");
315  sb.append(" and " + field + " not like '472%'");
316  sb.append(" and " + field + " not like '475%'");
317  } else if(done) {
318  sb.append(" and " + field + " = 'zzzzzzzzzzzz'");
319  }
320  return sb.toString();
321  }
322 
323  private void fillContactContracts(Contact contact) {
324  for(Contract ctc : getContactContracts(contact)) {
325  contractListIds.add(ctc.getId());
326  }
327  }
328 
329  private void fillCompanyContracts(Company company) {
330  for(Service serv : company.getServices()) {
331  fillServiceContracts(serv);
332  }
333  for(Headquarters hq : company.getHeadquarters()) {
334  fillHeadquartersContracts(hq);
335  }
336  }
337 
338  private void fillServiceContracts(Service service) {
339  for(Contract ctc : service.getStores()) {
340  contractListIds.add(ctc.getId());
341  }
342  }
343 
344  private void fillHeadquartersContracts(Headquarters headquarters) {
345  for(Department dept : headquarters.getDepartments()) {
346  fillDepartmentContracts(dept);
347  }
348  }
349 
350  private void fillDepartmentContracts(Department department) {
351  for(Contract ctc : department.getStores()) {
352  contractListIds.add(ctc.getId());
353  }
354  }
355 
356  private Iterable<Contract> getContactContracts(Contact contact) {
357  return new FinancialsPU().getResultList(
358  "select contract from Contract as contract " +
359  "where contract.contractor = ?",
360  new Object[] { contact.getId() }
361  );
362  }
363 }
Set< Headquarters > getHeadquarters()
Definition: Company.java:80
Collection< AccountRI > getRIs(BalanceFilter filter)
void setHeadquarters(Headquarters headquarters)
List< Account > getAccounts(String parent)
List< MajorAccount > getMajorAccounts(String parent)
void setDepartment(Department department)
Double[] getAmounts(String account, String viewFilter)
Set< RegisterView > getGroupA()
Definition: ViewSets.java:40
String getSqlWhereA(String field)
Definition: ViewSets.java:99
String getSqlWhereAB(String field)
Definition: ViewSets.java:75
Set< RegisterView > getGroupB()
Definition: ViewSets.java:49
String getSqlWhereB(String field)
Definition: ViewSets.java:115
static String get(String msg)
Definition: I_.java:41
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380