BrightSide Workbench Full Report + Source Code
OrdersListbox.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.purchase;
20 
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.List;
24 import org.amic.util.db.Converter;
25 import org.turro.elephant.context.Application;
26 import org.turro.elephant.db.WhereClause;
27 import org.turro.elephant.util.DateFormats;
28 import org.turro.elephant.util.DecimalFormats;
29 import org.turro.erp.db.ErpPU;
30 import org.turro.erp.entity.OrderItem;
31 import org.turro.financials.entity.Contract;
32 import org.turro.zkoss.input.CollectionListbox;
33 import org.zkoss.zul.Listhead;
34 import org.zkoss.zul.Listheader;
35 
40 public class OrdersListbox extends CollectionListbox<Order> {
41 
42  private Contract provider;
43 
44  public OrdersListbox() {
45  addHeaders();
46  }
47 
48  public void setProvider(Contract provider) {
49  this.provider = provider;
50  List l = new ErpPU().getResultList(
51  "select d.documentDate, d.documentNumber, count(d.id), sum(d.units), sum(d.units*d.cost) " +
52  "from OrderItem d " +
53  "where providerId = ? " +
54  "and d.sent = TRUE " +
55  "and not exists (select dr from ReceiptItem dr where dr.orderItem = d) " +
56  "group by d.documentDate, d.documentNumber",
57  new Object[] { provider.getId() });
58  List<Order> orders = new ArrayList<Order>();
59  for(Object[] o : (List<Object[]>) l) {
60  Order order = new Order();
61  order.setDate((Date) o[0]);
62  order.setNumber((String) o[1]);
63  order.setLines(new Converter(o[2]).getDouble());
64  order.setUnits(new Converter(o[3]).getDouble());
65  order.setCost(new Converter(o[4]).getDouble());
66  orders.add(order);
67  }
68  updateCollection(orders);
69  }
70 
71  public List<OrderItem> getOrders() {
72  Order order = getObjectValue();
73  if(order != null) {
74  WhereClause wc = new WhereClause();
75  wc.addClause("select d from OrderItem d");
76  wc.addClause("where d.providerId = :provider");
77  wc.addClause("and d.sent = TRUE");
78  wc.addNamedValue("provider", provider.getId());
79  if(order.getDate() == null) {
80  wc.addClause("and d.documentDate is null");
81  } else {
82  wc.addClause("and d.documentDate = :date");
83  wc.addNamedValue("date", order.getDate());
84  }
85  if(order.getNumber() == null) {
86  wc.addClause("and d.documentNumber is null");
87  } else {
88  wc.addClause("and d.documentNumber = :number");
89  wc.addNamedValue("number", order.getNumber());
90  }
91  wc.addClause("and not exists (select dr from ReceiptItem dr where dr.orderItem = d)");
92  return new ErpPU().getResultList(wc);
93  }
94  return null;
95  }
96 
97  @Override
98  protected String convertToString(Order v) {
99  return (v.getDate() == null ? "" : DateFormats.format(v.getDate(), true)) +
101  v.getNumber() +
108  }
109 
110  private void addHeaders() {
111  if(getListhead() != null) return;
112  Listhead lh = new Listhead();
113  lh.setSizable(true);
114  lh.appendChild(new Listheader(Application.getString("lDate"), null, "20%"));
115  lh.appendChild(new Listheader(Application.getString("lNumber"), null, "20%"));
116  lh.appendChild(new Listheader(Application.getString("lLines"), null, "20%"));
117  lh.appendChild(new Listheader(Application.getString("lUnits"), null, "20%"));
118  lh.appendChild(new Listheader(Application.getString("lAmount"), null, "20%"));
119  appendChild(lh);
120  }
121 
122 }
static String getString(String key)
void addNamedValue(String name, Object value)
static final String format(Date d, boolean dateOnly)
static String format(Number value, String pattern)
void setUnits(double units)
Definition: Order.java:69
void setNumber(String number)
Definition: Order.java:61
void setLines(double lines)
Definition: Order.java:53
void setDate(Date date)
Definition: Order.java:45
void setCost(double cost)
Definition: Order.java:37
void setProvider(Contract provider)