BrightSide Workbench Full Report + Source Code
m111/ModelSet.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.treasury.m111;
19 
20 import java.util.Date;
21 import java.util.List;
22 import java.util.TreeSet;
23 import org.turro.financials.db.FinancialsPU;
24 import org.turro.financials.entity.Document;
25 import org.turro.financials.entity.DocumentLine;
26 import org.turro.financials.entity.Register;
27 import org.turro.financials.entity.RegisterEntry;
28 import org.turro.jpa.Dao;
29 import org.turro.math.Round;
30 import org.turro.math.Zero;
31 
36 public class ModelSet extends TreeSet<ModelEntry> {
37 
38  private Date fromDate, toDate;
39  private Dao dao;
40 
41  public ModelSet(Date fromDate, Date toDate) {
42  this.fromDate = fromDate;
43  this.toDate = toDate;
44  createEntries();
45  }
46 
47  public Dao getDao() {
48  if(dao == null) {
49  dao = new FinancialsPU();
50  }
51  return dao;
52  }
53 
54  private void createEntries() {
55  List<Register> l = getDao().getResultList(
56  "select distinct r from Register r " +
57  "join r.registerEntries re " +
58  "where r.registerDate >= ? " +
59  "and r.registerDate <= ? " +
60  "and (r.exclude = FALSE and r.closing = FALSE and r.regularizeIRPF = FALSE) " +
61  "and re.account.id like '4751%' " +
62  "and re.credit <> 0 " +
63  "and r.view.id = 1",
64  new Object[] {
65  fromDate,
66  toDate
67  });
68  for(Register r : l) {
69  addRegister(r);
70  }
71  }
72 
73  private void addRegister(Register register) {
74  double retention = 0.0, perCent = 0.0, taxable = 0.0;
75  long contractId = 0, documentId = 0;
76  String contractName = "*****";
77  ModelType type = null;
78  Document doc = register.getDocument();
79  if(doc != null) {
80  documentId = doc.getId();
81  contractId = doc.getContract().getId();
82  contractName = doc.getContract().getFullDescription();
83  for(DocumentLine dl : doc.getDocumentLines()) {
84  if(!Zero.near(dl.getRetention(), 0)) {
85  perCent = dl.getRetention();
86  retention += dl.getRetained();
87  taxable += dl.getTaxable();
88  }
89  }
90  } else {
91  return;
92  }
93  boolean useRegister = false;
94  if(retention == 0.0) {
95  useRegister = true;
96  taxable = 0.0;
97  }
98  for(RegisterEntry re : register.getRegisterEntries()) {
99  if(type == null) {
100  type = ModelType.checkType(re.getAccount().getId());
101  if(type != null) {
102  if(useRegister) {
103  retention = re.getCredit();
104  } else if(retention != re.getCredit()) {
105  // alert!!
106  }
107  if(useRegister) {
108  perCent = Double.valueOf(re.getAccount().getId().substring(7));
109  }
110  }
111  }
112  if(useRegister) {
113  if(ModelType.isTaxable(re.getAccount().getId())) {
114  taxable += re.getDebit();
115  }
116  }
117  }
118  if(useRegister && perCent == 0.0 && taxable != 0.0 && retention != 0.0) {
119  perCent = (retention / taxable) * 100.0;
120  }
121  if(type != null) {
122  ModelEntry me = addModelEntry(type, perCent);
123  me.addValue(contractId, contractName, documentId, retention, taxable);
124  }
125  }
126 
127  private ModelEntry addModelEntry(ModelType type, double percent) {
128  for(ModelEntry me : this) {
129  if(me.getType().equals(type) && me.getPerCent() == new Round(percent).decimals(2).value()) {
130  return me;
131  }
132  }
133  ModelEntry me = new ModelEntry(this, type, percent);
134  add(me);
135  return me;
136  }
137 
138 }