KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ManagerEvents.java 6564 2006-01-24 17:02:46Z sichen $
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.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.sql.Timestamp JavaDoc;
31 import java.util.Locale JavaDoc;
32
33 import net.xoetrope.xui.XProjectManager;
34
35 import org.ofbiz.base.util.cache.UtilCache;
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.UtilMisc;
38 import org.ofbiz.base.util.UtilDateTime;
39 import org.ofbiz.base.util.UtilFormatOut;
40 import org.ofbiz.base.util.UtilValidate;
41 import org.ofbiz.pos.device.DeviceLoader;
42 import org.ofbiz.pos.device.impl.Receipt;
43 import org.ofbiz.pos.screen.PosScreen;
44 import org.ofbiz.pos.PosTransaction;
45 import org.ofbiz.pos.adaptor.SyncCallbackAdaptor;
46 import org.ofbiz.pos.component.Input;
47 import org.ofbiz.pos.component.Output;
48 import org.ofbiz.entity.GenericDelegator;
49 import org.ofbiz.entity.GenericEntityException;
50 import org.ofbiz.entity.GenericValue;
51 import org.ofbiz.entity.util.EntityListIterator;
52 import org.ofbiz.entity.condition.EntityExpr;
53 import org.ofbiz.entity.condition.EntityOperator;
54 import org.ofbiz.entity.condition.EntityConditionList;
55 import org.ofbiz.service.LocalDispatcher;
56 import org.ofbiz.service.GenericServiceException;
57 import org.ofbiz.service.ServiceUtil;
58 import org.ofbiz.guiapp.xui.XuiSession;
59 import org.ofbiz.base.util.UtilProperties;
60
61 /**
62  *
63  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
64  * @version $Rev: 6564 $
65  * @since 3.1
66  */

67 public class ManagerEvents {
68
69     public static final String JavaDoc module = ManagerEvents.class.getName();
70     public static boolean mgrLoggedIn = false;
71
72     public static void modifyPrice(PosScreen pos) {
73         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
74         String JavaDoc sku = null;
75         try {
76             sku = MenuEvents.getSelectedItem(pos);
77         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
78         }
79
80         if (sku == null) {
81             pos.getOutput().print(UtilProperties.getMessage("pos","Invalid_Selection",Locale.getDefault()));
82             pos.getJournal().refresh(pos);
83             pos.getInput().clear();
84         }
85
86         Input input = pos.getInput();
87         String JavaDoc value = input.value();
88         if (UtilValidate.isNotEmpty(value)) {
89             double price = 0.00;
90             boolean parsed = false;
91             try {
92                 price = Double.parseDouble(value);
93                 parsed = true;
94             } catch (NumberFormatException JavaDoc e) {
95             }
96
97             if (parsed) {
98                 price = price / 100;
99                 trans.modifyPrice(sku, price);
100
101                 // re-calc tax
102
trans.calcTax();
103             }
104         }
105
106         // refresh the other components
107
pos.refresh();
108     }
109
110     public static void openTerminal(PosScreen pos) {
111         if (!mgrLoggedIn) {
112             pos.showDialog("dialog/error/mgrnotloggedin");
113             return;
114         }
115
116         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
117         Input input = pos.getInput();
118         if (!trans.isOpen()) {
119             if (input.isFunctionSet("OPEN")) {
120                 String JavaDoc amountStr = input.value();
121                 Double JavaDoc amount = null;
122                 if (UtilValidate.isNotEmpty(amountStr)) {
123                     try {
124                         double amt = Double.parseDouble(amountStr);
125                         amt = amt / 100;
126                         amount = new Double JavaDoc(amt);
127                     } catch (NumberFormatException JavaDoc e) {
128                         Debug.logError(e, module);
129                     }
130                 }
131                 GenericValue state = pos.getSession().getDelegator().makeValue("PosTerminalState", null);
132                 state.set("posTerminalId", pos.getSession().getId());
133                 state.set("openedDate", UtilDateTime.nowTimestamp());
134                 state.set("openedByUserLoginId", pos.getSession().getUserId());
135                 state.set("startingTxId", trans.getTransactionId());
136                 state.set("startingDrawerAmount", amount);
137                 try {
138                     state.create();
139                 } catch (GenericEntityException e) {
140                     Debug.logError(e, module);
141                     pos.showDialog("dialog/error/exception", e.getMessage());
142                 }
143                 NavagationEvents.showPosScreen(pos);
144             } else {
145                 input.clear();
146                 input.setFunction("OPEN");
147                 pos.getOutput().print(UtilProperties.getMessage("pos","OPDRAM",Locale.getDefault()));
148                 return;
149             }
150         } else {
151             pos.showPage("pospanel");
152         }
153     }
154
155     public static void closeTerminal(PosScreen pos) {
156         if (!mgrLoggedIn) {
157             pos.showDialog("dialog/error/mgrnotloggedin");
158             return;
159         }
160
161         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
162         if (!trans.isOpen()) {
163             pos.showDialog("dialog/error/terminalclosed");
164             return;
165         }
166
167         Output output = pos.getOutput();
168         Input input = pos.getInput();
169         if (input.isFunctionSet("CLOSE")) {
170             String JavaDoc[] func = input.getFunction("CLOSE");
171             String JavaDoc lastValue = input.value();
172             if (UtilValidate.isNotEmpty(lastValue)) {
173                 try {
174                     double dbl = Double.parseDouble(lastValue);
175                     dbl = dbl / 100;
176                     lastValue = UtilFormatOut.formatPrice(dbl);
177                 } catch (NumberFormatException JavaDoc e) {
178                     Debug.logError(e, module);
179                 }
180                 if (UtilValidate.isNotEmpty(func[1])) {
181                     func[1] = func[1] + "|";
182                 }
183                 func[1] = func[1] + lastValue;
184                 input.setFunction("CLOSE", func[1]);
185             }
186
187             String JavaDoc[] closeInfo = new String JavaDoc[0];
188             if (UtilValidate.isNotEmpty(func[1])) {
189                 closeInfo = func[1].split("\\|");
190             }
191             switch (closeInfo.length) {
192                 case 0:
193                     output.print(UtilProperties.getMessage("pos","ENTCAS",Locale.getDefault()));
194                     break;
195                 case 1:
196                     output.print(UtilProperties.getMessage("pos","ENTCHK",Locale.getDefault()));
197                     break;
198                 case 2:
199                     output.print(UtilProperties.getMessage("pos","ENTCRC",Locale.getDefault()));
200                     break;
201                 case 3:
202                     output.print(UtilProperties.getMessage("pos","ENTGFC",Locale.getDefault()));
203                     break;
204                 case 4:
205                     output.print(UtilProperties.getMessage("pos","ENTOTH",Locale.getDefault()));
206                     break;
207                 case 5:
208                     GenericValue state = trans.getTerminalState();
209                     state.set("closedDate", UtilDateTime.nowTimestamp());
210                     state.set("closedByUserLoginId", pos.getSession().getUserId());
211                     state.set("actualEndingCash", new Double JavaDoc(closeInfo[0]));
212                     state.set("actualEndingCheck", new Double JavaDoc(closeInfo[1]));
213                     state.set("actualEndingCc", new Double JavaDoc(closeInfo[2]));
214                     state.set("actualEndingGc", new Double JavaDoc(closeInfo[3]));
215                     state.set("actualEndingOther", new Double JavaDoc(closeInfo[4]));
216                     state.set("endingTxId", trans.getTransactionId());
217                     Debug.log("Updated State - " + state, module);
218                     try {
219                         state.store();
220                         state.refresh();
221                     } catch (GenericEntityException e) {
222                         Debug.logError(e, module);
223                         pos.showDialog("dialog/error/exception", e.getMessage());
224                     }
225
226                     // print the totals report
227
printTotals(pos, state, true);
228
229                     // lock the terminal for the moment
230
output.print(UtilProperties.getMessage("pos","WaitingFinalSales",Locale.getDefault()));
231                     pos.getInput().setLock(true);
232                     pos.getButtons().setLock(true);
233                     pos.refresh(false);
234
235                     // transmit final data to server
236
GenericValue terminal = null;
237                     try {
238                         terminal = state.getRelatedOne("PosTerminal");
239                     } catch (GenericEntityException e) {
240                         Debug.logError(e, module);
241                         pos.showDialog("dialog/error/exception", e.getMessage());
242                     }
243                     if (terminal != null && terminal.get("pushEntitySyncId") != null) {
244                         String JavaDoc syncId = terminal.getString("pushEntitySyncId");
245                         SyncCallbackAdaptor cb = new SyncCallbackAdaptor(pos, syncId, state.getTimestamp("lastUpdatedTxStamp"));
246                         pos.getSession().getDispatcher().registerCallback("runEntitySync", cb);
247                     } else {
248                         // no sync setting; just logout
249
pos.showDialog("dialog/error/terminalclosed");
250                         SecurityEvents.logout(pos);
251                     }
252             }
253         } else {
254             trans.popDrawer();
255             input.clear();
256             input.setFunction("CLOSE");
257             output.print(UtilProperties.getMessage("pos","ENTCAS",Locale.getDefault()));
258         }
259
260     }
261
262     public static void voidOrder(PosScreen pos) {
263         if (!mgrLoggedIn) {
264             pos.showDialog("dialog/error/mgrnotloggedin");
265             return;
266         }
267
268         XuiSession session = pos.getSession();
269         PosTransaction trans = PosTransaction.getCurrentTx(session);
270         if (!trans.isOpen()) {
271             pos.showDialog("dialog/error/terminalclosed");
272             return;
273         }
274
275         Output output = pos.getOutput();
276         Input input = pos.getInput();
277         boolean lookup = false;
278
279         if (input.isFunctionSet("VOID")) {
280             lookup = true;
281         } else if (UtilValidate.isNotEmpty(input.value())) {
282             lookup = true;
283         }
284
285         if (lookup) {
286             GenericValue state = trans.getTerminalState();
287             Timestamp JavaDoc openDate = state.getTimestamp("openedDate");
288
289             String JavaDoc orderId = input.value();
290             GenericValue orderHeader = null;
291             try {
292                 orderHeader = session.getDelegator().findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId));
293             } catch (GenericEntityException e) {
294                 Debug.logError(e, module);
295             }
296             if (orderHeader == null) {
297                 input.clear();
298                 pos.showDialog("dialog/error/ordernotfound");
299                 return;
300             } else {
301                 Timestamp JavaDoc orderDate = orderHeader.getTimestamp("orderDate");
302                 if (orderDate.after(openDate)) {
303                     LocalDispatcher dispatcher = session.getDispatcher();
304                     Map JavaDoc returnResp = null;
305                     try {
306                         returnResp = dispatcher.runSync("quickReturnOrder", UtilMisc.toMap("orderId", orderId,
307                                                         "returnHeaderTypeId", "CUSTOMER_RETURN", "userLogin", session.getUserLogin()));
308                     } catch (GenericServiceException e) {
309                         Debug.logError(e, module);
310                         pos.showDialog("dialog/error/exception", e.getMessage());
311                         pos.refresh();
312                         return;
313                     }
314                     if (returnResp != null && ServiceUtil.isError(returnResp)) {
315                         pos.showDialog("dialog/error/exception", ServiceUtil.getErrorMessage(returnResp));
316                         pos.refresh();
317                         return;
318                     }
319
320                     // todo print void receipt
321

322                     trans.setTxAsReturn((String JavaDoc) returnResp.get("returnId"));
323                     input.clear();
324                     pos.showDialog("dialog/error/salevoided");
325                     pos.refresh();
326                 } else {
327                     input.clear();
328                     pos.showDialog("dialog/error/ordernotfound");
329                     return;
330                 }
331             }
332         } else {
333             input.setFunction("VOID");
334             output.print(UtilProperties.getMessage("pos","VOID",Locale.getDefault()));
335         }
336     }
337
338     public static void reprintLastTx(PosScreen pos) {
339         if (!mgrLoggedIn) {
340             pos.showDialog("dialog/error/mgrnotloggedin");
341             return;
342         }
343         DeviceLoader.receipt.reprintReceipt(true);
344         pos.refresh();
345     }
346
347     public static void popDrawer(PosScreen pos) {
348         if (!mgrLoggedIn) {
349             pos.showDialog("dialog/error/mgrnotloggedin");
350         } else {
351             PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
352             trans.popDrawer();
353             pos.refresh();
354         }
355     }
356
357     public static void clearCache(PosScreen pos) {
358         if (!mgrLoggedIn) {
359             pos.showDialog("dialog/error/mgrnotloggedin");
360         } else {
361             UtilCache.clearAllCaches();
362             pos.refresh();
363         }
364     }
365
366     public static void resetXui(PosScreen pos) {
367         if (!mgrLoggedIn) {
368             pos.showDialog("dialog/error/mgrnotloggedin");
369         } else {
370             XProjectManager.getPageManager().reset();
371             pos.refresh();
372         }
373     }
374
375     public static void shutdown(PosScreen pos) {
376         if (!mgrLoggedIn) {
377             pos.showDialog("dialog/error/mgrnotloggedin");
378         } else {
379             pos.getOutput().print(UtilProperties.getMessage("pos","Shutting_down",Locale.getDefault()));
380             PosTransaction.getCurrentTx(pos.getSession()).closeTx();
381             System.exit(0);
382         }
383     }
384
385     public static void totalsReport(PosScreen pos) {
386         if (!mgrLoggedIn) {
387             pos.showDialog("dialog/error/mgrnotloggedin");
388             return;
389         }
390         printTotals(pos, null, false);
391     }
392
393     private static void printTotals(PosScreen pos, GenericValue state, boolean runBalance) {
394         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
395         if (!trans.isOpen()) {
396             pos.showDialog("dialog/error/terminalclosed");
397             return;
398         }
399         if (state == null) {
400             state = trans.getTerminalState();
401         }
402
403         double checkTotal = 0.00;
404         double cashTotal = 0.00;
405         double gcTotal = 0.00;
406         double ccTotal = 0.00;
407         double othTotal = 0.00;
408         double total = 0.00;
409
410         GenericDelegator delegator = pos.getSession().getDelegator();
411         List JavaDoc exprs = UtilMisc.toList(new EntityExpr("originFacilityId", EntityOperator.EQUALS, trans.getFacilityId()),
412                 new EntityExpr("terminalId", EntityOperator.EQUALS, trans.getTerminalId()));
413         EntityListIterator eli = null;
414
415         try {
416             eli = delegator.findListIteratorByCondition("OrderHeaderAndPaymentPref", new EntityConditionList(exprs, EntityOperator.AND), null, null);
417         } catch (GenericEntityException e) {
418             Debug.logError(e, module);
419         }
420
421         Timestamp JavaDoc dayStart = state.getTimestamp("openedDate");
422         Timestamp JavaDoc dayEnd = state.getTimestamp("closedDate");
423         if (dayEnd == null) {
424             dayEnd = UtilDateTime.nowTimestamp();
425         }
426
427         if (eli != null) {
428             GenericValue ohpp;
429             while (((ohpp = (GenericValue) eli.next()) != null)) {
430                 Timestamp JavaDoc orderDate = ohpp.getTimestamp("orderDate");
431                 if (orderDate.after(dayStart) && orderDate.before(dayEnd)) {
432                     String JavaDoc pmt = ohpp.getString("paymentMethodTypeId");
433                     Double JavaDoc amt = ohpp.getDouble("maxAmount");
434
435                     if ("CASH".equals(pmt)) {
436                         cashTotal += amt.doubleValue();
437                     } else if ("CHECK".equals(pmt)) {
438                         checkTotal += amt.doubleValue();
439                     } else if ("GIFT_CARD".equals(pmt)) {
440                         gcTotal += amt.doubleValue();
441                     } else if ("CREDIT_CARD".equals(pmt)) {
442                         ccTotal += amt.doubleValue();
443                     } else {
444                         othTotal += amt.doubleValue();
445                     }
446                     total += amt.doubleValue();
447                 }
448             }
449
450             try {
451                 eli.close();
452             } catch (GenericEntityException e) {
453                 Debug.logWarning(e, "Trouble closing ELI", module);
454             }
455         }
456
457         Map JavaDoc reportMap = new HashMap JavaDoc();
458         String JavaDoc reportTemplate = "totals.txt";
459         
460         // miscellaneous
461
reportMap.put("term", UtilFormatOut.padString(UtilProperties.getMessage("pos","term",Locale.getDefault()), 20, false, ' '));
462         reportMap.put("draw", UtilFormatOut.padString(UtilProperties.getMessage("pos","draw",Locale.getDefault()), 20, false, ' '));
463         reportMap.put("clerk", UtilFormatOut.padString(UtilProperties.getMessage("pos","clerk",Locale.getDefault()), 20, false, ' '));
464         reportMap.put("total_report", UtilFormatOut.padString(UtilProperties.getMessage("pos","total_report",Locale.getDefault()), 20, false, ' '));
465
466         // titles
467
reportMap.put("cashTitle", UtilFormatOut.padString(UtilProperties.getMessage("pos","CASH",Locale.getDefault()), 20, false, ' '));
468         reportMap.put("checkTitle", UtilFormatOut.padString(UtilProperties.getMessage("pos","CHECK",Locale.getDefault()), 20, false, ' '));
469         reportMap.put("giftCardTitle", UtilFormatOut.padString(UtilProperties.getMessage("pos","GIFT_CARD",Locale.getDefault()), 20, false, ' '));
470         reportMap.put("creditCardTitle", UtilFormatOut.padString(UtilProperties.getMessage("pos","CREDIT_CARD",Locale.getDefault()), 20, false, ' '));
471         reportMap.put("otherTitle", UtilFormatOut.padString(UtilProperties.getMessage("pos","OTHER",Locale.getDefault()), 20, false, ' '));
472         reportMap.put("grossSalesTitle", UtilFormatOut.padString(UtilProperties.getMessage("pos","GROSS_SALES",Locale.getDefault()), 20, false, ' '));
473         reportMap.put("+/-", UtilFormatOut.padString("+/-", 20, false, ' '));
474         reportMap.put("spacer", UtilFormatOut.padString("", 20, false, ' '));
475
476         // logged
477
reportMap.put("cashTotal", UtilFormatOut.padString(UtilFormatOut.formatPrice(cashTotal), 8, false, ' '));
478         reportMap.put("checkTotal", UtilFormatOut.padString(UtilFormatOut.formatPrice(checkTotal), 8, false, ' '));
479         reportMap.put("ccTotal", UtilFormatOut.padString(UtilFormatOut.formatPrice(ccTotal), 8, false, ' '));
480         reportMap.put("gcTotal", UtilFormatOut.padString(UtilFormatOut.formatPrice(gcTotal), 8, false, ' '));
481         reportMap.put("otherTotal", UtilFormatOut.padString(UtilFormatOut.formatPrice(othTotal), 8, false, ' '));
482         reportMap.put("grossTotal", UtilFormatOut.padString(UtilFormatOut.formatPrice(total), 8, false, ' '));
483
484         if (runBalance) {
485             // actuals
486
double cashEnd = state.getDouble("actualEndingCash").doubleValue();
487             double checkEnd = state.getDouble("actualEndingCheck").doubleValue();
488             double ccEnd = state.getDouble("actualEndingCc").doubleValue();
489             double gcEnd = state.getDouble("actualEndingGc").doubleValue();
490             double othEnd = state.getDouble("actualEndingOther").doubleValue();
491             double grossEnd = cashEnd + checkEnd + ccEnd + gcEnd + othEnd;
492
493             reportMap.put("cashEnd", UtilFormatOut.padString(UtilFormatOut.formatPrice(cashEnd), 8, false, ' '));
494             reportMap.put("checkEnd", UtilFormatOut.padString(UtilFormatOut.formatPrice(checkEnd), 8, false, ' '));
495             reportMap.put("ccEnd", UtilFormatOut.padString(UtilFormatOut.formatPrice(ccEnd), 8, false, ' '));
496             reportMap.put("gcEnd", UtilFormatOut.padString(UtilFormatOut.formatPrice(gcEnd), 8, false, ' '));
497             reportMap.put("otherEnd", UtilFormatOut.padString(UtilFormatOut.formatPrice(othEnd), 8, false, ' '));
498             reportMap.put("grossEnd", UtilFormatOut.padString(UtilFormatOut.formatPrice(grossEnd), 8, false, ' '));
499
500             // diffs
501
double cashDiff = cashEnd - cashTotal;
502             double checkDiff = checkEnd - checkTotal;
503             double ccDiff = ccEnd - ccTotal;
504             double gcDiff = gcEnd - gcTotal;
505             double othDiff = othEnd - othTotal;
506             double grossDiff = cashDiff + checkDiff + ccDiff + gcDiff + othDiff;
507
508             reportMap.put("cashDiff", UtilFormatOut.padString(UtilFormatOut.formatPrice(cashDiff), 8, false, ' '));
509             reportMap.put("checkDiff", UtilFormatOut.padString(UtilFormatOut.formatPrice(checkDiff), 8, false, ' '));
510             reportMap.put("ccDiff", UtilFormatOut.padString(UtilFormatOut.formatPrice(ccDiff), 8, false, ' '));
511             reportMap.put("gcDiff", UtilFormatOut.padString(UtilFormatOut.formatPrice(gcDiff), 8, false, ' '));
512             reportMap.put("otherDiff", UtilFormatOut.padString(UtilFormatOut.formatPrice(othDiff), 8, false, ' '));
513             reportMap.put("grossDiff", UtilFormatOut.padString(UtilFormatOut.formatPrice(grossDiff), 8, false, ' '));
514
515             // set the report template
516
reportTemplate = "balance.txt";
517         }
518
519         Receipt receipt = DeviceLoader.receipt;
520         if (receipt.isEnabled()) {
521             receipt.printReport(trans, reportTemplate, reportMap);
522         }
523     }
524 }
525
Popular Tags