BrightSide Workbench Full Report + Source Code
DocumentByContractList.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.model.document.contract;
19 
20 import java.util.Date;
21 import java.util.TreeSet;
22 import javax.persistence.NoResultException;
23 import org.turro.financials.db.FinancialsPU;
24 import org.turro.financials.entity.Contract;
25 import org.turro.financials.entity.Document;
26 import org.turro.financials.entity.RegisterView;
27 import org.turro.financials.model.AccountFormat;
28 import org.turro.jpa.Dao;
29 
34 public class DocumentByContractList extends TreeSet<Document> {
35 
37  super(new DocumentByContractComparator());
38  }
39 
40  public void fillList(RegisterView view, Contract contract, Date from, Date to) {
41  Dao dao = new FinancialsPU();
42  if(view != null) {
43  addAll(dao.getResultList(
44  "select doc from Document doc " +
45  "where doc.contract = ? " +
46  "and doc.forcedView = ? " +
47  "and doc.documentDate >= ? " +
48  "and doc.documentDate <= ?",
49  new Object[] { contract, view, from, to }));
50  } else {
51  addAll(dao.getResultList(
52  "select doc from Document doc " +
53  "where doc.contract = ? " +
54  "and doc.forcedView is null " +
55  "and doc.documentDate >= ? " +
56  "and doc.documentDate <= ?",
57  new Object[] { contract, from, to }));
58  }
59  }
60 
61  public double getInitialBalance(RegisterView view, Contract contract, Date from, Date to) {
62  Dao dao = new FinancialsPU();
63  Date init = getInitialDate(dao, from);
64  Object o = null;
65  if(view != null) {
66  o = dao.getSingleResult(
67  "select sum(re.debit - re.credit) " +
68  "from RegisterEntry re " +
69  "join re.register r " +
70  "where r.view = ? " +
71  "and r.registerDate < ? " +
72  (init != null ? "and r.registerDate >= ? " : "and ? is null ") +
73  "and (re.account.id like '" + AccountFormat.expand("400." + contract.getId()) + "' " +
74  "or re.account.id like '" + AccountFormat.expand("401." + contract.getId()) + "' " +
75  "or re.account.id like '" + AccountFormat.expand("410." + contract.getId()) + "' " +
76  "or re.account.id like '" + AccountFormat.expand("411." + contract.getId()) + "' " +
77  "or re.account.id like '" + AccountFormat.expand("430." + contract.getId()) + "' " +
78  "or re.account.id like '" + AccountFormat.expand("431." + contract.getId()) + "' " +
79  "or re.account.id like '" + AccountFormat.expand("440." + contract.getId()) + "' " +
80  "or re.account.id like '" + AccountFormat.expand("441." + contract.getId()) + "')",
81  new Object[] { view, from, init });
82  } else {
83  o = dao.getSingleResult(
84  "select sum(re.debit - re.credit) " +
85  "from RegisterEntry re " +
86  "join re.register r " +
87  "where r.view.id = 1 " +
88  "and r.registerDate < ? " +
89  (init != null ? "and r.registerDate >= ? " : "and ? is null ") +
90  "and (re.account.id like '" + AccountFormat.expand("400." + contract.getId()) + "' " +
91  "or re.account.id like '" + AccountFormat.expand("401." + contract.getId()) + "' " +
92  "or re.account.id like '" + AccountFormat.expand("410." + contract.getId()) + "' " +
93  "or re.account.id like '" + AccountFormat.expand("411." + contract.getId()) + "' " +
94  "or re.account.id like '" + AccountFormat.expand("430." + contract.getId()) + "' " +
95  "or re.account.id like '" + AccountFormat.expand("431." + contract.getId()) + "' " +
96  "or re.account.id like '" + AccountFormat.expand("440." + contract.getId()) + "' " +
97  "or re.account.id like '" + AccountFormat.expand("441." + contract.getId()) + "')",
98  new Object[] { from, init });
99  }
100  if(o instanceof Double) {
101  if(contract.getContractDefinition().getId() == 55) {
102  return -((Double) o);
103  }
104  return (Double) o;
105  }
106  return 0.0;
107  }
108 
109  public Date getInitialDate(Dao dao, Date fromDate) {
110  try {
111  return (Date) dao.getSingleResult(
112  "select max(r.registerDate) from Register r " +
113  "where r.registerDate < ? " +
114  "and r.closing = TRUE " +
115  "and day(r.registerDate) = 1 " +
116  "and month(r.registerDate) = 1",
117  new Object[] { fromDate });
118  } catch(NoResultException ex) {
119  return null;
120  }
121  }
122 
123 }
ContractDefinition getContractDefinition()
Definition: Contract.java:125
void fillList(RegisterView view, Contract contract, Date from, Date to)
double getInitialBalance(RegisterView view, Contract contract, Date from, Date to)
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380