1 16 17 package com.opensourcestrategies.crmsfa.opportunities; 18 19 import java.util.Map ; 20 import java.util.List ; 21 import java.util.Locale ; 22 import java.sql.Timestamp ; 23 24 import javolution.util.FastMap; 25 26 import org.ofbiz.base.util.Debug; 27 import org.ofbiz.base.util.UtilMisc; 28 import org.ofbiz.base.util.UtilProperties; 29 import org.ofbiz.base.util.UtilDateTime; 30 import org.ofbiz.entity.GenericDelegator; 31 import org.ofbiz.entity.GenericEntityException; 32 import org.ofbiz.entity.GenericValue; 33 import org.ofbiz.entity.util.EntityUtil; 34 import org.ofbiz.service.DispatchContext; 35 import org.ofbiz.service.GenericServiceException; 36 import org.ofbiz.service.LocalDispatcher; 37 import org.ofbiz.service.ServiceUtil; 38 import org.ofbiz.security.Security; 39 40 import com.opensourcestrategies.crmsfa.party.PartyHelper; 41 import com.opensourcestrategies.crmsfa.security.CrmsfaSecurity; 42 import com.opensourcestrategies.crmsfa.util.UtilCommon; 43 import com.opensourcestrategies.crmsfa.opportunities.UtilOpportunity; 44 45 50 51 public class OpportunitiesServices { 52 53 public static final String module = OpportunitiesServices.class.getName(); 54 55 public static Map createOpportunity(DispatchContext dctx, Map context) { 57 GenericDelegator delegator = dctx.getDelegator(); 58 LocalDispatcher dispatcher = dctx.getDispatcher(); 59 Security security = dctx.getSecurity(); 60 GenericValue userLogin = (GenericValue) context.get("userLogin"); 61 Locale locale = (Locale ) context.get("locale"); 62 63 String internalPartyId = (String ) context.get("internalPartyId"); 64 String accountPartyId = (String ) context.get("accountPartyId"); 65 String contactPartyId = (String ) context.get("contactPartyId"); 66 String leadPartyId = (String ) context.get("leadPartyId"); 67 68 if (internalPartyId == null && ((accountPartyId == null && leadPartyId == null) || (accountPartyId != null && leadPartyId != null))) { 70 return UtilCommon.createAndLogServiceError("Please specify an account or a lead (not both).", "CrmErrorCreateOpportunityFail", locale, module); 71 } 72 73 String partyId = null; 75 if (accountPartyId != null) partyId = accountPartyId; 76 if (leadPartyId != null) partyId = leadPartyId; 77 if (internalPartyId != null) partyId = internalPartyId; 78 79 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_OPP", "_CREATE", userLogin, partyId)) { 81 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 82 } 83 84 try { 85 if (internalPartyId != null) { 87 String roleTypeId = PartyHelper.getFirstValidInternalPartyRoleTypeId(internalPartyId, delegator); 88 if ("ACCOUNT".equals(roleTypeId)) accountPartyId = internalPartyId; 89 if ("PROSPECT".equals(roleTypeId)) leadPartyId = internalPartyId; 90 } 91 92 if (leadPartyId != null) { 94 GenericValue party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", leadPartyId)); 95 if (party == null) { 96 return UtilCommon.createAndLogServiceError("Unknown lead ["+leadPartyId+"].", "CrmErrorPermissionDenied", locale, module); 97 } 98 if (!"PTYLEAD_QUALIFIED".equals(party.get("statusId"))) { 99 return UtilCommon.createAndLogServiceError("Cannot create opportunities for unqualified lead [" 100 +leadPartyId+"].", "CrmErrorPermissionDenied", locale, module); 101 } 102 } 103 104 String estimatedCloseDateString = (String ) context.get("estimatedCloseDate"); 106 Timestamp estimatedCloseDate = Timestamp.valueOf(estimatedCloseDateString + " 23:59:59.999"); 107 108 String salesOpportunityId = delegator.getNextSeqId("SalesOpportunity"); 110 GenericValue opportunity = delegator.makeValue("SalesOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId)); 111 opportunity.setNonPKFields(context); 112 opportunity.set("estimatedCloseDate", estimatedCloseDate); 113 opportunity.set("createdByUserLogin", userLogin.getString("userLoginId")); 114 115 String opportunityStageId = (String ) context.get("opportunityStageId"); 117 if (opportunityStageId != null) { 118 GenericValue stage = opportunity.getRelatedOne("SalesOpportunityStage"); 119 opportunity.set("estimatedProbability", stage.getDouble("defaultProbability")); 120 } 121 122 opportunity.create(); 124 125 UtilOpportunity.createSalesOpportunityHistory(opportunity, delegator, context); 127 128 if (accountPartyId != null) { 130 Map serviceResults = dispatcher.runSync("crmsfa.assignOpportunityToAccount", 131 UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "accountPartyId", accountPartyId, "userLogin", userLogin)); 132 if (ServiceUtil.isError(serviceResults)) { 133 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateOpportunityFail", locale, module); 134 } 135 } 136 137 if (leadPartyId != null) { 139 Map serviceResults = dispatcher.runSync("crmsfa.assignOpportunityToLead", 140 UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "leadPartyId", leadPartyId, "userLogin", userLogin)); 141 if (ServiceUtil.isError(serviceResults)) { 142 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateOpportunityFail", locale, module); 143 } 144 } 145 146 if (contactPartyId != null && accountPartyId != null) { 148 Map serviceResults = dispatcher.runSync("crmsfa.addContactToOpportunity", 149 UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "contactPartyId", contactPartyId, "userLogin", userLogin)); 150 if (ServiceUtil.isError(serviceResults)) { 151 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateOpportunityFail", locale, module); 152 } 153 } 154 155 GenericValue system = delegator.findByPrimaryKeyCache("UserLogin", UtilMisc.toMap("userLoginId", "system")); 157 Map serviceResults = dispatcher.runSync("crmsfa.updateForecastsRelatedToOpportunity", 158 UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "userLogin", system)); 159 if (ServiceUtil.isError(serviceResults)) { 160 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateOpportunityFail", locale, module); 161 } 162 163 Map results = ServiceUtil.returnSuccess(); 165 results.put("salesOpportunityId", salesOpportunityId); 166 return results; 167 } catch (GenericServiceException e) { 168 return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateOpportunityFail", locale, module); 169 } catch (GenericEntityException e) { 170 return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateOpportunityFail", locale, module); 171 } 172 } 173 174 public static Map updateOpportunity(DispatchContext dctx, Map context) { 175 GenericDelegator delegator = dctx.getDelegator(); 176 LocalDispatcher dispatcher = dctx.getDispatcher(); 177 Security security = dctx.getSecurity(); 178 GenericValue userLogin = (GenericValue) context.get("userLogin"); 179 Locale locale = (Locale ) context.get("locale"); 180 181 String salesOpportunityId = (String ) context.get("salesOpportunityId"); 182 183 try { 184 GenericValue opportunity = delegator.findByPrimaryKey("SalesOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId)); 185 if (opportunity == null) { 186 return UtilCommon.createAndLogServiceError("CrmErrorUpdateOpportunityFail", locale, module); 187 } 188 189 String partyId = UtilOpportunity.getOpportunityAccountOrLeadPartyId(opportunity); 191 192 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_OPP", "_UPDATE", userLogin, partyId)) { 194 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 195 } 196 197 String stageId = opportunity.getString("opportunityStageId"); 199 String newStageId = (String ) context.get("opportunityStageId"); 200 if (stageId == null) stageId = ""; 201 if (newStageId == null) newStageId = ""; 202 203 Timestamp previousEstimatedCloseDate = opportunity.getTimestamp("estimatedCloseDate"); 205 206 opportunity.setNonPKFields(context); 208 209 String estimatedCloseDateString = (String ) context.get("estimatedCloseDate"); 211 Timestamp estimatedCloseDate = Timestamp.valueOf(estimatedCloseDateString + " 23:59:59.999"); 212 opportunity.set("estimatedCloseDate", estimatedCloseDate); 213 214 if (!stageId.equals(newStageId)) { 216 opportunity.set("estimatedProbability", opportunity.getRelatedOne("SalesOpportunityStage").getDouble("defaultProbability")); 217 } 218 219 opportunity.store(); 221 222 UtilOpportunity.createSalesOpportunityHistory(opportunity, delegator, context); 224 225 GenericValue system = delegator.findByPrimaryKeyCache("UserLogin", UtilMisc.toMap("userLoginId", "system")); 227 Map serviceResults = dispatcher.runSync("crmsfa.updateForecastsRelatedToOpportunity", 228 UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "previousEstimatedCloseDate", previousEstimatedCloseDate, 229 "changeNote", context.get("changeNote"), "userLogin", system)); 230 if (ServiceUtil.isError(serviceResults)) { 231 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateOpportunityFail", locale, module); 232 } 233 } catch (GenericServiceException e) { 234 return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateOpportunityFail", locale, module); 235 } catch (GenericEntityException e) { 236 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateOpportunityFail", locale, module); 237 } 238 return ServiceUtil.returnSuccess(); 239 } 240 241 public static Map assignOpportunityToAccount(DispatchContext dctx, Map context) { 242 return assignOpportunityToPartyHelper(dctx, context, (String ) context.get("accountPartyId"), "ACCOUNT", "CRMSFA_ACCOUNT"); 243 } 244 245 public static Map assignOpportunityToLead(DispatchContext dctx, Map context) { 246 return assignOpportunityToPartyHelper(dctx, context, (String ) context.get("leadPartyId"), "PROSPECT", "CRMSFA_LEAD"); 247 } 248 249 250 private static Map assignOpportunityToPartyHelper(DispatchContext dctx, Map context, String partyId, String roleTypeId, String permissionId) { 251 GenericDelegator delegator = dctx.getDelegator(); 252 LocalDispatcher dispatcher = dctx.getDispatcher(); 253 Security security = dctx.getSecurity(); 254 GenericValue userLogin = (GenericValue) context.get("userLogin"); 255 Locale locale = (Locale ) context.get("locale"); 256 257 String salesOpportunityId = (String ) context.get("salesOpportunityId"); 258 259 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, permissionId, "_UPDATE", userLogin, partyId)) { 261 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 262 } 263 try { 264 GenericValue role = delegator.makeValue("SalesOpportunityRole", UtilMisc.toMap("salesOpportunityId", salesOpportunityId, 266 "partyId", partyId, "roleTypeId", roleTypeId)); 267 role.create(); 268 } catch (GenericEntityException e) { 269 return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module); 270 } 271 return ServiceUtil.returnSuccess(); 272 } 273 274 public static Map addContactToOpportunity(DispatchContext dctx, Map context) { 275 GenericDelegator delegator = dctx.getDelegator(); 276 LocalDispatcher dispatcher = dctx.getDispatcher(); 277 Security security = dctx.getSecurity(); 278 GenericValue userLogin = (GenericValue) context.get("userLogin"); 279 Locale locale = (Locale ) context.get("locale"); 280 281 String salesOpportunityId = (String ) context.get("salesOpportunityId"); 282 String contactPartyId = (String ) context.get("contactPartyId"); 283 284 try { 285 GenericValue opportunity = delegator.findByPrimaryKey("SalesOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId)); 286 if (opportunity == null) { 287 return UtilCommon.createAndLogServiceError("CrmErrorAddContactToOpportunity", locale, module); 288 } 289 290 String accountPartyId = UtilOpportunity.getOpportunityAccountPartyId(opportunity); 292 293 if (accountPartyId == null) { 295 String leadPartyId = UtilOpportunity.getOpportunityLeadPartyId(opportunity); 296 if (leadPartyId != null) { 297 return UtilCommon.createAndLogServiceError("Cannot add contact to a lead opportunity.", "CrmErrorAddContactToOpportunity", locale, module); 298 } else { 299 return UtilCommon.createAndLogServiceError("Cound not find account for opportunity ["+salesOpportunityId+"].", "CrmErrorAddContactToOpportunity", locale, module); 300 } 301 } 302 303 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_OPP", "_UPDATE", userLogin, accountPartyId)) { 305 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 306 } 307 308 List candidates = EntityUtil.filterByDate(delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", contactPartyId, 310 "partyIdTo", accountPartyId, "partyRelationshipTypeId", "CONTACT_REL_INV"), UtilMisc.toList("fromDate DESC"))); 311 if (candidates.size() == 0) { 312 return UtilCommon.createAndLogServiceError("Contact with ID [" + contactPartyId + "] is not associated with Account with ID [" + 313 accountPartyId + "]", "CrmErrorAddContactToOpportunity", locale, module); 314 } 315 316 Map keys = UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "partyId", contactPartyId, "roleTypeId", "CONTACT"); 318 GenericValue role = delegator.findByPrimaryKey("SalesOpportunityRole", keys); 319 if (role != null) { 320 return UtilCommon.createAndLogServiceError("Contact is already associated with this Opportunity.", "CrmErrorAddContactToOpportunity", locale, module); 321 } 322 323 role = delegator.makeValue("SalesOpportunityRole", keys); 325 role.create(); 326 } catch (GenericEntityException e) { 327 return UtilCommon.createAndLogServiceError(e, "CrmErrorAddContactToOpportunity", locale, module); 328 } 329 return ServiceUtil.returnSuccess(); 330 } 331 332 public static Map removeContactFromOpportunity(DispatchContext dctx, Map context) { 333 GenericDelegator delegator = dctx.getDelegator(); 334 LocalDispatcher dispatcher = dctx.getDispatcher(); 335 Security security = dctx.getSecurity(); 336 GenericValue userLogin = (GenericValue) context.get("userLogin"); 337 Locale locale = (Locale ) context.get("locale"); 338 339 String salesOpportunityId = (String ) context.get("salesOpportunityId"); 340 String contactPartyId = (String ) context.get("contactPartyId"); 341 342 try { 343 GenericValue opportunity = delegator.findByPrimaryKey("SalesOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId)); 344 if (opportunity == null) { 345 return UtilCommon.createAndLogServiceError("No opportunity with ID [" + salesOpportunityId + "] found.", 346 "CrmErrorRemoveContactFromOpportunity", locale, module); 347 } 348 349 String accountPartyId = UtilOpportunity.getOpportunityAccountPartyId(opportunity); 351 352 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_OPP", "_UPDATE", userLogin, accountPartyId)) { 354 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 355 } 356 357 GenericValue role = delegator.findByPrimaryKey("SalesOpportunityRole", UtilMisc.toMap("salesOpportunityId", salesOpportunityId, 359 "partyId", contactPartyId, "roleTypeId", "CONTACT")); 360 if (role == null) { 361 return UtilCommon.createAndLogServiceError("Could not find contact with ID [" + 362 contactPartyId + "] for the opportunity with ID [" + salesOpportunityId + "].", "CrmErrorRemoveContactFromOpportunity", locale, module); 363 } 364 role.remove(); 365 } catch (GenericEntityException e) { 366 return UtilCommon.createAndLogServiceError(e, "CrmErrorRemoveContactFromOpportunity", locale, module); 367 } 368 return ServiceUtil.returnSuccess(); 369 } 370 371 public static Map addQuoteToOpportunity(DispatchContext dctx, Map context) { 372 GenericDelegator delegator = dctx.getDelegator(); 373 LocalDispatcher dispatcher = dctx.getDispatcher(); 374 Security security = dctx.getSecurity(); 375 GenericValue userLogin = (GenericValue) context.get("userLogin"); 376 Locale locale = (Locale ) context.get("locale"); 377 378 String salesOpportunityId = (String ) context.get("salesOpportunityId"); 379 String quoteId = (String ) context.get("quoteId"); 380 381 try { 382 String partyId = UtilOpportunity.getOpportunityAccountOrLeadPartyId(delegator.findByPrimaryKey("SalesOpportunity", 384 UtilMisc.toMap("salesOpportunityId", salesOpportunityId))); 385 386 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_OPP", "_UPDATE", userLogin, partyId)) { 388 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 389 } 390 391 393 Map input = UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "quoteId", quoteId); 395 GenericValue relation = delegator.findByPrimaryKey("SalesOpportunityQuote", input); 396 if (relation == null) { 397 relation = delegator.makeValue("SalesOpportunityQuote", input); 398 relation.create(); 399 } 400 } catch (GenericEntityException e) { 401 return UtilCommon.createAndLogServiceError(e, "CrmErrorAddQuoteFail", locale, module); 402 } 403 return ServiceUtil.returnSuccess(); 404 } 405 406 public static Map removeQuoteFromOpportunity(DispatchContext dctx, Map context) { 407 GenericDelegator delegator = dctx.getDelegator(); 408 LocalDispatcher dispatcher = dctx.getDispatcher(); 409 Security security = dctx.getSecurity(); 410 GenericValue userLogin = (GenericValue) context.get("userLogin"); 411 Locale locale = (Locale ) context.get("locale"); 412 413 String salesOpportunityId = (String ) context.get("salesOpportunityId"); 414 String quoteId = (String ) context.get("quoteId"); 415 416 try { 417 String partyId = UtilOpportunity.getOpportunityAccountOrLeadPartyId(delegator.findByPrimaryKey("SalesOpportunity", 419 UtilMisc.toMap("salesOpportunityId", salesOpportunityId))); 420 421 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_OPP", "_UPDATE", userLogin, partyId)) { 423 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 424 } 425 426 428 Map input = UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "quoteId", quoteId); 430 GenericValue relation = delegator.findByPrimaryKey("SalesOpportunityQuote", input); 431 if (relation != null) { 432 relation.remove(); 433 } 434 } catch (GenericEntityException e) { 435 return UtilCommon.createAndLogServiceError(e, "CrmErrorRemoveQuoteFail", locale, module); 436 } 437 return ServiceUtil.returnSuccess(); 438 } 439 440 } 441 | Popular Tags |