| | |
| | | using Microsoft.Extensions.Logging; |
| | | using Autofac.Core; |
| | | using Microsoft.Extensions.Logging; |
| | | using SqlSugar; |
| | | using WIDESEA_Common.LocationEnum; |
| | | using WIDESEA_Common.OrderEnum; |
| | |
| | | |
| | | // æç©æåæ¹æ¬¡åç»å¤ç |
| | | var groupDetails = outboundOrderDetails |
| | | .GroupBy(x => new { x.MaterielCode, x.BatchNo, x.SupplyCode }) |
| | | .GroupBy(x => new { x.MaterielCode, x.BatchNo, x.SupplyCode,x.WarehouseCode }) |
| | | .Select(x => new |
| | | { |
| | | MaterielCode = x.Key.MaterielCode, |
| | | BatchNo = x.Key.BatchNo, |
| | | SupplyCode = x.Key.SupplyCode, |
| | | Details = x.ToList(), |
| | | WarehouseCode=x.Key.WarehouseCode, |
| | | TotalNeedQuantity = CalculateReassignNeedQuantity(x.ToList()) |
| | | }) |
| | | .Where(x => x.TotalNeedQuantity > 0) |
| | |
| | | foreach (var item in groupDetails) |
| | | { |
| | | var needQuantity = item.TotalNeedQuantity; |
| | | |
| | | |
| | | // è·åå¯ç¨åºåï¼æå
è¿å
åºæåºï¼ |
| | | List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, item.SupplyCode); |
| | | List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, item.SupplyCode,item.WarehouseCode, outboundOrder.FactoryArea); |
| | | |
| | | if (!stockInfos.Any()) |
| | | { |
| | |
| | | |
| | | // æ´æ°åºåºåæç»ç¶æ |
| | | UpdateOrderDetailStatus(item.Details, allocatedQuantity, needQuantity); |
| | | |
| | | |
| | | // æ´æ°åºååæçç¶æ |
| | | UpdateNormalStocksAndPalletsStatus(autoAssignStocks, stockAllocations); |
| | | } |
| | | |
| | | if (outStocks.Any()) |
| | |
| | | |
| | | return (outStocks, outboundOrderDetails, outStockLockInfos, locationInfos); |
| | | } |
| | | /// <summary> |
| | | /// æ´æ°åºååæçç¶æ |
| | | /// </summary> |
| | | private void UpdateNormalStocksAndPalletsStatus(List<Dt_StockInfo> stocks, Dictionary<int, decimal> stockAllocations) |
| | | { |
| | | foreach (var stock in stocks) |
| | | { |
| | | var stockDetail = stock.Details.First(); |
| | | if (stockAllocations.TryGetValue(stockDetail.Id, out decimal allocatedQty) && allocatedQty > 0) |
| | | { |
| | | |
| | | _stockService.StockInfoService.Db.Updateable<Dt_StockInfo>() |
| | | .SetColumns(x => new Dt_StockInfo |
| | | { |
| | | StockStatus = (int)StockStatusEmun.åºåºéå®, |
| | | }) |
| | | .Where(x => x.Id == stock.Id) |
| | | .ExecuteCommand(); |
| | | } |
| | | } |
| | | } |
| | | /// <summary> |
| | | /// æ£æ¥è®¢åæ¯å¦å
è®¸éæ°åé
|
| | | /// </summary> |
| | |
| | | |
| | | // æç©æåæ¹æ¬¡åç»å¤çï¼è¿éåªæä¸ä¸ªæç»ï¼ |
| | | var groupDetails = new List<Dt_OutboundOrderDetail> { orderDetail } |
| | | .GroupBy(x => new { x.MaterielCode, x.BatchNo, x.SupplyCode }) |
| | | .GroupBy(x => new { x.MaterielCode, x.BatchNo, x.SupplyCode ,x.WarehouseCode}) |
| | | .Select(x => new |
| | | { |
| | | MaterielCode = x.Key.MaterielCode, |
| | | BatchNo = x.Key.BatchNo, |
| | | SupplyCode = x.Key.SupplyCode, |
| | | WarehouseCode=x.Key.WarehouseCode, |
| | | Details = x.ToList(), |
| | | TotalNeedQuantity = batchQuantity // 使ç¨åæ¹æ°é |
| | | }) |
| | |
| | | var needQuantity = item.TotalNeedQuantity; |
| | | |
| | | // è·åå¯ç¨åºåï¼æå
è¿å
åºæåºï¼ |
| | | List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, item.SupplyCode); |
| | | List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, item.SupplyCode,item.WarehouseCode,outboundOrder.FactoryArea); |
| | | if (!stockInfos.Any()) |
| | | { |
| | | throw new Exception($"ç©æ[{item.MaterielCode}]æ¹æ¬¡[{item.BatchNo}]æªæ¾å°å¯åé
åºå"); |
| | |
| | | |
| | | // æ´æ°åºåºåæç»ç¶æ |
| | | UpdateOrderDetailStatus(item.Details, allocatedQuantity, needQuantity); |
| | | |
| | | // æ´æ°åºååæçç¶æä¸ºåºåºéå® |
| | | await UpdateStocksAndPalletsStatus(autoAssignStocks, stockAllocations); |
| | | } |
| | | |
| | | if (outStocks.Any()) |
| | |
| | | |
| | | return (outStocks, groupDetails.SelectMany(x => x.Details).ToList(), outStockLockInfos, locationInfos); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ´æ°åºååæçç¶æ |
| | | /// </summary> |
| | | private async Task UpdateStocksAndPalletsStatus(List<Dt_StockInfo> stocks, Dictionary<int, decimal> stockAllocations) |
| | | { |
| | | foreach (var stock in stocks) |
| | | { |
| | | var stockDetail = stock.Details.First(); |
| | | if (stockAllocations.TryGetValue(stockDetail.Id, out decimal allocatedQty) && allocatedQty > 0) |
| | | { |
| | | |
| | | await _stockService.StockInfoService.Db.Updateable<Dt_StockInfo>() |
| | | .SetColumns(x => new Dt_StockInfo |
| | | { |
| | | StockStatus = (int)StockStatusEmun.åºåºéå®, |
| | | }) |
| | | .Where(x => x.Id == stock.Id) |
| | | .ExecuteCommandAsync(); |
| | | } |
| | | } |
| | | } |
| | | /// <summary> |
| | | /// æå
è¿å
åºåååé
é宿°é |
| | | /// </summary> |
| | |
| | | if (userSelection == null) continue; |
| | | |
| | | // 计ç®è¯¥æçå®é
å¯ç¨æ°é |
| | | var availableQuantity = CalculateAvailableQuantity(stock, outboundOrderDetail.MaterielCode, |
| | | outboundOrderDetail.BatchNo, outboundOrderDetail.SupplyCode); |
| | | var availableQuantity = CalculateAvailableQuantityByBarcode(stock, outboundOrderDetail.MaterielCode, |
| | | outboundOrderDetail.BatchNo, outboundOrderDetail.SupplyCode, userSelection.Barcode); |
| | | |
| | | // ç¡®å®åé
æ°éï¼åç¨æ·éæ©æ°éãå¯ç¨æ°éåå©ä½éæ±çæå°å¼ |
| | | var assignQuantity = Math.Min(Math.Min(userSelection.UseableQuantity, availableQuantity),remainingNeedQuantity); |
| | | var assignQuantity = Math.Min(Math.Min(userSelection.UseableQuantity, availableQuantity), remainingNeedQuantity); |
| | | |
| | | if (assignQuantity <= 0) continue; |
| | | |
| | | // æ§è¡åé
|
| | | var actualAssigned = AssignStockQuantity(stock, outboundOrderDetail, assignQuantity); |
| | | // æ§è¡åé
ï¼ä½¿ç¨ç¨æ·éæ©çæ¡ç |
| | | var actualAssigned = AssignStockQuantity(stock, outboundOrderDetail, assignQuantity, userSelection.Barcode); |
| | | |
| | | if (actualAssigned > 0) |
| | | { |
| | | outStocks.Add(stock); |
| | | totalAssignedFromUserSelection += actualAssigned; |
| | | remainingNeedQuantity -= actualAssigned; |
| | | |
| | | // å建éå®è®°å½ |
| | | var lockInfo = CreateOutStockLockInfo(outboundOrder, outboundOrderDetail, stock, actualAssigned); |
| | | // å建éå®è®°å½ï¼ä½¿ç¨ç¨æ·éæ©çæ¡ç |
| | | var lockInfo = CreateOutStockLockInfo(outboundOrder, outboundOrderDetail, stock, actualAssigned, userSelection.Barcode); |
| | | outStockLockInfos.Add(lockInfo); |
| | | } |
| | | } |
| | |
| | | |
| | | if (remainingNeedQuantity > 0) |
| | | { |
| | | //// å¯éï¼è¿éå¯ä»¥æ ¹æ®ä¸å¡éæ±å³å®æ¯å¦å
许èªå¨åé
|
| | | //// å¦æè¦æ±ä¸¥æ ¼æç¨æ·éæ©åºåºï¼å¯ä»¥æåºå¼å¸¸ |
| | | //// throw new Exception($"ç¨æ·éæ©çåºåæ°éä¸è¶³ï¼è¿é{remainingNeedQuantity}ï¼è¯·éæ°éæ©"); |
| | | |
| | | //// 妿éè¦èªå¨åé
ï¼è®°å½æ¥å¿ |
| | | //_logger.LogInformation($"ç¨æ·éæ©çåºåæ°éä¸è¶³ï¼è¿é{remainingNeedQuantity}ï¼ç³»ç»å°èªå¨åé
"); |
| | | |
| | | //List<Dt_StockInfo> autoStocks = _stockService.StockInfoService.GetUseableStocks( |
| | | // outboundOrderDetail.MaterielCode, |
| | | // outboundOrderDetail.BatchNo, |
| | | // ""); |
| | | |
| | | //// æé¤ç¨æ·å·²éæ©ä¸å·²åé
çæç |
| | | //var assignedPalletCodes = outStocks.Select(x => x.PalletCode).ToList(); |
| | | //autoStocks = autoStocks |
| | | // .Where(x => !assignedPalletCodes.Contains(x.PalletCode)) |
| | | // .ToList(); |
| | | |
| | | //var (autoAssignStocks, stockAllocations) = _stockService.StockInfoService.GetOutboundStocks( |
| | | // autoStocks, |
| | | // outboundOrderDetail.MaterielCode, |
| | | // remainingNeedQuantity, |
| | | // out decimal residueQuantity); |
| | | |
| | | //if (autoAssignStocks != null && autoAssignStocks.Any()) |
| | | //{ |
| | | // outStocks.AddRange(autoAssignStocks); |
| | | |
| | | // // 为èªå¨åé
çåºåå建éå®è®°å½ |
| | | // var autoLockInfos = CreateLockInfosForAutoAssign(outboundOrder, outboundOrderDetail, autoAssignStocks, stockAllocations); |
| | | // outStockLockInfos.AddRange(autoLockInfos); |
| | | |
| | | // // æ´æ°å·²åé
æ°é |
| | | // decimal autoAssignedQuantity = remainingNeedQuantity - residueQuantity; |
| | | // totalAssignedFromUserSelection += autoAssignedQuantity; |
| | | // remainingNeedQuantity = residueQuantity; // æ´æ°å©ä½éæ± |
| | | //} |
| | | //else if (remainingNeedQuantity > 0) |
| | | //{ |
| | | // _logger.LogWarning($"èªå¨åé
失败ï¼å©ä½éæ±{remainingNeedQuantity}æ æ³æ»¡è¶³"); |
| | | //} |
| | | |
| | | } |
| | | |
| | | // æ´æ°é宿°é |
| | | outboundOrderDetail.LockQuantity += totalAssignedFromUserSelection + totalAssignedFromUserSelection; |
| | | outboundOrderDetail.LockQuantity += totalAssignedFromUserSelection; |
| | | |
| | | // æ´æ°ç¶æ |
| | | UpdateOrderDetailStatus(outboundOrderDetail, remainingNeedQuantity); |
| | |
| | | |
| | | return (outStocks, outboundOrderDetail, outStockLockInfos, locationInfos); |
| | | } |
| | | private decimal CalculateAvailableQuantityByBarcode(Dt_StockInfo stock, string materielCode, string batchNo, string supplyCode, string barcode) |
| | | { |
| | | var query = stock.Details.AsQueryable() |
| | | .Where(d => d.MaterielCode == materielCode && |
| | | (d.StockQuantity - d.OutboundQuantity) > 0 && |
| | | d.Barcode == barcode); |
| | | |
| | | if (!string.IsNullOrEmpty(batchNo)) |
| | | { |
| | | query = query.Where(x => x.BatchNo == batchNo); |
| | | } |
| | | |
| | | if (!string.IsNullOrEmpty(supplyCode)) |
| | | { |
| | | query = query.Where(d => d.SupplyCode == supplyCode); |
| | | } |
| | | |
| | | return query.Sum(d => d.StockQuantity - d.OutboundQuantity); |
| | | } |
| | | // è¾
婿¹æ³ |
| | | private decimal CalculateAvailableQuantity(Dt_StockInfo stock, string materielCode, string batchNo, string supplyCode) |
| | | { |
| | |
| | | return relevantDetails.Sum(d => d.StockQuantity - d.OutboundQuantity); |
| | | } |
| | | |
| | | private decimal AssignStockQuantity(Dt_StockInfo stock, Dt_OutboundOrderDetail detail, decimal assignQuantity) |
| | | private decimal AssignStockQuantity(Dt_StockInfo stock, Dt_OutboundOrderDetail detail, decimal assignQuantity, string barcode) |
| | | { |
| | | decimal remainingAssign = assignQuantity; |
| | | |
| | | // æå
è¿å
åºåé
æå®æ¡ç çåºåæç» |
| | | var query = stock.Details.AsQueryable() |
| | | .Where(d => d.MaterielCode == detail.MaterielCode && |
| | | (d.StockQuantity - d.OutboundQuantity) > 0 && |
| | | d.Barcode == barcode); // åªåé
æå®æ¡ç |
| | | |
| | | query = query.Where(x => x.WarehouseCode == detail.WarehouseCode); |
| | | |
| | | if (!string.IsNullOrEmpty(detail.BatchNo)) |
| | | { |
| | | query = query.Where(x => x.BatchNo == detail.BatchNo); |
| | | } |
| | | // 妿åºåºåæä¾åºåè¦æ±ï¼æä¾åºåè¿æ»¤ |
| | | if (!string.IsNullOrEmpty(detail.SupplyCode)) |
| | | { |
| | | query = query.Where(d => d.SupplyCode == detail.SupplyCode); |
| | | } |
| | | var sortedDetails = query.ToList().OrderBy(d => d.CreateDate); |
| | | |
| | | foreach (var stockDetail in sortedDetails) |
| | | { |
| | | if (remainingAssign <= 0) break; |
| | | |
| | | var available = stockDetail.StockQuantity - stockDetail.OutboundQuantity; |
| | | var assign = Math.Min(available, remainingAssign); |
| | | |
| | | stockDetail.OutboundQuantity += assign; |
| | | remainingAssign -= assign; |
| | | } |
| | | |
| | | return assignQuantity - remainingAssign; // è¿åå®é
åé
æ°é |
| | | } |
| | | |
| | | private (decimal assignedQuantity, string barcode) AssignStockQuantity(Dt_StockInfo stock, Dt_OutboundOrderDetail detail, decimal assignQuantity) |
| | | { |
| | | decimal remainingAssign = assignQuantity; |
| | | string assignedBarcode = string.Empty; |
| | | |
| | | // æå
è¿å
åºåé
åºåæç» |
| | | var query = stock.Details.AsQueryable() |
| | |
| | | |
| | | stockDetail.OutboundQuantity += assign; |
| | | remainingAssign -= assign; |
| | | if (string.IsNullOrEmpty(assignedBarcode) && assign > 0) |
| | | { |
| | | assignedBarcode = stockDetail.Barcode; |
| | | } |
| | | } |
| | | |
| | | return assignQuantity - remainingAssign; // è¿åå®é
åé
æ°é |
| | | |
| | | return (assignQuantity - remainingAssign, assignedBarcode); |
| | | } |
| | | |
| | | private Dt_OutStockLockInfo CreateOutStockLockInfo(Dt_OutboundOrder outboundOrder, Dt_OutboundOrderDetail detail, |
| | | Dt_StockInfo stock, decimal quantity) |
| | | Dt_StockInfo stock, decimal quantity,string barcode="") |
| | | { |
| | | var barcode = stock.Details |
| | | .Where(d => !string.IsNullOrEmpty(d.Barcode)) |
| | | .Select(d => d.Barcode) |
| | | .FirstOrDefault(); |
| | | |
| | | |
| | | return _outStockLockInfoService.GetOutStockLockInfo(outboundOrder, detail, stock, quantity, barcode); |
| | | } |
| | |
| | | return (false, $"æç[{selection.PalletCode}]ä¸åå¨"); |
| | | } |
| | | |
| | | var available = CalculateAvailableQuantity(stock, outboundOrderDetail.MaterielCode, |
| | | outboundOrderDetail.BatchNo, outboundOrderDetail.SupplyCode); |
| | | var available = CalculateAvailableQuantityByBarcode(stock, outboundOrderDetail.MaterielCode, |
| | | outboundOrderDetail.BatchNo, outboundOrderDetail.SupplyCode,selection.Barcode); |
| | | |
| | | if (available <= 0) |
| | | { |