From e755a1c79f29b42f9344e6ab87475e9a4d6c7dc6 Mon Sep 17 00:00:00 2001
From: wankeda <Administrator@DESKTOP-HAU3ST3>
Date: 星期四, 15 一月 2026 13:52:25 +0800
Subject: [PATCH] 合托功能

---
 WMS/WIDESEA_WMSServer/WIDESEA_InboundService/Base/InboundOrderService.cs |  232 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 219 insertions(+), 13 deletions(-)

diff --git a/WMS/WIDESEA_WMSServer/WIDESEA_InboundService/Base/InboundOrderService.cs b/WMS/WIDESEA_WMSServer/WIDESEA_InboundService/Base/InboundOrderService.cs
index 10471c1..5d1dbbf 100644
--- a/WMS/WIDESEA_WMSServer/WIDESEA_InboundService/Base/InboundOrderService.cs
+++ b/WMS/WIDESEA_WMSServer/WIDESEA_InboundService/Base/InboundOrderService.cs
@@ -37,6 +37,9 @@
 using WIDESEA_Model.Models;
 using WIDESEA_Model.Models.Inbound;
 using static WIDESEA_Common.HouseInventoryIn;
+using static WIDESEA_Common.HouseSyncretism;
+using static WIDESEA_Common.InventoryAllocate;
+using Parameter = WIDESEA_Common.Parameter;
 
 namespace WIDESEA_InboundService
 {
@@ -171,8 +174,17 @@
                 {
                     return WebResponseContent.Instance.Error($"鏈壘鍒拌鐗╂枡鐨勪俊鎭�");
                 }
+                List<Dt_InboundOrderDetail> inboundOrderDetails = new List<Dt_InboundOrderDetail>();
                 ///鎵炬暟閲忓尮閰嶇殑
-                List<Dt_InboundOrderDetail> inboundOrderDetails = inboundOrder.Details.Where(x => x.MaterielCode == materielCode && x.SupplierBatch == (models.FirstOrDefault()?.LotNo ?? "") && x.OrderQuantity > x.ReceiptQuantity && x.OrderQuantity == (quantitys.FirstOrDefault())).ToList();
+                if (warehouse.WarehouseCode.Contains("BC"))
+                {
+                    inboundOrderDetails = inboundOrder.Details.Where(x => x.MaterielCode == materielCode && x.SupplierBatch == (models.FirstOrDefault()?.LotNo ?? "") && x.OrderQuantity > x.ReceiptQuantity && x.OrderQuantity == (quantitys.FirstOrDefault())).ToList();
+                }
+                else
+                {
+                    inboundOrderDetails = inboundOrder.Details.Where(x => x.MaterielCode == materielCode && x.BatchNo == (models.FirstOrDefault()?.LotNo ?? "")).ToList();
+                }
+
 
                 if (inboundOrderDetails == null || inboundOrderDetails.Count <= 0)
                 {
@@ -246,7 +258,18 @@
                 List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>();
                 foreach (var model in models)
                 {
-                    Dt_InboundOrderDetail? notGroupDetail = inboundOrderDetails.Where(x => x.OrderDetailStatus <= OrderDetailStatusEnum.Inbounding.ObjToInt() && x.MaterielCode == model.MaterielCode && x.SupplierBatch == model.LotNo && x.OrderQuantity > x.ReceiptQuantity).FirstOrDefault();
+
+                    Dt_InboundOrderDetail notGroupDetail = new Dt_InboundOrderDetail();
+                    ///鎵炬暟閲忓尮閰嶇殑
+                    if (warehouse.WarehouseCode.Contains("BC"))
+                    {
+                        notGroupDetail = inboundOrderDetails.Where(x => x.OrderDetailStatus <= OrderDetailStatusEnum.Inbounding.ObjToInt() && x.MaterielCode == model.MaterielCode && x.SupplierBatch == model.LotNo && x.OrderQuantity > x.ReceiptQuantity).FirstOrDefault();
+                    }
+                    else
+                    {
+                        notGroupDetail = inboundOrderDetails.Where(x => x.OrderDetailStatus <= OrderDetailStatusEnum.Inbounding.ObjToInt() && x.MaterielCode == model.MaterielCode && x.BatchNo == model.LotNo && x.OrderQuantity > x.ReceiptQuantity).FirstOrDefault();
+                    }
+
 
                     if (notGroupDetail == null)
                     {
@@ -266,7 +289,8 @@
                         Id = inboundOrderDet.LinId.ObjToInt(),
                         DeliveryNote = model.DeliveryNote,
                         SupplierBatch = inboundOrderDet.SupplierBatch,
-                        MaterieSpec = inboundOrderDet.MaterieSpec
+                        MaterieSpec = inboundOrderDet.MaterieSpec,
+                        OrinalLocation = inboundOrderDet.OrinalLocation
                     };
 
                     if (stockInfo.Id > 0)
@@ -393,7 +417,8 @@
                     .Select((cp, d, o) => cp)
                     .ToList();
 
-                float totalQty = CPinboundOrderDetails.Sum(x => x.QtyOfpcs);
+                //float totalQty = CPinboundOrderDetails.Sum(x => x.QtyOfpcs);
+
 
                 // 楠岃瘉鏌ヨ鍒扮殑鏁版嵁鏁伴噺鏄惁鍖归厤
                 if (CPinboundOrderDetails.Count < serialNumbers.Count)
@@ -407,6 +432,20 @@
                     .ToDictionary(g => g.Key, g => g.ToList());
 
 
+                var existingStockBoxCodes = _stockRepository.StockInfoRepository.Db
+                .Queryable<Dt_StockInfo>()
+                .LeftJoin<Dt_StockInfoDetail>((s, d) => s.Id == d.StockId)
+                .LeftJoin<Dt_StockInfoDetailCP>((s, d, cp) => d.Id == cp.StockDetailId)
+                .Where((s, d, cp) =>
+                    serialNumbers.Contains(cp.BoxCode))
+                .Select((s, d, cp) => cp.BoxCode)
+                .Distinct()
+                .ToList();
+
+                if (existingStockBoxCodes.Any())
+                {
+                    return WebResponseContent.Instance.Error($"浠ヤ笅绠卞彿宸插湪搴撳瓨涓細{string.Join(",", existingStockBoxCodes)}");
+                }
 
                 // 鑾峰彇鎵�鏈夌浉鍏崇殑鍏ュ簱鍗曟槑缁咺D
                 var orderDetailIds = CPinboundOrderDetails.Select(x => x.OrderDetailId).Distinct().ToList();
@@ -415,6 +454,11 @@
                 List<Dt_InboundOrderDetail> inboundOrderDetails = BaseDal.Db.Queryable<Dt_InboundOrderDetail>()
                     .Where(x => orderDetailIds.Contains(x.Id))
                     .ToList();
+
+                if (inboundOrderDetails.Sum(x => x.OrderQuantity) <= inboundOrderDetails.Sum(x => x.ReceiptQuantity))
+                {
+                    return WebResponseContent.Instance.Error($"姝ゅ崟鎹凡鍏ㄩ儴缁勭洏瀹屾垚");
+                }
 
                 // 鎸夎鍗曟槑缁咺D鍒嗙粍锛屾鏌ユ瘡涓槑缁嗙殑鎵�鏈夌鍙锋槸鍚﹂兘鎵弿浜�
                 var detailsByOrderDetailId = CPinboundOrderDetails
@@ -449,16 +493,18 @@
                     // 鏍囪璇ユ槑缁嗕负宸插畬鎴愮粍鐩�
                     orderDetail.OrderDetailStatus = OrderDetailStatusEnum.GroupAndInbound.ObjToInt();
                     var recqty = orderDetail.ReceiptQuantity;
-                    orderDetail.ReceiptQuantity = ((decimal)totalQty) + recqty; // 鏀惰揣鏁伴噺绛変簬璁㈠崟鏁伴噺
+                    float totalQty = cpDetails.Sum(item => item.QtyOfpcs);
+                    orderDetail.ReceiptQuantity = (decimal)totalQty + recqty; // 鏀惰揣鏁伴噺绛変簬璁㈠崟鏁伴噺
 
                     detailsToUpdate.Add(orderDetail);
                 }
+
+
 
                 // 妫�鏌ユ墭鐩樻槸鍚﹀凡瀛樺湪
                 Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.Db
                     .Queryable<Dt_StockInfo>()
                     .Where(x => x.PalletCode == palletCode)
-                    .Includes(x => x.Details)
                     .First();
 
                 if (stockInfo == null)
@@ -490,6 +536,7 @@
                     }
                 }
 
+                List<Dt_CPInboundOrderDetail> cPInboundOrderDetail = new List<Dt_CPInboundOrderDetail>();
                 // 鍒涘缓搴撳瓨鏄庣粏 - 鎸夎鍗曟槑缁嗗垱寤�
                 foreach (var orderDetail in orderDetails)
                 {
@@ -498,7 +545,8 @@
                         .Select(x => x.BoxCode)
                         .ToList();
 
-                    List<Dt_CPInboundOrderDetail> cPInboundOrderDetail = BaseDal.Db.Queryable<Dt_CPInboundOrderDetail>().Where(x => detailBoxCodes.Contains(x.BoxCode)).ToList();
+                    cPInboundOrderDetail = BaseDal.Db.Queryable<Dt_CPInboundOrderDetail>().Where(x => detailBoxCodes.Contains(x.BoxCode)).ToList();
+
                     List<Dt_StockInfoDetailCP> stockInfoDetailCP = new List<Dt_StockInfoDetailCP>();
                     foreach (var item in cPInboundOrderDetail)
                     {
@@ -528,14 +576,15 @@
                         BatchNo = orderDetail.BatchNo,
                         SupplierBatch = orderDetail.SupplierBatch,
                         LinId = orderDetail.LinId,
-                        StockQuantity = ((decimal)totalQty),
+                        StockQuantity = (decimal)cPInboundOrderDetail.Sum(item => item.QtyOfpcs),
                         Status = (int)StockStatusEmun.缁勭洏鏆傚瓨,
                         Creater = "WMS",
                         CreateDate = DateTime.Now,
                         Id = orderDetail.LinId.ObjToInt(),
                         //DeliveryNote = orderDetail.DeliveryNote,
                         MaterieSpec = orderDetail.MaterieSpec,
-                        StockDetails = stockInfoDetailCP
+                        StockDetails = stockInfoDetailCP,
+                        OrinalLocation = orderDetail.OrinalLocation
                     };
 
                     if (stockInfo.Id > 0)
@@ -581,7 +630,7 @@
                     // 鏇存柊璁㈠崟
                     _inboundRepository.InboundOrderRepository.UpdateData(inboundOrder);
 
-                    _cPInboundOrderDetailRepository.UpdateData(CPinboundOrderDetails);
+                    _cPInboundOrderDetailRepository.UpdateData(cPInboundOrderDetail);
 
                     _unitOfWorkManage.CommitTran();
 
@@ -600,6 +649,137 @@
 
             return content;
         }
+        public string ReceiveWMSTaskin = WIDESEA_Core.Helper.AppSettings.Configuration["ReceiveWMSTaskin"];
+        /// <summary>
+        /// 缁勭洏鍚堟墭
+        /// </summary>
+        /// <param name="saveModel"></param>
+        /// <returns></returns>
+        public WebResponseContent SYMaterielGroup(SaveModel saveModel)
+        {
+
+            WebResponseContent webResponseContent = new WebResponseContent();
+            try
+            {
+                var soussAddress = saveModel.MainData["soussAddress"];
+                var targetAddress = saveModel.MainData["targetAddress"];
+
+                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.PalletCode == soussAddress).First();
+                Dt_StockInfo targetstockInfo = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.PalletCode == targetAddress).First();
+                Dt_Warehouse warehouse = _warehouseService.Repository.QueryFirst(x => x.WarehouseId == targetstockInfo.WarehouseId);
+                if (stockInfo == null || targetstockInfo == null)
+                {
+                    throw new Exception("搴撳瓨鏈壘鍒版墭鐩樺彿");
+                }
+                foreach (var item in stockInfo.Details)
+                {
+                    item.StockId = targetstockInfo.Id;
+                }
+                if (warehouse.WarehouseCode == "SC02_CP")
+                {
+                    warehouse.WarehouseCode = "SC01_CP";
+                }
+                if (warehouse.WarehouseCode == "SC02_BC")
+                {
+                    warehouse.WarehouseCode = "SC01_BC";
+                }
+                var houseSyncretism = new HouseSyncretism
+                {
+                    ApiType = "InventoryMoveController",
+                    Method = "AsrsSubmitMoveDatas",
+
+                    Parameters = new List<HouseSyncretism.Parame>
+                    {
+                        new HouseSyncretism.Parame {
+                            Value =  stockInfo.Details.Select(g => new Parame.Syncretism
+                        {
+                            Lpn = stockInfo.PalletCode,
+                            MoveType = 0,
+                            WareHouseCode = warehouse.WarehouseCode,
+                            ItemCode = g.MaterielCode,
+                            MoveNumber = g.StockQuantity,
+                            LotNo = g.BatchNo,
+                            WipBatch = g.BatchNo,
+                            LocationName = stockInfo.LocationCode,
+                            TargetLocName = targetstockInfo.LocationCode,
+                            TargetLpn = targetstockInfo.PalletCode,
+
+                        }).ToList()
+                            }
+                    }
+                    //    Parameters = stockInfo.Details.Select(g => new HouseSyncretism.data
+                    //    {
+                    //        Lpn = stockInfo.PalletCode,
+                    //        MoveType = 0,
+                    //        WareHouseCode = warehouse.WarehouseCode,
+                    //        ItemCode = g.MaterielCode,
+                    //        MoveNumber = g.StockQuantity,
+                    //        //LotNo = g.,
+                    //        WipBatch = g.BatchNo,
+                    //        LocationName = stockInfo.PalletCode,
+                    //        TargetLocName = targetstockInfo.PalletCode,
+                    //        TargetLpn = targetstockInfo.PalletCode,
+
+                    //    }).ToList()
+                    //};
+                };
+                var authResult = AuthenticateWithWMS();
+                if (authResult.IsSuccess)
+                {
+                    houseSyncretism.Context = new Dictionary<string, string>
+                                            {
+                                                { "Ticket", authResult.Ticket },
+                                                { "InvOrgId", authResult.InvOrgId }
+
+                                            };
+
+                    var response = HttpHelper.Post<MomRequestContent>(ReceiveWMSTaskin, houseSyncretism, "绔嬪簱鍏ュ簱鏁伴噺鍥炰紶WMS");
+                    // 鍒ゆ柇Success鐨勫��
+                    if (!response.Success)
+                    {
+                        throw new Exception($"鎿嶄綔澶辫触: {response.Message ?? "鏈彁渚涢敊璇俊鎭�"}");
+                    }
+
+                }
+                _unitOfWorkManage.BeginTran();
+                targetstockInfo.Details.AddRange(stockInfo.Details);
+                _stockRepository.StockInfoRepository.UpdateData(targetstockInfo);
+                _stockRepository.StockInfoDetailRepository.UpdateData(stockInfo.Details);
+                _stockRepository.StockInfoRepository.DeleteData(stockInfo);
+                _unitOfWorkManage.CommitTran();
+                webResponseContent = WebResponseContent.Instance.OK("鍚堟墭鎴愬姛");
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                webResponseContent = WebResponseContent.Instance.Error($"缁勭洏澶辫触锛歿ex.Message}");
+            }
+            return webResponseContent;
+        }
+
+        private (bool IsSuccess, string Ticket, string InvOrgId) AuthenticateWithWMS()
+        {
+            var authentication = new Authentication
+            {
+                ApiType = "AuthenticationController",
+                Parameters = new List<Parameter>
+        {
+            new Parameter { Value = "LK-Admin" },
+            new Parameter { Value = "LK-Admin" }
+        },
+                Method = "Login",
+            };
+
+            var response = HttpHelper.Post<WebResponseContent>(ReceiveWMSTaskin, authentication, "鐧诲綍WMS璐﹀彿");
+
+            if (response.Context != null)
+            {
+                return (true, response.Context["Ticket"].ToString(), response.Context["InvOrgId"].ToString());
+            }
+
+            return (false, null, null);
+        }
+
 
         public int GetPalletType(Dt_Warehouse warehouse, string palletCode)
         {
@@ -736,6 +916,24 @@
                             }
                             else
                             {
+                                List<Dt_CPInboundOrderDetail> cPInboundOrderDetail = new List<Dt_CPInboundOrderDetail>();
+                                foreach (var list in item.BoxList)
+                                {
+                                    Dt_CPInboundOrderDetail cPInboundOrderDetail1 = new Dt_CPInboundOrderDetail()
+                                    {
+                                        BoxId = (float)list.BoxId,
+                                        BoxCode = list.BoxCode,
+                                        DateCode = list.DateCode,
+                                        JobId = (float)list.JobId,
+                                        PartNum = list.PartNum,
+                                        QtyOfpcs = (float)list.QtyOfpcs,
+                                        QtyOfxout = (float)list.QtyOfxout,
+                                        CPOrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt(),
+                                        Creater = "涓婃父WMS",
+                                    };
+                                    cPInboundOrderDetail.Add(cPInboundOrderDetail1);
+                                }
+
                                 Dt_InboundOrderDetail orderDetail = new Dt_InboundOrderDetail()
                                 {
                                     OrderId = orderDetail1.OrderId,
@@ -750,9 +948,12 @@
                                     MaterielName = item.MaterielName,
                                     MaterieSpec = item.MaterieSpec,
                                     Creater = "涓婃父WMS",
-                                    SupplierBatch = item.SupplierBatch
+                                    SupplierBatch = item.SupplierBatch,
+                                    OrinalLocation = item.OrinalLocation,
+                                    CPDetails = cPInboundOrderDetail
                                 };
-                                _inboundRepository.InboundOrderDetailRepository.AddData(orderDetail);
+                                //_inboundRepository.InboundOrderDetailRepository.AddData(orderDetail);
+                                Db.InsertNav(orderDetail).Include(x => x.CPDetails).ExecuteCommand();
                             }
                         }
                         else
@@ -789,6 +990,7 @@
                                 MaterieSpec = item.MaterieSpec,
                                 Creater = "涓婃父WMS",
                                 SupplierBatch = item.SupplierBatch,
+                                OrinalLocation = item.OrinalLocation,
                                 CPDetails = cPInboundOrderDetail
                             };
 
@@ -1098,7 +1300,11 @@
             }
             return content;
         }
-
+        /// <summary>
+        /// erp鍗曟嵁涓嬪彂
+        /// </summary>
+        /// <param name="addInboundOrder"></param>
+        /// <returns></returns>
         public WebResponseContent Save(AddInboundOrderModel addInboundOrder)
         {
             try

--
Gitblit v1.9.3