📄 forecastsservices.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 *//* Copyright (c) 2005-2006 Open Source Strategies, Inc. *//* * $Id:$ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package com.opensourcestrategies.crmsfa.forecasts;import java.util.*;import java.sql.Timestamp;import org.ofbiz.base.util.*;import org.ofbiz.base.util.collections.ResourceBundleMapWrapper;import org.ofbiz.entity.*;import org.ofbiz.entity.condition.*;import org.ofbiz.entity.util.EntityUtil;import org.ofbiz.service.*;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.forecasts.UtilForecast;import com.opensourcestrategies.crmsfa.opportunities.UtilOpportunity;/** * Forecasts services. The service documentation is in services_forecasts.xml. * * @author <a href="mailto:leon@opensourcestrategies.com">Leon Torres</a> * @author <a href="mailto:sichen@opensourcestrategies.com">Si Chen</a> * @version $Rev: 93 $ */public class ForecastsServices { public static final String module = ForecastsServices.class.getName(); protected static final String FORECAST_CHANGE_NOTE_PREFIX_UILABEL = "CrmForecastChangeNotePrefix"; public static Map updateForecast(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 salesForecastId = (String) context.get("salesForecastId"); Double quotaAmount = (Double) context.get("quotaAmount"); try { GenericValue forecast = delegator.findByPrimaryKey("SalesForecast", UtilMisc.toMap("salesForecastId", salesForecastId)); if (forecast == null) { return UtilCommon.createAndLogServiceError("Forecast with ID [" + salesForecastId + "] not found.", "CrmErrorComputeForecastFail", locale, module); } // compute the fields for the forecast (use the internalPartyId of the existing forecast) Map computed = UtilForecast.computeForecastByOpportunities(quotaAmount, forecast.getString("organizationPartyId"), forecast.getString("internalPartyId"), forecast.getString("currencyUomId"), forecast.getString("customTimePeriodId"), delegator); // make the service input map from the context ModelService service = dctx.getModelService("updateSalesForecast"); Map input = service.makeValid(context, "IN"); // add rest of fields (in this case we preserve the previous internalPartyId) input.put("salesForecastId", salesForecastId); input.putAll(computed); input.put("userLogin", userLogin); // run our update/create service Map serviceResults = dispatcher.runSync("updateSalesForecast", input); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorComputeForecastFail", locale, module); } // now recompute the parent forecast by calling our service for this with the parent as input // TODO: normally we could use: GenericValue parent = forecast.getRelatedOne("ParentSalesForecast"); // TODO: but we can't yet untill we re-arrange the way forecasts are created (create all of them once quarter is selected, then compute values) // TODO: so this is a complete hack: GenericValue period = forecast.getRelatedOne("CustomTimePeriod"); List parents = delegator.findByAnd("SalesForecastAndCustomTimePeriod", UtilMisc.toMap("customTimePeriodId", period.getString("parentPeriodId"), "internalPartyId", forecast.getString("internalPartyId"), "organizationPartyId", forecast.getString("organizationPartyId"), "periodTypeId", "FISCAL_QUARTER")); // XXX HACK GenericValue parent = (GenericValue) parents.get(0); // XXX HACK service = dctx.getModelService("crmsfa.computeForecastParentPeriod"); input = service.makeValid(parent.getAllFields(), "IN"); input.put("userLogin", userLogin); input.put("parentPeriodId", period.getString("parentPeriodId")); input.put("changeNote", context.get("changeNote")); // also update the change note in parent 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(); } // TODO: The next two methods have a lot of overlapping code. We should try to re-factor. public static Map computeForecastPeriod(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 customTimePeriodId = (String) context.get("customTimePeriodId"); String organizationPartyId = (String) context.get("organizationPartyId"); String currencyUomId = (String) context.get("currencyUomId"); // set the quota to 0.00 if the user didn't supply it, that way we get all forecasts for a set of periods rather than a few that had quotas defined Double quotaAmount = (Double) context.get("quotaAmount"); if (quotaAmount == null) quotaAmount = new Double(0.00); try { 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.computeForecastByOpportunities(quotaAmount, organizationPartyId, internalPartyId, currencyUomId, customTimePeriodId, delegator); // make the service input map from the context ModelService service = dctx.getModelService(serviceName); Map input = service.makeValid(context, "IN"); // 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); } } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorComputeForecastFail", locale, module); } catch (GenericServiceException e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -