RhSolutions-AddIn/src/PriceListTools/ExportTool.cs

127 lines
3.8 KiB
C#
Raw Normal View History

2022-01-27 17:34:03 +03:00
using Microsoft.Office.Interop.Excel;
2021-12-24 16:22:03 +03:00
using RehauSku.Assistant;
using System;
using System.Collections.Generic;
namespace RehauSku.PriceListTools
{
2022-01-27 10:22:30 +03:00
internal class ExportTool : PriceListTool
2021-12-24 16:22:03 +03:00
{
private Dictionary<string, double> SkuAmount { get; set; }
2022-01-09 10:37:25 +03:00
private Range Selection;
2021-12-24 16:22:03 +03:00
2022-01-27 17:34:03 +03:00
public void TryGetSelection()
2021-12-24 16:22:03 +03:00
{
Selection = ExcelApp.Selection;
2022-01-27 17:34:03 +03:00
if (Selection == null || Selection.Columns.Count != 2)
{
throw new Exception("Неверный диапазон");
}
2021-12-24 16:22:03 +03:00
}
2022-01-27 17:34:03 +03:00
public void FillTarget()
2021-12-24 16:22:03 +03:00
{
2022-01-27 17:34:03 +03:00
ExcelApp.ScreenUpdating = false;
GetSelected();
2022-01-27 20:43:19 +03:00
FillColumn(SkuAmount, TargetFile.amountCell.Column);
2022-01-27 17:34:03 +03:00
FilterByAmount();
ExcelApp.ScreenUpdating = true;
2022-01-27 17:34:03 +03:00
Forms.Dialog.SaveWorkbookAs();
2021-12-24 16:22:03 +03:00
}
2022-01-28 11:41:35 +03:00
private void FillColumn(IEnumerable<KeyValuePair<string, double>> dictionary, int column)
{
List<KeyValuePair<string, double>> missing = new List<KeyValuePair<string, double>>();
foreach (var kvp in dictionary)
{
Range cell = TargetFile.skuCell.EntireColumn.Find(kvp.Key);
if (cell == null)
{
missing.Add(kvp);
}
else
{
Range sumCell = TargetFile.Sheet.Cells[cell.Row, column];
if (sumCell.Value2 == null)
{
sumCell.Value2 = kvp.Value;
}
else
{
sumCell.Value2 += kvp.Value;
}
}
}
if (missing.Count > 0)
{
System.Windows.Forms.MessageBox.Show
($"{missing.Count} артикулов отсутствует в таблице заказов {RegistryUtil.PriceListPath} Попробовать найти новый вариант?",
"Отсутствует позиция в конечной таблице заказов",
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Information);
}
}
2022-01-27 17:34:03 +03:00
private void GetSelected()
2021-12-24 16:22:03 +03:00
{
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.ToString().IsRehauSku())
{
sku = current.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)
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
if (SkuAmount.ContainsKey(sku))
2022-01-27 20:43:19 +03:00
{
2021-12-24 16:22:03 +03:00
SkuAmount[sku] += amount.Value;
2022-01-27 20:43:19 +03:00
}
2021-12-24 16:22:03 +03:00
else
2022-01-27 20:43:19 +03:00
{
2021-12-24 16:22:03 +03:00
SkuAmount.Add(sku, amount.Value);
2022-01-27 20:43:19 +03:00
}
2021-12-24 16:22:03 +03:00
}
}
}
}