BrightSide Workbench Full Report + Source Code
BudgetGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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 
19 package org.turro.erp.reference;
20 
21 import java.util.Iterator;
22 import org.turro.command.Command;
23 import org.turro.command.Context;
24 import org.turro.elephant.context.Application;
25 import org.turro.elephant.util.DecimalFormats;
26 import org.turro.elephant.util.Images;
27 import org.turro.elephant.util.ZkossUtils;
28 import org.turro.erp.entity.Breakdown;
29 import org.turro.erp.entity.Budget;
30 import org.turro.erp.entity.OrderReference;
31 import org.turro.erp.entity.WorkOrder;
32 import org.turro.erp.workorder.BreakdownSet;
33 import org.turro.zkoss.grid.PagingGrid;
34 import org.turro.zkoss.layout.GridLayout;
35 import org.zkoss.lang.Strings;
36 import org.zkoss.zk.ui.Component;
37 import org.zkoss.zk.ui.event.Event;
38 import org.zkoss.zk.ui.event.EventListener;
39 import org.zkoss.zk.ui.event.Events;
40 import org.zkoss.zk.ui.ext.AfterCompose;
41 import org.zkoss.zul.*;
42 
47 public class BudgetGrid extends PagingGrid implements AfterCompose {
48 
49  private WorkOrder workOrder;
50  private Long number = 1L;
51 
52  public void setWorkOrder(WorkOrder workOrder) {
53  this.workOrder = workOrder;
54  }
55 
56  public void setBudget(Object selnumber) {
57  Long select = (Long) selnumber;
58  if(select == -1) {
59  deleteBudget();
60  } else if(select == 0) {
61  this.number = workOrder.getNextBudgetNumber();
62  for(OrderReference or : workOrder.getOrderReferences()) {
63  if(!or.isEmpty()) {
64  Budget b = new Budget();
65  b.initFrom(or, this.number);
66  or.getBudgets().add(b);
67  }
68  }
69  } else {
70  this.number = select;
71  }
72  refresh();
73  }
74 
75  public void refresh() {
76  addColumns();
77  addRows();
78  }
79 
80  @Override
81  public void afterCompose() {
82  addColumns();
83  addRows();
84  }
85 
86  private void addRows() {
88 
89  int fractionDigits = workOrder.getContract() != null ?
90  workOrder.getContract().getCurrency().getDefaultFractionDigits() :
91  2;
92 
93  Rows rows = getRows(true);
94  appendChild(rows);
95  rows.getChildren().clear();
96 
97  for(final OrderReference or : workOrder.getOrderReferences()) {
98  if(!or.isEmpty()) {
99  for(Budget b : or.getBudgets()) {
100  if(b.getBudgetNumber() != number) continue;
101 
102  Row row = new Row();
103  row.setSclass("textCaption3");
104  rows.appendChild(row);
105 
106  row.appendChild(new Label(b.getBudgetNumber() + ""));
107  row.appendChild(new Label(DecimalFormats.format(b.getUnits(), DecimalFormats.getStringFormat(3))));
108  Vlayout vbox = new Vlayout();
109  row.appendChild(vbox);
110  vbox.appendChild(new Label(b.getDescription()));
111  if(!b.getBreakdowns().isEmpty()) {
112  GridLayout gl = new GridLayout("right-40px,300px,right-60px,right-60px");
113  gl.setSclass(gl.getSclass() + " textSubCaption3");
114  vbox.appendChild(gl);
115  for(Breakdown bd : new BreakdownSet(b.getBreakdowns())) {
116  if(!Strings.isBlank(bd.getDescription())) {
117  gl.addRow();
118  if(bd.getQuantity() == null || bd.getQuantity() == 0.0d) {
119  gl.addSpace();
120  } else {
121  gl.addComponent(new Label(DecimalFormats.format(bd.getQuantity(), DecimalFormats.getStringFormat(3))));
122  }
123  gl.addComponent(new Label(bd.getDescription()));
124  if(bd.getPrice() == null || bd.getPrice() == 0.0d) {
125  gl.addSpace();
126  gl.addSpace();
127  } else {
128  gl.addComponent(new Label(DecimalFormats.format(bd.getPrice(), DecimalFormats.getStringFormat(fractionDigits))));
129  gl.addComponent(new Label(DecimalFormats.format(bd.getAmount(), DecimalFormats.getStringFormat(fractionDigits))));
130  }
131  }
132  }
133  }
134  row.appendChild(new Label(DecimalFormats.format(b.getPrice(), DecimalFormats.getStringFormat(fractionDigits))));
135  row.appendChild(getActions(b));
136  }
137  }
138  }
139 
140  setRowCount(rows.getChildren().size());
141  }
142 
143  private void addColumns() {
144  Columns cols = getColumns(true);
145  appendChild(cols);
146  cols.getChildren().clear();
147 
148  Column col = new Column("#", null, "60px");
149  cols.appendChild(col);
150 
151  col = new Column(Application.getString("lQuantity"), null, "90px");
152  col.setAlign("right");
153  cols.appendChild(col);
154 
155  col = new Column(Application.getString("lDescription"));
156  col.setHflex("1");
157  cols.appendChild(col);
158 
159  col = new Column(Application.getString("lPrice"), null, "120px");
160  col.setAlign("right");
161  cols.appendChild(col);
162 
163  col = new Column(null, null, "400px");
164  cols.appendChild(col);
165  }
166 
167  private void deleteBudget() {
168  ZkossUtils.confirmDeletion(Application.getString("lBudget") + ": " + number, new Command() {
169  @Override
170  public Object execute(Context context) {
171  for(final OrderReference or : workOrder.getOrderReferences()) {
172  if(!or.isEmpty()) {
173  Iterator<Budget> itb = or.getBudgets().iterator();
174  while(itb.hasNext()) {
175  Budget b = itb.next();
176  if(b.getBudgetNumber() == number) {
177  itb.remove();
178  b.setOrderReference(null);
179  }
180  }
181  }
182  }
183  return null;
184  }
185  });
186  }
187 
188  private Component getActions(final Budget b) {
189  Hlayout hbox = new Hlayout();
190  if(b.isAccepted()) {
191  hbox.appendChild(new Image(Images.getImage("ok")));
192  hbox.appendChild(new Label(Application.getString("lAccepted")));
193  } else if(b.isRefused()) {
194  hbox.appendChild(new Image(Images.getImage("cancel")));
195  hbox.appendChild(new Label(Application.getString("lRefused")));
196  } else if(b.isDelivered()) {
197  hbox.appendChild(new Label(Application.getString("lDelivered")));
198  hbox.appendChild(new Image(Images.getImage("forward")));
199  Toolbarbutton accept = new Toolbarbutton(Application.getString("lAccepted"), Images.getImage("ok"));
200  accept.addEventListener(Events.ON_CLICK, new EventListener() {
201  @Override
202  public void onEvent(Event event) throws Exception {
203  b.setDelivered(true);
204  b.setAccepted(true);
205  b.setRefused(false);
206  refresh();
207  }
208  });
209  hbox.appendChild(accept);
210  Toolbarbutton refused = new Toolbarbutton(Application.getString("lRefused"), Images.getImage("cancel"));
211  refused.addEventListener(Events.ON_CLICK, new EventListener() {
212  @Override
213  public void onEvent(Event event) throws Exception {
214  b.setDelivered(true);
215  b.setAccepted(false);
216  b.setRefused(true);
217  refresh();
218  }
219  });
220  hbox.appendChild(refused);
221  } else {
222  hbox.appendChild(new Label(Application.getString("lPending")));
223  hbox.appendChild(new Image(Images.getImage("forward")));
224  Toolbarbutton delivered = new Toolbarbutton(Application.getString("lDelivered"), Images.getImage("mail_send"));
225  delivered.addEventListener(Events.ON_CLICK, new EventListener() {
226  @Override
227  public void onEvent(Event event) throws Exception {
228  b.setDelivered(true);
229  b.setAccepted(false);
230  b.setRefused(false);
231  refresh();
232  }
233  });
234  hbox.appendChild(delivered);
235  }
236  return hbox;
237  }
238 
239 }
static String format(Number value, String pattern)
static String getStringFormat(int fractionDigits)
void initFrom(OrderReference orderReference, long number)
Definition: Budget.java:148
Set< OrderReference > getOrderReferences()
Definition: WorkOrder.java:124
void setBudget(Object selnumber)
Definition: BudgetGrid.java:56
void setWorkOrder(WorkOrder workOrder)
Definition: BudgetGrid.java:52
Columns getColumns(boolean create)
Rows getRows(boolean create)
GridLayout addComponent(HtmlBasedComponent comp)