📄 forecastsservices.java
字号:
return UtilCommon.createAndLogServiceError(e, "CrmErrorComputeForecastFail", locale, module); } return ServiceUtil.returnSuccess(); } public static Map computeForecastParentPeriod(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 parentPeriodId = (String) context.get("parentPeriodId"); String organizationPartyId = (String) context.get("organizationPartyId"); String currencyUomId = (String) context.get("currencyUomId"); try { // see if we were passed a salesForecastId String salesForecastId = (String) context.get("salesForecastId"); String serviceName = null; String internalPartyId = null; // see if we were passed a salesForecastId and determine service to run and party for whom forecast is computed if (salesForecastId == null) { serviceName = "createSalesForecast"; internalPartyId = userLogin.getString("partyId"); } else { serviceName = "updateSalesForecast"; GenericValue forecast = delegator.findByPrimaryKey("SalesForecast", UtilMisc.toMap("salesForecastId", salesForecastId)); if ((forecast != null) && (forecast.getString("internalPartyId") != null)) { internalPartyId = forecast.getString("internalPartyId"); } else { return ServiceUtil.returnError("Invalid forecast or missing forecast.internalPartyId for forecast [" + salesForecastId + "]"); } } // compute the fields for the forecast Map computed = UtilForecast.computeForecastByChildren(parentPeriodId, organizationPartyId, internalPartyId, currencyUomId, delegator); // make the service input map from the context ModelService service = dctx.getModelService(serviceName); Map input = service.makeValid(context, "IN"); input.put("customTimePeriodId", parentPeriodId); // the parent period is the custom time period // add our computed fields and the userlogin input.putAll(computed); input.put("userLogin", userLogin); input.put("internalPartyId", internalPartyId); // run our update/create service Map serviceResults = dispatcher.runSync(serviceName, input); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorComputeForecastFail", locale, module); } // if we had no sales forecast, then return the one we just created if (salesForecastId == null) { salesForecastId = (String) serviceResults.get("salesForecastId"); } Map results = ServiceUtil.returnSuccess(); results.put("salesForecastId", salesForecastId); return results; } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorComputeForecastFail", locale, module); } catch (GenericServiceException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorComputeForecastFail", locale, module); } } public static Map updateForecastsRelatedToOpportunity(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"); ResourceBundleMapWrapper uiLabelMap = (ResourceBundleMapWrapper) UtilProperties.getResourceBundleMap("CRMSFAUiLabels", locale); String salesOpportunityId = (String) context.get("salesOpportunityId"); String changeNote = (String) context.get("changeNote"); Timestamp previousEstimatedCloseDate = (Timestamp) context.get("previousEstimatedCloseDate"); // get the organization from the properties String organizationPartyId = (String) UtilProperties.getPropertyValue("crmsfa.properties", "organizationPartyId"); try { GenericValue opportunity = delegator.findByPrimaryKey("SalesOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId)); // construct the changeNote with a link back to the opportunity (for errors/messages) StringBuffer changeNoteBuff = new StringBuffer((String) uiLabelMap.get(FORECAST_CHANGE_NOTE_PREFIX_UILABEL)); changeNoteBuff.append("<a class='buttontext' href='viewOpportunity?salesOpportunityId=").append(salesOpportunityId).append("'>") .append(opportunity.getString("opportunityName")).append(" (").append(salesOpportunityId).append(")</a>"); if (changeNote != null) { changeNoteBuff.append(": ").append(changeNote); } /* * Strategy: Build a list of partyIds whose forecasts would be affected by opportunity. * * For account opportunities, get the team members for the one related account. * For lead opportunities, get the LEAD_OWNER partyId. * * Then, find all forecasts for these partyId's in the "affected" periods. An * "affected" time period falls in the opportunity's estimatedCloseDate and the last * estimatedCloseDate (determined from SalesOpportunityHistory). For each FISCAL_MONTH * forecast found, the compute forecast service is called with all the values of that * forecast, as if the forecast were being updated. After doing months, compute the * quarters (FISCAL_QUARTER) in the same manner. */ String accountPartyId = (String) UtilOpportunity.getOpportunityAccountPartyId(opportunity); String leadPartyId = (String) UtilOpportunity.getOpportunityLeadPartyId(opportunity); boolean isAccountOpportunity = (accountPartyId != null ? true : false); Set partyIds = new HashSet(); if (isAccountOpportunity) { // get all the team members and collect their IDs into a Set List teamMembers = UtilOpportunity.getOpportunityTeamMembers(salesOpportunityId, delegator); for (Iterator iter = teamMembers.iterator(); iter.hasNext(); ) { GenericValue teamMember = (GenericValue) iter.next(); partyIds.add(teamMember.getString("partyId")); } } else { // get the LEAD_OWNER partyId GenericValue leadOwner = PartyHelper.getCurrentLeadOwner(leadPartyId, delegator); if (leadOwner == null) { return UtilCommon.createAndLogServiceError("No LEAD_OWNER for lead ["+leadPartyId+"] found!", locale, module); } partyIds.add(leadOwner.getString("partyId")); } // if no parties found, then we're done if (partyIds.size() == 0) { return ServiceUtil.returnSuccess(); } // We want all time periods that contain the new estimatedCloseDate, and the old one if it's different List periodConditions = new ArrayList(); periodConditions.add(EntityUtil.getFilterByDateExpr(opportunity.getTimestamp("estimatedCloseDate"))); if (previousEstimatedCloseDate != null) { // because this condition will be joined by OR, we don't need to worry about them being different periodConditions.add(EntityUtil.getFilterByDateExpr(previousEstimatedCloseDate)); } // get the forecasts (ideally we want a distinct, but the way the query is constructed should guarantee a distinct set anyway) EntityConditionList conditions = new EntityConditionList( UtilMisc.toList( new EntityConditionList(periodConditions, EntityOperator.OR), // join the periods by OR new EntityExpr("organizationPartyId", EntityOperator.EQUALS, organizationPartyId), new EntityExpr("internalPartyId", EntityOperator.IN, partyIds) ), EntityOperator.AND); List forecasts = delegator.findByCondition("SalesForecastAndCustomTimePeriod", conditions, null, null); // update forecasts of type FISCAL_MONTH first for (Iterator iter = forecasts.iterator(); iter.hasNext(); ) { GenericValue forecast = (GenericValue) iter.next(); if (!forecast.getString("periodTypeId").equals("FISCAL_MONTH")) continue; Map input = UtilMisc.toMap("userLogin", userLogin, "organizationPartyId", organizationPartyId, "currencyUomId", forecast.get("currencyUomId")); input.put("customTimePeriodId", forecast.getString("customTimePeriodId")); input.put("salesForecastId", forecast.getString("salesForecastId")); input.put("quotaAmount", forecast.getDouble("quotaAmount")); input.put("changeNote", changeNoteBuff.toString()); Map serviceResults = dispatcher.runSync("crmsfa.computeForecastPeriod", input); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorComputeForecastFail", locale, module); } iter.remove(); // this helps speed up the next iteration } // update forecasts of type FISCAL_QUARTER for (Iterator iter = forecasts.iterator(); iter.hasNext(); ) { GenericValue forecast = (GenericValue) iter.next(); if (!forecast.getString("periodTypeId").equals("FISCAL_QUARTER")) continue; Map input = UtilMisc.toMap("userLogin", userLogin, "organizationPartyId", organizationPartyId, "currencyUomId", forecast.get("currencyUomId")); input.put("parentPeriodId", forecast.getString("customTimePeriodId")); input.put("salesForecastId", forecast.getString("salesForecastId")); input.put("changeNote", changeNoteBuff.toString()); Map serviceResults = dispatcher.runSync("crmsfa.computeForecastParentPeriod", input); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorComputeForecastFail", locale, module); } } } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorComputeForecastFail", locale, module); } catch (GenericServiceException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorComputeForecastFail", locale, module); } return ServiceUtil.returnSuccess(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -