BrightSide Workbench Full Report + Source Code
OrderItem.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.erp.entity;
20 
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.Set;
24 import javax.persistence.*;
25 import org.turro.erp.db.ErpPU;
26 import org.turro.financials.db.FinancialsPU;
27 import org.turro.financials.entity.Contract;
28 import org.turro.financials.entity.DocumentLine;
29 import org.turro.financials.entity.Product;
30 import org.turro.financials.entity.ProductByContractor;
31 import org.turro.financials.product.CachedIProduct;
32 import org.turro.financials.product.IProduct;
33 import org.turro.financials.product.IProductKey;
34 import org.turro.jpa.Dao;
35 import org.turro.jpa.entity.CachedEntity;
36 
41 @Entity
42 public class OrderItem implements java.io.Serializable {
43 
44  @Id
45  @GeneratedValue(strategy=GenerationType.IDENTITY)
46  @Column(name="IDENTIFIER")
47  private long id;
48 
49  @Lob
50  @Column(length=4096)
51  private String description;
52 
53  private long providerId, providerProductId, productId, documentLineId;
54 
55  @ManyToOne
56  private HumanResource humanResource;
57 
58  @ManyToOne
59  private Resource resource;
60 
61  @Temporal(value = TemporalType.DATE)
62  private Date documentDate;
63 
64  private String documentNumber;
65 
66  private double units, dedication, cost, tax, maxCost;
67 
68  private boolean costMaximum, providerFixed, sent;
69 
70  private String productDescription;
71 
72  @Column(name="DELIVERY_DATE")
73  @Temporal(value = TemporalType.TIMESTAMP)
74  private Date delivery;
75 
76  @ManyToOne
77  private RequiredUsage requiredUsage;
78 
79  @OneToOne(cascade= CascadeType.ALL)
80  private OrderItem related;
81 
82  @OneToMany(mappedBy = "orderItem", fetch = FetchType.LAZY)
83  private Set<ReceiptItem> receiptItems = new HashSet<ReceiptItem>();
84 
85  public double getCost() {
86  return cost;
87  }
88 
89  public void setCost(double cost) {
90  this.cost = cost;
91  }
92 
93  public boolean isCostMaximum() {
94  return costMaximum;
95  }
96 
97  public void setCostMaximum(boolean costMaximum) {
98  this.costMaximum = costMaximum;
99  }
100 
101  public double getDedication() {
102  return dedication;
103  }
104 
105  public void setDedication(double dedication) {
106  this.dedication = dedication;
107  }
108 
109  public Date getDelivery() {
110  return delivery;
111  }
112 
113  public void setDelivery(Date delivery) {
114  this.delivery = delivery;
115  }
116 
117  public String getDescription() {
118  return description;
119  }
120 
121  public void setDescription(String description) {
122  this.description = description;
123  }
124 
125  public Date getDocumentDate() {
126  return documentDate;
127  }
128 
129  public void setDocumentDate(Date documentDate) {
130  this.documentDate = documentDate;
131  }
132 
133  public long getDocumentLineId() {
134  return documentLineId;
135  }
136 
137  public void setDocumentLineId(long documentLineId) {
138  this.documentLineId = documentLineId;
139  }
140 
141  public String getDocumentNumber() {
142  return documentNumber;
143  }
144 
145  public void setDocumentNumber(String documentNumber) {
146  this.documentNumber = documentNumber;
147  }
148 
149  public double getUnits() {
150  return units;
151  }
152 
153  public void setUnits(double units) {
154  this.units = units;
155  }
156 
158  return humanResource;
159  }
160 
161  public void setHumanResource(HumanResource humanResource) {
162  this.humanResource = humanResource;
163  }
164 
165  public long getId() {
166  return id;
167  }
168 
169  public void setId(long id) {
170  this.id = id;
171  }
172 
173  public double getMaxCost() {
174  return maxCost;
175  }
176 
177  public void setMaxCost(double maxCost) {
178  this.maxCost = maxCost;
179  }
180 
181  public String getProductDescription() {
182  return productDescription;
183  }
184 
185  public void setProductDescription(String productDescription) {
186  this.productDescription = productDescription;
187  }
188 
189  public long getProductId() {
190  return productId;
191  }
192 
193  public void setProductId(long productId) {
194  this.productId = productId;
195  }
196 
197  public boolean isProviderFixed() {
198  return providerFixed;
199  }
200 
201  public void setProviderFixed(boolean providerFixed) {
202  this.providerFixed = providerFixed;
203  }
204 
205  public long getProviderId() {
206  return providerId;
207  }
208 
209  public void setProviderId(long providerId) {
210  this.providerId = providerId;
211  }
212 
213  public long getProviderProductId() {
214  return providerProductId;
215  }
216 
217  public void setProviderProductId(long providerProductId) {
218  this.providerProductId = providerProductId;
219  }
220 
221  public Set<ReceiptItem> getReceiptItems() {
222  Dao dao = new ErpPU();
223  if(id > 0 && dao.isNotLoaded(receiptItems)) {
224  receiptItems = dao.lazyLoader(OrderItem.class, this, "receiptItems").receiptItems;
225  }
226  return receiptItems;
227  }
228 
229  public Set<ReceiptItem> getUpdatedReceiptItems() {
230  Dao dao = new ErpPU();
231  if(id > 0) {
232  receiptItems = dao.lazyLoader(OrderItem.class, this, "receiptItems").receiptItems;
233  }
234  return receiptItems;
235  }
236 
237  public void setReceiptItems(Set<ReceiptItem> receiptItems) {
238  this.receiptItems = receiptItems;
239  }
240 
242  return related;
243  }
244 
245  public void setRelated(OrderItem related) {
246  this.related = related;
247  }
248 
250  return requiredUsage;
251  }
252 
253  public void setRequiredUsage(RequiredUsage requiredUsage) {
254  this.requiredUsage = requiredUsage;
255  }
256 
258  return resource;
259  }
260 
261  public void setResource(Resource resource) {
262  this.resource = resource;
263  }
264 
265  public boolean isSent() {
266  return sent;
267  }
268 
269  public void setSent(boolean sent) {
270  this.sent = sent;
271  }
272 
273  public double getTax() {
274  return tax;
275  }
276 
277  public void setTax(double tax) {
278  this.tax = tax;
279  }
280 
281  /* Financials Helpers */
282 
283  private transient CachedEntity<DocumentLine, Long> documentLine =
284  new CachedEntity<DocumentLine, Long>() {
285  @Override
286  protected Dao createDao() {
287  return new FinancialsPU();
288  }
289  };
290 
292  return documentLine.getEntity(documentLineId);
293  }
294 
295  public void setDocumentLine(DocumentLine documentLine) {
296  documentLineId = this.documentLine.setEntity(documentLine);
297  }
298 
299  private transient CachedEntity<Product, Long> product =
300  new CachedEntity<Product, Long>() {
301  @Override
302  protected Dao createDao() {
303  return new FinancialsPU();
304  }
305  };
306 
307  public Product getProduct() {
308  return product.getEntity(productId);
309  }
310 
311  public void setProduct(Product product) {
312  productId = this.product.setEntity(product);
313  }
314 
315  private transient CachedIProduct iproduct = new CachedIProduct();
316 
318  return iproduct.getIProduct(new IProductKey(productId, providerId, providerProductId, productDescription));
319  }
320 
321  public void setIProduct(IProduct product) {
322  IProductKey productKey = iproduct.setIProduct(product);
323  productId = productKey.getProductId();
324  providerId = productKey.getProviderId();
325  providerProductId = productKey.getProviderProductId();
326  productDescription = productKey.getProductDescription();
327  }
328 
329  private transient CachedEntity<Contract, Long> provider =
330  new CachedEntity<Contract, Long>() {
331  @Override
332  protected Dao createDao() {
333  return new FinancialsPU();
334  }
335  };
336 
338  return provider.getEntity(providerId);
339  }
340 
341  public void setProvider(Contract provider) {
342  providerId = this.provider.setEntity(provider);
343  }
344 
345  private transient CachedEntity<ProductByContractor, Long> providerProduct =
346  new CachedEntity<ProductByContractor, Long>() {
347  @Override
348  protected Dao createDao() {
349  return new FinancialsPU();
350  }
351  };
352 
354  return providerProduct.getEntity(providerProductId);
355  }
356 
357  public void setProviderProduct(ProductByContractor providerProduct) {
358  providerProductId = this.providerProduct.setEntity(providerProduct);
359  }
360 
361  /* Helpers */
362 
363  public boolean isEmpty() {
364  return units == 0;
365  }
366 
367  public OrderItem getOwner() {
368  return (OrderItem) new ErpPU().getSingleResultOrNull(
369  "select oi from OrderItem oi " +
370  "where related = ?",
371  new Object[] { this });
372  }
373 
375  if(requiredUsage == null) {
376  OrderItem oi = getOwner();
377  if(oi != null) {
378  return oi.getRequiredUsage();
379  }
380  }
381  return requiredUsage;
382  }
383 
384  public String getName() {
385  if(humanResource != null) {
386  return humanResource.getName();
387  } else if(resource != null) {
388  return resource.getName();
389  } else {
390  IProduct product = getIProduct();
391  return product != null ? product.getProductName() : null;
392  }
393  }
394 
395  public String getType() {
396  if(humanResource != null) {
397  return "operator";
398  } else if(resource != null) {
399  return "resource";
400  } else {
401  return "product";
402  }
403  }
404 
405  public double getAmount() {
406  return cost * units * dedication;
407  }
408 
409  public ReceiptItem startReceipt(Date startWorking) {
410  return startReceipt(startWorking, units);
411  }
412 
413  public ReceiptItem startReceipt(Date startWorking, double receiptUnits) {
414  ReceiptItem ri = new ReceiptItem();
415  ri.setOrderItem(this);
416  ri.setDocumentDate(documentDate);
417  ri.setDocumentNumber(documentNumber);
418  ri.setHumanResource(humanResource);
419  ri.setResource(resource);
420  ri.setProductId(productId);
421  ri.setProviderId(providerId);
422  ri.setProviderProductId(providerProductId);
423  ri.setProductDescription(productDescription);
424  ri.setDescription(description);
425  ri.setUnits(receiptUnits);
426  ri.setDedication(dedication);
427  ri.setStartWorking(startWorking);
428  ri.setCost(cost);
429  ri.setTax(tax);
430  return ri;
431  }
432 
433  /* Status helpers */
434 
436  Set<ReceiptItem> lri = getReceiptItems();
437  if(lri.isEmpty()) {
438  if(isSent()) {
439  return OrderStatus.ORDER_PENDING;
440  } else {
441  return OrderStatus.ORDER_READY;
442  }
443  }
444  double tmp = 0.0;
445  boolean processed = false;
446  for(ReceiptItem ri : lri) {
447  tmp += ri.getUnits();
448  processed = true;
449  }
450  if(processed) {
451  if(tmp >= units) {
453  } else {
455  }
456  } else {
457  return OrderStatus.ORDER_READY;
458  }
459  }
460 
461  public double getRealDedication() {
462  double q = 0.0d, d = 0.0d;
463  if(humanResource != null) {
464  for(ReceiptItem ri : getReceiptItems()) {
465  q += ri.getUnits();
466  d += ri.getUnits() * ri.getDedication();
467  }
468  }
469  return q != 0.0d ? d / q : 1.0d;
470  }
471 
472  public double getExpectedUnits() {
473  return units * dedication;
474  }
475 
476  public double getRealUnits() {
477  double q = 0.0d;
478  for(ReceiptItem ri : getReceiptItems()) {
479  q += ri.getUnits() * ri.getDedication();
480  }
481  return q;
482  }
483 
484  public double getRealAmount() {
485  double q = 0.0d;
486  for(ReceiptItem ri : getReceiptItems()) {
487  q += ri.getAmount();
488  }
489  return q;
490  }
491 
492  public double convertToReal(double d) {
493  return d / dedication;
494  }
495 
496  public boolean hasReceipts() {
497  boolean empty = getReceiptItems().isEmpty();
498  if(empty && related != null) {
499  empty = related.getReceiptItems().isEmpty();
500  }
501  return !empty;
502  }
503 
504 }
void setDocumentLine(DocumentLine documentLine)
Definition: OrderItem.java:295
void setProvider(Contract provider)
Definition: OrderItem.java:341
HumanResource getHumanResource()
Definition: OrderItem.java:157
void setDocumentDate(Date documentDate)
Definition: OrderItem.java:129
ReceiptItem startReceipt(Date startWorking)
Definition: OrderItem.java:409
void setReceiptItems(Set< ReceiptItem > receiptItems)
Definition: OrderItem.java:237
RequiredUsage getOwnerRequiredUsage()
Definition: OrderItem.java:374
Set< ReceiptItem > getReceiptItems()
Definition: OrderItem.java:221
void setDescription(String description)
Definition: OrderItem.java:121
void setCost(double cost)
Definition: OrderItem.java:89
void setDocumentNumber(String documentNumber)
Definition: OrderItem.java:145
void setDelivery(Date delivery)
Definition: OrderItem.java:113
void setProviderProductId(long providerProductId)
Definition: OrderItem.java:217
double convertToReal(double d)
Definition: OrderItem.java:492
void setUnits(double units)
Definition: OrderItem.java:153
void setIProduct(IProduct product)
Definition: OrderItem.java:321
RequiredUsage getRequiredUsage()
Definition: OrderItem.java:249
void setDedication(double dedication)
Definition: OrderItem.java:105
void setProductDescription(String productDescription)
Definition: OrderItem.java:185
void setSent(boolean sent)
Definition: OrderItem.java:269
Set< ReceiptItem > getUpdatedReceiptItems()
Definition: OrderItem.java:229
void setProviderId(long providerId)
Definition: OrderItem.java:209
void setRequiredUsage(RequiredUsage requiredUsage)
Definition: OrderItem.java:253
void setProductId(long productId)
Definition: OrderItem.java:193
ReceiptItem startReceipt(Date startWorking, double receiptUnits)
Definition: OrderItem.java:413
void setCostMaximum(boolean costMaximum)
Definition: OrderItem.java:97
ProductByContractor getProviderProduct()
Definition: OrderItem.java:353
void setProviderFixed(boolean providerFixed)
Definition: OrderItem.java:201
void setProviderProduct(ProductByContractor providerProduct)
Definition: OrderItem.java:357
void setMaxCost(double maxCost)
Definition: OrderItem.java:177
void setProduct(Product product)
Definition: OrderItem.java:311
void setDocumentLineId(long documentLineId)
Definition: OrderItem.java:137
void setHumanResource(HumanResource humanResource)
Definition: OrderItem.java:161
void setRelated(OrderItem related)
Definition: OrderItem.java:245
void setResource(Resource resource)
Definition: OrderItem.java:261
void setProductId(long productId)
void setDedication(double dedication)
void setProductDescription(String productDescription)
void setResource(Resource resource)
void setHumanResource(HumanResource humanResource)
void setDescription(String description)
void setOrderItem(OrderItem orderItem)
void setProviderId(long providerId)
void setDocumentNumber(String documentNumber)
void setProviderProductId(long providerProductId)
void setStartWorking(Date startWorking)
void setDocumentDate(Date documentDate)
IProductKey setIProduct(IProduct product)
IProduct getIProduct(IProductKey productKey)
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
boolean isNotLoaded(Object o, String attribute)
Definition: Dao.java:216