From 1866b69e8f67e382a0a75268d63e6418c8ae02e7 Mon Sep 17 00:00:00 2001
From: heshaofeng <heshaofeng@hnkhzn.com>
Date: 星期二, 13 一月 2026 09:55:01 +0800
Subject: [PATCH] 1

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundService.cs |  143 ++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 115 insertions(+), 28 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundService.cs"
index c7cd1e8..6adef6d 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundService.cs"
@@ -3,6 +3,7 @@
 using Dm.filter;
 using MailKit.Search;
 using Mapster;
+using Microsoft.IdentityModel.Tokens;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Serialization;
 using Org.BouncyCastle.Asn1.Ocsp;
@@ -18,6 +19,7 @@
 using WIDESEA_Core.BaseRepository;
 using WIDESEA_Core.CodeConfigEnum;
 using WIDESEA_Core.Helper;
+using WIDESEA_DTO.Base;
 using WIDESEA_DTO.Basic;
 using WIDESEA_DTO.CalcOut;
 using WIDESEA_DTO.ReturnMES;
@@ -549,7 +551,7 @@
         private List<Dt_StockInfo> BuildStockQueryWithInfo(MaterielOutboundCalculationDTO materielCalc, string factoryArea)
         {
             // 鍩虹鏌ヨ鏉′欢锛氱墿鏂欑紪鍙枫�佹壒娆″彿锛堝鏋滄彁渚涳級銆佸簱瀛樻暟閲�>0
-            ISugarQueryable<Dt_StockInfoDetail> stockDetails = _stockDetailRepository.Db.Queryable<Dt_StockInfoDetail>().Where(x => x.MaterielCode == materielCalc.MaterielCode && x.StockQuantity > 0);
+            ISugarQueryable<Dt_StockInfoDetail> stockDetails = _stockDetailRepository.Db.Queryable<Dt_StockInfoDetail>().Where(x => x.MaterielCode == materielCalc.MaterielCode && x.StockQuantity > 0 && (x.Status == (int)StockStatusEmun.鍏ュ簱瀹屾垚 || x.Status == (int)StockStatusEmun.鎵嬪姩瑙i攣));
 
             // 鏍规嵁鏉′欢娣诲姞渚涘簲鍟嗙紪鍙峰尮閰嶏紙涓嶄负绌烘椂鎵嶉渶瑕佸尮閰嶏級
             if (!string.IsNullOrEmpty(materielCalc.SupplyCode))
@@ -878,22 +880,6 @@
                     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)
@@ -916,6 +902,67 @@
                 {
                     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;
+                bool isMatMixed = false;
+
+                bool includeBatchNo = !string.IsNullOrEmpty(lockInfo.BatchNo);
+                bool includeSupplyCode = !string.IsNullOrEmpty(lockInfo.SupplyCode);
+
+                if (includeBatchNo && includeSupplyCode)
+                {
+                    isMatMixed = stockInfo.Details.GroupBy(x => new
+                    {
+                        x.MaterielCode,
+                        x.MaterielName,
+                        x.BatchNo,
+                        x.SupplyCode,
+                        x.WarehouseCode
+                    }).Count() > 1;
+                }
+                else if (includeBatchNo && !includeSupplyCode)
+                {
+                    isMatMixed = stockInfo.Details.GroupBy(x => new
+                    {
+                        x.MaterielCode,
+                        x.MaterielName,
+                        x.BatchNo,
+                        x.WarehouseCode
+                    }).Count() > 1;
+                }
+                else if (!includeBatchNo && includeSupplyCode)
+                {
+                    isMatMixed = stockInfo.Details.GroupBy(x => new
+                    {
+                        x.MaterielCode,
+                        x.MaterielName,
+                        x.SupplyCode,
+                        x.WarehouseCode
+                    }).Count() > 1;
+                }
+                else
+                {
+                    isMatMixed = stockInfo.Details.GroupBy(x => new
+                    {
+                        x.MaterielCode,
+                        x.MaterielName,
+                        x.WarehouseCode
+                    }).Count() > 1;
+                }
+
+                if (isMatMixed)
+                {
+                    response.Success = false;
+                    response.Message = $"娣锋枡鎵樼洏 {request.PalletCode} 涓嶈兘鏁寸鍑哄簱";
                     return WebResponseContent.Instance.Error(response.Message);
                 }
 
@@ -1017,18 +1064,23 @@
                         List<Dt_StockInfoDetail> stockInfoDetails = stockInfo.Details.Where((x => x.StockQuantity > x.OutboundQuantity)).ToList();
 
                         decimal itemQuantity = item.LockQuantity - item.OverOutQuantity;
-
+                        decimal unitbarcodeQuantity;
                         foreach (var stockDetail in stockInfoDetails)
                         {
+                            
+
                             if (itemQuantity >= stockDetail.StockQuantity - stockDetail.OutboundQuantity)
                             {
+                                unitbarcodeQuantity = stockDetail.StockQuantity - stockDetail.OutboundQuantity;
+                                UnitConvertResultDTO currentResult = _basicService.UnitQuantityConvert(item.MaterielCode, item.Unit, item.BarcodeUnit, unitbarcodeQuantity);
+
                                 Barcodes barcodes = new Barcodes
                                 {
                                     Barcode = stockDetail.Barcode,
-                                    Qty = stockDetail.StockQuantity - stockDetail.OutboundQuantity,
+                                    Qty = currentResult.ToQuantity,
                                     SupplyCode = stockDetail?.SupplyCode ?? "",
                                     BatchNo = stockDetail?.BatchNo ?? "",
-                                    Unit = stockDetail?.Unit ?? ""
+                                    Unit = currentResult.ToUnit ?? ""
                                 };
 
                                 itemQuantity -= (stockDetail.StockQuantity - stockDetail.OutboundQuantity);
@@ -1039,13 +1091,14 @@
                             }
                             else
                             {
+                                UnitConvertResultDTO currentResult = _basicService.UnitQuantityConvert(item.MaterielCode, item.Unit, item.BarcodeUnit, itemQuantity);
                                 Barcodes barcodes = new Barcodes
                                 {
                                     Barcode = stockDetail.Barcode,
-                                    Qty = itemQuantity,
+                                    Qty = currentResult.ToQuantity,
                                     SupplyCode = stockDetail?.SupplyCode ?? "",
                                     BatchNo = stockDetail?.BatchNo ?? "",
-                                    Unit = stockDetail?.Unit ?? ""
+                                    Unit = currentResult.ToUnit ?? ""
                                 };
                                 stockDetail.OutboundQuantity += itemQuantity;
                                 barcodesList.Add(barcodes);
@@ -1402,7 +1455,6 @@
                     {
                         if (allocatedQuantity <= 0) break;
 
-
                         //if (item.OrderQuantity - item.MoveQty - item.OverOutQuantity >= allocatedQuantity)
                         //{
                         //    item.OverOutQuantity += allocatedQuantity;
@@ -1433,13 +1485,15 @@
                         updateDetails.Add(item);
 
                         List<Barcodes> barcodesList = new List<Barcodes>();
+                        UnitConvertResultDTO currentResult = _basicService.UnitQuantityConvert(item.MaterielCode, item.Unit, item.BarcodeUnit, barcodeQuantity);
+
                         Barcodes barcodes = new Barcodes
                         {
                             Barcode = isUnpacked ? newBarcode : stockDetail?.Barcode,
-                            Qty = barcodeQuantity,
+                            Qty = currentResult.ToQuantity,
                             SupplyCode = stockDetail?.SupplyCode ?? "",
                             BatchNo = stockDetail?.BatchNo ?? "",
-                            Unit = stockDetail?.Unit ?? ""
+                            Unit = currentResult.ToUnit ?? ""
                         };
                         if (!string.IsNullOrEmpty(item.ReturnJsonData))
                         {
@@ -1455,7 +1509,7 @@
 
                     lockInfo.SortedQuantity = lockInfo.SortedQuantity + actualOutboundQuantity;
 
-                    if (lockInfo.SortedQuantity == lockInfo.AssignQuantity)
+                    if (lockInfo.SortedQuantity >= lockInfo.AssignQuantity)
                     {
                         _outboundLockInfoRepository.DeleteAndMoveIntoHty(lockInfo, WIDESEA_Core.Enums.OperateTypeEnum.鑷姩瀹屾垚);
                     }
@@ -1501,6 +1555,22 @@
                         _feedbackMesService.BarcodeFeedback(newBarcode);
                     }
 
+                    // 鍒犻櫎閿佸畾璁板綍锛堝鏋滃嚭搴撴槑缁嗗叏閮ㄥ畬鎴愶級 涓嶤alculateActualOutboundQuantity鏂规硶閲岄潰娉ㄩ噴浠g爜2閫�1浣跨敤
+                    if (CheckOutboundOrderDetailCompletedByMatCode(request.OrderNo, lockInfo.MaterielCode, outboundOrderDetails.First()))
+                    {
+                        Func<Dt_OutStockLockInfo, bool> supWhere = x => string.IsNullOrEmpty(outboundOrderDetails.First().SupplyCode) ? true : x.SupplyCode == outboundOrderDetails.First().SupplyCode;
+
+                        Func<Dt_OutStockLockInfo, bool> wareWhere = x => string.IsNullOrEmpty(outboundOrderDetails.First().WarehouseCode) ? true : x.WarehouseCode == outboundOrderDetails.First().WarehouseCode;
+
+                        List<Dt_OutStockLockInfo> stockLockInfos = _outboundLockInfoRepository.QueryData(x =>
+                                x.OrderNo == request.OrderNo &&
+                                x.MaterielCode == stockDetail.MaterielCode).Where(supWhere).Where(wareWhere).ToList();
+                        if (stockLockInfos != null && stockLockInfos.Any())
+                        {
+                            _outboundLockInfoRepository.DeleteAndMoveIntoHty(stockLockInfos, WIDESEA_Core.Enums.OperateTypeEnum.鑷姩鍒犻櫎);
+                        }
+                    }
+
                     // 妫�鏌ュ嚭搴撳崟鏄惁瀹屾垚
                     if (CheckOutboundOrderCompleted(request.OrderNo))
                     {
@@ -1511,6 +1581,7 @@
                             _feedbackMesService.OutboundFeedback(outboundOrder.OrderNo);
                         }
                     }
+
                 }
                 catch (Exception ex)
                 {
@@ -1558,6 +1629,8 @@
         /// </summary>
         private decimal CalculateActualOutboundQuantity(Dt_StockInfoDetail stockDetail, List<Dt_OutboundOrderDetail> outboundDetails, Dt_OutStockLockInfo lockInfo)
         {
+            // decimal availableOutboundQuantity = lockInfo.AssignQuantity - lockInfo.SortedQuantity;
+
             decimal availableOutboundQuantity = lockInfo.AssignQuantity;
             decimal detailRemainingQuantity = outboundDetails.Sum(x => x.OrderQuantity - x.OverOutQuantity - x.MoveQty);//outboundDetail.OrderQuantity - outboundDetail.OverOutQuantity;
 
@@ -1576,7 +1649,7 @@
         /// <param name="beforeQuantity"></param>
         /// <param name="taskNum"></param>
         /// <returns></returns>
-        private (string NewBarcode, List<MaterialCodeReturnDTO> MaterialCodeReturnDTOs) PerformUnpackOperation(Dt_StockInfoDetail stockDetail, Dt_StockInfo stockInfo,
+        public (string NewBarcode, List<MaterialCodeReturnDTO> MaterialCodeReturnDTOs) PerformUnpackOperation(Dt_StockInfoDetail stockDetail, Dt_StockInfo stockInfo,
             decimal actualOutboundQuantity, OutboundCompleteRequestDTO request, decimal beforeQuantity, int taskNum, int orderId, string orderNo)
         {
             string newBarcode = GenerateNewBarcode();
@@ -1654,7 +1727,7 @@
         /// <summary>
         /// 鎵ц瀹屾暣鍑哄簱鎿嶄綔锛堜笉鎷嗗寘锛�
         /// </summary>
-        private void PerformFullOutboundOperation(Dt_StockInfoDetail stockDetail, Dt_StockInfo stockInfo,
+        public void PerformFullOutboundOperation(Dt_StockInfoDetail stockDetail, Dt_StockInfo stockInfo,
             decimal actualOutboundQuantity, OutboundCompleteRequestDTO request, decimal beforeQuantity, int taskNum)
         {
             // 淇濆瓨搴撳瓨鏄庣粏鍒板巻鍙茶褰�
@@ -1834,6 +1907,20 @@
             return details.All(x => x.OverOutQuantity >= x.OrderQuantity - x.MoveQty);
         }
 
+        /// <summary>
+        /// 妫�鏌ュ嚭搴撳崟鏄庣粏鏄惁瀹屾垚
+        /// </summary>
+        public bool CheckOutboundOrderDetailCompletedByMatCode(string orderNo, string materialCode, Dt_OutboundOrderDetail outboundOrderDetail)
+        {
+            Dt_OutboundOrder outboundOrder = _outboundRepository.QueryFirst(x => x.OrderNo == orderNo);
+            if (outboundOrder == null) return false;
+
+            List<Dt_OutboundOrderDetail> details = _detailRepository.QueryData(x => x.OrderId == outboundOrder.Id && x.MaterielCode == materialCode && (string.IsNullOrEmpty(outboundOrderDetail.SupplyCode) || x.SupplyCode == outboundOrderDetail.SupplyCode) && (string.IsNullOrEmpty(outboundOrderDetail.WarehouseCode) || x.WarehouseCode == outboundOrderDetail.WarehouseCode));
+
+            // 妫�鏌ユ墍鏈夋槑缁嗙殑宸插嚭鏁伴噺鏄惁閮界瓑浜庡崟鎹暟閲�
+            return details.All(x => x.OverOutQuantity >= x.OrderQuantity - x.MoveQty);
+        }
+
         #endregion
 
         #region 鍙栫┖绠�

--
Gitblit v1.9.3