2023-04-06 08:29:39 +03:00
|
|
|
|
#if !NET472
|
|
|
|
|
using System.Runtime.Versioning;
|
|
|
|
|
#endif
|
2021-11-11 16:33:40 +03:00
|
|
|
|
|
2023-06-08 07:47:14 +03:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
|
|
2023-04-06 08:29:39 +03:00
|
|
|
|
namespace RhSolutions.AddIn;
|
|
|
|
|
|
|
|
|
|
#if !NET472
|
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
|
|
|
#endif
|
2023-06-08 06:44:11 +03:00
|
|
|
|
public class RhSolutionsFunctions
|
2021-11-11 16:33:40 +03:00
|
|
|
|
{
|
2023-05-17 07:58:13 +03:00
|
|
|
|
[ExcelFunction(Description = "Поиск артикула в базе данных")]
|
|
|
|
|
public static object RHSOLUTIONS([ExcelArgument(Name = "СТРОКА", Description = "Ячейка с артикулом РЕХАУ или поисковый запрос в свободной форме")] string line)
|
2021-11-11 16:33:40 +03:00
|
|
|
|
{
|
2023-03-28 10:36:36 +03:00
|
|
|
|
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
2023-04-06 08:29:39 +03:00
|
|
|
|
|
2023-04-20 06:58:27 +03:00
|
|
|
|
ProductSku.TryParse(line, out var skus);
|
2023-04-06 08:29:39 +03:00
|
|
|
|
|
2023-06-08 06:44:11 +03:00
|
|
|
|
if (ExcelAsyncUtil.Run(nameof(RHSOLUTIONS), line, delegate
|
2022-12-19 09:13:14 +03:00
|
|
|
|
{
|
2023-03-28 10:36:36 +03:00
|
|
|
|
return databaseClient.GetProducts(line)
|
|
|
|
|
.GetAwaiter()
|
|
|
|
|
.GetResult();
|
2023-04-06 08:29:39 +03:00
|
|
|
|
}) is not IEnumerable<Product> requestResult)
|
2023-03-28 10:36:36 +03:00
|
|
|
|
{
|
2023-04-06 08:29:39 +03:00
|
|
|
|
if (skus.Any())
|
2022-12-19 09:13:14 +03:00
|
|
|
|
{
|
2023-03-28 10:36:36 +03:00
|
|
|
|
return $"{skus.First()} ...";
|
2022-12-19 09:13:14 +03:00
|
|
|
|
}
|
2023-03-27 15:52:28 +03:00
|
|
|
|
else
|
2022-12-19 09:13:14 +03:00
|
|
|
|
{
|
2023-03-28 10:36:36 +03:00
|
|
|
|
return "Загрузка...";
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-27 15:52:28 +03:00
|
|
|
|
|
2023-03-28 10:36:36 +03:00
|
|
|
|
else
|
|
|
|
|
{
|
2023-04-06 08:29:39 +03:00
|
|
|
|
if (!requestResult.Any() && !skus.Any())
|
2023-03-28 10:36:36 +03:00
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
2023-04-06 08:29:39 +03:00
|
|
|
|
else if (!requestResult.Any())
|
2023-03-28 10:36:36 +03:00
|
|
|
|
{
|
|
|
|
|
return $"{skus.First()}";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var firstProduct = requestResult.First();
|
|
|
|
|
return $"{firstProduct.ProductSku} {firstProduct.Name}";
|
2022-12-19 09:13:14 +03:00
|
|
|
|
}
|
2022-12-23 21:10:43 +03:00
|
|
|
|
}
|
2022-12-19 09:13:14 +03:00
|
|
|
|
}
|
2023-05-15 06:33:24 +03:00
|
|
|
|
|
2023-05-17 07:58:13 +03:00
|
|
|
|
[ExcelFunction(Description = "Поиск артикула в базе данных")]
|
|
|
|
|
public static object РЕХАУ([ExcelArgument(Name = "СТРОКА", Description = "Ячейка с артикулом РЕХАУ или поисковый запрос в свободной форме")] string line)
|
|
|
|
|
=> RHSOLUTIONS(line);
|
2023-05-15 06:33:24 +03:00
|
|
|
|
|
2023-05-17 07:58:13 +03:00
|
|
|
|
[ExcelFunction(Description = "Выделить артикул РЕХАУ из ячейки")]
|
|
|
|
|
public static object РЕХАУАРТИКУЛ([ExcelArgument(Name = "СТРОКА", Description = "Ячейка содержащая артикул РЕХАУ")] string line)
|
2023-05-12 10:18:00 +03:00
|
|
|
|
{
|
|
|
|
|
if (ProductSku.TryParse(line, out var skus))
|
|
|
|
|
{
|
|
|
|
|
return skus.First().Id;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-15 06:33:24 +03:00
|
|
|
|
|
2023-05-17 07:58:13 +03:00
|
|
|
|
[ExcelFunction(Description = "Поиск названия по артикулу РЕХАУ")]
|
|
|
|
|
public static object РЕХАУИМЯ([ExcelArgument(Name = "АРТИКУЛ", Description = "Ячейка содержащая артикул РЕХАУ")] string line)
|
2023-05-12 10:18:00 +03:00
|
|
|
|
{
|
|
|
|
|
if (!ProductSku.TryParse(line, out var skus))
|
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var article = skus.First().Id;
|
|
|
|
|
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
|
|
|
|
|
2023-06-08 06:44:11 +03:00
|
|
|
|
if (ExcelAsyncUtil.Run(nameof(РЕХАУИМЯ), line, delegate
|
2023-05-12 10:18:00 +03:00
|
|
|
|
{
|
|
|
|
|
return databaseClient.GetProducts(article)
|
|
|
|
|
.GetAwaiter()
|
|
|
|
|
.GetResult();
|
|
|
|
|
}) is not IEnumerable<Product> requestResult)
|
|
|
|
|
{
|
|
|
|
|
return "Загрузка...";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!requestResult.Any())
|
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var firstProduct = requestResult.First();
|
|
|
|
|
return firstProduct.Name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-15 06:33:24 +03:00
|
|
|
|
|
2023-05-17 07:58:13 +03:00
|
|
|
|
[ExcelFunction(Description = "Поиск цены артикула РЕХАУ в евро")]
|
|
|
|
|
public static object РЕХАУЦЕНА([ExcelArgument(Name = "АРТИКУЛ", Description = "Ячейка содержащая артикул РЕХАУ")] string line)
|
2023-05-12 10:18:00 +03:00
|
|
|
|
{
|
|
|
|
|
if (!ProductSku.TryParse(line, out var skus))
|
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var article = skus.First().Id;
|
|
|
|
|
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
|
|
|
|
|
2023-06-08 06:44:11 +03:00
|
|
|
|
if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНА), line, delegate
|
2023-05-12 10:18:00 +03:00
|
|
|
|
{
|
|
|
|
|
return databaseClient.GetProducts(article)
|
|
|
|
|
.GetAwaiter()
|
|
|
|
|
.GetResult();
|
|
|
|
|
}) is not IEnumerable<Product> requestResult)
|
|
|
|
|
{
|
|
|
|
|
return "Загрузка...";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!requestResult.Any())
|
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var firstProduct = requestResult.First();
|
2023-05-15 06:33:24 +03:00
|
|
|
|
return Math.Round(firstProduct.Price * 1.2m, 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 07:58:13 +03:00
|
|
|
|
[ExcelFunction(Description = "Поиск цены артикула РЕХАУ в рублях")]
|
|
|
|
|
public static object РЕХАУЦЕНАРУБ([ExcelArgument(Name = "АРТИКУЛ", Description = "Ячейка содержащая артикул РЕХАУ")] string line,
|
|
|
|
|
[ExcelArgument(Name = "ДАТА", Description = "Дата в формате Excel (необязательно)")] double dateField)
|
2023-05-15 06:33:24 +03:00
|
|
|
|
{
|
|
|
|
|
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>();
|
|
|
|
|
|
2023-06-08 06:44:11 +03:00
|
|
|
|
if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНАРУБ), line, delegate
|
2023-05-15 06:33:24 +03:00
|
|
|
|
{
|
|
|
|
|
var product = databaseClient.GetProducts(article)
|
|
|
|
|
.GetAwaiter()
|
|
|
|
|
.GetResult()
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
2023-05-27 08:58:09 +03:00
|
|
|
|
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
|
2023-05-15 06:33:24 +03:00
|
|
|
|
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);
|
2023-05-12 10:18:00 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-17 08:11:24 +03:00
|
|
|
|
|
|
|
|
|
[ExcelFunction(Description = "Получить курс евро по ЦБ")]
|
|
|
|
|
public static object КУРСЕВРО([ExcelArgument(Name = "ДАТА", Description = "Дата в формате Excel (необязательно)")] double dateField)
|
|
|
|
|
{
|
|
|
|
|
ICurrencyClient currencyClient = RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
|
2023-06-08 07:47:14 +03:00
|
|
|
|
IMemoryCache memoryCache = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IMemoryCache>();
|
|
|
|
|
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
|
2023-05-17 08:11:24 +03:00
|
|
|
|
|
2023-06-08 07:47:14 +03:00
|
|
|
|
if (!memoryCache.TryGetValue(date, out decimal exchangeRate))
|
2023-05-17 08:11:24 +03:00
|
|
|
|
{
|
2023-06-08 07:47:14 +03:00
|
|
|
|
var result = ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate
|
|
|
|
|
{
|
|
|
|
|
var requestResult = currencyClient.GetCurrencyCourse(date)
|
|
|
|
|
.GetAwaiter()
|
|
|
|
|
.GetResult();
|
2023-05-17 08:11:24 +03:00
|
|
|
|
|
2023-06-08 07:47:14 +03:00
|
|
|
|
return requestResult ?? -1m;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (result is not decimal)
|
2023-05-17 08:11:24 +03:00
|
|
|
|
{
|
2023-06-08 07:47:14 +03:00
|
|
|
|
return "Загрузка...";
|
2023-05-17 08:11:24 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-06-08 07:47:14 +03:00
|
|
|
|
exchangeRate = (decimal)result;
|
|
|
|
|
var cacheEntryOptions = new MemoryCacheEntryOptions()
|
|
|
|
|
.SetSlidingExpiration(TimeSpan.FromHours(1));
|
|
|
|
|
memoryCache.Set(date, exchangeRate, cacheEntryOptions);
|
2023-05-17 08:11:24 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-08 07:47:14 +03:00
|
|
|
|
|
|
|
|
|
return exchangeRate < 0 ? ExcelError.ExcelErrorNA : exchangeRate;
|
2023-05-17 08:11:24 +03:00
|
|
|
|
}
|
2021-11-11 16:33:40 +03:00
|
|
|
|
}
|