aboutsummaryrefslogtreecommitdiff
path: root/src/PriceListTools/SourcePriceList.cs
blob: b11e0608fdb4e964d6180e23e85c98b5cfb31b0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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(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<Position, 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;

                    Position p = new Position(group.ToString(), sku.ToString(), name.ToString());

                    if (PositionAmount.ContainsKey(p))
                    {
                        PositionAmount[p] += amount.Value;
                    }

                    else
                    {
                        PositionAmount.Add(p, amount.Value);
                    }
                }
            }
        }
    }
}