BrightSide Workbench Full Report + Source Code
ReceiptGrid.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.usage;
20 
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.elephant.context.Application;
24 import org.turro.elephant.util.DecimalFormats;
25 import org.turro.erp.entity.ReceiptItem;
26 import org.turro.erp.entity.RequiredUsage;
27 import org.turro.erp.entity.Resource;
28 import org.turro.erp.entity.Task;
29 import org.turro.erp.entity.WorkOrder;
30 import org.turro.erp.humanres.HumanResourceAptitudeCombobox;
31 import org.turro.erp.humanres.HumanResourceCombobox;
32 import org.turro.erp.resource.ResourceCombobox;
33 import org.turro.erp.task.ReceiptItemAdapter;
34 import org.turro.financials.contract.ProviderCombobox;
35 import org.turro.financials.entity.Contract;
36 import org.turro.financials.entity.LineType;
37 import org.turro.financials.linetype.BillLineTypeListbox;
38 import org.turro.financials.model.document.LineTypeDescription;
39 import org.turro.financials.product.IProduct;
40 import org.turro.financials.product.ProductExtendedCombobox;
41 import org.turro.hr.humanres.HumanResourceAptitude;
42 import org.turro.zkoss.grid.CollectionGrid;
43 import org.turro.zkoss.grid.EditableCell;
44 import org.turro.zkoss.grid.EditableColumn;
45 import org.turro.zkoss.input.Percentbox;
46 import org.turro.zkoss.label.LabelTypes;
47 import org.zkoss.zk.ui.HtmlBasedComponent;
48 import org.zkoss.zul.Row;
49 import org.zkoss.zul.Vlayout;
50 
55 public class ReceiptGrid extends CollectionGrid<ReceiptItem> {
56 
57  private Task task;
58  private WorkOrder workOrder;
59 
60  public ReceiptGrid() {
61  setAllowInsertions(false);
62  }
63 
64  public Task getTask() {
65  return task;
66  }
67 
68  public void setTask(Task task) {
69  this.task = task;
70  }
71 
73  return workOrder;
74  }
75 
76  public void setWorkOrder(WorkOrder workOrder) {
77  this.workOrder = workOrder;
78  }
79 
80  @Override
81  protected void initiateRow(Row row, ReceiptItem value) {
82  if(value == null) {
83  throw new RuntimeException("Empty row on receipt!");
84  }
85  row.setValue(value);
86  }
87 
88  @Override
89  protected boolean deleteRow(Row row) {
90  return false;
91  }
92 
93  @Override
94  protected boolean isValid(ReceiptItem v) {
95  return !v.isEmpty();
96  }
97 
98  @Override
99  public void afterCompose() {
100  addColumns();
101  setCollection(task == null ? new ReceiptItemAdapter(task) : new ReceiptItemAdapter(workOrder));
102  super.afterCompose();
103  }
104 
105  @Override
106  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
107  if(editableCell.getColumn() instanceof EditableColumn) {
108  EditableColumn ec = (EditableColumn) editableCell.getColumn();
109  RequiredUsage ru = editableCell.getRow().getValue();
110  if(ec.getJavaClass().equals(HumanResourceAptitude.class)) {
111  Object value = getCellValue(editableCell);
113  hrac.setTask(task);
114  if(value != null) hrac.setObjectValue((HumanResourceAptitude) value);
115  return hrac;
116  } else if(ec.getJavaClass().equals(Resource.class)) {
117  Object value = getCellValue(editableCell);
119  if(value != null) rcb.setObjectValue((Resource) value);
120  return rcb;
121  } else if(ec.getJavaClass().equals(Contract.class)) {
122  Object value = getCellValue(editableCell);
124  if(value != null) pcb.setObjectValue((Contract) value);
125  return pcb;
126  } else if(ec.getJavaClass().equals(IProduct.class)) {
127  Object value = getCellValue(editableCell);
129  pecb.setContractorId(ru.getProviderId());
130  if(value != null) pecb.setObjectValue((IProduct) value);
131  return pecb;
132  } else if(ec.getJavaClass().equals(LineType.class)) {
133  Object value = getCellValue(editableCell);
135  bltlb.setMold("select");
137  return bltlb;
138  } else if(editableCell.getColumn().getLabel().equals(Application.getString("lDedication"))) {
139  Object value = getCellValue(editableCell);
140  Percentbox pb = new Percentbox();
141  if(value != null) pb.setValue((Double) value);
142  return pb;
143  }
144  }
145  return super.createEditor(editableCell);
146  }
147 
148  @Override
149  protected HtmlBasedComponent createRenderer(EditableCell editableCell) {
150  if(editableCell.getColumn() instanceof EditableColumn) {
151  EditableColumn ec = (EditableColumn) editableCell.getColumn();
152  final ReceiptItem ri = editableCell.getRow().getValue();
153  if(editableCell.getCellIndex() == 0) {
154  return getResourceBox(ri, false);
155  }
156  }
157  return super.createRenderer(editableCell);
158  }
159 
160  @Override
161  protected String formatCell(EditableCell editableCell, Object value) {
162  if(value instanceof HumanResourceAptitude) {
164  return res.getRealName();
165  } else if(value instanceof Resource) {
166  Resource res = ((Resource) value);
167  return res.getName();
168  } else if(value instanceof Contract) {
169  Contract ctc = ((Contract) value);
170  return ctc.getPartialDescription();
171  } else if(value instanceof IProduct) {
172  IProduct p = ((IProduct) value);
173  return p.getProductString();
174  } else if(value instanceof LineType) {
175  RequiredUsage ru = editableCell.getRow().getValue();
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 Object getEditorValue(EditableCell editableCell) {
185  if(editableCell.getJavaClass().equals(LineType.class)) {
186  RequiredUsage ru = editableCell.getRow().getValue();
187  BillLineTypeListbox bltlb = (BillLineTypeListbox) editableCell.getEditor();
188  LineType lt = bltlb.getObjectValue();
189  if(lt != null) {
190  ru.setLineTypeId(lt.getId());
191  if(lt.getContractPreference() != null) {
193  }
194  }
195  return lt;
196  }
197  return super.getEditorValue(editableCell);
198  }
199 
200  private void addColumns() {
201  try {
202  addColumn(Application.getString("lResource") + "/" + Application.getString("lProduct"), String.class, "", null, 2, false, false).setHflex("1");
203  addColumn(Application.getString("lUnits"), "double", "units", null, 3, false, false).setWidth("60px");
204  addColumn(Application.getString("lDedication"), "double", "dedication", null, 2, false, false).setWidth("80px");
205  addColumn(Application.getString("lCost"), "double", "cost", null, 3, false, false).setWidth("90px");
206  } catch (ClassNotFoundException ex) {
207  Logger.getLogger(UsageGrid.class.getName()).log(Level.SEVERE, null, ex);
208  }
209  }
210 
211  private HtmlBasedComponent getResourceBox(final ReceiptItem ri, boolean edit) {
212  RequiredUsage ru = ri.getOrderItem().getRequiredUsage();
213  Vlayout vbox = new Vlayout();
214  if(edit && ri.isHumanResourceType() && ru.isCanChange()) {
215  HumanResourceCombobox hrc = new HumanResourceCombobox();
216  hrc.setObjectValue(ri.getHumanResource());
217  // on change ri and oi
218  vbox.appendChild(hrc);
219  } else if(edit && ri.isProductType() && !ru.isProviderFixed()) {
220  ProviderCombobox pc = new ProviderCombobox();
221  pc.setObjectValue(ri.getProvider());
222  // on change ri and oi
223  vbox.appendChild(pc);
224  } else {
225  vbox.appendChild(LabelTypes.getCaptionLabel(ri.getOrderItem()
226  .getRequiredUsage().getName()));
227  }
228  vbox.appendChild(LabelTypes.getSoftLabel(ri.getOrderItem()
229  .getOwnerRequiredUsage().getTask().getName()));
230  vbox.appendChild(LabelTypes.getSoftLabel(ri.getOrderItem()
231  .getOwnerRequiredUsage().getTask().getOrderReference().getDescription()));
232  return vbox;
233  }
234 
235 }
static String getString(String key)
static String format(Number value, String pattern)
void setContractPreferenceId(long contractPreferenceId)
void setLineTypeId(long lineTypeId)
boolean isValid(ReceiptItem v)
HtmlBasedComponent createEditor(EditableCell editableCell)
void setWorkOrder(WorkOrder workOrder)
Object getEditorValue(EditableCell editableCell)
void initiateRow(Row row, ReceiptItem value)
HtmlBasedComponent createRenderer(EditableCell editableCell)
String formatCell(EditableCell editableCell, Object value)
ContractPreference getContractPreference()
Definition: LineType.java:77
void setData(Contract contract, long lineTypeId, long contractPreferenceId)
void setCollection(Collection< V > collection)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
void setAllowInsertions(boolean allowInsertions)
Object getCellValue(EditableCell editableCell)