Ejemplo n.º 1
0
            /// <summary>
            /// The data service method to get the sales tax group for the given predicates.
            /// </summary>
            /// <param name="request">The data service request.</param>
            /// <returns>The data service response.</returns>
            private SingleEntityDataServiceResponse <string> GetSalesTaxGroup(GetSalesTaxGroupDataRequest request)
            {
                ThrowIf.NullOrEmpty(request.Predicates, "predicates");

                var taxDataManager = this.GetDataManagerInstance(request.RequestContext);
                TaxL2CacheDataStoreAccessor level2CacheDataAccessor = (TaxL2CacheDataStoreAccessor)taxDataManager.DataStoreManagerInstance.RegisteredAccessors[DataStoreType.L2Cache];

                bool   found;
                bool   updateL2Cache;
                string result = DataManager.GetDataFromCache(() => level2CacheDataAccessor.GetSalesTaxGroup(request.Predicates), out found, out updateL2Cache);

                if (!found)
                {
                    result = string.Empty;

                    ParameterSet parameters = new ParameterSet();

                    foreach (var predicate in request.Predicates)
                    {
                        parameters["nvc_" + predicate.Key] = predicate.Value;
                    }

                    SalesTaxGroup group;
                    using (SqlServerDatabaseContext databaseContext = new SqlServerDatabaseContext(request))
                    {
                        group = databaseContext.ExecuteStoredProcedure <SalesTaxGroup>(GetTaxRegimeSprocName, parameters).FirstOrDefault();
                    }

                    if (group != null)
                    {
                        result = group.TaxGroupName;
                    }

                    updateL2Cache &= result != null;
                }

                if (updateL2Cache)
                {
                    level2CacheDataAccessor.PutSalesTaxGroup(request.Predicates, result);
                }

                return(new SingleEntityDataServiceResponse <string>(result));
            }
Ejemplo n.º 2
0
            /// <summary>
            /// Gets the tax regime.
            /// </summary>
            /// <param name="context">The request context.</param>
            /// <param name="shippingAddress">Shipping address.</param>
            /// <returns>
            /// The sales tax regime information.
            /// </returns>
            internal static string GetTaxRegime(RequestContext context, Address shippingAddress)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }

                if (context.Runtime == null)
                {
                    throw new ArgumentNullException("context", "context.Runtime");
                }

                if (shippingAddress == null)
                {
                    throw new ArgumentNullException("shippingAddress");
                }

                if (string.IsNullOrWhiteSpace(shippingAddress.ThreeLetterISORegionName) &&
                    string.IsNullOrWhiteSpace(shippingAddress.TwoLetterISORegionName))
                {
                    throw new ArgumentNullException("shippingAddress", "shippingAddress ISORegionName");
                }

                string taxRegime = string.Empty;

                // If address has a tax group, then return this tax group.
                if (!string.IsNullOrWhiteSpace(shippingAddress.TaxGroup))
                {
                    taxRegime = shippingAddress.TaxGroup;
                    return(taxRegime);
                }

                Dictionary <string, string> predicates = new Dictionary <string, string>();

                var addressComponentsFilterHandlers = BuildDbtFilterList();

                for (int i = 0; i < addressComponentsFilterHandlers.Count; i++)
                {
                    predicates.Clear();

                    for (int j = i; j < addressComponentsFilterHandlers.Count; j++)
                    {
                        if (addressComponentsFilterHandlers[j].CanHandle(shippingAddress))
                        {
                            addressComponentsFilterHandlers[j].Handle(shippingAddress, predicates);
                        }
                    }

                    GetSalesTaxGroupDataRequest dataRequest = new GetSalesTaxGroupDataRequest(predicates);
                    taxRegime = context.Execute <SingleEntityDataServiceResponse <string> >(dataRequest).Entity;

                    if (!string.IsNullOrWhiteSpace(taxRegime))
                    {
                        break;
                    }
                }

                if (string.IsNullOrWhiteSpace(taxRegime))
                {
                    InvalidTaxGroupNotification notification = new InvalidTaxGroupNotification(shippingAddress);
                    context.Notify(notification);
                }

                return(taxRegime);
            }