RhSolutions-AddIn/src/PriceListTools/SourcePriceList.cs

112 lines
3.7 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.Linq;
2022-02-02 10:23:50 +03:00
using RehauSku.Interface;
2022-01-27 09:59:33 +03:00
namespace RehauSku.PriceListTools
{
2022-02-02 18:05:49 +03:00
internal class SourcePriceList : AbstractPriceList
2022-01-27 09:59:33 +03:00
{
2022-01-28 11:41:35 +03:00
public Dictionary<Position, 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;
2022-01-27 10:22:30 +03:00
Name = workbook.Name;
2022-01-27 09:59:33 +03:00
Range[] cells = new[]
{
amountCell = Sheet.Cells.Find(amountHeader),
skuCell = Sheet.Cells.Find(skuHeader),
groupCell = Sheet.Cells.Find(groupHeader),
nameCell = Sheet.Cells.Find(nameHeader)
};
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-01-28 11:41:35 +03:00
PositionAmount = new Dictionary<Position, double>();
2022-01-27 09:59:33 +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
{
2022-01-28 15:59:47 +03:00
object amount = Sheet.Cells[row, amountCell.Column].Value2;
2022-01-27 09:59:33 +03:00
if (amount != null && (double)amount != 0)
{
2022-01-28 15:59:47 +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-02-04 09:17:12 +03:00
if (group == null || name == null || sku == null)
continue;
if (!sku.ToString().IsRehauSku())
continue;
2022-01-28 11:41:35 +03:00
Position p = new Position(group.ToString(), sku.ToString(), name.ToString());
2022-01-27 09:59:33 +03:00
2022-01-28 11:41:35 +03:00
if (PositionAmount.ContainsKey(p))
{
PositionAmount[p] += (double)amount;
}
2022-01-27 09:59:33 +03:00
else
2022-01-28 11:41:35 +03:00
{
PositionAmount.Add(p, (double)amount);
}
2022-01-27 09:59:33 +03:00
}
}
}
}
}