RhSolutions-AddIn/src/Models/SourcePriceList.cs

116 lines
3.9 KiB
C#
Raw Normal View History

2022-01-28 16:19:39 +03:00
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
2022-01-27 09:59:33 +03:00
using System;
2022-01-27 10:22:30 +03:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
2022-01-27 09:59:33 +03:00
2022-12-20 12:27:47 +03:00
namespace RhSolutions.Models
2022-01-27 09:59:33 +03:00
{
2022-12-20 11:53:55 +03:00
internal class SourcePriceList : PriceListBase
2022-01-27 09:59:33 +03:00
{
2022-12-20 08:30:58 +03:00
public Dictionary<Product, double> PositionAmount { get; private set; }
2022-01-27 09:59:33 +03:00
2022-02-02 18:05:49 +03:00
public SourcePriceList(Workbook workbook)
2022-01-27 09:59:33 +03:00
{
2022-01-28 12:44:37 +03:00
if (workbook == null)
{
throw new ArgumentException($"Нет рабочего файла");
}
2022-01-27 09:59:33 +03:00
Sheet = workbook.ActiveSheet;
Name = Path.GetFileNameWithoutExtension(workbook.FullName);
2022-01-27 09:59:33 +03:00
Range[] cells = new[]
{
2022-02-12 16:53:34 +03:00
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
if (cells.Any(x => x == null))
2022-01-27 09:59:33 +03:00
{
2022-01-27 17:34:03 +03:00
throw new ArgumentException($"Файл {Name} не распознан");
2022-01-27 09:59:33 +03:00
}
2022-01-28 11:41:35 +03:00
CreatePositionsDict();
2022-01-27 09:59:33 +03:00
}
2022-02-02 18:05:49 +03:00
public static List<SourcePriceList> GetSourceLists(string[] files)
2022-01-28 16:19:39 +03:00
{
var ExcelApp = (Application)ExcelDnaUtil.Application;
2022-02-03 21:56:14 +03:00
ProgressBar bar = new ProgressBar("Открываю исходные файлы...", files.Length);
2022-01-28 16:19:39 +03:00
2022-02-02 18:05:49 +03:00
List<SourcePriceList> sourceFiles = new List<SourcePriceList>();
2022-01-28 16:19:39 +03:00
foreach (string file in files)
{
ExcelApp.ScreenUpdating = false;
2022-01-28 16:19:39 +03:00
Workbook wb = ExcelApp.Workbooks.Open(file);
try
{
2022-02-02 18:05:49 +03:00
SourcePriceList priceList = new SourcePriceList(wb);
2022-01-28 16:19:39 +03:00
sourceFiles.Add(priceList);
wb.Close();
2022-02-04 09:17:12 +03:00
bar.Update();
2022-01-28 16:19:39 +03:00
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show
(ex.Message,
"Ошибка открытия исходного прайс-листа",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
wb.Close();
2022-02-04 09:17:12 +03:00
bar.Update();
2022-01-28 16:19:39 +03:00
}
ExcelApp.ScreenUpdating = true;
2022-01-28 16:19:39 +03:00
}
return sourceFiles;
}
2022-01-28 11:41:35 +03:00
private void CreatePositionsDict()
2022-01-27 09:59:33 +03:00
{
2022-12-20 08:30:58 +03:00
PositionAmount = new Dictionary<Product, double>();
2022-01-27 09:59:33 +03:00
2022-02-12 16:53:34 +03:00
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
{
double? amount = Sheet.Cells[row, AmountCell.Column].Value2 as double?;
2022-01-27 09:59:33 +03:00
if (amount != null && amount.Value != 0)
2022-01-27 09:59:33 +03:00
{
2022-02-12 16:53:34 +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;
2022-01-28 15:59:47 +03:00
2022-02-04 09:17:12 +03:00
if (group == null || name == null || sku == null)
continue;
2022-12-23 21:20:41 +03:00
if (!Sku.TryParse(sku.ToString(), out _))
2022-02-04 09:17:12 +03:00
continue;
2022-12-20 08:30:58 +03:00
Product p = new Product
{
ProductSku = sku.ToString(),
ProductLine = group.ToString(),
Name = name.ToString()
};
2022-01-27 09:59:33 +03:00
2022-01-28 11:41:35 +03:00
if (PositionAmount.ContainsKey(p))
{
PositionAmount[p] += amount.Value;
2022-01-28 11:41:35 +03:00
}
2022-01-27 09:59:33 +03:00
else
2022-01-28 11:41:35 +03:00
{
PositionAmount.Add(p, amount.Value);
2022-01-28 11:41:35 +03:00
}
2022-01-27 09:59:33 +03:00
}
}
}
}
}