From d13e6e7d5df05ba244971d2fabb788ccb0f5c12a Mon Sep 17 00:00:00 2001
From: wankeda <Administrator@DESKTOP-HAU3ST3>
Date: 星期二, 27 一月 2026 15:12:10 +0800
Subject: [PATCH] Merge branch 'master' of http://115.159.85.185:8098/r/MeiRuiAn/JiAnLiKu

---
 WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/Service/OutboundOrderDetailService.cs |  205 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 192 insertions(+), 13 deletions(-)

diff --git a/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/Service/OutboundOrderDetailService.cs b/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/Service/OutboundOrderDetailService.cs
index b103c2a..23849ff 100644
--- a/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/Service/OutboundOrderDetailService.cs
+++ b/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/Service/OutboundOrderDetailService.cs
@@ -348,18 +348,22 @@
                 }
                 else
                 {
-                   stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, warehouse);
+                    stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, warehouse);
                 }
                     
                 if (!stockInfos.Any())
                 {
                     throw new Exception($"鏈壘鍒板彲鍒嗛厤搴撳瓨");
                 }
-                List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, item.MaterielCode, needQuantity, out decimal residueQuantity);
+                List<Dt_StockInfo> autoAssignStocks = new List<Dt_StockInfo>();
+                decimal newResidueQuantity = 0; 
 
-                item.LockQuantity += needQuantity - residueQuantity;
+                 autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, item.MaterielCode, needQuantity, out decimal residueQuantity);
+                 newResidueQuantity = residueQuantity;
+              
+                item.LockQuantity += needQuantity - newResidueQuantity;
                 outStocks.AddRange(autoAssignStocks);
-                decimal assignQuantity = needQuantity - residueQuantity;
+                decimal assignQuantity = needQuantity - newResidueQuantity;
 
                 List<Dt_OutboundOrderDetail> details = outboundOrderDetails.Where(x => !string.IsNullOrEmpty(x.BatchNo) ? x.BatchNo == item.BatchNo : true && x.MaterielCode == item.MaterielCode).ToList();
 
@@ -375,7 +379,17 @@
                         {
                             palletAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == item.MaterielCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//鍑哄簱璇︽儏宸插垎閰嶆暟閲�
                         }
-                        decimal palletOutboundQuantity = autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity);
+                        decimal palletOutboundQuantity = 0;
+                        if (warehouse.Any(x => x.WarehouseCode.Contains("DW")) || warehouse.Any(x => x.WarehouseCode.Contains("YS")))
+                        {
+                            palletOutboundQuantity = autoAssignStocks[j].Details.Where(x => x.BatchNo == item.BatchNo).Sum(x => x.OutboundQuantity);
+
+                        }
+                        else
+                        {
+                             palletOutboundQuantity = autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity);
+
+                        }
                         if (palletAssignQuantity < palletOutboundQuantity)//濡傛灉鍑哄簱璇︽儏宸插垎閰嶆暟閲忓皬浜庢墭鐩樺凡鍒嗛厤鏁伴噺锛屽垯鍙互缁х画娣诲姞璇ユ墭鐩樺嚭搴撲俊鎭�
                         {
                             decimal orderDetailNeedQuantity = details[i].OrderQuantity - detailAssignQuantity;
@@ -399,9 +413,174 @@
 
                 locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList()));
             }
-
             return (outStocks, outboundOrderDetails, outStockLockInfos, locationInfos);
         }
+
+        /// <summary>
+        /// 浣庢俯銆佽嵂姘村簱瀛樺垎閰�
+        /// </summary>
+        /// <param name="outboundOrderDetails"></param>
+        /// <returns></returns>
+        /// <exception cref="Exception"></exception>
+        public (List<Dt_StockInfo>, List<Dt_OutboundOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) DWANDYSAssignStockOutbound(List<Dt_OutboundOrderDetail> outboundOrderDetails)
+        {
+            if (!outboundOrderDetails.Any())
+            {
+                throw new Exception($"鏈壘鍒板嚭搴撳崟鏄庣粏淇℃伅");
+            }
+
+            if (outboundOrderDetails.GroupBy(x => x.OrderId).Count() > 1)
+            {
+                throw new Exception($"璇峰嬁鍚屾椂鎿嶄綔澶氫釜鍗曟嵁鏄庣粏");
+            }
+            Dt_OutboundOrder outboundOrder = _outboundRepository.OutboundOrderRepository.QueryFirst(x => x.Id == outboundOrderDetails.FirstOrDefault().OrderId);
+            List<Dt_OutboundOrderDetail> dt_OutboundOrderDetails = _outboundRepository.OutboundOrderDetailRepository.QueryData(x => x.OrderId == outboundOrder.Id);
+            List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>();
+            List<Dt_OutboundOrderDetail> groupDetails = outboundOrderDetails.GroupBy(x => new { x.MaterielCode, x.BatchNo, x.LocationName }).Select(x => new Dt_OutboundOrderDetail { OrderQuantity = x.Sum(v => v.OrderQuantity) - x.Sum(v => v.LockQuantity), MaterielCode = x.Key.MaterielCode, BatchNo = x.Key.BatchNo, LocationName = x.Key.LocationName }).ToList();
+            List<Dt_Warehouse> warehouse = new List<Dt_Warehouse>();
+
+            warehouse = _basicService.WarehouseService.Repository.QueryData(x => x.WarehouseDes == outboundOrder.OutWareHouse);
+
+            List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
+            List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
+            List<string> PalletCodes = new List<string>();
+            foreach(var groupDetail in groupDetails)
+            {
+                Dt_StockInfo dt_StockInfo = new Dt_StockInfo();
+                List<Dt_StockInfoDetail> dt_StockInfoDetails = _stockService.StockInfoDetailService.Repository.QueryData(x => x.MaterielCode == groupDetail.MaterielCode && x.BatchNo == groupDetail.BatchNo);
+                foreach(var stockInfoDetail in dt_StockInfoDetails)
+                {
+                    dt_StockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.Id == stockInfoDetail.StockId && x.StockStatus == StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt());
+                    if(dt_StockInfo != null)
+                    {
+                        PalletCodes.Add(dt_StockInfo.PalletCode);
+                    }
+                }
+                
+                if(!PalletCodes.Any())
+                {
+                    throw new Exception($"鏈壘鍒板彲鍒嗛厤搴撳瓨");
+                }
+              
+            }
+            foreach(var PalletCode in PalletCodes.Distinct()) 
+            {
+                List<Dt_StockInfo> stockInfos = new List<Dt_StockInfo>();
+                if (outboundOrderDetails.All(x => x.OrderQuantity == x.LockQuantity)) break;
+                foreach (var item in groupDetails)
+                {
+                    if (item.OrderQuantity == item.LockQuantity) continue;
+                    ///鍑哄簱鎸囧畾搴撲綅鍑哄簱鍒ゆ柇
+                    if (item.LocationName != null && item.LocationName != "")
+                    {
+                        //stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, warehouse).Where(x => x.LocationCode == item.LocationName).ToList();
+                        List<Dt_StockInfo> nowStockInfos = Db.Queryable<Dt_StockInfo>()
+                            .Where(x => x.PalletCode == PalletCode && x.Details.Any(v => v.BatchNo == item.BatchNo && v.MaterielCode == item.MaterielCode) && x.LocationCode == item.LocationName)
+                            .Includes(x => x.Details).ToList();
+                        if (stockInfos.Count() == 0 && nowStockInfos.Count() > 0)
+                        {
+                            stockInfos.AddRange(nowStockInfos);
+                        }
+                    }
+                    else
+                    {
+                        //stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, warehouse);
+                        List<Dt_StockInfo> nowStockInfos = Db.Queryable<Dt_StockInfo>()
+                            .Where(x => x.PalletCode == PalletCode && x.Details.Any(v => v.BatchNo == item.BatchNo && v.MaterielCode == item.MaterielCode))
+                            .Includes(x => x.Details).ToList();
+                        if(stockInfos.Count() == 0 && nowStockInfos.Count() > 0)
+                        {
+                            stockInfos.AddRange(nowStockInfos);
+                        }
+                    }
+ 
+                }
+                if(stockInfos.Count() == 0)
+                {
+                    continue;
+                }
+                List<Dt_StockInfo> autoAssignStocks = new List<Dt_StockInfo>();
+                List<IStockInfoService.residueQuantity> newResidueQuantitys = new List<IStockInfoService.residueQuantity>();
+                if (warehouse.Any(x => x.WarehouseCode.Contains("DW")) || warehouse.Any(x => x.WarehouseCode.Contains("YS")))
+                {
+                    autoAssignStocks = _stockService.StockInfoService.DWANDYSGetOutboundStocks(stockInfos, dt_OutboundOrderDetails, out List<IStockInfoService.residueQuantity> residueQuantitys);
+                    newResidueQuantitys.AddRange(residueQuantitys);
+                    outStocks.AddRange(autoAssignStocks);
+                }
+                foreach(var residueQuantity in newResidueQuantitys)
+                {
+                    foreach (var item in dt_OutboundOrderDetails)
+                    {
+                        if(residueQuantity.MaterielCode == item.MaterielCode && residueQuantity.BatchNo == item.BatchNo)
+                        {
+                            decimal originalNeedQuantity = item.OrderQuantity;
+
+                            decimal needQuantity = originalNeedQuantity - item.LockQuantity;
+
+                            item.LockQuantity += needQuantity - residueQuantity.NewNeendQuantity;
+
+                            decimal assignQuantity = needQuantity -  residueQuantity.NewNeendQuantity;
+
+                            List<Dt_OutboundOrderDetail> details = dt_OutboundOrderDetails.Where(x => !string.IsNullOrEmpty(x.BatchNo) ? x.BatchNo == item.BatchNo : true && x.MaterielCode == item.MaterielCode).ToList();
+
+                            for (int i = 0; i < details.Count; i++)
+                            {
+                                decimal orderQuantity = details[i].OrderQuantity;
+                                for (int j = 0; j < autoAssignStocks.Count; j++)
+                                {
+                                    decimal detailAssignQuantity = outStockLockInfos.Where(x => !string.IsNullOrEmpty(x.BatchNo) ? x.BatchNo == item.BatchNo : true && x.MaterielCode == item.MaterielCode && x.OrderDetailId == details[i].Id).Sum(x => x.AssignQuantity);//鍑哄簱璁㈠崟鏄庣粏宸插垎閰嶆暟閲�
+
+                                    decimal palletAssignQuantity = outStockLockInfos.Where(x => x.BatchNo == item.BatchNo && x.MaterielCode == item.MaterielCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//鍑哄簱璇︽儏宸插垎閰嶆暟閲�
+                                    if (string.IsNullOrEmpty(item.BatchNo))
+                                    {
+                                        palletAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == item.MaterielCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//鍑哄簱璇︽儏宸插垎閰嶆暟閲�
+                                    }
+                                    decimal palletOutboundQuantity = 0;
+                                    if (warehouse.Any(x => x.WarehouseCode.Contains("DW")) || warehouse.Any(x => x.WarehouseCode.Contains("YS")))
+                                    {
+                                        palletOutboundQuantity = autoAssignStocks[j].Details.Where(x => x.BatchNo == item.BatchNo && x.MaterielCode == item.MaterielCode).Sum(x => x.OutboundQuantity);
+
+                                    }
+                                    if (palletAssignQuantity < palletOutboundQuantity)//濡傛灉鍑哄簱璇︽儏宸插垎閰嶆暟閲忓皬浜庢墭鐩樺凡鍒嗛厤鏁伴噺锛屽垯鍙互缁х画娣诲姞璇ユ墭鐩樺嚭搴撲俊鎭�
+                                    {
+                                        decimal orderDetailNeedQuantity = details[i].OrderQuantity - detailAssignQuantity;
+                                        if (orderDetailNeedQuantity > autoAssignStocks[j].Details.Where(x => x.MaterielCode == details[i].MaterielCode && x.BatchNo == details[i].BatchNo).Sum(v => v.OutboundQuantity) - palletAssignQuantity)
+                                        {
+                                            //details[i].LockQuantity += autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity;
+                                            Dt_StockInfoDetail dt_StockInfoDetail = new Dt_StockInfoDetail();
+                                            foreach(var autoStockDestail in autoAssignStocks[j].Details)
+                                            {
+                                                if(autoStockDestail.MaterielCode == details[i].MaterielCode && autoStockDestail.BatchNo == details[i].BatchNo)
+                                                {
+                                                    dt_StockInfoDetail = autoStockDestail;
+                                                }
+                                            }
+                                            Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(outboundOrder, details[i], autoAssignStocks[j],dt_StockInfoDetail.OutboundQuantity);
+                                            outStockLockInfos.Add(outStockLockInfo);
+                                            details[i].LockQuantity += outStockLockInfo.AssignQuantity;
+                                            break;
+                                        }
+                                        else
+                                        {
+                                            Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(outboundOrder, details[i], autoAssignStocks[j], details[i].OrderQuantity - details[i].LockQuantity);
+                                            outStockLockInfos.Add(outStockLockInfo);
+                                            details[i].LockQuantity = details[i].OrderQuantity;
+                                            break;
+                                        }
+
+                                    }
+                                }
+                            }
+                            locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(autoAssignStocks.Select(x => x.LocationCode).ToList()));
+                        }
+                     }
+
+                }
+                newResidueQuantitys = new List<IStockInfoService.residueQuantity>();
+            }
+            return (outStocks, dt_OutboundOrderDetails, outStockLockInfos, locationInfos);
+        }
+
         private (bool, string) CheckSelectStockDeital(Dt_OutboundOrderDetail outboundOrderDetail, List<StockSelectViewDTO> stockSelectViews)
         {
             if (outboundOrderDetail == null)
@@ -498,7 +677,6 @@
             {
                 ids = item.LinId;
                 var postContent = new MultipartFormDataContent();
-                postContent.Headers.Add("ContentType", $"multipart/form-data");
                 postContent.Add(new StringContent(ids), "ids");
                 string result = string.Empty;
                 HttpClient client = null;
@@ -506,13 +684,14 @@
                 {
                     using (client = new HttpClient())
                     {
-                        HttpResponseMessage response = client.PostAsync(ToCancelOutFeedbackERP, postContent)
-                            .ConfigureAwait(false).GetAwaiter().GetResult();
+                        // 2. 鍙戦�佽姹�
+                        string url = $"{ToCancelOutFeedbackERP}?ids={Uri.EscapeDataString(ids)}";
+                        var response = HttpHelper.Post<WebResponseContent>(url, "鍑哄簱鏄庣粏鍙栨秷鍥炰紶ERP");
 
-                        // 纭繚鍝嶅簲鎴愬姛
-                        response.EnsureSuccessStatusCode();
-                        result = response.Content.ReadAsStringAsync()
-                            .ConfigureAwait(false).GetAwaiter().GetResult();
+                        if (response.Code !=0)
+                        {
+                            throw new Exception($"鎿嶄綔澶辫触: {response.msg ?? "鏈彁渚涢敊璇俊鎭�"}");
+                        }
                     }
                     _unitOfWorkManage.BeginTran();
                     _outboundRepository.OutboundOrderDetailRepository.DeleteAndMoveIntoHty(outboundOrderDetails, OperateType.浜哄伐鍙栨秷);

--
Gitblit v1.9.3