BrightSide Workbench Full Report + Source Code
ContractItemSet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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 
19 package org.turro.financials.operating;
20 
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.TreeSet;
25 import org.amic.util.date.CheckDate;
26 import org.turro.elephant.db.WhereClause;
27 import org.turro.financials.db.FinancialsPU;
28 import org.turro.financials.entity.Account;
29 import org.turro.jpa.Dao;
30 import org.turro.util.CompareUtil;
31 
36 public class ContractItemSet extends TreeSet<ContractItem> {
37 
38  private Date start, end;
39 
40  public ContractItem getContract(long id) {
41  ContractItem ci = ceiling(new ContractItem(id));
42  if(ci != null && ci.getId() == id) {
43  return ci;
44  } else {
45  ci = new ContractItem(id);
46  add(ci);
47  return ci;
48  }
49  }
50 
51  public void loadPeriod(Date start, Date end) {
52  if(CompareUtil.compare(this.start, start) != 0 || CompareUtil.compare(this.end, end) != 0) {
53  this.start = start;
54  this.end = end;
55  this.clear();
56  processData(loadData());
57  }
58  }
59 
60  private List<Object[]> loadData() {
61  Dao dao = new FinancialsPU();
62  return dao.getResultList(getCriteria());
63  }
64 
65  private WhereClause getCriteria() {
66  WhereClause wc = new WhereClause();
67  wc.addClause("select r.id, r.registerDate, re.account, re.debit, re.credit");
68  wc.addClause("from Register as r");
69  wc.addClause("join r.registerEntries re");
70  wc.addClause("where r.registerDate >= :start");
71  wc.addNamedValue("start", start);
72  wc.addClause("and r.registerDate <= :end");
73  wc.addNamedValue("end", end);
74  wc.addClause("and (re.account like '6%'");
75  wc.addClause("or re.account like '7%'");
76  wc.addClause("or re.account like '400%'");
77  wc.addClause("or re.account like '410%'");
78  wc.addClause("or re.account like '430%'");
79  wc.addClause("or re.account like '440%')");
80  return wc;
81  }
82 
83  private long currentId = 0;
84  private List<RegisterItem> register;
85 
86  private void processData(List<Object[]> data) {
87  currentId = 0;
88  for(Object[] o : data) {
89  processLines(o);
90  }
91  processList();
92  }
93 
94  private void processLines(Object[] data) {
95  long regId = (long) data[0];
96  if(regId != currentId) {
97  processList();
98  register = new ArrayList<>();
99  currentId = regId;
100  }
101  RegisterItem ri = new RegisterItem();
102  ri.regId = regId;
103  ri.date = new CheckDate((Date) data[1]);
104  ri.account = (Account) data[2];
105  ri.debit = (double) data[3];
106  ri.credit = (double) data[4];
107  register.add(ri);
108  }
109 
110  private RegisterItem participant;
111  private final List<RegisterItem> stores = new ArrayList<>();
112 
113  private void processList() {
114  stores.clear();
115  participant = null;
116  if(register != null && !register.isEmpty()) {
117  findPrimaryStore();
118  findParticipant();
119  if(!stores.isEmpty() && participant != null) {
120  for(RegisterItem store : stores) {
121  ContractItem ci = getContract(store.getContractId());
122  if(ci.getName() == null) {
123  ci.setName(null);
124  }
125  MonthItem mi = ci.getMonths().getMonth(store.getYear(), store.getMonth());
126  ValueItem vi = mi.getMovements().getValues().getItem(store.account.getId().substring(0, 5));
127  if(vi.getName() == null) {
128  vi.setName(store.account.getParent().getDescription());
129  }
130  vi.setValue(vi.getValue() + store.getValue());
131  // Clear '9' for 4xx9
132  String major = participant.account.getId();
133  if(major.charAt(3) == '9') {
134  major = major.substring(0, 3) + "0" + major.substring(4);
135  }
136  vi = mi.getMovements().getParticipants().getItem(major);
137  if(vi.getName() == null) {
138  vi.setName(participant.account.getDescription());
139  }
140  vi.setValue(vi.getValue() + store.getValue());
141  }
142  }
143  }
144  }
145 
146  private void findPrimaryStore() {
147  for(RegisterItem ri : register) {
148  if(ri.account.getId().matches("6.*") && ri.debit != 0.0) {
149  stores.add(ri);
150  } if(ri.account.getId().matches("7.*") && ri.credit != 0.0) {
151  stores.add(ri);
152  }
153  }
154  }
155 
156  private void findParticipant() {
157  double max = 0;
158  for(RegisterItem ri : register) {
159  if(ri.account.getId().matches("4[34]0.*") && ri.debit != 0.0) {
160  if(max < Math.abs(ri.debit)) {
161  participant = ri;
162  max = Math.abs(ri.debit);
163  }
164  } if(ri.account.getId().matches("4[01]0.*") && ri.credit != 0.0) {
165  if(max < Math.abs(ri.credit)) {
166  participant = ri;
167  max = Math.abs(ri.credit);
168  }
169  }
170  }
171  }
172 
173 }