RhSolutions-AddIn/RhSolutions.AddIn/Controllers/ExportTool.cs

98 lines
2.5 KiB
C#
Raw Normal View History

2023-03-28 10:36:36 +03:00
using RhSolutions.Models;
2021-12-24 16:22:03 +03:00
using System.Collections.Generic;
using System.Linq;
2021-12-24 16:22:03 +03:00
2023-03-28 10:36:36 +03:00
namespace RhSolutions.Controllers;
internal class ExportTool : ToolBase
2021-12-24 16:22:03 +03:00
{
2023-03-28 10:36:36 +03:00
private Dictionary<Product, double> PositionAmount;
private readonly Range Selection;
public ExportTool()
2021-12-24 16:22:03 +03:00
{
2023-03-28 10:36:36 +03:00
Selection = ExcelApp.Selection;
GetSelected();
2021-12-24 16:22:03 +03:00
2023-03-28 10:36:36 +03:00
if (PositionAmount.Count == 0)
2021-12-24 16:22:03 +03:00
{
2023-03-28 10:36:36 +03:00
throw new Exception("В выделенном диапазоне не найдены позиции для экспорта");
2021-12-24 16:22:03 +03:00
}
2023-03-28 10:36:36 +03:00
}
2023-03-28 10:36:36 +03:00
public override void FillTarget()
{
using (ProgressBar = new ProgressBar("Заполняю строки...", PositionAmount.Count))
using (ResultBar = new ResultBar())
2021-12-24 16:22:03 +03:00
{
2023-03-28 10:36:36 +03:00
foreach (var kvp in PositionAmount)
{
2023-03-28 10:36:36 +03:00
FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column);
ProgressBar.Update();
2022-04-01 17:55:36 +03:00
}
2023-03-28 10:36:36 +03:00
FilterByAmount();
ResultBar.Update();
2021-12-24 16:22:03 +03:00
}
2023-03-28 10:36:36 +03:00
}
private void GetSelected()
{
object[,] cells = Selection.Value2;
PositionAmount = new Dictionary<Product, double>();
int rowsCount = Selection.Rows.Count;
2021-12-24 16:22:03 +03:00
2023-03-28 10:36:36 +03:00
for (int row = 1; row <= rowsCount; row++)
2021-12-24 16:22:03 +03:00
{
2023-03-28 10:36:36 +03:00
if (cells[row, 1] == null || cells[row, 2] == null)
continue;
2023-03-28 10:36:36 +03:00
string sku = null;
double? amount = null;
2021-12-24 16:22:03 +03:00
2023-03-28 10:36:36 +03:00
for (int column = 1; column <= 2; column++)
2021-12-24 16:22:03 +03:00
{
2023-03-28 10:36:36 +03:00
object current = cells[row, column];
2021-12-24 16:22:03 +03:00
2023-03-28 10:36:36 +03:00
if (Sku.TryParse(current.ToString(), out var rauSku))
2021-12-24 16:22:03 +03:00
{
2023-03-28 10:36:36 +03:00
sku = rauSku.FirstOrDefault().ToString() ?? null;
2021-12-24 16:22:03 +03:00
}
2023-03-28 10:36:36 +03:00
else if (current.GetType() == typeof(string)
&& double.TryParse(current.ToString(), out _))
2022-01-27 20:43:19 +03:00
{
2023-03-28 10:36:36 +03:00
amount = double.Parse((string)current);
2022-01-27 20:43:19 +03:00
}
2021-12-24 16:22:03 +03:00
2023-03-28 10:36:36 +03:00
else if (current.GetType() == typeof(double))
2022-01-27 20:43:19 +03:00
{
2023-03-28 10:36:36 +03:00
amount = (double)current;
2022-01-27 20:43:19 +03:00
}
2023-03-28 10:36:36 +03:00
}
2023-03-28 10:36:36 +03:00
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);
2021-12-24 16:22:03 +03:00
}
}
}
}