BrightSide Workbench Full Report + Source Code
OrdersGrid.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 package org.turro.erp.purchase;
19 
20 import java.util.List;
21 import org.turro.command.Command;
22 import org.turro.command.Context;
23 import org.turro.elephant.context.Application;
24 import org.turro.elephant.util.Images;
25 import org.turro.elephant.zkoss.Modal;
26 import org.turro.erp.db.ErpPU;
27 import org.turro.erp.entity.OrderItem;
28 import org.turro.erp.entity.OrderStatus;
29 import org.turro.erp.entity.RequiredUsage;
30 import org.turro.erp.entity.TaskStatus;
31 import org.turro.jpa.Dao;
32 import org.turro.zkoss.grid.PagingGrid;
33 import org.turro.zkoss.label.LabelExtended;
34 import org.zkoss.zk.ui.Component;
35 import org.zkoss.zk.ui.Executions;
36 import org.zkoss.zk.ui.event.Event;
37 import org.zkoss.zk.ui.event.EventListener;
38 import org.zkoss.zk.ui.event.Events;
39 import org.zkoss.zk.ui.ext.AfterCompose;
40 import org.zkoss.zul.*;
41 
46 public class OrdersGrid extends PagingGrid implements AfterCompose {
47 
48  @Override
49  public void afterCompose() {
50  addColumns();
51  setRowRenderer(new OrderItemRowRenderer(this));
52  setModel(new OrderItemModel(new ErpPU().getResultList(
53  "select dco from OrderItem as dco " +
54  "where dco.sent = FALSE " +
55  "and (dco.documentLineId is null or dco.documentLineId = 0)"), this));
56  }
57 
58  private void addColumns() {
59  Columns cols = new Columns();
60  appendChild(cols);
61 
62  Column col = new Column(null, null, "100px");
63  cols.appendChild(col);
64 
65  col = new Column(Application.getString("lResource") + " / " + Application.getString("lProduct"));
66  cols.appendChild(col);
67 
68  col = new Column(Application.getString("lWorkOrder"));
69  cols.appendChild(col);
70 
71  col = new Column(Application.getString("lConcept"));
72  cols.appendChild(col);
73 
74  col = new Column(Application.getString("lDate"), null, "120px");
75  cols.appendChild(col);
76 
77  col = new Column(Application.getString("lHours") + " / " + Application.getString("lQuantity"), null, "100px");
78  col.setAlign("right");
79  cols.appendChild(col);
80 
81  col = new Column(Application.getString("lCost"), null, "100px");
82  col.setAlign("right");
83  cols.appendChild(col);
84 
85  col = new Column(null, null, "40px");
86  cols.appendChild(col);
87 
88  }
89 
90  public Component getEditBox(final Row row, final OrderItem oi) {
91  Vlayout vbox = new Vlayout();
92  Button edit = new Button(Application.getString("lEdit"), Images.getImage(oi.getType()));
93  edit.addEventListener(Events.ON_CLICK, new EventListener() {
94  @Override
95  public void onEvent(Event event) throws Exception {
96  doEdit(oi);
97  }
98  });
99  vbox.appendChild(edit);
100  LabelExtended le = new LabelExtended();
101  le.setResourceValue(oi.getStatus().toString());
102  le.setSclass("softLabel");
103  vbox.appendChild(le);
104  edit.setVisible(oi.getStatus().equals(OrderStatus.ORDER_READY));
105  return vbox;
106  }
107 
108  public void deleteOrder(OrderItem oi) {
109  if(oi.getUpdatedReceiptItems().isEmpty()) {
110  new ErpPU().executeUpdate(
111  "delete from OrderItem as dro where dro.id=? and dro.receiptItems is empty",
112  new Object[] { oi.getId() });
113  ((OrderItemModel) OrdersGrid.this.getGroupsModel()).removeItem(oi);
114  }
115  if(oi.getRelated() != null) {
116  deleteOrder(oi.getRelated());
117  }
118  }
119 
120  public void refresh() {
121  setModel(OrdersGrid.this.getGroupsModel());
122  }
123 
124  private void doEdit(final OrderItem oi) {
125  Window w = (Window) Executions.createComponents("/WEB-INF/_zul/erp/purchase/editOrder.zul", null, null);
126  w.setMode(Window.MODAL);
127  Events.sendEvent(Events.ON_USER, w, oi);
128  Modal.doModal(w, new Command() {
129  @Override
130  public Object execute(Context context) {
131  ((OrderItemModel) OrdersGrid.this.getGroupsModel()).replaceItem(new ErpPU().find(OrderItem.class, oi.getId()));
132  return null;
133  }
134  });
135  }
136 
137  public void importUsages() {
138  Dao dao = new ErpPU();
139  List l = dao.getResultList(
140  "select u from RequiredUsage u " +
141  "where u.orderItems is empty " +
142  "and (u.task.status = ? or u.task.status = ?)",
143  new Object[] { TaskStatus.TASK_READY, TaskStatus.TASK_ONWORK }
144  );
145  for(Object o : l) {
146  addOrder((RequiredUsage) o);
147  }
148  }
149 
150  public void addOrder(RequiredUsage ru) {
151  if(ru instanceof RequiredUsage) {
152  OrderItem oi = ru.startOrder();
153  if(!oi.isEmpty()) {
154  ((OrderItemModel) OrdersGrid.this.getGroupsModel()).addItem(new OrderItemWrapper(oi).save());
155  }
156  if(oi.getRelated() != null && !oi.getRelated().isEmpty()) {
157  ((OrderItemModel) OrdersGrid.this.getGroupsModel()).addItem(oi.getRelated());
158  }
159  }
160  }
161 
162 }
163 
static String getString(String key)
static String getImage(String image)
Definition: Images.java:36
static int doModal(String file)
Definition: Modal.java:38
Set< ReceiptItem > getUpdatedReceiptItems()
Definition: OrderItem.java:229
void addOrder(RequiredUsage ru)
Component getEditBox(final Row row, final OrderItem oi)
Definition: OrdersGrid.java:90
int executeUpdate(String query)
Definition: Dao.java:463
LabelExtended setResourceValue(String resourceValue)