| 1 16 17 package com.opensourcestrategies.financials.configuration; 18 19 import java.util.Date ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.ArrayList ; 25 import java.sql.Timestamp ; 26 import java.math.BigDecimal ; 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 58 59 public class ConfigurationServices { 60 61 public static String module = ConfigurationServices.class.getName(); 62 63 66 public static Map removeGlAccountFromOrganization(DispatchContext dctx, Map context) { 67 GenericDelegator delegator = dctx.getDelegator(); 68 LocalDispatcher dispatcher = dctx.getDispatcher(); 69 GenericValue userLogin = (GenericValue) context.get("userLogin"); 70 String glAccountId = (String ) context.get("glAccountId"); 71 String organizationPartyId = (String ) context.get("organizationPartyId"); 72 Map result = new HashMap (); 73 List value = null; 74 GenericValue glAccount = null; 75 76 Map fields = UtilMisc.toMap("glAccountId", glAccountId, "organizationPartyId", organizationPartyId); 77 78 try { 79 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 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 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 fields = UtilMisc.toMap("glAccountId", glAccountId, "organizationPartyId", organizationPartyId); 100 101 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 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 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 Map updateGlAccountOrganizationContext = UtilMisc.toMap("glAccountId", glAccountId, "organizationPartyId", organizationPartyId, "thruDate", UtilDateTime.nowTimestamp(), "userLogin", userLogin); 121 Map 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 139 public static Map addNewGlAccount(DispatchContext dctx, Map context) { 140 GenericDelegator delegator = dctx.getDelegator(); 141 LocalDispatcher dispatcher = dctx.getDispatcher(); 142 GenericValue userLogin = (GenericValue) context.get("userLogin"); 143 String glAccountId = null; 144 String accountCode = (String ) context.get("accountCode"); 145 String accountName = (String ) context.get("accountName"); 146 String description = (String ) context.get("description"); 147 String glAccountTypeId = (String ) context.get("glAccountTypeId"); 148 String glAccountClassId = (String ) context.get("glAccountClassId"); 149 String glResourceTypeId = (String ) context.get("glResourceTypeId"); 150 String parentGlAccountId = (String ) context.get("parentGlAccountId"); 151 Double postedBalance = (Double ) context.get("postedBalance"); 152 String organizationPartyId = (String ) context.get("organizationPartyId"); 153 Map result = new HashMap (); 154 List value = null; 155 156 Map fields = UtilMisc.toMap("accountCode", accountCode); 157 158 try { 159 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 Map 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 addNewGlAccountResult = dispatcher.runSync("createGlAccount", addNewGlAccountContext); 176 177 if(ServiceUtil.isError(addNewGlAccountResult)) { 178 return addNewGlAccountResult; 179 } 180 181 Map addNewGlAccountOrganizationContext = UtilMisc.toMap("glAccountId", glAccountId, "organizationPartyId", organizationPartyId, "postedBalance", postedBalance, "userLogin", userLogin); 182 Map 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 |