Fix controli null ingresso

This commit is contained in:
Samuele E. Locatelli (W11-AI)
2026-03-18 18:11:29 +01:00
parent 5593ddc7fc
commit 2c1bf139c9
4 changed files with 65 additions and 65 deletions
@@ -190,6 +190,9 @@ namespace EgwCoreLib.Lux.Data.Repository.Sales
public async Task<bool> SaveRowsAsync(List<OfferRowModel> 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
@@ -110,6 +110,9 @@ namespace EgwCoreLib.Lux.Data.Repository.Sales
public async Task<bool> SaveRowsAsync(List<OfferRowModel> 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)
@@ -240,6 +240,9 @@ namespace EgwCoreLib.Lux.Data.Repository.Sales
public async Task<bool> SaveRowsAsync(List<OrderRowModel> 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<List<BomItemDTO>>(currRec.ItemBOM);
// se ho trovato elementi...
if (bomList != null)
{
// deserializzo
var bomList = JsonConvert.DeserializeObject<List<BomItemDTO>>(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
@@ -55,22 +55,22 @@ namespace EgwCoreLib.Lux.Data.Services.Sales
/// </summary>
/// <param name="offerId">Key</param>
/// <returns></returns>
public async Task<bool> FixImgTypeAsync(int offerId)
public async Task<bool> 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)