BrightSide Workbench Full Report + Source Code
OrderBox.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.task.usage;
20 
21 import org.turro.command.Command;
22 import org.turro.command.CommandUtil;
23 import org.turro.command.Context;
24 import org.turro.elephant.context.Application;
25 import org.turro.elephant.util.DateFormats;
26 import org.turro.elephant.util.DecimalFormats;
27 import org.turro.elephant.util.ZkossUtils;
28 import org.turro.elephant.zkoss.Modal;
29 import org.turro.erp.db.ErpPU;
30 import org.turro.erp.entity.OrderItem;
31 import org.turro.erp.entity.ReceiptItem;
32 import org.turro.zkoss.label.PercentLabel;
33 import org.turro.zkoss.layout.GridLayout;
34 import org.zkoss.zk.ui.Executions;
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 OrderBox extends Groupbox {
45 
46  private OrderItem orderItem;
47  private GridLayout gl;
48 
49  public OrderBox(OrderItem orderItem) {
50  this.orderItem = orderItem;
51  setSclass("orderBox");
52  setMold("3d");
53  setOpen(false);
54  addComponents();
55  }
56 
57  private void addComponents() {
58  if(getCaption() != null) {
59  getCaption().setLabel(orderItem.getName() + " - " + DateFormats.formatNull(orderItem.getDocumentDate(), true) +
60  " " + orderItem.getDocumentNumber());
61  getCaption().setImage("/_zul/images/order.png");
62  gl.getRows().getChildren().clear();
63  } else {
64  appendChild(new Caption(orderItem.getName() + " - " + DateFormats.formatNull(orderItem.getDocumentDate(), true) +
65  " " + orderItem.getDocumentNumber(),
66  "/_zul/images/order.png"));
67  gl = new GridLayout("min,right-1,right-1");
68  gl.setColCaptions(new String[] {
69  "",
70  Application.getString("lExpected"),
71  Application.getString("lReal")
72  });
73  appendChild(gl);
74  }
75  renderOrder();
76  }
77 
78  private void renderOrder() {
79  if(orderItem.getProviderId() > 0) {
80  gl.addRow();
81  gl.addSpannedComponent(CommandUtil.getLabel(orderItem.getProvider()), 3);
82  }
83  gl.addRow();
84  gl.addCaption(Application.getString("lUnits"));
85  gl.addComponent(new Label(DecimalFormats.format(orderItem.getExpectedUnits())));
86  gl.addComponent(new Label(DecimalFormats.format(orderItem.getRealUnits())));
87  if(orderItem.getHumanResource() != null) {
88  gl.addRow();
89  gl.addCaption(Application.getString("lDedication"));
90  gl.addComponent(new PercentLabel(orderItem.getDedication()));
91  gl.addComponent(new PercentLabel(orderItem.getRealDedication()));
92  }
93  gl.addRow();
94  gl.addCaption(Application.getString("lCost"));
95  gl.addComponent(new Label(DecimalFormats.format(orderItem.getAmount())));
96  gl.addComponent(new Label(DecimalFormats.format(orderItem.getRealAmount())));
97  gl.addRow();
98  Hlayout hbox = new Hlayout();
99  hbox.setStyle("text-align:right");
100  Button edit = new Button(Application.getString("lEdit"));
101  hbox.appendChild(edit);
102  edit.addEventListener(Events.ON_CLICK, new EventListener() {
103  @Override
104  public void onEvent(Event event) throws Exception {
105  doEdit(orderItem, new Command() {
106  @Override
107  public Object execute(Context context) {
108  if(!orderItem.isEmpty()) {
109  orderItem = new ErpPU().saveObject(orderItem);
110  }
111  Events.postEvent(new Event(Events.ON_CHANGE, ZkossUtils.getParent(OrderBox.this, UsageTree.class)));
112  return null;
113  }
114  });
115  }
116  });
117  if(orderItem.getReceiptItems().isEmpty()) {
118  Button delete = new Button(Application.getString("lDelete"));
119  hbox.appendChild(delete);
120  delete.addEventListener(Events.ON_CLICK, new EventListener() {
121  @Override
122  public void onEvent(Event event) throws Exception {
123  ZkossUtils.confirmDeletion(null, new Command() {
124  @Override
125  public Object execute(Context context) {
126  new ErpPU().deleteObject(orderItem);
127  Events.postEvent(new Event(Events.ON_CHANGE, ZkossUtils.getParent(OrderBox.this, UsageTree.class)));
128  return null;
129  }
130  });
131  }
132  });
133  }
134  if(orderItem.getId() > 0) {
135  Button order = new Button(Application.getString("lNewReceipt"));
136  hbox.appendChild(order);
137  order.addEventListener(Events.ON_CLICK, new EventListener() {
138  @Override
139  public void onEvent(Event event) throws Exception {
140  ReceiptItem ri = orderItem.startReceipt(null);
141  if(!ri.isEmpty()) {
142  new ErpPU().saveObject(ri);
143  }
144  Events.postEvent(new Event(Events.ON_CHANGE, ZkossUtils.getParent(OrderBox.this, UsageTree.class)));
145  }
146  });
147  }
148  gl.addSpannedComponent(hbox, 3);
149  }
150 
151  public static void doEdit(OrderItem oi, Command command) {
152  Window w = (Window) Executions.createComponents("/WEB-INF/_zul/erp/purchase/editOrder.zul", null, null);
153  w.setMode(Window.MODAL);
154  Events.sendEvent(Events.ON_USER, w, oi);
155  Modal.doModal(w, command);
156  }
157 
158 }
159 
static String getString(String key)
static String formatNull(Date d, boolean dateOnly)
static int doModal(String file)
Definition: Modal.java:38
HumanResource getHumanResource()
Definition: OrderItem.java:157
ReceiptItem startReceipt(Date startWorking)
Definition: OrderItem.java:409
Set< ReceiptItem > getReceiptItems()
Definition: OrderItem.java:221
OrderBox(OrderItem orderItem)
Definition: OrderBox.java:49
static void doEdit(OrderItem oi, Command command)
Definition: OrderBox.java:151
void setColCaptions(String captions)
Definition: GridLayout.java:94
GridLayout addComponent(HtmlBasedComponent comp)
Rows getRows(boolean create)
GridLayout addSpannedComponent(HtmlBasedComponent comp, int cols)
GridLayout addCaption(String label)