BrightSide Workbench Full Report + Source Code
bsfinancials-core/src/main/java/org/turro/financials/model/contract/ContractWrapper.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.contract;
19 
20 import java.util.*;
21 import org.turro.string.Strings;
22 import org.turro.action.Plugins;
23 import org.turro.elephant.db.WhereClause;
24 import org.turro.financials.db.FinancialsPU;
25 import org.turro.financials.entity.*;
26 import org.turro.financials.model.AccountFormat;
27 import org.turro.financials.model.document.DocumentWrapper;
28 import org.turro.financials.model.register.ViewWrapper;
29 import org.turro.jpa.Dao;
30 import org.turro.jpa.entity.DaoEntity;
31 import org.turro.plugin.contacts.IContact;
32 import org.turro.plugin.dossier.ICategory;
33 import org.turro.plugin.dossier.IDossier;
34 
39 public class ContractWrapper extends DaoEntity<Contract, Long> {
40 
42  super(entity);
43  }
44 
45  @Override
46  protected Dao createDao() {
47  return new FinancialsPU();
48  }
49 
50  public Collection<DocumentDefinition> getExpiryDefinitions(DocumentDefinition source) {
51  if(entity == null || source == null) return Collections.EMPTY_LIST;
52  ArrayList<DocumentDefinition> list = new ArrayList<>();
53  for(DocumentWorkflow dw : entity.getContractDefinition().getDocumentWorkflows()) {
54  if(dw.getAncestor().getId() == source.getId() && dw.isAllowNewDescendants()) {
55  list.add(dw.getDescendant());
56  }
57  }
58  return list;
59  }
60 
61  @Override
62  public boolean canDelete() {
63  if(!super.canDelete()) return false;
64  Long count = (Long) getDao().getSingleResult(
65  "select count(*) from Document " +
66  "where contract = ?",
67  new Object[] { entity });
68  if(count == 0) {
69  count = (Long) getDao().getSingleResult(
70  "select count(*) from DocumentLine " +
71  "where store = ?",
72  new Object[] { entity });
73  }
74  return count == 0;
75  }
76 
77  @Override
78  protected boolean shouldLog() {
79  return true;
80  }
81 
82  public void generateRegisters(DocumentDefinition docDef, int year) {
83  for(Document doc : getDocuments(docDef, year)) {
84  new DocumentWrapper(doc).save(null, null);
85  }
86  }
87 
88  public Collection<Document> getDocuments(DocumentDefinition docDef, int year) {
89  return getDao().getResultList(
90  "select doc from Document as doc " +
91  "where doc.contract = ? " +
92  "and doc.documentDefinition = ? " +
93  "and year(doc.receiptDate) = ? " +
94  "order by doc.documentDate",
95  new Object[] { entity, docDef, year });
96  }
97 
98  transient RegisterView lastView;
99  transient Double advance;
100 
101  public double getAdvance(RegisterView view) {
102  String acc = entity.getContractDefinition().getAsCash();
103  if(!Strings.isBlank(acc) && (acc.startsWith("560") ||
104  acc.startsWith("561") || acc.startsWith("565") || acc.startsWith("566"))) {
105  WhereClause wc = new WhereClause();
106  wc.addClause("select sum(re.debit-re.credit) from RegisterEntry as re");
107  wc.addClause("where re.account.id = :idacc");
108  wc.addNamedValue("idacc", AccountFormat.expand(acc + "." + entity.getId()));
109  wc.addClause("and re.register.view = :view");
110  if(view != null) {
111  wc.addNamedValue("view", view);
112  } else {
114  }
115  advance = (Double) getDao().getSingleResultOrNull(wc);
116  if("566".equals(entity.getContractDefinition().getAsCash())) {
117  advance = advance != null ? -advance : 0.0d;
118  } else {
119  advance = advance != null ? advance : 0.0d;
120  }
121  return advance;
122  } else {
123  return 0.0d;
124  }
125  }
126 
127  public void addAdvanceIfNeeded(RegisterView view, Collection<Contract> contracts) {
128  if(getAdvance(view) != 0.0d) {
129  Iterator<Contract> it = contracts.iterator();
130  while(it.hasNext()) {
131  Contract ctc = it.next();
132  if(ctc.getId() == entity.getId()) {
133  return;
134  }
135  }
136  contracts.add(entity);
137  }
138  }
139 
140  public static Collection<Contract> getContracts(ContractDefinition contractDefinition) {
141  Dao dao = new FinancialsPU();
142  return dao.getResultList(
143  "select ctc from Contract as ctc " +
144  "where ctc.contractDefinition = ? " +
145  "and ctc.active = TRUE " +
146  "order by ctc.name",
147  new Object[] { contractDefinition });
148  }
149 
150  public static Collection<Contract> getContracts(Long contractDefinitionId) {
151  Dao dao = new FinancialsPU();
152  return dao.getResultList(
153  "select ctc from Contract as ctc " +
154  "where ctc.contractDefinition.id = ? " +
155  "and ctc.active = TRUE " +
156  "order by ctc.name",
157  new Object[] { contractDefinitionId });
158  }
159 
160  public static void clearActives(Collection<Contract> contracts) {
161  Date now = new Date();
162  Iterator<Contract> it = contracts.iterator();
163  while(it.hasNext()) {
164  if(isActive(now, it.next())) {
165  it.remove();
166  }
167  }
168  }
169 
170  public static void clearInactives(Collection<Contract> contracts) {
171  Date now = new Date();
172  Iterator<Contract> it = contracts.iterator();
173  while(it.hasNext()) {
174  if(!isActive(now, it.next())) {
175  it.remove();
176  }
177  }
178  }
179 
180  public IDossier getDossier() {
181  if(entity.getId() > 0) {
182  IDossier dossier = (IDossier) Plugins.loadImplementation(IDossier.class);//PluginChecker.get("dossier");
183  ICategory cat = dossier.addCategory();
184  cat.setDescription("Financials");
185  cat.setId(9000);
186  cat = dossier.addCategory();
187  cat.setDescription("Contracts");
188  cat.setId(9001);
189  cat.setParentId(9000);
190  cat = dossier.addCategory();
191  cat.setDescription(entity.getContractDefinition().getName());
192  cat.setId(9500 + entity.getContractDefinition().getId());
193  cat.setParentId(9001);
194  dossier.setDescription(entity.getFullDescription());
195  dossier.setIdCategory(cat.getId());
197  dossier.setSubject(entity.getIContractor());
198  return dossier;
199  }
200  return null;
201  }
202 
203  public static boolean isActive(Date now, Contract contract) {
204  return contract.isActive() &&
205  (contract.getStartDate() == null || now.after(contract.getStartDate())) &&
206  (contract.getEndDate() == null || now.before(contract.getEndDate()));
207  }
208 
209  public static Collection<Contract> getContracts(IContact contact) {
210  if(contact == null) return null;
211 
212  Dao dao = new FinancialsPU();
213  return dao.getResultList(
214  "select ctc from Contract as ctc " +
215  "left outer join ctc.participants participant " +
216  "where ctc.contractor = ? or (participant.idContact = ? and participant.interventionType = ?) " +
217  "order by ctc.name",
218  new Object[] { contact.getId(), contact.getId(), ContractInterventionType.INT_OPERATOR });
219  }
220 
221  public static Collection<DocumentDefinition> getExpiryDefinitions(ContractDefinition contractDefinition, DocumentDefinition documentDefinition) {
222  ArrayList<DocumentDefinition> list = new ArrayList<DocumentDefinition>();
223  for(DocumentWorkflow dw : contractDefinition.getDocumentWorkflows()) {
224  if(dw.getAncestor().getId() == documentDefinition.getId() && dw.isAllowNewDescendants()) {
225  list.add(dw.getDescendant());
226  }
227  }
228  return list;
229  }
230 
231 }
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
void addNamedValue(String name, Object value)
static String getObjectPath(Object object)
static Collection< DocumentDefinition > getExpiryDefinitions(ContractDefinition contractDefinition, DocumentDefinition documentDefinition)
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
boolean equals(Object obj)
Definition: DaoEntity.java:154
void setDescription(String description)
void setSubject(IContact subject)
void setIdCategory(Long idCategory)
void setDescription(String description)