BrightSide Workbench Full Report + Source Code
DocumentByContractViewerGrid.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.contract;
19 
20 import java.util.Date;
21 import org.amic.util.date.CheckDate;
22 import org.turro.auth.Authentication;
23 import org.turro.elephant.context.Application;
24 import org.turro.elephant.util.DateFormats;
25 import org.turro.elephant.util.DecimalFormats;
26 import org.turro.financials.entity.Contract;
27 import org.turro.financials.entity.Document;
28 import org.turro.financials.entity.RegisterView;
29 import org.turro.financials.model.document.contract.DocumentByContractList;
30 import org.turro.financials.view.ViewListbox;
31 import org.turro.i18n.I_;
32 import org.turro.plugin.contacts.IContact;
33 import org.turro.zkoss.grid.PagingGrid;
34 import org.turro.zkoss.input.DateboxShort;
35 import org.turro.zkoss.layout.GridLayout;
36 import org.zkoss.zk.ui.event.Event;
37 import org.zkoss.zk.ui.event.EventListener;
38 import org.zkoss.zk.ui.event.Events;
39 import org.zkoss.zk.ui.ext.AfterCompose;
40 import org.zkoss.zul.Button;
41 import org.zkoss.zul.Cell;
42 import org.zkoss.zul.Column;
43 import org.zkoss.zul.Columns;
44 import org.zkoss.zul.Label;
45 import org.zkoss.zul.Row;
46 import org.zkoss.zul.Rows;
47 import org.zkoss.zul.Space;
48 
53 public class DocumentByContractViewerGrid extends PagingGrid implements AfterCompose {
54 
55  private GridLayout toolbar;
56  private Button searchButton;
57  private IContact contact;
58  private boolean searchBar = true;
59  private RegisterView view;
60  private Contract contract;
61  private Date from, to;
62 
64  addColumns();
65  from = new CheckDate(-1, 1, 1, 0, 0, 0).getDate();
66  to = new Date();
67  }
68 
69  public void setContact(IContact contact) {
70  this.contact = contact;
71  }
72 
73  public void setSearchBar(boolean searchBar) {
74  this.searchBar = searchBar;
75  }
76 
77  private void addColumns() {
78  Columns cols = new Columns();
79  appendChild(cols);
80 
81  Column col = new Column(I_.get("Date"));
82  col.setWidth("90px");
83  cols.appendChild(col);
84 
85  col = new Column(I_.get("Concept"));
86  col.setHflex("1");
87  cols.appendChild(col);
88 
89  col = new Column(I_.get("Invoice"));
90  col.setWidth("120px");
91  col.setAlign("right");
92  cols.appendChild(col);
93 
94  col = new Column(I_.get("Portfolio"));
95  col.setWidth("120px");
96  col.setAlign("right");
97  cols.appendChild(col);
98 
99  col = new Column(I_.get("Settlement"));
100  col.setWidth("120px");
101  col.setAlign("right");
102  cols.appendChild(col);
103 
104  col = new Column(I_.get("Balance"));
105  col.setWidth("120px");
106  col.setAlign("right");
107  cols.appendChild(col);
108 
109  col = new Column();
110  col.setWidth("50px");
111  cols.appendChild(col);
112  }
113 
114  private void addRows() {
115  Rows rows = getRows(true);
116  rows.getChildren().clear();
117 
118  if(contract == null) return;
119 
120  DocumentByContractList dbcl = new DocumentByContractList();
121  dbcl.fillList(view, contract, from, to);
122 
123  double portfolio_balance = 0.0, final_balance = dbcl.getInitialBalance(view, contract, from, to);
124  int count = 0;
125 
126  if(final_balance != 0.0) {
127  count++;
128 
129  Row row = new Row();
130  rows.appendChild(row);
131 
132  row.appendChild(new Space());
133  row.appendChild(new Label(I_.get("Accumulated")));
134  row.appendChild(new Space());
135  row.appendChild(new Space());
136  //row.appendChild(new Space());
137  row.appendChild(new Space());
138  row.appendChild(new Label(DecimalFormats.format(final_balance,
140  row.appendChild(new Space());
141  }
142 
143  for(final Document doc : dbcl) {
144 
145  if(doc.getDocumentDefinition().getId() != 1 &&
146  doc.getDocumentDefinition().getId() != 67 &&
147  doc.getDocumentDefinition().getId() != 2 &&
148  doc.getDocumentDefinition().getId() != 43 &&
149  doc.getDocumentDefinition().getId() != 44 &&
150  doc.getDocumentDefinition().getId() != 6 &&
151  doc.getDocumentDefinition().getId() != 7) {
152  continue;
153  }
154 
155  count++;
156 
157  Row row = new Row();
158  rows.appendChild(row);
159 
160  row.appendChild(new Label(DateFormats.format(doc.getDocumentDate(), true)));
161 
162  row.appendChild(new Label(doc.getDocumentDefinition().getName() + " " + doc.getDocumentNumber()));
163 
164  double amount = doc.getTotalAmount();
165 
166  if(doc.getDocumentDefinition().getId() == 1 ||
167  doc.getDocumentDefinition().getId() == 67 ||
168  doc.getDocumentDefinition().getId() == 2) {
169  row.appendChild(new Label(DecimalFormats.format(amount,
170  DecimalFormats.getStringFormat(2))));
171  row.appendChild(new Cell());
172  portfolio_balance += amount;
173 // row.appendChild(new Label(DecimalFormats.format(portfolio_balance,
174 // DecimalFormats.getStringFormat(2))));
175  row.appendChild(new Cell());
176  final_balance += amount;
177  row.appendChild(new Label(DecimalFormats.format(final_balance,
178  DecimalFormats.getStringFormat(2))));
179  } else if(doc.getDocumentDefinition().getId() == 44 ||
180  doc.getDocumentDefinition().getId() == 43) {
181  row.appendChild(new Cell());
182  row.appendChild(new Label(DecimalFormats.format(amount,
183  DecimalFormats.getStringFormat(2))));
184  portfolio_balance -= amount;
185 // row.appendChild(new Label(DecimalFormats.format(portfolio_balance,
186 // DecimalFormats.getStringFormat(2))));
187  row.appendChild(new Cell());
188  row.appendChild(new Label(DecimalFormats.format(final_balance,
189  DecimalFormats.getStringFormat(2))));
190  } else if(doc.getDocumentDefinition().getId() == 6 ||
191  doc.getDocumentDefinition().getId() == 7) {
192  row.appendChild(new Cell());
193  row.appendChild(new Cell());
194 // row.appendChild(new Label(DecimalFormats.format(portfolio_balance,
195 // DecimalFormats.getStringFormat(2))));
196  row.appendChild(new Label(DecimalFormats.format(amount,
197  DecimalFormats.getStringFormat(2))));
198  final_balance -= amount;
199  row.appendChild(new Label(DecimalFormats.format(final_balance,
200  DecimalFormats.getStringFormat(2))));
201  }
202 
203  DocumentReport dr = new DocumentReport();
204  dr.setDocument(doc);
205  dr.setShowLabels(false);
206  row.appendChild(dr);
207  dr.afterCompose();
208  }
209 
210  setRowCount(count);
211  }
212 
213  private void addSearchBar() {
214  Application app = Application.getApplication();
215 
216  if(toolbar != null) getParent().removeChild(toolbar);
217 
218  if(!searchBar) return;
219 
220  toolbar = new GridLayout("min,1");
221  getParent().insertBefore(toolbar, this);
222  toolbar.setStyle("font-size:11px");
223 
224  toolbar.addCaption(I_.get("Contract"));
225  if(app.isInRole("finan-contract:list")) {
226  final DocumentsByContractCombobox dbc = new DocumentsByContractCombobox();
227  dbc.setHflex("1");
228  dbc.addEventListener(Events.ON_CHANGE, new EventListener() {
229  @Override
230  public void onEvent(Event event) throws Exception {
231  contract = dbc.getObjectValue();
232  }
233  });
234  toolbar.addComponent(dbc);
235  } else {
236  final ContractByInterventionListbox cbi = new ContractByInterventionListbox();
237  cbi.setContact(Authentication.getIContact());
238  cbi.setMold("select");
239  cbi.setAllowNull(true);
240  cbi.addEventListener(Events.ON_SELECT, new EventListener() {
241  @Override
242  public void onEvent(Event event) throws Exception {
243  contract = cbi.getObjectValue();
244  }
245  });
246  toolbar.addComponent(cbi);
247  }
248 
249  if(app.isInRole("finan-view:list")) {
250  toolbar.addRow();
251  toolbar.addCaption(I_.get("View"));
252  final ViewListbox vl = new ViewListbox();
253  vl.setMold("select");
254  vl.setAllowNull(true);
255  toolbar.addComponent(vl);
256  vl.addEventListener(Events.ON_SELECT, new EventListener() {
257  @Override
258  public void onEvent(Event event) throws Exception {
259  view = vl.getObjectValue();
260  }
261  });
262  }
263 
264  toolbar.addRow();
265  toolbar.addCaption(I_.get("From"));
266  final DateboxShort fromInput = new DateboxShort(from);
267  fromInput.addEventListener(Events.ON_CHANGE, new EventListener() {
268  @Override
269  public void onEvent(Event event) throws Exception {
270  from = fromInput.getValue();
271  }
272  });
273  toolbar.addComponent(fromInput);
274 
275  toolbar.addRow();
276  toolbar.addCaption(I_.get("To"));
277  final DateboxShort toInput = new DateboxShort(to);
278  fromInput.addEventListener(Events.ON_CHANGE, new EventListener() {
279  @Override
280  public void onEvent(Event event) throws Exception {
281  to = toInput.getValue();
282  }
283  });
284  toolbar.addComponent(toInput);
285 
286  searchButton = new Button(
287  I_.get("Search"),
288  "/_zul/images/search.png"
289  );
290  searchButton.addEventListener(Events.ON_CLICK, new EventListener() {
291  @Override
292  public void onEvent(Event event) throws Exception {
293  addRows();
294  }
295  });
296  toolbar.addRow();
297  toolbar.addSpace();
298  toolbar.addComponent(searchButton);
299 
300  }
301 
302  @Override
303  public void afterCompose() {
304  addSearchBar();
305  addRows();
306  }
307 
308 }
static String format(Number value, String pattern)
static String getStringFormat(int fractionDigits)
static String get(String msg)
Definition: I_.java:41
Rows getRows(boolean create)
GridLayout addComponent(HtmlBasedComponent comp)
GridLayout addCaption(String label)