RhSolutions-AddIn/src/PriceListTools/PriceListTool.cs

204 lines
7.2 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;
2022-01-28 18:15:13 +03:00
using System.Windows.Forms;
using Application = Microsoft.Office.Interop.Excel.Application;
2022-01-27 10:22:30 +03:00
namespace RehauSku.PriceListTools
{
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-28 15:38:57 +03:00
protected private List<KeyValuePair<Position, double>> Missing;
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)
{
2022-01-28 18:15:13 +03:00
MessageBox.Show
2022-01-27 10:22:30 +03:00
(ex.Message,
"Ошибка открытия шаблонного прайс-листа",
2022-01-28 18:15:13 +03:00
MessageBoxButtons.OK,
MessageBoxIcon.Information);
2022-01-27 10:22:30 +03:00
wb.Close();
2022-01-27 17:34:03 +03:00
throw ex;
2022-01-27 10:22:30 +03:00
}
}
2022-01-28 18:15:13 +03:00
protected private void FillColumnsWithDictionary(IEnumerable<KeyValuePair<Position, double>> dictionary, params int[] columns)
2022-01-27 10:22:30 +03:00
{
2022-01-28 15:38:57 +03:00
Missing = new List<KeyValuePair<Position, double>>();
2022-01-28 18:15:13 +03:00
foreach (var positionAmount in dictionary)
2022-01-27 10:22:30 +03:00
{
2022-01-28 18:15:13 +03:00
FillPositionAmountToColumns(positionAmount, columns);
2022-01-27 10:22:30 +03:00
}
2022-01-27 20:43:19 +03:00
2022-01-28 15:38:57 +03:00
if (Missing.Count > 0)
2022-01-28 18:15:13 +03:00
{
DialogResult result =
MessageBox.Show
2022-01-28 15:38:57 +03:00
($"{Missing.Count} артикулов отсутствует в таблице заказов {RegistryUtil.PriceListPath} Попробовать найти новый вариант?",
"Отсутствует позиция в конечной таблице заказов",
2022-01-28 18:15:13 +03:00
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
var lookUp = new List<KeyValuePair<Position, double>>(Missing);
foreach (var missingPosition in lookUp)
{
TryFillVariantlessSkuToColumns(missingPosition, columns);
}
}
}
if (Missing.Count > 0)
{
FillMissing(columns);
2022-01-28 18:15:13 +03:00
MessageBox.Show
($"{Missing.Count} артикулов отсутствует в таблице заказов {RegistryUtil.PriceListPath}\n" +
$"Под основной таблицей составлен список не найденных артикулов",
"Отсутствует позиция в конечной таблице заказов",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
2022-01-27 10:22:30 +03:00
}
2022-01-28 18:15:13 +03:00
protected private void FillPositionAmountToColumns(KeyValuePair<Position, double> positionAmount, int[] columns)
{
2022-01-28 18:15:13 +03:00
Range foundCell = TargetFile.skuCell.EntireColumn.Find(positionAmount.Key.Sku);
2022-01-28 16:19:39 +03:00
if (foundCell == null)
{
2022-01-28 18:15:13 +03:00
Missing.Add(positionAmount);
return;
}
2022-01-28 15:38:57 +03:00
string foundCellGroup = TargetFile.Sheet.Cells[foundCell.Row, TargetFile.groupCell.Column].Value2.ToString();
2022-01-28 18:15:13 +03:00
while (foundCell != null && foundCellGroup != positionAmount.Key.Group)
{
if (positionAmount.Key.Group == null) break;
foundCell = TargetFile.skuCell.EntireColumn.FindNext(foundCell);
2022-01-28 15:38:57 +03:00
foundCellGroup = TargetFile.Sheet.Cells[foundCell.Row, TargetFile.groupCell.Column].Value2.ToString();
}
if (foundCell == null)
{
2022-01-28 18:15:13 +03:00
Missing.Add(positionAmount);
}
else
{
foreach (var column in columns)
{
Range sumCell = TargetFile.Sheet.Cells[foundCell.Row, column];
2022-01-28 18:15:13 +03:00
if (sumCell.Value2 == null)
{
2022-01-28 18:15:13 +03:00
sumCell.Value2 = positionAmount.Value;
}
else
{
2022-01-28 18:15:13 +03:00
sumCell.Value2 += positionAmount.Value;
}
}
}
}
2022-01-28 18:15:13 +03:00
protected private void TryFillVariantlessSkuToColumns(KeyValuePair<Position, double> positionAmount, int[] columns)
{
string sku = positionAmount.Key.Sku.Substring(1, 6);
Range foundCell = TargetFile.skuCell.EntireColumn.Find(sku);
if (foundCell == null)
{
return;
}
string foundCellGroup = TargetFile.Sheet.Cells[foundCell.Row, TargetFile.groupCell.Column].Value2.ToString();
while (foundCell != null && foundCellGroup != positionAmount.Key.Group)
{
if (positionAmount.Key.Group == null) break;
2022-01-28 18:15:13 +03:00
foundCell = TargetFile.skuCell.EntireColumn.FindNext(foundCell);
foundCellGroup = TargetFile.Sheet.Cells[foundCell.Row, TargetFile.groupCell.Column].Value2.ToString();
}
if (foundCell == null)
{
return;
}
foreach (var column in columns)
{
Range sumCell = TargetFile.Sheet.Cells[foundCell.Row, column];
if (sumCell.Value2 == null)
{
sumCell.Value2 = positionAmount.Value;
}
else
{
sumCell.Value2 += positionAmount.Value;
}
}
Missing.Remove(positionAmount);
}
protected private void FillMissing(int[] columns)
2022-01-28 18:15:13 +03:00
{
int startRow =
TargetFile.Sheet.AutoFilter.Range.Row +
TargetFile.Sheet.AutoFilter.Range.Rows.Count + 5;
for (int i = 0; i < Missing.Count; i++)
{
Range group = TargetFile.Sheet.Cells[startRow + i, TargetFile.groupCell.Column];
Range sku = TargetFile.Sheet.Cells[startRow + i, TargetFile.skuCell.Column];
Range name = TargetFile.Sheet.Cells[startRow + i, TargetFile.nameCell.Column];
group.Value2 = Missing[i].Key.Group;
sku.Value2 = Missing[i].Key.Sku;
name.Value2 = Missing[i].Key.Name;
group.ClearFormats();
sku.ClearFormats();
name.ClearFormats();
foreach (int column in columns)
{
Range amount = TargetFile.Sheet.Cells[startRow + i, column];
amount.Value2 = Missing[i].Value;
amount.ClearFormats();
}
2022-01-28 18:15:13 +03:00
}
}
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-28 18:46:31 +03:00
int startColumn = filter.Range.Column;
2022-01-27 10:22:30 +03:00
2022-01-28 18:46:31 +03:00
filter.Range.AutoFilter(TargetFile.amountCell.Column - startColumn + 1, "<>");
2022-01-27 17:34:03 +03:00
TargetFile.Sheet.Range["A1"].Activate();
2022-01-27 10:22:30 +03:00
}
}
}