BrightSide Workbench Full Report + Source Code
HoursGrid.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.ArrayList;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.amic.util.date.CheckDate;
27 import org.turro.elephant.context.Application;
28 import org.turro.elephant.util.DateFormats;
29 import org.turro.elephant.util.DecimalFormats;
30 import org.turro.erp.db.ErpPU;
31 import org.turro.erp.entity.HumanResource;
32 import org.turro.erp.entity.Task;
33 import org.turro.erp.entity.WorkOrder;
34 import org.turro.erp.reference.OrderReferenceGrid;
35 import org.turro.erp.reference.OrderReferencePositionListbox;
36 import org.turro.erp.resource.ResourceAptitude;
37 import org.turro.erp.resource.ResourceAptitudeCombobox;
38 import org.turro.erp.task.TaskListbox;
39 import org.turro.jpa.Dao;
40 import org.turro.zkoss.grid.CollectionGrid;
41 import org.turro.zkoss.grid.EditableCell;
42 import org.turro.zkoss.grid.EditableColumn;
43 import org.turro.zkoss.input.Percentbox;
44 import org.zkoss.zk.ui.HtmlBasedComponent;
45 import org.zkoss.zk.ui.event.Event;
46 import org.zkoss.zk.ui.event.EventListener;
47 import org.zkoss.zk.ui.event.Events;
48 import org.zkoss.zk.ui.ext.AfterCompose;
49 import org.zkoss.zul.Row;
50 
55 public class HoursGrid extends CollectionGrid<HoursItem> implements AfterCompose {
56 
57  private long lastWorkOrderId;
58  private int lastOrderRef;
59  private Date lastDate;
60  private Task lastTask;
61  private ResourceAptitude lastResource;
62  private String lastConcept;
63  private boolean receipt;
64 
65  public HoursGrid() {
66  addColumns();
67  }
68 
69  public boolean isReceipt() {
70  return receipt;
71  }
72 
73  public void setReceipt(boolean receipt) {
74  this.receipt = receipt;
75  }
76 
77  public void setDocumentDate(Date documentDate) {
78  if(lastDate == null) {
79  lastDate = new CheckDate(documentDate).setHour(8).setMinute(0).setSecond(0).getDate();
80  }
81  }
82 
83  @Override
84  protected void initiateRow(Row row, HoursItem value) {
85  if(value == null) {
86  value = new HoursItem();
87  value.setDedication(1.0);
88  value.setWorkOrderId(lastWorkOrderId);
89  value.setOrderRef(lastOrderRef);
90  value.setStartWorking(lastDate);
91  value.setTask(lastTask);
92  value.setConcept(lastConcept);
93  value.setResourceAptitude(lastResource);
94  }
95  row.setValue(value);
96  }
97 
98  @Override
99  protected boolean deleteRow(Row row) {
100  return true;
101  }
102 
103  @Override
104  protected boolean isValid(HoursItem v) {
105  return v.isValid();
106  }
107 
108  @Override
109  protected HtmlBasedComponent createEditor(final EditableCell editableCell) {
110  if(editableCell.getColumn() instanceof EditableColumn) {
111  final HoursItem hoursItem = (HoursItem) editableCell.getRow().getValue();
112  if(hoursItem.getStartWorking() == null) {
113  hoursItem.setStartWorking(lastDate);
114  }
115  EditableColumn ec = (EditableColumn) editableCell.getColumn();
116  if(ec.getLabel().equals(Application.getString("lReference"))) {
117  Object value = getCellValue(editableCell);
119  orplb.setMold("select");
120  orplb.setAllowNull(true);
121  orplb.setNullAtBottom(true);
122  orplb.setNullLabel("lNewReference");
123  orplb.setWorkOrder(hoursItem.getWorkOrder());
124  // line value must update with new selection
125  orplb.addEventListener(Events.ON_SELECT, new EventListener() {
126  @Override
127  public void onEvent(Event event) throws Exception {
128  Integer oref = orplb.getObjectValue();
129  if(oref != null) {
130  hoursItem.setOrderRef(oref);
131  Events.postEvent(new Event(Events.ON_CHANGING, HoursGrid.this, editableCell));
132  }
133  }
134  });
135  if(value != null) orplb.setObjectValue((Integer) value);
136  return orplb;
137  } else if(ec.getJavaClass().equals(Task.class)) {
138  Object value = getCellValue(editableCell);
139  TaskListbox tlb = new TaskListbox();
140  tlb.setMold("select");
141  tlb.setAllowNull(true);
142  tlb.setNullAtBottom(true);
143  tlb.setNullLabel("lNewTask");
144  tlb.setOrderReference(hoursItem.getOrderReference());
145  if(value != null) tlb.setObjectValue((Task) value);
146  return tlb;
147  } else if(ec.getJavaClass().equals(ResourceAptitude.class)) {
148  Object value = getCellValue(editableCell);
150  if(value != null) racb.setObjectValue((ResourceAptitude) value);
151  return racb;
152  } else if(ec.getLabel().equals(Application.getString("lDedication"))) {
153  Object value = getCellValue(editableCell);
154  Percentbox pc = new Percentbox();
155  if(value != null) pc.setValue((Double) value);
156  return pc;
157  }
158  }
159  return super.createEditor(editableCell);
160  }
161 
162  @Override
163  protected String formatCell(EditableCell editableCell, Object value) {
164  HoursItem hoursItem = (HoursItem) editableCell.getRow().getValue();
165  lastWorkOrderId = hoursItem.getWorkOrderId();
166  lastOrderRef = hoursItem.getOrderRef();
167  lastDate = hoursItem.getStartWorking();
168  lastTask = hoursItem.getTask();
169  lastResource = hoursItem.getResourceAptitude();
170  lastConcept = hoursItem.getConcept();
171  if(value instanceof Task) {
172  Task t = ((Task) value);
173  return t.getName();
174  } else if(value instanceof ResourceAptitude) {
175  ResourceAptitude r = ((ResourceAptitude) value);
176  return r.getName();
177  } else if(editableCell.getColumn().getLabel().equals(Application.getString("lDedication"))) {
178  return DecimalFormats.format(((Double) value) * 100.0) + "%";
179  }
180  return super.formatCell(editableCell, value);
181  }
182 
183  @Override
184  protected void cellChanged(EditableCell editableCell, Object value) {
185  if(editableCell.getColumn() instanceof EditableColumn) {
186  EditableColumn ec = (EditableColumn) editableCell.getColumn();
187  if(ec.getLabel().equals(Application.getString("lAutomatic"))) {
188  updateRow(editableCell.getRow());
189  }
190  }
191  super.cellChanged(editableCell, value);
192  }
193 
194  @Override
195  protected boolean isCellValid(EditableCell editableCell, Object value) {
196  if(editableCell.getColumn() instanceof EditableColumn) {
197  EditableColumn ec = (EditableColumn) editableCell.getColumn();
198  if(ec.getLabel().equals(Application.getString("lWorkOrder"))) {
199  return WorkOrder.getByWorkOrderId((Long) value) != null;
200  }
201  }
202  return super.isCellValid(editableCell, value);
203  }
204 
205  private void addColumns() {
206  try {
207  addColumn(Application.getString("lWorkOrder"), "long", "workOrderId", null, 0, false, false).setWidth("100px");
208  addColumn(Application.getString("lReference"), "int", "orderRef", null, 0, false, false).setWidth("100px");
209  addColumn(Application.getString("lTask"), Task.class, "task", null, 3, false, false).setHflex("1");
210  addColumn(Application.getString("lDate"), Date.class, "startWorking", DateFormats.getDefaultDateTimeFormat(), 0, false, false).setWidth("150px");
211  addColumn(Application.getString("lHours"), "double", "units", null, 2, false, false).setWidth("60px");
212  addColumn(Application.getString("lCostHour"), "double", "costHour", null, 2, false, false).setWidth("60px");
213  addColumn(Application.getString("lDedication"), "double", "dedication", null, 2, false, false).setWidth("80px");
214  addColumn(Application.getString("lConcept"), String.class, "concept", null, 0, false, false).setHflex("1");
215  addColumn(Application.getString("lResource"), ResourceAptitude.class, "resourceAptitude", null, 0, false, false).setHflex("1");
216  addColumn(Application.getString("lAutomatic"), "boolean", "automatic", null, 0, false, false).setWidth("90px");
217  } catch (ClassNotFoundException ex) {
218  Logger.getLogger(OrderReferenceGrid.class.getName()).log(Level.SEVERE, null, ex);
219  }
220  }
221 
222  public void refreshUsing(HumanResource humanResource, String documentNumber, Date documentDate) {
223  Dao dao = new ErpPU();
224  List<HoursItem> l = new ArrayList<HoursItem>();
225 // for(HoursReceipt hr : (List<HoursReceipt>) dao.getResultList(
226 // "select hr from HoursReceipt as hr where humanReceipt.draftOrder.humanResourceUsage.humanResource = ? and documentNumber = ? and documentDate = ?",
227 // new Object[] { humanResource, documentNumber, documentDate })) {
228 // l.add(new HoursItem(hr));
229 // }
230 // for(HoursReceipt hr : (List<HoursReceipt>) dao.getResultList(
231 // "select hr from HoursReceipt as hr where resourceReceipt.draftOrder.resourceUsage.support = ? and documentNumber = ? and documentDate = ?",
232 // new Object[] { humanResource, documentNumber, documentDate })) {
233 // l.add(new HoursItem(hr));
234 // }
235  updateCollection(l);
236  }
237 
238 }
static String getString(String key)
static String format(Number value, String pattern)
static WorkOrder getByWorkOrderId(long value)
Definition: WorkOrder.java:365
boolean isCellValid(EditableCell editableCell, Object value)
Definition: HoursGrid.java:195
void cellChanged(EditableCell editableCell, Object value)
Definition: HoursGrid.java:184
void setDocumentDate(Date documentDate)
Definition: HoursGrid.java:77
void initiateRow(Row row, HoursItem value)
Definition: HoursGrid.java:84
HtmlBasedComponent createEditor(final EditableCell editableCell)
Definition: HoursGrid.java:109
String formatCell(EditableCell editableCell, Object value)
Definition: HoursGrid.java:163
boolean isValid(HoursItem v)
Definition: HoursGrid.java:104
void refreshUsing(HumanResource humanResource, String documentNumber, Date documentDate)
Definition: HoursGrid.java:222
void setReceipt(boolean receipt)
Definition: HoursGrid.java:73
void setStartWorking(Date startWorking)
Definition: HoursItem.java:139
void setWorkOrderId(long workOrderId)
Definition: HoursItem.java:175
ResourceAptitude getResourceAptitude()
Definition: HoursItem.java:127
void setResourceAptitude(ResourceAptitude resourceAptitude)
Definition: HoursItem.java:131
void setConcept(String concept)
Definition: HoursItem.java:81
OrderReference getOrderReference()
Definition: HoursItem.java:119
void setDedication(double dedication)
Definition: HoursItem.java:97
void setOrderRef(int orderRef)
Definition: HoursItem.java:105
void updateCollection(Collection< V > collection)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
Object getCellValue(EditableCell editableCell)
void setNullAtBottom(boolean nullAtBottom)