aboutsummaryrefslogtreecommitdiff
path: root/src/PriceListTools/PriceListTool.cs
blob: 9d898e77c9dc890292efae34cfc35cd1a9b64b4f (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Application = Microsoft.Office.Interop.Excel.Application;

namespace RehauSku.PriceListTools
{
    internal abstract class PriceListTool
    {
        protected private Application ExcelApp = (Application)ExcelDnaUtil.Application;
        protected private Target TargetFile;
        protected private List<KeyValuePair<Position, double>> Missing;

        public void OpenNewPrice()
        {
            Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath);

            try
            {
                TargetFile = new Target(wb);
            }

            catch (Exception ex)
            {
                MessageBox.Show
                    (ex.Message,
                    "Ошибка открытия шаблонного прайс-листа",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                wb.Close();
                throw ex;
            }
        }

        protected private void FillColumnsWithDictionary(IEnumerable<KeyValuePair<Position, double>> dictionary, params int[] columns)
        {
            Missing = new List<KeyValuePair<Position, double>>();

            foreach (var positionAmount in dictionary)
            {
                FillPositionAmountToColumns(positionAmount, columns);
            }

            if (Missing.Count > 0)
            { 
                DialogResult result = 
                MessageBox.Show
                    ($"{Missing.Count} артикулов отсутствует в таблице заказов {RegistryUtil.PriceListPath} Попробовать найти новый вариант?",
                    "Отсутствует позиция в конечной таблице заказов",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Information);

                if (result == DialogResult.Yes)
                {
                    var lookUp = new List<KeyValuePair<Position, double>>(Missing);

                    foreach (var missingPosition in lookUp)
                    {
                        TryFillVariantlessSkuToColumns(missingPosition, columns);
                    }
                }
            }

            if (Missing.Count > 0)
            {
                FillMissing();
                MessageBox.Show
                    ($"{Missing.Count} артикулов отсутствует в таблице заказов {RegistryUtil.PriceListPath}\n" +
                    $"Под основной таблицей составлен список не найденных артикулов",
                    "Отсутствует позиция в конечной таблице заказов",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }
        }

        protected private void FillPositionAmountToColumns(KeyValuePair<Position, double> positionAmount, int[] columns)
        {
            Range foundCell = TargetFile.skuCell.EntireColumn.Find(positionAmount.Key.Sku);

            if (foundCell == null)
            {
                Missing.Add(positionAmount);
                return;
            }

            string foundCellGroup = TargetFile.Sheet.Cells[foundCell.Row, TargetFile.groupCell.Column].Value2.ToString();

            while (foundCell != null && foundCellGroup != positionAmount.Key.Group)
            {
                foundCell = TargetFile.skuCell.EntireColumn.FindNext(foundCell);
                foundCellGroup = TargetFile.Sheet.Cells[foundCell.Row, TargetFile.groupCell.Column].Value2.ToString();
            }

            if (foundCell == null)
            {
                Missing.Add(positionAmount);
            }

            else
            {
                foreach (var column in columns)
                {
                    Range sumCell = TargetFile.Sheet.Cells[foundCell.Row, column];

                    if (sumCell.Value2 == null)
                    {
                        sumCell.Value2 = positionAmount.Value;
                    }

                    else
                    {
                        sumCell.Value2 += positionAmount.Value;
                    }
                }
            }
        }

        protected private void TryFillVariantlessSkuToColumns(KeyValuePair<Position, double> positionAmount, int[] columns)
        {
            string sku = positionAmount.Key.Sku.Substring(1, 6);

            Range foundCell = TargetFile.skuCell.EntireColumn.Find(sku);

            if (foundCell == null)
            {
                return;
            }

            string foundCellGroup = TargetFile.Sheet.Cells[foundCell.Row, TargetFile.groupCell.Column].Value2.ToString();

            while (foundCell != null && foundCellGroup != positionAmount.Key.Group)
            {
                foundCell = TargetFile.skuCell.EntireColumn.FindNext(foundCell);
                foundCellGroup = TargetFile.Sheet.Cells[foundCell.Row, TargetFile.groupCell.Column].Value2.ToString();
            }

            if (foundCell == null)
            {
                return;
            }

            foreach (var column in columns)
            {
                Range sumCell = TargetFile.Sheet.Cells[foundCell.Row, column];

                if (sumCell.Value2 == null)
                {
                    sumCell.Value2 = positionAmount.Value;
                }

                else
                {
                    sumCell.Value2 += positionAmount.Value;
                }
            }

            Missing.Remove(positionAmount);
        }

        protected private void FillMissing()
        {
            int startRow =
                TargetFile.Sheet.AutoFilter.Range.Row + 
                TargetFile.Sheet.AutoFilter.Range.Rows.Count + 5;

            for (int i = 0; i < Missing.Count; i++)
            {
                Range group = TargetFile.Sheet.Cells[startRow + i, TargetFile.groupCell.Column];
                Range sku = TargetFile.Sheet.Cells[startRow + i, TargetFile.skuCell.Column];
                Range name = TargetFile.Sheet.Cells[startRow + i, TargetFile.nameCell.Column];
                Range amount = TargetFile.Sheet.Cells[startRow + i, TargetFile.amountCell.Column];

                group.Value2 = Missing[i].Key.Group;
                sku.Value2 = Missing[i].Key.Sku;
                name.Value2 = Missing[i].Key.Name;
                amount.Value2 = Missing[i].Value;

                group.ClearFormats();
                sku.ClearFormats();
                name.ClearFormats();
                amount.ClearFormats();
            }
        }

        protected private void FilterByAmount()
        {
            AutoFilter filter = TargetFile.Sheet.AutoFilter;

            filter.Range.AutoFilter(TargetFile.amountCell.Column, "<>");
            TargetFile.Sheet.Range["A1"].Activate();
        }
    }
}