From 07b532a6a6adf0a9d965b037cf98c1f98accd138 Mon Sep 17 00:00:00 2001
From: yanjinhui <3306209981@qq.com>
Date: 星期五, 07 十一月 2025 20:11:09 +0800
Subject: [PATCH] 优化出入退货代码

---
 新建文件夹/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/LKBusiness/DeliveryOrderServicesLK.cs |   60 +++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 51 insertions(+), 9 deletions(-)

diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/LKBusiness/DeliveryOrderServicesLK.cs" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/LKBusiness/DeliveryOrderServicesLK.cs"
index 3c120d4..1dda607 100644
--- "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/LKBusiness/DeliveryOrderServicesLK.cs"
+++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/LKBusiness/DeliveryOrderServicesLK.cs"
@@ -11,6 +11,8 @@
 using WIDESEA_Core.Enums;
 using WIDESEA_ISquareCabinServices;
 using WIDESEA_Core.Helper;
+using WIDESEA_Common;
+using static WIDESEA_DTO.SquareCabin.OrderDto;
 
 namespace WIDESEA_SquareCabinServices
 {
@@ -25,9 +27,12 @@
                 List<Dt_SupplyTask_Hty> supplyTask_Hties = new List<Dt_SupplyTask_Hty>();
                 List<Dt_InventoryInfo> infosUp = new List<Dt_InventoryInfo>();
                 List<Dt_Inventory_Batch> batchesUp = new List<Dt_Inventory_Batch>();
+                List<Dt_InventoryInfo> infosDel = new List<Dt_InventoryInfo>();//娣诲姞鍒犻櫎
+                List<Dt_Inventory_Batch> batchesDel = new List<Dt_Inventory_Batch>();
                 List<Dt_MaterielInfo> materielInfos = _materielInfoService.Repository.QueryData(x => deliveryOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
                 List<Dt_InventoryInfo> inventoryInfos = _inventoryInfoService.Repository.QueryData(x => deliveryOrder.Warehouse_no == x.WarehouseCode && deliveryOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
                 List<Dt_Inventory_Batch> inventory_Batches = _inventory_BatchServices.Repository.QueryData(x => deliveryOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
+                List<Dt_MaterielInfo> MaterieInfoBusiness =new List<Dt_MaterielInfo>();
                 #region 鐩樹簭鍑哄簱鍗�
                 if (deliveryOrder.Out_type == "6")
                 {
@@ -36,25 +41,51 @@
                         foreach (var detail in request.details)
                         {
                             Dt_DeliveryOrderDetail? orderDetail = deliveryOrder.Details.FirstOrDefault(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo);
-                            if (orderDetail == null) throw new Exception($"鏈壘鍒扮洏鐐瑰崟銆恵deliveryOrder.Out_no}銆戠墿鏂欑紪鍙枫�恵detail.productCode}銆戠墿鏂欐壒娆°�恵detail.batchNo}銆戠殑鏄庣粏淇℃伅");
+                            if (orderDetail == null)
+                            {
+                                _messageInfoService.AddMessageInfo(MessageGroupByEnum.OutOrderAlarm, "鐩樹簭鍑哄簱鍗�", $"鏈壘鍒扮洏鐐瑰崟銆恵deliveryOrder.Out_no}銆戠墿鏂欑紪鍙枫�恵detail.productCode}銆戠墿鏂欐壒娆°�恵detail.batchNo}銆戠殑鏄庣粏淇℃伅");
+                                throw new Exception($"鏈壘鍒扮洏鐐瑰崟銆恵deliveryOrder.Out_no}銆戠墿鏂欑紪鍙枫�恵detail.productCode}銆戠墿鏂欐壒娆°�恵detail.batchNo}銆戠殑鏄庣粏淇℃伅");
+
+                            }
                             decimal orderQty = detail.orderDetails?.Sum(x => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0) ?? 0;
                             Dt_InventoryInfo inventoryInfo = inventoryInfos.Where(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo).First();
-                            var qty = inventoryInfo.SupplyQuantity - inventoryInfo.StockQuantity;
-                            if (orderQty != qty)
-                                throw new Exception($"鐩樹簭鍑哄簱鏁伴噺涓嶄竴鑷�,涓婁紶鏁伴噺銆恵orderQty}銆�,搴撳瓨鐩樹簭鏁伴噺銆恵qty}銆�");
-                            inventoryInfo.StockQuantity = inventoryInfo.SupplyQuantity;
+                            if (orderQty != Math.Abs(inventoryInfo.SupplyQuantity))
+                            {
+                                _messageInfoService.AddMessageInfo(MessageGroupByEnum.OutOrderAlarm, "鐩樹簭鍑哄簱鍗�", $"鐩樹簭鍑哄簱鏁伴噺涓嶄竴鑷�,涓婁紶鏁伴噺銆恵orderQty}銆�,搴撳瓨鐩樹簭鏁伴噺銆恵Math.Abs(inventoryInfo.SupplyQuantity)}銆�");
+                                throw new Exception($"鐩樹簭鍑哄簱鏁伴噺涓嶄竴鑷�,涓婁紶鏁伴噺銆恵orderQty}銆�,搴撳瓨鐩樹簭鏁伴噺銆恵Math.Abs(inventoryInfo.SupplyQuantity)}銆�");
+
+                            }
+                            //娣诲姞鐗╂枡淇℃伅涓殑绔嬪簱涓氬姟搴撳瓨淇℃伅
+                            var mater = materielInfos.FirstOrDefault(x => x.MaterielCode == detail.productCode);
+                            if (mater != null)
+                            {
+                                mater.Business_qty += inventoryInfo.SupplyQuantity;
+                                MaterieInfoBusiness.Add(mater);
+                            }
+                            inventoryInfo.StockQuantity += inventoryInfo.SupplyQuantity;
                             inventoryInfo.AvailableQuantity = inventoryInfo.StockQuantity;
                             inventoryInfo.SupplyQuantity = 0;
                             inventoryInfo.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt();
-                            infosUp.Add(inventoryInfo);
+                            // 妫�鏌ュ簱瀛樻槸鍚︿负0锛屽鏋滄槸鍒欐爣璁颁负鍒犻櫎
+                            if (inventoryInfo.StockQuantity <= 0)
+                                infosDel.Add(inventoryInfo);
+                            else
+                                infosUp.Add(inventoryInfo);
+                            //infosUp.Add(inventoryInfo);
+                            //搴撳瓨鎵规
                             Dt_Inventory_Batch inventory_Batch = inventory_Batches.First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
-                            inventory_Batch.StockQuantity = inventory_Batch.SupplyQuantity;
+                            inventory_Batch.StockQuantity += inventory_Batch.SupplyQuantity;
                             inventory_Batch.AvailableQuantity = inventory_Batch.StockQuantity;
                             inventory_Batch.SupplyQuantity = 0;
                             batchesUp.Add(inventory_Batch);
                             orderDetail.OotDetailStatus = "宸插畬鎴�";
+                            orderDetail.Order_Outqty = orderQty; //瀹屾垚鏁伴噺
+                            if (inventory_Batch.StockQuantity<=0)
+                                batchesDel.Add(inventory_Batch);
+                            else
+                                batchesUp.Add(inventory_Batch);
                             cabinOrderDetailsUp.Add(orderDetail);
-                            #region 娣诲姞鐩樼泩鍏ュ簱浠诲姟
+                            #region 娣诲姞鐩樹簭鍑哄簱浠诲姟
                             Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                             {
                                 WarehouseCode = inventoryInfo.WarehouseCode,
@@ -78,6 +109,7 @@
                             #endregion
                         }
                         deliveryOrder.OutStatus = "宸插畬鎴�";
+                        //浠庢墍鏈夊嚭搴撴槑缁嗕腑锛岀瓫閫夊嚭閭d簺涓嶅湪宸插畬鎴愬垪琛ㄤ腑鐨勬槑缁嗛」
                         if (deliveryOrder.Details.Where(x => !cabinOrderDetailsUp.Select(x => x.Id).Contains(x.Id)).Any())
                             deliveryOrder.OutStatus = "寮�濮�";
                         _unitOfWorkManage.BeginTran();
@@ -86,6 +118,8 @@
                         _inventory_BatchServices.UpdateData(batchesUp);
                         _deliveryOrderDetailServices.UpdateData(cabinOrderDetailsUp);
                         BaseDal.UpdateData(deliveryOrder);
+                        //娣诲姞鐗╂枡淇℃伅涓殑绔嬪簱涓氬姟搴撳瓨淇℃伅
+                        _materielInfoService.UpdateData(MaterieInfoBusiness);
                         _unitOfWorkManage.CommitTran();
                     }
                     catch (Exception ex)
@@ -102,9 +136,17 @@
                     {
                         decimal orderQty = detail.orderDetails?.Sum(x => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0) ?? 0;
                         Dt_SupplyTask? supplyTask = supplyTasks.FirstOrDefault(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo);
-                        if (supplyTask == null) throw new Exception($"鏈壘鍒拌鍗曞彿銆恵request.externalOrderNo}銆戠墿鏂欑紪鍙枫�恵detail.productCode}銆戠殑鍑哄簱浠诲姟");
+                        if (supplyTask == null)
+                        {
+                            _messageInfoService.AddMessageInfo(MessageGroupByEnum.OutOrderAlarm, "鍑哄簱鍗�", $"鏈壘鍒拌鍗曞彿銆恵request.externalOrderNo}銆戠墿鏂欑紪鍙枫�恵detail.productCode}銆戠殑鍑哄簱浠诲姟");
+                            throw new Exception($"鏈壘鍒拌鍗曞彿銆恵request.externalOrderNo}銆戠墿鏂欑紪鍙枫�恵detail.productCode}銆戠殑鍑哄簱浠诲姟");
+                        }
                         if (supplyTask.StockQuantity != orderQty)
+                        {
+                            _messageInfoService.AddMessageInfo(MessageGroupByEnum.OutOrderAlarm, "鍑哄簱鍗�", $"璁㈠崟鍙枫�恵request.orderNo}銆戠墿鏂欑紪鍙枫�恵detail.productCode}銆戠殑鍑哄簱鏁伴噺涓庡嚭搴撲换鍔℃暟閲忎笉鐩哥瓑");
                             throw new Exception($"璁㈠崟鍙枫�恵request.orderNo}銆戠墿鏂欑紪鍙枫�恵detail.productCode}銆戠殑鍑哄簱鏁伴噺涓庡嚭搴撲换鍔℃暟閲忎笉鐩哥瓑");
+                        }
+                        //澶勭悊鍑哄簱鍗曘�佸簱瀛樹俊鎭�佸嚭搴撲换鍔°�佽皟鎷ㄤ换鍔�
                         content = OutTaskFinish(supplyTask, deliveryOrder);
                         if (!content.Status) throw new Exception(content.Message);
                     }

--
Gitblit v1.9.3