BrightSide Workbench Full Report + Source Code
ProductsEdit.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.Date;
22 import org.turro.string.Strings;
23 import org.turro.erp.db.ErpPU;
24 import org.turro.erp.entity.OrderItem;
25 import org.turro.erp.entity.PurchaseMode;
26 import org.turro.erp.entity.ReceiptItem;
27 import org.turro.erp.entity.RequiredUsage;
28 import org.turro.financials.contract.ProviderCombobox;
29 import org.turro.financials.entity.Contract;
30 import org.turro.jpa.Dao;
31 import org.turro.zkoss.grid.EditableCell;
32 import org.zkoss.zk.ui.Component;
33 import org.zkoss.zk.ui.util.GenericForwardComposer;
34 import org.zkoss.zul.Button;
35 import org.zkoss.zul.Datebox;
36 import org.zkoss.zul.Label;
37 import org.zkoss.zul.Textbox;
38 
43 public class ProductsEdit extends GenericForwardComposer {
44 
45  private ProductsGrid productsGrid;
46  private ProviderCombobox provider;
47  private Textbox docnumber;
48  private Datebox docdate;
49  private Button doccreate;
50  private Label lWorkOrder, lOrderReference;
51 
52  public void onClick$doccreate() {
53  for(ProductsItem hi : productsGrid.getValues()) {
54  if(hi.isValid()) {
55  if(productsGrid.isReceipt()) {
56  createReceipt(provider.getObjectValue(), docnumber.getValue(), docdate.getValue(), hi);
57  } else {
58  createOrder(provider.getObjectValue(), docnumber.getValue(), docdate.getValue(), hi);
59  }
60  }
61  }
62  productsGrid.clearValidRows();
63  }
64 
65  public void onChange$docdate() {
66  updateButton();
67  }
68 
69  public void onChange$docnumber() {
70  updateButton();
71  }
72 
73  public void onChange$provider() {
74  updateButton();
75  }
76 
77  public void onChange$productsGrid() {
78  updateButton();
79  }
80 
81  public void onChanging$productsGrid() {
82  EditableCell ec = productsGrid.getCurrentCell();
83  if(ec != null) {
84  ProductsItem pi = (ProductsItem) ec.getRow().getValue();
85  if(pi != null) {
86  lWorkOrder.setValue(pi.getWorkOrder() == null ? null :
87  "#" + pi.getWorkOrder().getWorkOrderId() + "\n" + pi.getWorkOrder().getSomeDescription());
88  lOrderReference.setValue(pi.getOrderReference() == null ? null :
89  "#" + pi.getOrderReference().getOrderRef() + "\n" + pi.getOrderReference().getDescription());
90  }
91  }
92  }
93 
94  private void updateButton() {
95  doccreate.setDisabled(
96  provider.getObjectValue() == null ||
97  Strings.isEmpty(docnumber.getValue()) ||
98  docdate.getValue() == null ||
99  productsGrid.getValues().isEmpty()
100  );
101  }
102 
103  @Override
104  public void doAfterCompose(Component comp) throws Exception {
105  super.doAfterCompose(comp);
106  docdate.setValue(new Date());
107  doccreate.setDisabled(true);
108  }
109 
110  private void createOrder(Contract provider, String documentNumber, Date documentDate, ProductsItem pi) {
111  Dao dao = new ErpPU();
112  // Find Product Usage
113  RequiredUsage ru;
114  if(pi.getIProduct().isUncoded()) {
116  "select pu from RequiredUsage pu " +
117  "where task = ? " +
118  "and description = ?",
119  new Object[] { pi.getTask(), pi.getIProduct().getProductCodeStr() });
120  } else {
121  ru = (RequiredUsage) dao.getSingleResultOrNull(
122  "select pu from RequiredUsage pu " +
123  "where task = ? " +
124  "and productId = ?",
125  new Object[] { pi.getTask(), pi.getIProduct().getProductId() });
126  }
127  // Or create it
128  if(ru == null) {
129  ru = new RequiredUsage();
130  ru.setTask(pi.getTask());
131  ru.getTask().getRequiredUsages().add(ru);
132  ru.setPurchaseMode(PurchaseMode.PURCHASE_IMMEDIATELY);
133  ru.setPurchaseLag(0);
134  ru.setIProduct(pi.getIProduct());
135  ru.setUnits(pi.getUnits());
136  ru = dao.saveObject(ru);
137  }
138  // Or create DraftOrder
139  OrderItem dro = ru.startOrder();
140  dro.setDocumentDate(documentDate);
141  dro.setDocumentNumber("*" + documentNumber);
142  dro.setProviderId(provider.getId());
143  dao.saveObject(dro);
144  }
145 
146 
147  private void createReceipt(Contract provider, String documentNumber, Date documentDate, ProductsItem pi) {
148  Dao dao = new ErpPU();
149  // Find a DraftOrder
150  OrderItem oi;
151  if(pi.getIProduct().isUncoded()) {
152  oi = (OrderItem) dao.getSingleResultOrNull(
153  "select dro from OrderItem dro " +
154  "where requiredUsage.task = ? " +
155  "and providerId = ? " +
156  "and requiredUsage.description = ?",
157  new Object[] { pi.getTask(), provider.getId(), pi.getIProduct().getProductCodeStr() });
158  } else {
159  oi = (OrderItem) dao.getSingleResultOrNull(
160  "select dro from OrderItem dro " +
161  "where requiredUsage.task = ? " +
162  "and providerId = ? " +
163  "and requiredUsage.productId = ?",
164  new Object[] { pi.getTask(), provider.getId(), pi.getIProduct().getProductId() });
165  }
166  if(oi == null) {
167  // Find Product Usage
168  RequiredUsage pu;
169  if(pi.getIProduct().isUncoded()) {
170  pu = (RequiredUsage) dao.getSingleResultOrNull(
171  "select pu from RequiredUsage pu " +
172  "where requiredUsage.task = ? " +
173  "and description = ?",
174  new Object[] { pi.getTask(), pi.getIProduct().getProductCodeStr() });
175  } else {
176  pu = (RequiredUsage) dao.getSingleResultOrNull(
177  "select pu from RequiredUsage pu " +
178  "where requiredUsage.task = ? " +
179  "and productId = ?",
180  new Object[] { pi.getTask(), pi.getIProduct().getProductId() });
181  }
182  // Or create it
183  if(pu == null) {
184  pu = new RequiredUsage();
185  pu.setTask(pi.getTask());
186  pu.getTask().getRequiredUsages().add(pu);
187  pu.setPurchaseMode(PurchaseMode.PURCHASE_IMMEDIATELY);
188  pu.setPurchaseLag(0);
189  pu.setIProduct(pi.getIProduct());
190  pu.setUnits(pi.getUnits());
191  pu = dao.saveObject(pu);
192  }
193  // Or create DraftOrder
194  oi = pu.startOrder();
195  oi.setDocumentDate(documentDate);
196  oi.setDocumentNumber("*" + documentNumber);
197  oi.setProviderId(provider.getId());
198  dao.saveObject(oi);
199  }
200  // Create DraftReceipt
201  ReceiptItem ri = oi.startReceipt(null);
202  dao.saveObject(ri);
203  if(ri.getOrderItem().getRelated() != null) {
204  ri = ri.getOrderItem().getRelated().getReceiptItems().iterator().next();
205  dao.saveObject(ri);
206  }
207  }
208 
209 }
210 
void setDocumentDate(Date documentDate)
Definition: OrderItem.java:129
void setPurchaseMode(PurchaseMode purchaseMode)
void setPurchaseLag(double purchaseLag)
void setIProduct(IProduct product)
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419