1 24 package org.ofbiz.order; 25 26 import java.text.NumberFormat ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 33 import javax.servlet.ServletContext ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 import javax.servlet.http.HttpSession ; 37 38 import org.ofbiz.base.util.Debug; 39 import org.ofbiz.base.util.UtilDateTime; 40 import org.ofbiz.base.util.UtilHttp; 41 import org.ofbiz.base.util.UtilMisc; 42 import org.ofbiz.base.util.UtilProperties; 43 import org.ofbiz.base.util.UtilValidate; 44 import org.ofbiz.entity.GenericDelegator; 45 import org.ofbiz.entity.GenericEntityException; 46 import org.ofbiz.entity.GenericValue; 47 import org.ofbiz.entity.condition.EntityExpr; 48 import org.ofbiz.entity.condition.EntityOperator; 49 import org.ofbiz.entity.util.EntityUtil; 50 import org.ofbiz.order.order.OrderChangeHelper; 51 import org.ofbiz.service.ModelService; 52 import org.ofbiz.service.LocalDispatcher; 53 import org.ofbiz.service.GenericServiceException; 54 55 62 public class OrderManagerEvents { 63 64 public static final String module = OrderManagerEvents.class.getName(); 65 public static final String resource_error = "OrderErrorUiLabels"; 66 67 public static String processOfflinePayments(HttpServletRequest request, HttpServletResponse response) { 68 HttpSession session = request.getSession(); 69 ServletContext application = ((ServletContext ) request.getAttribute("servletContext")); 70 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 71 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 72 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 73 Locale locale = UtilHttp.getLocale(request); 74 75 if (session.getAttribute("OFFLINE_PAYMENTS") != null) { 76 String orderId = (String ) request.getAttribute("orderId"); 77 List toBeStored = new LinkedList (); 78 List paymentPrefs = null; 79 GenericValue placingCustomer = null; 80 try { 81 paymentPrefs = delegator.findByAnd("OrderPaymentPreference", UtilMisc.toMap("orderId", orderId)); 82 List pRoles = delegator.findByAnd("OrderRole", UtilMisc.toMap("orderId", orderId, "roleTypeId", "PLACING_CUSTOMER")); 83 if (pRoles != null && pRoles.size() > 0) 84 placingCustomer = EntityUtil.getFirst(pRoles); 85 } catch (GenericEntityException e) { 86 Debug.logError(e, "Problems looking up order payment preferences", module); 87 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderErrorProcessingOfflinePayments", locale)); 88 return "error"; 89 } 90 if (paymentPrefs != null) { 91 Iterator i = paymentPrefs.iterator(); 92 while (i.hasNext()) { 93 GenericValue ppref = (GenericValue) i.next(); 96 ppref.set("statusId", "PAYMENT_RECEIVED"); 97 ppref.set("authDate", UtilDateTime.nowTimestamp()); 98 toBeStored.add(ppref); 99 100 Map results = null; 102 try { 103 results = dispatcher.runSync("createPaymentFromPreference", UtilMisc.toMap("orderPaymentPreferenceId", ppref.get("orderPaymentPreferenceId"), 104 "paymentFromId", placingCustomer.getString("partyId"), "comments", "Payment received offline and manually entered.")); 105 } catch (GenericServiceException e) { 106 Debug.logError(e, "Failed to execute service createPaymentFromPreference", module); 107 request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); 108 return "error"; 109 } 110 111 if ((results == null) || (results.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR))) { 112 Debug.logError((String ) results.get(ModelService.ERROR_MESSAGE), module); 113 request.setAttribute("_ERROR_MESSAGE_", (String ) results.get(ModelService.ERROR_MESSAGE)); 114 return "error"; 115 } 116 } 117 118 try { 120 delegator.storeAll(toBeStored); 121 } catch (GenericEntityException e) { 122 Debug.logError(e, "Problems storing payment information", module); 123 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderProblemStoringReceivedPaymentInformation", locale)); 124 return "error"; 125 } 126 127 OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId); 129 } 130 } 131 return "success"; 132 } 133 134 public static String receiveOfflinePayment(HttpServletRequest request, HttpServletResponse response) { 135 HttpSession session = request.getSession(); 136 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 137 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 138 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 139 Locale locale = UtilHttp.getLocale(request); 140 141 String orderId = request.getParameter("orderId"); 142 143 GenericValue orderHeader = null; 145 try { 146 orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId)); 147 } catch (GenericEntityException e) { 148 Debug.logError(e, "Problems reading order header from datasource.", module); 149 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderProblemsReadingOrderHeaderInformation", locale)); 150 return "error"; 151 } 152 153 Double grandTotal = new Double (0.00); 154 if (orderHeader != null) { 155 grandTotal = orderHeader.getDouble("grandTotal"); 156 } 157 158 List paymentMethodTypes = null; 160 161 try { 162 List pmtFields = UtilMisc.toList(new EntityExpr("paymentMethodTypeId", EntityOperator.NOT_EQUAL, "EXT_OFFLINE")); 163 paymentMethodTypes = delegator.findByAnd("PaymentMethodType", pmtFields); 164 } catch (GenericEntityException e) { 165 Debug.logError(e, "Problems getting payment types", module); 166 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderProblemsWithPaymentTypeLookup", locale)); 167 return "error"; 168 } 169 170 if (paymentMethodTypes == null) { 171 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderProblemsWithPaymentTypeLookup", locale)); 172 return "error"; 173 } 174 175 List toBeStored = new LinkedList (); 176 GenericValue placingCustomer = null; 177 try { 178 List pRoles = delegator.findByAnd("OrderRole", UtilMisc.toMap("orderId", orderId, "roleTypeId", "PLACING_CUSTOMER")); 179 if (pRoles != null && pRoles.size() > 0) 180 placingCustomer = EntityUtil.getFirst(pRoles); 181 } catch (GenericEntityException e) { 182 Debug.logError(e, "Problems looking up order payment preferences", module); 183 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderErrorProcessingOfflinePayments", locale)); 184 return "error"; 185 } 186 187 Iterator pmti = paymentMethodTypes.iterator(); 188 double paymentTally = 0.00; 189 while (pmti.hasNext()) { 190 GenericValue paymentMethodType = (GenericValue) pmti.next(); 191 String paymentMethodTypeId = paymentMethodType.getString("paymentMethodTypeId"); 192 String amountStr = request.getParameter(paymentMethodTypeId + "_amount"); 193 String paymentReference = request.getParameter(paymentMethodTypeId + "_reference"); 194 if (!UtilValidate.isEmpty(amountStr)) { 195 double paymentTypeAmount = 0.00; 196 try { 197 paymentTypeAmount = NumberFormat.getNumberInstance(locale).parse(amountStr).doubleValue(); 198 } catch (java.text.ParseException pe) { 199 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderProblemsPaymentParsingAmount", locale)); 200 return "error"; 201 } 202 if (paymentTypeAmount > 0.00) { 203 paymentTally += paymentTypeAmount; 204 205 Map prefFields = UtilMisc.toMap("orderPaymentPreferenceId", delegator.getNextSeqId("OrderPaymentPreference").toString()); 208 GenericValue paymentPreference = delegator.makeValue("OrderPaymentPreference", prefFields); 209 paymentPreference.set("paymentMethodTypeId", paymentMethodType.getString("paymentMethodTypeId")); 210 paymentPreference.set("maxAmount", new Double (paymentTypeAmount)); 211 paymentPreference.set("statusId", "PAYMENT_RECEIVED"); 212 paymentPreference.set("orderId", orderId); 213 paymentPreference.set("createdDate", UtilDateTime.nowTimestamp()); 214 if (userLogin != null) { 215 paymentPreference.set("createdByUserLogin", userLogin.getString("userLoginId")); 216 } 217 218 try { 219 delegator.create(paymentPreference); 220 } catch (GenericEntityException ex) { 221 Debug.logError(ex, "Cannot create a new OrderPaymentPreference", module); 222 request.setAttribute("_ERROR_MESSAGE_", ex.getMessage()); 223 return "error"; 224 } 225 226 Map results = null; 228 try { 229 results = dispatcher.runSync("createPaymentFromPreference", UtilMisc.toMap("userLogin", userLogin, 230 "orderPaymentPreferenceId", paymentPreference.get("orderPaymentPreferenceId"), "paymentRefNum", paymentReference, 231 "paymentFromId", placingCustomer.getString("partyId"), "comments", "Payment received offline and manually entered.")); 232 } catch (GenericServiceException e) { 233 Debug.logError(e, "Failed to execute service createPaymentFromPreference", module); 234 request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); 235 return "error"; 236 } 237 238 if ((results == null) || (results.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR))) { 239 Debug.logError((String ) results.get(ModelService.ERROR_MESSAGE), module); 240 request.setAttribute("_ERROR_MESSAGE_", (String ) results.get(ModelService.ERROR_MESSAGE)); 241 return "error"; 242 } 243 } 244 } 245 } 246 247 GenericValue offlineValue = null; 249 List currentPrefs = null; 250 try { 251 List oppFields = UtilMisc.toList(new EntityExpr("orderId", EntityOperator.EQUALS, orderId), 252 new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "PAYMENT_CANCELLED")); 253 currentPrefs = delegator.findByAnd("OrderPaymentPreference", oppFields); 254 } catch (GenericEntityException e) { 255 Debug.logError(e, "ERROR: Unable to get existing payment preferences from order", module); 256 } 257 if (currentPrefs != null && currentPrefs.size() > 0) { 258 Iterator cpi = currentPrefs.iterator(); 259 while (cpi.hasNext()) { 260 GenericValue cp = (GenericValue) cpi.next(); 261 String paymentMethodType = cp.getString("paymentMethodTypeId"); 262 if ("EXT_OFFLINE".equals(paymentMethodType)) { 263 offlineValue = cp; 264 } else { 265 Double cpAmt = cp.getDouble("maxAmount"); 266 if (cpAmt != null) { 267 paymentTally += cpAmt.doubleValue(); 268 } 269 } 270 } 271 } 272 273 boolean okayToApprove = false; 275 if (paymentTally >= grandTotal.doubleValue()) { 276 okayToApprove = true; 278 if (offlineValue != null) { 279 offlineValue.set("statusId", "PAYMENT_CANCELLED"); 280 toBeStored.add(offlineValue); 281 } 282 } 283 284 try { 287 delegator.storeAll(toBeStored); 288 } catch (GenericEntityException e) { 289 Debug.logError(e, "Problems storing payment information", module); 290 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderProblemStoringReceivedPaymentInformation", locale)); 291 return "error"; 292 } 293 294 if (okayToApprove) { 295 OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId); 297 } 298 299 return "success"; 300 } 301 302 } 303 | Popular Tags |