RhSolutions-AddIn/RhSolutions.AddIn/Models/SourcePriceList.cs

110 lines
3.4 KiB
C#
Raw Normal View History

2023-03-28 10:36:36 +03:00
using System.IO;
namespace RhSolutions.Models;
internal class SourcePriceList : PriceListBase
2022-01-27 09:59:33 +03:00
{
2023-03-28 10:36:36 +03:00
public Dictionary<Product, double> PositionAmount { get; private set; }
2022-01-27 09:59:33 +03:00
2023-03-28 10:36:36 +03:00
public SourcePriceList(Workbook workbook)
{
if (workbook == null)
2022-01-27 09:59:33 +03:00
{
2023-03-28 10:36:36 +03:00
throw new ArgumentException($"Нет рабочего файла");
}
2022-01-27 09:59:33 +03:00
2023-03-28 10:36:36 +03:00
Sheet = workbook.ActiveSheet;
Name = Path.GetFileNameWithoutExtension(workbook.FullName);
2022-01-27 09:59:33 +03:00
2023-03-28 10:36:36 +03:00
Range[] cells = new[]
{
AmountCell = Sheet.Cells.Find(PriceListHeaders.Amount),
SkuCell = Sheet.Cells.Find(PriceListHeaders.Sku),
GroupCell = Sheet.Cells.Find(PriceListHeaders.Group),
NameCell = Sheet.Cells.Find(PriceListHeaders.Name)
};
2022-01-27 09:59:33 +03:00
2023-03-28 10:36:36 +03:00
if (cells.Any(x => x == null))
{
throw new ArgumentException($"Файл {Name} не распознан");
2022-01-27 09:59:33 +03:00
}
2023-03-28 10:36:36 +03:00
CreatePositionsDict();
}
public static List<SourcePriceList> GetSourceLists(string[] files)
{
var ExcelApp = (Application)ExcelDnaUtil.Application;
ProgressBar bar = new ProgressBar("Открываю исходные файлы...", files.Length);
2022-01-28 16:19:39 +03:00
2023-03-28 10:36:36 +03:00
List<SourcePriceList> sourceFiles = new List<SourcePriceList>();
2022-01-28 16:19:39 +03:00
2023-03-28 10:36:36 +03:00
foreach (string file in files)
{
ExcelApp.ScreenUpdating = false;
Workbook wb = ExcelApp.Workbooks.Open(file);
try
2022-01-28 16:19:39 +03:00
{
2023-03-28 10:36:36 +03:00
SourcePriceList priceList = new SourcePriceList(wb);
sourceFiles.Add(priceList);
wb.Close();
bar.Update();
2022-01-28 16:19:39 +03:00
}
2023-03-28 10:36:36 +03:00
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show
(ex.Message,
"Ошибка открытия исходного прайс-листа",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
wb.Close();
bar.Update();
}
ExcelApp.ScreenUpdating = true;
2022-01-28 16:19:39 +03:00
}
2023-03-28 10:36:36 +03:00
return sourceFiles;
}
private void CreatePositionsDict()
{
PositionAmount = new Dictionary<Product, double>();
for (int row = AmountCell.Row + 1; row <= Sheet.Cells[Sheet.Rows.Count, AmountCell.Column].End[XlDirection.xlUp].Row; row++)
2022-01-27 09:59:33 +03:00
{
2023-03-28 10:36:36 +03:00
double? amount = Sheet.Cells[row, AmountCell.Column].Value2 as double?;
2022-01-27 09:59:33 +03:00
2023-03-28 10:36:36 +03:00
if (amount != null && amount.Value != 0)
2022-01-27 09:59:33 +03:00
{
2023-03-28 10:36:36 +03:00
object group = Sheet.Cells[row, GroupCell.Column].Value2;
object name = Sheet.Cells[row, NameCell.Column].Value2;
object sku = Sheet.Cells[row, SkuCell.Column].Value2;
if (group == null || name == null || sku == null)
continue;
if (!Sku.TryParse(sku.ToString(), out _))
continue;
Product p = new Product
{
ProductSku = sku.ToString(),
ProductLine = group.ToString(),
Name = name.ToString()
};
if (PositionAmount.ContainsKey(p))
{
PositionAmount[p] += amount.Value;
}
2022-01-27 09:59:33 +03:00
2023-03-28 10:36:36 +03:00
else
2022-01-27 09:59:33 +03:00
{
2023-03-28 10:36:36 +03:00
PositionAmount.Add(p, amount.Value);
2022-01-27 09:59:33 +03:00
}
}
}
}
}