z8018
2025-12-17 53719f76ffb7a71db70c8b71bb5dd975904fbc83
ÏîÄ¿´úÂë/WMSÎÞ²Ö´¢°æ/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundService.cs
@@ -813,6 +813,332 @@
        }
        #endregion
        public WebResponseContent CompleteOutboundWithPallet(OutboundCompletePalletRequestDTO request)
        {
            WebResponseContent content = WebResponseContent.Instance;
            OutboundCompleteResponseDTO response = new();
            try
            {
                // 1. æ ¹æ®æ‰˜ç›˜å·æŸ¥æ‰¾åº“存信息
                Dt_StockInfo stockInfo = _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletCode == request.PalletCode).Includes(x => x.Details).First();
                if (stockInfo == null)
                {
                    response.Success = false;
                    response.Message = $"托盘号 {request.PalletCode} å¯¹åº”的库存不存在";
                    return WebResponseContent.Instance.Error(response.Message);
                }
                if (!stockInfo.Details.Any())
                {
                    response.Success = false;
                    response.Message = $"托盘 {request.PalletCode} å¯¹åº”的库存明细不存在";
                    return WebResponseContent.Instance.Error(response.Message);
                }
                bool isMatMixed = stockInfo.Details.GroupBy(x => new
                {
                    x.MaterielCode,
                    x.MaterielName,
                    x.BatchNo,
                    x.SupplyCode,
                    x.WarehouseCode
                }).Count() > 1;
                if (isMatMixed)
                {
                    response.Success = false;
                    response.Message = $"混料托盘 {request.PalletCode} ä¸èƒ½æ•´ç®±å‡ºåº“";
                    return WebResponseContent.Instance.Error(response.Message);
                }
                // 2. æŸ¥æ‰¾å‡ºåº“单信息
                Dt_OutboundOrder outboundOrder = _outboundRepository.QueryFirst(o => o.OrderNo == request.OrderNo);
                if (outboundOrder == null)
                {
                    response.Success = false;
                    response.Message = $"出库单 {request.OrderNo} ä¸å­˜åœ¨";
                    return WebResponseContent.Instance.Error(response.Message);
                }
                Dt_StockInfoDetail stockInfoDetail = stockInfo.Details.First();
                // 3. æŸ¥æ‰¾é”å®šè®°å½•
                Dt_OutStockLockInfo lockInfo = _outboundLockInfoRepository.QueryFirst(x =>
                    x.OrderNo == request.OrderNo &&
                    x.StockId == stockInfo.Id &&
                    x.MaterielCode == stockInfoDetail.MaterielCode &&
                    x.PalletCode == stockInfo.PalletCode);
                if (lockInfo == null || lockInfo.AssignQuantity <= 0)
                {
                    response.Success = false;
                    response.Message = $"该库存没有分配出库量,托盘号:{request.PalletCode}";
                    return WebResponseContent.Instance.Error(response.Message);
                }
                // æ‰¾å‡ºå·²åˆ†é…çš„订单明细Id
                List<int> detailIds = new List<int>();
                string[] ids = lockInfo.OrderDetailIds.Split(",");
                foreach (string id in ids)
                {
                    if (int.TryParse(id, out int detailId))
                    {
                        detailIds.Add(detailId);
                    }
                }
                // 4. æŸ¥æ‰¾å‡ºåº“单明细信息
                List<Dt_OutboundOrderDetail> outboundOrderDetails = FindMatchingOutboundDetails(outboundOrder.Id, stockInfoDetail, detailIds);
                if (!outboundOrderDetails.Any())
                {
                    response.Success = false;
                    response.Message = $"未找到匹配的出库单明细,物料:{stockInfoDetail.MaterielCode},批次:{stockInfoDetail.BatchNo}";
                    return WebResponseContent.Instance.Error(response.Message);
                }
                decimal totalStockQuantity = stockInfo.Details.Sum(x => x.StockQuantity);
                // 5. è®¡ç®—实际出库量
                decimal actualOutboundQuantity = CalculateActualOutboundQuantity(stockInfo.Details, outboundOrderDetails, lockInfo);// éœ€å‡ºåº“量
                if (actualOutboundQuantity <= 0)
                {
                    decimal totalAllocatedQuantity = lockInfo.AllocatedQuantity;
                    decimal availableOutboundQuantity = lockInfo.AssignQuantity - totalAllocatedQuantity;
                    decimal detailRemainingQuantity = outboundOrderDetails.Sum(x => x.OrderQuantity - x.OverOutQuantity - x.MoveQty);
                    response.Success = false;
                    response.Message = $"无法出库,托盘号:{request.PalletCode},库存量:{totalStockQuantity},已出库:{totalAllocatedQuantity},分配量:{lockInfo.AssignQuantity},明细剩余:{detailRemainingQuantity}";
                    return WebResponseContent.Instance.Error(response.Message);
                }
                if (lockInfo.AssignQuantity != totalStockQuantity)
                {
                    response.Success = false;
                    response.Message = $"无法出库,托盘号:{request.PalletCode},库存量:{totalStockQuantity},分配量:{lockInfo.AssignQuantity}";
                    return WebResponseContent.Instance.Error(response.Message);
                }
                // 6. å¼€å¯äº‹åŠ¡
                _unitOfWorkManage.BeginTran();
                try
                {
                    // æ•´ç®±å‡ºåº“无需拆包
                    PerformFullOutboundOperation(stockInfo, request, lockInfo.TaskNum.GetValueOrDefault());
                    decimal allocatedQuantity = actualOutboundQuantity;
                    List<Dt_OutboundOrderDetail> updateDetails = new();
                    foreach (var item in outboundOrderDetails)
                    {
                        if (allocatedQuantity <= 0) break;
                        //if (item.OrderQuantity - item.MoveQty - item.OverOutQuantity >= allocatedQuantity)
                        //{
                        //    item.OverOutQuantity += allocatedQuantity;
                        //    allocatedQuantity = 0;
                        //}
                        //else
                        //{
                        //    allocatedQuantity -= (item.OrderQuantity - item.MoveQty - item.OverOutQuantity);
                        //    item.OverOutQuantity = item.OrderQuantity - item.MoveQty;
                        //}
                        List<Barcodes> barcodesList = new List<Barcodes>();
                        List<Dt_StockInfoDetail> stockInfoDetails = stockInfo.Details.Where((x => x.StockQuantity > x.OutboundQuantity)).ToList();
                        foreach (var stockDetail in stockInfoDetails)
                        {
                            if (item.LockQuantity - item.OverOutQuantity >= stockDetail.StockQuantity - stockInfoDetail.OutboundQuantity)
                            {
                                Barcodes barcodes = new Barcodes
                                {
                                    Barcode = stockDetail.Barcode,
                                    Qty = stockDetail.StockQuantity - stockInfoDetail.OutboundQuantity,
                                    SupplyCode = stockDetail?.SupplyCode ?? "",
                                    BatchNo = stockDetail?.BatchNo ?? "",
                                    Unit = stockDetail?.Unit ?? ""
                                };
                                stockDetail.StockQuantity = stockInfoDetail.OutboundQuantity;
                                barcodesList.Add(barcodes);
                            }
                            else
                            {
                                Barcodes barcodes = new Barcodes
                                {
                                    Barcode = stockDetail.Barcode,
                                    Qty = item.LockQuantity - item.OverOutQuantity,
                                    SupplyCode = stockDetail?.SupplyCode ?? "",
                                    BatchNo = stockDetail?.BatchNo ?? "",
                                    Unit = stockDetail?.Unit ?? ""
                                };
                                stockInfoDetail.OutboundQuantity += item.LockQuantity - item.OverOutQuantity;
                                barcodesList.Add(barcodes);
                            }
                        }
                        decimal barcodeQuantity = allocatedQuantity;
                        if (item.LockQuantity - item.OverOutQuantity >= allocatedQuantity)
                        {
                            item.OverOutQuantity += allocatedQuantity;
                            item.CurrentDeliveryQty += allocatedQuantity;
                            allocatedQuantity = 0;
                        }
                        else
                        {
                            barcodeQuantity = item.LockQuantity - item.OverOutQuantity;
                            allocatedQuantity -= (item.LockQuantity - item.OverOutQuantity);
                            item.OverOutQuantity = item.LockQuantity;
                            item.CurrentDeliveryQty = item.LockQuantity;
                        }
                        updateDetails.Add(item);
                        if (!string.IsNullOrEmpty(item.ReturnJsonData))
                        {
                            barcodesList.AddRange(JsonConvert.DeserializeObject<List<Barcodes>>(item.ReturnJsonData) ?? new List<Barcodes>());
                        }
                        JsonSerializerSettings settings = new JsonSerializerSettings
                        {
                            ContractResolver = new CamelCasePropertyNamesContractResolver()
                        };
                        item.ReturnJsonData = JsonConvert.SerializeObject(barcodesList, settings);
                    }
                    lockInfo.SortedQuantity = lockInfo.SortedQuantity + actualOutboundQuantity;
                    if (lockInfo.SortedQuantity == lockInfo.AssignQuantity)
                    {
                        _outboundLockInfoRepository.DeleteAndMoveIntoHty(lockInfo, WIDESEA_Core.Enums.OperateTypeEnum.自动完成);
                    }
                    else
                    {
                        // æ›´æ–°é”å®šè®°å½•
                        _outboundLockInfoRepository.UpdateData(lockInfo);
                    }
                    // æ›´æ–°å‡ºåº“单明细的已出库数量
                    _detailRepository.UpdateData(updateDetails);
                    // æ›´æ–°é”å®šè®°å½•的累计已出库数量(需要更新该托盘该物料的所有相关记录)
                    //UpdateLockInfoAllocatedQuantity(stockInfo.Id, stockDetail.MaterielCode, stockDetail.BatchNo, actualOutboundQuantity);
                    // æäº¤äº‹åŠ¡
                    _unitOfWorkManage.CommitTran();
                    response.Success = true;
                    response.Message = "出库完成";
                    response.UpdatedDetails = updateDetails;
                    // æ£€æŸ¥å‡ºåº“单是否完成
                    if (CheckOutboundOrderCompleted(request.OrderNo))
                    {
                        UpdateOutboundOrderStatus(request.OrderNo, OutOrderStatusEnum.出库完成.ObjToInt());
                        //todo: å›žä¼ MES
                    }
                }
                catch (Exception ex)
                {
                    _unitOfWorkManage.RollbackTran();
                    response.Success = false;
                    response.Message = $"出库处理失败:{ex.Message}";
                    return WebResponseContent.Instance.Error(ex.Message);
                }
                content = WebResponseContent.Instance.OK(data: response);
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error("处理出库完成失败:" + ex.Message);
            }
            return content;
        }
        /// <summary>
        /// è®¡ç®—实际出库数量
        /// </summary>
        private decimal CalculateActualOutboundQuantity(List<Dt_StockInfoDetail> stockDetails, List<Dt_OutboundOrderDetail> outboundDetails, Dt_OutStockLockInfo lockInfo)
        {
            decimal availableOutboundQuantity = lockInfo.AssignQuantity;
            decimal detailRemainingQuantity = outboundDetails.Sum(x => x.OrderQuantity - x.OverOutQuantity - x.MoveQty);//outboundDetail.OrderQuantity - outboundDetail.OverOutQuantity;
            return Math.Min(
                Math.Min(availableOutboundQuantity, detailRemainingQuantity),
                stockDetails.Sum(x => x.StockQuantity));
        }
        /// <summary>
        /// æ‰§è¡Œå®Œæ•´å‡ºåº“操作(不拆包)
        /// </summary>
        private void PerformFullOutboundOperation(Dt_StockInfo stockInfo, OutboundCompletePalletRequestDTO request, int taskNum)
        {
            List<Dt_StockInfoDetail_Hty> historyRecords = new List<Dt_StockInfoDetail_Hty>();
            List<Dt_StockQuantityChangeRecord> changeRecords = new List<Dt_StockQuantityChangeRecord>();
            foreach (var item in stockInfo.Details)
            {
                // ä¿å­˜åº“存明细到历史记录
                Dt_StockInfoDetail_Hty historyRecord = new Dt_StockInfoDetail_Hty
                {
                    SourceId = item.Id,
                    OperateType = "出库完成",
                    InsertTime = DateTime.Now,
                    StockId = item.StockId,
                    MaterielCode = item.MaterielCode,
                    MaterielName = item.MaterielName,
                    OrderNo = item.OrderNo,
                    BatchNo = item.BatchNo,
                    ProductionDate = item.ProductionDate,
                    EffectiveDate = item.EffectiveDate,
                    SerialNumber = item.SerialNumber,
                    StockQuantity = item.StockQuantity,
                    OutboundQuantity = item.StockQuantity,
                    Status = item.Status,
                    Unit = item.Unit,
                    InboundOrderRowNo = item.InboundOrderRowNo,
                    SupplyCode = item.SupplyCode,
                    FactoryArea = item.FactoryArea,
                    WarehouseCode = item.WarehouseCode,
                    Barcode = item.Barcode,
                    Remark = $"整箱出库完成删除,条码:{request.PalletCode},原数量:{item.StockQuantity},出库数量:{item.StockQuantity},操作者:{request.Operator}"
                };
                historyRecords.Add(historyRecord);
                // è®°å½•库存变动
                Dt_StockQuantityChangeRecord changeRecord = new Dt_StockQuantityChangeRecord
                {
                    StockDetailId = item.Id,
                    PalleCode = stockInfo.PalletCode,
                    MaterielCode = item.MaterielCode,
                    MaterielName = item.MaterielName,
                    BatchNo = item.BatchNo,
                    OriginalSerilNumber = item.Barcode,
                    NewSerilNumber = "",
                    OrderNo = request.OrderNo,
                    TaskNum = taskNum,
                    ChangeType = (int)StockChangeTypeEnum.Outbound,
                    ChangeQuantity = -item.StockQuantity,
                    BeforeQuantity = item.StockQuantity,
                    AfterQuantity = 0,
                    SupplyCode = item.SupplyCode,
                    WarehouseCode = item.WarehouseCode,
                    Remark = $"整箱出库完成删除库存明细,条码:{request.PalletCode},出库数量:{item.StockQuantity},操作者:{request.Operator}"
                };
                changeRecords.Add(changeRecord);
            }
            _stockDetailHistoryRepository.AddData(historyRecords);
            // åˆ é™¤åº“存明细记录
            _stockDetailRepository.DeleteData(stockInfo.Details);
            _stockChangeRepository.AddData(changeRecords);
        }
        #region æ‹£é€‰
        /// <summary>
        /// å‡ºåº“完成处理(扫描条码扣减库存)