BrightSide Workbench Full Report + Source Code
OrderReference.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.erp.entity;
19 
20 import java.util.*;
21 import javax.persistence.*;
22 import org.turro.string.Strings;
23 import org.hibernate.annotations.SortType;
24 import org.turro.elephant.impl.util.StringParser;
25 import org.turro.erp.db.ErpPU;
26 import org.turro.erp.workorder.ReferenceStatusWrapper;
27 import org.turro.financials.db.FinancialsPU;
28 import org.turro.financials.entity.Contract;
29 import org.turro.financials.entity.Product;
30 import org.turro.jpa.Dao;
31 import org.turro.jpa.entity.CachedEntity;
32 
37 @Entity
38 public class OrderReference implements java.io.Serializable {
39 
40  @Id
41  @GeneratedValue(strategy=GenerationType.IDENTITY)
42  @Column(name="IDENTIFIER")
43  private long id;
44 
45  @ManyToOne
46  private org.turro.erp.entity.WorkOrder workOrder;
47 
48  @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
49  private CustomerOrder customerOrder;
50 
51  @Lob
52  @Column(length=4096)
53  private String description;
54 
55  private long productId;
56 
57  private int orderRef;
58 
59  private boolean validated, noFraction, delivered;
60 
61  private double quantity, price, tax, retention;
62 
63  private long serviceId;
64 
65  @ManyToOne
66  private org.turro.erp.entity.Project project;
67 
68  @OneToMany(fetch = FetchType.EAGER, cascade= CascadeType.ALL, orphanRemoval=true)
69  private Set<Budget> budgets = new HashSet<Budget>();
70 
71  @OneToMany(fetch = FetchType.EAGER, cascade= CascadeType.ALL, orphanRemoval=true)
72  @OrderBy("orderRef")
73  private Set<Breakdown> breakdowns = new HashSet<Breakdown>();
74 
75  @OneToMany(mappedBy = "orderReference", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, orphanRemoval=true)
76  @org.hibernate.annotations.Sort(type=SortType.COMPARATOR, comparator=org.turro.erp.entity.TaskComparator.class)
77  private SortedSet<Task> tasks = new TreeSet<Task>(new org.turro.erp.entity.TaskComparator());
78 
79  public Set<Breakdown> getBreakdowns() {
80  return breakdowns;
81  }
82 
83  public void setBreakdowns(Set<Breakdown> breakdowns) {
84  this.breakdowns = breakdowns;
85  }
86 
87  public Set<Budget> getBudgets() {
88  return budgets;
89  }
90 
91  public void setBudgets(Set<Budget> budgets) {
92  this.budgets = budgets;
93  }
94 
96  return customerOrder;
97  }
98 
99  public void setCustomerOrder(CustomerOrder customerOrder) {
100  this.customerOrder = customerOrder;
101  if(customerOrder != null) {
102  customerOrder.setServiceId(serviceId);
103  customerOrder.setCustomerId(workOrder.getContractId());
104  }
105  }
106 
107  public boolean isDelivered() {
108  return delivered;
109  }
110 
111  public void setDelivered(boolean delivered) {
112  this.delivered = delivered;
113  }
114 
115  public String getDescription() {
116  return description;
117  }
118 
119  public void setDescription(String description) {
120  this.description = description;
121  }
122 
123  public long getId() {
124  return id;
125  }
126 
127  public void setId(long id) {
128  this.id = id;
129  }
130 
131  public boolean isNoFraction() {
132  return noFraction;
133  }
134 
135  public void setNoFraction(boolean noFraction) {
136  this.noFraction = noFraction;
137  }
138 
139  public int getOrderRef() {
140  return orderRef;
141  }
142 
143  public void setOrderRef(int orderRef) {
144  this.orderRef = orderRef;
145  }
146 
147  public double getPrice() {
148  return price;
149  }
150 
151  public void setPrice(double price) {
152  this.price = price;
153  }
154 
155  public long getProductId() {
156  return productId;
157  }
158 
159  public void setProductId(long productId) {
160  this.productId = productId;
161  }
162 
163  public double getQuantity() {
164  return quantity;
165  }
166 
167  public void setQuantity(double quantity) {
168  this.quantity = quantity;
169  }
170 
171  public double getRetention() {
172  return retention;
173  }
174 
175  public void setRetention(double retention) {
176  this.retention = retention;
177  }
178 
179  public long getServiceId() {
180  return serviceId;
181  }
182 
183  public void setServiceId(long serviceId) {
184  this.serviceId = serviceId;
185  }
186 
187  public SortedSet<Task> getTasks() {
188  return tasks;
189  }
190 
191  public void setTasks(SortedSet<Task> tasks) {
192  this.tasks = tasks;
193  }
194 
195  public double getTax() {
196  return tax;
197  }
198 
199  public void setTax(double tax) {
200  this.tax = tax;
201  }
202 
203  public boolean isValidated() {
204  return validated;
205  }
206 
207  public void setValidated(boolean validated) {
208  this.validated = validated;
209  }
210 
212  return workOrder;
213  }
214 
215  public void setWorkOrder(WorkOrder workOrder) {
216  this.workOrder = workOrder;
217  }
218 
219  /* Financials Helpers */
220 
221  private transient CachedEntity<Product, Long> product =
222  new CachedEntity<Product, Long>() {
223  @Override
224  protected Dao createDao() {
225  return new FinancialsPU();
226  }
227  };
228 
229  public Product getProduct() {
230  return product.getEntity(productId);
231  }
232 
233  public void setProduct(Product product) {
234  productId = this.product.setEntity(product);
235  }
236 
237  private transient CachedEntity<Contract, Long> service =
238  new CachedEntity<Contract, Long>() {
239  @Override
240  protected Dao createDao() {
241  return new FinancialsPU();
242  }
243  };
244 
245  public Contract getService() {
246  return service.getEntity(serviceId);
247  }
248 
249  public void setService(Contract service) {
250  serviceId = this.service.setEntity(service);
251  }
252 
253  /* Description Helper */
254 
255  public String getOrderDescription() {
256  return orderRef + " - " + getSomeDescription();
257  }
258 
259  public String getSomeDescription() {
260  return (productId > 0 ? getProduct().getProductString() : "") +
261  description;
262  }
263 
264  public String getFullDescription() {
265  return getSomeDescription() + " (" + workOrder.getFullDescription() + ") ";
266  }
267 
268  public String getHtmlDescription() {
269  return StringParser.toHTML(description);
270  }
271 
272  /* Helpers */
273 
274  public boolean isEmpty() {
275  boolean empty = (Strings.isBlank(description) && productId == 0) ||
276  serviceId == 0;
277  return empty;
278  }
279 
280  public void clearEmptyLines() {
281  Iterator<Task> it = tasks.iterator();
282  while(it.hasNext()) {
283  Task t = it.next();
284  if(t.isEmpty()) {
285  t.setOrderReference(null);
286  it.remove();
287  }
288  }
289  Iterator<Breakdown> itb = breakdowns.iterator();
290  while(itb.hasNext()) {
291  Breakdown b = itb.next();
292  if(b.isEmpty()) {
293  itb.remove();
294  }
295  }
296  }
297 
298  public void prepareForSaving() {
299  if(workOrder.getProductId() > 0) {
300  productId = workOrder.getProductId();
301  }
302  clearEmptyLines();
303  for(Task t : tasks) {
304  t.prepareForSaving();
305  }
306  }
307 
308  public Set<Task> getUpdatedTasks() {
309  if(id > 0) {
310  Dao dao = new ErpPU();
311  tasks = dao.lazyLoader(OrderReference.class, this, "tasks").tasks;
312  }
313  return tasks;
314  }
315 
316  public void initFrom(CustomerOrder cu) {
317  this.customerOrder = cu;
318  description = cu.getDescription();
319  noFraction = cu.isNoFraction();
320  quantity = cu.getQuantity();
321  price = cu.getPrice();
322  serviceId = cu.getServiceId();
323  }
324 
326  return getStatusWrapper(null);
327  }
328 
330  return new ReferenceStatusWrapper(this, now);
331  }
332 
333  private boolean hasDeliveredBudget() {
334  if(budgets.isEmpty()) return false;
335  for(Budget b : budgets) {
336  if(b.isDelivered()) {
337  return true;
338  }
339  }
340  return false;
341  }
342 
343  public boolean hasAcceptedBudget() {
344  if(budgets.isEmpty()) return false;
345  for(Budget b : budgets) {
346  if(b.isAccepted()) {
347  return true;
348  }
349  }
350  return false;
351  }
352 
353  public boolean requiresBudget() {
354  return customerOrder != null && customerOrder.isWantBudget();
355  }
356 
357  public boolean hasPendingBudget() {
358  if(!hasAcceptedBudget() && (
359  !budgets.isEmpty() || requiresBudget())) {
360  return true;
361  }
362  return false;
363  }
364 
365  public boolean hasBugdetStopper() {
366  return hasAcceptedBudget() || hasDeliveredBudget();
367  }
368 
369  public double getCost() {
370  double cost = 0;
371  for(Task t : getTasks()) {
372  cost += t.getCost();
373  }
374  return cost;
375  }
376 
377 }
static String toHTML(String value)
void setCustomerId(long customerId)
ReferenceStatusWrapper getStatusWrapper(Date now)
void setValidated(boolean validated)
void setNoFraction(boolean noFraction)
void setWorkOrder(WorkOrder workOrder)
ReferenceStatusWrapper getStatusWrapper()
void setBreakdowns(Set< Breakdown > breakdowns)
void setDescription(String description)
void setTasks(SortedSet< Task > tasks)
void setDelivered(boolean delivered)
void setBudgets(Set< Budget > budgets)
void setCustomerOrder(CustomerOrder customerOrder)