BrightSide Workbench Full Report + Source Code
WorkOrderValues.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.workorder.valuation;
20 
21 import java.util.Arrays;
22 import org.turro.erp.entity.OrderReference;
23 import org.turro.erp.entity.RequiredUsage;
24 import org.turro.erp.entity.Task;
25 import org.turro.erp.entity.WorkOrder;
26 import org.turro.financials.entity.Company;
27 import org.turro.financials.entity.Department;
28 import org.turro.financials.entity.Headquarters;
29 import org.turro.financials.entity.Service;
30 import org.turro.financials.model.business.CompanyWrapper;
31 import org.turro.math.Round;
32 import org.turro.valuation.*;
33 
38 public class WorkOrderValues extends ValuesGrid {
39 
40  private WorkOrder workOrder;
41 
42  public void setWorkOrder(WorkOrder workOrder) {
43  this.workOrder = workOrder;
44  createModel();
45  model.setMarginModes(Arrays.asList(MarginOverCostMode.values()));
46  model.setPriceModes(Arrays.asList(PropossedPriceMode.values()));
47  addColumns();
48  addRows();
49  }
50 
51  public void reload() {
52  getChildren().clear();
53  createModel();
54  addColumns();
55  addRows();
56  }
57 
58  public void setPrices() {
59  for(Value value : model.getRootValues()) {
60  if(value.getData() instanceof NodeData) {
61  OrderReference or = (OrderReference) ((NodeData) value.getData()).object;
62  or.setPrice(new Round(value.getFinalPrice()).decimals(2).value());
63  }
64  }
65  }
66 
67  private void createModel() {
68  if(model != null) {
69  model = new ValuationModel(model.getMarginModes(), model.getPriceModes());
70  } else {
71  model = new ValuationModel();
72  }
73  for(OrderReference or : workOrder.getOrderReferences()) {
74  if(or.isEmpty()) continue;
75  Value vor = new Value(or.getId() + "", "reference", or.getSomeDescription());
76  vor.setData(new NodeData(false, or));
77  vor.setOrder(or.getOrderRef());
78  vor.setExpressedUnits(or.getQuantity());
79  if(or.getCustomerOrder() != null && or.getCustomerOrder().isWantBudget()) {
80  vor.setOfferedPrice(or.getCustomerOrder().getAmount());
81  }
82  model.addValue(vor);
83  for(Task t : or.getTasks()) {
84  Value vt = new Value(t.getId() + "", "task", t.getName());
85  vt.setData(new NodeData(false, t));
86  vor.addChildren(vt);
87  for(RequiredUsage ru : t.getRequiredUsages()) {
88  Usage usage = null;
89  if(ru.hasResource()) {
90  usage = new Usage(ru.getResource().getId() + "", "resource",
91  ru.getResource().getName());
92  }
93  Resource rru = new Resource(ru.getResourceId() + "",
94  ru.getType(),
95  ru.isHumanResourceType() ? ru.getHumanResourceAptitude().getName() : ru.getName(),
96  usage);
97  rru.setData(new NodeData(false, ru));
98  rru.setEstimatedCostUnit(ru.getEstimatedUnitCost());
99  rru.setExpectedCostUnit(ru.getExpectedUnitCost());
100  rru.setEstimatedUnits(ru.getEstimatedUnits());
101  rru.setExpectedUnits(ru.getExpectedUnits());
102  rru.setPriceUnit(ru.getUnitPrice());
103  rru.setRealCostUnit(ru.getRealUnitCost());
104  rru.setRealUnits(ru.getRealUnits());
105  vt.addResource(rru);
106  addVariables(rru, t);
107  }
108  }
109  }
110  }
111 
112  private void addVariables(Resource resource, Task task) {
113  if(task.getDepartment() != null) {
114  Department department = task.getDepartment().getDepartment();
115  resource.addVariableRef(department.getId() + "", "b_department", department.getFullName(),
116  department.getStructureMargin(), department.getProfitMargin(), department.isAlwaysApply(), 3);
117  Headquarters headquarters = department.getHeadquarters();
118  resource.addVariableRef(headquarters.getId() + "", "b_headquarters", headquarters.getName(),
119  headquarters.getStructureMargin(), headquarters.getProfitMargin(), headquarters.isAlwaysApply(), 2);
120  Company company = headquarters.getCompany();
121  resource.addVariableRef(company.getId() + "", "b_company", company.getName(),
122  company.getStructureMargin(), company.getProfitMargin(), company.isAlwaysApply(), 0);
123  }
124  if(task.getOrderReference().getService() != null) {
125  Service service = task.getOrderReference().getService().getService();
126  resource.addVariableRef(service.getId() + "", "b_service", service.getFullName(),
127  service.getStructureMargin(), service.getProfitMargin(), service.isAlwaysApply(), 1);
128  }
129  addAlwaysApplyVariables(resource);
130  }
131 
132  private void addAlwaysApplyVariables(Resource resource) {
133  Company c = CompanyWrapper.getDefaultCompany();
134  if(c != null) {
135  if(c.isAlwaysApply()) {
136  resource.addVariableRef(c.getId() + "", "b_company", c.getName(),
137  c.getStructureMargin(), c.getProfitMargin(), c.isAlwaysApply(), 0);
138  }
139  for(Service s : c.getServices()) {
140  if(s.isAlwaysApply()) {
141  resource.addVariableRef(s.getId() + "", "b_service", s.getFullName(),
142  s.getStructureMargin(), s.getProfitMargin(), s.isAlwaysApply(), 1);
143  }
144  }
145  for(Headquarters h : c.getHeadquarters()) {
146  if(h.isAlwaysApply()) {
147  resource.addVariableRef(h.getId() + "", "b_headquarters", h.getName(),
148  h.getStructureMargin(), h.getProfitMargin(), h.isAlwaysApply(), 2);
149  }
150  for(Department d : h.getDepartments()) {
151  if(d.isAlwaysApply()) {
152  resource.addVariableRef(d.getId() + "", "b_department", d.getFullName(),
153  d.getStructureMargin(), d.getProfitMargin(), d.isAlwaysApply(), 3);
154  }
155  }
156  }
157  }
158  }
159 
160 }
Set< OrderReference > getOrderReferences()
Definition: WorkOrder.java:124
Round decimals(int digits)
Definition: Round.java:32