diff options
author | Sergey Chebotar <s.chebotar@gmail.com> | 2021-12-26 18:22:32 +0300 |
---|---|---|
committer | Sergey Chebotar <s.chebotar@gmail.com> | 2021-12-26 18:22:32 +0300 |
commit | 54fc3320e7d64d7903b4d091fe0d5c15df01fd78 (patch) | |
tree | ac8b9aa1e883a85339a594b2797ab319cca73c4e /src/PriceListTools | |
parent | 20cfbfcca3a779c04aecdca5e4b465651e2be42a (diff) |
Move to /src
Diffstat (limited to 'src/PriceListTools')
-rw-r--r-- | src/PriceListTools/ExportTool.cs | 115 | ||||
-rw-r--r-- | src/PriceListTools/MergeTool.cs | 88 | ||||
-rw-r--r-- | src/PriceListTools/PriceList.cs | 87 | ||||
-rw-r--r-- | src/PriceListTools/PriceListUtil.cs | 41 |
4 files changed, 331 insertions, 0 deletions
diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs new file mode 100644 index 0000000..61f6f0f --- /dev/null +++ b/src/PriceListTools/ExportTool.cs @@ -0,0 +1,115 @@ +using ExcelDna.Integration; +using Microsoft.Office.Interop.Excel; +using RehauSku.Assistant; +using System; +using System.Collections.Generic; + +namespace RehauSku.PriceListTools +{ + class ExportTool : IDisposable + { + private Application ExcelApp; + private Dictionary<string, double> SkuAmount { get; set; } + private Range Selection { get; set; } + + public ExportTool() + { + this.ExcelApp = (Application)ExcelDnaUtil.Application; + Selection = ExcelApp.Selection; + + if (IsRangeValid()) + _FillSkuAmountDict(); + } + + public bool IsRangeValid() + { + return Selection != null && + Selection.Columns.Count == 2; + } + + private void _FillSkuAmountDict() + { + object[,] cells = Selection.Value2; + SkuAmount = new Dictionary<string, 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 (current.ToString().IsRehauSku()) + { + sku = current.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; + + if (SkuAmount.ContainsKey(sku)) + SkuAmount[sku] += amount.Value; + else + SkuAmount.Add(sku, amount.Value); + } + } + + public void ExportToNewFile() + { + if (SkuAmount.Count < 1) + { + return; + } + + string exportFile = PriceListUtil.CreateNewExportFile(); + Workbook wb = ExcelApp.Workbooks.Open(exportFile); + + try + { + PriceList priceList = new PriceList(wb); + priceList.Fill(SkuAmount); + } + + catch(Exception ex) + { + System.Windows.Forms.MessageBox.Show + ($"{RegistryUtil.PriceListPath} не является файлом прайс-листа \n\n {ex.Message}", + "Неверный файл прайс-листа!", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Error); + + wb.Close(); + } + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + + } + } +} + diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs new file mode 100644 index 0000000..ddf74d2 --- /dev/null +++ b/src/PriceListTools/MergeTool.cs @@ -0,0 +1,88 @@ +using ExcelDna.Integration; +using Microsoft.Office.Interop.Excel; +using System; +using System.Collections.Generic; + +namespace RehauSku.PriceListTools +{ + class MergeTool : IDisposable + { + private Application ExcelApp; + private Dictionary<string, double> SkuAmount { get; set; } + + public MergeTool() + { + this.ExcelApp = (Application)ExcelDnaUtil.Application; + this.SkuAmount = new Dictionary<string, double>(); + } + + public void AddSkuAmountToDict(string[] files) + { + ExcelApp.ScreenUpdating = false; + foreach (string file in files) + { + Workbook wb = ExcelApp.Workbooks.Open(file); + + try + { + PriceList priceList = new PriceList(wb); + SkuAmount.AddValues(priceList); + } + + catch (Exception ex) + { + System.Windows.Forms.MessageBox.Show + ( $"{wb.Name} не является файлом прайс-листа \n\n {ex.Message}", + "Неверный файл прайс-листа!", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Error); + } + + finally + { + wb.Close(); + } + } + ExcelApp.ScreenUpdating = true; + } + + public void ExportToNewFile(string exportFile) + { + if (SkuAmount.Count < 1) + { + return; + } + + Workbook wb = ExcelApp.Workbooks.Open(exportFile); + PriceList priceList; + + try + { + priceList = new PriceList(wb); + priceList.Fill(SkuAmount); + } + + catch (Exception ex) + { + System.Windows.Forms.MessageBox.Show + ($"{RegistryUtil.PriceListPath} не является файлом прайс-листа \n\n {ex.Message}", + "Неверный файл прайс-листа!", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Error); + + wb.Close(); + } + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + + } + } +} diff --git a/src/PriceListTools/PriceList.cs b/src/PriceListTools/PriceList.cs new file mode 100644 index 0000000..1460c07 --- /dev/null +++ b/src/PriceListTools/PriceList.cs @@ -0,0 +1,87 @@ +using Microsoft.Office.Interop.Excel; +using System.Collections.Generic; + +namespace RehauSku.PriceListTools +{ + class PriceList + { + public readonly Workbook Workbook; + public readonly PriceListSheet OfferSheet; + public readonly PriceListSheet ActiveSheet; + + private const string _amountHeader = "Кол-во"; + private const string _skuHeader = "Актуальный материал"; + private const string _offerSheetHeader = "КП"; + + public PriceList(Workbook workbook) + { + Workbook = workbook; + OfferSheet = new PriceListSheet(workbook.Sheets[_offerSheetHeader]); + + Worksheet active = workbook.ActiveSheet; + + if (active.Name == _offerSheetHeader) + ActiveSheet = OfferSheet; + + else + ActiveSheet = new PriceListSheet(active); + } + + public bool IsValid() + { + return OfferSheet.IsValid() && + ActiveSheet.IsValid(); + } + + public void Fill(Dictionary<string, double> values) + { + Worksheet ws = OfferSheet.sheet; + ws.Activate(); + + int amountColumn = OfferSheet.amountColumn.Value; + int skuColumn = OfferSheet.skuColumn.Value; + + foreach (KeyValuePair<string, double> kvp in values) + { + Range cell = ws.Columns[skuColumn].Find(kvp.Key); + ws.Cells[cell.Row, amountColumn].Value = kvp.Value; + } + + AutoFilter filter = ws.AutoFilter; + int firstFilterColumn = filter.Range.Column; + + filter.Range.AutoFilter(amountColumn - firstFilterColumn + 1, "<>"); + ws.Range["A1"].Activate(); + } + + public class PriceListSheet + { + public readonly Worksheet sheet; + public readonly int? headerRow; + public readonly int? amountColumn; + public readonly int? skuColumn; + public object[,] amountCells; + public object[,] skuCells; + + public PriceListSheet(Worksheet sheet) + { + this.sheet = sheet; + headerRow = sheet.Cells.Find(_amountHeader).Row; + amountColumn = sheet.Cells.Find(_amountHeader).Column; + skuColumn = sheet.Cells.Find(_skuHeader).Column; + + amountCells = sheet.Columns[amountColumn].Value2; + skuCells = sheet.Columns[skuColumn].Value2; + } + + public bool IsValid() + { + return sheet != null && + headerRow != null && + amountColumn != null && + skuColumn != null; + } + } + } +} + diff --git a/src/PriceListTools/PriceListUtil.cs b/src/PriceListTools/PriceListUtil.cs new file mode 100644 index 0000000..14797d9 --- /dev/null +++ b/src/PriceListTools/PriceListUtil.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; +using System.IO; + +namespace RehauSku.PriceListTools +{ + static class PriceListUtil + { + public static string CreateNewExportFile() + { + string fileExtension = Path.GetExtension(RegistryUtil.PriceListPath); + string path = Path.GetTempFileName() + fileExtension; + + File.Copy(RegistryUtil.PriceListPath, path); + return path; + } + + public static void AddValues(this Dictionary<string, double> SkuAmount, PriceList priceList) + { + object[,] amountCells = priceList.ActiveSheet.amountCells; + object[,] skuCells = priceList.ActiveSheet.skuCells; + + for (int row = priceList.ActiveSheet.headerRow.Value + 1; row < amountCells.GetLength(0); row++) + { + object amount = amountCells[row, 1]; + object sku = skuCells[row, 1]; + + if (amount != null && (double)amount != 0) + { + if (SkuAmount.ContainsKey(sku.ToString())) + { + SkuAmount[sku.ToString()] += (double)amount; + } + + else + SkuAmount.Add(sku.ToString(), (double)amount); + } + } + } + } +} + |