1 16 17 18 41 package com.opensourcestrategies.crmsfa.forecasts; 42 43 import java.util.*; 44 import java.sql.Timestamp ; 45 46 import org.ofbiz.base.util.*; 47 import org.ofbiz.base.util.collections.ResourceBundleMapWrapper; 48 import org.ofbiz.entity.*; 49 import org.ofbiz.entity.condition.*; 50 import org.ofbiz.entity.util.EntityUtil; 51 import org.ofbiz.service.DispatchContext; 52 import org.ofbiz.service.GenericServiceException; 53 import org.ofbiz.service.ModelService; 54 import org.ofbiz.service.LocalDispatcher; 55 import org.ofbiz.service.ServiceUtil; 56 import org.ofbiz.security.Security; 57 58 import com.opensourcestrategies.crmsfa.party.PartyHelper; 59 import com.opensourcestrategies.crmsfa.security.CrmsfaSecurity; 60 import com.opensourcestrategies.crmsfa.util.UtilCommon; 61 import com.opensourcestrategies.crmsfa.forecasts.UtilForecast; 62 import com.opensourcestrategies.crmsfa.opportunities.UtilOpportunity; 63 64 71 72 public class ForecastsServices { 73 74 public static final String module = ForecastsServices.class.getName(); 75 76 protected static final String FORECAST_CHANGE_NOTE_PREFIX_UILABEL = "CrmForecastChangeNotePrefix"; 77 78 public static Map updateForecast(DispatchContext dctx, Map context) { 79 GenericDelegator delegator = dctx.getDelegator(); 80 LocalDispatcher dispatcher = dctx.getDispatcher(); 81 Security security = dctx.getSecurity(); 82 GenericValue userLogin = (GenericValue) context.get("userLogin"); 83 Locale locale = (Locale) context.get("locale"); 84 85 String salesForecastId = (String ) context.get("salesForecastId"); 86 Double quotaAmount = (Double ) context.get("quotaAmount"); 87 try { 88 GenericValue forecast = delegator.findByPrimaryKey("SalesForecast", UtilMisc.toMap("salesForecastId", salesForecastId)); 89 if (forecast == null) { 90 return UtilCommon.createAndLogServiceError("Forecast with ID [" + salesForecastId + "] not found.", "CrmErrorComputeForecastFail", locale, module); 91 } 92 93 Map computed = UtilForecast.computeForecastByOpportunities(quotaAmount, forecast.getString("organizationPartyId"), 95 forecast.getString("internalPartyId"), forecast.getString("currencyUomId"), forecast.getString("customTimePeriodId"), delegator); 96 97 ModelService service = dctx.getModelService("updateSalesForecast"); 99 Map input = service.makeValid(context, "IN"); 100 101 input.put("salesForecastId", salesForecastId); 103 input.putAll(computed); 104 input.put("userLogin", userLogin); 105 106 Map serviceResults = dispatcher.runSync("updateSalesForecast", input); 108 if (ServiceUtil.isError(serviceResults)) { 109 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorComputeForecastFail", locale, module); 110 } 111 112 GenericValue period = forecast.getRelatedOne("CustomTimePeriod"); 117 List parents = delegator.findByAnd("SalesForecastAndCustomTimePeriod", UtilMisc.toMap("customTimePeriodId", period.getString("parentPeriodId"), 118 "internalPartyId", forecast.getString("internalPartyId"), "organizationPartyId", forecast.getString("organizationPartyId"), 119 "periodTypeId", "FISCAL_QUARTER")); GenericValue parent = (GenericValue) parents.get(0); 122 service = dctx.getModelService("crmsfa.computeForecastParentPeriod"); 123 input = service.makeValid(parent.getAllFields(), "IN"); 124 input.put("userLogin", userLogin); 125 input.put("parentPeriodId", period.getString("parentPeriodId")); 126 input.put("changeNote", context.get("changeNote")); serviceResults = dispatcher.runSync("crmsfa.computeForecastParentPeriod", input); 128 if (ServiceUtil.isError(serviceResults)) { 129 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorComputeForecastFail", locale, module); 130 } 131 } catch (GenericEntityException e) { 132 return UtilCommon.createAndLogServiceError(e, "CrmErrorComputeForecastFail", locale, module); 133 } catch (GenericServiceException e) { 134 return UtilCommon.createAndLogServiceError(e, "CrmErrorComputeForecastFail", locale, module); 135 } 136 return ServiceUtil.returnSuccess(); 137 138 } 139 140 public static Map computeForecastPeriod(DispatchContext dctx, Map context) { 142 GenericDelegator delegator = dctx.getDelegator(); 143 LocalDispatcher dispatcher = dctx.getDispatcher(); 144 Security security = dctx.getSecurity(); 145 GenericValue userLogin = (GenericValue) context.get("userLogin"); 146 Locale locale = (Locale) context.get("locale"); 147 148 String customTimePeriodId = (String ) context.get("customTimePeriodId"); 149 String organizationPartyId = (String ) context.get("organizationPartyId"); 150 String currencyUomId = (String ) context.get("currencyUomId"); 151 152 Double quotaAmount = (Double ) context.get("quotaAmount"); 154 if (quotaAmount == null) quotaAmount = new Double (0.00); 155 156 try { 157 String salesForecastId = (String ) context.get("salesForecastId"); 158 String serviceName = null; 159 String internalPartyId = null; 160 161 if (salesForecastId == null) { 163 serviceName = "createSalesForecast"; 164 internalPartyId = userLogin.getString("partyId"); 165 } else { 166 serviceName = "updateSalesForecast"; 167 GenericValue forecast = delegator.findByPrimaryKey("SalesForecast", UtilMisc.toMap("salesForecastId", salesForecastId)); 168 if ((forecast != null) && (forecast.getString("internalPartyId") != null)) { 169 internalPartyId = forecast.getString("internalPartyId"); 170 } else { 171 return ServiceUtil.returnError("Invalid forecast or missing forecast.internalPartyId for forecast [" + salesForecastId + "]"); 172 } 173 } 174 175 Map computed = UtilForecast.computeForecastByOpportunities(quotaAmount, organizationPartyId, internalPartyId, 177 currencyUomId, customTimePeriodId, delegator); 178 179 ModelService service = dctx.getModelService(serviceName); 181 Map input = service.makeValid(context, "IN"); 182 183 input.putAll(computed); 185 input.put("userLogin", userLogin); 186 input.put("internalPartyId", internalPartyId); 187 188 Map serviceResults = dispatcher.runSync(serviceName, input); 190 if (ServiceUtil.isError(serviceResults)) { 191 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorComputeForecastFail", locale, module); 192 } 193 } catch (GenericEntityException e) { 194 return UtilCommon.createAndLogServiceError(e, "CrmErrorComputeForecastFail", locale, module); 195 } catch (GenericServiceException e) { 196 return UtilCommon.createAndLogServiceError(e, "CrmErrorComputeForecastFail", locale, module); 197 } 198 return ServiceUtil.returnSuccess(); 199 } 200 201 public static Map computeForecastParentPeriod(DispatchContext dctx, Map context) { 202 GenericDelegator delegator = dctx.getDelegator(); 203 LocalDispatcher dispatcher = dctx.getDispatcher(); 204 Security security = dctx.getSecurity(); 205 GenericValue userLogin = (GenericValue) context.get("userLogin"); 206 Locale locale = (Locale) context.get("locale"); 207 208 String parentPeriodId = (String ) context.get("parentPeriodId"); 209 String organizationPartyId = (String ) context.get("organizationPartyId"); 210 String currencyUomId = (String ) context.get("currencyUomId"); 211 212 try { 213 String salesForecastId = (String ) context.get("salesForecastId"); 215 String serviceName = null; 216 String internalPartyId = null; 217 218 if (salesForecastId == null) { 220 serviceName = "createSalesForecast"; 221 internalPartyId = userLogin.getString("partyId"); 222 } else { 223 serviceName = "updateSalesForecast"; 224 GenericValue forecast = delegator.findByPrimaryKey("SalesForecast", UtilMisc.toMap("salesForecastId", salesForecastId)); 225 if ((forecast != null) && (forecast.getString("internalPartyId") != null)) { 226 internalPartyId = forecast.getString("internalPartyId"); 227 } else { 228 return ServiceUtil.returnError("Invalid forecast or missing forecast.internalPartyId for forecast [" + salesForecastId + "]"); 229 } 230 } 231 232 Map computed = UtilForecast.computeForecastByChildren(parentPeriodId, organizationPartyId, internalPartyId, currencyUomId, delegator); 234 235 ModelService service = dctx.getModelService(serviceName); 237 Map input = service.makeValid(context, "IN"); 238 input.put("customTimePeriodId", parentPeriodId); 240 input.putAll(computed); 242 input.put("userLogin", userLogin); 243 input.put("internalPartyId", internalPartyId); 244 245 Map serviceResults = dispatcher.runSync(serviceName, input); 247 if (ServiceUtil.isError(serviceResults)) { 248 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorComputeForecastFail", locale, module); 249 } 250 if (salesForecastId == null) { 252 salesForecastId = (String ) serviceResults.get("salesForecastId"); 253 } 254 255 Map results = ServiceUtil.returnSuccess(); 256 results.put("salesForecastId", salesForecastId); 257 return results; 258 } catch (GenericEntityException e) { 259 return UtilCommon.createAndLogServiceError(e, "CrmErrorComputeForecastFail", locale, module); 260 } catch (GenericServiceException e) { 261 return UtilCommon.createAndLogServiceError(e, "CrmErrorComputeForecastFail", locale, module); 262 } 263 } 264 265 public static Map updateForecastsRelatedToOpportunity(DispatchContext dctx, Map context) { 266 GenericDelegator delegator = dctx.getDelegator(); 267 LocalDispatcher dispatcher = dctx.getDispatcher(); 268 Security security = dctx.getSecurity(); 269 GenericValue userLogin = (GenericValue) context.get("userLogin"); 270 Locale locale = (Locale) context.get("locale"); 271 ResourceBundleMapWrapper uiLabelMap = (ResourceBundleMapWrapper) UtilProperties.getResourceBundleMap("CRMSFAUiLabels", locale); 272 273 String salesOpportunityId = (String ) context.get("salesOpportunityId"); 274 String changeNote = (String ) context.get("changeNote"); 275 Timestamp previousEstimatedCloseDate = (Timestamp ) context.get("previousEstimatedCloseDate"); 276 277 String organizationPartyId = (String ) UtilProperties.getPropertyValue("crmsfa.properties", "organizationPartyId"); 279 280 try { 281 GenericValue opportunity = delegator.findByPrimaryKey("SalesOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId)); 282 283 StringBuffer changeNoteBuff = new StringBuffer ((String ) uiLabelMap.get(FORECAST_CHANGE_NOTE_PREFIX_UILABEL)); 285 changeNoteBuff.append("<a class='buttontext' HREF='viewOpportunity?salesOpportunityId=").append(salesOpportunityId).append("'>") 286 .append(opportunity.getString("opportunityName")).append(" (").append(salesOpportunityId).append(")</a>"); 287 if (changeNote != null) { 288 changeNoteBuff.append(": ").append(changeNote); 289 } 290 291 304 305 String accountPartyId = (String ) UtilOpportunity.getOpportunityAccountPartyId(opportunity); 306 String leadPartyId = (String ) UtilOpportunity.getOpportunityLeadPartyId(opportunity); 307 boolean isAccountOpportunity = (accountPartyId != null ? true : false); 308 Set partyIds = new HashSet(); 309 310 if (isAccountOpportunity) { 311 List teamMembers = UtilOpportunity.getOpportunityTeamMembers(salesOpportunityId, delegator); 313 for (Iterator iter = teamMembers.iterator(); iter.hasNext(); ) { 314 GenericValue teamMember = (GenericValue) iter.next(); 315 partyIds.add(teamMember.getString("partyId")); 316 } 317 } else { 318 GenericValue leadOwner = PartyHelper.getCurrentLeadOwner(leadPartyId, delegator); 320 if (leadOwner == null) { 321 return UtilCommon.createAndLogServiceError("No LEAD_OWNER for lead ["+leadPartyId+"] found!", locale, module); 322 } 323 partyIds.add(leadOwner.getString("partyId")); 324 } 325 326 if (partyIds.size() == 0) { 328 return ServiceUtil.returnSuccess(); 329 } 330 331 List periodConditions = new ArrayList(); 333 periodConditions.add(EntityUtil.getFilterByDateExpr(opportunity.getTimestamp("estimatedCloseDate"))); 334 if (previousEstimatedCloseDate != null) { 335 periodConditions.add(EntityUtil.getFilterByDateExpr(previousEstimatedCloseDate)); 337 } 338 339 EntityConditionList conditions = new EntityConditionList( UtilMisc.toList( 341 new EntityConditionList(periodConditions, EntityOperator.OR), new EntityExpr("organizationPartyId", EntityOperator.EQUALS, organizationPartyId), 343 new EntityExpr("internalPartyId", EntityOperator.IN, partyIds) 344 ), EntityOperator.AND); 345 346 List forecasts = delegator.findByCondition("SalesForecastAndCustomTimePeriod", conditions, null, null); 347 348 for (Iterator iter = forecasts.iterator(); iter.hasNext(); ) { 350 GenericValue forecast = (GenericValue) iter.next(); 351 if (!forecast.getString("periodTypeId").equals("FISCAL_MONTH")) continue; 352 353 Map input = UtilMisc.toMap("userLogin", userLogin, "organizationPartyId", organizationPartyId, "currencyUomId", forecast.get("currencyUomId")); 354 input.put("customTimePeriodId", forecast.getString("customTimePeriodId")); 355 input.put("salesForecastId", forecast.getString("salesForecastId")); 356 input.put("quotaAmount", forecast.getDouble("quotaAmount")); 357 input.put("changeNote", changeNoteBuff.toString()); 358 Map serviceResults = dispatcher.runSync("crmsfa.computeForecastPeriod", input); 359 if (ServiceUtil.isError(serviceResults)) { 360 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorComputeForecastFail", locale, module); 361 } 362 iter.remove(); } 364 365 for (Iterator iter = forecasts.iterator(); iter.hasNext(); ) { 367 GenericValue forecast = (GenericValue) iter.next(); 368 if (!forecast.getString("periodTypeId").equals("FISCAL_QUARTER")) continue; 369 370 Map input = UtilMisc.toMap("userLogin", userLogin, "organizationPartyId", organizationPartyId, "currencyUomId", forecast.get("currencyUomId")); 371 input.put("parentPeriodId", forecast.getString("customTimePeriodId")); 372 input.put("salesForecastId", forecast.getString("salesForecastId")); 373 input.put("changeNote", changeNoteBuff.toString()); 374 Map serviceResults = dispatcher.runSync("crmsfa.computeForecastParentPeriod", input); 375 if (ServiceUtil.isError(serviceResults)) { 376 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorComputeForecastFail", locale, module); 377 } 378 } 379 } catch (GenericEntityException e) { 380 return UtilCommon.createAndLogServiceError(e, "CrmErrorComputeForecastFail", locale, module); 381 } catch (GenericServiceException e) { 382 return UtilCommon.createAndLogServiceError(e, "CrmErrorComputeForecastFail", locale, module); 383 } 384 return ServiceUtil.returnSuccess(); 385 } 386 } 387 | Popular Tags |