BrightSide Workbench Full Report + Source Code
PayrollExpiriesGrid.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.payroll;
19 
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.string.Strings;
26 import org.turro.collections.CollectionUtil;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.financials.contract.ContractListbox;
29 import org.turro.financials.contract.logic.ContractWrapper;
30 import org.turro.financials.db.FinancialsPU;
31 import org.turro.financials.document.logic.DocumentDefinitionWrapper;
32 import org.turro.financials.entity.Contract;
33 import org.turro.financials.entity.Document;
34 import org.turro.financials.entity.DocumentDefinition;
35 import org.turro.financials.entity.DocumentRelation;
36 import org.turro.financials.entity.LineType;
37 import org.turro.financials.model.contract.ContractUsualSet;
38 import org.turro.financials.model.document.SingleLineDocumentWrapper;
39 import org.turro.i18n.I_;
40 import org.turro.jpa.Dao;
41 import org.turro.zkoss.grid.CollectionGrid;
42 import org.turro.zkoss.grid.EditableCell;
43 import org.turro.zkoss.grid.EditableColumn;
44 import org.zkoss.zk.ui.HtmlBasedComponent;
45 import org.zkoss.zk.ui.event.Event;
46 import org.zkoss.zk.ui.event.Events;
47 import org.zkoss.zul.Row;
48 
53 public class PayrollExpiriesGrid extends CollectionGrid<SingleLineDocumentWrapper> {
54 
55  private Document document;
56  private DocumentDefinition expiryDefinition;
57  private LineType lineType;
58  private Collection<Contract> stores;
59  private Contract lastStore;
60 
62  super();
63  }
64 
65  public Collection<DocumentRelation> getRelations() {
66  ArrayList<DocumentRelation> list = new ArrayList<DocumentRelation>();
67  for(SingleLineDocumentWrapper sldw : getValues()) {
68  list.add(sldw.getRelation());
69  }
70  return list;
71  }
72 
73  public Document getDocument() {
74  return document;
75  }
76 
77  public void setDocument(Document document) {
78  this.document = document;
79  Dao dao = new FinancialsPU();
80  expiryDefinition = dao.find(DocumentDefinition.class, 8L);
81  lineType = dao.find(LineType.class, 7L);
82  stores = new DocumentDefinitionWrapper(expiryDefinition)
83  .getRelatedStores(document.getContract());
84  if(stores.isEmpty()) {
85  stores = new DocumentDefinitionWrapper(expiryDefinition)
87  }
89  lastStore = CollectionUtil.from(stores).first();
91  }
92 
93  public void notifyChange() {
94  Events.postEvent(new Event(Events.ON_CHANGE, this));
95  updateBox();
96  }
97 
98  public void updateBox() {
99  clearTable();
100  setDocument(document);
101  afterCompose();
102  }
103 
104  public void updateLines() {
105  clearRows();
106  setDocument(document);
107  super.afterCompose();
108  }
109 
110  @Override
111  protected void initiateRow(Row row, SingleLineDocumentWrapper value) {
112  if(value == null) {
113  value = new SingleLineDocumentWrapper(document, expiryDefinition);
114  //value.setStore(lastStore);
115  if(!Strings.isBlank(document.getDocumentNumber())) {
116  value.setDocumentNumber(document.getDocumentNumber() + "/" + getRowCount());
117  }
118  value.setLineType(lineType);
120  }
121  row.setValue(value);
122  }
123 
124  @Override
125  protected boolean deleteRow(Row row) {
127  dr.removeRelation();
128  return true;
129  }
130 
131  @Override
132  protected boolean isValid(SingleLineDocumentWrapper v) {
133  return v != null && v.isValid();
134  }
135 
136  @Override
137  public void afterCompose() {
138  setReadOnly(false);
139  try {
140  addColumns();
141  } catch (ClassNotFoundException ex) {
142  Logger.getLogger(PayrollExpiriesGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
143  }
144  super.afterCompose();
145  getColumns().setVisible(false);
146  }
147 
148  @Override
149  protected String formatCell(EditableCell editableCell, Object value) {
150  if(value instanceof DocumentDefinition) {
152  return dd.getName();
153  } else if(value instanceof LineType) {
154  LineType lt = ((LineType) value);
155  return lt.getName();
156  } else if(value instanceof Contract) {
157  Contract ctc = ((Contract) value);
158  return ctc.getPartialDescription();
159  }
160  return super.formatCell(editableCell, value);
161  }
162 
163  @Override
164  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
165  if(editableCell.getColumn() instanceof EditableColumn) {
166  EditableColumn ec = (EditableColumn) editableCell.getColumn();
167  if(ec.getJavaClass().equals(Contract.class)) {
168  SingleLineDocumentWrapper sldw = (SingleLineDocumentWrapper) editableCell.getRow().getValue();
169  Object value = getCellValue(editableCell);
171  new ContractUsualSet(stores, true,
172  sldw.getTarget().getUsualPath() + "st:"));
173  if(value != null) {
174  slb.setObjectValue((Contract) value);
175  }
176  slb.setMold("select");
177  return slb;
178  } else if("price".equals(ec.getProperty())) {
179  SingleLineDocumentWrapper sldw = (SingleLineDocumentWrapper) editableCell.getRow().getValue();
180  if(sldw.getTotal() == 0) {
181  sldw.setPrice(getCurrentDifference());
182  }
183  return super.createEditor(editableCell);
184  }
185  }
186  return super.createEditor(editableCell);
187  }
188 
189  @Override
190  protected void cellChanged(EditableCell editableCell, Object value) {
191  super.cellChanged(editableCell, value);
192  updateRow(editableCell.getRow());
193  if(value instanceof Contract) {
194  lastStore = (Contract) value;
195  }
196  }
197 
198  private void addColumns() throws ClassNotFoundException {
199  int fractionDigits = document.getCurrency().getDefaultFractionDigits();
200  addColumn(I_.get("Date"), Date.class,
201  "documentDate", null, 0, true, false).setWidth("80px");
202  addColumn(I_.get("Price"), "double",
203  "price", null, fractionDigits, false, false).setWidth("70px");
204  addColumn(I_.get("Store"), Contract.class,
205  "store", null, 0, false, false).setWidth("80px");
206  setSpan(true);
207  }
208 
209  private Double getCurrentDifference() {
210  double total = 0;
211  for(SingleLineDocumentWrapper sldw : getValues()) {
212  total += sldw.getTotal();
213  }
214  return document.getTotalAmount() - total;
215  }
216 
217 }
Set< DocumentRelation > getDescendants()
Definition: Document.java:139
ContractPreference getDefaultContractPreference()
Definition: Document.java:429
void setContractPreference(ContractPreference contractPreference)
Definition: LineType.java:81
static Collection< SingleLineDocumentWrapper > adapt(Document source, Collection< DocumentRelation > relations)
void initiateRow(Row row, SingleLineDocumentWrapper value)
void cellChanged(EditableCell editableCell, Object value)
String formatCell(EditableCell editableCell, Object value)
boolean isValid(SingleLineDocumentWrapper v)
HtmlBasedComponent createEditor(EditableCell editableCell)
static String get(String msg)
Definition: I_.java:41
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)
Columns getColumns(boolean create)