KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > event > MenuEvents


1 /*
2  * $Id: MenuEvents.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.pos.event;
26
27 import java.util.List JavaDoc;
28 import java.awt.AWTEvent JavaDoc;
29
30 import org.ofbiz.base.util.UtilValidate;
31 import org.ofbiz.base.util.Debug;
32 import org.ofbiz.base.util.GeneralException;
33 import org.ofbiz.order.shoppingcart.CartItemModifyException;
34 import org.ofbiz.order.shoppingcart.ItemNotFoundException;
35 import org.ofbiz.pos.PosTransaction;
36 import org.ofbiz.pos.config.ButtonEventConfig;
37 import org.ofbiz.pos.component.Input;
38 import org.ofbiz.pos.component.Journal;
39 import org.ofbiz.pos.screen.PosScreen;
40 import org.ofbiz.entity.GenericValue;
41 import org.ofbiz.entity.util.EntityUtil;
42
43 /**
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @version $Rev: 5462 $
47  * @since 3.1
48  */

49 public class MenuEvents {
50
51     public static final String JavaDoc module = MenuEvents.class.getName();
52
53     // extended number events
54
public static void triggerClear(PosScreen pos) {
55         // clear the pieces
56
String JavaDoc[] totalFunc = pos.getInput().getFunction("TOTAL");
57         String JavaDoc[] paidFunc = pos.getInput().getFunction("PAID");
58         if (paidFunc != null) {
59             pos.getInput().clear();
60             pos.showPage("pospanel");
61         } else {
62             if (UtilValidate.isEmpty(pos.getInput().value())) {
63                 pos.getInput().clear();
64             }
65             if (totalFunc != null) {
66                 pos.getInput().setFunction("TOTAL", totalFunc[1]);
67             }
68         }
69
70         // refresh the current screen
71
pos.refresh();
72
73         // clear out the manual locks
74
if (!pos.isLocked()) {
75             pos.getInput().setLock(false);
76             pos.getButtons().setLock(false);
77         } else {
78             // just re-call set lock
79
pos.setLock(true);
80         }
81     }
82
83     public static void triggerQty(PosScreen pos) {
84         pos.getInput().setFunction("QTY");
85     }
86
87     public static void triggerEnter(PosScreen pos, AWTEvent JavaDoc event) {
88         // enter key maps to various different events; depending on the function
89
Input input = pos.getInput();
90         String JavaDoc[] lastFunc = input.getLastFunction();
91         if (lastFunc != null) {
92             if ("MGRLOGIN".equals(lastFunc[0])) {
93                 SecurityEvents.mgrLogin(pos);
94             } else if ("LOGIN".equals(lastFunc[0])) {
95                 SecurityEvents.login(pos);
96             } else if ("OPEN".equals(lastFunc[0])) {
97                 ManagerEvents.openTerminal(pos);
98             } else if ("CLOSE".equals(lastFunc[0])) {
99                 ManagerEvents.closeTerminal(pos);
100             } else if ("VOID".equals(lastFunc[0])) {
101                 ManagerEvents.voidOrder(pos);
102             } else if ("REFNUM".equals(lastFunc[0])) {
103                 PaymentEvents.setRefNum(pos);
104             } else if ("CREDIT".equals(lastFunc[0])) {
105                 PaymentEvents.payCredit(pos);
106             } else if ("CHECK".equals(lastFunc[0])) {
107                 PaymentEvents.payCheck(pos);
108             } else if ("GIFTCARD".equals(lastFunc[0])) {
109                 PaymentEvents.payGiftCard(pos);
110             } else if ("MSRINFO".equals(lastFunc[0])) {
111                 if (input.isFunctionSet("CREDIT")) {
112                     PaymentEvents.payCredit(pos);
113                 } else if (input.isFunctionSet("GIFTCARD")) {
114                     PaymentEvents.payGiftCard(pos);
115                 }
116             } else if ("SKU".equals(lastFunc[0])) {
117                 MenuEvents.addItem(pos, event);
118             }
119         } else if (input.value().length() > 0) {
120             MenuEvents.addItem(pos, event);
121         }
122     }
123
124     public static void addItem(PosScreen pos, AWTEvent JavaDoc event) {
125         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
126         Input input = pos.getInput();
127         String JavaDoc[] func = input.getFunction("QTY");
128         String JavaDoc value = input.value();
129
130         // no value; just return
131
if (event != null && UtilValidate.isEmpty(value)) {
132             String JavaDoc buttonName = ButtonEventConfig.getButtonName(event);
133             if (UtilValidate.isNotEmpty(buttonName)) {
134                 if (buttonName.startsWith("SKU.")) {
135                     value = buttonName.substring(4);
136                 }
137             }
138             if (UtilValidate.isEmpty(value)) {
139                 return;
140             }
141         }
142
143         if (!trans.isOpen()) {
144             pos.showDialog("dialog/error/terminalclosed");
145         } else {
146
147             // check for quantity
148
double quantity = 1;
149             if (func != null && "QTY".equals(func[0])) {
150                 try {
151                     quantity = Double.parseDouble(func[1]);
152                 } catch (NumberFormatException JavaDoc e) {
153                     quantity = 1;
154                 }
155             }
156
157             // locate the product ID
158
String JavaDoc productId = null;
159             try {
160                 List JavaDoc items = trans.lookupItem(value);
161                 if (items != null && items.size() == 1) {
162                     GenericValue product = EntityUtil.getFirst(items);
163                     productId = product.getString("productId");
164                 } else if (items != null && items.size() > 0) {
165                     Debug.logInfo("Multiple products found; need to select one from the list", module);
166                 }
167             } catch (GeneralException e) {
168                 Debug.logError(e, module);
169                 pos.showDialog("dialog/error/producterror");
170             }
171
172             // add the item to the cart; report any errors to the user
173
if (productId != null) {
174                 try {
175                     trans.addItem(productId, quantity);
176                 } catch (CartItemModifyException e) {
177                     Debug.logError(e, module);
178                     pos.showDialog("dialog/error/producterror");
179                 } catch (ItemNotFoundException e) {
180                     pos.showDialog("dialog/error/productnotfound");
181                 }
182             } else {
183                 pos.showDialog("dialog/error/productnotfound");
184             }
185         }
186
187         // clear the qty flag
188
input.clearFunction("QTY");
189         
190         // re-calc tax
191
trans.calcTax();
192
193         // refresh the others
194
pos.refresh();
195     }
196
197     public static void changeQty(PosScreen pos) {
198         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
199         String JavaDoc sku = null;
200         try {
201             sku = getSelectedItem(pos);
202         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
203         }
204
205         if (sku == null) {
206             pos.getOutput().print("Invalid Selection!");
207             pos.getJournal().refresh(pos);
208             pos.getInput().clear();
209         }
210
211         Input input = pos.getInput();
212         String JavaDoc value = input.value();
213
214         boolean increment = true;
215         double quantity = 1;
216         if (UtilValidate.isNotEmpty(value)) {
217             try {
218                 quantity = Double.parseDouble(value);
219             } catch (NumberFormatException JavaDoc e) {
220                 quantity = 1;
221             }
222         } else {
223             String JavaDoc[] func = input.getLastFunction();
224             if (func != null && "QTY".equals(func[0])) {
225                 increment = false;
226                 try {
227                     quantity = Double.parseDouble(func[1]);
228                 } catch (NumberFormatException JavaDoc e) {
229                     quantity = trans.getItemQuantity(sku);
230                 }
231             }
232         }
233
234         // adjust the quantity
235
quantity = (increment ? trans.getItemQuantity(sku) + quantity : quantity);
236
237         try {
238             trans.modifyQty(sku, quantity);
239         } catch (CartItemModifyException e) {
240             Debug.logError(e, module);
241             pos.showDialog("dialog/error/producterror");
242         }
243
244         // clear the qty flag
245
input.clearFunction("QTY");
246
247         // re-calc tax
248
trans.calcTax();
249
250         // refresh the others
251
pos.refresh();
252     }
253
254     public static void saleDiscount(PosScreen pos) {
255         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
256         if (!trans.isOpen()) {
257             pos.showDialog("dialog/error/terminalclosed");
258         } else {
259             Input input = pos.getInput();
260             String JavaDoc value = input.value();
261             if (UtilValidate.isNotEmpty(value)) {
262                 double amount = 0.00;
263                 boolean percent = false;
264                 if (value.endsWith("%")) {
265                     percent = true;
266                     value = value.substring(0, value.length() - 1);
267                 }
268                 try {
269                     amount = Double.parseDouble(value);
270                 } catch (NumberFormatException JavaDoc e) {
271                 }
272
273                 amount = (amount / 100) * -1;
274                 trans.addDiscount(null, amount, percent);
275                 trans.calcTax();
276             }
277         }
278         pos.refresh();
279     }
280
281     public static void itemDiscount(PosScreen pos) {
282         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
283         if (!trans.isOpen()) {
284             pos.showDialog("dialog/error/terminalclosed");
285         } else {
286             String JavaDoc sku = null;
287             try {
288                 sku = getSelectedItem(pos);
289             } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
290             }
291
292             if (sku == null) {
293                 pos.getOutput().print("Invalid Selection!");
294                 pos.getJournal().refresh(pos);
295                 pos.getInput().clear();
296             }
297
298             Input input = pos.getInput();
299             String JavaDoc value = input.value();
300             if (UtilValidate.isNotEmpty(value)) {
301                 double amount = 0.00;
302                 boolean percent = false;
303                 if (value.endsWith("%")) {
304                     percent = true;
305                     value = value.substring(0, value.length() - 1);
306                 }
307                 try {
308                     amount = Double.parseDouble(value);
309                 } catch (NumberFormatException JavaDoc e) {
310                 }
311
312                 amount = (amount / 100) * -1;
313                 trans.addDiscount(sku, amount, percent);
314                 trans.calcTax();
315             }
316         }
317         pos.refresh();
318     }
319
320     public static void clearDiscounts(PosScreen pos) {
321         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
322         trans.clearDiscounts();
323         trans.calcTax();
324         pos.refresh();
325     }
326
327     public static void calcTotal(PosScreen pos) {
328         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
329         trans.calcTax();
330
331         pos.getInput().setFunction("TOTAL");
332         pos.getJournal().refresh(pos);
333     }
334
335     public static void voidItem(PosScreen pos) {
336         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
337         String JavaDoc sku = null;
338         try {
339             sku = getSelectedItem(pos);
340         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
341         }
342
343         if (sku == null) {
344             pos.getOutput().print("Invalid Selection!");
345             pos.getJournal().refresh(pos);
346             pos.getInput().clear();
347         }
348
349         try {
350             trans.voidItem(sku);
351         } catch (CartItemModifyException e) {
352             pos.getOutput().print(e.getMessage());
353         }
354
355         // re-calc tax
356
trans.calcTax();
357         pos.refresh();
358     }
359
360     public static void voidAll(PosScreen pos) {
361         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
362         trans.voidSale();
363         pos.refresh();
364     }
365
366     public static String JavaDoc getSelectedItem(PosScreen pos) {
367         Journal journal = pos.getJournal();
368         return journal.getSelectedSku();
369     }
370 }
371
Popular Tags