001: /*
002: * $Id: OrderManagerEvents.java,v 1.2 2003/11/01 18:01:49 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.order;
025:
026: import java.text.NumberFormat;
027: import java.util.Iterator;
028: import java.util.LinkedList;
029: import java.util.List;
030: import java.util.Map;
031:
032: import javax.servlet.ServletContext;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035: import javax.servlet.http.HttpSession;
036:
037: import org.ofbiz.base.util.Debug;
038: import org.ofbiz.base.util.UtilDateTime;
039: import org.ofbiz.base.util.UtilMisc;
040: import org.ofbiz.base.util.UtilValidate;
041: import org.ofbiz.entity.GenericDelegator;
042: import org.ofbiz.entity.GenericEntityException;
043: import org.ofbiz.entity.GenericValue;
044: import org.ofbiz.entity.condition.EntityExpr;
045: import org.ofbiz.entity.condition.EntityOperator;
046: import org.ofbiz.entity.util.EntityUtil;
047: import org.ofbiz.order.order.OrderChangeHelper;
048: import org.ofbiz.service.LocalDispatcher;
049:
050: /**
051: * Order Manager Events
052: *
053: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
054: * @version $Revision: 1.2 $
055: * @since 2.0
056: */
057: public class OrderManagerEvents {
058:
059: public static final String module = OrderManagerEvents.class
060: .getName();
061:
062: public static String processOfflinePayments(
063: HttpServletRequest request, HttpServletResponse response) {
064: HttpSession session = request.getSession();
065: ServletContext application = ((ServletContext) request
066: .getAttribute("servletContext"));
067: LocalDispatcher dispatcher = (LocalDispatcher) request
068: .getAttribute("dispatcher");
069: GenericDelegator delegator = (GenericDelegator) request
070: .getAttribute("delegator");
071: GenericValue userLogin = (GenericValue) session
072: .getAttribute("userLogin");
073:
074: if (session.getAttribute("OFFLINE_PAYMENTS") != null) {
075: String orderId = (String) request.getAttribute("order_id");
076: List toBeStored = new LinkedList();
077: List paymentPrefs = null;
078: GenericValue placingCustomer = null;
079: try {
080: paymentPrefs = delegator.findByAnd(
081: "OrderPaymentPreference", UtilMisc.toMap(
082: "orderId", orderId));
083: List pRoles = delegator.findByAnd("OrderRole", UtilMisc
084: .toMap("orderId", orderId, "roleTypeId",
085: "PLACING_CUSTOMER"));
086: if (pRoles != null && pRoles.size() > 0)
087: placingCustomer = EntityUtil.getFirst(pRoles);
088: } catch (GenericEntityException e) {
089: Debug
090: .logError(
091: e,
092: "Problems looking up order payment preferences",
093: module);
094: request.setAttribute("_ERROR_MESSAGE_",
095: "<li>Error processing offline payments.");
096: return "error";
097: }
098: if (paymentPrefs != null) {
099: Iterator i = paymentPrefs.iterator();
100: while (i.hasNext()) {
101: // update the preference to received
102: GenericValue ppref = (GenericValue) i.next();
103: ppref.set("statusId", "PAYMENT_RECEIVED");
104: ppref.set("authDate", UtilDateTime.nowTimestamp());
105: toBeStored.add(ppref);
106:
107: // create a payment record
108: toBeStored
109: .add(OrderChangeHelper
110: .createPaymentFromPreference(
111: ppref,
112: null,
113: placingCustomer
114: .getString("partyId"),
115: "Payment received offline and manually entered."));
116: }
117:
118: // store the updated preferences and newly created payments
119: try {
120: delegator.storeAll(toBeStored);
121: } catch (GenericEntityException e) {
122: Debug.logError(e,
123: "Problems storing payment information",
124: module);
125: request
126: .setAttribute("_ERROR_MESSAGE_",
127: "<li>Problem storing received payment information.");
128: return "error";
129: }
130:
131: // set the status of the order to approved
132: OrderChangeHelper.approveOrder(dispatcher, userLogin,
133: orderId);
134: }
135: }
136: return "success";
137: }
138:
139: public static String receiveOfflinePayment(
140: HttpServletRequest request, HttpServletResponse response) {
141: HttpSession session = request.getSession();
142: LocalDispatcher dispatcher = (LocalDispatcher) request
143: .getAttribute("dispatcher");
144: GenericDelegator delegator = (GenericDelegator) request
145: .getAttribute("delegator");
146: GenericValue userLogin = (GenericValue) session
147: .getAttribute("userLogin");
148:
149: String orderId = request.getParameter("orderId");
150:
151: // get the order header & payment preferences
152: GenericValue orderHeader = null;
153: try {
154: orderHeader = delegator.findByPrimaryKey("OrderHeader",
155: UtilMisc.toMap("orderId", orderId));
156: } catch (GenericEntityException e) {
157: Debug.logError(e,
158: "Problems reading order header from datasource.",
159: module);
160: request.setAttribute("_ERROR_MESSAGE_",
161: "<li>Problems reading order header information.");
162: return "error";
163: }
164:
165: Double grandTotal = new Double(0.00);
166: if (orderHeader != null)
167: grandTotal = orderHeader.getDouble("grandTotal");
168:
169: // get the payment types to receive
170: List paymentMethodTypes = null;
171:
172: try {
173: List pmtFields = UtilMisc.toList(new EntityExpr(
174: "paymentMethodTypeId", EntityOperator.NOT_EQUAL,
175: "EXT_OFFLINE"));
176: paymentMethodTypes = delegator.findByAnd(
177: "PaymentMethodType", pmtFields);
178: } catch (GenericEntityException e) {
179: Debug.logError(e, "Problems getting payment types", module);
180: request.setAttribute("_ERROR_MESSAGE_",
181: "<li>Problems with PaymentType lookup.");
182: return "error";
183: }
184:
185: if (paymentMethodTypes == null) {
186: request.setAttribute("_ERROR_MESSAGE_",
187: "<li>Problems with PaymentType lookup.");
188: return "error";
189: }
190:
191: List toBeStored = new LinkedList();
192: GenericValue placingCustomer = null;
193: try {
194: List pRoles = delegator.findByAnd("OrderRole", UtilMisc
195: .toMap("orderId", orderId, "roleTypeId",
196: "PLACING_CUSTOMER"));
197: if (pRoles != null && pRoles.size() > 0)
198: placingCustomer = EntityUtil.getFirst(pRoles);
199: } catch (GenericEntityException e) {
200: Debug.logError(e,
201: "Problems looking up order payment preferences",
202: module);
203: request.setAttribute("_ERROR_MESSAGE_",
204: "<li>Error processing offline payments.");
205: return "error";
206: }
207:
208: Iterator pmti = paymentMethodTypes.iterator();
209: double paymentTally = 0.00;
210: while (pmti.hasNext()) {
211: GenericValue paymentMethodType = (GenericValue) pmti.next();
212: String paymentMethodTypeId = paymentMethodType
213: .getString("paymentMethodTypeId");
214: String amountStr = request.getParameter(paymentMethodTypeId
215: + "_amount");
216: String paymentReference = request
217: .getParameter(paymentMethodTypeId + "_reference");
218: if (!UtilValidate.isEmpty(amountStr)) {
219: double paymentTypeAmount = 0.00;
220: try {
221: paymentTypeAmount = NumberFormat
222: .getNumberInstance().parse(amountStr)
223: .doubleValue();
224: } catch (java.text.ParseException pe) {
225: request.setAttribute("_ERROR_MESSAGE_",
226: "<li>Problems payment parsing amount.");
227: return "error";
228: }
229: if (paymentTypeAmount > 0.00) {
230: paymentTally += paymentTypeAmount;
231:
232: // create the OrderPaymentPreference
233: Map prefFields = UtilMisc.toMap(
234: "orderPaymentPreferenceId", delegator
235: .getNextSeqId(
236: "OrderPaymentPreference")
237: .toString());
238: GenericValue paymentPreference = delegator
239: .makeValue("OrderPaymentPreference",
240: prefFields);
241: paymentPreference.set("paymentMethodTypeId",
242: paymentMethodType
243: .getString("paymentMethodTypeId"));
244: paymentPreference.set("maxAmount", new Double(
245: paymentTypeAmount));
246: paymentPreference.set("statusId",
247: "PAYMENT_RECEIVED");
248: paymentPreference.set("orderId", orderId);
249: toBeStored.add(paymentPreference);
250:
251: // create a payment record
252: toBeStored
253: .add(OrderChangeHelper
254: .createPaymentFromPreference(
255: paymentPreference,
256: paymentReference,
257: placingCustomer
258: .getString("partyId"),
259: "Payment received offline and manually entered."));
260: }
261: }
262: }
263:
264: // get the current payment prefs
265: GenericValue offlineValue = null;
266: List currentPrefs = null;
267: try {
268: List oppFields = UtilMisc.toList(new EntityExpr("orderId",
269: EntityOperator.EQUALS, orderId), new EntityExpr(
270: "statusId", EntityOperator.NOT_EQUAL,
271: "PAYMENT_CANCELLED"));
272: currentPrefs = delegator.findByAnd(
273: "OrderPaymentPreference", oppFields);
274: } catch (GenericEntityException e) {
275: Debug
276: .logError(
277: e,
278: "ERROR: Unable to get existing payment preferences from order",
279: module);
280: }
281: if (currentPrefs != null && currentPrefs.size() > 0) {
282: Iterator cpi = currentPrefs.iterator();
283: while (cpi.hasNext()) {
284: GenericValue cp = (GenericValue) cpi.next();
285: String paymentMethodType = cp
286: .getString("paymentMethodTypeId");
287: if ("EXT_OFFLINE".equals(paymentMethodType)) {
288: offlineValue = cp;
289: } else {
290: Double cpAmt = cp.getDouble("maxAmount");
291: if (cpAmt != null) {
292: paymentTally += cpAmt.doubleValue();
293: }
294: }
295: }
296: }
297:
298: // now finish up
299: boolean okayToApprove = false;
300: if (paymentTally >= grandTotal.doubleValue()) {
301: // cancel the offline preference
302: okayToApprove = true;
303: if (offlineValue != null) {
304: offlineValue.set("statusId", "PAYMENT_CANCELLED");
305: toBeStored.add(offlineValue);
306: }
307: }
308:
309: // store the status changes and the newly created payment preferences and payments
310: try {
311: delegator.storeAll(toBeStored);
312: } catch (GenericEntityException e) {
313: Debug.logError(e, "Problems storing payment information",
314: module);
315: request
316: .setAttribute("_ERROR_MESSAGE_",
317: "<li>Problem storing received payment information.");
318: return "error";
319: }
320:
321: if (okayToApprove) {
322: // update the status of the order and items
323: OrderChangeHelper.approveOrder(dispatcher, userLogin,
324: orderId);
325: }
326:
327: return "success";
328: }
329:
330: }
|