Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

TaxModelFactory.cs

using Nop.Services.Tax;
using Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions;
using Nop.Web.Areas.Admin.Models.Tax;
using Nop.Web.Framework.Models.Extensions;

namespace Nop.Web.Areas.Admin.Factories;

/// 
/// Represents the tax model factory implementation
/// 
public partial class TaxModelFactory : ITaxModelFactory
{
    #region Fields

    protected readonly ITaxCategoryService _taxCategoryService;
    protected readonly ITaxPluginManager _taxPluginManager;

    #endregion

    #region Ctor

    public TaxModelFactory(
        ITaxCategoryService taxCategoryService,
        ITaxPluginManager taxPluginManager)
    {
        _taxCategoryService = taxCategoryService;
        _taxPluginManager = taxPluginManager;
    }

    #endregion

    #region Methods

    /// 
    /// Prepare tax provider search model
    /// 
    /// Tax provider search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the ax provider search model
    /// 
    public virtual Task PrepareTaxProviderSearchModelAsync(TaxProviderSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return Task.FromResult(searchModel);
    }

    /// 
    /// Prepare paged tax provider list model
    /// 
    /// Tax provider search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the ax provider list model
    /// 
    public virtual async Task PrepareTaxProviderListModelAsync(TaxProviderSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get tax providers
        var taxProviders = (await _taxPluginManager.LoadAllPluginsAsync()).ToPagedList(searchModel);

        //prepare grid model
        var model = new TaxProviderListModel().PrepareToGrid(searchModel, taxProviders, () =>
        {
            return taxProviders.Select(provider =>
            {
                //fill in model values from the entity
                var taxProviderModel = provider.ToPluginModel();

                //fill in additional values (not existing in the entity)
                taxProviderModel.ConfigurationUrl = provider.GetConfigurationPageUrl();
                taxProviderModel.IsPrimaryTaxProvider = _taxPluginManager.IsPluginActive(provider);

                return taxProviderModel;
            });
        });

        return model;
    }

    /// 
    /// Prepare tax category search model
    /// 
    /// Tax category search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the ax category search model
    /// 
    public virtual Task PrepareTaxCategorySearchModelAsync(TaxCategorySearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return Task.FromResult(searchModel);
    }

    /// 
    /// Prepare paged tax category list model
    /// 
    /// Tax category search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the ax category list model
    /// 
    public virtual async Task PrepareTaxCategoryListModelAsync(TaxCategorySearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(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());
        });

        return model;
    }

    #endregion
}