aboutsummaryrefslogtreecommitdiff
path: root/src/Controllers/ExportTool.cs
blob: e2bbde4540c36953444e315271c4fb5b43c50436 (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
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using RhSolutions.Models;
using System.Linq;
using Range = Microsoft.Office.Interop.Excel.Range;

namespace RhSolutions.Controllers
{
    internal class ExportTool : ToolBase
    {
        private Dictionary<Product, double> PositionAmount;
        private readonly Range Selection;

        public ExportTool()
        {
            Selection = ExcelApp.Selection;
            GetSelected();

            if (PositionAmount.Count == 0)
            {
                throw new Exception("В выделенном диапазоне не найдены позиции для экспорта");
            }
        }

        public override void FillTarget()
        {
            using (ProgressBar = new ProgressBar("Заполняю строки...", PositionAmount.Count))
            using (ResultBar = new ResultBar())
            {
                foreach (var kvp in PositionAmount)
                {
                    FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column);
                    ProgressBar.Update();
                }

                FilterByAmount();
                ResultBar.Update();
            }
        }

        private void GetSelected()
        {
            object[,] cells = Selection.Value2;
            PositionAmount = new Dictionary<Product, 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 (Sku.TryParse(current.ToString(), out var rauSku))
                    {
                        sku = rauSku.FirstOrDefault().ToString() ?? null;
                    }

                    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;
                }

                Product position = new Product
                {
                    ProductSku = sku
                };

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

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