diff options
author | Serghei Cebotari <51533848+schebotar@users.noreply.github.com> | 2022-02-05 13:18:18 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-05 13:18:18 +0300 |
commit | ad7234fda7927c45e5ca457facae71a4b9be6b31 (patch) | |
tree | d9fc89b0e02a5fdc92d54674b7b48ad2d10859e4 /src/PriceListTools/SourcePriceList.cs | |
parent | 180807d749f4eb3a16c1f136d42b90ea2945008f (diff) | |
parent | eb6a28b955b5b179bd40f21dcf1daa6f9337765f (diff) |
Merge pull request #15 from schebotar/dev
Dev
Diffstat (limited to 'src/PriceListTools/SourcePriceList.cs')
-rw-r--r-- | src/PriceListTools/SourcePriceList.cs | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/src/PriceListTools/SourcePriceList.cs b/src/PriceListTools/SourcePriceList.cs new file mode 100644 index 0000000..01637f8 --- /dev/null +++ b/src/PriceListTools/SourcePriceList.cs @@ -0,0 +1,111 @@ +using ExcelDna.Integration; +using Microsoft.Office.Interop.Excel; +using System; +using System.Collections.Generic; +using System.Linq; +using RehauSku.Interface; + +namespace RehauSku.PriceListTools +{ + + internal class SourcePriceList : AbstractPriceList + { + public Dictionary<Position, 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(amountHeader), + skuCell = Sheet.Cells.Find(skuHeader), + groupCell = Sheet.Cells.Find(groupHeader), + nameCell = Sheet.Cells.Find(nameHeader) + }; + + 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<Position, double>(); + + for (int row = amountCell.Row + 1; row <= Sheet.Cells[Sheet.Rows.Count, amountCell.Column].End[XlDirection.xlUp].Row; row++) + { + object amount = Sheet.Cells[row, amountCell.Column].Value2; + + if (amount != null && (double)amount != 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; + + Position p = new Position(group.ToString(), sku.ToString(), name.ToString()); + + if (PositionAmount.ContainsKey(p)) + { + PositionAmount[p] += (double)amount; + } + + else + { + PositionAmount.Add(p, (double)amount); + } + } + } + } + } +} + |