BrightSide Workbench Full Report + Source Code
RequiredUsage.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.Date;
21 import java.util.EnumSet;
22 import java.util.HashSet;
23 import java.util.Set;
24 import javax.persistence.*;
25 import org.turro.string.Strings;
26 import org.turro.erp.db.ErpPU;
27 import org.turro.erp.resource.ResourceAptitude;
28 import org.turro.erp.task.UsageValuation;
29 import org.turro.financials.db.FinancialsPU;
30 import org.turro.financials.entity.Contract;
31 import org.turro.financials.entity.LineType;
32 import org.turro.financials.entity.Product;
33 import org.turro.financials.entity.ProductByContractor;
34 import org.turro.financials.entity.RegisterView;
35 import org.turro.financials.model.document.LineTypeDescription;
36 import org.turro.financials.product.IProduct;
37 import org.turro.financials.product.ProductFactory;
38 import org.turro.hr.humanres.HumanResourceAptitude;
39 import org.turro.jpa.Dao;
40 import org.turro.jpa.entity.CachedEntity;
41 import org.turro.math.Zero;
42 import org.turro.util.PhraseBuilder;
43 
48 @Entity
49 public class RequiredUsage implements java.io.Serializable {
50 
51  @Id
52  @GeneratedValue(strategy=GenerationType.IDENTITY)
53  @Column(name="IDENTIFIER")
54  private long id;
55 
56  private double units, cost, price, dedication, purchaseLag;
57 
58  private long providerId, providerProductId, productId, lineTypeId, contractPreferenceId, viewId;
59 
60  private boolean providerFixed, costMaximum, canChange, representHours;
61 
62  private String productDescription;
63 
64  @Lob
65  @Column(length=4096)
66  private String description;
67 
68  @ManyToOne
69  private org.turro.erp.entity.Task task;
70 
71  @ManyToOne
72  private org.turro.erp.entity.HumanResource humanResource;
73 
74  @ManyToOne
75  private org.turro.erp.entity.Resource resource;
76 
77  @ManyToOne
78  private AptitudeDegree aptitudeDegree;
79 
80  @ManyToOne
81  private AptitudeDegree aptitudeAssesment;
82 
83  private PurchaseMode purchaseMode;
84 
85  @Temporal(value = TemporalType.DATE)
86  private Date purchaseDate;
87 
88  @ManyToOne
89  private org.turro.erp.entity.Task purchaseTask;
90 
91  @OneToMany(mappedBy = "requiredUsage", fetch = FetchType.LAZY)
92  private Set<OrderItem> orderItems = new HashSet<OrderItem>();
93 
94  public double getCost() {
95  return cost;
96  }
97 
98  public void setCost(double cost) {
99  this.cost = cost;
100  }
101 
102  public boolean isCostMaximum() {
103  return costMaximum;
104  }
105 
106  public void setCostMaximum(boolean costMaximum) {
107  this.costMaximum = costMaximum;
108  }
109 
110  public double getDedication() {
111  return dedication;
112  }
113 
114  public void setDedication(double dedication) {
115  this.dedication = dedication;
116  }
117 
118  public String getDescription() {
119  return description;
120  }
121 
122  public void setDescription(String description) {
123  this.description = description;
124  }
125 
126  public long getId() {
127  return id;
128  }
129 
130  public void setId(long id) {
131  this.id = id;
132  }
133 
135  return aptitudeDegree;
136  }
137 
138  public void setAptitudeDegree(AptitudeDegree aptitudeDegree) {
139  this.aptitudeDegree = aptitudeDegree;
140  }
141 
143  return aptitudeAssesment;
144  }
145 
146  public void setAptitudeAssesment(AptitudeDegree aptitudeAssesment) {
147  this.aptitudeAssesment = aptitudeAssesment;
148  }
149 
150  public boolean isCanChange() {
151  return canChange;
152  }
153 
154  public void setCanChange(boolean canChange) {
155  this.canChange = canChange;
156  }
157 
159  return humanResource;
160  }
161 
162  public void setHumanResource(HumanResource resource) {
163  this.humanResource = resource;
164  }
165 
166  public Set<OrderItem> getOrderItems() {
167  Dao dao = new ErpPU();
168  if(id > 0 && dao.isNotLoaded(orderItems)) {
169  orderItems = dao.lazyLoader(RequiredUsage.class, this, "orderItems").orderItems;
170  }
171  return orderItems;
172  }
173 
174  public Set<OrderItem> getUpdatedOrderItems() {
175  Dao dao = new ErpPU();
176  if(id > 0) {
177  orderItems = dao.lazyLoader(RequiredUsage.class, this, "orderItems").orderItems;
178  }
179  return orderItems;
180  }
181 
182  public void setOrderItems(Set<OrderItem> orderItems) {
183  this.orderItems = orderItems;
184  }
185 
186  public double getPrice() {
187  return price;
188  }
189 
190  public void setPrice(double price) {
191  this.price = price;
192  }
193 
194  public String getProductDescription() {
195  return productDescription;
196  }
197 
198  public void setProductDescription(String productDescription) {
199  this.productDescription = productDescription;
200  }
201 
202  public long getProductId() {
203  return productId;
204  }
205 
206  public void setProductId(long productId) {
207  this.productId = productId;
208  }
209 
210  public boolean isProviderFixed() {
211  return providerFixed;
212  }
213 
214  public void setProviderFixed(boolean providerFixed) {
215  this.providerFixed = providerFixed;
216  }
217 
218  public long getProviderId() {
219  return providerId;
220  }
221 
222  public void setProviderId(long providerId) {
223  this.providerId = providerId;
224  }
225 
226  public long getProviderProductId() {
227  return providerProductId;
228  }
229 
230  public void setProviderProductId(long providerProductId) {
231  this.providerProductId = providerProductId;
232  }
233 
234  public Date getPurchaseDate() {
235  return purchaseDate;
236  }
237 
238  public long getLineTypeId() {
239  return lineTypeId;
240  }
241 
242  public void setLineTypeId(long lineTypeId) {
243  this.lineTypeId = lineTypeId;
244  }
245 
246  public long getContractPreferenceId() {
247  return contractPreferenceId;
248  }
249 
250  public void setContractPreferenceId(long contractPreferenceId) {
251  this.contractPreferenceId = contractPreferenceId;
252  }
253 
254  public void setPurchaseDate(Date purchaseDate) {
255  this.purchaseDate = purchaseDate;
256  }
257 
258  public double getPurchaseLag() {
259  return purchaseLag;
260  }
261 
262  public void setPurchaseLag(double purchaseLag) {
263  this.purchaseLag = purchaseLag;
264  }
265 
267  return purchaseMode;
268  }
269 
270  public void setPurchaseMode(PurchaseMode purchaseMode) {
271  this.purchaseMode = purchaseMode;
272  }
273 
275  return purchaseTask;
276  }
277 
278  public void setPurchaseTask(Task purchaseTask) {
279  this.purchaseTask = purchaseTask;
280  }
281 
282  public boolean isRepresentHours() {
283  return representHours;
284  }
285 
286  public void setRepresentHours(boolean representHours) {
287  this.representHours = representHours;
288  }
289 
291  return resource;
292  }
293 
294  public void setResource(Resource resource) {
295  this.resource = resource;
296  }
297 
298  public Task getTask() {
299  return task;
300  }
301 
302  public void setTask(Task task) {
303  this.task = task;
304  }
305 
306  public double getUnits() {
307  return units;
308  }
309 
310  public void setUnits(double units) {
311  this.units = units;
312  }
313 
314  public long getViewId() {
315  return viewId;
316  }
317 
318  public void setViewId(long viewId) {
319  this.viewId = viewId;
320  }
321 
322  /* Financials Helpers */
323 
324  private transient CachedEntity<Contract, Long> provider =
325  new CachedEntity<Contract, Long>() {
326  @Override
327  protected Dao createDao() {
328  return new FinancialsPU();
329  }
330  };
331 
333  return provider.getEntity(providerId);
334  }
335 
336  public void setProvider(Contract provider) {
337  providerId = this.provider.setEntity(provider);
338  }
339 
340  private transient CachedEntity<Product, Long> product =
341  new CachedEntity<Product, Long>() {
342  @Override
343  protected Dao createDao() {
344  return new FinancialsPU();
345  }
346  };
347 
348  public Product getProduct() {
349  return product.getEntity(productId);
350  }
351 
352  public void setProduct(Product product) {
353  productId = this.product.setEntity(product);
354  }
355 
357  return ProductFactory.getProduct(productId, providerId, providerProductId, productDescription);
358  }
359 
360  public void setIProduct(IProduct product) {
361  if(product != null) {
362  if(product.isUncoded()) {
363  productId = 0;
364  productDescription = product.getProductCodeStr();
365  } else {
366  productId = ((Product) product).getId();
367  if(product.getProductByContractor() != null) {
369  }
370  }
371  } else {
372  productId = 0;
373  }
374  }
375 
376  private transient CachedEntity<ProductByContractor, Long> providerProduct =
377  new CachedEntity<ProductByContractor, Long>() {
378  @Override
379  protected Dao createDao() {
380  return new FinancialsPU();
381  }
382  };
383 
385  return providerProduct.getEntity(providerProductId);
386  }
387 
388  public void setProviderProduct(ProductByContractor providerProduct) {
389  providerProductId = this.providerProduct.setEntity(providerProduct);
390  }
391 
392  private transient CachedEntity<LineType, Long> lineType =
393  new CachedEntity<LineType, Long>() {
394  @Override
395  protected Dao createDao() {
396  return new FinancialsPU();
397  }
398  };
399 
401  LineType lt = lineType.getEntity(lineTypeId);
402  lt.setContractPreference(null);
403  return lt;
404  }
405 
406  public void setLineType(LineType lineType) {
407  lineTypeId = this.lineType.setEntity(lineType);
408  }
409 
410  private transient CachedEntity<RegisterView, Long> view =
411  new CachedEntity<RegisterView, Long>() {
412  @Override
413  protected Dao createDao() {
414  return new FinancialsPU();
415  }
416  };
417 
419  return view.getEntity(viewId);
420  }
421 
422  public void setView(RegisterView view) {
423  viewId = this.view.setEntity(view);
424  }
425 
426  /* Typers */
427 
428  protected transient int preparedType = 0;
429 
430  public boolean isProductType() {
431  return ((providerProductId > 0 || productId > 0) ||
432  !Strings.isEmpty(productDescription)) || preparedType == 2;
433  }
434 
435  public boolean isHumanResourceType() {
436  return (humanResource != null && aptitudeDegree != null) || preparedType == 1;
437  }
438 
439  public boolean hasResource() {
440  return isHumanResourceType() && resource != null;
441  }
442 
443  public void prepareHumanResource() {
444  providerProductId = 0;
445  productId = 0;
446  description = null;
447  dedication = 1;
448  preparedType = 1;
449  }
450 
451  public void prepareProduct() {
452  humanResource = null;
453  aptitudeDegree = null;
454  resource = null;
455  dedication = 1;
456  preparedType = 2;
457  }
458 
459  /* Helpers */
460 
461  public boolean isEmpty() {
462  clearEmptyLines();
463  preparedType = 0;
464  return (!isHumanResourceType() && !isProductType()) || units == 0.0d;
465  }
466 
467  public void clearEmptyLines() {
468  }
469 
470  public String getName() {
471  if(isHumanResourceType()) {
472  return getHumanResourceAptitude().getName() +
473  (hasResource() ?
474  " - " + getResource().getName() :
475  "");
476  } else if(isProductType()) {
477  PhraseBuilder pb = new PhraseBuilder();
478  IProduct p = getIProduct();
479  if(p != null) {
481  pb.addWord(p.getProductString());
482  }
483  if(!p.isUncoded()) {
484  pb.addWord(getDescription());
485  }
486  String ltype = new LineTypeDescription(lineTypeId, contractPreferenceId).getDescription();
487  if(ltype != null) {
488  pb.addWord("(" + ltype + ")");
489  }
490  return pb.toString();
491  }
492  return null;
493  }
494 
495  public String getType() {
496  if(isHumanResourceType()) {
497  return "operator";
498  } else if(isProductType()) {
499  return "product";
500  }
501  return null;
502  }
503 
505  return new HumanResourceAptitude(humanResource, canChange, aptitudeDegree);
506  }
507 
509  humanResource = hro.getHumanResource();
510  aptitudeDegree = hro.getAptitudeDegree();
511  }
512 
514  return new HumanResourceAptitude(humanResource, canChange,
515  aptitudeAssesment == null ? aptitudeDegree : aptitudeAssesment);
516  }
517 
519  return new ResourceAptitude(resource, aptitudeDegree);
520  }
521 
523  if(ro != null) {
524  resource = ro.getResource();
525  aptitudeDegree = ro.getAptitudeDegree();
526  }
527  }
528 
529  public boolean fits(HumanResource humanResource) {
530  return this.humanResource != null &&
531  ((this.humanResource.getId() == humanResource.getId()) ||
532  humanResource.fits(aptitudeDegree, new Date()));
533  }
534 
535  public boolean isInternal() {
536  return (humanResource != null && humanResource.isInternal()) ||
537  (resource != null && resource.isInternal());
538  }
539 
540  private transient double orderUnits = 0.0d;
541 
542  public double getOrderUnits() {
543  return orderUnits;
544  }
545 
546  public void setOrderUnits(double orderUnits) {
547  this.orderUnits = orderUnits;
548  }
549 
551  return startOrder(orderUnits == 0.0d ? getUnits() : orderUnits);
552  }
553 
554  public OrderItem startOrder(double orderUnits) {
555  OrderItem oi = new OrderItem();
556  oi.setSent(false);
557  oi.setDocumentDate(null);
558  oi.setDocumentNumber("");
559  oi.setRequiredUsage(this);
560  oi.setUnits(orderUnits);
561  oi.setDelivery(task.getDelivery());
562  oi.setDescription(description);
563  if(isHumanResourceType()) {
564  oi.setHumanResource(humanResource);
565  oi.setProviderId(humanResource.getIdContract());
566  oi.setDedication(dedication);
568  oi.setTax(0);
569  oi.setProviderFixed(true);
570  if(hasResource()) {
571  OrderItem oir = new OrderItem();
572  oir.setSent(false);
573  oir.setDocumentDate(null);
574  oir.setDocumentNumber("");
575  oir.setRequiredUsage(null);
576  oir.setResource(resource);
577  oir.setProviderId(resource.getIdContract());
578  oir.setUnits(orderUnits);
579  oir.setDelivery(task.getDelivery());
580  oir.setDedication(1.0d);
581  oir.setCost(resource.getCostHour());
582  oir.setTax(0);
583  oir.setProviderFixed(true);
584  oi.setRelated(oir);
585  }
586  } else if(isProductType()) {
587  oi.setProductId(productId);
588  oi.setProviderProductId(providerProductId);
589  oi.setProviderId(providerId);
590  oi.setProviderFixed(providerFixed);
591  oi.setProductDescription(productDescription);
592  oi.setDedication(1.0d);
593  oi.setCost(cost);
594  oi.setCostMaximum(costMaximum);
595  if(isCostMaximum()) {
596  oi.setMaxCost(cost);
597  }
598  oi.setTax(0);
599  }
600  return oi;
601  }
602 
603  /* Status helpers */
604 
606  double estimatedUnits = getEstimatedUnits(),
607  expectedUnits = getExpectedUnits(),
608  realUnits = getRealUnits();
609  if(realUnits == 0.0d) {
610  if(expectedUnits != 0.0d) {
611  return UsageStatus.USAGE_PENDING;
612  } else {
613  return UsageStatus.USAGE_READY;
614  }
615  } else {
616  if(realUnits >= estimatedUnits) {
617  if(EnumSet.of(TaskStatus.TASK_FINISHED, TaskStatus.TASK_ONWORK).contains(getTask().getStatus())) {
619  } else {
621  }
622  } else {
624  }
625  }
626  }
627 
628  public long getResourceId() {
629  if(isHumanResourceType()) {
630  return getHumanResource().getId();
631  } else if(isProductType()) {
632  return getProductId();
633  }
634  return 0;
635  }
636 
637  public double getEstimatedDuration() {
638  double q = 0.0;
639  if(isHumanResourceType()/* || representHours*/) {
640  q += getUnits();
641  }
642  return q;
643  }
644 
645  public double getExpectedDuration() {
646  double q = 0.0;
647  if(isHumanResourceType()) {
648  for(OrderItem oi : getOrderItems()) {
649  q += oi.getUnits();
650  }
651  }
652  return q;
653  }
654 
655  public double getRealDuration() {
656  double q = 0.0;
657  if(isHumanResourceType()) {
658  for(OrderItem oi : getOrderItems()) {
659  for(ReceiptItem ri : oi.getReceiptItems()) {
660  q += ri.getUnits();
661  }
662  }
663  }
664  return q;
665  }
666 
667  public double getExpectedDedication() {
668  double q = 0.0d, d = 0.0d;
669  if(isHumanResourceType()) {
670  for(OrderItem oi : getOrderItems()) {
671  q += oi.getUnits();
672  d += oi.getUnits() * oi.getDedication();
673  }
674  }
675  return q != 0.0d ? d / q : 1.0d;
676  }
677 
678  public double getRealDedication() {
679  double q = 0.0d, d = 0.0d;
680  if(isHumanResourceType()) {
681  for(OrderItem oi : getOrderItems()) {
682  for(ReceiptItem ri : oi.getReceiptItems()) {
683  q += ri.getUnits();
684  d += ri.getUnits() * ri.getDedication();
685  }
686  }
687  }
688  return q != 0.0d ? d / q : 1.0d;
689  }
690 
691  public double getEstimatedUnits() {
692  if(isHumanResourceType()) {
693  return (units * dedication) + (
694  hasResource() ? units : 0);
695  } else {
696  return units;
697  }
698  }
699 
700  public double getCalculatedUnitCost() {
701  return new UsageValuation(this).getUnitCost();
702  }
703 
704  public double getEstimatedUnitCost() {
705  if(Zero.near(cost, 2)) {
706  return getCalculatedUnitCost();
707  }
708  return cost;
709  }
710 
711  public double getEstimatedCost() {
713  }
714 
715  public double getExpectedUnits() {
716  double q = 0.0d;
717  for(OrderItem oi : getOrderItems()) {
718  q += oi.getUnits() * oi.getDedication();
719  if(oi.getRelated() != null) {
720  q += oi.getRelated().getUnits() * oi.getRelated().getDedication();
721  }
722  }
723  return q;
724  }
725 
726  public double getExpectedUnitCost() {
727  return getExpectedCost() / getExpectedUnits();
728  }
729 
730  public double getExpectedCost() {
731  double q = 0.0d;
732  for(OrderItem oi : getOrderItems()) {
733  q += oi.getAmount();
734  if(oi.getRelated() != null) {
735  q += oi.getRelated().getAmount();
736  }
737  }
738  return q;
739  }
740 
741  public double getRealUnits() {
742  double q = 0.0d;
743  for(OrderItem oi : getOrderItems()) {
744  q += oi.getRealUnits();
745  if(oi.getRelated() != null) {
746  q += oi.getRelated().getRealUnits();
747  }
748  }
749  return q;
750  }
751 
752  public double getRealUnitCost() {
753  double rc = getRealCost(),
754  ru = getRealUnits(),
755  result = rc / ru;
756  return Double.isNaN(result) ? 0.0d : result;
757  }
758 
759  public double getRealCost() {
760  double q = 0.0d;
761  for(OrderItem oi : getOrderItems()) {
762  q += oi.getRealAmount();
763  if(oi.getRelated() != null) {
764  q += oi.getRelated().getRealAmount();
765  }
766  }
767  return q;
768  }
769 
770  public double getEstimatedPrice() {
771  return getEstimatedUnits() * getUnitPrice();
772  }
773 
774  public double getProposedPrice() {
775  return getRealUnits() * getUnitPrice();
776  }
777 
778  public double getUnitPrice() {
779  if(Zero.near(price, 2)) {
780  return new UsageValuation(this).getUnitPrice();
781  }
782  return price;
783  }
784 
785  public double convertToReal(double convertedUnits) {
786  return (units * convertedUnits) / getEstimatedUnits();
787  }
788 
789  public double getMostRealCost() {
790  double mostRealCost = getRealCost();
791  if(mostRealCost == 0.0) {
792  mostRealCost = getExpectedCost();
793  }
794  if(mostRealCost == 0.0) {
795  mostRealCost = getEstimatedCost();
796  }
797  return mostRealCost;
798  }
799 
800 }
boolean fits(IContact contact)
void setDocumentDate(Date documentDate)
Definition: OrderItem.java:129
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
void setUnits(double units)
Definition: OrderItem.java:153
void setDedication(double dedication)
Definition: OrderItem.java:105
void setProductDescription(String productDescription)
Definition: OrderItem.java:185
void setSent(boolean sent)
Definition: OrderItem.java:269
void setProviderId(long providerId)
Definition: OrderItem.java:209
void setRequiredUsage(RequiredUsage requiredUsage)
Definition: OrderItem.java:253
void setProductId(long productId)
Definition: OrderItem.java:193
void setCostMaximum(boolean costMaximum)
Definition: OrderItem.java:97
void setProviderFixed(boolean providerFixed)
Definition: OrderItem.java:201
void setMaxCost(double maxCost)
Definition: OrderItem.java:177
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 setHumanResource(HumanResource resource)
void setResource(Resource resource)
void setCostMaximum(boolean costMaximum)
void setHumanResourceAptitude(HumanResourceAptitude hro)
void setProviderFixed(boolean providerFixed)
boolean fits(HumanResource humanResource)
void setOrderItems(Set< OrderItem > orderItems)
void setPurchaseDate(Date purchaseDate)
void setLineType(LineType lineType)
void setPurchaseMode(PurchaseMode purchaseMode)
void setView(RegisterView view)
void setAptitudeAssesment(AptitudeDegree aptitudeAssesment)
void setResourceAptitude(ResourceAptitude ro)
void setProductDescription(String productDescription)
ProductByContractor getProviderProduct()
void setProviderProductId(long providerProductId)
void setProviderId(long providerId)
HumanResourceAptitude getHumanResourceAptitude()
double convertToReal(double convertedUnits)
void setDedication(double dedication)
void setPurchaseLag(double purchaseLag)
void setDescription(String description)
HumanResourceAptitude getHumanResourceAptitudeAssest()
void setContractPreferenceId(long contractPreferenceId)
OrderItem startOrder(double orderUnits)
void setAptitudeDegree(AptitudeDegree aptitudeDegree)
void setOrderUnits(double orderUnits)
void setIProduct(IProduct product)
void setLineTypeId(long lineTypeId)
void setPurchaseTask(Task purchaseTask)
void setProviderProduct(ProductByContractor providerProduct)
void setRepresentHours(boolean representHours)
void setProvider(Contract provider)
void setCanChange(boolean canChange)
void setContractPreference(ContractPreference contractPreference)
Definition: LineType.java:81
boolean isNotLoaded(Object o, String attribute)
Definition: Dao.java:216
static boolean near(double value, int digits)
Definition: Zero.java:30
ProductByContractor getProductByContractor()
void setProductByContractor(ProductByContractor pbc)