Example #1
0
        /// <summary>
        /// Prepare paged tax category list model
        /// </summary>
        /// <param name="searchModel">Tax category search model</param>
        /// <returns>Tax category list model</returns>
        public override TaxCategoryListModel PrepareTaxCategoryListModel(TaxCategorySearchModel searchModel)
        {
            //ensure that Avalara tax provider is active
            if (!(_taxService.LoadActiveTaxProvider(_workContext.CurrentCustomer) is AvalaraTaxProvider))
            {
                return(base.PrepareTaxCategoryListModel(searchModel));
            }

            if (searchModel == null)
            {
                throw new ArgumentNullException(nameof(searchModel));
            }

            //get tax categories
            var taxCategories = _taxCategoryService.GetAllTaxCategories();

            //get tax types and define the default value
            var taxTypes = _cacheManager.Get(AvalaraTaxDefaults.TaxCodeTypesCacheKey, () => _avalaraTaxManager.GetTaxCodeTypes())
                           ?.Select(taxType => new { Id = taxType.Key, Name = taxType.Value });
            var defaultType = taxTypes
                              ?.FirstOrDefault(taxType => taxType.Name.Equals("Unknown", StringComparison.InvariantCultureIgnoreCase))
                              ?? taxTypes?.FirstOrDefault();

            //prepare grid model
            var model = new TaxCategoryListModel
            {
                Data = taxCategories.PaginationByRequestModel(searchModel).Select(taxCategory =>
                {
                    //fill in model values from the entity
                    var taxCategoryModel = new Models.Tax.TaxCategoryModel
                    {
                        Id           = taxCategory.Id,
                        Name         = taxCategory.Name,
                        DisplayOrder = taxCategory.DisplayOrder
                    };

                    //try to get previously saved tax code type and description
                    var taxCodeType = taxTypes?.FirstOrDefault(type =>
                                                               type.Id.Equals(_genericAttributeService.GetAttribute <string>(taxCategory, AvalaraTaxDefaults.TaxCodeTypeAttribute) ?? string.Empty))
                                      ?? defaultType;
                    taxCategoryModel.Type        = taxCodeType?.Name ?? string.Empty;
                    taxCategoryModel.TypeId      = taxCodeType?.Id ?? Guid.Empty.ToString();
                    taxCategoryModel.Description = _genericAttributeService
                                                   .GetAttribute <string>(taxCategory, AvalaraTaxDefaults.TaxCodeDescriptionAttribute) ?? string.Empty;

                    return(taxCategoryModel);
                }),
                Total = taxCategories.Count
            };

            return(model);
        }
Example #2
0
        /// <summary>
        /// Prepare paged tax category list model
        /// </summary>
        /// <param name="searchModel">Tax category search model</param>
        /// <returns>Tax category list model</returns>
        public virtual async Task <TaxCategoryListModel> PrepareTaxCategoryListModelAsync(TaxCategorySearchModel searchModel)
        {
            if (searchModel == null)
            {
                throw new ArgumentNullException(nameof(searchModel));
            }

            //get tax categories
            var taxCategories = (await _taxCategoryService.GetAllTaxCategoriesAsync()).ToPagedList(searchModel);

            //prepare grid model
            var model = new TaxCategoryListModel().PrepareToGrid(searchModel, taxCategories, () =>
            {
                //fill in model values from the entity
                return(taxCategories.Select(taxCategory => taxCategory.ToModel <TaxCategoryModel>()));
            });

            return(model);
        }
Example #3
0
        /// <summary>
        /// Prepare paged tax category list model
        /// </summary>
        /// <param name="searchModel">Tax category search model</param>
        /// <returns>Tax category list model</returns>
        public virtual TaxCategoryListModel PrepareTaxCategoryListModel(TaxCategorySearchModel searchModel)
        {
            if (searchModel == null)
            {
                throw new ArgumentNullException(nameof(searchModel));
            }

            //get tax categories
            var taxCategories = _taxCategoryService.GetAllTaxCategories();

            //prepare grid model
            var model = new TaxCategoryListModel
            {
                //fill in model values from the entity
                Data  = taxCategories.PaginationByRequestModel(searchModel).Select(taxCategory => taxCategory.ToModel <TaxCategoryModel>()),
                Total = taxCategories.Count
            };

            return(model);
        }