BrightSide Workbench Full Report + Source Code
HoursEdit.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.purchase;
20 
21 import java.util.Date;
22 import org.turro.string.Strings;
23 import org.turro.erp.db.ErpPU;
24 import org.turro.erp.entity.*;
25 import org.turro.erp.humanres.HumanResourceCombobox;
26 import org.turro.jpa.Dao;
27 import org.turro.zkoss.grid.EditableCell;
28 import org.zkoss.zk.ui.Component;
29 import org.zkoss.zk.ui.util.GenericForwardComposer;
30 import org.zkoss.zul.Button;
31 import org.zkoss.zul.Datebox;
32 import org.zkoss.zul.Label;
33 import org.zkoss.zul.Textbox;
34 
39 public class HoursEdit extends GenericForwardComposer {
40 
41  private HoursGrid hoursGrid, hoursHistGrid;
42  private HumanResourceCombobox humanres;
43  private Textbox docnumber;
44  private Datebox docdate;
45  private Button doccreate;
46  private Label lWorkOrder, lOrderReference;
47 
48  public void onClick$doccreate() {
49  for(HoursItem hi : hoursGrid.getValues()) {
50  if(hi.isValid()) {
51  if(hoursGrid.isReceipt()) {
52  createReceipt(humanres.getObjectValue(), docnumber.getValue(), docdate.getValue(), hi);
53  } else {
54  createOrder(humanres.getObjectValue(), docnumber.getValue(), docdate.getValue(), hi);
55  }
56  }
57  }
58  hoursGrid.clearValidRows();
59  if(hoursHistGrid != null) {
60  hoursHistGrid.refreshUsing(humanres.getObjectValue(), docnumber.getValue(), docdate.getValue());
61  }
62  }
63 
64  public void onChange$docdate() {
65  hoursGrid.setDocumentDate(docdate.getValue());
66  if(hoursHistGrid != null) {
67  hoursHistGrid.refreshUsing(humanres.getObjectValue(), docnumber.getValue(), docdate.getValue());
68  }
69  updateButton();
70  }
71 
72  public void onChange$docnumber() {
73  if(hoursHistGrid != null) {
74  hoursHistGrid.refreshUsing(humanres.getObjectValue(), docnumber.getValue(), docdate.getValue());
75  }
76  updateButton();
77  }
78 
79  public void onChange$humanres() {
80  if(hoursHistGrid != null) {
81  hoursHistGrid.refreshUsing(humanres.getObjectValue(), docnumber.getValue(), docdate.getValue());
82  }
83  updateButton();
84  }
85 
86  public void onChange$hoursGrid() {
87  updateButton();
88  }
89 
90  public void onChanging$hoursGrid() {
91  EditableCell ec = hoursGrid.getCurrentCell();
92  if(ec != null) {
93  HoursItem pi = (HoursItem) ec.getRow().getValue();
94  if(pi != null) {
95  lWorkOrder.setValue(pi.getWorkOrder() == null ? null :
96  "#" + pi.getWorkOrder().getWorkOrderId() + "\n" + pi.getWorkOrder().getSomeDescription());
97  lOrderReference.setValue(pi.getOrderReference() == null ? null :
99  }
100  }
101  }
102 
103  public void onChanging$hoursHistGrid() {
104  EditableCell ec = hoursGrid.getCurrentCell();
105  if(ec != null) {
106  HoursItem hi = (HoursItem) ec.getRow().getValue();
107  if(hi != null) {
108  lWorkOrder.setValue(hi.getWorkOrder() == null ? null : hi.getWorkOrder().getSomeDescription());
109  lOrderReference.setValue(hi.getOrderReference() == null ? null : hi.getOrderReference().getSomeDescription());
110  }
111  }
112  }
113 
114  private void updateButton() {
115  doccreate.setDisabled(
116  humanres.getObjectValue() == null ||
117  Strings.isEmpty(docnumber.getValue()) ||
118  docdate.getValue() == null ||
119  hoursGrid.getValues().isEmpty()
120  );
121  }
122 
123  @Override
124  public void doAfterCompose(Component comp) throws Exception {
125  super.doAfterCompose(comp);
126  docdate.setValue(new Date());
127  doccreate.setDisabled(true);
128  hoursGrid.setDocumentDate(docdate.getValue());
129  }
130 
131  private void createOrder(HumanResource humanResource, String documentNumber, Date documentDate, HoursItem hi) {
132  Dao dao = new ErpPU();
133  if(hi.getResourceAptitude() == null) {
134  // Find Human Resource RequiredUsage
136  "select ru from RequiredUsage ru " +
137  "where task = ? " +
138  "and humanResource = ?",
139  new Object[] { hi.getTask(), humanResource });
140  // Or create it
141  if(ru == null) {
142  ru = new RequiredUsage();
143  ru.setTask(hi.getTask());
144  ru.getTask().getRequiredUsages().add(ru);
145  ru.setPurchaseMode(PurchaseMode.PURCHASE_IMMEDIATELY);
146  ru.setPurchaseLag(0);
147  ru.setHumanResourceAptitude(humanResource.getDefaultHumanResourceAptitude(documentDate));
148  ru.setUnits(0);
149  ru.setDedication(hi.getDedication());
150  ru = dao.saveObject(ru);
151  }
152  // Create DraftOrder
153  OrderItem oi = ru.startOrder();
154  oi.setDocumentDate(documentDate);
155  oi.setDocumentNumber("*" + documentNumber);
156  oi = dao.saveObject(oi);
157  } else {
158  // Find Resource RequiredUsage
160  "select ru from RequiredUsage ru " +
161  "where task = ? " +
162  "and resource = ? " +
163  "and humanResource = ? " +
164  "and aptitudeDegree = ?",
165  new Object[] { hi.getTask(), hi.getResourceAptitude().getResource(),
166  humanResource, hi.getResourceAptitude().getAptitudeDegree() });
167  // Or create it
168  if(ru == null) {
169  ru = new RequiredUsage();
170  ru.setTask(hi.getTask());
171  ru.getTask().getRequiredUsages().add(ru);
173  ru.setPurchaseLag(0);
175  ru.setHumanResource(humanResource);
176  ru.setUnits(hi.getUnits());
177  ru.setDedication(hi.getDedication());
178  ru = dao.saveObject(ru);
179  }
180  // Create DraftOrder
181  OrderItem oi = ru.startOrder();
182  oi.setDocumentDate(documentDate);
183  oi.setDocumentNumber("*" + documentNumber);
184  oi = dao.saveObject(oi);
185  }
186  }
187 
188  private void createReceipt(HumanResource humanResource, String documentNumber, Date documentDate, HoursItem hi) {
189  Dao dao = new ErpPU();
190  if(hi.getResourceAptitude() == null) {
191  // Find Human Resource RequiredUsage
193  "select hru from HumanResourceUsage hru " +
194  "where task = ? " +
195  "and humanResource = ?",
196  new Object[] { hi.getTask(), humanResource });
197  // Or create it
198  if(ru == null) {
199  ru = new RequiredUsage();
200  ru.setTask(hi.getTask());
201  ru.getTask().getRequiredUsages().add(ru);
203  ru.setPurchaseLag(0);
204  ru.setHumanResourceAptitude(humanResource.getDefaultHumanResourceAptitude(documentDate));
205  ru.setUnits(0);
206  ru.setDedication(hi.getDedication());
207  ru = dao.saveObject(ru);
208  }
209  // Create DraftOrder
210  OrderItem oi = ru.startOrder();
211  oi.setDocumentDate(documentDate);
212  oi.setDocumentNumber("*" + documentNumber);
213  oi = dao.saveObject(oi);
214 
215  ReceiptItem ri = oi.startReceipt(hi.getStartWorking());
216  dao.saveObject(ri);
217  if(ri.getOrderItem().getRelated() != null) {
218  ri = ri.getOrderItem().getRelated().getReceiptItems().iterator().next();
219  dao.saveObject(ri);
220  }
221  } else {
222  // Find Resource RequiredUsage
224  "select hru from HumanResourceUsage hru " +
225  "where task = ? " +
226  "and resource = ? " +
227  "and humanResource = ? " +
228  "and aptitudeDegree = ?",
229  new Object[] { hi.getTask(), hi.getResourceAptitude().getResource(),
230  humanResource, hi.getResourceAptitude().getAptitudeDegree() });
231  // Or create it
232  if(ru == null) {
233  ru = new RequiredUsage();
234  ru.setTask(hi.getTask());
235  ru.getTask().getRequiredUsages().add(ru);
237  ru.setPurchaseLag(0);
238  ru.setResourceAptitude(hi.getResourceAptitude());
239  ru.setHumanResource(humanResource);
240  ru.setUnits(hi.getUnits());
241  ru.setDedication(hi.getDedication());
242  ru = dao.saveObject(ru);
243  }
244  // Create DraftOrder
245  OrderItem oi = ru.startOrder();
246  oi.setDocumentDate(documentDate);
247  oi.setDocumentNumber("*" + documentNumber);
248  oi = dao.saveObject(oi);
249 
250  ReceiptItem ri = oi.startReceipt(hi.getStartWorking());
251  dao.saveObject(ri);
252  if(ri.getOrderItem().getRelated() != null) {
253  ri = ri.getOrderItem().getRelated().getReceiptItems().iterator().next();
254  dao.saveObject(ri);
255  }
256  }
257  }
258 
259 }
HumanResourceAptitude getDefaultHumanResourceAptitude(Date date)
void setDocumentDate(Date documentDate)
Definition: OrderItem.java:129
ReceiptItem startReceipt(Date startWorking)
Definition: OrderItem.java:409
Set< ReceiptItem > getReceiptItems()
Definition: OrderItem.java:221
void setDocumentNumber(String documentNumber)
Definition: OrderItem.java:145
void setHumanResource(HumanResource resource)
void setHumanResourceAptitude(HumanResourceAptitude hro)
void setPurchaseMode(PurchaseMode purchaseMode)
void setResourceAptitude(ResourceAptitude ro)
void setDedication(double dedication)
void setPurchaseLag(double purchaseLag)
void onChanging $hoursHistGrid()
Definition: HoursEdit.java:103
void onChanging $hoursGrid()
Definition: HoursEdit.java:90
void doAfterCompose(Component comp)
Definition: HoursEdit.java:124
void setDocumentDate(Date documentDate)
Definition: HoursGrid.java:77
void refreshUsing(HumanResource humanResource, String documentNumber, Date documentDate)
Definition: HoursGrid.java:222
ResourceAptitude getResourceAptitude()
Definition: HoursItem.java:127
OrderReference getOrderReference()
Definition: HoursItem.java:119
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419