From f58dec38a4cc1a8ea0b3d9208e2bc8f73d77add5 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Sat, 27 May 2023 08:19:10 +0300 Subject: [PATCH] Fix wrong article found on replaced lookup --- RhSolutions.AddIn/Services/ExcelWriter.cs | 49 ++++++++++++++--------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/RhSolutions.AddIn/Services/ExcelWriter.cs b/RhSolutions.AddIn/Services/ExcelWriter.cs index 8267627..99018b0 100644 --- a/RhSolutions.AddIn/Services/ExcelWriter.cs +++ b/RhSolutions.AddIn/Services/ExcelWriter.cs @@ -3,7 +3,6 @@ using System.Runtime.Versioning; using RhSolutions.Tools; #endif - using System.Text.RegularExpressions; namespace RhSolutions.Services; @@ -112,7 +111,7 @@ public class ExcelWriter : IWriter, IDisposable Range worksheetCells = _worksheet.Cells; 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) { @@ -128,7 +127,7 @@ public class ExcelWriter : IWriter, IDisposable 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) { @@ -149,8 +148,7 @@ public class ExcelWriter : IWriter, IDisposable } } - string sku = positionAmount.Key.ProductSku.Article; - row = GetPositionRow(skuColumn, sku, positionAmount.Key.ProductLines.FirstOrDefault()); + row = GetPositionRow(skuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLines.FirstOrDefault(), true); 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 foundSkuValue; if (found == null) { @@ -224,25 +224,36 @@ public class ExcelWriter : IWriter, IDisposable int firstFoundRow = found.Row; - if (string.IsNullOrEmpty(group)) - { - return found.Row; - } - while (true) { + foundSkuValue = _worksheet.Cells[found.Row, range.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; + } + } } - - found = range.FindNext(found); - - if (found.Row == firstFoundRow) + else { - return null; + found = range.FindNext(found); + + if (found.Row == firstFoundRow) + { + return null; + } } } }