diff options
Diffstat (limited to 'src/PriceListTools')
-rw-r--r-- | src/PriceListTools/CombineTool.cs | 56 | ||||
-rw-r--r-- | src/PriceListTools/ConvertTool.cs | 30 | ||||
-rw-r--r-- | src/PriceListTools/ExportTool.cs | 99 | ||||
-rw-r--r-- | src/PriceListTools/MergeTool.cs | 46 | ||||
-rw-r--r-- | src/PriceListTools/PriceListBase.cs | 15 | ||||
-rw-r--r-- | src/PriceListTools/PriceListHeaders.cs | 11 | ||||
-rw-r--r-- | src/PriceListTools/Product.cs | 35 | ||||
-rw-r--r-- | src/PriceListTools/SourcePriceList.cs | 115 | ||||
-rw-r--r-- | src/PriceListTools/TargetPriceList.cs | 39 | ||||
-rw-r--r-- | src/PriceListTools/ToolBase.cs | 189 |
10 files changed, 0 insertions, 635 deletions
diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs deleted file mode 100644 index e0429bd..0000000 --- a/src/PriceListTools/CombineTool.cs +++ /dev/null @@ -1,56 +0,0 @@ -using Microsoft.Office.Interop.Excel; -using RhSolutions.Interface; -using System; -using System.Collections.Generic; -using System.Linq; -using Dialog = RhSolutions.Interface.Dialog; - -namespace RhSolutions.PriceListTools -{ - internal class CombineTool : ToolBase - { - private List<SourcePriceList> SourceFiles { get; set; } - - public CombineTool() - { - string[] files = Dialog.GetMultiplyFiles(); - - if (files != null) - { - SourceFiles = SourcePriceList.GetSourceLists(files); - } - - else - { - throw new Exception("Не выбраны файлы"); - } - } - - public override void FillTarget() - { - using (ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(file => file.PositionAmount.Count))) - using (ResultBar = new ResultBar()) - { - foreach (SourcePriceList source in SourceFiles) - { - TargetFile.Sheet.Columns[TargetFile.AmountCell.Column] - .EntireColumn - .Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow); - - Range newColumnHeader = TargetFile.Sheet.Cells[TargetFile.AmountCell.Row, TargetFile.AmountCell.Column - 1]; - newColumnHeader.Value2 = $"{source.Name}"; - newColumnHeader.WrapText = true; - - foreach (var kvp in source.PositionAmount) - { - FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column - 1, TargetFile.AmountCell.Column); - ProgressBar.Update(); - } - } - - FilterByAmount(); - ResultBar.Update(); - } - } - } -} diff --git a/src/PriceListTools/ConvertTool.cs b/src/PriceListTools/ConvertTool.cs deleted file mode 100644 index 788d6ff..0000000 --- a/src/PriceListTools/ConvertTool.cs +++ /dev/null @@ -1,30 +0,0 @@ -using RhSolutions.Interface; - -namespace RhSolutions.PriceListTools -{ - internal class ConvertTool : ToolBase - { - private SourcePriceList Current { get; set; } - - public ConvertTool() - { - Current = new SourcePriceList(ExcelApp.ActiveWorkbook); - } - - public override void FillTarget() - { - using (ProgressBar = new ProgressBar("Заполняю строки...", Current.PositionAmount.Count)) - using (ResultBar = new ResultBar()) - { - foreach (var kvp in Current.PositionAmount) - { - FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column); - ProgressBar.Update(); - } - - FilterByAmount(); - ResultBar.Update(); - } - } - } -}
\ No newline at end of file diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs deleted file mode 100644 index c21e9b1..0000000 --- a/src/PriceListTools/ExportTool.cs +++ /dev/null @@ -1,99 +0,0 @@ -using Microsoft.Office.Interop.Excel; -using System; -using System.Collections.Generic; -using RhSolutions.Interface; - -namespace RhSolutions.PriceListTools -{ - internal class ExportTool : ToolBase - { - private Dictionary<Product, double> PositionAmount; - private readonly Range Selection; - - public ExportTool() - { - Selection = ExcelApp.Selection; - GetSelected(); - - if (PositionAmount.Count == 0) - { - throw new Exception("В выделенном диапазоне не найдены позиции для экспорта"); - } - } - - public override void FillTarget() - { - using (ProgressBar = new ProgressBar("Заполняю строки...", PositionAmount.Count)) - using (ResultBar = new ResultBar()) - { - foreach (var kvp in PositionAmount) - { - FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column); - ProgressBar.Update(); - } - - FilterByAmount(); - ResultBar.Update(); - } - } - - private void GetSelected() - { - object[,] cells = Selection.Value2; - PositionAmount = new Dictionary<Product, double>(); - - int rowsCount = Selection.Rows.Count; - - for (int row = 1; row <= rowsCount; row++) - { - if (cells[row, 1] == null || cells[row, 2] == null) - continue; - - string sku = null; - double? amount = null; - - for (int column = 1; column <= 2; column++) - { - object current = cells[row, column]; - - if (Sku.TryParse(current.ToString(), out Sku rauSku)) - { - sku = rauSku.ToString(); - } - - else if (current.GetType() == typeof(string) - && double.TryParse(current.ToString(), out _)) - { - amount = double.Parse((string)current); - } - - else if (current.GetType() == typeof(double)) - { - amount = (double)current; - } - } - - if (sku == null || amount == null) - { - continue; - } - - Product position = new Product - { - ProductSku = sku - }; - - if (PositionAmount.ContainsKey(position)) - { - PositionAmount[position] += amount.Value; - } - - else - { - PositionAmount.Add(position, amount.Value); - } - } - } - } -} - diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs deleted file mode 100644 index 3a945dd..0000000 --- a/src/PriceListTools/MergeTool.cs +++ /dev/null @@ -1,46 +0,0 @@ -using RhSolutions.Interface; -using System; -using System.Collections.Generic; -using System.Linq; - -namespace RhSolutions.PriceListTools -{ - internal class MergeTool : ToolBase - { - private List<SourcePriceList> SourceFiles { get; set; } - - public MergeTool() - { - string[] files = Dialog.GetMultiplyFiles(); - - if (files != null) - { - SourceFiles = SourcePriceList.GetSourceLists(files); - } - - else - { - throw new Exception("Не выбраны файлы"); - } - } - - public override void FillTarget() - { - using (ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(x => x.PositionAmount.Count))) - using (ResultBar = new ResultBar()) - { - foreach (SourcePriceList source in SourceFiles) - { - foreach (var kvp in source.PositionAmount) - { - FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column); - ProgressBar.Update(); - } - } - - FilterByAmount(); - ResultBar.Update(); - } - } - } -} diff --git a/src/PriceListTools/PriceListBase.cs b/src/PriceListTools/PriceListBase.cs deleted file mode 100644 index 805cd04..0000000 --- a/src/PriceListTools/PriceListBase.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Microsoft.Office.Interop.Excel; - -namespace RhSolutions.PriceListTools -{ - internal abstract class PriceListBase - { - public Range AmountCell { get; protected set; } - public Range SkuCell { get; protected set; } - public Range GroupCell { get; protected set; } - public Range NameCell { get; protected set; } - - public Worksheet Sheet { get; protected set; } - public string Name { get; protected set; } - } -}
\ No newline at end of file diff --git a/src/PriceListTools/PriceListHeaders.cs b/src/PriceListTools/PriceListHeaders.cs deleted file mode 100644 index 9c09ed7..0000000 --- a/src/PriceListTools/PriceListHeaders.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace RhSolutions.PriceListTools -{ - internal static class PriceListHeaders - { - public static readonly string Amount = "Кол-во"; - public static readonly string OldSku = "Прежний материал"; - public static readonly string Sku = "Актуальный материал"; - public static readonly string Group = "Программа"; - public static readonly string Name = "Наименование"; - } -}
\ No newline at end of file diff --git a/src/PriceListTools/Product.cs b/src/PriceListTools/Product.cs deleted file mode 100644 index e631293..0000000 --- a/src/PriceListTools/Product.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Linq; - -namespace RhSolutions.PriceListTools -{ - public class Product - { - public string ProductLine { get; set; } - public string ProductSku { get; set; } - public string Name { get; set; } - - public override bool Equals(object obj) - { - if (obj as Product == null) - return false; - - Product other = obj as Product; - - return ProductLine == other.ProductLine && - ProductSku == other.ProductSku && - Name == other.Name; - } - - public override int GetHashCode() - { - string[] properties = new[] - { - ProductLine, - ProductSku, - Name - }; - - return string.Concat(properties.Where(p => p != null)).GetHashCode(); - } - } -}
\ No newline at end of file diff --git a/src/PriceListTools/SourcePriceList.cs b/src/PriceListTools/SourcePriceList.cs deleted file mode 100644 index 4e43d7a..0000000 --- a/src/PriceListTools/SourcePriceList.cs +++ /dev/null @@ -1,115 +0,0 @@ -using ExcelDna.Integration; -using Microsoft.Office.Interop.Excel; -using System; -using System.Collections.Generic; -using System.Linq; -using RhSolutions.Interface; - -namespace RhSolutions.PriceListTools -{ - internal class SourcePriceList : PriceListBase - { - public Dictionary<Product, double> PositionAmount { get; private set; } - - public SourcePriceList(Workbook workbook) - { - if (workbook == null) - { - throw new ArgumentException($"Нет рабочего файла"); - } - - Sheet = workbook.ActiveSheet; - Name = workbook.Name; - - 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) - }; - - if (cells.Any(x => x == null)) - { - throw new ArgumentException($"Файл {Name} не распознан"); - } - - CreatePositionsDict(); - } - - public static List<SourcePriceList> GetSourceLists(string[] files) - { - var ExcelApp = (Application)ExcelDnaUtil.Application; - ProgressBar bar = new ProgressBar("Открываю исходные файлы...", files.Length); - - List<SourcePriceList> sourceFiles = new List<SourcePriceList>(); - - foreach (string file in files) - { - ExcelApp.ScreenUpdating = false; - Workbook wb = ExcelApp.Workbooks.Open(file); - try - { - SourcePriceList priceList = new SourcePriceList(wb); - sourceFiles.Add(priceList); - wb.Close(); - bar.Update(); - } - 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; - } - - 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++) - { - double? amount = Sheet.Cells[row, AmountCell.Column].Value2 as double?; - - if (amount != null && amount.Value != 0) - { - 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.ToString().IsRehauSku()) - continue; - - Product p = new Product - { - ProductSku = sku.ToString(), - ProductLine = group.ToString(), - Name = name.ToString() - }; - - if (PositionAmount.ContainsKey(p)) - { - PositionAmount[p] += amount.Value; - } - - else - { - PositionAmount.Add(p, amount.Value); - } - } - } - } - } -} - diff --git a/src/PriceListTools/TargetPriceList.cs b/src/PriceListTools/TargetPriceList.cs deleted file mode 100644 index e360062..0000000 --- a/src/PriceListTools/TargetPriceList.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Microsoft.Office.Interop.Excel; -using System; -using System.Linq; - -namespace RhSolutions.PriceListTools -{ - internal class TargetPriceList : PriceListBase - { - public Range OldSkuCell { get; private set; } - - public TargetPriceList(Workbook workbook) - { - if (workbook == null) - { - throw new ArgumentException("Невозможно открыть книгу шаблонного файла. " + - "Возможно открыт файл с именем, совпадающим с именем шаблонного файла."); - } - - Sheet = workbook.ActiveSheet; - Name = workbook.FullName; - - 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) - }; - - OldSkuCell = Sheet.Cells.Find(PriceListHeaders.OldSku); - - if (cells.Any(x => x == null)) - { - throw new ArgumentException($"Шаблон { Name } не является прайс-листом"); - } - } - } -} - diff --git a/src/PriceListTools/ToolBase.cs b/src/PriceListTools/ToolBase.cs deleted file mode 100644 index d64522d..0000000 --- a/src/PriceListTools/ToolBase.cs +++ /dev/null @@ -1,189 +0,0 @@ -using Microsoft.Office.Interop.Excel; -using RhSolutions.Interface; -using System; -using System.Collections.Generic; -using System.Linq; -using ProgressBar = RhSolutions.Interface.ProgressBar; - -namespace RhSolutions.PriceListTools -{ - internal abstract class ToolBase - { - protected Application ExcelApp = AddIn.Excel; - protected TargetPriceList TargetFile { get; set; } - protected ResultBar ResultBar { get; set; } - protected ProgressBar ProgressBar { get; set; } - - public abstract void FillTarget(); - - public void OpenNewPrice() - { - if (ExcelApp.Workbooks - .Cast<Workbook>() - .FirstOrDefault(w => w.FullName == RegistryUtil.PriceListPath) != null) - { - throw new ArgumentException("Шаблонный файл редактируется в другом месте"); - } - - Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath, null, true); - - try - { - TargetFile = new TargetPriceList(wb); - } - - catch (Exception exception) - { - if (wb != null) - { - wb.Close(); - } - - throw exception; - } - } - - protected void FillPositionAmountToColumns(KeyValuePair<Product, double> positionAmount, params int[] columns) - { - Range worksheetCells = TargetFile.Sheet.Cells; - Range skuColumn = TargetFile.SkuCell.EntireColumn; - Range oldSkuColumn = TargetFile.OldSkuCell.EntireColumn; - - int? row = GetPositionRow(skuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLine); - - if (row != null) - { - foreach (int column in columns) - { - Range cell = worksheetCells[row, column]; - cell.AddValue(positionAmount.Value); - } - - ResultBar.IncrementSuccess(); - return; - } - - if (TargetFile.OldSkuCell != null) - { - row = GetPositionRow(oldSkuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLine); - - if (row != null) - { - foreach (int column in columns) - { - Range cell = worksheetCells[row, column]; - cell.AddValue(positionAmount.Value); - } - - ResultBar.IncrementReplaced(); - return; - } - } - - string sku = positionAmount.Key.ProductSku.Substring(1, 6); - row = GetPositionRow(skuColumn, sku, positionAmount.Key.ProductLine); - - if (row != null) - { - foreach (int column in columns) - { - Range cell = worksheetCells[row, column]; - cell.AddValue(positionAmount.Value); - } - - ResultBar.IncrementReplaced(); - return; - } - - FillMissing(positionAmount, columns); - ResultBar.IncrementNotFound(); - } - - protected void FillMissing(KeyValuePair<Product, double> positionAmount, params int[] columns) - { - Range worksheetCells = TargetFile.Sheet.Cells; - Range worksheetRows = TargetFile.Sheet.Rows; - int skuColumn = TargetFile.SkuCell.Column; - int groupColumn = TargetFile.GroupCell.Column; - int nameColumn = TargetFile.NameCell.Column; - - int row = worksheetCells[worksheetRows.Count, skuColumn] - .End[XlDirection.xlUp] - .Row + 1; - - worksheetRows[row] - .EntireRow - .Insert(XlInsertShiftDirection.xlShiftDown, XlInsertFormatOrigin.xlFormatFromLeftOrAbove); - - Range previous = worksheetRows[row - 1]; - Range current = worksheetRows[row]; - - previous.Copy(current); - current.ClearContents(); - - worksheetCells[row, groupColumn].Value2 = positionAmount.Key.ProductLine; - worksheetCells[row, nameColumn].Value2 = positionAmount.Key.Name; - - if (TargetFile.OldSkuCell != null) - { - worksheetCells[row, skuColumn].Value2 = "Не найден"; - worksheetCells[row, TargetFile.OldSkuCell.Column].Value2 = positionAmount.Key.ProductSku; - } - - else - { - worksheetCells[row, skuColumn].Value2 = positionAmount.Key.ProductSku; - } - - foreach (int column in columns) - { - Range cell = worksheetCells[row, column]; - cell.AddValue(positionAmount.Value); - } - } - - protected int? GetPositionRow(Range range, string sku, string group) - { - Range found = range.Find(sku); - string foundGroupValue; - - if (found == null) - { - return null; - } - - int firstFoundRow = found.Row; - - if (string.IsNullOrEmpty(group)) - { - return found.Row; - } - - while (true) - { - foundGroupValue = TargetFile.Sheet.Cells[found.Row, TargetFile.GroupCell.Column].Value2.ToString(); - - if (group.Equals(foundGroupValue)) - { - return found.Row; - } - - found = range.FindNext(found); - - if (found.Row == firstFoundRow) - { - return null; - } - } - } - - protected void FilterByAmount() - { - AutoFilter filter = TargetFile.Sheet.AutoFilter; - int startColumn = filter.Range.Column; - - filter.Range.AutoFilter(TargetFile.AmountCell.Column - startColumn + 1, "<>"); - TargetFile.Sheet.Range["A1"].Activate(); - } - } -}
\ No newline at end of file |