Exemple #1
0
            private static EntityDataServiceResponse <ReasonCode> GetReasonCodes(GetReasonCodesDataRequest request)
            {
                string defaultLanguageId  = GetDefaultLanguageId(request.RequestContext);
                string employeeLanguageId = GetEmployeeLanguageId(request.RequestContext);

                GetReasonCodesProcedure  getReasonCodesProcedure = new GetReasonCodesProcedure(request, defaultLanguageId, employeeLanguageId);
                PagedResult <ReasonCode> reasonCodes             = getReasonCodesProcedure.Execute();

                return(new EntityDataServiceResponse <ReasonCode>(reasonCodes));
            }
            /// <summary>
            /// Gets the reason codes.
            /// </summary>
            /// <param name="request">The request.</param>
            /// <returns>The info codes response.</returns>
            private static GetReasonCodesServiceResponse GetReasonCodes(GetReasonCodesServiceRequest request)
            {
                RequestContext context = request.RequestContext;

                GetReasonCodesDataRequest getReasonCodeRequest = new GetReasonCodesDataRequest(request.QueryResultSettings, request.ReasonCodeIds);
                var reasonCodes = context.Execute <EntityDataServiceResponse <ReasonCode> >(getReasonCodeRequest).PagedEntityCollection;

                SetProductIdsForUpsell(request.RequestContext, reasonCodes.Results);

                return(new GetReasonCodesServiceResponse(reasonCodes));
            }
Exemple #3
0
            /// <summary>
            /// Calculates reason codes which can be triggered by another reason codes.
            /// </summary>
            /// <param name="reasonCodes">The reason codes collection.</param>
            /// <param name="reasonCodeLines">The reason code lines collection.</param>
            /// <param name="context">The request context.</param>
            /// <returns>Collection of reason codes which can be triggered by another reason codes.</returns>
            private static IEnumerable <ReasonCode> CalculateTriggeredReasonCodes(
                IEnumerable <ReasonCode> reasonCodes, IEnumerable <ReasonCodeLine> reasonCodeLines, RequestContext context)
            {
                HashSet <string> missingIds = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                IDictionary <string, ReasonCode> triggeredReasonCodes = new Dictionary <string, ReasonCode>();
                var subReasonCodes      = GetReasonSubCodesById(reasonCodes);
                var reasonCodeLinesById = reasonCodeLines.ToDictionary(r => r.ReasonCodeId);

                foreach (var reasonCodeLine in reasonCodeLinesById.Values)
                {
                    ReasonSubCode subCode;
                    if (subReasonCodes.TryGetValue(reasonCodeLine.ReasonCodeId + reasonCodeLine.SubReasonCodeId, out subCode))
                    {
                        // if this sub code triggers another reason code.
                        if (subCode.TriggerFunctionType == TriggerFunctionType.InfoCode &&
                            !string.IsNullOrWhiteSpace(subCode.TriggerCode))
                        {
                            // check whether we have the reason code already.
                            var reasonCode = reasonCodes.FirstOrDefault(r => string.Equals(r.ReasonCodeId, subCode.TriggerCode, StringComparison.OrdinalIgnoreCase));
                            if (reasonCode != null)
                            {
                                if (!triggeredReasonCodes.ContainsKey(reasonCode.ReasonCodeId) &&
                                    !reasonCodeLinesById.ContainsKey(reasonCode.ReasonCodeId))
                                {
                                    triggeredReasonCodes[reasonCode.ReasonCodeId] = reasonCode;
                                }
                            }
                            else
                            {
                                missingIds.Add(subCode.TriggerCode);
                            }
                        }
                    }
                }

                if (missingIds.Any())
                {
                    GetReasonCodesDataRequest getReasonCodeRequest = new GetReasonCodesDataRequest(QueryResultSettings.AllRecords, missingIds);
                    var missingReasonCodes = context.Execute <EntityDataServiceResponse <ReasonCode> >(getReasonCodeRequest).PagedEntityCollection.Results;

                    foreach (var missingReasonCode in missingReasonCodes)
                    {
                        if (!triggeredReasonCodes.ContainsKey(missingReasonCode.ReasonCodeId) &&
                            !reasonCodeLinesById.ContainsKey(missingReasonCode.ReasonCodeId))
                        {
                            triggeredReasonCodes[missingReasonCode.ReasonCodeId] = missingReasonCode;
                        }
                    }
                }

                return(triggeredReasonCodes.Values);
            }
Exemple #4
0
            /// <summary>
            /// Calculates reason codes that are linked by other reason codes.
            /// </summary>
            /// <param name="reasonCodes">The reason codes collection.</param>
            /// <param name="reasonCode">The reason code.</param>
            /// <param name="context">The request context.</param>
            /// <returns>Collection of reason codes which are linked by present reason codes and not on the original collection.</returns>
            private static IEnumerable <ReasonCode> CalculateLinkedReasonCodes(
                IEnumerable <ReasonCode> reasonCodes,
                ReasonCode reasonCode,
                RequestContext context)
            {
                HashSet <string> missingIds = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                IDictionary <string, ReasonCode> linkedReasonCodes = new Dictionary <string, ReasonCode>();
                var reasonCodesById = reasonCodes.ToDictionary(r => r.ReasonCodeId);

                missingIds.Add(reasonCode.LinkedReasonCodeId);

                // get all the missing reason codes. also makes sure all the linked reason codes by the missing reason codes are also included and so on.
                while (missingIds.Any())
                {
                    HashSet <string>          missingLinkedIds     = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                    GetReasonCodesDataRequest getReasonCodeRequest = new GetReasonCodesDataRequest(QueryResultSettings.AllRecords, missingIds);
                    var missingReasonCodes = context.Execute <EntityDataServiceResponse <ReasonCode> >(getReasonCodeRequest).PagedEntityCollection.Results;

                    foreach (var missingReasonCode in missingReasonCodes)
                    {
                        if (!linkedReasonCodes.ContainsKey(missingReasonCode.ReasonCodeId) && !reasonCodesById.ContainsKey(missingReasonCode.ReasonCodeId))
                        {
                            linkedReasonCodes[missingReasonCode.ReasonCodeId] = missingReasonCode;

                            // if the new reason code links to a different one, and the new one is not present, adds it for later.
                            if (!string.IsNullOrWhiteSpace(missingReasonCode.LinkedReasonCodeId) &&
                                !reasonCodesById.ContainsKey(missingReasonCode.LinkedReasonCodeId))
                            {
                                missingLinkedIds.Add(missingReasonCode.LinkedReasonCodeId);
                            }
                        }
                    }

                    // verify the new linked reason codes
                    missingIds = missingLinkedIds;
                }

                return(linkedReasonCodes.Values);
            }
Exemple #5
0
            /// <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,
                                            }));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            private static EntityDataServiceResponse <ReasonCode> GetReasonCodes(GetReasonCodesDataRequest request)
            {
                ThrowIf.Null(request, "request");
                ThrowIf.Null(request.QueryResultSettings, "request.QueryResultSettings");

                string channelLanguageId  = GetDefaultLanguageId(request.RequestContext);
                string employeeLanguageId = GetEmployeeLanguageId(request.RequestContext);

                ReasonCodeL2CacheDataStoreAccessor level2CacheDataAccessor = GetReasonCodeL2CacheDataStoreAccessor(request.RequestContext);

                bool found;
                bool updateL2Cache;
                PagedResult <ReasonCode> result = DataManager.GetDataFromCache(() => level2CacheDataAccessor.GetReasonCodes(request.ReasonCodeIds, channelLanguageId, employeeLanguageId, request.QueryResultSettings), out found, out updateL2Cache);

                if (!found)
                {
                    SortingInfo sortingInfo = request.QueryResultSettings.Sorting;
                    if (!sortingInfo.IsSpecified)
                    {
                        sortingInfo = new SortingInfo(ReasonCode.PriorityColumn, false);
                    }

                    var query = new SqlPagedQuery(request.QueryResultSettings)
                    {
                        OrderBy = sortingInfo.ToString(),
                        From    = ReasonCodeFunctionName,
                        Aliased = true,
                    };

                    AddLanguageIdToQuery(query, request.RequestContext);
                    AddDataAreaIdToQuery(query, request.RequestContext);

                    // Load info codes
                    using (SqlServerDatabaseContext sqlServerDatabaseContext = new SqlServerDatabaseContext(request))
                        using (StringIdTableType groupIds = new StringIdTableType(request.ReasonCodeIds, "GROUPID"))
                        {
                            // the view sets the INFOCODEID to GROUPID when the reason code is not part of a group, so we always query by GROUPID
                            query.Parameters["@tvp_groupIds"] = groupIds.DataTable;

                            result = sqlServerDatabaseContext.ReadEntity <ReasonCode>(query);
                        }

                    // Load sub codes
                    if (result.Results.Any())
                    {
                        QueryResultSettings subCodeSettings = QueryResultSettings.AllRecords;
                        var subcodes = GetReasonSubCodes(result.Results.Select(x => x.ReasonCodeId), null, subCodeSettings, request).Results.ToLookup(x => x.ReasonCodeId);

                        foreach (var infoCode in result.Results)
                        {
                            infoCode.ReasonSubCodes.Clear();
                            infoCode.ReasonSubCodes.AddRange(subcodes[infoCode.ReasonCodeId]);
                        }
                    }

                    updateL2Cache &= result != null && result.Results.Count < MaxCachedCollectionSize;
                }

                if (updateL2Cache)
                {
                    level2CacheDataAccessor.PutReasonCodes(request.ReasonCodeIds, channelLanguageId, employeeLanguageId, request.QueryResultSettings, result);
                }

                return(new EntityDataServiceResponse <ReasonCode>(result));
            }
            /// <summary>
            /// Gets the reason sub codes with given reason code identifier or reason sub code identifier.
            /// </summary>
            /// <param name="reasonCodeIds">The reason code identifier.</param>
            /// <param name="reasonSubCodeId">The reason sub code identifier.</param>
            /// <param name="settings">The query result settings.</param>
            /// <param name="request">The get reason codes data request.</param>
            /// <returns>The info sub codes.</returns>
            private static PagedResult <ReasonSubCode> GetReasonSubCodes(IEnumerable <string> reasonCodeIds, string reasonSubCodeId, QueryResultSettings settings, GetReasonCodesDataRequest request)
            {
                ThrowIf.Null(reasonCodeIds, "reasonCodeIds");
                ThrowIf.Null(settings, "settings");

                var query = new SqlPagedQuery(settings)
                {
                    From    = ReasonSubCodeFunctionName,
                    Aliased = true
                };

                BuildSubReasonCodesQuery(reasonCodeIds, reasonSubCodeId, query, request.RequestContext);

                PagedResult <ReasonSubCode> reasonSubcodes;

                using (StringIdTableType type = new StringIdTableType(reasonCodeIds, "REASONCODEID"))
                {
                    query.Parameters["@TVP_INFOCODEIDTABLETYPE"] = type;

                    using (SqlServerDatabaseContext sqlServerDatabaseContext = new SqlServerDatabaseContext(request))
                    {
                        reasonSubcodes = sqlServerDatabaseContext.ReadEntity <ReasonSubCode>(query);
                    }

                    return(reasonSubcodes);
                }
            }
 /// <summary>
 /// Initializes a new instance of the <see cref="GetReasonCodesProcedure" /> class.
 /// </summary>
 /// <param name="request">The request message.</param>
 /// <param name="defaultlanguageId">The channel default language identifier.</param>
 /// <param name="employeeLanguageId">The employee language identifier.</param>
 public GetReasonCodesProcedure(GetReasonCodesDataRequest request, string defaultlanguageId, string employeeLanguageId)
 {
     this.request            = request;
     this.defaultLanguageId  = defaultlanguageId;
     this.employeeLanguageId = employeeLanguageId;
 }
            private EntityDataServiceResponse <ReasonCode> GetReasonCodesByTableRefType(GetReasonCodesByTableRefTypeDataRequest request)
            {
                ThrowIf.Null(request, "request");
                ThrowIf.Null(request.QueryResultSettings, "request.QueryResultSettings");

                ReasonCodeL2CacheDataStoreAccessor level2CacheDataAccessor = GetReasonCodeL2CacheDataStoreAccessor(request.RequestContext);

                bool found;
                bool updateL2Cache;
                PagedResult <ReasonCode> result = DataManager.GetDataFromCache(() => level2CacheDataAccessor.GetReasonCodeSpecific(request.TableRefType, request.RefRelation, request.RefRelation2, request.RefRelation3), out found, out updateL2Cache);

                if (!found)
                {
                    var query = new SqlPagedQuery(QueryResultSettings.AllRecords)
                    {
                        From    = ReasonCodeTableSpecificViewName,
                        OrderBy = InfoCodeIdColumnName
                    };

                    var whereClauses = new List <string>();

                    whereClauses.Add(@"(REFTABLEID = @RefTableId)");
                    query.Parameters["@RefTableId"] = request.TableRefType;

                    // Add query clause for ref relations
                    if (!string.IsNullOrWhiteSpace(request.RefRelation))
                    {
                        whereClauses.Add(@"(REFRELATION = @RefRelation)");
                        query.Parameters["@RefRelation"] = request.RefRelation;
                    }

                    if (!string.IsNullOrWhiteSpace(request.RefRelation2))
                    {
                        whereClauses.Add(@"(REFRELATION2 = @RefRelation2)");
                        query.Parameters["@RefRelation2"] = request.RefRelation2;
                    }

                    if (!string.IsNullOrWhiteSpace(request.RefRelation3))
                    {
                        whereClauses.Add(@"(REFRELATION3 = @RefRelation3)");
                        query.Parameters["@RefRelation3"] = request.RefRelation3;
                    }

                    // Compose the where clause
                    if (whereClauses.Count != 0)
                    {
                        query.Where = string.Join(" AND ", whereClauses);
                    }

                    query.OrderBy = "SEQUENCE ASC";

                    var reasonCodes = new Collection <ReasonCode>();

                    // Load specific reason codes only if at least one refRelation specified with tableRefType
                    if (whereClauses.Count >= 2)
                    {
                        PagedResult <ReasonCodeSpecific> reasonCodesSpecific;

                        using (DatabaseContext databaseContext = new DatabaseContext(request.RequestContext))
                        {
                            reasonCodesSpecific = databaseContext.ReadEntity <ReasonCodeSpecific>(query);
                        }

                        if (reasonCodesSpecific != null)
                        {
                            foreach (var reasonCodeSpecific in reasonCodesSpecific.Results)
                            {
                                GetReasonCodesDataRequest getReasonCodesDataRequest = new GetReasonCodesDataRequest(QueryResultSettings.AllRecords, new string[] { reasonCodeSpecific.ReasonCodeId });
                                var machingReasonCodes = request.RequestContext.Runtime.Execute <EntityDataServiceResponse <ReasonCode> >(getReasonCodesDataRequest, request.RequestContext).PagedEntityCollection.Results;

                                foreach (var matchingCode in machingReasonCodes)
                                {
                                    matchingCode.InputRequired     = reasonCodeSpecific.InputRequired;
                                    matchingCode.InputRequiredType = (ReasonCodeInputRequiredType)reasonCodeSpecific.InputRequiredTypeValue;
                                }

                                reasonCodes.AddRange(machingReasonCodes);
                            }
                        }
                    }

                    result = reasonCodes.AsPagedResult();

                    updateL2Cache &= result != null && result.Results.Count < MaxCachedCollectionSize;
                }

                if (updateL2Cache)
                {
                    level2CacheDataAccessor.PutReasonCodeSpecific(request.TableRefType, request.RefRelation, request.RefRelation2, request.RefRelation3, result);
                }

                return(new EntityDataServiceResponse <ReasonCode>(result));
            }