aboutsummaryrefslogtreecommitdiff
path: root/src/PriceListTools/ExportTool.cs
blob: 8bf274a1ee7992d4c7d49c8a56fd203040d7a1af (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
using Microsoft.Office.Interop.Excel;
using RehauSku.Assistant;
using System;
using System.Collections.Generic;

namespace RehauSku.PriceListTools
{
    internal class ExportTool : PriceListTool
    {
        private Dictionary<string, double> SkuAmount { get; set; }
        private Range Selection;

        public void TryGetSelection()
        {
            Selection = ExcelApp.Selection;

            if (Selection == null || Selection.Columns.Count != 2)
            {
                throw new Exception("Неверный диапазон");
            }
        }

        public void FillTarget()
        {
            ExcelApp.ScreenUpdating = false;
            GetSelected();
            FillColumn(SkuAmount, TargetFile.amountCell.Column);
            FilterByAmount();
            ExcelApp.ScreenUpdating = true;

            Forms.Dialog.SaveWorkbookAs();
        }

        private void FillColumn(IEnumerable<KeyValuePair<string, double>> dictionary, int column)
        {
            List<KeyValuePair<string, double>> missing = new List<KeyValuePair<string, double>>();

            foreach (var kvp in dictionary)
            {
                Range cell = TargetFile.skuCell.EntireColumn.Find(kvp.Key);

                if (cell == null)
                {
                    missing.Add(kvp);
                }

                else
                {
                    Range sumCell = TargetFile.Sheet.Cells[cell.Row, column];

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

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

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


        private void GetSelected()
        {
            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);
                }
            }
        }
    }
}