69 lines
2.3 KiB
C#
69 lines
2.3 KiB
C#
using CMSMicroservice.Protobuf.Protos.Configuration;
|
|
|
|
namespace FrontOffice.BFF.Application.UserOrderCQ.Queries.GetVATRate;
|
|
|
|
/// <summary>
|
|
/// هندلر دریافت نرخ مالیات بر ارزش افزوده
|
|
/// </summary>
|
|
public class GetVATRateQueryHandler : IRequestHandler<GetVATRateQuery, GetVATRateResponseDto>
|
|
{
|
|
private readonly IApplicationContractContext _context;
|
|
|
|
public GetVATRateQueryHandler(IApplicationContractContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<GetVATRateResponseDto> Handle(GetVATRateQuery request, CancellationToken cancellationToken)
|
|
{
|
|
// Default values
|
|
var response = new GetVATRateResponseDto
|
|
{
|
|
VatRate = 0.09, // 9% default
|
|
VatPercentage = 9,
|
|
IsEnabled = true
|
|
};
|
|
|
|
try
|
|
{
|
|
// دریافت تنظیمات VAT از CMS
|
|
// Scope: VAT = 4
|
|
var allConfigs = await _context.Configuration.GetAllConfigurationsAsync(
|
|
new GetAllConfigurationsRequest
|
|
{
|
|
Filter = new GetAllConfigurationsFilter
|
|
{
|
|
Scope = 4, // VAT scope
|
|
IsActive = true
|
|
}
|
|
},
|
|
cancellationToken: cancellationToken);
|
|
|
|
if (allConfigs?.Models != null)
|
|
{
|
|
foreach (var config in allConfigs.Models)
|
|
{
|
|
if (config.Key == "Shop.VAT" && !string.IsNullOrEmpty(config.Value))
|
|
{
|
|
if (double.TryParse(config.Value, out var rate))
|
|
{
|
|
response.VatRate = rate;
|
|
response.VatPercentage = (int)(rate * 100);
|
|
}
|
|
}
|
|
else if (config.Key == "IsEnabled" && !string.IsNullOrEmpty(config.Value))
|
|
{
|
|
response.IsEnabled = bool.TryParse(config.Value, out var enabled) && enabled;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// در صورت خطا، مقادیر پیشفرض برگردانده میشود
|
|
}
|
|
|
|
return response;
|
|
}
|
|
}
|