📄 opportunitiesservices.java
字号:
/* * Copyright (C) 2006 Open Source Strategies, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */package com.opensourcestrategies.crmsfa.opportunities;import java.util.Map;import java.util.List;import java.util.Locale;import java.sql.Timestamp;import javolution.util.FastMap;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.util.EntityUtil;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ServiceUtil;import org.ofbiz.security.Security;import com.opensourcestrategies.crmsfa.party.PartyHelper;import com.opensourcestrategies.crmsfa.security.CrmsfaSecurity;import com.opensourcestrategies.crmsfa.util.UtilCommon;import com.opensourcestrategies.crmsfa.opportunities.UtilOpportunity;/** * Opportunities services. The service documentation is in services_opportunities.xml. * * @author <a href="mailto:leon@opensourcestrategies.com">Leon Torres</a> */public class OpportunitiesServices { public static final String module = OpportunitiesServices.class.getName(); public static Map createOpportunity(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 accountPartyId = (String) context.get("accountPartyId"); String contactPartyId = (String) context.get("contactPartyId"); String leadPartyId = (String) context.get("leadPartyId"); // make sure either an account or lead is supplied, but not both if ((accountPartyId == null && leadPartyId == null) || (accountPartyId != null && leadPartyId != null)) { return UtilCommon.createAndLogServiceError("Please specify an account or a lead (not both).", "CrmErrorCreateOpportunityFail", locale, module); } // track which partyId we're using, the account or the lead String partyId = (accountPartyId != null ? accountPartyId : leadPartyId); // make sure userLogin has CRMSFA_OPP_CREATE permission for the account or lead if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_OPP", "_CREATE", userLogin, partyId)) { return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); } try { // make sure the lead is qualified if we're doing initial lead if (leadPartyId != null) { GenericValue party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", leadPartyId)); if (party == null) { return UtilCommon.createAndLogServiceError("Unknown lead ["+leadPartyId+"].", "CrmErrorPermissionDenied", locale, module); } if (!"PTYLEAD_QUALIFIED".equals(party.get("statusId"))) { return UtilCommon.createAndLogServiceError("Cannot create opportunities for unqualified lead [" +leadPartyId+"].", "CrmErrorPermissionDenied", locale, module); } } // set estimatedCloseDate to 23:59:59.999 so that it's at the end of the day String estimatedCloseDateString = (String) context.get("estimatedCloseDate"); Timestamp estimatedCloseDate = Timestamp.valueOf(estimatedCloseDateString + " 23:59:59.999"); // create the opportunity String salesOpportunityId = delegator.getNextSeqId("SalesOpportunity"); GenericValue opportunity = delegator.makeValue("SalesOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId)); opportunity.setNonPKFields(context); opportunity.set("estimatedCloseDate", estimatedCloseDate); opportunity.set("createdByUserLogin", userLogin.getString("userLoginId")); // if an opportunityStageId is present, set the estimated probability to that of the related stage String opportunityStageId = (String) context.get("opportunityStageId"); if (opportunityStageId != null) { GenericValue stage = opportunity.getRelatedOne("SalesOpportunityStage"); opportunity.set("estimatedProbability", stage.getDouble("defaultProbability")); } // store it opportunity.create(); // copy to history UtilOpportunity.createSalesOpportunityHistory(opportunity, delegator, context); // assign the initial account if (accountPartyId != null) { Map serviceResults = dispatcher.runSync("crmsfa.assignOpportunityToAccount", UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "accountPartyId", accountPartyId, "userLogin", userLogin)); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateOpportunityFail", locale, module); } } // assign the initial lead if (leadPartyId != null) { Map serviceResults = dispatcher.runSync("crmsfa.assignOpportunityToLead", UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "leadPartyId", leadPartyId, "userLogin", userLogin)); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateOpportunityFail", locale, module); } } // assign the initial contact, but only if account was specified if (contactPartyId != null && accountPartyId != null) { Map serviceResults = dispatcher.runSync("crmsfa.addContactToOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "contactPartyId", contactPartyId, "userLogin", userLogin)); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateOpportunityFail", locale, module); } } // update forecasts as the system user, so we can update all forecasts for all team members that need updating GenericValue system = delegator.findByPrimaryKeyCache("UserLogin", UtilMisc.toMap("userLoginId", "system")); Map serviceResults = dispatcher.runSync("crmsfa.updateForecastsRelatedToOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "userLogin", system)); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateOpportunityFail", locale, module); } // return the resulting opportunity ID Map results = ServiceUtil.returnSuccess(); results.put("salesOpportunityId", salesOpportunityId); return results; } catch (GenericServiceException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateOpportunityFail", locale, module); } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateOpportunityFail", locale, module); } } public static Map updateOpportunity(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"); try { GenericValue opportunity = delegator.findByPrimaryKey("SalesOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId)); if (opportunity == null) { return UtilCommon.createAndLogServiceError("CrmErrorUpdateOpportunityFail", locale, module); } // for security, we need to get the accountPartyId or leadPartyId for this opportunity String partyId = UtilOpportunity.getOpportunityAccountOrLeadPartyId(opportunity); // make sure userLogin has CRMSFA_OPP_UPDATE permission for this account or lead if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_OPP", "_UPDATE", userLogin, partyId)) { return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); } // get the new and old stages String stageId = opportunity.getString("opportunityStageId"); String newStageId = (String) context.get("opportunityStageId"); if (stageId == null) stageId = ""; if (newStageId == null) newStageId = ""; // this is needed for updating forecasts Timestamp previousEstimatedCloseDate = opportunity.getTimestamp("estimatedCloseDate"); // update the fields opportunity.setNonPKFields(context); // set estimatedCloseDate to 23:59:59.999 so that it's at the end of the day String estimatedCloseDateString = (String) context.get("estimatedCloseDate"); Timestamp estimatedCloseDate = Timestamp.valueOf(estimatedCloseDateString + " 23:59:59.999"); opportunity.set("estimatedCloseDate", estimatedCloseDate); // if the stage changed, set the probability to the one of the stage if (!stageId.equals(newStageId)) { opportunity.set("estimatedProbability", opportunity.getRelatedOne("SalesOpportunityStage").getDouble("defaultProbability")); } // store opportunity.store(); // copy the _new_ opportunity into history UtilOpportunity.createSalesOpportunityHistory(opportunity, delegator, context); // update forecasts as the system user, so we can update all forecasts for all team members that need updating GenericValue system = delegator.findByPrimaryKeyCache("UserLogin", UtilMisc.toMap("userLoginId", "system"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -