aboutsummaryrefslogtreecommitdiff
path: root/Source/DataExport/ExportTool.cs
blob: 8ea65cd37db0413f30e1df6b53dac3edd189a48d (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
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.IO;
using RehauSku.Assistant;

namespace RehauSku.DataExport
{
    public class ExportTool : IDisposable
    {
        private Application xlApp;
        private Dictionary<string, double> SkuAmount { get; set; }
        private Range Selection { get; set; }
        private string CurrentFilePath { get; set; }

        public ExportTool()
        {
            this.xlApp = (Application)ExcelDnaUtil.Application;
            this.CurrentFilePath = xlApp.ActiveWorkbook.FullName;

            _GetSelectedCells();
        }

        private void _GetSelectedCells()
        {
            Selection = xlApp.Selection;
        }

        public bool IsRangeValid()
        {
            return 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.GetType() == typeof(string)
                        && ((string)current).IsRehauSku())
                        sku = (string)current;

                    else if (current.GetType() == typeof(string)
                        && double.TryParse((string)current, 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 FillNewPriceList()
        {
            const string amountHeader = "Кол-во";
            const string skuHeader = "Актуальный материал";

            FillSkuAmountDict();
            string exportFile = _GetExportFullPath();
            File.Copy(RegistryUtil.PriceListPath, exportFile, true);

            Workbook wb = xlApp.Workbooks.Open(exportFile);
            Worksheet ws = wb.Sheets["КП"];
            ws.Activate();

            int amountColumn = ws.Cells.Find(amountHeader).Column;
            int skuColumn = ws.Cells.Find(skuHeader).Column;

            foreach (KeyValuePair<string, double> kvp in SkuAmount)
            {
                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();
        }

        private string _GetExportFullPath()
        {
            string fileExtension = Path.GetExtension(RegistryUtil.PriceListPath);

            return Path.GetTempFileName() + fileExtension;
        }


        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {

        }
    }

    class SelectionCheck
    {

    }
}