Пример #1
0
            /// <summary>
            /// Calculates the specific reason code on an entity like sales line, tender line etc.
            /// </summary>
            /// <param name="request">The request.</param>
            /// <param name="sourceId">The source identifier.</param>
            /// <param name="tableRefType">The table identifier for the specific reason code.</param>
            /// <param name="refRelation">The first reference relation key corresponding to the entity and table.</param>
            /// <param name="refRelation2">The second reference relation key corresponding to the entity and table.</param>
            /// <param name="refRelation3">The third reference relation key corresponding to the entity and table.</param>
            /// <param name="requiredReasonCodes">The collection to which required reason codes are added.</param>
            /// <param name="reasonCodeRequirements">The required specific reason codes map.</param>
            /// <param name="presentReasonCodeLines">The collection with the already present reason code lines.</param>
            /// <param name="salesLine">The sales line when applicable.</param>
            private static void CalculateReasonCodesSpecificToEntity(
                CalculateRequiredReasonCodesServiceRequest request,
                string sourceId,
                ReasonCodeTableRefType tableRefType,
                string refRelation,
                string refRelation2,
                string refRelation3,
                IDictionary <string, ReasonCode> requiredReasonCodes,
                HashSet <ReasonCodeRequirement> reasonCodeRequirements,
                IEnumerable <ReasonCodeLine> presentReasonCodeLines,
                SalesLine salesLine)
            {
                GetReasonCodesByTableRefTypeDataRequest getReasonCodesByTableRefTypeDataRequest = new GetReasonCodesByTableRefTypeDataRequest(tableRefType, refRelation, refRelation2, refRelation3, QueryResultSettings.AllRecords);
                IEnumerable <ReasonCode> reasonCodes = request.RequestContext.Runtime.Execute <EntityDataServiceResponse <ReasonCode> >(getReasonCodesByTableRefTypeDataRequest, request.RequestContext).PagedEntityCollection.Results;

                reasonCodes = AddLinkedReasonCodes(reasonCodes, request.RequestContext);

                var triggeredReasonCodes = CalculateTriggeredReasonCodes(reasonCodes, presentReasonCodeLines, request.RequestContext);

                reasonCodes = reasonCodes.Union(triggeredReasonCodes).Distinct();

                reasonCodes = AddLinkedReasonCodes(reasonCodes, request.RequestContext);

                foreach (var reasonCode in reasonCodes)
                {
                    if (presentReasonCodeLines.Any(rc => string.Equals(rc.ReasonCodeId, reasonCode.ReasonCodeId, StringComparison.OrdinalIgnoreCase)))
                    {
                        continue;
                    }

                    bool entitySpecificApplicability = true;

                    switch (tableRefType)
                    {
                    case ReasonCodeTableRefType.Item:
                        entitySpecificApplicability = HasSalesLineSpecificApplicability(reasonCode, salesLine);
                        break;
                    }

                    if (entitySpecificApplicability &&
                        ShouldReasonCodeBeApplied(reasonCode, request.SalesTransaction))
                    {
                        var reasonCodeRequirement = new ReasonCodeRequirement()
                        {
                            ReasonCodeId      = reasonCode.ReasonCodeId,
                            SourceId          = sourceId,
                            TableRefTypeValue = (int)tableRefType,
                        };

                        reasonCodeRequirements.Add(reasonCodeRequirement);
                        requiredReasonCodes[reasonCode.ReasonCodeId] = reasonCode;
                    }
                }
            }
            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));
            }