Add =РЕХАУЦЕНАРУБ() function

This commit is contained in:
Sergey Chebotar 2023-05-15 06:33:24 +03:00
parent 53f82b03fb
commit d4d98ec850
4 changed files with 101 additions and 5 deletions

View File

@ -22,6 +22,7 @@ public sealed class RhSolutionsAddIn : IExcelAddIn
.AddSingleton((Application)ExcelDnaUtil.Application)
.AddSingleton<IAddInConfiguration, AddInConfiguration>()
.AddSingleton<IDatabaseClient, DatabaseClient>()
.AddTransient<ICurrencyClient, CurrencyClient>()
.AddTransient<IFileDialog, FileDialog>()
.AddTransient<IReader, ExcelReader>();

View File

@ -39,12 +39,10 @@ public class RhSolutionsFunction
{
return ExcelError.ExcelErrorNA;
}
else if (!requestResult.Any())
{
return $"{skus.First()}";
}
else
{
var firstProduct = requestResult.First();
@ -52,8 +50,10 @@ public class RhSolutionsFunction
}
}
}
[ExcelFunction]
public static object РЕХАУ(string line) => RHSOLUTIONS(line);
[ExcelFunction]
public static object РЕХАУАРТИКУЛ(string line)
{
@ -66,6 +66,7 @@ public class RhSolutionsFunction
return ExcelError.ExcelErrorNA;
}
}
[ExcelFunction]
public static object РЕХАУИМЯ(string line)
{
@ -93,7 +94,6 @@ public class RhSolutionsFunction
{
return ExcelError.ExcelErrorNA;
}
else
{
var firstProduct = requestResult.First();
@ -102,6 +102,7 @@ public class RhSolutionsFunction
}
}
}
[ExcelFunction]
public static object РЕХАУЦЕНА(string line)
{
@ -129,11 +130,55 @@ public class RhSolutionsFunction
{
return ExcelError.ExcelErrorNA;
}
else
{
var firstProduct = requestResult.First();
return string.Format("{0:N2} €", firstProduct.Price * 1.2m);
return Math.Round(firstProduct.Price * 1.2m, 2);
}
}
}
}
[ExcelFunction]
public static object РЕХАУЦЕНАРУБ(string line, double dateField)
{
if (!ProductSku.TryParse(line, out var skus))
{
return ExcelError.ExcelErrorNA;
}
else
{
var article = skus.First().Id;
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
ICurrencyClient currencyClient = RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
if (ExcelAsyncUtil.Run("Database request", line, delegate
{
var product = databaseClient.GetProducts(article)
.GetAwaiter()
.GetResult()
.FirstOrDefault();
DateTime date = dateField == 0 ? DateTime.Now : DateTime.FromOADate(dateField);
var exchangeRate = currencyClient.GetCurrencyCourse(date)
.GetAwaiter()
.GetResult();
return product == null ? -1m :
product.Price * exchangeRate * 1.2m;
}) is not decimal requestResult)
{
return "Загрузка...";
}
else
{
if (requestResult < 0)
{
return ExcelError.ExcelErrorNA;
}
else
{
return Math.Round(requestResult, 2);
}
}
}

View File

@ -0,0 +1,41 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace RhSolutions.Services;
public class CurrencyClient : ICurrencyClient
{
private readonly HttpClient _httpClient;
public HttpStatusCode StatusCode { get; private set; }
public CurrencyClient(IServiceProvider serviceProvider)
{
_httpClient = serviceProvider.GetRequiredService<HttpClient>();
}
public async Task<decimal?> GetCurrencyCourse(DateTime date)
{
string request = $"https://www.cbr.ru/scripts/XML_daily.asp?date_req={date.Date.ToString("dd/MM/yyyy")}";
var response = await _httpClient.GetAsync(request);
try
{
response.EnsureSuccessStatusCode();
string xml = await response.Content.ReadAsStringAsync();
XElement valCourses = XElement.Parse(xml);
var course = decimal.Parse(valCourses.Elements("Valute")
.Where(e => e.Element("Name").Value == "Евро")
.FirstOrDefault()
.Element("Value").Value);
return course;
}
catch
{
StatusCode = response.StatusCode;
}
return null;
}
}

View File

@ -0,0 +1,9 @@
using Newtonsoft.Json;
using System.Threading.Tasks;
namespace RhSolutions.Services;
public interface ICurrencyClient
{
public Task<decimal?> GetCurrencyCourse(DateTime date);
}