BrightSide Workbench Full Report + Source Code
PayrollGrid.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.Collection;
21 import java.util.Date;
22 import org.turro.financials.contract.ContractListbox;
23 import org.turro.financials.document.DocumentTotal;
24 import org.turro.financials.document.logic.DocumentDefinitionWrapper;
25 import org.turro.financials.entity.Contract;
26 import org.turro.financials.entity.DocumentLine;
27 import org.turro.financials.model.contract.ContractUsualSet;
28 import org.turro.financials.model.contract.ContractWrapper;
29 import org.turro.financials.model.document.PayrollWrapper;
30 import org.turro.zkoss.grid.PagingGrid;
31 import org.turro.zkoss.input.ExpressionInput;
32 import org.turro.zkoss.label.LabelTypes;
33 import org.zkoss.zk.ui.Component;
34 import org.zkoss.zk.ui.event.Event;
35 import org.zkoss.zk.ui.event.EventListener;
36 import org.zkoss.zk.ui.event.Events;
37 import org.zkoss.zk.ui.ext.AfterCompose;
38 import org.zkoss.zul.*;
39 
44 public class PayrollGrid extends PagingGrid implements AfterCompose {
45 
46  private final PayrollFilter filter = new PayrollFilter();
47  private boolean newPayrolls = true;
48  private Date date = new Date();
49 
51  return filter;
52  }
53 
54  public Date getDate() {
55  return date;
56  }
57 
58  public void setDate(Date date) {
59  this.date = date;
60  for(Component row : getRows().getChildren()) {
61  PayrollWrapper pr = (PayrollWrapper) ((Row)row).getValue();
62  pr.getPayroll().setDocumentDate(date);
63  pr.getPayroll().setReceiptDate(date);
64  }
65  }
66 
67  public boolean isNewPayrolls() {
68  return newPayrolls;
69  }
70 
71  public void setNewPayrolls(boolean newPayrolls) {
72  this.newPayrolls = newPayrolls;
73  }
74 
75  public void save() {
76  for(Component row : getRows().getChildren()) {
77  PayrollWrapper pr = (PayrollWrapper) ((Row)row).getValue();
78  pr.save();
79  }
80  }
81 
82  public void reload() {
83  addRows();
84  }
85 
86  @Override
87  public void afterCompose() {
88  addColumns();
89  addRows();
90  }
91 
92  private void addRows() {
93  Collection<PayrollWrapper> list = new PayrollAdapter(filter.getPayrolls(date, newPayrolls));
94 
95  Rows rows = getRows(true);
96  rows.getChildren().clear();
97 
98  for(final PayrollWrapper pr : list) {
99  if(newPayrolls) {
100  pr.getPayroll().setDocumentDate(date);
101  pr.getPayroll().setReceiptDate(date);
102  pr.getPayroll().setDocumentNumber(null);
103  } else {
104  date = pr.getPayroll().getDocumentDate();
105  }
106  Row row = new Row();
107  row.setValue(pr);
108  row.setValign("top");
109  rows.appendChild(row);
110  pr.getPayroll().setDraft(false);
111  if(pr.getPayroll().isDraft()) {
112  row.setSclass("draft");
113  }
114 
115  Vbox vbox = new Vbox();
116  row.appendChild(vbox);
117 
118  final ContractListbox cl = new ContractListbox(ContractWrapper.getContracts(26L));
119  cl.setMold("select");
120  cl.setObjectValue(pr.getPayroll().getContract());
121  cl.addEventListener(Events.ON_SELECT, new EventListener() {
122  @Override
123  public void onEvent(Event event) throws Exception {
124  pr.getPayroll().setContract(cl.getObjectValue());
125  }
126  });
127  vbox.appendChild(cl);
128  cl.afterCompose();
129 
130  Grid grid = new Grid();
131  row.appendChild(grid);
132 
133  DocumentTotal total = new DocumentTotal();
134  addLine(grid, pr.getPayrollLine(), total);
135  addLine(grid, pr.getInsuranceLine(), total);
136  addLine(grid, pr.getRetentionLine(), total);
137  addLine(grid, pr.getSeizureLine(), total);
138  addLine(grid, pr.getProtectionLine(), total);
139  addLine(grid, pr.getContingenciesLine(), total);
140  addLine(grid, total);
141  total.setDocument(pr.getPayroll());
142 
143  PayrollExpiriesGrid expiries = new PayrollExpiriesGrid();
144  expiries.setDocument(pr.getPayroll());
145  expiries.setAllowInsertions(true);
146  expiries.setAllowDeletions(true);
147  expiries.setDefaultBehavior(false);
148  row.appendChild(expiries);
149  expiries.afterCompose();
150  }
151 
152  setRowCount(list.size());
153  }
154 
155  private void addLine(Grid grid, final DocumentLine line, final DocumentTotal total) {
156  Rows rows = grid.getRows();
157  if(rows == null) {
158  rows = new Rows();
159  grid.appendChild(rows);
160  }
161 
162  Row row = new Row();
163  rows.appendChild(row);
164 
165  row.appendChild(LabelTypes.getSoftLabel(line.getLineType().getName()));
166  final ExpressionInput ei = new ExpressionInput();
167  ei.setWidth("70px");
168  ei.setValue(line.getPrice());
169  ei.addEventListener(Events.ON_CHANGE, new EventListener() {
170  @Override
171  public void onEvent(Event event) throws Exception {
172  line.setPrice(((Number) ei.getNumber(2)).doubleValue());
173  total.setDocument(line.getDocument());
174  }
175  });
176  row.appendChild(ei);
177 
178  Collection<Contract> stores = new DocumentDefinitionWrapper(line.getDocument().getDocumentDefinition())
179  .getRelatedStores(line.getDocument().getContract());
180  if(stores.isEmpty()) {
181  stores = new DocumentDefinitionWrapper(line.getDocument().getDocumentDefinition())
182  .getRelatedStores();
183  }
184  ContractWrapper.clearInactives(stores);
185  final ContractListbox slb = new ContractListbox(new ContractUsualSet(stores, true, line.getDocument().getUsualPath() + "st:"));
186  if(line.getStore() != null) slb.setObjectValue((Contract) line.getStore());
187  slb.setMold("select");
188  slb.addEventListener(Events.ON_SELECT, new EventListener() {
189  @Override
190  public void onEvent(Event event) throws Exception {
191  line.setStore(slb.getObjectValue());
192  }
193  });
194  slb.afterCompose();
195  row.appendChild(slb);
196  }
197 
198  private void addLine(Grid grid, DocumentTotal total) {
199  Rows rows = grid.getRows();
200  if(rows == null) {
201  rows = new Rows();
202  grid.appendChild(rows);
203  }
204 
205  Row row = new Row();
206  rows.appendChild(row);
207 
208  row.appendChild(LabelTypes.getEmptyLabel());
209  row.appendChild(total);
210  }
211 
212  private void addColumns() {
213  Columns cols = new Columns();
214  appendChild(cols);
215 
216  cols.appendChild(new Column(null, null, "20%"));
217  cols.appendChild(new Column(null, null, "40%"));
218  cols.appendChild(new Column(null, null, "40%"));
219 
220  cols.setVisible(false);
221  }
222 
223 }
void setDocumentDate(Date documentDate)
Definition: Document.java:163
void setReceiptDate(Date receiptDate)
Definition: Document.java:256
Collection< Document > getPayrolls(Date date, boolean newPayrolls)
void setNewPayrolls(boolean newPayrolls)
Rows getRows(boolean create)