From 73569a43644309d0342817580bcfd86c1face5b8 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 20 Dec 2022 12:27:47 +0300 Subject: Namespace refactoring --- src/Controllers/CombineTool.cs | 56 +++++++++++ src/Controllers/ConvertTool.cs | 30 ++++++ src/Controllers/ExportTool.cs | 99 +++++++++++++++++++ src/Controllers/MergeTool.cs | 46 +++++++++ src/Controllers/RibbonController.cs | 137 ++++++++++++++++++++++++++ src/Controllers/ToolBase.cs | 189 ++++++++++++++++++++++++++++++++++++ 6 files changed, 557 insertions(+) create mode 100644 src/Controllers/CombineTool.cs create mode 100644 src/Controllers/ConvertTool.cs create mode 100644 src/Controllers/ExportTool.cs create mode 100644 src/Controllers/MergeTool.cs create mode 100644 src/Controllers/RibbonController.cs create mode 100644 src/Controllers/ToolBase.cs (limited to 'src/Controllers') diff --git a/src/Controllers/CombineTool.cs b/src/Controllers/CombineTool.cs new file mode 100644 index 0000000..75c0f51 --- /dev/null +++ b/src/Controllers/CombineTool.cs @@ -0,0 +1,56 @@ +using Microsoft.Office.Interop.Excel; +using RhSolutions.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using Dialog = RhSolutions.Models.Dialog; + +namespace RhSolutions.Controllers +{ + internal class CombineTool : ToolBase + { + private List 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/Controllers/ConvertTool.cs b/src/Controllers/ConvertTool.cs new file mode 100644 index 0000000..5b2cd4d --- /dev/null +++ b/src/Controllers/ConvertTool.cs @@ -0,0 +1,30 @@ +using RhSolutions.Models; + +namespace RhSolutions.Controllers +{ + 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/Controllers/ExportTool.cs b/src/Controllers/ExportTool.cs new file mode 100644 index 0000000..6d8c348 --- /dev/null +++ b/src/Controllers/ExportTool.cs @@ -0,0 +1,99 @@ +using Microsoft.Office.Interop.Excel; +using System; +using System.Collections.Generic; +using RhSolutions.Models; + +namespace RhSolutions.Controllers +{ + internal class ExportTool : ToolBase + { + private Dictionary 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(); + + 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/Controllers/MergeTool.cs b/src/Controllers/MergeTool.cs new file mode 100644 index 0000000..dec8ff7 --- /dev/null +++ b/src/Controllers/MergeTool.cs @@ -0,0 +1,46 @@ +using RhSolutions.Models; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace RhSolutions.Controllers +{ + internal class MergeTool : ToolBase + { + private List 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/Controllers/RibbonController.cs b/src/Controllers/RibbonController.cs new file mode 100644 index 0000000..87c62e1 --- /dev/null +++ b/src/Controllers/RibbonController.cs @@ -0,0 +1,137 @@ +using ExcelDna.Integration.CustomUI; +using Microsoft.Office.Interop.Excel; +using RhSolutions.Services; +using System; +using System.IO; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Windows.Forms; + +namespace RhSolutions.Controllers +{ + [ComVisible(true)] + public class RibbonController : ExcelRibbon + { + private static IRibbonUI ribbonUi; + + public override string GetCustomUI(string RibbonID) + { + return @" + + + + + +