Fix wrong article found on replaced lookup

This commit is contained in:
Sergey Chebotar 2023-05-27 08:19:10 +03:00
parent 9c3c65a4f8
commit f58dec38a4

View File

@ -3,7 +3,6 @@ using System.Runtime.Versioning;
using RhSolutions.Tools; using RhSolutions.Tools;
#endif #endif
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace RhSolutions.Services; namespace RhSolutions.Services;
@ -112,7 +111,7 @@ public class ExcelWriter : IWriter, IDisposable
Range worksheetCells = _worksheet.Cells; Range worksheetCells = _worksheet.Cells;
Range skuColumn = _skuCell.EntireColumn; Range skuColumn = _skuCell.EntireColumn;
int? row = GetPositionRow(skuColumn, positionAmount.Key.ProductSku.ToString(), positionAmount.Key.ProductLines.FirstOrDefault()); int? row = GetPositionRow(skuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLines.FirstOrDefault());
if (row != null) if (row != null)
{ {
@ -128,7 +127,7 @@ public class ExcelWriter : IWriter, IDisposable
if (_oldSkuCell != null) if (_oldSkuCell != null)
{ {
row = GetPositionRow(_oldSkuCell.EntireColumn, positionAmount.Key.ProductSku.ToString(), positionAmount.Key.ProductLines.FirstOrDefault()); row = GetPositionRow(_oldSkuCell.EntireColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLines.FirstOrDefault());
if (row != null) if (row != null)
{ {
@ -149,8 +148,7 @@ public class ExcelWriter : IWriter, IDisposable
} }
} }
string sku = positionAmount.Key.ProductSku.Article; row = GetPositionRow(skuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLines.FirstOrDefault(), true);
row = GetPositionRow(skuColumn, sku, positionAmount.Key.ProductLines.FirstOrDefault());
if (row != null) if (row != null)
{ {
@ -212,10 +210,12 @@ public class ExcelWriter : IWriter, IDisposable
} }
} }
private int? GetPositionRow(Range range, string sku, string group) private int? GetPositionRow(Range range, ProductSku sku, string productLine, bool justArticle = false)
{ {
Range found = range.Find(sku); string lookupString = justArticle ? sku.Article : sku.ToString();
Range found = range.Find(lookupString);
string foundGroupValue; string foundGroupValue;
string foundSkuValue;
if (found == null) if (found == null)
{ {
@ -224,25 +224,36 @@ public class ExcelWriter : IWriter, IDisposable
int firstFoundRow = found.Row; int firstFoundRow = found.Row;
if (string.IsNullOrEmpty(group))
{
return found.Row;
}
while (true) while (true)
{ {
foundSkuValue = _worksheet.Cells[found.Row, range.Column].Value2.ToString();
foundGroupValue = _worksheet.Cells[found.Row, _programLineCell.Column].Value2.ToString(); foundGroupValue = _worksheet.Cells[found.Row, _programLineCell.Column].Value2.ToString();
if (group.Equals(foundGroupValue)) if (ProductSku.TryParse(foundSkuValue, out var skus))
{ {
return found.Row; if (skus.Any(s => s.Article == sku.Article) &&
(string.IsNullOrEmpty(productLine) || productLine.Equals(foundGroupValue)) )
{
return found.Row;
}
else
{
found = range.FindNext(found);
if (found.Row == firstFoundRow)
{
return null;
}
}
} }
else
found = range.FindNext(found);
if (found.Row == firstFoundRow)
{ {
return null; found = range.FindNext(found);
if (found.Row == firstFoundRow)
{
return null;
}
} }
} }
} }