diff --git a/EgwCoreLib.Lux.Data/Repository/Sales/OfferRepository.cs b/EgwCoreLib.Lux.Data/Repository/Sales/OfferRepository.cs index 482b08ad..e36993a2 100644 --- a/EgwCoreLib.Lux.Data/Repository/Sales/OfferRepository.cs +++ b/EgwCoreLib.Lux.Data/Repository/Sales/OfferRepository.cs @@ -190,6 +190,9 @@ namespace EgwCoreLib.Lux.Data.Repository.Sales public async Task SaveRowsAsync(List rows) { + // Add validation for null or empty list + if (rows == null || rows.Count == 0) return false; + await using var dbCtx = await CreateContextAsync(); // Wrap in transaction for atomicity (batch update multiple rows) @@ -246,6 +249,9 @@ namespace EgwCoreLib.Lux.Data.Repository.Sales .Where(x => x.OfferID == OfferID) .ToListAsync(); + // If no rows found, nothing to update + if (offRowList.Count == 0) return false; + // recupero l'elenco degli itemGroup gestiti var itemGroupList = await dbCtx .DbSetItemGroup diff --git a/EgwCoreLib.Lux.Data/Repository/Sales/OfferRowRepository.cs b/EgwCoreLib.Lux.Data/Repository/Sales/OfferRowRepository.cs index 323cbffe..186e66de 100644 --- a/EgwCoreLib.Lux.Data/Repository/Sales/OfferRowRepository.cs +++ b/EgwCoreLib.Lux.Data/Repository/Sales/OfferRowRepository.cs @@ -110,6 +110,9 @@ namespace EgwCoreLib.Lux.Data.Repository.Sales public async Task SaveRowsAsync(List rows) { + // Add validation for null or empty list + if (rows == null || rows.Count == 0) return false; + await using var dbCtx = await CreateContextAsync(); // Wrap in transaction for atomicity (batch update multiple rows) diff --git a/EgwCoreLib.Lux.Data/Repository/Sales/OrderRepository.cs b/EgwCoreLib.Lux.Data/Repository/Sales/OrderRepository.cs index 4c23329e..d0455019 100644 --- a/EgwCoreLib.Lux.Data/Repository/Sales/OrderRepository.cs +++ b/EgwCoreLib.Lux.Data/Repository/Sales/OrderRepository.cs @@ -240,6 +240,9 @@ namespace EgwCoreLib.Lux.Data.Repository.Sales public async Task SaveRowsAsync(List rows) { + // Add validation for null or empty list + if (rows == null || rows.Count == 0) return false; + await using var dbCtx = await CreateContextAsync(); // Wrap in transaction for atomicity (batch update multiple rows) @@ -285,73 +288,61 @@ namespace EgwCoreLib.Lux.Data.Repository.Sales { await using var dbCtx = await CreateContextAsync(); - // Wrap in transaction for atomicity (multi-row update in loop) - await using var tx = dbCtx.Database.BeginTransaction(); - try + // recupero righe Orderta... + var offRowList = await dbCtx + .DbSetOrderRow + .Where(x => x.OrderID == OrderID) + .ToListAsync(); + + // If no rows found, nothing to update + if (offRowList.Count == 0) return false; + + // recupero l'elenco degli itemGroup gestiti + var itemGroupList = await dbCtx + .DbSetItemGroup + .ToListAsync(); + + // recupero il subset item da BOM / BomAlt... + var bomGenList = await dbCtx + .DbSetItem + .Where(x => (x.ItemType == Core.Enums.ItemClassType.Bom || x.ItemType == Core.Enums.ItemClassType.BomAlt)) + .ToListAsync(); + + // ciclo! + foreach (var currRec in offRowList) { - // recupero righe Orderta... - var offRowList = await dbCtx - .DbSetOrderRow - .Where(x => x.OrderID == OrderID) - .ToListAsync(); - - // recupero l'elenco degli itemGroup gestiti - var itemGroupList = await dbCtx - .DbSetItemGroup - .ToListAsync(); - - // recupero il subset item da BOM / BomAlt... - var bomGenList = await dbCtx - .DbSetItem - .Where(x => (x.ItemType == Core.Enums.ItemClassType.Bom || x.ItemType == Core.Enums.ItemClassType.BomAlt)) - .ToListAsync(); - - // ciclo! - foreach (var currRec in offRowList) + // se contiene qualcosa x BOM... + if (!string.IsNullOrEmpty(currRec.ItemBOM) && currRec.ItemBOM.Length > 2) { - // se contiene qualcosa x BOM... - if (!string.IsNullOrEmpty(currRec.ItemBOM) && currRec.ItemBOM.Length > 2) + // deserializzo + var bomList = JsonConvert.DeserializeObject>(currRec.ItemBOM); + // se ho trovato elementi... + if (bomList != null) { - // deserializzo - var bomList = JsonConvert.DeserializeObject>(currRec.ItemBOM); - // se ho trovato elementi... - if (bomList != null) - { - // calcolo il NUOVO costo e lo aggiorno... - double totCost = 0; - double totPrice = 0; - int totItemQty = 0; - int numGroupOk = 0; - int numItemOk = 0; - int numElems = bomList.Count; - // validazione e completamento BOM - BomCalculator.Validate(itemGroupList, bomGenList, ref bomList, null, ref totCost, ref totPrice, ref totItemQty, ref numGroupOk, ref numItemOk); - // salvo BOM... - string itemBom = JsonConvert.SerializeObject(bomList); - currRec.ItemBOM = itemBom; - // salvo arrotondato alla 3° decimale - currRec.BomCost = Math.Round(totCost, 3); - currRec.BomPrice = Math.Round(totPrice, 3); - currRec.BomOk = numElems == numGroupOk; - currRec.ItemOk = numElems == numItemOk; - currRec.ProdItemQty = totItemQty; - dbCtx.Entry(currRec).State = EntityState.Modified; - } + // calcolo il NUOVO costo e lo aggiorno... + double totCost = 0; + double totPrice = 0; + int totItemQty = 0; + int numGroupOk = 0; + int numItemOk = 0; + int numElems = bomList.Count; + // validazione e completamento BOM + BomCalculator.Validate(itemGroupList, bomGenList, ref bomList, null, ref totCost, ref totPrice, ref totItemQty, ref numGroupOk, ref numItemOk); + // salvo BOM... + string itemBom = JsonConvert.SerializeObject(bomList); + currRec.ItemBOM = itemBom; + // salvo arrotondato alla 3° decimale + currRec.BomCost = Math.Round(totCost, 3); + currRec.BomPrice = Math.Round(totPrice, 3); + currRec.BomOk = numElems == numGroupOk; + currRec.ItemOk = numElems == numItemOk; + currRec.ProdItemQty = totItemQty; + dbCtx.Entry(currRec).State = EntityState.Modified; } } + } - bool done = await dbCtx.SaveChangesAsync() > 0; - - if (done) - tx.Commit(); - - return done; - } - catch - { - tx.Rollback(); - throw; - } + return await dbCtx.SaveChangesAsync() > 0; } #endregion Public Methods diff --git a/EgwCoreLib.Lux.Data/Services/Sales/OfferRowService.cs b/EgwCoreLib.Lux.Data/Services/Sales/OfferRowService.cs index 2130cc30..0ef1fa25 100644 --- a/EgwCoreLib.Lux.Data/Services/Sales/OfferRowService.cs +++ b/EgwCoreLib.Lux.Data/Services/Sales/OfferRowService.cs @@ -55,22 +55,22 @@ namespace EgwCoreLib.Lux.Data.Services.Sales /// /// Key /// - public async Task FixImgTypeAsync(int offerId) +public async Task FixImgTypeAsync(int offerId) { return await TraceAsync($"{_className}.FixImgType", async (activity) => { // 1. Recupero righe var rows = await _repo.GetByParentAsync(offerId); - if (rows == null) return false; // 2. Trovo quelle da sistemare var list2fix = rows .Where(x => x.ImgType == ImageType.ND) .ToList(); - // 3. Se non c’è nulla da fare → ritorno + // 3. Se non c'è nulla da fare → ritorno (nessun cambio necessario) if (list2fix.Count == 0) - return false; + return true; + // 5. Aggiorno i record foreach (var row in list2fix)