BrightSide Workbench Full Report + Source Code
ReceiptItemAdapter.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.task;
20 
21 import java.util.Comparator;
22 import java.util.HashSet;
23 import org.turro.erp.entity.OrderItem;
24 import org.turro.erp.entity.OrderReference;
25 import org.turro.erp.entity.ReceiptItem;
26 import org.turro.erp.entity.RequiredUsage;
27 import org.turro.erp.entity.Task;
28 import org.turro.erp.entity.WorkOrder;
29 import org.turro.util.CompareUtil;
30 
35 public class ReceiptItemAdapter extends HashSet<ReceiptItem> implements Comparator<ReceiptItem> {
36 
37  public ReceiptItemAdapter(Task task) {
38  addReceipts(task);
39  }
40 
41  public ReceiptItemAdapter(WorkOrder workOrder) {
42  for(OrderReference or : workOrder.getOrderReferences()) {
43  for(Task task : or.getTasks()) {
44  addReceipts(task);
45  }
46  }
47  }
48 
49  private void addReceipts(Task task) {
50  for(RequiredUsage ru : task.getRequiredUsages()) {
51  double units = ru.getEstimatedUnits(),
52  expected = ru.getExpectedUnits();
53  for(OrderItem oi : ru.getOrderItems()) {
54  generateReceipts(oi);
55  }
56  if(units > expected) {
57  OrderItem oi = ru.startOrder(ru.convertToReal(units - expected));
58  ru.getOrderItems().add(oi);
59  generateReceipts(oi);
60  }
61 
62  }
63  }
64 
65  private void generateReceipts(OrderItem oi) {
66  double oexpected = oi.getExpectedUnits(),
67  oreal = oi.getRealUnits();
68  if(oexpected > oreal) {
69  ReceiptItem ri = oi.startReceipt(null, oi.convertToReal(oexpected - oreal));
70  oi.getReceiptItems().add(ri);
71  }
72  addAll(oi.getReceiptItems());
73  }
74 
75  @Override
76  public int compare(ReceiptItem o1, ReceiptItem o2) {
77  return CompareUtil.compare(getStringId(o1), getStringId(o2));
78  }
79 
80  private String getStringId(ReceiptItem ri) {
82  "/" +
84  "/" +
86  "/" +
87  ri.getOrderItem().getId();
88  }
89 
90 }
RequiredUsage getOwnerRequiredUsage()
Definition: OrderItem.java:374
Set< OrderReference > getOrderReferences()
Definition: WorkOrder.java:124
int compare(ReceiptItem o1, ReceiptItem o2)