KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensourcestrategies > financials > configuration > ConfigurationServices


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.configuration;
18
19 import java.util.Date JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.sql.Timestamp JavaDoc;
26 import java.math.BigDecimal JavaDoc;
27
28 import javolution.util.FastMap;
29
30 import org.ofbiz.accounting.util.UtilAccounting;
31 import org.ofbiz.base.util.Debug;
32 import org.ofbiz.base.util.UtilDateTime;
33 import org.ofbiz.base.util.UtilMisc;
34 import org.ofbiz.base.util.UtilNumber;
35 import org.ofbiz.base.util.UtilProperties;
36 import org.ofbiz.entity.GenericDelegator;
37 import org.ofbiz.entity.GenericEntityException;
38 import org.ofbiz.entity.GenericValue;
39 import org.ofbiz.entity.condition.EntityConditionList;
40 import org.ofbiz.entity.condition.EntityExpr;
41 import org.ofbiz.entity.condition.EntityOperator;
42 import org.ofbiz.entity.util.EntityUtil;
43 import org.ofbiz.service.DispatchContext;
44 import org.ofbiz.service.GenericServiceException;
45 import org.ofbiz.service.LocalDispatcher;
46 import org.ofbiz.service.ModelService;
47 import org.ofbiz.service.ServiceUtil;
48
49 import com.opensourcestrategies.financials.util.UtilFinancial;
50
51 /**
52  * ConfigurationServices - Services for configuring GL Accounts
53  *
54  * @author <a HREF="mailto:ali@opensourcestrategies.com">Ali Afzal Malik</a>
55  * @version $Rev: 150 $
56  * @since 2.2
57  */

58
59 public class ConfigurationServices {
60     
61     public static String JavaDoc module = ConfigurationServices.class.getName();
62     
63     /**
64      * Removes a GL Account from an organization if it is not associated with any other entity
65      */

66     public static Map JavaDoc removeGlAccountFromOrganization(DispatchContext dctx, Map JavaDoc context) {
67         GenericDelegator delegator = dctx.getDelegator();
68         LocalDispatcher dispatcher = dctx.getDispatcher();
69         GenericValue userLogin = (GenericValue) context.get("userLogin");
70         String JavaDoc glAccountId = (String JavaDoc) context.get("glAccountId");
71         String JavaDoc organizationPartyId = (String JavaDoc) context.get("organizationPartyId");
72         Map JavaDoc result = new HashMap JavaDoc();
73         List JavaDoc value = null;
74         GenericValue glAccount = null;
75
76         Map JavaDoc fields = UtilMisc.toMap("glAccountId", glAccountId, "organizationPartyId", organizationPartyId);
77         
78         try {
79             //check for relation with GlAccountTypeDefault
80
value = delegator.findByAnd("GlAccountTypeDefault", fields);
81             if (!value.isEmpty()) {
82                 return ServiceUtil.returnError("Could not remove Gl Account from organization because it is associated with an account type through GlAccountTypeDefault.");
83             }
84             
85             //check for relation with InvoiceItemTypeGlAccount
86
value = delegator.findByAnd("InvoiceItemTypeGlAccount", fields);
87             if (!value.isEmpty()) {
88                 return ServiceUtil.returnError("Could not remove Gl Account from organization because it is associated with an invoice item type through InvoiceItemTypeGlAccount.");
89             }
90             
91             //check for relation with PaymentMethod
92
fields = UtilMisc.toMap("glAccountId", glAccountId, "partyId", organizationPartyId);
93             value = delegator.findByAnd("PaymentMethod", fields);
94             if (!value.isEmpty()) {
95                 return ServiceUtil.returnError("Could not remove Gl Account from organization because it is associated with a payment method through PaymentMethod.");
96             }
97             
98             //reset fields
99
fields = UtilMisc.toMap("glAccountId", glAccountId, "organizationPartyId", organizationPartyId);
100             
101             //check for relation with PaymentMethodTypeGlAccount
102
value = delegator.findByAnd("PaymentMethodTypeGlAccount", fields);
103             if (!value.isEmpty()) {
104                 return ServiceUtil.returnError("Could not remove Gl Account from organization because it is associated with a payment method type through PaymentMethodTypeGlAccount.");
105             }
106             
107             //check for relation with ProductGlAccount
108
value = delegator.findByAnd("ProductGlAccount", fields);
109             if (!value.isEmpty()) {
110                 return ServiceUtil.returnError("Could not remove Gl Account from organization because it is associated with a product through ProductGlAccount.");
111             }
112             
113             //check for relation with VarianceReasonGlAccount
114
value = delegator.findByAnd("VarianceReasonGlAccount", fields);
115             if (!value.isEmpty()) {
116                 return ServiceUtil.returnError("Could not remove Gl Account from organization because it is associated with an inventory variance reason through VarianceReasonGlAccount.");
117             }
118             
119             //remove the GL Account by setting the thru date to now date
120
Map JavaDoc updateGlAccountOrganizationContext = UtilMisc.toMap("glAccountId", glAccountId, "organizationPartyId", organizationPartyId, "thruDate", UtilDateTime.nowTimestamp(), "userLogin", userLogin);
121             Map JavaDoc updateGlAccountOrganizationResult = dispatcher.runSync("updateGlAccountOrganization", updateGlAccountOrganizationContext);
122             
123             if(ServiceUtil.isError(updateGlAccountOrganizationResult)) {
124                 return updateGlAccountOrganizationResult;
125             }
126             
127             return ServiceUtil.returnSuccess();
128         } catch (GenericEntityException e) {
129             return ServiceUtil.returnError("Could not remove Gl Account from organization (" + e.getMessage() + ").");
130         } catch (GenericServiceException e) {
131             return ServiceUtil.returnError("Could not remove Gl Account from organization (" + e.getMessage() + ").");
132         }
133     }
134     
135     /**
136      * Adds a new GL Account and associates it to an Organization if the specified account
137      * code is unique i.e. no existing GL Account has the same account code
138      */

139     public static Map JavaDoc addNewGlAccount(DispatchContext dctx, Map JavaDoc context) {
140         GenericDelegator delegator = dctx.getDelegator();
141         LocalDispatcher dispatcher = dctx.getDispatcher();
142         GenericValue userLogin = (GenericValue) context.get("userLogin");
143         String JavaDoc glAccountId = null;
144         String JavaDoc accountCode = (String JavaDoc) context.get("accountCode");
145         String JavaDoc accountName = (String JavaDoc) context.get("accountName");
146         String JavaDoc description = (String JavaDoc) context.get("description");
147         String JavaDoc glAccountTypeId = (String JavaDoc) context.get("glAccountTypeId");
148         String JavaDoc glAccountClassId = (String JavaDoc) context.get("glAccountClassId");
149         String JavaDoc glResourceTypeId = (String JavaDoc) context.get("glResourceTypeId");
150         String JavaDoc parentGlAccountId = (String JavaDoc) context.get("parentGlAccountId");
151         Double JavaDoc postedBalance = (Double JavaDoc) context.get("postedBalance");
152         String JavaDoc organizationPartyId = (String JavaDoc) context.get("organizationPartyId");
153         Map JavaDoc result = new HashMap JavaDoc();
154         List JavaDoc value = null;
155
156         Map JavaDoc fields = UtilMisc.toMap("accountCode", accountCode);
157         
158         try {
159             //check whether the account code is already present
160
value = delegator.findByAnd("GlAccount", fields);
161             if (!value.isEmpty()) {
162                 return ServiceUtil.returnError("The account code specified [" + accountCode + "] is already associated with an existing Gl Account.");
163             }
164             else {
165                 glAccountId = accountCode;
166             }
167             
168             //TODO: Confirm parameters
169
//Add a new Gl Account and a new Gl Account Organization
170
Map JavaDoc addNewGlAccountContext = UtilMisc.toMap("glAccountId", glAccountId, "accountCode", accountCode, "accountName", accountName, "description", description, "glAccountTypeId", glAccountTypeId, "glAccountClassId", glAccountClassId);
171             addNewGlAccountContext.put("glResourceTypeId", glResourceTypeId);
172             addNewGlAccountContext.put("parentGlAccountId", parentGlAccountId);
173             addNewGlAccountContext.put("postedBalance", postedBalance);
174             addNewGlAccountContext.put("userLogin", userLogin);
175             Map JavaDoc addNewGlAccountResult = dispatcher.runSync("createGlAccount", addNewGlAccountContext);
176             
177             if(ServiceUtil.isError(addNewGlAccountResult)) {
178                 return addNewGlAccountResult;
179             }
180             
181             Map JavaDoc addNewGlAccountOrganizationContext = UtilMisc.toMap("glAccountId", glAccountId, "organizationPartyId", organizationPartyId, "postedBalance", postedBalance, "userLogin", userLogin);
182             Map JavaDoc addNewGlAccountOrganizationResult = dispatcher.runSync("createGlAccountOrganization", addNewGlAccountOrganizationContext);
183             if(ServiceUtil.isError(addNewGlAccountOrganizationResult)) {
184                 return addNewGlAccountOrganizationResult;
185             }
186             
187             return ServiceUtil.returnSuccess();
188         } catch (GenericEntityException e) {
189             return ServiceUtil.returnError("Could not add the Gl Account (" + e.getMessage() + ").");
190         } catch (GenericServiceException e) {
191             return ServiceUtil.returnError("Could not add the Gl Account (" + e.getMessage() + ").");
192         }
193     }
194 }
195
Popular Tags