BrightSide Workbench Full Report + Source Code
PortfolioGrid.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.portfolio;
19 
20 import java.util.Map;
21 import org.turro.collections.AggregateCountItem;
22 import org.turro.collections.AggregateCountMap;
23 import org.turro.elephant.util.DateFormats;
24 import org.turro.elephant.util.DecimalFormats;
25 import org.turro.elephant.util.Messages;
26 import org.turro.financials.db.FinancialsPU;
27 import org.turro.financials.document.action.DocumentActionMenu;
28 import org.turro.financials.menu.FinancialsMenu;
29 import org.turro.i18n.I_;
30 import org.turro.zkoss.filter.FilterGrid;
31 import org.turro.zkoss.grid.PagingGrid;
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.zk.ui.util.Clients;
39 import org.zkoss.zul.*;
40 
45 public class PortfolioGrid extends PagingGrid implements AfterCompose {
46 
47  private final PortfolioSet filter = new PortfolioSet();
48  private DocumentActionMenu dam;
49  private FilterGrid filterGrid;
50 
51  public void setFilterGrid(FilterGrid filterGrid) {
52  this.filterGrid = filterGrid;
53  }
54 
56  return filter;
57  }
58 
59  public void reload() {
60  if(getRows() != null) {
61  getRows().detach();
62  }
63  addRows();
64  }
65 
66  public void process() {
67  if(isEmpty()) return;
69  for(Component row : getRows().getChildren()) {
70  PortfolioItem pi = (PortfolioItem) ((Row)row).getValue();
71  if(pi != null) {
72  for(WorkflowItem dw : pi.getForward()) {
73  if(dw.isSelected()) {
74  process.add(new PortfolioAction(pi.getDocument(), dw.getWorkflow(), true, (Row) row));
75  }
76  }
77  for(WorkflowItem dw : pi.getBackward()) {
78  if(dw.isSelected()) {
79  process.add(new PortfolioAction(pi.getDocument(), dw.getWorkflow(), false, (Row) row));
80  }
81  }
82  }
83  }
84  process.doIt(this);
85  }
86 
87  public void count() {
88  if(isEmpty()) return;
89  AggregateCountMap acm = new AggregateCountMap();
90  for(Component row : getRows().getChildren()) {
91  PortfolioItem pi = (PortfolioItem) ((Row)row).getValue();
92  if(pi != null) {
93  for(WorkflowItem dw : pi.getForward()) {
94  if(dw.isSelected()) {
95  acm.sum(dw.getWorkflow().getDescendant().getName(), pi.getDocument().getTotalAmount());
96  }
97  }
98  for(WorkflowItem dw : pi.getBackward()) {
99  if(dw.isSelected()) {
100  acm.sum(dw.getWorkflow().getAncestor().getName(), pi.getDocument().getTotalAmount());
101  }
102  }
103  }
104  }
105  StringBuilder sb = new StringBuilder();
106  for(Map.Entry<String, AggregateCountItem> entry : acm.entrySet()) {
107  sb.append(entry.getKey())
108  .append(": ")
109  .append(String.format("%.2f (%d)",entry.getValue().getAmount(), entry.getValue().getCount()))
110  .append("\n");
111  }
112  Clients.showNotification(sb.toString());
113  }
114 
115  @Override
116  public void afterCompose() {
117  dam = new DocumentActionMenu();
118  getParent().appendChild(dam);
119  addColumns();
120  }
121 
122  private void addRows() {
123  if(filterGrid != null && !filterGrid.hasValues()) return;
124 
125  Rows rows = new Rows();
126  appendChild(rows);
127 
128  if(filterGrid != null) {
129  filter.load(RelationMode.RELATE_BOTH, filterGrid.getValues());
130  }
131 
132  for(final PortfolioItem item : filter) {
133  filter.checkGroup(this, item.getDocument());
134  final Row row = new Row();
135  row.setValue(item);
136  rows.appendChild(row);
137  if(item.getDocument().isDraft()) {
138  row.setSclass("draft");
139  }
140  row.appendChild(new Label(item.getDocument().getId() + ""));
141  row.appendChild(new Label(DateFormats.format(item.getDocument().getReceiptDate(), true)));
142  row.appendChild(new Label(DateFormats.format(item.getDocument().getDocumentDate(), true)));
143  row.appendChild(new Label(item.getDocument().getDocumentNumber()));
144  Vbox vbox = new Vbox();
145  A b = new A(
146  item.getDocument().getDocumentDefinition().getName() +
147  (item.getDocument().getForcedView() == null ? "" : " (" + item.getDocument().getForcedView().getName() + ")"));
148  b.setImage("/_zul/images/document.png");
149  b.addEventListener(Events.ON_CLICK, new EventListener() {
150  @Override
151  public void onEvent(Event event) throws Exception {
152  FinancialsMenu.showDocument(item.getDocument().getId());
153  }
154  });
155  b.setContext(dam);
156  b.setAttribute("document", item.getDocument());
157  row.appendChild(b);
158  vbox.appendChild(b);
159  vbox.appendChild(LabelTypes.getSoftLabel(item.getDocument().getConcept()));
160  vbox.appendChild(LabelTypes.getSoftLabel(item.getDocument().getNotes()));
161  row.appendChild(vbox);
162  row.appendChild(new Label(DecimalFormats.format(item.getDocument().getTotalAmount(),
163  DecimalFormats.getStringFormat(item.getDocument().getCurrency().getDefaultFractionDigits()))));
164  if(filterGrid != null) {
165  vbox = new Vbox();
166  for(final WorkflowItem dw : item.getForward()) {
167  Hlayout hbox = new Hlayout();
168  vbox.appendChild(hbox);
169  final Checkbox cb = new Checkbox(dw.getWorkflow().getDescendant().getName(), "/_zul/images/forward.png");
170  cb.setChecked(dw.isSelected());
171  cb.addEventListener(Events.ON_CHECK, new EventListener() {
172  @Override
173  public void onEvent(Event event) throws Exception {
174  dw.setSelected(cb.isChecked());
175  }
176  });
177  hbox.appendChild(cb);
178  Toolbarbutton del = new Toolbarbutton(null, "/_zul/images/edit-delete.png");
179  del.addEventListener(Events.ON_CLICK, new EventListener() {
180  @Override
181  public void onEvent(Event event) throws Exception {
182  Messages.question().add(I_.get("Mark as") + ": " + I_.get("No descendants")).show(() -> {
183  item.getDocument().setNoDescendants(true);
184  new FinancialsPU().saveObject(item.getDocument());
185  row.detach();
186  });
187  }
188  });
189  hbox.appendChild(del);
190  }
191  for(final WorkflowItem dw : item.getBackward()) {
192  Hlayout hbox = new Hlayout();
193  vbox.appendChild(hbox);
194  final Checkbox cb = new Checkbox(dw.getWorkflow().getAncestor().getName(), "/_zul/images/back.png");
195  cb.setChecked(dw.isSelected());
196  cb.addEventListener(Events.ON_CHECK, new EventListener() {
197  @Override
198  public void onEvent(Event event) throws Exception {
199  dw.setSelected(cb.isChecked());
200  }
201  });
202  hbox.appendChild(cb);
203  Toolbarbutton del = new Toolbarbutton(null, "/_zul/images/edit-delete.png");
204  del.addEventListener(Events.ON_CLICK, new EventListener() {
205  @Override
206  public void onEvent(Event event) throws Exception {
207  Messages.question().add(I_.get("Mark as") + ": " + I_.get("No ancestors")).show(() -> {
208  item.getDocument().setNoAncestors(true);
209  new FinancialsPU().saveObject(item.getDocument());
210  row.detach();
211  });
212  }
213  });
214  hbox.appendChild(del);
215  }
216  row.appendChild(vbox);
217  }
218  }
219 
220  setRowCount(filter.size());
221 
222  }
223 
224  private void addColumns() {
225  Columns cols = getColumns(true);
226  appendChild(cols);
227 
228  Column col = new Column("#", null, "40px");
229  cols.appendChild(col);
230 
231  col = new Column(I_.get("Receipt/Issue"), null, "120px");
232  cols.appendChild(col);
233 
234  col = new Column(I_.get("Date"), null, "90px");
235  cols.appendChild(col);
236 
237  col = new Column(I_.get("Number"), null, "180px");
238  cols.appendChild(col);
239 
240  col = new Column(I_.get("Document"));
241  cols.appendChild(col);
242 
243  col = new Column(I_.get("Amount"), null, "120px");
244  col.setAlign("right");
245  cols.appendChild(col);
246 
247  if(filterGrid != null) {
248  col = new Column("", null, "220px");
249  cols.appendChild(col);
250  }
251  }
252 
253 }
void setFilterGrid(FilterGrid filterGrid)
Collection< WorkflowItem > getBackward()
Collection< WorkflowItem > getForward()
void load(RelationMode mode, List< IFilterValue > values)
List< IFilterValue > getValues()
Columns getColumns(boolean create)
Rows getRows(boolean create)