RhSolutions-AddIn/RhSolutions.AddIn/AddIn/RhSolutionsFunctions.cs

203 lines
5.7 KiB
C#
Raw Normal View History

2024-01-18 23:16:38 +03:00
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-18 23:16:38 +03:00
private static IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
private static ICurrencyClient currencyClient = RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
2024-01-18 23:23:26 +03:00
[ExcelFunction]
public static object РЕХАУ(string query)
2021-11-11 16:33:40 +03:00
{
2024-01-18 23:16:38 +03:00
ProductSku.TryParse(query, out var skus);
2023-04-06 08:29:39 +03:00
2024-01-18 23:16:38 +03:00
if (ExcelAsyncUtil.Run(nameof(РЕХАУ), query, delegate
2022-12-19 09:13:14 +03:00
{
2024-01-18 23:16:38 +03:00
return databaseClient.GetProducts(query)
2023-03-28 10:36:36 +03:00
.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-19 09:13:14 +03:00
}
2024-01-18 23:23:26 +03:00
[ExcelFunction]
public static object РЕХАУАРТИКУЛ(string query)
{
2024-01-18 23:16:38 +03:00
if (ProductSku.TryParse(query, out var skus))
{
return skus.First().Id;
}
else
{
return ExcelError.ExcelErrorNA;
}
}
2024-01-18 23:23:26 +03:00
[ExcelFunction]
public static object РЕХАУИМЯ(string query)
{
2024-01-18 23:16:38 +03:00
if (!ProductSku.TryParse(query, out var skus))
{
return ExcelError.ExcelErrorNA;
}
else
{
var article = skus.First().Id;
2024-01-18 23:16:38 +03:00
if (ExcelAsyncUtil.Run(nameof(РЕХАУИМЯ), query, delegate
{
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;
}
}
}
}
2024-01-18 23:23:26 +03:00
[ExcelFunction]
public static object РЕХАУЦЕНА(string query)
{
2024-01-18 23:16:38 +03:00
if (!ProductSku.TryParse(query, out var skus))
{
return ExcelError.ExcelErrorNA;
}
else
{
var article = skus.First().Id;
2024-01-18 23:16:38 +03:00
if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНА), query, delegate
{
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 Math.Round(firstProduct.Price * 1.2m, 2);
}
}
}
}
2024-01-18 23:23:26 +03:00
[ExcelFunction]
public static object РЕХАУЦЕНАРУБ(string line, double dateField)
{
if (!ProductSku.TryParse(line, out var skus))
{
return ExcelError.ExcelErrorNA;
}
else
{
2023-06-09 14:49:20 +03:00
var article = skus.First().Id;
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
2024-01-18 23:16:38 +03:00
if (ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate
2023-06-09 14:49:20 +03:00
{
2024-01-18 23:16:38 +03:00
var requestResult = currencyClient.GetExchangeRate(date)
.GetAwaiter()
.GetResult();
2023-06-09 14:49:20 +03:00
2024-01-18 23:16:38 +03:00
return requestResult ?? -1m;
}) is not decimal exchangeRate)
{
return "Загрузка...";
2023-06-09 14:49:20 +03:00
}
2024-01-18 23:16:38 +03:00
else if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНАРУБ), line, delegate
{
var product = databaseClient.GetProducts(article)
.GetAwaiter()
.GetResult()
.FirstOrDefault();
return product == null ? -1m :
2024-01-18 23:16:38 +03:00
product.Price * (decimal)exchangeRate * 1.2m;
}) is not decimal requestResult)
{
return "Загрузка...";
}
else
{
if (requestResult < 0)
{
return ExcelError.ExcelErrorNA;
}
else
{
return Math.Round(requestResult, 2);
}
}
}
}
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
{
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
2023-05-17 08:11:24 +03:00
2024-01-18 23:16:38 +03:00
var exchangeRate = ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate
2023-05-17 08:11:24 +03:00
{
2024-01-18 23:16:38 +03:00
var requestResult = currencyClient.GetExchangeRate(date)
.GetAwaiter()
.GetResult();
2023-05-17 08:11:24 +03:00
2024-01-18 23:16:38 +03:00
return requestResult ?? -1m;
});
2024-01-18 23:16:38 +03:00
if (exchangeRate is not decimal)
{
return "Загрузка...";
2023-05-17 08:11:24 +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
}