1 16 17 package com.opensourcestrategies.financials.transactions; 18 19 import java.util.*; 20 21 import org.ofbiz.base.util.*; 22 import org.ofbiz.entity.GenericDelegator; 23 import org.ofbiz.entity.GenericEntityException; 24 import org.ofbiz.entity.GenericValue; 25 import org.ofbiz.service.DispatchContext; 26 import org.ofbiz.service.GenericServiceException; 27 import org.ofbiz.service.LocalDispatcher; 28 import org.ofbiz.service.ServiceUtil; 29 import org.ofbiz.security.Security; 30 31 37 38 39 public class TransactionServices { 40 41 public static final String module = TransactionServices.class.getName(); 42 43 46 public static Map reverseAcctgTrans(DispatchContext dctx, Map context) { 47 GenericDelegator delegator = dctx.getDelegator(); 48 LocalDispatcher dispatcher = dctx.getDispatcher(); 49 GenericValue userLogin = (GenericValue) context.get("userLogin"); 50 Security security = dctx.getSecurity(); 51 Locale locale = (Locale) context.get("locale"); 52 String acctgTransId = (String ) context.get("acctgTransId"); 53 54 if (!security.hasEntityPermission("FINANCIALS", "_REVERSE", userLogin)) { 55 return ServiceUtil.returnError(UtilProperties.getMessage("FinancialsUiLabels", "FinancialsServiceErrorNoPermission", locale)); 56 } 57 58 try { 59 60 GenericValue acctgTrans = delegator.findByPrimaryKey("AcctgTrans", UtilMisc.toMap("acctgTransId", acctgTransId)); 62 if( acctgTrans == null) { 63 return ServiceUtil.returnError(UtilProperties.getMessage("FinancialsUiLabels", "FinancialsServiceErrorReverseTransactionNotFound", locale) + ":" + acctgTransId); 64 } 65 66 List acctgTransEntries = delegator.findByAnd("AcctgTransEntry", UtilMisc.toMap("acctgTransId", acctgTransId), UtilMisc.toList("acctgTransEntrySeqId")); 68 if( acctgTrans == null) { 69 return ServiceUtil.returnError(UtilProperties.getMessage("FinancialsUiLabels", "FinancialsServiceErrorReverseTransactionNoEntries", locale) + ":" + acctgTransId); 70 } 71 72 Iterator ateit = acctgTransEntries.iterator(); 74 while(ateit.hasNext()) { 75 GenericValue acctgTransEntry = (GenericValue) ateit.next(); 76 if (acctgTransEntry.getString("debitCreditFlag").equals("C")) { 77 acctgTransEntry.set("debitCreditFlag", "D"); 78 } else if (acctgTransEntry.getString("debitCreditFlag").equals("D")) { 79 acctgTransEntry.set("debitCreditFlag", "C"); 80 } 81 acctgTransEntry.set("reconcileStatusId", "AES_NOT_RECONCILED"); 82 acctgTransEntry.remove("acctgTransId"); 83 } 84 85 Map serviceMap = acctgTrans.getAllFields(); 87 serviceMap.remove("acctgTransId"); 88 serviceMap.remove("createdStamp"); 89 serviceMap.remove("createdTxStamp"); 90 serviceMap.remove("lastUpdatedStamp"); 91 serviceMap.remove("lastUpdatedTxStamp"); 92 serviceMap.put("transactionDate", UtilDateTime.nowTimestamp()); 93 serviceMap.put("description", "Reversal of Acctg Trans ID# " + acctgTransId); 94 serviceMap.put("acctgTransTypeId", "REVERSE"); 95 serviceMap.put("acctgTransEntries", acctgTransEntries); 96 serviceMap.put("userLogin", userLogin); 97 98 Map createAcctgTransAndEntriesResult = dispatcher.runSync("createAcctgTransAndEntries", serviceMap); 100 if (ServiceUtil.isError(createAcctgTransAndEntriesResult)) { 101 return createAcctgTransAndEntriesResult; 102 } 103 104 Map serviceResult = ServiceUtil.returnSuccess(); 105 serviceResult.put("acctgTransId", (String ) createAcctgTransAndEntriesResult.get("acctgTransId")); 106 return serviceResult; 107 108 } catch( GenericEntityException e ) { 109 Debug.logError(e.getMessage(), module); 110 return ServiceUtil.returnError(e.getMessage()); 111 } catch( GenericServiceException e ) { 112 Debug.logError(e.getMessage(), module); 113 return ServiceUtil.returnError(e.getMessage()); 114 } 115 } 116 117 120 public static Map voidPayment(DispatchContext dctx, Map context) { 121 GenericDelegator delegator = dctx.getDelegator(); 122 LocalDispatcher dispatcher = dctx.getDispatcher(); 123 GenericValue userLogin = (GenericValue) context.get("userLogin"); 124 Security security = dctx.getSecurity(); 125 Locale locale = (Locale) context.get("locale"); 126 String paymentId = (String ) context.get("paymentId"); 127 128 if (! security.hasEntityPermission("FINANCIALS", "_REVERSE", userLogin)) { 129 return ServiceUtil.returnError(UtilProperties.getMessage("FinancialsUiLabels", "FinancialsServiceErrorNoPermission", locale)); 130 } 131 132 try { 133 134 GenericValue payment = delegator.findByPrimaryKey("Payment", UtilMisc.toMap("paymentId", paymentId)); 136 if( payment == null) { 137 return ServiceUtil.returnError(UtilProperties.getMessage("FinancialsUiLabels", "FinancialsServiceErrorVoidPaymentNotFound", locale) + ":" + payment); 138 } 139 140 if (! (payment.getString("statusId").equals("PMNT_SENT") || payment.getString("statusId").equals("PMNT_RECEIVED"))) { 142 return ServiceUtil.returnError(UtilProperties.getMessage("FinancialsUiLabels", "FinancialsServiceErrorVoidPaymentIncorrectStatus", locale) + ":" + payment); 143 } 144 145 Map setPaymentStatusResult = dispatcher.runSync("setPaymentStatus", UtilMisc.toMap("paymentId", paymentId, "statusId", "PMNT_VOID", "userLogin", userLogin)); 147 if (ServiceUtil.isError(setPaymentStatusResult)) { 148 return setPaymentStatusResult; 149 } 150 151 List paymentApplications = delegator.getRelated("PaymentApplication", payment); 153 Iterator pait = paymentApplications.iterator(); 154 while (pait.hasNext()) { 155 GenericValue paymentApplication = (GenericValue) pait.next(); 156 157 if (paymentApplication.getString("invoiceId") != null) { 159 GenericValue invoice = delegator.getRelatedOne("Invoice", paymentApplication); 160 if (invoice.getString("statusId").equals("INVOICE_PAID")) { 161 invoice.set("paidDate", null); 162 delegator.store(invoice); 163 Map setInvoiceStatusResult = dispatcher.runSync("setInvoiceStatus", UtilMisc.toMap("invoiceId", invoice.getString("invoiceId"), "statusId", "INVOICE_READY", "userLogin", userLogin)); 164 if (ServiceUtil.isError(setInvoiceStatusResult)) { 165 return setInvoiceStatusResult; 166 } 167 } 168 } 169 170 delegator.removeValue(paymentApplication); 172 173 } 174 175 List acctgTransList = delegator.getRelated("AcctgTrans", payment); 177 Iterator atlit = acctgTransList.iterator(); 178 while (atlit.hasNext()) { 179 GenericValue acctgTrans = (GenericValue) atlit.next(); 180 Map reverseAcctgTransResult = dispatcher.runSync("reverseAcctgTrans", UtilMisc.toMap("acctgTransId", acctgTrans.getString("acctgTransId"), "userLogin", userLogin)); 181 if (ServiceUtil.isError(reverseAcctgTransResult)) { 182 return reverseAcctgTransResult; 183 } 184 185 186 } 187 188 return ServiceUtil.returnSuccess(); 189 190 } catch( GenericEntityException e ) { 191 Debug.logError(e.getMessage(), module); 192 return ServiceUtil.returnError(e.getMessage()); 193 } catch( GenericServiceException e ) { 194 Debug.logError(e.getMessage(), module); 195 return ServiceUtil.returnError(e.getMessage()); 196 } 197 } 198 } 199 | Popular Tags |