1 25 package org.ofbiz.pos.event; 26 27 import org.ofbiz.base.util.Debug; 28 import org.ofbiz.base.util.GeneralException; 29 import org.ofbiz.base.util.UtilValidate; 30 import org.ofbiz.pos.PosTransaction; 31 import org.ofbiz.pos.component.Input; 32 import org.ofbiz.pos.component.Journal; 33 import org.ofbiz.pos.screen.PosScreen; 34 import org.ofbiz.base.util.UtilProperties; 35 36 import java.util.Locale ; 37 38 39 45 public class PaymentEvents { 46 47 public static final String module = PaymentEvents.class.getName(); 48 49 public static synchronized void payCash(PosScreen pos) { 50 PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession()); 51 trans.clearPayment("CASH"); 52 53 try { 55 double amount = processAmount(trans, pos, null); 56 Debug.log("Processing [Cash] Amount : " + amount, module); 57 58 trans.addPayment("CASH", amount); 60 } catch (GeneralException e) { 61 } 63 64 pos.refresh(); 65 } 66 67 public static synchronized void payCheck(PosScreen pos) { 68 PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession()); 69 Input input = pos.getInput(); 70 String [] ckInfo = input.getFunction("CHECK"); 71 72 int paymentCheck = trans.checkPaymentMethodType("PERSONAL_CHECK"); 74 if (paymentCheck == PosTransaction.NO_PAYMENT) { 75 processNoPayment(pos, "PERSONAL_CHECK"); 76 return; 77 } else if (paymentCheck == PosTransaction.EXTERNAL_PAYMENT) { 78 if (ckInfo == null) { 79 input.setFunction("CHECK"); 80 pos.getOutput().print(UtilProperties.getMessage("pos","REFNUM",Locale.getDefault())); 81 return; 82 } else { 83 processExternalPayment(pos, "PERSONAL_CHECK", ckInfo[1]); 84 return; 85 } 86 } 87 88 pos.showDialog("dialog/error/notyetsupported"); 90 } 91 92 public static synchronized void payGiftCard(PosScreen pos) { 93 PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession()); 94 Input input = pos.getInput(); 95 String [] gcInfo = input.getFunction("GIFTCARD"); 96 97 int paymentCheck = trans.checkPaymentMethodType("GIFT_CARD"); 99 if (paymentCheck == PosTransaction.NO_PAYMENT) { 100 processNoPayment(pos, "GIFT_CARD"); 101 return; 102 } else if (paymentCheck == PosTransaction.EXTERNAL_PAYMENT) { 103 if (gcInfo == null) { 104 input.setFunction("GIFTCARD"); 105 pos.getOutput().print(UtilProperties.getMessage("pos","REFNUM",Locale.getDefault())); 106 return; 107 } else { 108 processExternalPayment(pos, "GIFT_CARD", gcInfo[1]); 109 return; 110 } 111 } 112 113 pos.showDialog("dialog/error/notyetsupported"); 115 } 116 117 public static synchronized void payCredit(PosScreen pos) { 118 PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession()); 119 Input input = pos.getInput(); 120 String [] msrInfo = input.getFunction("MSRINFO"); 121 String [] crtInfo = input.getFunction("CREDIT"); 122 123 int paymentCheck = trans.checkPaymentMethodType("CREDIT_CARD"); 125 if (paymentCheck == PosTransaction.NO_PAYMENT) { 126 processNoPayment(pos, "CREDIT_CARD"); 127 return; 128 } else if (paymentCheck == PosTransaction.EXTERNAL_PAYMENT) { 129 if (crtInfo == null) { 130 input.setFunction("CREDIT"); 131 pos.getOutput().print(UtilProperties.getMessage("pos","REFNUM",Locale.getDefault())); 132 return; 133 } else { 134 processExternalPayment(pos, "CREDIT_CARD", crtInfo[1]); 135 return; 136 } 137 } 138 139 if (crtInfo == null) { 141 input.setFunction("CREDIT"); 142 pos.getOutput().print(UtilProperties.getMessage("pos","CREDNO",Locale.getDefault())); 143 } else { 144 Debug.log("Credit Func Info : " + crtInfo[1], module); 145 if (msrInfo == null) { 146 if (UtilValidate.isCreditCard(input.value())) { 147 input.setFunction("MSRINFO"); 148 pos.getOutput().print(UtilProperties.getMessage("pos","CREDEX",Locale.getDefault())); 149 } else { 150 Debug.log("Invalid card number - " + input.value(), module); 151 input.clearFunction("MSRINFO"); 152 input.clearInput(); 153 pos.showDialog("dialog/error/invalidcardnumber"); 154 } 155 } else { 156 String msrInfoStr = msrInfo[1]; 157 if (UtilValidate.isNotEmpty(input.value())) { 158 if (UtilValidate.isNotEmpty(msrInfoStr)) { 159 msrInfoStr = msrInfoStr + "|" + input.value(); 160 } else { 161 msrInfoStr = input.value(); 162 } 163 } 164 input.setFunction("MSRINFO", msrInfoStr); 165 String [] msrInfoArr = msrInfoStr.split("\\|"); 166 int allInfo = msrInfoArr.length; 167 String firstName = null; 168 String lastName = null; 169 switch (allInfo) { 170 case 4: 171 lastName = msrInfoArr[3]; 172 case 3: 173 firstName = msrInfoArr[2]; 174 case 2: double amount = 0; 176 try { 177 amount = processAmount(trans, pos, crtInfo[1]); 178 Debug.log("Processing Credit Card Amount : " + amount, module); 179 } catch (GeneralException e) { 180 } 181 182 String cardNumber = msrInfoArr[0]; 183 String expDate = msrInfoArr[1]; 184 String pmId = trans.makeCreditCardVo(cardNumber, expDate, firstName, lastName); 185 if (pmId != null) { 186 trans.addPayment(pmId, amount); 187 } 188 pos.refresh(); 189 break; 190 case 1: pos.getOutput().print(UtilProperties.getMessage("pos","CREDEX",Locale.getDefault())); 192 break; 193 default: 194 Debug.log("Hit the default switch case [" + allInfo + "] refreshing.", module); 195 input.clearFunction("MSRINFO"); 196 pos.getOutput().print(UtilProperties.getMessage("pos","CREDNO",Locale.getDefault())); 197 break; 198 } 199 } 200 } 201 } 202 203 private static void processNoPayment(PosScreen pos, String paymentMethodTypeId) { 204 PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession()); 205 206 try { 207 double amount = processAmount(trans, pos, null); 208 Debug.log("Processing [" + paymentMethodTypeId + "] Amount : " + amount, module); 209 210 trans.addPayment(paymentMethodTypeId, amount, "N/A", null); 212 } catch (GeneralException e) { 213 } 215 216 pos.refresh(); 217 } 218 219 private static void processExternalPayment(PosScreen pos, String paymentMethodTypeId, String amountStr) { 220 PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession()); 221 Input input = pos.getInput(); 222 String refNum = input.value(); 223 if (refNum == null) { 224 pos.getOutput().print(UtilProperties.getMessage("pos","REFNUM",Locale.getDefault())); 225 return; 226 } 227 input.clearInput(); 228 229 try { 230 double amount = processAmount(trans, pos, amountStr); 231 Debug.log("Processing [" + paymentMethodTypeId + "] Amount : " + amount, module); 232 233 trans.addPayment(paymentMethodTypeId, amount, refNum, null); 235 } catch (GeneralException e) { 236 } 238 239 pos.refresh(); 240 } 241 242 public static void clearPayment(PosScreen pos) { 243 PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession()); 244 Journal journal = pos.getJournal(); 245 String sku = journal.getSelectedSku(); 246 String idx = journal.getSelectedIdx(); 247 if (UtilValidate.isNotEmpty(idx) && UtilValidate.isEmpty(sku)) { 248 int index = -1; 249 try { 250 index = Integer.parseInt(idx); 251 } catch (Exception e) { 252 } 253 if (index > -1) { 254 trans.clearPayment(index); 255 } 256 } 257 pos.getInput().clearFunction("GIFTCARD"); 258 pos.getInput().clearFunction("CREDIT"); 259 pos.getInput().clearFunction("CHECK"); 260 pos.refresh(); 261 } 262 263 public static void clearAllPayments(PosScreen pos) { 264 PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession()); 265 trans.clearPayments(); 266 pos.getInput().clearFunction("GIFTCARD"); 267 pos.getInput().clearFunction("CREDIT"); 268 pos.getInput().clearFunction("CHECK"); 269 pos.refresh(); 270 } 271 272 public static void setRefNum(PosScreen pos) { 273 PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession()); 274 Journal journal = pos.getJournal(); 275 String sku = journal.getSelectedSku(); 276 String idx = journal.getSelectedIdx(); 277 278 if (UtilValidate.isNotEmpty(idx) && UtilValidate.isEmpty(sku)) { 279 String refNum = pos.getInput().value(); 280 if (UtilValidate.isEmpty(refNum)) { 281 pos.getOutput().print(UtilProperties.getMessage("pos","REFNUM",Locale.getDefault())); 282 pos.getInput().setFunction("REFNUM"); 283 } else { 284 int index = -1; 285 try { 286 index = Integer.parseInt(idx); 287 } catch (Exception e) { 288 } 289 if (index > -1) { 290 trans.setPaymentRefNum(index, refNum, refNum); 291 pos.refresh(); 292 } 293 } 294 } else { 295 pos.refresh(); 296 } 297 } 298 299 public static synchronized void processSale(PosScreen pos) { 300 PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession()); 301 PosScreen.currentScreen.getOutput().print(UtilProperties.getMessage("pos","Processing",Locale.getDefault())); 302 303 if (trans.isEmpty()) { 304 PosScreen newPos = pos.showPage("pospanel"); 305 newPos.showDialog("dialog/error/noitems"); 306 } else if (trans.getTotalDue() > 0) { 307 pos.showDialog("dialog/error/notenoughfunds"); 308 } else { 309 pos.getInput().setLock(true); 311 pos.getButtons().setLock(true); 312 pos.refresh(false); 313 314 try { 316 trans.processSale(pos.getOutput()); 317 pos.getInput().setFunction("PAID"); 318 } catch (GeneralException e) { 319 Debug.logError(e, e.getMessage(), module); 320 pos.getInput().setLock(false); 321 pos.getButtons().setLock(false); 322 pos.showDialog("dialog/error/exception", e.getMessage()); 323 } 324 } 325 } 326 327 private static double processAmount(PosTransaction trans, PosScreen pos, String amountStr) throws GeneralException { 328 Input input = pos.getInput(); 329 330 if (input.isFunctionSet("TOTAL")) { 331 String amtStr = amountStr != null ? amountStr : input.value(); 332 double amount; 333 if (UtilValidate.isNotEmpty(amtStr)) { 334 try { 335 amount = Double.parseDouble(amtStr); 336 } catch (NumberFormatException e) { 337 Debug.logError("Invalid number for amount : " + amtStr, module); 338 pos.getOutput().print("Invalid Amount!"); 339 input.clearInput(); 340 throw new GeneralException(); 341 } 342 amount = amount / 100; Debug.log("Set amount / 100 : " + amount, module); 344 } else { 345 Debug.log("Amount is empty; assumption is full amount : " + trans.getTotalDue(), module); 346 amount = trans.getTotalDue(); 347 if (amount <= 0) { 348 throw new GeneralException(); 349 } 350 } 351 return amount; 352 } else { 353 Debug.log("TOTAL function NOT set", module); 354 throw new GeneralException(); 355 } 356 } 357 } | Popular Tags |