BrightSide Workbench Full Report + Source Code
ReceiptBox.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.ReceiptItem;
31 import org.turro.zkoss.label.PercentLabel;
32 import org.turro.zkoss.layout.GridLayout;
33 import org.zkoss.zk.ui.Executions;
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.zul.*;
38 
43 public class ReceiptBox extends Groupbox {
44 
45  private ReceiptItem receiptItem;
46  private GridLayout gl;
47 
48  public ReceiptBox(ReceiptItem receiptItem) {
49  this.receiptItem = receiptItem;
50  setSclass("receiptBox");
51  setMold("3d");
52  setOpen(false);
53  addComponents();
54  }
55 
56  private void addComponents() {
57  if(getCaption() != null) {
58  getCaption().setLabel(receiptItem.getName() + " - " + DateFormats.formatNull(receiptItem.getDocumentDate(), true) +
59  " " + receiptItem.getDocumentNumber());
60  getCaption().setImage("/_zul/images/receipt.png");
61  gl.getRows().getChildren().clear();
62  } else {
63  appendChild(new Caption(receiptItem.getName() + " - " + DateFormats.formatNull(receiptItem.getDocumentDate(), true) +
64  " " + receiptItem.getDocumentNumber(),
65  "/_zul/images/receipt.png"));
66  gl = new GridLayout("min,right-1");
67  appendChild(gl);
68  }
69  renderReceipt();
70  }
71 
72  private void renderReceipt() {
73  if(receiptItem.getProviderId() > 0) {
74  gl.addRow();
75  gl.addSpannedComponent(CommandUtil.getLabel(receiptItem.getProvider()), 2);
76  }
77  gl.addRow();
78  gl.addCaption(Application.getString("lUnits"));
79  gl.addComponent(new Label(DecimalFormats.format(receiptItem.getRealUnits())));
80  if(receiptItem.getHumanResource() != null) {
81  gl.addRow();
82  gl.addCaption(Application.getString("lDedication"));
83  gl.addComponent(new PercentLabel(receiptItem.getDedication()));
84  }
85  gl.addRow();
86  gl.addCaption(Application.getString("lCost"));
87  gl.addComponent(new Label(DecimalFormats.format(receiptItem.getAmount())));
88  gl.addRow();
89  Hlayout hbox = new Hlayout();
90  hbox.setStyle("text-align:right");
91  Button edit = new Button(Application.getString("lEdit"));
92  hbox.appendChild(edit);
93  edit.addEventListener(Events.ON_CLICK, new EventListener() {
94  @Override
95  public void onEvent(Event event) throws Exception {
96  doEdit(receiptItem, new Command() {
97  @Override
98  public Object execute(Context context) {
99  if(!receiptItem.isEmpty()) {
100  receiptItem = new ErpPU().saveObject(receiptItem);
101  }
102  Events.postEvent(new Event(Events.ON_CHANGE, ZkossUtils.getParent(ReceiptBox.this, UsageTree.class)));
103  return null;
104  }
105  });
106  }
107  });
108  Button delete = new Button(Application.getString("lDelete"));
109  hbox.appendChild(delete);
110  delete.addEventListener(Events.ON_CLICK, new EventListener() {
111  @Override
112  public void onEvent(Event event) throws Exception {
113  ZkossUtils.confirmDeletion(null, new Command() {
114  @Override
115  public Object execute(Context context) {
116  new ErpPU().deleteObject(receiptItem);
117  Events.postEvent(new Event(Events.ON_CHANGE, ZkossUtils.getParent(ReceiptBox.this, UsageTree.class)));
118  return null;
119  }
120  });
121  }
122  });
123  gl.addSpannedComponent(hbox, 2);
124  }
125 
126  public static void doEdit(ReceiptItem ri, Command command) {
127  Window w = (Window) Executions.createComponents("/WEB-INF/_zul/erp/purchase/editReceipt.zul", null, null);
128  w.setMode(Window.MODAL);
129  Events.sendEvent(Events.ON_USER, w, ri);
130  Modal.doModal(w, command);
131  }
132 
133 }
static String formatNull(Date d, boolean dateOnly)
static int doModal(String file)
Definition: Modal.java:38
static void doEdit(ReceiptItem ri, Command command)
ReceiptBox(ReceiptItem receiptItem)
Definition: ReceiptBox.java:48
GridLayout addComponent(HtmlBasedComponent comp)
Rows getRows(boolean create)
GridLayout addSpannedComponent(HtmlBasedComponent comp, int cols)
GridLayout addCaption(String label)