Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

2.2 KB
Raw Blame
using Grand.Business.Core.Interfaces.Catalog.Tax;
using Grand.Business.Core.Interfaces.Common.Configuration;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Utilities.Catalog;
using Tax.FixedRate.Models;

namespace Tax.FixedRate;

public class FixedRateTaxProvider : ITaxProvider
{
    private readonly ISettingService _settingService;
    private readonly ITranslationService _translationService;

    public FixedRateTaxProvider(ISettingService settingService, ITranslationService translationService)
    {
        _settingService = settingService;
        _translationService = translationService;
    }

    public string ConfigurationUrl => FixedRateTaxDefaults.ConfigurationUrl;

    public string SystemName => FixedRateTaxDefaults.ProviderSystemName;

    public string FriendlyName => _translationService.GetResource(FixedRateTaxDefaults.FriendlyName);

    public int Priority => 0;

    public IList<string> LimitedToStores => new List<string>();

    public IList<string> LimitedToGroups => new List<string>();

    /// <summary>
    ///     Gets tax rate
    /// </summary>
    /// <param name="calculateTaxRequest">Tax calculation request</param>
    /// <returns>Tax</returns>
    public async Task<TaxResult> GetTaxRate(TaxRequest calculateTaxRequest)
    {
        var result = new TaxResult {
            TaxRate = await GetTaxRate(calculateTaxRequest.TaxCategoryId,
                calculateTaxRequest.Store?.Id ?? string.Empty)
        };
        return result;
    }

    /// <summary>
    ///     Gets a tax rate
    /// </summary>
    /// <param name="taxCategoryId">The tax category identifier</param>
    /// <param name="storeId">The store identifier the tax is being calculated for</param>
    /// <returns>Tax rate</returns>
    private async Task<double> GetTaxRate(string taxCategoryId, string storeId)
    {
        //use the store specific rate when defined, otherwise fall back to the global rate
        //(SettingService.GetSettingByKey resolves the store override first and the global value second)
        var rate = (await _settingService.GetSettingByKey<FixedTaxRate>(
            $"Tax.TaxProvider.FixedRate.TaxCategoryId{taxCategoryId}", storeId: storeId))?.Rate;
        return rate ?? 0;
    }
}