feat: add GetVATRate RPC and corresponding query handler for VAT rate retrieval
This commit is contained in:
+61
@@ -0,0 +1,61 @@
|
||||
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
|
||||
{
|
||||
// دریافت نرخ مالیات از تنظیمات سیستم
|
||||
// Scope: VAT = 4
|
||||
var rateConfig = await _context.Configuration.GetConfigurationByKeyAsync(
|
||||
new GetConfigurationByKeyRequest { Key = "VAT.Rate" },
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
if (rateConfig != null && !string.IsNullOrEmpty(rateConfig.Value))
|
||||
{
|
||||
if (double.TryParse(rateConfig.Value, out var rate))
|
||||
{
|
||||
response.VatRate = rate;
|
||||
response.VatPercentage = (int)(rate * 100);
|
||||
}
|
||||
}
|
||||
|
||||
// دریافت وضعیت فعال بودن مالیات
|
||||
var enabledConfig = await _context.Configuration.GetConfigurationByKeyAsync(
|
||||
new GetConfigurationByKeyRequest { Key = "VAT.IsEnabled" },
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
if (enabledConfig != null && !string.IsNullOrEmpty(enabledConfig.Value))
|
||||
{
|
||||
response.IsEnabled = bool.TryParse(enabledConfig.Value, out var enabled) && enabled;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// در صورت خطا، مقادیر پیشفرض برگردانده میشود
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user