KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensourcestrategies > financials > transactions > TransactionServices


1 /*
2  * Copyright (c) 2006 - 2007 Open Source Strategies, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the Honest Public License.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * Honest Public License for more details.
11  *
12  * You should have received a copy of the Honest Public License
13  * along with this program; if not, write to Funambol,
14  * 643 Bair Island Road, Suite 305 - Redwood City, CA 94063, USA
15  */

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 /**
32  * TransactionServices - Services for dealing with transactions
33  *
34  * @author <a HREF="mailto:libertine@ars-industria.com">Chris Liberty</a>
35  * @version $Rev: 553 $
36  */

37
38
39 public class TransactionServices {
40
41     public static final String JavaDoc module = TransactionServices.class.getName();
42
43     /**
44     * Service to reverse an AcctgTrans entity
45     */

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 JavaDoc acctgTransId = (String JavaDoc) 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             // Get the AcctgTrans record
61
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             // Get the related AcctgTransEntry records
67
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             // Toggle the debit/credit flag, set the reconcileStatusId and remove the acctgTransId from each AcctgTransEntry
73
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             // Assemble the context for the service that creates and posts AcctgTrans and AcctgTransEntry records
86
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             // Call the service
99
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 JavaDoc) 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     /**
118     * Service to void a payment
119     */

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 JavaDoc paymentId = (String JavaDoc) 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             // Get the Payment record
135
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             // Check the Payment status - only payments with status PMNT_SENT or PMNT_RECEIVED can be voided
141
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             // Change the Payment status to void
146
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             // Iterate through related PaymentApplications
152
List paymentApplications = delegator.getRelated("PaymentApplication", payment);
153             Iterator pait = paymentApplications.iterator();
154             while (pait.hasNext()) {
155                 GenericValue paymentApplication = (GenericValue) pait.next();
156                 
157                 // Set the status of related invoice from INVOICE_PAID to INVOICE_READY, if necessary
158
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                 // Remove the PaymentApplication
171
delegator.removeValue(paymentApplication);
172
173             }
174             
175             // Iterate through related AcctgTrans, calling the reverseAcctgTrans service on each
176
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