BrightSide Workbench Full Report + Source Code
purchase/ReceiptsGrid.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.erp.purchase;
19 
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.List;
24 import org.amic.util.date.CheckDate;
25 import org.turro.command.Command;
26 import org.turro.command.Context;
27 import org.turro.elephant.context.Application;
28 import org.turro.elephant.util.Images;
29 import org.turro.elephant.zkoss.Modal;
30 import org.turro.erp.db.ErpPU;
31 import org.turro.erp.entity.OrderItem;
32 import org.turro.erp.entity.ReceiptItem;
33 import org.turro.zkoss.grid.PagingGrid;
34 import org.turro.zkoss.label.LabelExtended;
35 import org.turro.zkoss.menu.ElephantMenu;
36 import org.zkoss.zk.ui.Component;
37 import org.zkoss.zk.ui.Executions;
38 import org.zkoss.zk.ui.event.Event;
39 import org.zkoss.zk.ui.event.EventListener;
40 import org.zkoss.zk.ui.event.Events;
41 import org.zkoss.zk.ui.ext.AfterCompose;
42 import org.zkoss.zul.*;
43 
48 public class ReceiptsGrid extends PagingGrid implements AfterCompose {
49 
50  private Date lastDate = null;
51 
52  public void newReceipt() {
53  final ReceiptItem dr = new ReceiptItem();
54  if(lastDate == null) {
55  lastDate = new CheckDate().setHour(8).setMinute(0).setSecond(0).getDate();
56  }
57  dr.setStartWorking(lastDate);
58  dr.setDedication(1.0);
59  doEdit(dr, new Command() {
60  @Override
61  public Object execute(Context context) {
62  if(!dr.isEmpty()) {
63  Row row = new Row();
64  getRows().appendChild(row);
65  ((ReceiptItemModel) ReceiptsGrid.this.getGroupsModel()).addItem(new ErpPU().saveObject(dr));
66  }
67  return null;
68  }
69  });
70  }
71 
72  public void importReceipt() {
73  final List<OrderItem> l = new ArrayList<>();
74  HashMap<String, Object> map = new HashMap<>();
75  map.put("orderList", l);
76  ElephantMenu.showModalZulFile("lSelect", "/WEB-INF/_zul/erp/purchase/orderSelector.zul", map, new Command() {
77  @Override
78  public Object execute(Context context) {
79  for(OrderItem oi : l) {
80  ReceiptItem ri = oi.startReceipt(lastDate);
81  if(!ri.isEmpty()) {
82  Row row = new Row();
83  getRows().appendChild(row);
84  ((ReceiptItemModel) ReceiptsGrid.this.getGroupsModel()).addItem(new ErpPU().saveObject(ri));
85  ri = ri.getRelated();
86  if(ri != null) {
87  if(!ri.isEmpty()) {
88  row = new Row();
89  getRows().appendChild(row);
90  ((ReceiptItemModel) ReceiptsGrid.this.getGroupsModel()).addItem(new ErpPU().saveObject(ri));
91  }
92  }
93  }
94  }
95  return null;
96  }
97  });
98  }
99 
100  @Override
101  public void afterCompose() {
102  addColumns();
103  setRowRenderer(new ReceiptItemRowRenderer(this));
104  setModel(new ReceiptItemModel(new ArrayList<ReceiptItem>(), this));
105 // setModel(new ReceiptItemModel(new ErpPU().getResultList(
106 // "select dco from DraftReceipt as dco " +
107 // "where (dco.documentLineId is null or dco.documentLineId = 0)"), this));
108  }
109 
110  private void addColumns() {
111  Columns cols = new Columns();
112  appendChild(cols);
113 
114  Column col = new Column(null, null, "100px");
115  cols.appendChild(col);
116 
117  col = new Column(Application.getString("lResource") + " / " + Application.getString("lProduct"));
118  cols.appendChild(col);
119 
120  col = new Column(Application.getString("lWorkOrder"));
121  cols.appendChild(col);
122 
123  col = new Column(Application.getString("lConcept"));
124  cols.appendChild(col);
125 
126  col = new Column(Application.getString("lDate"), null, "120px");
127  cols.appendChild(col);
128 
129  col = new Column(Application.getString("lHours") + " / " + Application.getString("lQuantity"), null, "100px");
130  col.setAlign("right");
131  cols.appendChild(col);
132 
133  col = new Column(Application.getString("lCost"), null, "100px");
134  col.setAlign("right");
135  cols.appendChild(col);
136 
137  col = new Column(null, null, "40px");
138  cols.appendChild(col);
139 
140  }
141 
142  public Component getEditBox(final Row row, final ReceiptItem dr) {
143  Vlayout vbox = new Vlayout();
144  Button edit = new Button(Application.getString("lEdit"), Images.getImage(dr.getOrderItem().getType()));
145  edit.addEventListener(Events.ON_CLICK, new EventListener() {
146  @Override
147  public void onEvent(Event event) throws Exception {
148  doEdit(dr, new Command() {
149  @Override
150  public Object execute(Context context) {
151  if(dr.isEmpty()) {
152  new ErpPU().deleteObject(dr);
153  row.detach();
154  } else {
155  row.getChildren().clear();
156  ((ReceiptItemModel) ReceiptsGrid.this.getGroupsModel()).replaceItem(new ErpPU().saveObject(dr));
157  lastDate = dr.getDocumentDate();
158  }
159  return null;
160  }
161  });
162  }
163  });
164  vbox.appendChild(edit);
165  LabelExtended le = new LabelExtended();
166  le.setResourceValue(dr.getStatus().toString());
167  le.setSclass("softLabel");
168  vbox.appendChild(le);
169  return vbox;
170  }
171 
172  public void deleteReceipt(ReceiptItem dro) {
173  new ErpPU().deleteObject(dro);
174  ((ReceiptItemModel) ReceiptsGrid.this.getGroupsModel()).removeItem(dro);
175  }
176 
177  public void refresh() {
178  setModel(ReceiptsGrid.this.getGroupsModel());
179  }
180 
181  private void doEdit(ReceiptItem dr, Command command) {
182  Window w = (Window) Executions.createComponents("/WEB-INF/_zul/erp/purchase/editReceipt.zul", null, null);
183  w.setMode(Window.MODAL);
184  Events.sendEvent(Events.ON_USER, w, dr);
185  Modal.doModal(w, command);
186  }
187 
188 }
189 
static String getString(String key)
static String getImage(String image)
Definition: Images.java:36
static int doModal(String file)
Definition: Modal.java:38
void setDedication(double dedication)
void setStartWorking(Date startWorking)
Component getEditBox(final Row row, final ReceiptItem dr)
void deleteObject(Object obj)
Definition: Dao.java:162
Rows getRows(boolean create)
LabelExtended setResourceValue(String resourceValue)