1 25 package org.ofbiz.pos.event; 26 27 import java.util.List ; 28 import java.awt.AWTEvent ; 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 49 public class MenuEvents { 50 51 public static final String module = MenuEvents.class.getName(); 52 53 public static void triggerClear(PosScreen pos) { 55 String [] totalFunc = pos.getInput().getFunction("TOTAL"); 57 String [] 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 pos.refresh(); 72 73 if (!pos.isLocked()) { 75 pos.getInput().setLock(false); 76 pos.getButtons().setLock(false); 77 } else { 78 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 event) { 88 Input input = pos.getInput(); 90 String [] 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 event) { 125 PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession()); 126 Input input = pos.getInput(); 127 String [] func = input.getFunction("QTY"); 128 String value = input.value(); 129 130 if (event != null && UtilValidate.isEmpty(value)) { 132 String 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 double quantity = 1; 149 if (func != null && "QTY".equals(func[0])) { 150 try { 151 quantity = Double.parseDouble(func[1]); 152 } catch (NumberFormatException e) { 153 quantity = 1; 154 } 155 } 156 157 String productId = null; 159 try { 160 List 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 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 input.clearFunction("QTY"); 189 190 trans.calcTax(); 192 193 pos.refresh(); 195 } 196 197 public static void changeQty(PosScreen pos) { 198 PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession()); 199 String sku = null; 200 try { 201 sku = getSelectedItem(pos); 202 } catch (ArrayIndexOutOfBoundsException 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 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 e) { 220 quantity = 1; 221 } 222 } else { 223 String [] 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 e) { 229 quantity = trans.getItemQuantity(sku); 230 } 231 } 232 } 233 234 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 input.clearFunction("QTY"); 246 247 trans.calcTax(); 249 250 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 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 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 sku = null; 287 try { 288 sku = getSelectedItem(pos); 289 } catch (ArrayIndexOutOfBoundsException 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 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 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 sku = null; 338 try { 339 sku = getSelectedItem(pos); 340 } catch (ArrayIndexOutOfBoundsException 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 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 getSelectedItem(PosScreen pos) { 367 Journal journal = pos.getJournal(); 368 return journal.getSelectedSku(); 369 } 370 } 371 | Popular Tags |