BrightSide Workbench Full Report + Source Code
DocumentByContractGrid.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.elephant.util.DateFormats;
23 import org.turro.elephant.util.DecimalFormats;
24 import org.turro.financials.command.DocumentLabel;
25 import org.turro.financials.entity.Contract;
26 import org.turro.financials.entity.Document;
27 import org.turro.financials.entity.DocumentRelation;
28 import org.turro.financials.entity.RegisterView;
29 import org.turro.financials.model.document.contract.DocumentByContractList;
30 import org.turro.i18n.I_;
31 import org.turro.zkoss.grid.PagingGrid;
32 import org.turro.zkoss.label.LabelTypes;
33 import org.turro.zkoss.label.StyledLabel;
34 import org.turro.zkoss.layout.GridLayout;
35 import org.zkoss.zk.ui.event.Event;
36 import org.zkoss.zk.ui.event.EventListener;
37 import org.zkoss.zk.ui.event.Events;
38 import org.zkoss.zul.*;
39 
44 public class DocumentByContractGrid extends PagingGrid {
45 
46  private Contract contract;
47  private RegisterView view = null;
48  private Date from, to;
49 
51  from = new CheckDate(-1, 1, 1, 0, 0, 0).getDate();
52  to = new Date();
53  }
54 
55  public Date getFrom() {
56  return from;
57  }
58 
59  public Date getTo() {
60  return to;
61  }
62 
63  public void setView(RegisterView view) {
64  this.view = view;
65  }
66 
67  public void setContract(Contract contract) {
68  this.contract = contract;
69  }
70 
71  public void setFrom(Date from) {
72  this.from = from;
73  }
74 
75  public void setTo(Date to) {
76  this.to = to;
77  }
78 
79  public void update() {
80  addCols();
81  addRows();
82  }
83 
84  private void addCols() {
85  Columns cols = getColumns(true);
86  cols.getChildren().clear();
87 
88  addDetailColumn(new Image("/_zul/images/relation.png"));
89 
90  Column col = new Column(I_.get("Date"));
91  col.setWidth("90px");
92  cols.appendChild(col);
93 
94  col = new Column(I_.get("Concept"));
95  cols.appendChild(col);
96 
97  col = new Column(I_.get("Invoice"));
98  col.setWidth("120px");
99  col.setAlign("right");
100  cols.appendChild(col);
101 
102  col = new Column(I_.get("Portfolio"));
103  col.setWidth("120px");
104  col.setAlign("right");
105  cols.appendChild(col);
106 
107  col = new Column(I_.get("Balance"));
108  col.setWidth("120px");
109  col.setAlign("right");
110  cols.appendChild(col);
111 
112  col = new Column(I_.get("Settlement"));
113  col.setWidth("120px");
114  col.setAlign("right");
115  cols.appendChild(col);
116 
117  col = new Column(I_.get("Balance"));
118  col.setWidth("120px");
119  col.setAlign("right");
120  cols.appendChild(col);
121  }
122 
123  private void addRows() {
124  Rows rows = getRows(true);
125  rows.getChildren().clear();
126 
127  DocumentByContractList dbcl = new DocumentByContractList();
128  dbcl.fillList(view, contract, from, to);
129 
130  double portfolio_balance = 0.0, final_balance = dbcl.getInitialBalance(view, contract, from, to);
131  int count = 0;
132 
133  if(final_balance != 0.0) {
134  count++;
135 
136  Row row = new Row();
137  rows.appendChild(row);
138 
139  final Detail d = new Detail();
140  row.appendChild(d);
141  row.appendChild(new Space());
142  row.appendChild(new Label(I_.get("Accumulated")));
143  row.appendChild(new Space());
144  row.appendChild(new Space());
145  row.appendChild(new Space());
146  row.appendChild(new Space());
147  row.appendChild(new Label(DecimalFormats.format(final_balance,
149  }
150 
151  for(final Document doc : dbcl) {
152 
153  if(doc.getDocumentDefinition().getId() != 1 &&
154  doc.getDocumentDefinition().getId() != 67 &&
155  doc.getDocumentDefinition().getId() != 2 &&
156  doc.getDocumentDefinition().getId() != 43 &&
157  doc.getDocumentDefinition().getId() != 44 &&
158  doc.getDocumentDefinition().getId() != 6 &&
159  doc.getDocumentDefinition().getId() != 7) {
160  continue;
161  }
162 
163  count++;
164 
165  Row row = new Row();
166  rows.appendChild(row);
167 
168  final Detail d = new Detail();
169  row.appendChild(d);
170  d.addEventListener(Events.ON_OPEN, new EventListener() {
171  @Override
172  public void onEvent(Event event) throws Exception {
173  if(d.getChildren().isEmpty() && !doc.getDescendants().isEmpty()) {
174  Vbox vbox = new Vbox();
175  vbox.setClass("softLabel");
176  vbox.setStyle("margin:3px;margin-left:150px;");
177  d.appendChild(vbox);
178  GridLayout gl = new GridLayout("min,right-120px");
179  vbox.appendChild(gl);
180  double total = 0.0;
181  for(DocumentRelation dr : doc.getDescendants()) {
182  Document dd = dr.getDescendant();
183  A b = new DocumentLabel(dd);
184  gl.addRow();
185  gl.addComponent(b);
186  double tdd = dd.getTotalAmount();
187  gl.addComponent(new Label(DecimalFormats.format(tdd,
188  DecimalFormats.getStringFormat(2))));
189  total += tdd;
190  }
191  gl.addRow();
192  gl.addSpace();
193  gl.addComponent(new StyledLabel(DecimalFormats.format(total,
194  DecimalFormats.getStringFormat(2)), "font-weight:bold"));
195  }
196  }
197  });
198 
199  row.appendChild(new Label(DateFormats.format(doc.getDocumentDate(), true)));
200 
201  Vlayout vbox = new Vlayout();
202  vbox.setStyle("padding:2px;padding-left:20px");
203  row.appendChild(vbox);
204  A b = new DocumentLabel(doc);
205  vbox.appendChild(b);
206  vbox.appendChild(LabelTypes.getSoftLabel(doc.getConcept()));
207  vbox.appendChild(LabelTypes.getSoftLabel(doc.getNotes()));
208 
209  double amount = doc.getTotalAmount();
210 
211  if(doc.getDocumentDefinition().getId() == 1 ||
212  doc.getDocumentDefinition().getId() == 67 ||
213  doc.getDocumentDefinition().getId() == 2) {
214  row.appendChild(new Label(DecimalFormats.format(amount,
215  DecimalFormats.getStringFormat(2))));
216  row.appendChild(new Cell());
217  portfolio_balance += amount;
218  row.appendChild(new Label(DecimalFormats.format(portfolio_balance,
219  DecimalFormats.getStringFormat(2))));
220  row.appendChild(new Cell());
221  final_balance += amount;
222  row.appendChild(new Label(DecimalFormats.format(final_balance,
223  DecimalFormats.getStringFormat(2))));
224  } else if(doc.getDocumentDefinition().getId() == 44 ||
225  doc.getDocumentDefinition().getId() == 43) {
226  row.appendChild(new Cell());
227  row.appendChild(new Label(DecimalFormats.format(amount,
228  DecimalFormats.getStringFormat(2))));
229  portfolio_balance -= amount;
230  row.appendChild(new Label(DecimalFormats.format(portfolio_balance,
231  DecimalFormats.getStringFormat(2))));
232  row.appendChild(new Cell());
233  row.appendChild(new Label(DecimalFormats.format(final_balance,
234  DecimalFormats.getStringFormat(2))));
235  } else if(doc.getDocumentDefinition().getId() == 6 ||
236  doc.getDocumentDefinition().getId() == 7) {
237  row.appendChild(new Cell());
238  row.appendChild(new Cell());
239  row.appendChild(new Label(DecimalFormats.format(portfolio_balance,
240  DecimalFormats.getStringFormat(2))));
241  row.appendChild(new Label(DecimalFormats.format(amount,
242  DecimalFormats.getStringFormat(2))));
243  final_balance -= amount;
244  row.appendChild(new Label(DecimalFormats.format(final_balance,
245  DecimalFormats.getStringFormat(2))));
246  }
247 
248  }
249 
250  setRowCount(count);
251  }
252 
253 }
static String format(Number value, String pattern)
static String getStringFormat(int fractionDigits)
static String get(String msg)
Definition: I_.java:41
Columns getColumns(boolean create)
Rows getRows(boolean create)