2022-01-27 17:34:03 +03:00
|
|
|
|
using Microsoft.Office.Interop.Excel;
|
2021-12-24 16:22:03 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-12-20 12:27:47 +03:00
|
|
|
|
using RhSolutions.Models;
|
2022-12-22 14:52:54 +03:00
|
|
|
|
using System.Linq;
|
2023-02-08 15:59:30 +03:00
|
|
|
|
using Range = Microsoft.Office.Interop.Excel.Range;
|
2021-12-24 16:22:03 +03:00
|
|
|
|
|
2022-12-20 12:27:47 +03:00
|
|
|
|
namespace RhSolutions.Controllers
|
2021-12-24 16:22:03 +03:00
|
|
|
|
{
|
2022-12-20 11:53:55 +03:00
|
|
|
|
internal class ExportTool : ToolBase
|
2021-12-24 16:22:03 +03:00
|
|
|
|
{
|
2022-12-20 08:30:58 +03:00
|
|
|
|
private Dictionary<Product, double> PositionAmount;
|
2022-02-12 16:53:34 +03:00
|
|
|
|
private readonly Range Selection;
|
2021-12-24 16:22:03 +03:00
|
|
|
|
|
2022-02-05 13:04:18 +03:00
|
|
|
|
public ExportTool()
|
2021-12-24 16:22:03 +03:00
|
|
|
|
{
|
|
|
|
|
Selection = ExcelApp.Selection;
|
2022-02-12 16:02:55 +03:00
|
|
|
|
GetSelected();
|
|
|
|
|
|
|
|
|
|
if (PositionAmount.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("В выделенном диапазоне не найдены позиции для экспорта");
|
|
|
|
|
}
|
2021-12-24 16:22:03 +03:00
|
|
|
|
}
|
2022-02-01 20:32:29 +03:00
|
|
|
|
|
2022-03-30 12:26:18 +03:00
|
|
|
|
public override void FillTarget()
|
2021-12-24 16:22:03 +03:00
|
|
|
|
{
|
2022-04-01 17:55:36 +03:00
|
|
|
|
using (ProgressBar = new ProgressBar("Заполняю строки...", PositionAmount.Count))
|
|
|
|
|
using (ResultBar = new ResultBar())
|
2022-02-01 20:32:29 +03:00
|
|
|
|
{
|
2022-04-01 17:55:36 +03:00
|
|
|
|
foreach (var kvp in PositionAmount)
|
|
|
|
|
{
|
|
|
|
|
FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column);
|
|
|
|
|
ProgressBar.Update();
|
|
|
|
|
}
|
2022-02-01 20:32:29 +03:00
|
|
|
|
|
2022-04-01 17:55:36 +03:00
|
|
|
|
FilterByAmount();
|
|
|
|
|
ResultBar.Update();
|
|
|
|
|
}
|
2021-12-24 16:22:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 17:34:03 +03:00
|
|
|
|
private void GetSelected()
|
2021-12-24 16:22:03 +03:00
|
|
|
|
{
|
2022-04-01 17:55:36 +03:00
|
|
|
|
object[,] cells = Selection.Value2;
|
2022-12-20 08:30:58 +03:00
|
|
|
|
PositionAmount = new Dictionary<Product, double>();
|
2022-01-31 17:54:18 +03:00
|
|
|
|
|
2021-12-24 16:22:03 +03:00
|
|
|
|
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];
|
|
|
|
|
|
2022-12-22 14:52:54 +03:00
|
|
|
|
if (Sku.TryParse(current.ToString(), out var rauSku))
|
2021-12-24 16:22:03 +03:00
|
|
|
|
{
|
2022-12-22 14:52:54 +03:00
|
|
|
|
sku = rauSku.FirstOrDefault().ToString() ?? null;
|
2021-12-24 16:22:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2022-01-27 20:43:19 +03:00
|
|
|
|
{
|
2021-12-24 16:22:03 +03:00
|
|
|
|
continue;
|
2022-01-27 20:43:19 +03:00
|
|
|
|
}
|
2021-12-24 16:22:03 +03:00
|
|
|
|
|
2022-12-20 08:30:58 +03:00
|
|
|
|
Product position = new Product
|
|
|
|
|
{
|
|
|
|
|
ProductSku = sku
|
|
|
|
|
};
|
2022-02-01 20:32:29 +03:00
|
|
|
|
|
|
|
|
|
if (PositionAmount.ContainsKey(position))
|
2022-01-27 20:43:19 +03:00
|
|
|
|
{
|
2022-02-01 20:32:29 +03:00
|
|
|
|
PositionAmount[position] += amount.Value;
|
2022-01-27 20:43:19 +03:00
|
|
|
|
}
|
2022-01-31 17:54:18 +03:00
|
|
|
|
|
2021-12-24 16:22:03 +03:00
|
|
|
|
else
|
2022-01-27 20:43:19 +03:00
|
|
|
|
{
|
2022-02-01 20:32:29 +03:00
|
|
|
|
PositionAmount.Add(position, amount.Value);
|
2022-01-27 20:43:19 +03:00
|
|
|
|
}
|
2021-12-24 16:22:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|