KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > accounting > payment > PaymentWorker


1 /*
2  * $Id: PaymentWorker.java 7223 2006-04-07 02:16:27Z hansbak $
3  *
4  * Copyright 2003-2006 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7  * use this file except in compliance with the License. You may obtain a copy of
8  * the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */

18 package org.ofbiz.accounting.payment;
19
20 import java.math.BigDecimal JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.servlet.ServletRequest JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29
30 import org.ofbiz.base.util.Debug;
31 import org.ofbiz.base.util.UtilFormatOut;
32 import org.ofbiz.base.util.UtilMisc;
33 import org.ofbiz.base.util.UtilNumber;
34 import org.ofbiz.base.util.UtilValidate;
35 import org.ofbiz.entity.GenericDelegator;
36 import org.ofbiz.entity.GenericEntityException;
37 import org.ofbiz.entity.GenericValue;
38 import org.ofbiz.entity.util.EntityUtil;
39
40 /**
41  * Worker methods for Payments
42  *
43  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
44  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
45  * @since 2.0
46  */

47 public class PaymentWorker {
48     
49     public static final String JavaDoc module = PaymentWorker.class.getName();
50     private static int decimals = UtilNumber.getBigDecimalScale("invoice.decimals");
51     private static int rounding = UtilNumber.getBigDecimalRoundingMode("invoice.rounding");
52     
53     public static void getPartyPaymentMethodValueMaps(PageContext JavaDoc pageContext, String JavaDoc partyId, boolean showOld, String JavaDoc paymentMethodValueMapsAttr) {
54         GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator");
55         List JavaDoc paymentMethodValueMaps = getPartyPaymentMethodValueMaps(delegator, partyId, showOld);
56         pageContext.setAttribute(paymentMethodValueMapsAttr, paymentMethodValueMaps);
57     }
58
59     public static List JavaDoc getPartyPaymentMethodValueMaps(GenericDelegator delegator, String JavaDoc partyId, boolean showOld) {
60         List JavaDoc paymentMethodValueMaps = new LinkedList JavaDoc();
61         try {
62             List JavaDoc paymentMethods = delegator.findByAnd("PaymentMethod", UtilMisc.toMap("partyId", partyId));
63
64             if (!showOld) paymentMethods = EntityUtil.filterByDate(paymentMethods, true);
65             if (paymentMethods != null) {
66                 Iterator JavaDoc pmIter = paymentMethods.iterator();
67
68                 while (pmIter.hasNext()) {
69                     GenericValue paymentMethod = (GenericValue) pmIter.next();
70                     Map JavaDoc valueMap = new HashMap JavaDoc();
71
72                     paymentMethodValueMaps.add(valueMap);
73                     valueMap.put("paymentMethod", paymentMethod);
74                     if ("CREDIT_CARD".equals(paymentMethod.getString("paymentMethodTypeId"))) {
75                         GenericValue creditCard = paymentMethod.getRelatedOne("CreditCard");
76                         if (creditCard != null) valueMap.put("creditCard", creditCard);
77                     } else if ("GIFT_CARD".equals(paymentMethod.getString("paymentMethodTypeId"))) {
78                         GenericValue giftCard = paymentMethod.getRelatedOne("GiftCard");
79                         if (giftCard != null) valueMap.put("giftCard", giftCard);
80                     } else if ("EFT_ACCOUNT".equals(paymentMethod.getString("paymentMethodTypeId"))) {
81                         GenericValue eftAccount = paymentMethod.getRelatedOne("EftAccount");
82                         if (eftAccount != null) valueMap.put("eftAccount", eftAccount);
83                     }
84                 }
85             }
86         } catch (GenericEntityException e) {
87             Debug.logWarning(e, module);
88         }
89         return paymentMethodValueMaps;
90     }
91
92     /** TODO: REMOVE (DEJ 20030301): This is the OLD style and should be removed when the eCommerce and party mgr JSPs are */
93     public static void getPaymentMethodAndRelated(PageContext JavaDoc pageContext, String JavaDoc partyId,
94             String JavaDoc paymentMethodAttr, String JavaDoc creditCardAttr, String JavaDoc eftAccountAttr, String JavaDoc paymentMethodIdAttr, String JavaDoc curContactMechIdAttr,
95             String JavaDoc donePageAttr, String JavaDoc tryEntityAttr) {
96
97         ServletRequest JavaDoc request = pageContext.getRequest();
98         Map JavaDoc results = getPaymentMethodAndRelated(request, partyId);
99         
100         if (results.get("paymentMethod") != null) pageContext.setAttribute(paymentMethodAttr, results.get("paymentMethod"));
101         if (results.get("creditCard") != null) pageContext.setAttribute(creditCardAttr, results.get("creditCard"));
102         if (results.get("eftAccount") != null) pageContext.setAttribute(eftAccountAttr, results.get("eftAccount"));
103         if (results.get("paymentMethodId") != null) pageContext.setAttribute(paymentMethodIdAttr, results.get("paymentMethodId"));
104         if (results.get("curContactMechId") != null) pageContext.setAttribute(curContactMechIdAttr, results.get("curContactMechId"));
105         if (results.get("donePage") != null) pageContext.setAttribute(donePageAttr, results.get("donePage"));
106         if (results.get("tryEntity") != null) pageContext.setAttribute(tryEntityAttr, results.get("tryEntity"));
107     }
108     
109     public static Map JavaDoc getPaymentMethodAndRelated(ServletRequest JavaDoc request, String JavaDoc partyId) {
110         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
111         Map JavaDoc results = new HashMap JavaDoc();
112         
113         boolean tryEntity = true;
114         if (request.getAttribute("_ERROR_MESSAGE_") != null) tryEntity = false;
115
116         String JavaDoc donePage = request.getParameter("DONE_PAGE");
117         if (donePage == null || donePage.length() <= 0)
118             donePage = "viewprofile";
119         results.put("donePage", donePage);
120
121         String JavaDoc paymentMethodId = request.getParameter("paymentMethodId");
122
123         // check for a create
124
if (request.getAttribute("paymentMethodId") != null) {
125             paymentMethodId = (String JavaDoc) request.getAttribute("paymentMethodId");
126         }
127
128         // check for an update
129
if (request.getAttribute("newPaymentMethodId") != null) {
130             paymentMethodId = (String JavaDoc) request.getAttribute("newPaymentMethodId");
131         }
132
133         results.put("paymentMethodId", paymentMethodId);
134
135         GenericValue paymentMethod = null;
136         GenericValue creditCard = null;
137         GenericValue giftCard = null;
138         GenericValue eftAccount = null;
139
140         if (UtilValidate.isNotEmpty(paymentMethodId)) {
141             try {
142                 paymentMethod = delegator.findByPrimaryKey("PaymentMethod", UtilMisc.toMap("paymentMethodId", paymentMethodId));
143                 creditCard = delegator.findByPrimaryKey("CreditCard", UtilMisc.toMap("paymentMethodId", paymentMethodId));
144                 giftCard = delegator.findByPrimaryKey("GiftCard", UtilMisc.toMap("paymentMethodId", paymentMethodId));
145                 eftAccount = delegator.findByPrimaryKey("EftAccount", UtilMisc.toMap("paymentMethodId", paymentMethodId));
146             } catch (GenericEntityException e) {
147                 Debug.logWarning(e, module);
148             }
149         }
150         if (paymentMethod != null) {
151             results.put("paymentMethod", paymentMethod);
152         } else {
153             tryEntity = false;
154         }
155
156         if (creditCard != null) {
157             results.put("creditCard", creditCard);
158         }
159         if (giftCard != null) {
160             results.put("giftCard", giftCard);
161         }
162         if (eftAccount != null) {
163             results.put("eftAccount", eftAccount);
164         }
165
166         String JavaDoc curContactMechId = null;
167
168         if (creditCard != null) {
169             curContactMechId = UtilFormatOut.checkNull(tryEntity ? creditCard.getString("contactMechId") : request.getParameter("contactMechId"));
170         } else if (giftCard != null) {
171             curContactMechId = UtilFormatOut.checkNull(tryEntity ? giftCard.getString("contactMechId") : request.getParameter("contactMechId"));
172         } else if (eftAccount != null) {
173             curContactMechId = UtilFormatOut.checkNull(tryEntity ? eftAccount.getString("contactMechId") : request.getParameter("contactMechId"));
174         }
175         if (curContactMechId != null) {
176             results.put("curContactMechId", curContactMechId);
177         }
178
179         results.put("tryEntity", new Boolean JavaDoc(tryEntity));
180         
181         return results;
182     }
183             
184     public static GenericValue getPaymentAddress(GenericDelegator delegator, String JavaDoc partyId) {
185         List JavaDoc paymentAddresses = null;
186         try {
187             paymentAddresses = delegator.findByAnd("PartyContactMechPurpose",
188                 UtilMisc.toMap("partyId", partyId, "contactMechPurposeTypeId", "PAYMENT_LOCATION"),
189                 UtilMisc.toList("-fromDate"));
190             paymentAddresses = EntityUtil.filterByDate(paymentAddresses);
191         } catch (GenericEntityException e) {
192             Debug.logError(e, "Trouble getting PartyContactMechPurpose entity list", module);
193         }
194         
195         // get the address for the primary contact mech
196
GenericValue purpose = EntityUtil.getFirst(paymentAddresses);
197         GenericValue postalAddress = null;
198         if (purpose != null) {
199             try {
200                 postalAddress = delegator.findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId", purpose.getString("contactMechId")));
201             } catch (GenericEntityException e) {
202                 Debug.logError(e, "Trouble getting PostalAddress record for contactMechId: " + purpose.getString("contactMechId"), module);
203             }
204         }
205                                 
206         return postalAddress;
207     }
208     
209     /**
210      * Returns the total from a list of Payment entities
211      *
212      * @param payments List of Payment GenericValue items
213      * @return total payments as double
214      */

215     public static double getPaymentsTotal(List JavaDoc payments) {
216         if (payments == null) {
217             throw new IllegalArgumentException JavaDoc("Payment list cannot be null");
218         }
219         
220         double paymentsTotal = 0.00;
221         Iterator JavaDoc i = payments.iterator();
222         while (i.hasNext()) {
223             GenericValue payment = (GenericValue) i.next();
224             paymentsTotal += payment.getDouble("amount").doubleValue();
225         }
226         return paymentsTotal;
227     }
228
229     /**
230      * Method to return the total amount of an payment which is applied to a payment
231      * @param payment GenericValue object of the Payment
232      * @return the applied total as double
233      */

234     public static double getPaymentApplied(GenericDelegator delegator, String JavaDoc paymentId) {
235         return getPaymentAppliedBd(delegator, paymentId).doubleValue();
236     }
237     
238     public static BigDecimal JavaDoc getPaymentAppliedBd(GenericDelegator delegator, String JavaDoc paymentId) {
239         if (delegator == null) {
240             throw new IllegalArgumentException JavaDoc("Null delegator is not allowed in this method");
241         }
242         
243         GenericValue payment = null;
244         try {
245             payment = delegator.findByPrimaryKey("Payment", UtilMisc.toMap("paymentId", paymentId));
246         } catch (GenericEntityException e) {
247             Debug.logError(e, "Problem getting Payment", module);
248         }
249         
250         if (payment == null) {
251             throw new IllegalArgumentException JavaDoc("The paymentId passed does not match an existing payment");
252         }
253         
254         return getPaymentAppliedBd(payment);
255     }
256     /**
257      * Method to return the total amount of an payment which is applied to a payment
258      * @param payment GenericValue object of the Payment
259      * @return the applied total as double
260      */

261     public static double getPaymentApplied(GenericValue payment) {
262         return getPaymentAppliedBd(payment).doubleValue();
263     }
264
265     public static BigDecimal JavaDoc getPaymentAppliedBd(GenericValue payment) {
266         BigDecimal JavaDoc paymentApplied = new BigDecimal JavaDoc("0");
267         List JavaDoc paymentApplications = null;
268         try {
269             paymentApplications = payment.getRelated("PaymentApplication");
270         } catch (GenericEntityException e) {
271             Debug.logError(e, "Trouble getting paymentApplicationlist", module);
272         }
273         if (paymentApplications != null && paymentApplications.size() > 0) {
274             Iterator JavaDoc p = paymentApplications.iterator();
275             while (p.hasNext()) {
276                 GenericValue paymentApplication = (GenericValue) p.next();
277                 paymentApplied = paymentApplied.add(paymentApplication.getBigDecimal("amountApplied")).setScale(decimals,rounding);
278             }
279         }
280         // check for payment to payment applications
281
paymentApplications = null;
282         try {
283             paymentApplications = payment.getRelated("ToPaymentApplication");
284         } catch (GenericEntityException e) {
285             Debug.logError(e, "Trouble getting the 'to' paymentApplicationlist", module);
286         }
287         if (paymentApplications != null && paymentApplications.size() > 0) {
288             Iterator JavaDoc p = paymentApplications.iterator();
289             while (p.hasNext()) {
290                 GenericValue paymentApplication = (GenericValue) p.next();
291                 paymentApplied = paymentApplied.add(paymentApplication.getBigDecimal("amountApplied")).setScale(decimals,rounding);
292             }
293         }
294         return paymentApplied;
295     }
296     public static double getPaymentNotApplied(GenericValue payment) {
297         return getPaymentNotAppliedBd(payment).doubleValue();
298     }
299
300     public static BigDecimal JavaDoc getPaymentNotAppliedBd(GenericValue payment) {
301         return payment.getBigDecimal("amount").subtract(getPaymentAppliedBd(payment));
302     }
303     
304     public static BigDecimal JavaDoc getPaymentNotAppliedBd(GenericDelegator delegator, String JavaDoc paymentId) {
305         if (delegator == null) {
306             throw new IllegalArgumentException JavaDoc("Null delegator is not allowed in this method");
307         }
308         
309         GenericValue payment = null;
310         try {
311             payment = delegator.findByPrimaryKey("Payment", UtilMisc.toMap("paymentId", paymentId));
312         } catch (GenericEntityException e) {
313             Debug.logError(e, "Problem getting Payment", module);
314         }
315         
316         if (payment == null) {
317             throw new IllegalArgumentException JavaDoc("The paymentId passed does not match an existing payment");
318         }
319         
320         return org.ofbiz.accounting.payment.PaymentWorker.getPaymentNotAppliedBd(payment);
321     }
322     
323 }
324
Popular Tags