BrightSide Workbench Full Report + Source Code
DocumentExpiriesGrid.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.*;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.string.Strings;
24 import org.turro.collections.CollectionUtil;
25 import org.turro.elephant.context.ElephantContext;
26 import org.turro.financials.contract.ContractListbox;
27 import org.turro.financials.contract.logic.ContractWrapper;
28 import org.turro.financials.document.logic.DocumentDefinitionWrapper;
29 import org.turro.financials.entity.*;
30 import org.turro.financials.linetype.LineTypePreferenceListbox;
31 import org.turro.financials.model.contract.ContractUsualSet;
32 import org.turro.financials.model.document.DocumentDefinitionUsualSet;
33 import org.turro.financials.model.document.SingleLineDocumentWrapper;
34 import org.turro.financials.model.product.ProductWrapper;
35 import org.turro.financials.product.ProductByContractorCombobox;
36 import org.turro.financials.product.ProductCombobox;
37 import org.turro.i18n.I_;
38 import org.turro.usual.UsualUsages;
39 import org.turro.zkoss.grid.CollectionGrid;
40 import org.turro.zkoss.grid.EditableCell;
41 import org.turro.zkoss.grid.EditableColumn;
42 import org.zkoss.zk.ui.HtmlBasedComponent;
43 import org.zkoss.zk.ui.event.Event;
44 import org.zkoss.zk.ui.event.Events;
45 import org.zkoss.zul.Row;
46 
51 public class DocumentExpiriesGrid extends CollectionGrid<SingleLineDocumentWrapper> {
52 
53  private Document document;
54  private Collection<DocumentDefinition> expiryDefinitions;
55  private final Map<DocumentDefinition, Contract> lastStoreMap = new HashMap<>();
56  private DocumentDefinition lastDocDef = null;
57  private final Map<Long, Collection<Contract>> storeMap = new HashMap<>();
58  //private boolean showStoreInput = false, showLineTypeInput = false;
59 
61  super();
62  }
63 
64  public Collection<DocumentRelation> getRelations() {
65  ArrayList<DocumentRelation> list = new ArrayList<>();
66  for(SingleLineDocumentWrapper sldw : getValues()) {
67  list.add(sldw.getRelation());
68  }
69  return list;
70  }
71 
72  public Document getDocument() {
73  return document;
74  }
75 
76  public void setDocument(Document document) {
77  this.document = document;
78  }
79 
80  private void updateDocument() {
81  clearTable();
82  if(document == null || document.getContract() == null) {
83  afterCompose();
84  return;
85  }
86  expiryDefinitions = new ContractWrapper(document.getContract()).getExpiryDefinitions(document.getDocumentDefinition());
87  expiryDefinitions = new DocumentDefinitionUsualSet(expiryDefinitions, document.getUsualPath() + "df:");
88  Iterator<DocumentDefinition> it = expiryDefinitions.iterator();
89  while (it.hasNext()) {
90  DocumentDefinition dd = it.next();
91  //if(dd.getRelatedLineTypes().size() > 1) {
92  //showLineTypeInput = true;
93  //}
94  Collection<Contract> stores = new DocumentDefinitionWrapper(dd).getRelatedStores(document.getContract());
95  if(stores.isEmpty()) {
96  stores = new DocumentDefinitionWrapper(dd)
98  }
100  new ContractWrapper(document.getContract()).addAdvanceIfNeeded(document.getForcedView(), stores);
101  //if(stores.size() > 1) {
102  //showStoreInput = true;
103  //}
104  storeMap.put(dd.getId(), stores);
105  lastStoreMap.put(dd, CollectionUtil.from(stores).first());
106  if(lastDocDef == null) {
107  lastDocDef = dd;
108  }
109  }
110  setCollection(SingleLineDocumentWrapper.adapt(document, document.getUpdatedDescendants()));
111  }
112 
113  public void notifyChange() {
114  Events.postEvent(new Event(Events.ON_CHANGE, this));
115  updateDocument();
116  afterCompose();
117  }
118 
119  public void updateLines() {
120  if(document == null || getValues().isEmpty()) {
121  updateDocument();
122  } else {
123  clearRows();
125  super.afterCompose();
126  }
127  }
128 
129  @Override
130  protected void initiateRow(Row row, SingleLineDocumentWrapper value) {
131  if(value == null && expiryDefinitions != null && !expiryDefinitions.isEmpty()) {
132  value = new SingleLineDocumentWrapper(document, lastDocDef);
133  //value.setStore(lastStoreMap.get(value.getExpiryDefinition()));
134  if(!Strings.isBlank(document.getDocumentNumber())) {
135  value.setDocumentNumber(document.getDocumentNumber() + "/" + getRowCount());
136  }
137  value.setLineType(null);
138  }
139  row.setValue(value);
140  }
141 
142  @Override
143  protected boolean deleteRow(Row row) {
145  dr.removeRelation();
146  return true;
147  }
148 
149  @Override
150  protected boolean isValid(SingleLineDocumentWrapper v) {
151  return v != null && v.isValid();
152  }
153 
154  @Override
155  public void afterCompose() {
156  if(expiryDefinitions != null && !expiryDefinitions.isEmpty()) {
157  setReadOnly(false);
158  try {
159  addColumns();
160  invalidate();
161  } catch (ClassNotFoundException ex) {
162  Logger.getLogger(DocumentExpiriesGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
163  }
164  } else {
165  setReadOnly(true);
166  }
167  super.afterCompose();
168  }
169 
170  @Override
171  protected String formatCell(EditableCell editableCell, Object value) {
172  if(value instanceof DocumentDefinition) {
174  return dd.getName();
175  } else if(value instanceof Product) {
176  Product p = ((Product) value);
177  return "#" + p.getProductCodeStr() + " " + p.getDescription();
178  } else if(value instanceof ProductByContractor) {
180  return "#" + pbp.getContractorCode() + " " + pbp.getProduct().getDescription();
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(DocumentDefinition.class)) {
196  Object value = getCellValue(editableCell);
198  ddl.setCollection(expiryDefinitions);
199  if(value != null) {
200  ddl.setObjectValue((DocumentDefinition) value);
201  lastDocDef = (DocumentDefinition) value;
202  }
203  ddl.setMold("select");
204  return ddl;
205  } else if(ec.getJavaClass().equals(Product.class)) {
206  Object value = getCellValue(editableCell);
207  ProductCombobox pc = new ProductCombobox();
208  if(value != null) pc.setObjectValue((Product) value);
209  return pc;
210  } else if(ec.getJavaClass().equals(ProductByContractor.class)) {
211  Object value = getCellValue(editableCell);
213  if(value != null) pbpc.setObjectValue((ProductByContractor) value);
214  return pbpc;
215  } else if(ec.getJavaClass().equals(LineType.class)) {
216  SingleLineDocumentWrapper sldw = (SingleLineDocumentWrapper) editableCell.getRow().getValue();
217  Object value = getCellValue(editableCell);
219  if(value != null) {
220  //TODO: check whether is preferable to assign preferences from document line
221  //((LineType) value).setContractPreference(sldw.getContractPreference());
222  ltl.setObjectValue((LineType) value);
223  }
224  ltl.setMold("select");
225  return ltl;
226  } else if(ec.getJavaClass().equals(Contract.class)) {
227  SingleLineDocumentWrapper sldw = (SingleLineDocumentWrapper) editableCell.getRow().getValue();
228  Object value = getCellValue(editableCell);
230  new ContractUsualSet(storeMap.get(sldw.getExpiryDefinition().getId()), true,
231  sldw.getTarget().getUsualPath() + "st:"));
232  if(value != null) {
233  slb.setObjectValue((Contract) value);
234  }
235  slb.setMold("select");
236  return slb;
237  } else if(ec.getJavaClass().equals(String.class) && "concept".equals(ec.getProperty())) {
238  SingleLineDocumentWrapper sldw = (SingleLineDocumentWrapper) editableCell.getRow().getValue();
239  Object value = getCellValue(editableCell);
240  ConceptCombobox slb = new ConceptCombobox();
243  if(value != null) slb.setObjectValue((String) value);
244  return slb;
245  } else if("price".equals(ec.getProperty())) {
246  SingleLineDocumentWrapper sldw = (SingleLineDocumentWrapper) editableCell.getRow().getValue();
247  if(sldw.getTotal() == 0) {
248  sldw.setPrice(getCurrentDifference());
249  }
250  return super.createEditor(editableCell);
251  }
252  }
253  return super.createEditor(editableCell);
254  }
255 
256  @Override
257  protected void cellChanged(EditableCell editableCell, Object value) {
258  super.cellChanged(editableCell, value);
259  updateRow(editableCell.getRow());
260  if(value instanceof DocumentDefinition) {
261  SingleLineDocumentWrapper sldw = (SingleLineDocumentWrapper) editableCell.getRow().getValue();
262  if(((DocumentDefinition) value).getId() != lastDocDef.getId()) {
263  lastDocDef = (DocumentDefinition) value;
264  sldw.getExpiryDefinition();
265  sldw.setExpiryDefinition(lastDocDef);
266  sldw.setStore(null);
267  sldw.setLineType(null);
268  }
269  } else if(value instanceof Contract) {
270  SingleLineDocumentWrapper sldw = (SingleLineDocumentWrapper) editableCell.getRow().getValue();
271  lastStoreMap.put(sldw.getExpiryDefinition(), (Contract) value);
272  }
273  }
274 
275  private void addColumns() throws ClassNotFoundException {
276  int fractionDigits = document.getCurrency().getDefaultFractionDigits();
277  if(expiryDefinitions.size() > 1) {
278  addColumn(I_.get("Document"), DocumentDefinition.class,
279  "expiryDefinition", null, 0, false, false).setWidth("280px");
280  }
281  addColumn(I_.get("Date"), Date.class,
282  "documentDate", null, 0, true, false).setWidth("100px");
283  addColumn(I_.get("Number"), String.class,
284  "documentNumber", null, 0, false, false).setWidth("100px");
285  for(RelatedColumn dc : getDocumentColumns()) {
286  if(DocumentColumn.DCOL_PRODUCT.equals(dc.getDocumentColumn())) {
287  if(document.getContract().isUseContractorCode()) {
288  if(ProductWrapper.hasAvailableContractorProducts(document.getContract())) {
289  addColumn(I_.get("Product / Contractor"), ProductByContractor.class,
290  "productByContractor", null, 0, false, false).setHflex("10");
291  }
292  } else {
293  if(ProductWrapper.hasAvailableProducts()) {
294  addColumn(I_.get("Product"), Product.class,
295  "product", null, 0, false, false).setHflex("10");
296  }
297  }
298  } else if(DocumentColumn.DCOL_CONCEPT.equals(dc.getDocumentColumn())) {
299  addColumn(I_.get("Concept"), String.class,
300  "concept", null, 0, false, false).setHflex("10");
301  } else if(DocumentColumn.DCOL_QUANTITY.equals(dc.getDocumentColumn())) {
302  addColumn(I_.get("Quantity"), "double",
303  "quantity", null, 3, false, false).setHflex("3");
304  } else if(DocumentColumn.DCOL_PRICE.equals(dc.getDocumentColumn())) {
305  addColumn(I_.get("Price"), "double",
306  "price", null, fractionDigits, false, false).setHflex("4");
307  } else if(DocumentColumn.DCOL_MONEY_DISCOUNT.equals(dc.getDocumentColumn())) {
308  addColumn(I_.get("Discount"), "double",
309  "discountMoney", null, fractionDigits, false, false).setHflex("2");
310  } else if(DocumentColumn.DCOL_PERCENT_DISCOUNT.equals(dc.getDocumentColumn())) {
311  addColumn(I_.get("% Dnt."), "double",
312  "discountPerCent", null, 0, false, false).setHflex("2");
313  } else if(DocumentColumn.DCOL_TAX.equals(dc.getDocumentColumn())) {
314  addColumn(I_.get("Tax"), "double",
315  "tax", null, 2, false, false).setHflex("2");
316  } else if(DocumentColumn.DCOL_AMOUNT.equals(dc.getDocumentColumn())) {
317  addColumn(I_.get("Amount"), "double",
318  "subtotal", null, fractionDigits, false, true).setHflex("4");
319  }
320  }
321  //if(showStoreInput) {
322  addColumn(I_.get("Store"), Contract.class,
323  "store", null, 0, false, false).setHflex("5");
324  //}
325  //if(showLineTypeInput) {
326  addColumn(I_.get("Line type"), LineType.class,
327  "lineType", null, 0, false, false).setHflex("5");
328  //}
329  }
330 
331  private Collection<RelatedColumn> getDocumentColumns() {
332  return expiryDefinitions.iterator().next().getDocumentColumns();
333  }
334 
335  private Double getCurrentDifference() {
336  double total = 0;
337  for(SingleLineDocumentWrapper sldw : getValues()) {
338  total += sldw.getTotal();
339  }
340  return document.getTotalAmount() - total;
341  }
342 
343 }
void setDocumentDefinition(DocumentDefinition documentDefinition)
void initiateRow(Row row, SingleLineDocumentWrapper value)
void cellChanged(EditableCell editableCell, Object value)
HtmlBasedComponent createEditor(EditableCell editableCell)
String formatCell(EditableCell editableCell, Object value)
Set< DocumentRelation > getDescendants()
Definition: Document.java:139
Set< DocumentRelation > getUpdatedDescendants()
Definition: Document.java:147
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167
static Collection< SingleLineDocumentWrapper > adapt(Document source, Collection< DocumentRelation > relations)
static String get(String msg)
Definition: I_.java:41
static String getMostUsedvalue(String path)
void setCollection(Collection< V > collection)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
Object getCellValue(EditableCell editableCell)
void setReadOnly(boolean readOnly)
void setCollection(Collection< V > collection)