1 18 package org.ofbiz.accounting.payment; 19 20 import java.math.BigDecimal ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import javax.servlet.ServletRequest ; 28 import javax.servlet.jsp.PageContext ; 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 47 public class PaymentWorker { 48 49 public static final String 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 pageContext, String partyId, boolean showOld, String paymentMethodValueMapsAttr) { 54 GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator"); 55 List paymentMethodValueMaps = getPartyPaymentMethodValueMaps(delegator, partyId, showOld); 56 pageContext.setAttribute(paymentMethodValueMapsAttr, paymentMethodValueMaps); 57 } 58 59 public static List getPartyPaymentMethodValueMaps(GenericDelegator delegator, String partyId, boolean showOld) { 60 List paymentMethodValueMaps = new LinkedList (); 61 try { 62 List paymentMethods = delegator.findByAnd("PaymentMethod", UtilMisc.toMap("partyId", partyId)); 63 64 if (!showOld) paymentMethods = EntityUtil.filterByDate(paymentMethods, true); 65 if (paymentMethods != null) { 66 Iterator pmIter = paymentMethods.iterator(); 67 68 while (pmIter.hasNext()) { 69 GenericValue paymentMethod = (GenericValue) pmIter.next(); 70 Map valueMap = new HashMap (); 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 93 public static void getPaymentMethodAndRelated(PageContext pageContext, String partyId, 94 String paymentMethodAttr, String creditCardAttr, String eftAccountAttr, String paymentMethodIdAttr, String curContactMechIdAttr, 95 String donePageAttr, String tryEntityAttr) { 96 97 ServletRequest request = pageContext.getRequest(); 98 Map 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 getPaymentMethodAndRelated(ServletRequest request, String partyId) { 110 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 111 Map results = new HashMap (); 112 113 boolean tryEntity = true; 114 if (request.getAttribute("_ERROR_MESSAGE_") != null) tryEntity = false; 115 116 String donePage = request.getParameter("DONE_PAGE"); 117 if (donePage == null || donePage.length() <= 0) 118 donePage = "viewprofile"; 119 results.put("donePage", donePage); 120 121 String paymentMethodId = request.getParameter("paymentMethodId"); 122 123 if (request.getAttribute("paymentMethodId") != null) { 125 paymentMethodId = (String ) request.getAttribute("paymentMethodId"); 126 } 127 128 if (request.getAttribute("newPaymentMethodId") != null) { 130 paymentMethodId = (String ) 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 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 (tryEntity)); 180 181 return results; 182 } 183 184 public static GenericValue getPaymentAddress(GenericDelegator delegator, String partyId) { 185 List 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 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 215 public static double getPaymentsTotal(List payments) { 216 if (payments == null) { 217 throw new IllegalArgumentException ("Payment list cannot be null"); 218 } 219 220 double paymentsTotal = 0.00; 221 Iterator 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 234 public static double getPaymentApplied(GenericDelegator delegator, String paymentId) { 235 return getPaymentAppliedBd(delegator, paymentId).doubleValue(); 236 } 237 238 public static BigDecimal getPaymentAppliedBd(GenericDelegator delegator, String paymentId) { 239 if (delegator == null) { 240 throw new IllegalArgumentException ("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 ("The paymentId passed does not match an existing payment"); 252 } 253 254 return getPaymentAppliedBd(payment); 255 } 256 261 public static double getPaymentApplied(GenericValue payment) { 262 return getPaymentAppliedBd(payment).doubleValue(); 263 } 264 265 public static BigDecimal getPaymentAppliedBd(GenericValue payment) { 266 BigDecimal paymentApplied = new BigDecimal ("0"); 267 List 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 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 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 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 getPaymentNotAppliedBd(GenericValue payment) { 301 return payment.getBigDecimal("amount").subtract(getPaymentAppliedBd(payment)); 302 } 303 304 public static BigDecimal getPaymentNotAppliedBd(GenericDelegator delegator, String paymentId) { 305 if (delegator == null) { 306 throw new IllegalArgumentException ("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 ("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 |