RhSolutions-AddIn/src/PriceListTools/AbstractTool.cs

189 lines
5.9 KiB
C#
Raw Normal View History

2022-02-12 16:08:01 +03:00
using Microsoft.Office.Interop.Excel;
2022-02-04 09:17:12 +03:00
using RehauSku.Interface;
2022-01-27 10:22:30 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
2022-02-04 09:17:12 +03:00
using ProgressBar = RehauSku.Interface.ProgressBar;
2022-01-27 10:22:30 +03:00
namespace RehauSku.PriceListTools
{
2022-02-02 18:02:17 +03:00
internal abstract class AbstractTool
2022-01-27 10:22:30 +03:00
{
2022-02-12 16:08:01 +03:00
protected Application ExcelApp = AddIn.Excel;
protected TargetPriceList TargetFile { get; set; }
protected ResultBar ResultBar { get; set; }
protected ProgressBar ProgressBar { get; set; }
public abstract void FillTarget();
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
{
if (ExcelApp.Workbooks
.Cast<Workbook>()
.FirstOrDefault(w => w.FullName == RegistryUtil.PriceListPath) != null)
{
throw new ArgumentException("Шаблонный файл редактируется в другом месте");
}
2022-02-08 16:06:30 +03:00
Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath, null, true);
2022-01-27 10:22:30 +03:00
try
{
2022-02-02 18:05:49 +03:00
TargetFile = new TargetPriceList(wb);
2022-01-27 10:22:30 +03:00
}
2022-02-08 16:46:53 +03:00
catch (Exception exception)
2022-01-27 10:22:30 +03:00
{
2022-02-08 16:46:53 +03:00
if (wb != null)
{
wb.Close();
}
throw exception;
2022-01-27 10:22:30 +03:00
}
}
protected void FillPositionAmountToColumns(KeyValuePair<Position, double> positionAmount, params int[] columns)
2022-01-27 10:22:30 +03:00
{
2022-03-24 06:51:55 +03:00
Range worksheetCells = TargetFile.Sheet.Cells;
Range skuColumn = TargetFile.SkuCell.EntireColumn;
Range oldSkuColumn = TargetFile.OldSkuCell.EntireColumn;
int? row = GetPositionRow(skuColumn, positionAmount.Key.Sku, positionAmount.Key.Group);
if (row != null)
2022-01-27 10:22:30 +03:00
{
foreach (int column in columns)
2022-01-28 18:15:13 +03:00
{
2022-03-24 06:51:55 +03:00
Range cell = worksheetCells[row, column];
2022-02-08 16:27:53 +03:00
cell.AddValue(positionAmount.Value);
2022-01-28 18:15:13 +03:00
}
2022-02-04 09:17:12 +03:00
ResultBar.IncrementSuccess();
return;
2022-01-28 18:15:13 +03:00
}
2022-02-12 16:53:34 +03:00
if (TargetFile.OldSkuCell != null)
2022-01-28 18:15:13 +03:00
{
2022-03-24 06:51:55 +03:00
row = GetPositionRow(oldSkuColumn, positionAmount.Key.Sku, positionAmount.Key.Group);
2022-01-28 16:19:39 +03:00
2022-02-08 16:27:53 +03:00
if (row != null)
{
foreach (int column in columns)
{
2022-03-24 06:51:55 +03:00
Range cell = worksheetCells[row, column];
2022-02-08 16:27:53 +03:00
cell.AddValue(positionAmount.Value);
}
2022-02-04 09:17:12 +03:00
ResultBar.IncrementReplaced();
return;
}
2022-02-04 09:17:12 +03:00
}
string sku = positionAmount.Key.Sku.Substring(1, 6);
2022-03-24 06:51:55 +03:00
row = GetPositionRow(skuColumn, sku, positionAmount.Key.Group);
2022-02-04 09:17:12 +03:00
if (row != null)
{
foreach (int column in columns)
2022-02-08 16:27:53 +03:00
{
2022-03-24 06:51:55 +03:00
Range cell = worksheetCells[row, column];
cell.AddValue(positionAmount.Value);
}
2022-02-04 09:17:12 +03:00
ResultBar.IncrementReplaced();
return;
}
FillMissing(positionAmount, columns);
ResultBar.IncrementNotFound();
}
protected void FillMissing(KeyValuePair<Position, double> positionAmount, params int[] columns)
2022-01-28 18:15:13 +03:00
{
2022-03-24 06:51:55 +03:00
Range worksheetCells = TargetFile.Sheet.Cells;
Range worksheetRows = TargetFile.Sheet.Rows;
int skuColumn = TargetFile.SkuCell.Column;
int groupColumn = TargetFile.GroupCell.Column;
int nameColumn = TargetFile.NameCell.Column;
int row = worksheetCells[worksheetRows.Count, skuColumn]
2022-02-04 09:17:12 +03:00
.End[XlDirection.xlUp]
.Row + 1;
2022-01-28 18:15:13 +03:00
2022-03-24 06:51:55 +03:00
worksheetRows[row]
2022-02-04 09:17:12 +03:00
.EntireRow
.Insert(XlInsertShiftDirection.xlShiftDown, XlInsertFormatOrigin.xlFormatFromLeftOrAbove);
2022-01-28 18:15:13 +03:00
2022-03-24 06:51:55 +03:00
Range previous = worksheetRows[row - 1];
Range current = worksheetRows[row];
2022-01-28 18:15:13 +03:00
2022-02-04 09:17:12 +03:00
previous.Copy(current);
current.ClearContents();
2022-03-24 06:51:55 +03:00
worksheetCells[row, groupColumn].Value2 = positionAmount.Key.Group;
worksheetCells[row, nameColumn].Value2 = positionAmount.Key.Name;
2022-02-12 16:53:34 +03:00
if (TargetFile.OldSkuCell != null)
2022-02-04 09:17:12 +03:00
{
2022-03-24 06:51:55 +03:00
worksheetCells[row, skuColumn].Value2 = "Не найден";
worksheetCells[row, TargetFile.OldSkuCell.Column].Value2 = positionAmount.Key.Sku;
2022-01-28 18:15:13 +03:00
}
else
2022-01-28 18:15:13 +03:00
{
2022-03-24 06:51:55 +03:00
worksheetCells[row, skuColumn].Value2 = positionAmount.Key.Sku;
2022-01-28 18:15:13 +03:00
}
foreach (int column in columns)
2022-01-28 18:15:13 +03:00
{
2022-03-24 06:51:55 +03:00
Range cell = worksheetCells[row, column];
2022-02-08 16:27:53 +03:00
cell.AddValue(positionAmount.Value);
2022-01-28 18:15:13 +03:00
}
}
protected int? GetPositionRow(Range range, string sku, string group)
2022-01-28 18:15:13 +03:00
{
Range found = range.Find(sku);
2022-03-23 17:56:46 +03:00
string foundGroupValue;
2022-01-28 18:15:13 +03:00
if (found == null)
{
return null;
}
2022-01-28 18:15:13 +03:00
2022-03-23 17:56:46 +03:00
int firstFoundRow = found.Row;
if (string.IsNullOrEmpty(group))
{
return found.Row;
}
while (true)
{
2022-03-23 17:56:46 +03:00
foundGroupValue = TargetFile.Sheet.Cells[found.Row, TargetFile.GroupCell.Column].Value2.ToString();
2022-03-23 17:56:46 +03:00
if (group.Equals(foundGroupValue))
{
return found.Row;
}
found = range.FindNext(found);
if (found.Row == firstFoundRow)
{
return null;
}
}
2022-01-28 18:15:13 +03:00
}
protected void FilterByAmount()
2022-01-27 10:22:30 +03:00
{
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-02-12 16:53:34 +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
}
}
}