diff --git a/server/AyaNova/biz/PMBiz.cs b/server/AyaNova/biz/PMBiz.cs index 4c9e81e0..32732c35 100644 --- a/server/AyaNova/biz/PMBiz.cs +++ b/server/AyaNova/biz/PMBiz.cs @@ -4819,35 +4819,42 @@ namespace AyaNova.Biz //message = list: part name, -xx (short quantity), no text if possible so no translate needed //collect the parts on the pm - List PartStockRequired = new List(); + List PartsOnPM = new List(); foreach (PMItem pmi in p.Items) { foreach (PMItemPart pmp in pmi.Parts) { - var CurrentInventory = await ct.PartInventory.AsNoTracking().OrderByDescending(m => m.EntryDate).FirstOrDefaultAsync(m => m.PartId == pmp.PartId && m.PartWarehouseId == pmp.PartWarehouseId); - if (CurrentInventory.Balance < pmp.Quantity) + if (pmp.Quantity > 0) { - var i = new PMRestockListItem() { PartId = pmp.PartId, WarehouseId = pmp.PartWarehouseId, QuantityRequired = pmp.Quantity - (decimal)CurrentInventory.Balance }; - PartStockRequired.Add(i); + var i = new PMRestockListItem() { PartId = pmp.PartId, WarehouseId = pmp.PartWarehouseId, QuantityRequired = pmp.Quantity }; + PartsOnPM.Add(i); } } } - if (PartStockRequired.Count == 0) continue; + if (PartsOnPM.Count == 0) continue; - //summarize and notify - var sumList = PartStockRequired.GroupBy(x => new { x.WarehouseId, x.PartId }) + //group parts and whs, summarize totals + var SummarizedPartsOnPM = PartsOnPM.GroupBy(x => new { x.WarehouseId, x.PartId }) .OrderBy(g => g.Key.PartId) .ThenBy(g => g.Key.WarehouseId) .Select(cl => new PMRestockListItem() { PartId = cl.First().PartId, WarehouseId = cl.First().WarehouseId, QuantityRequired = cl.Sum(c => c.QuantityRequired) }); //ok, should have all summarized partid/warehouseid required combos, can now build output System.Text.StringBuilder sb = new System.Text.StringBuilder(); - foreach (var i in sumList) + foreach (var i in SummarizedPartsOnPM) { - var part = await ct.Part.AsNoTracking().Where(x => x.Id == i.PartId).Select(x => x.PartNumber).FirstOrDefaultAsync(); - var whs = await ct.PartWarehouse.AsNoTracking().Where(x => x.Id == i.WarehouseId).Select(x => x.Name).FirstOrDefaultAsync(); - sb.AppendLine($"{part} {whs} - {i.QuantityRequired}"); + //check inventory and add to sb if necessary + var CurrentInventory = await ct.PartInventory.AsNoTracking().OrderByDescending(m => m.EntryDate).FirstOrDefaultAsync(m => m.PartId == i.PartId && m.PartWarehouseId == i.WarehouseId); + decimal dBalance = 0; + if (CurrentInventory != null) + dBalance = CurrentInventory.Balance; + if (dBalance < i.QuantityRequired) + { + var part = await ct.Part.AsNoTracking().Where(x => x.Id == i.PartId).Select(x => x.PartNumber).FirstOrDefaultAsync(); + var whs = await ct.PartWarehouse.AsNoTracking().Where(x => x.Id == i.WarehouseId).Select(x => x.Name).FirstOrDefaultAsync(); + sb.AppendLine($"{part} @ {whs}: {i.QuantityRequired - dBalance}"); + } } if (sb.Length > 0) { @@ -5151,10 +5158,14 @@ namespace AyaNova.Biz { //not already requested, so check inventory, this is new var CurrentInventory = await ct.PartInventory.AsNoTracking().OrderByDescending(m => m.EntryDate).FirstOrDefaultAsync(m => m.PartId == pmp.PartId && m.PartWarehouseId == pmp.PartWarehouseId); - if (CurrentInventory.Balance < pmp.Quantity) + decimal dBalance = 0; + if (CurrentInventory != null)//can be null if it has no opening balance for that warehouse yet + dBalance = CurrentInventory.Balance; + + if (dBalance < pmp.Quantity) { //we will need a part request here and also need to reserve what there is available - wipQuantity = (decimal)CurrentInventory.Balance; + wipQuantity = dBalance; requestQuantity = pmp.Quantity - wipQuantity; } }