RhSolutions-AddIn/src/PriceListTools/PriceListTool.cs

103 lines
3.1 KiB
C#
Raw Normal View History

2022-01-27 10:22:30 +03:00
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
2022-01-27 10:22:30 +03:00
namespace RehauSku.PriceListTools
{
2022-01-28 09:08:35 +03:00
internal class ConvertTool : PriceListTool
{
private Source Current;
public void GetCurrent()
{
Current = new Source(ExcelApp.ActiveWorkbook);
}
public void FillTarget()
{
ExcelApp.ScreenUpdating = false;
FillColumn(Current.SkuAmount, TargetFile.amountCell.Column);
FilterByAmount();
ExcelApp.ScreenUpdating = true;
Forms.Dialog.SaveWorkbookAs();
}
}
2022-01-27 17:34:03 +03:00
internal abstract class PriceListTool
2022-01-27 10:22:30 +03:00
{
2022-01-27 17:34:03 +03:00
protected private Application ExcelApp = (Application)ExcelDnaUtil.Application;
protected private Target TargetFile;
2022-01-27 10:22:30 +03:00
2022-01-27 17:34:03 +03:00
public void OpenNewPrice()
2022-01-27 10:22:30 +03:00
{
2022-01-27 17:34:03 +03:00
Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath);
2022-01-27 10:22:30 +03:00
try
{
2022-01-27 17:34:03 +03:00
TargetFile = new Target(wb);
2022-01-27 10:22:30 +03:00
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show
(ex.Message,
"Ошибка открытия шаблонного прайс-листа",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
wb.Close();
2022-01-27 17:34:03 +03:00
throw ex;
2022-01-27 10:22:30 +03:00
}
}
2022-01-27 20:43:19 +03:00
protected private void FillColumn(Dictionary<string, double> dictionary, int column)
2022-01-27 10:22:30 +03:00
{
List<KeyValuePair<string, double>> missing = new List<KeyValuePair<string, double>>();
2022-01-27 20:43:19 +03:00
foreach (var kvp in dictionary)
2022-01-27 10:22:30 +03:00
{
2022-01-27 20:43:19 +03:00
Range cell = TargetFile.Sheet.Columns[TargetFile.skuCell.Column].Find(kvp.Key);
2022-01-27 10:22:30 +03:00
2022-01-27 20:43:19 +03:00
if (cell == null)
2022-01-27 10:22:30 +03:00
{
missing.Add(kvp);
2022-01-27 20:43:19 +03:00
}
else
{
Range sumCell = TargetFile.Sheet.Cells[cell.Row, column];
2022-01-27 10:22:30 +03:00
2022-01-27 20:43:19 +03:00
if (sumCell.Value2 == null)
2022-01-27 10:22:30 +03:00
{
2022-01-27 20:43:19 +03:00
sumCell.Value2 = kvp.Value;
2022-01-27 10:22:30 +03:00
}
else
{
2022-01-27 20:43:19 +03:00
sumCell.Value2 += kvp.Value;
2022-01-27 10:22:30 +03:00
}
}
}
2022-01-27 20:43:19 +03:00
if (missing.Count > 0)
{
System.Windows.Forms.MessageBox.Show
($"{missing.Count} артикулов отсутствует в таблице заказов {RegistryUtil.PriceListPath}",
"Отсутствует позиция в конечной таблице заказов",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
}
2022-01-27 10:22:30 +03:00
}
protected private void FilterByAmount()
{
2022-01-27 17:34:03 +03:00
AutoFilter filter = TargetFile.Sheet.AutoFilter;
2022-01-27 10:22:30 +03:00
2022-01-27 17:34:03 +03:00
filter.Range.AutoFilter(TargetFile.amountCell.Column, "<>");
TargetFile.Sheet.Range["A1"].Activate();
2022-01-27 10:22:30 +03:00
}
}
}