aboutsummaryrefslogtreecommitdiff
path: root/src/Controllers/ExportTool.cs
diff options
context:
space:
mode:
authorSergey Chebotar <s.chebotar@gmail.com>2022-12-20 12:27:47 +0300
committerSergey Chebotar <s.chebotar@gmail.com>2022-12-20 12:27:47 +0300
commit73569a43644309d0342817580bcfd86c1face5b8 (patch)
treef3c6e15db82130b02ec8c3fa1b64674e6a9cf48d /src/Controllers/ExportTool.cs
parent3d186c22e8665b80839495fdcf4b176c2f3e03b9 (diff)
Namespace refactoring
Diffstat (limited to 'src/Controllers/ExportTool.cs')
-rw-r--r--src/Controllers/ExportTool.cs99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/Controllers/ExportTool.cs b/src/Controllers/ExportTool.cs
new file mode 100644
index 0000000..6d8c348
--- /dev/null
+++ b/src/Controllers/ExportTool.cs
@@ -0,0 +1,99 @@
+using Microsoft.Office.Interop.Excel;
+using System;
+using System.Collections.Generic;
+using RhSolutions.Models;
+
+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 Sku rauSku))
+ {
+ sku = rauSku.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;
+ }
+
+ Product position = new Product
+ {
+ ProductSku = sku
+ };
+
+ if (PositionAmount.ContainsKey(position))
+ {
+ PositionAmount[position] += amount.Value;
+ }
+
+ else
+ {
+ PositionAmount.Add(position, amount.Value);
+ }
+ }
+ }
+ }
+}
+