RhSolutions-AddIn/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs
2023-05-15 06:33:24 +03:00

186 lines
5.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#if !NET472
using System.Runtime.Versioning;
#endif
namespace RhSolutions.AddIn;
#if !NET472
[SupportedOSPlatform("windows")]
#endif
public class RhSolutionsFunction
{
[ExcelFunction(Description = "Распознать артикул и попробовать найти его в прайс-листе")]
public static object RHSOLUTIONS([ExcelArgument(Name = "\"Строка с названием материала\"")] string line)
{
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
ProductSku.TryParse(line, out var skus);
if (ExcelAsyncUtil.Run("Database request", line, delegate
{
return databaseClient.GetProducts(line)
.GetAwaiter()
.GetResult();
}) is not IEnumerable<Product> requestResult)
{
if (skus.Any())
{
return $"{skus.First()} ...";
}
else
{
return "Загрузка...";
}
}
else
{
if (!requestResult.Any() && !skus.Any())
{
return ExcelError.ExcelErrorNA;
}
else if (!requestResult.Any())
{
return $"{skus.First()}";
}
else
{
var firstProduct = requestResult.First();
return $"{firstProduct.ProductSku} {firstProduct.Name}";
}
}
}
[ExcelFunction]
public static object РЕХАУ(string line) => RHSOLUTIONS(line);
[ExcelFunction]
public static object РЕХАУАРТИКУЛ(string line)
{
if (ProductSku.TryParse(line, out var skus))
{
return skus.First().Id;
}
else
{
return ExcelError.ExcelErrorNA;
}
}
[ExcelFunction]
public static object РЕХАУИМЯ(string line)
{
if (!ProductSku.TryParse(line, out var skus))
{
return ExcelError.ExcelErrorNA;
}
else
{
var article = skus.First().Id;
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
if (ExcelAsyncUtil.Run("Database request", line, 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;
}
}
}
}
[ExcelFunction]
public static object РЕХАУЦЕНА(string line)
{
if (!ProductSku.TryParse(line, out var skus))
{
return ExcelError.ExcelErrorNA;
}
else
{
var article = skus.First().Id;
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
if (ExcelAsyncUtil.Run("Database request", line, 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);
}
}
}
}
[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);
}
}
}
}
}