BrightSide Workbench Full Report + Source Code
document/DocumentLinesGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.financials.document;
19 
20 import java.util.Collection;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.elephant.context.ElephantContext;
24 import org.turro.financials.contract.ContractListbox;
25 import org.turro.financials.contract.logic.ContractWrapper;
26 import org.turro.financials.entity.*;
27 import org.turro.financials.linetype.LineTypePreferenceListbox;
28 import org.turro.financials.model.contract.ContractUsualSet;
29 import org.turro.financials.model.document.DocumentDefinitionWrapper;
30 import org.turro.financials.model.product.ProductWrapper;
31 import org.turro.financials.product.IProduct;
32 import org.turro.financials.product.ProductExtendedCombobox;
33 import org.turro.financials.product.ProductFactory;
34 import org.turro.i18n.I_;
35 import org.turro.math.Zero;
36 import org.turro.usual.UsualUsages;
37 import org.turro.zkoss.grid.CollectionGrid;
38 import org.turro.zkoss.grid.EditableCell;
39 import org.turro.zkoss.grid.EditableColumn;
40 import org.turro.zkoss.input.CollectionListbox;
41 import org.zkoss.zk.ui.HtmlBasedComponent;
42 import org.zkoss.zul.Row;
43 
48 public class DocumentLinesGrid extends CollectionGrid<DocumentLine> {
49 
50  private Document document;
51  private Collection<Contract> stores;
52  private ContractUsualSet usualStores;
53  private Contract lastStore = null;
54  private final boolean hasProducts;
55  private double lastTax, lastReq, lastRet;
56 
57  public DocumentLinesGrid() {
58  super();
59  hasProducts = ProductWrapper.hasAvailableProducts();
60  }
61 
62  public Document getDocument() {
63  return document;
64  }
65 
66  public void setDocument(Document document) {
67  this.document = document;
68  if(document == null || !document.isValid()) return;
69  document.clearEmpties();
70  setCollection(document.getDocumentLines());
71  stores = new DocumentDefinitionWrapper(document.getDocumentDefinition())
72  .getRelatedStores(document.getContract());
73  if(stores.isEmpty()) {
74  stores = new DocumentDefinitionWrapper(document.getDocumentDefinition())
76  }
78  usualStores = new ContractUsualSet(stores, true, document.getUsualPath() + "st:");
79 // if(!stores.isEmpty()) {
80 // lastStore = stores.iterator().next();
81 // }
82  }
83 
84  public void updateDocument(Document document) {
85  clearTable();
86  setDocument(document);
87  afterCompose();
88  }
89 
90  public void updateLines() {
91  clearRows();
92  setDocument(document);
93  super.afterCompose();
94  }
95 
96  public Collection<Contract> getStores() {
97  return stores;
98  }
99 
100  @Override
101  protected void initiateRow(Row row, DocumentLine value) {
102  if(value == null) {
103  value = new DocumentLine();
104  value.setDocument(document);
105  value.setLineType(document.getDefaultLineType());
106  if(value.getLineType() != null) {
108  }
109  value.setStore(lastStore);
110  // avoid equalities
111  value.setLineOrder(document.getDocumentLines().size() + 90001);
112  document.getDocumentLines().add(value);
113  }
114  row.setValue(value);
115  }
116 
117  @Override
118  protected boolean deleteRow(Row row) {
119  document.getDocumentLines().remove((DocumentLine) row.getValue());
120  return true;
121  }
122 
123  @Override
124  protected boolean isValid(DocumentLine v) {
125  return !v.isEmpty();
126  }
127 
128  @Override
129  public void afterCompose() {
130  if(document != null && document.isValid()) {
131  setReadOnly(false);
132  try {
133  addColumns();
134  invalidate();
135  } catch (ClassNotFoundException ex) {
136  Logger.getLogger(DocumentLinesGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
137  }
138  } else {
139  setReadOnly(true);
140  }
141  super.afterCompose();
142  }
143 
144  @Override
145  protected Object getEditorValue(EditableCell editableCell) {
146  HtmlBasedComponent hbc = (HtmlBasedComponent) editableCell.getEditor();
147  if(hbc instanceof ProductExtendedCombobox) {
148  DocumentLine dl = (DocumentLine) editableCell.getRow().getValue();
149  IProduct prod = ((ProductExtendedCombobox) hbc).getObjectValue();
150  if(prod != null) {
151  ProductFactory.setPriceTax(dl, prod);
152  if(prod.isUncoded()) {
153  dl.setConcept(prod.getProductString());
154  return null;
155  }
157  } else {
158  dl.setProductByContractor(null);
159  }
160  return prod;
161  }
162  return super.getEditorValue(editableCell);
163  }
164 
165  @Override
166  protected String formatCell(EditableCell editableCell, Object value) {
167 // if(value instanceof Product) {
168 // Product p = ((Product) value);
169 // setValuesFrom(p, (DocumentLine) editableCell.getRow().getFieldValue());
170 // return "#" + p.getProductCodeStr() + " " + p.getDescription();
171 // } else if(value instanceof ProductByContractor) {
172 // ProductByContractor pbp = ((ProductByContractor) value);
173 // setValuesFrom(pbp, (DocumentLine) editableCell.getRow().getFieldValue());
174 // return "#" + pbp.getContractorCode() + " " + pbp.getProduct().getDescription();
175  if(value instanceof Product) {
176  Product p = ((Product) value);
177  DocumentLine dl = (DocumentLine) editableCell.getRow().getValue();
180  return p.getProductString();
181  } else if(value instanceof LineType) {
182  LineType lt = ((LineType) value);
183  return lt.getComposedName();
184  } else if(value instanceof Contract) {
185  Contract ctc = ((Contract) value);
186  return ctc.getPartialDescription();
187  }
188  return super.formatCell(editableCell, value);
189  }
190 
191  @Override
192  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
193  if(editableCell.getColumn() instanceof EditableColumn) {
194  EditableColumn ec = (EditableColumn) editableCell.getColumn();
195  if(ec.getJavaClass().equals(Product.class)) {
196  Object value = getCellValue(editableCell);
197  DocumentLine dl = (DocumentLine) editableCell.getRow().getValue();
198  /*
199  ProductCombobox pc = new ProductCombobox();
200  if(value != null) pc.setObjectValue((Product) value);
201  return pc;
202  */
204  pc.setContractorId(document.getContract().getId());
205  IProduct prod = (IProduct) value;
206  if(prod == null && dl.getProductByContractor() != null) {
207  prod = dl.getProductByContractor().getProduct();
208  }
209  if(prod != null) {
211  pc.setObjectValue(prod);
212  }
213  return pc;
214 // } else if(ec.getJavaClass().equals(ProductByContractor.class)) {
215 // Object value = getCellValue(editableCell);
216 // ProductByContractorCombobox pbpc = new ProductByContractorCombobox(document.getContract());
217 // if(value != null) pbpc.setObjectValue((ProductByContractor) value);
218 // return pbpc;
219  } else if(ec.getJavaClass().equals(LineType.class)) {
220  DocumentLine dl = (DocumentLine) editableCell.getRow().getValue();
221  Object value = getCellValue(editableCell);
223  //TODO: check whether is preferable to assign preferences from document line
224  //((LineType) value).setContractPreference(dl.getContractPreference());
225  if(value != null) ltl.setObjectValue((LineType) value);
226  ltl.setMold("select");
227  return ltl;
228  } else if(ec.getJavaClass().equals(Contract.class)) {
229  DocumentLine dl = (DocumentLine) editableCell.getRow().getValue();
230  Object value = getCellValue(editableCell);
231  ContractListbox slb = new ContractListbox(usualStores);
232  if(value != null) slb.setObjectValue((Contract) value);
233  slb.setMold("select");
234  return slb;
235  } else if(ec.getJavaClass().equals(String.class) && "concept".equals(ec.getProperty())) {
236  DocumentLine dl = (DocumentLine) editableCell.getRow().getValue();
237  Object value = getCellValue(editableCell);
238  ConceptCombobox slb = new ConceptCombobox();
240  if(value != null) {
241  slb.setObjectValue((String) value);
242  } else if(!hasProducts) {
243  slb.setObjectValue(UsualUsages.getMostUsedvalue(document.getUsualPath() + "cp:"));
244  }
245  return slb;
246  } else if("tax".equals(ec.getProperty())) {
247  HtmlBasedComponent hbc = new ContractWrapper(document.getContract()).getTaxCtrl();
248  Object value = getCellValue(editableCell);
249  if(hbc != null) {
250  if(hbc instanceof CollectionListbox) {
251  if(!Zero.near((double) value, 2)) {
252  ((CollectionListbox) hbc).setObjectValue(value);
253  } else {
254  ((CollectionListbox) hbc).setObjectValue(lastTax);
255  }
256  }
257  return hbc;
258  } else {
259  setCellValue(editableCell, !Zero.near((double) value, 2) ? value : lastTax);
260  }
261  } else if("equivalenceSurcharge".equals(ec.getProperty())) {
262  HtmlBasedComponent hbc = new ContractWrapper(document.getContract()).getReqCtrl();
263  Object value = getCellValue(editableCell);
264  if(hbc != null) {
265  if(hbc instanceof CollectionListbox) {
266  if(value != null) {
267  ((CollectionListbox) hbc).setObjectValue(value);
268  } else {
269  ((CollectionListbox) hbc).setObjectValue(lastReq);
270  }
271  }
272  return hbc;
273  } else {
274  setCellValue(editableCell, !Zero.near((double) value, 2) ? value : lastReq);
275  }
276  } else if("retention".equals(ec.getProperty())) {
277  HtmlBasedComponent hbc = new ContractWrapper(document.getContract()).getRetentionCtrl();
278  Object value = getCellValue(editableCell);
279  if(hbc != null) {
280  if(hbc instanceof CollectionListbox) {
281  if(value != null) ((CollectionListbox) hbc).setObjectValue(value);
282  }
283  return hbc;
284  } else {
285  setCellValue(editableCell, !Zero.near((double) value, 2) ? value : lastRet);
286  }
287  }
288  }
289  return super.createEditor(editableCell);
290  }
291 
292  @Override
293  protected void cellChanged(EditableCell editableCell, Object value) {
294  super.cellChanged(editableCell, value);
295  updateRow(editableCell.getRow());
296  EditableColumn ec = (EditableColumn) editableCell.getColumn();
297  if(value instanceof Contract) {
298  lastStore = (Contract) value;
299  } else if("tax".equals(ec.getProperty())) {
300  lastTax = (Double) value;
301  } else if("equivalenceSurcharge".equals(ec.getProperty())) {
302  lastReq = (Double) value;
303  } else if("retention".equals(ec.getProperty())) {
304  lastRet = (Double) value;
305  }
306  }
307 
308  private void addColumns() throws ClassNotFoundException {
309  int fractionDigits = document.getCurrency().getDefaultFractionDigits();
311  if(DocumentColumn.DCOL_PRODUCT.equals(dc.getDocumentColumn())) {
312 // if(document.getContract().isUseContractorCode()) {
313 // if(ProductWrapper.hasAvailableContractorProducts(document.getContract())) {
314 // addColumn(I_.get("Product / Contractor"), ProductByContractor.class,
315 // "productByContractor", null, 0, false, false).setWidth("300px");
316 // }
317 // } else {
318 // if(ProductWrapper.hasAvailableProducts()) {
319 // addColumn(I_.get("Product"), Product.class,
320 // "product", null, 0, false, false).setWidth("300px");
321 // }
322 // }
323  if(hasProducts) {
324  addColumn(I_.get("Product"), Product.class,
325  "product", null, 0, false, false).setHflex("10");
326  }
327  } else if(DocumentColumn.DCOL_CONCEPT.equals(dc.getDocumentColumn())) {
328  addColumn(I_.get("Concept"), String.class,
329  "concept", null, 10, false, false).setHflex("10");
330  } else if(DocumentColumn.DCOL_QUANTITY.equals(dc.getDocumentColumn())) {
331  addColumn(I_.get("Quantity"), "double",
332  "quantity", null, 3, false, false).setHflex("3");
333  } else if(DocumentColumn.DCOL_PRICE.equals(dc.getDocumentColumn())) {
334  addColumn(I_.get("Price"), "double",
335  "price", null, fractionDigits + 2, false, false).setHflex("4");
336  } else if(DocumentColumn.DCOL_MONEY_DISCOUNT.equals(dc.getDocumentColumn())) {
337  addColumn(I_.get("Discount"), "double",
338  "discountMoney", null, fractionDigits, false, false).setHflex("2");
339  } else if(DocumentColumn.DCOL_PERCENT_DISCOUNT.equals(dc.getDocumentColumn())) {
340  addColumn(I_.get("% Dnt."), "double",
341  "discountPerCent", "#.##", 2, false, false).setHflex("2");
342  } else if(DocumentColumn.DCOL_TAX.equals(dc.getDocumentColumn())) {
343  addColumn(I_.get("Tax"), "double",
344  "tax", null, 2, false, false).setHflex("2");
345  } else if(document.getContract().isEquivalenceSurcharge() && DocumentColumn.DCOL_REQ.equals(dc.getDocumentColumn())) {
346  addColumn(I_.get("EqSur."), "double",
347  "equivalenceSurcharge", null, 2, false, false).setHflex("2");
348  } else if(DocumentColumn.DCOL_RETENTION.equals(dc.getDocumentColumn())) {
349  addColumn(I_.get("% Ret."), "double",
350  "retention", null, 2, false, false).setHflex("2");
351  } else if(DocumentColumn.DCOL_AMOUNT.equals(dc.getDocumentColumn())) {
352  addColumn(I_.get("Amount"), "double",
353  "taxable", null, fractionDigits, false, true).setHflex("4");
354  }
355  }
356  //if(stores.size() > 1) {
357  addColumn(I_.get("Store"), Contract.class,
358  "store", null, 0, false, false).setHflex("5");
359  //}
360  //if(document.getDocumentDefinition().getRelatedLineTypes().size() > 1) {
361  addColumn(I_.get("Line type"), LineType.class,
362  "lineType", null, 0, false, false).setHflex("5");
363  //}
364  }
365 
366 }
void setDocumentDefinition(DocumentDefinition documentDefinition)
String formatCell(EditableCell editableCell, Object value)
HtmlBasedComponent createEditor(EditableCell editableCell)
void cellChanged(EditableCell editableCell, Object value)
void setContractPreference(ContractPreference contractPreference)
void setProductByContractor(ProductByContractor productByContractor)
ProductByContractor getProductByContractor()
Set< DocumentLine > getDocumentLines()
Definition: Document.java:180
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167
ContractPreference getContractPreference()
Definition: LineType.java:77
void setProductByContractor(ProductByContractor productByContractor)
Definition: Product.java:213
static void setPriceTax(DocumentLine dl, IProduct prod)
static String get(String msg)
Definition: I_.java:41
static boolean near(double value, int digits)
Definition: Zero.java:30
static String getMostUsedvalue(String path)
void setCollection(Collection< V > collection)
void setCellValue(EditableCell editableCell, Object value)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
Object getCellValue(EditableCell editableCell)
void setReadOnly(boolean readOnly)
ProductByContractor getProductByContractor()
void setProductByContractor(ProductByContractor pbc)