001: /*
002: * $Id: PaymentWorker.java,v 1.6 2003/11/04 18:46:29 ajzeneski Exp $
003: *
004: * Copyright (c) 2003 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.accounting.payment;
025:
026: import java.util.HashMap;
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.ServletRequest;
033: import javax.servlet.jsp.PageContext;
034:
035: import org.ofbiz.base.util.Debug;
036: import org.ofbiz.base.util.UtilFormatOut;
037: import org.ofbiz.base.util.UtilMisc;
038: import org.ofbiz.base.util.UtilValidate;
039: import org.ofbiz.entity.GenericDelegator;
040: import org.ofbiz.entity.GenericEntityException;
041: import org.ofbiz.entity.GenericValue;
042: import org.ofbiz.entity.util.EntityUtil;
043:
044: /**
045: * Worker methods for Payments
046: *
047: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
048: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
049: * @version $Revision: 1.6 $
050: * @since 2.0
051: */
052: public class PaymentWorker {
053:
054: public static final String module = PaymentWorker.class.getName();
055:
056: public static void getPartyPaymentMethodValueMaps(
057: PageContext pageContext, String partyId, boolean showOld,
058: String paymentMethodValueMapsAttr) {
059: GenericDelegator delegator = (GenericDelegator) pageContext
060: .getRequest().getAttribute("delegator");
061: List paymentMethodValueMaps = getPartyPaymentMethodValueMaps(
062: delegator, partyId, showOld);
063: pageContext.setAttribute(paymentMethodValueMapsAttr,
064: paymentMethodValueMaps);
065: }
066:
067: public static List getPartyPaymentMethodValueMaps(
068: GenericDelegator delegator, String partyId, boolean showOld) {
069: List paymentMethodValueMaps = new LinkedList();
070: try {
071: List paymentMethods = delegator.findByAnd("PaymentMethod",
072: UtilMisc.toMap("partyId", partyId));
073:
074: if (!showOld)
075: paymentMethods = EntityUtil.filterByDate(
076: paymentMethods, true);
077: if (paymentMethods != null) {
078: Iterator pmIter = paymentMethods.iterator();
079:
080: while (pmIter.hasNext()) {
081: GenericValue paymentMethod = (GenericValue) pmIter
082: .next();
083: Map valueMap = new HashMap();
084:
085: paymentMethodValueMaps.add(valueMap);
086: valueMap.put("paymentMethod", paymentMethod);
087: if ("CREDIT_CARD".equals(paymentMethod
088: .getString("paymentMethodTypeId"))) {
089: GenericValue creditCard = paymentMethod
090: .getRelatedOne("CreditCard");
091: if (creditCard != null)
092: valueMap.put("creditCard", creditCard);
093: } else if ("GIFT_CARD".equals(paymentMethod
094: .getString("paymentMethodTypeId"))) {
095: GenericValue giftCard = paymentMethod
096: .getRelatedOne("GiftCard");
097: if (giftCard != null)
098: valueMap.put("giftCard", giftCard);
099: } else if ("EFT_ACCOUNT".equals(paymentMethod
100: .getString("paymentMethodTypeId"))) {
101: GenericValue eftAccount = paymentMethod
102: .getRelatedOne("EftAccount");
103: if (eftAccount != null)
104: valueMap.put("eftAccount", eftAccount);
105: }
106: }
107: }
108: } catch (GenericEntityException e) {
109: Debug.logWarning(e, module);
110: }
111: return paymentMethodValueMaps;
112: }
113:
114: /** TODO: REMOVE (DEJ 20030301): This is the OLD style and should be removed when the eCommerce and party mgr JSPs are */
115: public static void getPaymentMethodAndRelated(
116: PageContext pageContext, String partyId,
117: String paymentMethodAttr, String creditCardAttr,
118: String eftAccountAttr, String paymentMethodIdAttr,
119: String curContactMechIdAttr, String donePageAttr,
120: String tryEntityAttr) {
121:
122: ServletRequest request = pageContext.getRequest();
123: Map results = getPaymentMethodAndRelated(request, partyId);
124:
125: if (results.get("paymentMethod") != null)
126: pageContext.setAttribute(paymentMethodAttr, results
127: .get("paymentMethod"));
128: if (results.get("creditCard") != null)
129: pageContext.setAttribute(creditCardAttr, results
130: .get("creditCard"));
131: if (results.get("eftAccount") != null)
132: pageContext.setAttribute(eftAccountAttr, results
133: .get("eftAccount"));
134: if (results.get("paymentMethodId") != null)
135: pageContext.setAttribute(paymentMethodIdAttr, results
136: .get("paymentMethodId"));
137: if (results.get("curContactMechId") != null)
138: pageContext.setAttribute(curContactMechIdAttr, results
139: .get("curContactMechId"));
140: if (results.get("donePage") != null)
141: pageContext.setAttribute(donePageAttr, results
142: .get("donePage"));
143: if (results.get("tryEntity") != null)
144: pageContext.setAttribute(tryEntityAttr, results
145: .get("tryEntity"));
146: }
147:
148: public static Map getPaymentMethodAndRelated(
149: ServletRequest request, String partyId) {
150: GenericDelegator delegator = (GenericDelegator) request
151: .getAttribute("delegator");
152: Map results = new HashMap();
153:
154: boolean tryEntity = true;
155: if (request.getAttribute("_ERROR_MESSAGE_") != null)
156: tryEntity = false;
157:
158: String donePage = request.getParameter("DONE_PAGE");
159: if (donePage == null || donePage.length() <= 0)
160: donePage = "viewprofile";
161: results.put("donePage", donePage);
162:
163: String paymentMethodId = request
164: .getParameter("paymentMethodId");
165:
166: // check for a create
167: if (request.getAttribute("paymentMethodId") != null) {
168: paymentMethodId = (String) request
169: .getAttribute("paymentMethodId");
170: }
171:
172: // check for an update
173: if (request.getAttribute("newPaymentMethodId") != null) {
174: paymentMethodId = (String) request
175: .getAttribute("newPaymentMethodId");
176: }
177:
178: results.put("paymentMethodId", paymentMethodId);
179:
180: GenericValue paymentMethod = null;
181: GenericValue creditCard = null;
182: GenericValue giftCard = null;
183: GenericValue eftAccount = null;
184:
185: if (UtilValidate.isNotEmpty(paymentMethodId)) {
186: try {
187: paymentMethod = delegator.findByPrimaryKey(
188: "PaymentMethod", UtilMisc.toMap(
189: "paymentMethodId", paymentMethodId));
190: creditCard = delegator.findByPrimaryKey("CreditCard",
191: UtilMisc.toMap("paymentMethodId",
192: paymentMethodId));
193: giftCard = delegator.findByPrimaryKey("GiftCard",
194: UtilMisc.toMap("paymentMethodId",
195: paymentMethodId));
196: eftAccount = delegator.findByPrimaryKey("EftAccount",
197: UtilMisc.toMap("paymentMethodId",
198: paymentMethodId));
199: } catch (GenericEntityException e) {
200: Debug.logWarning(e, module);
201: }
202: }
203: if (paymentMethod != null) {
204: results.put("paymentMethod", paymentMethod);
205: } else {
206: tryEntity = false;
207: }
208:
209: if (creditCard != null) {
210: results.put("creditCard", creditCard);
211: }
212: if (giftCard != null) {
213: results.put("giftCard", giftCard);
214: }
215: if (eftAccount != null) {
216: results.put("eftAccount", eftAccount);
217: }
218:
219: String curContactMechId = null;
220:
221: if (creditCard != null) {
222: curContactMechId = UtilFormatOut
223: .checkNull(tryEntity ? creditCard
224: .getString("contactMechId") : request
225: .getParameter("contactMechId"));
226: } else if (giftCard != null) {
227: curContactMechId = UtilFormatOut
228: .checkNull(tryEntity ? giftCard
229: .getString("contactMechId") : request
230: .getParameter("contactMechId"));
231: } else if (eftAccount != null) {
232: curContactMechId = UtilFormatOut
233: .checkNull(tryEntity ? eftAccount
234: .getString("contactMechId") : request
235: .getParameter("contactMechId"));
236: }
237: if (curContactMechId != null) {
238: results.put("curContactMechId", curContactMechId);
239: }
240:
241: results.put("tryEntity", new Boolean(tryEntity));
242:
243: return results;
244: }
245:
246: public static GenericValue getPaymentAddress(
247: GenericDelegator delegator, String partyId) {
248: List paymentAddresses = null;
249: try {
250: paymentAddresses = delegator.findByAnd(
251: "PartyContactMechPurpose", UtilMisc.toMap(
252: "partyId", partyId,
253: "contactMechPurposeTypeId",
254: "PAYMENT_LOCATION"), UtilMisc
255: .toList("-fromDate"));
256: paymentAddresses = EntityUtil
257: .filterByDate(paymentAddresses);
258: } catch (GenericEntityException e) {
259: Debug
260: .logError(
261: e,
262: "Trouble getting PartyContactMechPurpose entity list",
263: module);
264: }
265:
266: // get the address for the primary contact mech
267: GenericValue purpose = EntityUtil.getFirst(paymentAddresses);
268: GenericValue postalAddress = null;
269: if (purpose != null) {
270: try {
271: postalAddress = delegator.findByPrimaryKey(
272: "PostalAddress", UtilMisc.toMap(
273: "contactMechId", purpose
274: .getString("contactMechId")));
275: } catch (GenericEntityException e) {
276: Debug.logError(e,
277: "Trouble getting PostalAddress record for contactMechId: "
278: + purpose.getString("contactMechId"),
279: module);
280: }
281: }
282:
283: return postalAddress;
284: }
285:
286: /**
287: * Returns the total from a list of Payment entities
288: *
289: * @param payments List of Payment GenericValue items
290: * @return total payments as double
291: */
292: public static double getPaymentsTotal(List payments) {
293: if (payments == null) {
294: throw new IllegalArgumentException(
295: "Payment list cannot be null");
296: }
297:
298: double paymentsTotal = 0.00;
299: Iterator i = payments.iterator();
300: while (i.hasNext()) {
301: GenericValue payment = (GenericValue) i.next();
302: paymentsTotal += payment.getDouble("amount").doubleValue();
303: }
304: return paymentsTotal;
305: }
306: }
|