2024-01-21 15:04:51 +03:00
|
|
|
|
using ExcelDna.Registration.Utils;
|
|
|
|
|
|
|
|
|
|
namespace RhSolutions.AddIn;
|
2021-11-11 16:33:40 +03:00
|
|
|
|
|
2024-01-18 23:16:38 +03:00
|
|
|
|
public static class RhSolutionsFunctions
|
2021-11-11 16:33:40 +03:00
|
|
|
|
{
|
2024-01-21 15:04:51 +03:00
|
|
|
|
private static readonly IDatabaseClient databaseClient =
|
|
|
|
|
RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
|
|
|
|
private static readonly ICurrencyClient currencyClient =
|
|
|
|
|
RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
|
2024-01-18 23:16:38 +03:00
|
|
|
|
|
2024-01-18 23:23:26 +03:00
|
|
|
|
[ExcelFunction]
|
|
|
|
|
public static object РЕХАУ(string query)
|
2021-11-11 16:33:40 +03:00
|
|
|
|
{
|
2024-01-21 15:04:51 +03:00
|
|
|
|
var functionName = nameof(РЕХАУ);
|
|
|
|
|
var parameters = new object[] { query };
|
|
|
|
|
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
|
2022-12-19 09:13:14 +03:00
|
|
|
|
{
|
2024-01-21 15:04:51 +03:00
|
|
|
|
return await databaseClient.GetProducts(query);
|
|
|
|
|
}) is not IEnumerable<Product> products)
|
2023-03-28 10:36:36 +03:00
|
|
|
|
{
|
2024-01-21 15:04:51 +03:00
|
|
|
|
return "Загрузка...";
|
|
|
|
|
}
|
|
|
|
|
else if (!products.Any())
|
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
2023-03-28 10:36:36 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-01-21 15:04:51 +03:00
|
|
|
|
var product = products.First();
|
|
|
|
|
return $"{product.Id} {product.Name}";
|
2022-12-23 21:10:43 +03:00
|
|
|
|
}
|
2022-12-19 09:13:14 +03:00
|
|
|
|
}
|
2023-05-15 06:33:24 +03:00
|
|
|
|
|
2024-01-18 23:23:26 +03:00
|
|
|
|
[ExcelFunction]
|
|
|
|
|
public static object РЕХАУАРТИКУЛ(string query)
|
2023-05-12 10:18:00 +03:00
|
|
|
|
{
|
2024-01-18 23:16:38 +03:00
|
|
|
|
if (ProductSku.TryParse(query, out var skus))
|
2023-05-12 10:18:00 +03:00
|
|
|
|
{
|
|
|
|
|
return skus.First().Id;
|
|
|
|
|
}
|
2024-01-21 15:04:51 +03:00
|
|
|
|
return ExcelError.ExcelErrorNA;
|
2023-05-12 10:18:00 +03:00
|
|
|
|
}
|
2023-05-15 06:33:24 +03:00
|
|
|
|
|
2024-01-18 23:23:26 +03:00
|
|
|
|
[ExcelFunction]
|
|
|
|
|
public static object РЕХАУИМЯ(string query)
|
2023-05-12 10:18:00 +03:00
|
|
|
|
{
|
2024-01-18 23:16:38 +03:00
|
|
|
|
if (!ProductSku.TryParse(query, out var skus))
|
2023-05-12 10:18:00 +03:00
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
2024-01-21 15:04:51 +03:00
|
|
|
|
var article = skus.First().Id;
|
|
|
|
|
var functionName = nameof(РЕХАУИМЯ);
|
|
|
|
|
var parameters = new object[] { query };
|
|
|
|
|
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
|
|
|
|
|
{
|
|
|
|
|
return await databaseClient.GetProducts(article);
|
|
|
|
|
}) is not IEnumerable<Product> requestResult)
|
|
|
|
|
{
|
|
|
|
|
return "Загрузка...";
|
|
|
|
|
}
|
|
|
|
|
else if (!requestResult.Any())
|
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
2023-05-12 10:18:00 +03:00
|
|
|
|
else
|
|
|
|
|
{
|
2024-01-21 15:04:51 +03:00
|
|
|
|
var firstProduct = requestResult.First();
|
|
|
|
|
return firstProduct.Name;
|
2023-05-12 10:18:00 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-15 06:33:24 +03:00
|
|
|
|
|
2024-01-18 23:23:26 +03:00
|
|
|
|
[ExcelFunction]
|
|
|
|
|
public static object РЕХАУЦЕНА(string query)
|
2023-05-12 10:18:00 +03:00
|
|
|
|
{
|
2024-01-18 23:16:38 +03:00
|
|
|
|
if (!ProductSku.TryParse(query, out var skus))
|
2023-05-12 10:18:00 +03:00
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
2024-01-21 15:04:51 +03:00
|
|
|
|
var article = skus.First().Id;
|
|
|
|
|
var functionName = nameof(РЕХАУЦЕНА);
|
|
|
|
|
var parameters = new object[] { article };
|
|
|
|
|
|
|
|
|
|
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
|
|
|
|
|
{
|
|
|
|
|
return await databaseClient.GetProducts(article);
|
|
|
|
|
}) is not IEnumerable<Product> requestResult)
|
|
|
|
|
{
|
|
|
|
|
return "Загрузка...";
|
|
|
|
|
}
|
|
|
|
|
else if (!requestResult.Any())
|
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
2023-05-12 10:18:00 +03:00
|
|
|
|
else
|
|
|
|
|
{
|
2024-01-21 15:04:51 +03:00
|
|
|
|
var firstProduct = requestResult.First();
|
|
|
|
|
return Math.Round(firstProduct.Price * 1.2m, 2);
|
2023-05-15 06:33:24 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 23:23:26 +03:00
|
|
|
|
[ExcelFunction]
|
2024-01-21 15:04:51 +03:00
|
|
|
|
public static object РЕХАУЦЕНАРУБ(string query, double dateField)
|
2023-05-15 06:33:24 +03:00
|
|
|
|
{
|
2024-01-21 15:04:51 +03:00
|
|
|
|
if (!ProductSku.TryParse(query, out var skus))
|
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
|
|
|
|
var article = skus.First().Id;
|
|
|
|
|
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var functionName = nameof(РЕХАУЦЕНАРУБ);
|
|
|
|
|
var parameters = new object[] { date };
|
|
|
|
|
|
|
|
|
|
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
|
|
|
|
|
{
|
|
|
|
|
var requestResult = await currencyClient.GetExchangeRate(date);
|
|
|
|
|
return requestResult ?? -1m;
|
|
|
|
|
}) is not decimal exchangeRate)
|
|
|
|
|
{
|
|
|
|
|
return "Загрузка...";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parameters = new object[] { query };
|
|
|
|
|
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
|
|
|
|
|
{
|
|
|
|
|
var products = await databaseClient.GetProducts(article);
|
|
|
|
|
var product = products.FirstOrDefault();
|
|
|
|
|
return product == null ? -1m :
|
|
|
|
|
product.Price * (decimal)exchangeRate * 1.2m;
|
|
|
|
|
}) is not decimal requestResult)
|
|
|
|
|
{
|
|
|
|
|
return "Загрузка...";
|
|
|
|
|
}
|
|
|
|
|
else if (requestResult < 0 || exchangeRate < 0)
|
2023-05-15 06:33:24 +03:00
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-01-21 15:04:51 +03:00
|
|
|
|
return Math.Round(requestResult, 2);
|
2023-05-12 10:18:00 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-17 08:11:24 +03:00
|
|
|
|
|
2024-01-18 23:23:26 +03:00
|
|
|
|
[ExcelFunction]
|
|
|
|
|
public static object КУРСЕВРО(double dateField)
|
2023-05-17 08:11:24 +03:00
|
|
|
|
{
|
2023-06-08 07:47:14 +03:00
|
|
|
|
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
|
2024-01-21 15:04:51 +03:00
|
|
|
|
var functionName = nameof(КУРСЕВРО);
|
|
|
|
|
var parameters = new object[] { date };
|
2023-05-17 08:11:24 +03:00
|
|
|
|
|
2024-01-21 15:04:51 +03:00
|
|
|
|
var exchangeRate = AsyncTaskUtil.RunTask(functionName, parameters, async () =>
|
2023-05-17 08:11:24 +03:00
|
|
|
|
{
|
2024-01-21 15:04:51 +03:00
|
|
|
|
var requestResult = await currencyClient.GetExchangeRate(date);
|
2024-01-18 23:16:38 +03:00
|
|
|
|
return requestResult ?? -1m;
|
|
|
|
|
});
|
2023-06-08 07:47:14 +03:00
|
|
|
|
|
2024-01-18 23:16:38 +03:00
|
|
|
|
if (exchangeRate is not decimal)
|
|
|
|
|
{
|
|
|
|
|
return "Загрузка...";
|
2023-05-17 08:11:24 +03:00
|
|
|
|
}
|
2023-06-08 07:47:14 +03:00
|
|
|
|
|
2024-01-18 23:16:38 +03:00
|
|
|
|
return (decimal)exchangeRate < 0 ? ExcelError.ExcelErrorNA : exchangeRate;
|
2023-05-17 08:11:24 +03:00
|
|
|
|
}
|
2021-11-11 16:33:40 +03:00
|
|
|
|
}
|