Add =РЕХАУЦЕНАРУБ() function
This commit is contained in:
parent
53f82b03fb
commit
d4d98ec850
@ -22,6 +22,7 @@ public sealed class RhSolutionsAddIn : IExcelAddIn
|
|||||||
.AddSingleton((Application)ExcelDnaUtil.Application)
|
.AddSingleton((Application)ExcelDnaUtil.Application)
|
||||||
.AddSingleton<IAddInConfiguration, AddInConfiguration>()
|
.AddSingleton<IAddInConfiguration, AddInConfiguration>()
|
||||||
.AddSingleton<IDatabaseClient, DatabaseClient>()
|
.AddSingleton<IDatabaseClient, DatabaseClient>()
|
||||||
|
.AddTransient<ICurrencyClient, CurrencyClient>()
|
||||||
.AddTransient<IFileDialog, FileDialog>()
|
.AddTransient<IFileDialog, FileDialog>()
|
||||||
.AddTransient<IReader, ExcelReader>();
|
.AddTransient<IReader, ExcelReader>();
|
||||||
|
|
||||||
|
@ -39,12 +39,10 @@ public class RhSolutionsFunction
|
|||||||
{
|
{
|
||||||
return ExcelError.ExcelErrorNA;
|
return ExcelError.ExcelErrorNA;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (!requestResult.Any())
|
else if (!requestResult.Any())
|
||||||
{
|
{
|
||||||
return $"{skus.First()}";
|
return $"{skus.First()}";
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var firstProduct = requestResult.First();
|
var firstProduct = requestResult.First();
|
||||||
@ -52,8 +50,10 @@ public class RhSolutionsFunction
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ExcelFunction]
|
[ExcelFunction]
|
||||||
public static object РЕХАУ(string line) => RHSOLUTIONS(line);
|
public static object РЕХАУ(string line) => RHSOLUTIONS(line);
|
||||||
|
|
||||||
[ExcelFunction]
|
[ExcelFunction]
|
||||||
public static object РЕХАУАРТИКУЛ(string line)
|
public static object РЕХАУАРТИКУЛ(string line)
|
||||||
{
|
{
|
||||||
@ -66,6 +66,7 @@ public class RhSolutionsFunction
|
|||||||
return ExcelError.ExcelErrorNA;
|
return ExcelError.ExcelErrorNA;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ExcelFunction]
|
[ExcelFunction]
|
||||||
public static object РЕХАУИМЯ(string line)
|
public static object РЕХАУИМЯ(string line)
|
||||||
{
|
{
|
||||||
@ -93,7 +94,6 @@ public class RhSolutionsFunction
|
|||||||
{
|
{
|
||||||
return ExcelError.ExcelErrorNA;
|
return ExcelError.ExcelErrorNA;
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var firstProduct = requestResult.First();
|
var firstProduct = requestResult.First();
|
||||||
@ -102,6 +102,7 @@ public class RhSolutionsFunction
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ExcelFunction]
|
[ExcelFunction]
|
||||||
public static object РЕХАУЦЕНА(string line)
|
public static object РЕХАУЦЕНА(string line)
|
||||||
{
|
{
|
||||||
@ -129,11 +130,55 @@ public class RhSolutionsFunction
|
|||||||
{
|
{
|
||||||
return ExcelError.ExcelErrorNA;
|
return ExcelError.ExcelErrorNA;
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var firstProduct = requestResult.First();
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
41
RhSolutions.AddIn/Services/CurrencyClient.cs
Normal file
41
RhSolutions.AddIn/Services/CurrencyClient.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
9
RhSolutions.AddIn/Services/ICurrencyClient.cs
Normal file
9
RhSolutions.AddIn/Services/ICurrencyClient.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace RhSolutions.Services;
|
||||||
|
|
||||||
|
public interface ICurrencyClient
|
||||||
|
{
|
||||||
|
public Task<decimal?> GetCurrencyCourse(DateTime date);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user