2024-05-19 16:22:16 +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-11-06 22:46:44 +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-11-06 22:46:44 +03:00
|
|
|
|
[ExcelFunction(Name = "РЕХАУ")]
|
|
|
|
|
public static object ProductSearch(object[,] values)
|
|
|
|
|
{
|
|
|
|
|
List<string> strings = new();
|
|
|
|
|
int rows = values.GetLength(0);
|
|
|
|
|
int columns = values.GetLength(1);
|
|
|
|
|
for (int row = 0; row < rows; row++)
|
|
|
|
|
{
|
|
|
|
|
for (int column = 0; column < columns; column++)
|
|
|
|
|
{
|
|
|
|
|
object value = values[row, column];
|
|
|
|
|
strings.Add(value.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string query = string.Join(" ", strings.ToArray());
|
|
|
|
|
var functionName = nameof(ProductSearch);
|
|
|
|
|
var parameters = new object[] { query };
|
|
|
|
|
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
|
|
|
|
|
{
|
|
|
|
|
return await databaseClient.GetProducts(query);
|
|
|
|
|
}) is not IEnumerable<Product> products)
|
|
|
|
|
{
|
|
|
|
|
return "Загрузка...";
|
|
|
|
|
}
|
|
|
|
|
else if (!products.Any())
|
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var product = products.First();
|
|
|
|
|
return $"{product.Id} {product.Name}";
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-15 06:33:24 +03:00
|
|
|
|
|
2024-11-06 22:46:44 +03:00
|
|
|
|
[ExcelFunction(Name = "РЕХАУАРТИКУЛ")]
|
|
|
|
|
public static object SkuSearch(string query)
|
|
|
|
|
{
|
|
|
|
|
if (ProductSku.TryParse(query, out var skus))
|
|
|
|
|
{
|
|
|
|
|
return skus.First().Id;
|
|
|
|
|
}
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
2023-05-15 06:33:24 +03:00
|
|
|
|
|
2024-11-06 22:46:44 +03:00
|
|
|
|
[ExcelFunction(Name = "РЕХАУИМЯ")]
|
|
|
|
|
public static object GetProductName(string query)
|
|
|
|
|
{
|
|
|
|
|
if (!ProductSku.TryParse(query, out var skus))
|
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
|
|
|
|
var article = skus.First().Id;
|
|
|
|
|
var functionName = nameof(GetProductName);
|
|
|
|
|
var parameters = new object[] { query };
|
|
|
|
|
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
|
|
|
|
|
{
|
|
|
|
|
return await databaseClient.GetProducts(article);
|
|
|
|
|
}) 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
|
|
|
|
|
2024-11-06 22:46:44 +03:00
|
|
|
|
[ExcelFunction(Name = "РЕХАУЦЕНА")]
|
|
|
|
|
public static object GetProductPrice(string query)
|
|
|
|
|
{
|
|
|
|
|
if (!ProductSku.TryParse(query, out var skus))
|
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
|
|
|
|
var article = skus.First().Id;
|
|
|
|
|
var functionName = nameof(GetProductPrice);
|
|
|
|
|
var parameters = new object[] { article };
|
2024-01-21 15:04:51 +03:00
|
|
|
|
|
2024-11-06 22:46:44 +03:00
|
|
|
|
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
|
|
|
|
|
{
|
|
|
|
|
return await databaseClient.GetProducts(article);
|
|
|
|
|
}) 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-15 06:33:24 +03:00
|
|
|
|
|
2024-11-06 22:46:44 +03:00
|
|
|
|
[ExcelFunction(Name = "РЕХАУЦЕНАРУБ")]
|
|
|
|
|
public static object GetProductPriceRub(string query, double dateField)
|
|
|
|
|
{
|
|
|
|
|
if (!ProductSku.TryParse(query, out var skus))
|
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
|
|
|
|
var article = skus.First().Id;
|
|
|
|
|
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
|
2024-01-21 15:04:51 +03:00
|
|
|
|
|
|
|
|
|
|
2024-11-06 22:46:44 +03:00
|
|
|
|
var functionName = nameof(GetProductPriceRub);
|
|
|
|
|
var parameters = new object[] { date };
|
2024-01-21 15:04:51 +03:00
|
|
|
|
|
2024-11-06 22:46:44 +03:00
|
|
|
|
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
|
|
|
|
|
{
|
|
|
|
|
var requestResult = await currencyClient.GetExchangeRate(date);
|
|
|
|
|
return requestResult ?? -1m;
|
|
|
|
|
}) is not decimal exchangeRate)
|
|
|
|
|
{
|
|
|
|
|
return "Загрузка...";
|
|
|
|
|
}
|
2024-01-21 15:04:51 +03:00
|
|
|
|
|
2024-11-06 22:46:44 +03:00
|
|
|
|
parameters = new object[] { query };
|
|
|
|
|
if (ExcelAsyncUtil.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)
|
|
|
|
|
{
|
|
|
|
|
return ExcelError.ExcelErrorNA;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return Math.Round(requestResult, 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-11 16:33:40 +03:00
|
|
|
|
}
|