📄 opportunitiesservices.java
字号:
Map serviceResults = dispatcher.runSync("crmsfa.updateForecastsRelatedToOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "previousEstimatedCloseDate", previousEstimatedCloseDate, "changeNote", context.get("changeNote"), "userLogin", system)); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateOpportunityFail", locale, module); } } catch (GenericServiceException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateOpportunityFail", locale, module); } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateOpportunityFail", locale, module); } return ServiceUtil.returnSuccess(); } public static Map assignOpportunityToAccount(DispatchContext dctx, Map context) { return assignOpportunityToPartyHelper(dctx, context, (String) context.get("accountPartyId"), "ACCOUNT", "CRMSFA_ACCOUNT"); } public static Map assignOpportunityToLead(DispatchContext dctx, Map context) { return assignOpportunityToPartyHelper(dctx, context, (String) context.get("leadPartyId"), "PROSPECT", "CRMSFA_LEAD"); } /** Helper method to assign an opportunity to an account/lead party */ private static Map assignOpportunityToPartyHelper(DispatchContext dctx, Map context, String partyId, String roleTypeId, String permissionId) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Security security = dctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); String salesOpportunityId = (String) context.get("salesOpportunityId"); // check if userLogin has update permission for this party if (!CrmsfaSecurity.hasPartyRelationSecurity(security, permissionId, "_UPDATE", userLogin, partyId)) { return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); } try { // create a SalesOpportunityRole with salesOpportunityId, partyId and roleTypeId GenericValue role = delegator.makeValue("SalesOpportunityRole", UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "partyId", partyId, "roleTypeId", roleTypeId)); role.create(); } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module); } return ServiceUtil.returnSuccess(); } public static Map addContactToOpportunity(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Security security = dctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); String salesOpportunityId = (String) context.get("salesOpportunityId"); String contactPartyId = (String) context.get("contactPartyId"); try { GenericValue opportunity = delegator.findByPrimaryKey("SalesOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId)); if (opportunity == null) { return UtilCommon.createAndLogServiceError("CrmErrorAddContactToOpportunity", locale, module); } // for security, we need to get the accountPartyId for this opportunity String accountPartyId = UtilOpportunity.getOpportunityAccountPartyId(opportunity); // if no account exists, don't add contact to opportunity (rationale: this is a weird case and we don't know if convert lead will copy these) if (accountPartyId == null) { String leadPartyId = UtilOpportunity.getOpportunityLeadPartyId(opportunity); if (leadPartyId != null) { return UtilCommon.createAndLogServiceError("Cannot add contact to a lead opportunity.", "CrmErrorAddContactToOpportunity", locale, module); } else { return UtilCommon.createAndLogServiceError("Cound not find account for opportunity ["+salesOpportunityId+"].", "CrmErrorAddContactToOpportunity", locale, module); } } // check if userLogin has CRMSFA_OPP_UPDATE permission for this contact if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_OPP", "_UPDATE", userLogin, accountPartyId)) { return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); } // check first that this contact is associated with the account List candidates = EntityUtil.filterByDate(delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", contactPartyId, "partyIdTo", accountPartyId, "partyRelationshipTypeId", "CONTACT_REL_INV"), UtilMisc.toList("fromDate DESC"))); if (candidates.size() == 0) { return UtilCommon.createAndLogServiceError("Contact with ID [" + contactPartyId + "] is not associated with Account with ID [" + accountPartyId + "]", "CrmErrorAddContactToOpportunity", locale, module); } // avoid duplicates Map keys = UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "partyId", contactPartyId, "roleTypeId", "CONTACT"); GenericValue role = delegator.findByPrimaryKey("SalesOpportunityRole", keys); if (role != null) { return UtilCommon.createAndLogServiceError("Contact is already associated with this Opportunity.", "CrmErrorAddContactToOpportunity", locale, module); } // create a SalesOpportunityRole with salesOpportunityId and partyId=contactPartyId and roleTypeId=CONTACT role = delegator.makeValue("SalesOpportunityRole", keys); role.create(); } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorAddContactToOpportunity", locale, module); } return ServiceUtil.returnSuccess(); } public static Map removeContactFromOpportunity(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Security security = dctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); String salesOpportunityId = (String) context.get("salesOpportunityId"); String contactPartyId = (String) context.get("contactPartyId"); try { GenericValue opportunity = delegator.findByPrimaryKey("SalesOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId)); if (opportunity == null) { return UtilCommon.createAndLogServiceError("No opportunity with ID [" + salesOpportunityId + "] found.", "CrmErrorRemoveContactFromOpportunity", locale, module); } // for security, we need to get the accountPartyId for this opportunity String accountPartyId = UtilOpportunity.getOpportunityAccountPartyId(opportunity); // check if userLogin has CRMSFA_OPP_UPDATE permission for this contact if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_OPP", "_UPDATE", userLogin, accountPartyId)) { return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); } // delete the SalesOpportunityRole with salesOpportunityId and partyId=contactPartyId and roleTypeId=CONTACT GenericValue role = delegator.findByPrimaryKey("SalesOpportunityRole", UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "partyId", contactPartyId, "roleTypeId", "CONTACT")); if (role == null) { return UtilCommon.createAndLogServiceError("Could not find contact with ID [" + contactPartyId + "] for the opportunity with ID [" + salesOpportunityId + "].", "CrmErrorRemoveContactFromOpportunity", locale, module); } role.remove(); } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorRemoveContactFromOpportunity", locale, module); } return ServiceUtil.returnSuccess(); } public static Map addQuoteToOpportunity(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Security security = dctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); String salesOpportunityId = (String) context.get("salesOpportunityId"); String quoteId = (String) context.get("quoteId"); try { // for security, we need to get the account or lead partyId for this opportunity String partyId = UtilOpportunity.getOpportunityAccountOrLeadPartyId(delegator.findByPrimaryKey("SalesOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId))); // make sure userLogin has CRMSFA_OPP_UPDATE permission for this account if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_OPP", "_UPDATE", userLogin, partyId)) { return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); } // There's no service in ofbiz to create SalesOpportunityQuote entries, so we do it by hand // see if the relation already exists and if not, create it Map input = UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "quoteId", quoteId); GenericValue relation = delegator.findByPrimaryKey("SalesOpportunityQuote", input); if (relation == null) { relation = delegator.makeValue("SalesOpportunityQuote", input); relation.create(); } } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorAddQuoteFail", locale, module); } return ServiceUtil.returnSuccess(); } public static Map removeQuoteFromOpportunity(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Security security = dctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); String salesOpportunityId = (String) context.get("salesOpportunityId"); String quoteId = (String) context.get("quoteId"); try { // for security, we need to get the account or lead partyId for this opportunity String partyId = UtilOpportunity.getOpportunityAccountOrLeadPartyId(delegator.findByPrimaryKey("SalesOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId))); // make sure userLogin has CRMSFA_OPP_UPDATE permission for this account if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_OPP", "_UPDATE", userLogin, partyId)) { return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); } // There's no service in ofbiz to remove SalesOpportunityQuote entries, so we do it by hand // see if the relation already exists and if so, remove it Map input = UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "quoteId", quoteId); GenericValue relation = delegator.findByPrimaryKey("SalesOpportunityQuote", input); if (relation != null) { relation.remove(); } } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorRemoveQuoteFail", locale, module); } return ServiceUtil.returnSuccess(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -