BrightSide Workbench Full Report + Source Code
ContractExpiry.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.entity;
20 
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.HashSet;
24 import java.util.Set;
25 import javax.persistence.Column;
26 import javax.persistence.ElementCollection;
27 import javax.persistence.Entity;
28 import javax.persistence.FetchType;
29 import javax.persistence.GeneratedValue;
30 import javax.persistence.GenerationType;
31 import javax.persistence.Id;
32 import javax.persistence.ManyToOne;
33 import org.amic.util.date.CheckDate;
34 import org.turro.financials.model.contract.ContractUsualSet;
35 import org.turro.financials.model.contract.ContractWrapper;
36 import org.turro.financials.model.document.AmountTaxable;
37 import org.turro.financials.model.document.DocumentDefinitionWrapper;
38 import org.turro.jpa.entity.IDaoEntity;
39 import org.turro.scheduler.constraints.WeekDay;
40 
45 @Entity
46 public class ContractExpiry implements java.io.Serializable, IDaoEntity {
47 
48  @Id
49  @GeneratedValue(strategy=GenerationType.IDENTITY)
50  @Column(name="IDENTIFIER")
51  private long id;
52 
53  @ManyToOne
54  private ContractFlow contractFlow;
55 
56  @ManyToOne
57  private DocumentDefinition documentDefinition;
58 
59  @ManyToOne
60  private LineType lineType;
61 
62  private String concept;
63 
64  private int daysToAdd, dayOfWeek;
65 
66  private double percentValue, amountValue;
67 
68  @ElementCollection(fetch=FetchType.EAGER)
69  private Set<Integer> daysOfMonth = new HashSet<Integer>();
70 
71  @ElementCollection(fetch=FetchType.EAGER)
72  private Set<Integer> excludedMonths = new HashSet<Integer>();
73 
74  public long getId() {
75  return id;
76  }
77 
78  public void setId(long id) {
79  this.id = id;
80  }
81 
83  return contractFlow;
84  }
85 
86  public void setContractFlow(ContractFlow contractFlow) {
87  this.contractFlow = contractFlow;
88  }
89 
91  return documentDefinition;
92  }
93 
94  public void setDocumentDefinition(DocumentDefinition documentDefinition) {
95  this.documentDefinition = documentDefinition;
96  }
97 
98  public LineType getLineType() {
99  return lineType;
100  }
101 
102  public void setLineType(LineType lineType) {
103  this.lineType = lineType;
104  }
105 
106  public String getConcept() {
107  return concept;
108  }
109 
110  public void setConcept(String concept) {
111  this.concept = concept;
112  }
113 
114  public int getDaysToAdd() {
115  return daysToAdd;
116  }
117 
118  public void setDaysToAdd(int daysToAdd) {
119  this.daysToAdd = daysToAdd;
120  }
121 
122  public Set<Integer> getDaysOfMonth() {
123  return daysOfMonth;
124  }
125 
126  public void setDaysOfMonth(Set<Integer> daysOfMonth) {
127  this.daysOfMonth = daysOfMonth;
128  }
129 
130  public int getDayOfWeek() {
131  return dayOfWeek;
132  }
133 
134  public void setDayOfWeek(int dayOfWeek) {
135  this.dayOfWeek = dayOfWeek;
136  }
137 
138  public Set<Integer> getExcludedMonths() {
139  return excludedMonths;
140  }
141 
142  public void setExcludedMonths(Set<Integer> excludedMonths) {
143  this.excludedMonths = excludedMonths;
144  }
145 
146  public double getPercentValue() {
147  return percentValue;
148  }
149 
150  public void setPercentValue(double percentValue) {
151  this.percentValue = percentValue;
152  }
153 
154  public double getAmountValue() {
155  return amountValue;
156  }
157 
158  public void setAmountValue(double amountValue) {
159  this.amountValue = amountValue;
160  }
161 
162  /* IDaoEntity */
163 
164  @Override
165  public Object entityId() {
166  return id;
167  }
168 
169  @Override
170  public boolean isEmpty() {
171  return documentDefinition == null ||
172  (percentValue == 0.0d && amountValue == 0.0d);
173  }
174 
175  /* Helpers */
176 
177  public double generateFlow(Document document, double pendingAmount, int count) {
178  Document ndoc = new Document();
179  ndoc.setDocumentDate(newDate(document.getDocumentDate()));
180  ndoc.setContract(document.getContract());
181  ndoc.setCurrency(document.getCurrency());
182  ndoc.setDocumentNumber(document.getDocumentNumber() + "/" + count);
183  ndoc.setReceiptDate(ndoc.getDocumentDate());
184  ndoc.setDocumentDefinition(documentDefinition);
185  ndoc.setDraft(document.isDraft());
186  ndoc.setNotes(document.getNotes());
187  pendingAmount = appendAmount(document, ndoc, getStore(ndoc), pendingAmount);
189  dr.setAncestor(document);
190  dr.setDescendant(ndoc);
191  document.getDescendants().add(dr);
192  return pendingAmount;
193  }
194 
195  private Date newDate(Date date) {
196  CheckDate d = new CheckDate(date);
197  d.addDays(daysToAdd);
198  boolean correct = false;
199  while(!correct) {
200  while(isExcluded(d)) {
201  d.addDays(1);
202  }
203  if(!daysOfMonth.isEmpty()) {
204  boolean done = false;
205  do {
206  for(Integer i : daysOfMonth) {
207  done = i == d.getDay();
208  if(done) break;
209  }
210  if(!done) d.addDays(1);
211  } while(!done);
212  }
213  if(dayOfWeek > -1) {
214  while(d.getDayOfWeek() != WeekDay.map(dayOfWeek)) {
215  d.addDays(1);
216  }
217  }
218  correct = !isExcluded(d);
219  }
220  return d.getDate();
221  }
222 
223  private boolean isExcluded(CheckDate d) {
224  for(Integer i : excludedMonths) {
225  if(d.getMonth() == i) return true;
226  }
227  return false;
228  }
229 
230  public double appendAmount(Document source, Document destination, Contract store, double pendingAmount) {
231  int count = 0;
232  if(documentDefinition.hasColumn(DocumentColumn.DCOL_TAX)) {
233  for(AmountTaxable at : source.getAmounts().getTaxables()) {
234  DocumentLine dl = new DocumentLine();
235  dl.setDocument(destination);
236  if(concept != null) {
237  dl.setConcept(concept + " [" + at.getTax() + "]");
238  }
239  pendingAmount = assignAmount(dl, getAmount(at.getAmount()), pendingAmount);
240  dl.setTax(at.getTax());
241  dl.setLineType(lineType);
243  dl.setStore(store);
244  dl.setLineOrder(count++);
245  destination.getDocumentLines().add(dl);
246  }
247  } else {
248  DocumentLine dl = new DocumentLine();
249  dl.setDocument(destination);
250  if(concept != null) {
251  dl.setConcept(concept);
252  }
253  pendingAmount = assignAmount(dl, getAmount(source.getTotalAmount()), pendingAmount);
254  dl.setLineType(lineType);
256  dl.setStore(store);
257  dl.setLineOrder(count++);
258  destination.getDocumentLines().add(dl);
259  }
260  return pendingAmount;
261  }
262 
263  private double getAmount(double amount) {
264  if(amountValue != 0.0d) {
265  return amountValue;
266  }
267  if(percentValue != 0.0d) {
268  return amount * percentValue;
269  }
270  return 0;
271  }
272 
273  private Contract getStore(Document document) {
274  Collection<Contract> stores = new DocumentDefinitionWrapper(documentDefinition)
275  .getRelatedStores(contractFlow.getContract());
276  if(stores.isEmpty()) {
277  stores = new DocumentDefinitionWrapper(documentDefinition)
278  .getRelatedStores();
279  }
280  ContractWrapper.clearInactives(stores);
281  return stores.isEmpty() ? null : new ContractUsualSet(stores, true, document.getUsualPath() + "st:").iterator().next();
282  }
283 
284  private double assignAmount(DocumentLine dl, double amount, double pendingAmount) {
285  if(amount > pendingAmount) {
286  amount = pendingAmount;
287  }
288  dl.setPrice(amount);
289  pendingAmount -= amount;
290  return pendingAmount;
291  }
292 
293 }
void setContractFlow(ContractFlow contractFlow)
void setDocumentDefinition(DocumentDefinition documentDefinition)
void setDaysOfMonth(Set< Integer > daysOfMonth)
double generateFlow(Document document, double pendingAmount, int count)
double appendAmount(Document source, Document destination, Contract store, double pendingAmount)
void setExcludedMonths(Set< Integer > excludedMonths)
void setContractPreference(ContractPreference contractPreference)
void setDocumentNumber(String documentNumber)
Definition: Document.java:192
void setDocumentDate(Date documentDate)
Definition: Document.java:163
Set< DocumentRelation > getDescendants()
Definition: Document.java:139
void setContract(Contract contract)
Definition: Document.java:123
void setCurrency(Currency currency)
Definition: Document.java:135
ContractPreference getDefaultContractPreference()
Definition: Document.java:429
Set< DocumentLine > getDocumentLines()
Definition: Document.java:180
void setDocumentDefinition(DocumentDefinition documentDefinition)
Definition: Document.java:175
void setReceiptDate(Date receiptDate)
Definition: Document.java:256