BrightSide Workbench Full Report + Source Code
bsfinancials-www/src/main/java/org/turro/financials/contract/logic/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.contract.logic;
19 
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.List;
23 import org.turro.string.Strings;
24 import org.turro.elephant.util.Messages;
25 import org.turro.financials.db.FinancialsPU;
26 import org.turro.financials.entity.Contract;
27 import org.turro.financials.entity.ContractDefinition;
28 import org.turro.financials.entity.DocumentDefinition;
29 import org.turro.financials.entity.Tax;
30 import org.turro.financials.entity.TaxType;
31 import org.turro.i18n.I_;
32 import org.turro.jpa.Dao;
33 import org.turro.tags.Tags;
34 import org.turro.zkoss.input.CollectionListbox;
35 import org.zkoss.zk.ui.HtmlBasedComponent;
36 
41 public class ContractWrapper extends org.turro.financials.model.contract.ContractWrapper {
42 
43  public ContractWrapper(Contract contract) {
44  super(contract);
45  }
46 
47  public String getTabLabel() {
48  return getSelfLabel() + " #" + entity.getId();
49  }
50 
51  public String getSelfLabel() {
52  return I_.get("Contract");
53  }
54 
55  public String getTooltiptext() {
56  try {
57  return entity.getFullDescription();
58  } catch(Exception ex) {
59  return null;
60  }
61  }
62 
63  public void assignDefinition(ContractDefinition contractDefinition) {
64  entity.setContractDefinition(contractDefinition);
65  entity.setCash(contractDefinition.isCash());
66  entity.setStock(contractDefinition.isStock());
67  entity.setUseContractorCode(contractDefinition.isUseContractorCode());
68  }
69 
70  @Override
71  public Contract save() {
73 
74  if(entity.getContractDefinition() == null || entity.getContractor() == null) {
75  return null;
76  }
77 
78  entity = super.save();
79  checkContractedTag(entity.getContractor());
80  checkTaxes();
81  return entity;
82  }
83 
84  @Override
85  public void generateRegisters(final DocumentDefinition docDef, final int year) {
86  Messages.confirmProcess().show(() -> {
87  ContractWrapper.super.generateRegisters(docDef, year);
88  });
89  }
90 
91  public HtmlBasedComponent getTaxCtrl() {
92  ArrayList<Double> taxes = new ArrayList<Double>();
93  for(Tax tax : entity.getTaxes()) {
94  if(tax.getTaxType().equals(TaxType.VAT)) {
95  taxes.add(tax.getTax());
96  }
97  }
98  if(!taxes.isEmpty()) {
100  @Override
101  protected String convertToString(Double v) {
102  return v.toString();
103  }
104  };
105  taxes.add(0.0);
106  cl.setCollection(taxes);
107  cl.setMold("select");
108  return cl;
109  }
110  return null;
111 // if(!Strings.isBlank(entity.getNotes())) {
112 // String taxs = Strings.extract(entity.getNotes(), "IVA:([0-9\\,]+)");
113 // if(!Strings.isBlank(taxs)) {
114 // CollectionListbox<Double> cl = new CollectionListbox<Double>() {
115 // @Override
116 // protected String convertToString(Double v) {
117 // return v.toString();
118 // }
119 // };
120 // ArrayList<Double> al = new ArrayList<Double>();
121 // for(String tax : taxs.split("\\,")) {
122 // al.add(new Double(tax));
123 // }
124 // al.add(0.0);
125 // cl.setCollection(al);
126 // cl.setMold("select");
127 // return cl;
128 // }
129 // }
130 // return null;
131  }
132 
133  public HtmlBasedComponent getReqCtrl() {
134  ArrayList<Double> taxes = new ArrayList<Double>();
135  for(Tax tax : entity.getTaxes()) {
136  if(tax.getTaxType().equals(TaxType.VAT)) {
137  taxes.add(tax.getEquivalenceSurcharge());
138  }
139  }
140  if(!taxes.isEmpty()) {
142  @Override
143  protected String convertToString(Double v) {
144  return v.toString();
145  }
146  };
147  taxes.add(0.0);
148  cl.setCollection(taxes);
149  cl.setMold("select");
150  return cl;
151  }
152  return null;
153  }
154 
155  public HtmlBasedComponent getRetentionCtrl() {
156  ArrayList<Double> retentions = new ArrayList<Double>();
157  for(Tax tax : entity.getTaxes()) {
158  if(tax.getTaxType().equals(TaxType.RETENTION)) {
159  retentions.add(tax.getTax());
160  }
161  }
162  if(!retentions.isEmpty()) {
164  @Override
165  protected String convertToString(Double v) {
166  return v.toString();
167  }
168  };
169  retentions.add(0.0);
170  cl.setCollection(retentions);
171  cl.setMold("select");
172  return cl;
173  }
174  return null;
175 // if(!Strings.isBlank(entity.getNotes())) {
176 // String rets = Strings.extract(entity.getNotes(), "IRPF:([0-9\\,]+)");
177 // if(!Strings.isBlank(rets)) {
178 // CollectionListbox<Double> cl = new CollectionListbox<Double>() {
179 // @Override
180 // protected String convertToString(Double v) {
181 // return v.toString();
182 // }
183 // };
184 // ArrayList<Double> al = new ArrayList<Double>();
185 // for(String ret : rets.split("\\,")) {
186 // al.add(new Double(ret));
187 // }
188 // al.add(0.0);
189 // cl.setCollection(al);
190 // cl.setMold("select");
191 // return cl;
192 // }
193 // }
194 // return null;
195  }
196 
197  private void checkContractedTag(String contractor) {
198  Dao dao = new FinancialsPU();
199  Date now = new Date();
200  boolean active = false;
201  for(Contract ctc : (List<Contract>) dao.getResultList(
202  "select ctc from Contract as ctc where ctc.contractor = ?",
203  new Object[] { contractor })) {
204  if(isActive(now, ctc)) {
205  active = true;
206  break;
207  }
208  }
209  if(active) {
210  Tags.addTag("/contact/" + contractor, "contracted");
211  } else {
212  Tags.removeTag("/contact/" + contractor, "contracted");
213  }
214  }
215 
216  public void checkAllContractor() {
217  if(!Tags.existsTag("contracted")) {
218  Dao dao = new FinancialsPU();
219  for(String contractor : (List<String>) dao.getResultList(
220  "select distinct ctc.contractor from Contract as ctc")) {
221  checkContractedTag(contractor);
222  }
223  }
224  }
225 
226  private void checkTaxes() {
227  Dao dao = new FinancialsPU();
228  for(Contract ctc : (List<Contract>) dao.getResultList(
229  "select ctc from Contract as ctc where ctc.notes like '%IVA:%' or ctc.notes like '%IRPF:%'")) {
230  killTaxes(dao, ctc);
231  }
232  }
233 
234  private void killTaxes(Dao dao, Contract ctc) {
235  if(!Strings.isBlank(ctc.getNotes())) {
236  boolean done = false;
237  String taxs = Strings.extract(ctc.getNotes(), "IVA:([0-9\\,]+)");
238  if(!Strings.isBlank(taxs)) {
239  for(String tax : taxs.split("\\,")) {
240  double dtax = Double.valueOf(tax);
241  boolean exists = false;
242  for(Tax t : ctc.getTaxes()) {
243  if(t.getTax() == dtax) {
244  exists = true;
245  break;
246  }
247  }
248  if(!exists) {
249  Tax t = new Tax();
250  t.setTaxType(TaxType.VAT);
251  t.setTax(dtax);
252  t.setContract(ctc);
253  ctc.getTaxes().add(t);
254  }
255  }
256  ctc.setNotes(ctc.getNotes().replaceAll("IVA:([0-9\\,]+)", ""));
257  done = true;
258  }
259  String rets = Strings.extract(ctc.getNotes(), "IRPF:([0-9\\,]+)");
260  if(!Strings.isBlank(rets)) {
261  for(String tax : rets.split("\\,")) {
262  double dtax = Double.valueOf(tax);
263  boolean exists = false;
264  for(Tax t : ctc.getTaxes()) {
265  if(t.getTax() == dtax) {
266  exists = true;
267  break;
268  }
269  }
270  if(!exists) {
271  Tax t = new Tax();
272  t.setTaxType(TaxType.RETENTION);
273  t.setTax(dtax);
274  t.setContract(ctc);
275  ctc.getTaxes().add(t);
276  }
277  }
278  ctc.setNotes(ctc.getNotes().replaceAll("IRPF:([0-9\\,]+)", ""));
279  done = true;
280  }
281  if(done) {
282  dao.saveObject(ctc);
283  }
284  }
285  }
286 
287 }
static Messages confirmProcess()
Definition: Messages.java:95
static String get(String msg)
Definition: I_.java:41
static boolean existsTag(String tagName)
Definition: Tags.java:140
void setCollection(Collection< V > collection)