/// <summary> /// Gets the reason code settings dictionary. /// </summary> /// <param name="context">The context.</param> /// <returns>The reason code settings dictionary.</returns> private static ReasonCodeSettings GetReasonCodeSettings(RequestContext context) { ReasonCodeSettings settings = null; GetReasonCodeSettingsDataRequest getReasonCodeSettingsDataRequest = new GetReasonCodeSettingsDataRequest(QueryResultSettings.SingleRecord); settings = context.Runtime.Execute <SingleEntityDataServiceResponse <ReasonCodeSettings> >(getReasonCodeSettingsDataRequest, context).Entity; // Reason code settings should be available for retail store. if (settings == null) { var getChannelByIdDataRequest = new GetChannelByIdDataRequest(context.GetPrincipal().ChannelId); Channel currentChannel = context.Runtime.Execute <SingleEntityDataServiceResponse <Channel> >(getChannelByIdDataRequest, context).Entity; if (currentChannel.OrgUnitType == RetailChannelType.RetailStore) { throw new DataValidationException( DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_ObjectNotFound, "The required reason code settings not found."); } // Use default settings for non-retail-store channels. settings = ReasonCodeSettings.DefaultSettings; } return(settings); }
private SingleEntityDataServiceResponse <ReasonCodeSettings> GetReasonCodeSettings(GetReasonCodeSettingsDataRequest request) { ThrowIf.Null(request, "request"); ThrowIf.Null(request.QueryResultSettings, "request.QueryResultSettings"); ReasonCodeL2CacheDataStoreAccessor level2CacheDataAccessor = GetReasonCodeL2CacheDataStoreAccessor(request.RequestContext); bool found; bool updateL2Cache; ReasonCodeSettings result = DataManager.GetDataFromCache(() => level2CacheDataAccessor.GetReasonCodeSettings(request.QueryResultSettings.ColumnSet), out found, out updateL2Cache); if (!found) { var query = new SqlPagedQuery(QueryResultSettings.SingleRecord) { Select = request.QueryResultSettings.ColumnSet, From = ReasonCodeSettingsViewName, Where = "CHANNELID = @channelId", }; query.Parameters["@channelId"] = request.RequestContext.GetPrincipal().ChannelId; using (DatabaseContext databaseContext = new DatabaseContext(request.RequestContext)) { result = databaseContext.ReadEntity <ReasonCodeSettings>(query).SingleOrDefault(); updateL2Cache &= result != null; } } if (updateL2Cache) { level2CacheDataAccessor.PutReasonCodeSettings(result); } return(new SingleEntityDataServiceResponse <ReasonCodeSettings>(result)); }
/// <summary> /// Calculates the required reason codes given the source type. /// </summary> /// <param name="request">The request.</param> /// <param name="requiredReasonCodes">The collection to which required reason codes are added.</param> /// <param name="transactionRequiredReasonCodeIds">The identifiers of reason codes required at transaction level.</param> /// <param name="reasonCodeRequirements">The collection of reason code requirements.</param> private static void CalculateRequiredReasonCodesBySourceType( CalculateRequiredReasonCodesServiceRequest request, Dictionary <string, ReasonCode> requiredReasonCodes, HashSet <string> transactionRequiredReasonCodeIds, HashSet <ReasonCodeRequirement> reasonCodeRequirements) { // Look up operation level reason codes if available. if (request.SourceType != ReasonCodeSourceType.None) { ReasonCodeSettings settingsDictionary = GetReasonCodeSettings(request.RequestContext); string reasonCodeId = settingsDictionary[request.SourceType]; if (!string.IsNullOrWhiteSpace(reasonCodeId)) { GetReasonCodesDataRequest getReasonCodeRequest = new GetReasonCodesDataRequest(QueryResultSettings.AllRecords, new string[] { reasonCodeId }); IEnumerable <ReasonCode> reasonCodes = request.RequestContext.Execute <EntityDataServiceResponse <ReasonCode> >(getReasonCodeRequest).PagedEntityCollection.Results; foreach (var reasonCode in reasonCodes) { if (IsTransactionSourceType(request.SourceType) && ShouldReasonCodeBeApplied(reasonCode, request.SalesTransaction)) { if (!ContainsReasonCode(request.SalesTransaction.ReasonCodeLines, reasonCode.ReasonCodeId)) { requiredReasonCodes[reasonCode.ReasonCodeId] = reasonCode; transactionRequiredReasonCodeIds.Add(reasonCode.ReasonCodeId); } var triggeredReasonCodes = CalculateTriggeredReasonCodes( new ReasonCode[] { reasonCode }, request.SalesTransaction.ReasonCodeLines, request.RequestContext); if (triggeredReasonCodes.Any()) { requiredReasonCodes.AddRange(triggeredReasonCodes.ToDictionary(rc => rc.ReasonCodeId, rc => rc)); transactionRequiredReasonCodeIds.AddRange(triggeredReasonCodes.Select(rc => rc.ReasonCodeId)); } } else { foreach (var salesLine in request.SalesLines) { if (ShouldReasonCodeBeApplied(reasonCode, request.SalesTransaction)) { if (!ContainsReasonCode(salesLine.ReasonCodeLines, reasonCode.ReasonCodeId)) { var reasonCodeRequirement = new ReasonCodeRequirement() { ReasonCodeId = reasonCode.ReasonCodeId, SourceId = salesLine.ProductId.ToString(CultureInfo.InvariantCulture), TableRefTypeValue = (int)ReasonCodeTableRefType.Item, }; reasonCodeRequirements.Add(reasonCodeRequirement); requiredReasonCodes[reasonCode.ReasonCodeId] = reasonCode; } var triggeredReasonCodes = CalculateTriggeredReasonCodes( new ReasonCode[] { reasonCode }, salesLine.ReasonCodeLines, request.RequestContext); if (triggeredReasonCodes.Any()) { requiredReasonCodes.AddRange(triggeredReasonCodes.ToDictionary(rc => rc.ReasonCodeId, rc => rc)); reasonCodeRequirements.AddRange(triggeredReasonCodes.Select(rc => new ReasonCodeRequirement() { ReasonCodeId = rc.ReasonCodeId, SourceId = salesLine.ProductId.ToString(CultureInfo.InvariantCulture), TableRefTypeValue = (int)ReasonCodeTableRefType.Item, })); } } } } } } } }