RhSolutions-AddIn/src/PriceListTools/AbstractTool.cs

176 lines
5.7 KiB
C#
Raw Normal View History

2022-01-27 10:22:30 +03:00
using ExcelDna.Integration;
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-01-28 18:15:13 +03:00
using Application = Microsoft.Office.Interop.Excel.Application;
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
{
protected Application ExcelApp = (Application)ExcelDnaUtil.Application;
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
{
int? row = GetPositionRow(TargetFile.skuCell.EntireColumn, 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-02-08 16:27:53 +03:00
Range cell = TargetFile.Sheet.Cells[row, column];
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
}
if (TargetFile.oldSkuCell != null)
2022-01-28 18:15:13 +03:00
{
row = GetPositionRow(TargetFile.oldSkuCell.EntireColumn, 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-02-08 16:27:53 +03:00
Range cell = TargetFile.Sheet.Cells[row, column];
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);
row = GetPositionRow(TargetFile.skuCell.EntireColumn, 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
{
Range cell = TargetFile.Sheet.Cells[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-02-04 09:17:12 +03:00
int row = TargetFile.Sheet.Cells[TargetFile.Sheet.Rows.Count, TargetFile.skuCell.Column]
.End[XlDirection.xlUp]
.Row + 1;
2022-01-28 18:15:13 +03:00
2022-02-04 09:17:12 +03:00
TargetFile.Sheet.Rows[row]
.EntireRow
.Insert(XlInsertShiftDirection.xlShiftDown, XlInsertFormatOrigin.xlFormatFromLeftOrAbove);
2022-01-28 18:15:13 +03:00
2022-02-04 09:17:12 +03:00
Range previous = TargetFile.Sheet.Rows[row - 1];
Range current = TargetFile.Sheet.Rows[row];
2022-01-28 18:15:13 +03:00
2022-02-04 09:17:12 +03:00
previous.Copy(current);
current.ClearContents();
2022-02-04 09:17:12 +03:00
TargetFile.Sheet.Cells[row, TargetFile.groupCell.Column].Value2 = positionAmount.Key.Group;
TargetFile.Sheet.Cells[row, TargetFile.nameCell.Column].Value2 = positionAmount.Key.Name;
2022-02-04 09:17:12 +03:00
if (TargetFile.oldSkuCell != null)
{
TargetFile.Sheet.Cells[row, TargetFile.skuCell.Column].Value2 = "Не найден";
2022-02-04 09:17:12 +03:00
TargetFile.Sheet.Cells[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-02-04 09:17:12 +03:00
TargetFile.Sheet.Cells[row, TargetFile.skuCell.Column].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-02-08 16:27:53 +03:00
Range cell = TargetFile.Sheet.Cells[row, column];
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);
string foundGroupValue;
2022-01-28 18:15:13 +03:00
if (found == null)
{
return null;
}
2022-01-28 18:15:13 +03:00
int firstFoundRow = found.Row;
while (true)
{
foundGroupValue = TargetFile.Sheet.Cells[found.Row, TargetFile.groupCell.Column].Value2.ToString();
if (string.IsNullOrEmpty(group) || 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-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
}
}
}