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/extend/Check.cs |  114 ++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 84 insertions(+), 30 deletions(-)

diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/extend/Check.cs" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/extend/Check.cs"
index 7a2e10e..fa1f6cf 100644
--- "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/extend/Check.cs"
+++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/extend/Check.cs"
@@ -3,6 +3,7 @@
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using WIDESEA_Common;
 using WIDESEA_Common.OrderEnum;
 using WIDESEA_Common.StockEnum;
 using WIDESEA_Common.TaskEnum;
@@ -32,13 +33,21 @@
                 //List<Dt_SupplyTask> supplyTasks = new List<Dt_SupplyTask>();
                 List<Dt_SupplyTask> supplyTasks1 = new List<Dt_SupplyTask>();
                 List<Dt_InventoryInfo> _InventoryInfos = new List<Dt_InventoryInfo>();
-                List<Dt_Inventory_Batch> inventory_Batches = _inventory_BatchServices.Repository.QueryData(x => keys.Contains(x.Id));
+                List<Dt_Inventory_Batch> inventory_Batches = _inventory_BatchServices.Repository.QueryData(x => keys.Contains(x.Id)).ToList();
                 foreach (var item in inventory_Batches)
                 {
                     var supplyTasks = _supplyTaskService.Repository.QueryData(x => x.MaterielCode == item.MaterielCode && x.BatchNo == item.BatchNo && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt()).ToList();
+                    //if (item.SupplyQuantity != 0) throw new Exception($"鐗╂枡缂栧彿銆恵item.MaterielCode}銆戞壒娆″彿銆恵item.BatchNo}銆戝凡瀛樺湪鐩樼偣宸紓!");
+                    if (item.SupplyQuantity != 0)
+                    {
+                        item.SupplyQuantity = 0;
+                        // 杩欓噷闇�瑕佹洿鏂版壒娆¤〃鐨勫樊寮傛暟閲�
+                        _inventory_BatchServices.UpdateData(item);
+                    }
+
                     if (supplyTasks.Count > 0) throw new Exception($"鐗╂枡缂栧彿銆恵item.MaterielCode}銆戞壒娆″彿銆恵item.BatchNo}銆戝凡瀛樺湪鐩樼偣浠诲姟锛岃鍏堝畬鎴愬綋鍓嶇洏鐐逛换鍔�");
                     supplyTasks = _supplyTaskService.Repository.QueryData(x => x.MaterielCode == item.MaterielCode && x.BatchNo == item.BatchNo && x.TaskStatus == SupplyStatusEnum.NewOut.ObjToInt()).ToList();
-                    if (supplyTasks.Count > 0) throw new Exception($"鐗╂枡缂栧彿銆恵item.MaterielCode}銆戞壒娆″彿銆恵item.BatchNo}銆戝凡瀛樺湪鐩樼偣浠诲姟锛岃鍏堝畬鎴愬綋鍓嶇洏鐐逛换鍔�");
+                    if (supplyTasks.Count > 0) throw new Exception($"鐗╂枡缂栧彿銆恵item.MaterielCode}銆戞壒娆″彿銆恵item.BatchNo}銆戝凡瀛樺湪鍑哄簱浠诲姟锛岃鍏堝畬鎴愬綋鍓嶅嚭搴撲换鍔�");
                     List<Dt_InventoryInfo> inventoryInfos = _inventoryInfoService.Repository.QueryData(x => item.MaterielCode == x.MaterielCode && x.BatchNo == item.BatchNo);
                     if (inventoryInfos.Count > 0)
                     {
@@ -77,6 +86,8 @@
                             #region 鍒涘缓鐩樼偣浠诲姟
                             foreach (var inventory in items)
                             {
+                                //鍦ㄥ垱寤虹洏鐐逛换鍔℃椂閲嶇疆搴撳瓨鐨勭洏鐐瑰樊寮備负0
+                                inventory.SupplyQuantity = 0;
                                 Dt_SupplyTask supplyTask = new Dt_SupplyTask()
                                 {
                                     WarehouseCode = inventory.WarehouseCode,
@@ -204,7 +215,7 @@
             {
                 var LocationCode = saveModel.MainData["locationCode"].ToString();
                 var TaskId = saveModel.MainData["taskId"].ObjToInt();
-                var qty = saveModel.MainData["qty"].ObjToInt();
+                var qty = saveModel.MainData["qty"].ObjToDecimal();
                 Dt_SupplyTask supplyTask = _supplyTaskService.Repository.QueryFirst(x => x.TaskId == TaskId && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt());
                 if (supplyTask == null) throw new Exception("褰撳墠鐩樼偣浠诲姟宸插畬鎴�");
                 if (supplyTask.LocationCode != LocationCode) throw new Exception($"褰撳墠鐩樼偣璐т綅銆恵LocationCode}銆戜笌浠诲姟鍒嗛厤璐т綅涓嶅尮閰�");
@@ -234,7 +245,7 @@
                 if (materielInfo == null) return WebResponseContent.Instance.Error($"璇风淮鎶ょ墿鏂欑紪鍙枫�恵cabinOrderDetail.Goods_no}銆戠殑鐗╂枡淇℃伅");
                 cabinOrderDetail.Order_Outqty += supplyTask.SupplyQuantity;
                 #region 骞冲簱鐩樼偣鍙兘鍑虹幇鏁寸宸紓
-                if (supplyTask.WarehouseCode == WarehouseEnum.澶т欢搴�.ObjToInt().ToString("000"))
+                if (supplyTask.WarehouseCode == WarehouseEnum.澶т欢搴�.ObjToInt().ToString("000") && materielInfo.MaterielSourceType != MaterielSourceTypeEnum.PurchasePart)
                 {
                     var Qty = supplyTask.SupplyQuantity % materielInfo.BoxQty;
                     if (Qty != 0) return WebResponseContent.Instance.Error($"澶т欢搴撳簱瀛樹负鏁寸锛佺洏鐐规暟閲忎笉鑳藉瓨鍦ㄦ暎浠�");
@@ -247,52 +258,95 @@
                 #endregion
 
                 #region 搴撳瓨
-                Dt_InventoryInfo inventoryInfo = new Dt_InventoryInfo();
+                List<Dt_Inventory_Batch> inventory_Batchs = _inventory_BatchServices.Repository.QueryData(x => cabinOrder.Details.Select(x => x.Goods_no).Contains(x.MaterielCode)).ToList();
+                List<Dt_InventoryInfo> inventoryInfos = _inventoryInfoService.Repository.QueryData(x => cabinOrder.Details.Select(x => x.Goods_no).Contains(x.MaterielCode) && x.WarehouseCode == supplyTask.WarehouseCode && x.StockStatus == StockStatusEmun.鐩樼偣閿佸畾.ObjToInt()).ToList();
+                Dt_InventoryInfo? inventoryInfo = new();
+                List<Dt_InventoryInfo> inventoryInfosDel = new List<Dt_InventoryInfo>();
                 if (supplyTask.WarehouseCode != WarehouseEnum.绔嬪簱.ObjToInt().ToString("000"))
                 {
-                    inventoryInfo = _inventoryInfoService.Repository.QueryFirst(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no && x.WarehouseCode == supplyTask.WarehouseCode && x.LocationCode == supplyTask.LocationCode);
+                    inventoryInfo = inventoryInfos.FirstOrDefault(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no && x.LocationCode == supplyTask.LocationCode);
                     if (inventoryInfo == null) return WebResponseContent.Instance.Error($"鏈壘鍒拌揣浣嶃�恵LocationCode}銆戠殑搴撳瓨淇℃伅");
                 }
                 else
                 {
-                    inventoryInfo = _inventoryInfoService.Repository.QueryFirst(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no && x.WarehouseCode == supplyTask.WarehouseCode);
+                    inventoryInfo = inventoryInfos.FirstOrDefault(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no);
                 }
-                inventoryInfo.SupplyQuantity = supplyTask.SupplyQuantity - inventoryInfo.StockQuantity;
+                if (inventoryInfo == null) return WebResponseContent.Instance.Error($"鏈壘鍒扮珛搴撶殑搴撳瓨淇℃伅");
+                inventoryInfo.SupplyQuantity = supplyTask.SupplyQuantity - inventoryInfo.StockQuantity;//鐩樼偣宸紓鏁�
                 #endregion
 
-                #region 鍒ゆ柇褰撳墠鐗╂枡鎵规鐨勭洏鐐逛换鍔℃槸鍚﹀叏閮ㄥ畬鎴�
-                List<Dt_SupplyTask> supplyTasks = _supplyTaskService.Repository.QueryData(x => x.MaterielCode == supplyTask.MaterielCode && x.BatchNo == supplyTask.BatchNo && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt() && x.WarehouseCode == supplyTask.WarehouseCode && x.TaskId != supplyTask.TaskId).ToList();
-                List<Dt_SupplyTask> supplyTasks1 = _supplyTaskService.Repository.QueryData(x => x.MaterielCode == supplyTask.MaterielCode && x.BatchNo == supplyTask.BatchNo && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt() && x.TaskId != supplyTask.TaskId).ToList();
-                Dt_Inventory_Batch inventory_Batch = new Dt_Inventory_Batch();
-                List<Dt_InventoryInfo> inventoryInfos = new List<Dt_InventoryInfo>();
-                if (supplyTasks1.Count < 1)
+                List<Dt_SupplyTask> supplyTasks = _supplyTaskService.Repository.QueryData(x => x.OrderNo == supplyTask.OrderNo && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt() && x.TaskId != supplyTask.TaskId).ToList();//鏌ユ壘褰撳墠鐩樼偣鍗曠殑鐩樼偣浠诲姟
+                if (!supplyTasks.Where(x => cabinOrderDetail.Goods_no == x.MaterielCode && x.BatchNo == cabinOrderDetail.Batch_num).Any())
+                    cabinOrderDetail.OotDetailStatus = "宸插畬鎴�";
+                #region 鍒ゆ柇褰撳墠鐩樼偣鍗曚换鍔℃槸鍚﹀叏閮ㄥ畬鎴�
+                if (supplyTasks.Count < 1)
                 {
-                    #region 鏌ユ壘搴撳瓨
-                    inventoryInfos = _inventoryInfoService.Repository.QueryData(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no && x.Id != inventoryInfo.Id);
-                    var SupplyQuantitys = inventoryInfos.Sum(x => x.SupplyQuantity) + inventoryInfo.SupplyQuantity;//璁$畻鐩樼偣鎬绘暟
-                    #endregion
-
-                    #region 搴撳瓨鎵规
-                    inventory_Batch = _inventory_BatchServices.Repository.QueryFirst(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no);
-                    
-                    if (SupplyQuantitys == 0)
+                    #region 澶勭悊褰撳墠鐗╂枡鎵规鐨勫簱瀛樺拰鎵规淇℃伅
+                    Dt_Inventory_Batch inventory_Batch = inventory_Batchs.First(x => x.BatchNo == inventoryInfo.BatchNo && x.MaterielCode == inventoryInfo.MaterielCode);
+                    var SupplyQuantitys = inventoryInfos.Where(x => x.BatchNo == inventoryInfo.BatchNo && x.MaterielCode == inventoryInfo.MaterielCode).Sum(x => x.SupplyQuantity);
+                    if (SupplyQuantitys == 0)//涓嶅瓨鍦ㄧ洏鐐瑰樊寮�
                     {
-                        foreach (var item in inventoryInfos)
+                        foreach (var item in inventoryInfos.Where(x => x.BatchNo == inventoryInfo.BatchNo && x.MaterielCode == inventoryInfo.MaterielCode))
                         {
                             item.StockQuantity += item.SupplyQuantity;
+                            if (item.StockQuantity <= 0)
+                            {
+                                inventoryInfosDel.Add(item);
+                                continue;
+                            }
+                            item.AvailableQuantity = item.StockQuantity;
                             item.SupplyQuantity = 0;
                             item.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt();
                         }
-                        inventoryInfo.StockQuantity += inventoryInfo.SupplyQuantity;
-                        inventoryInfo.SupplyQuantity = 0;
-                        inventoryInfo.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt();
+                        //inventoryInfo.StockQuantity += inventoryInfo.SupplyQuantity;
+                        //if (inventoryInfo.StockQuantity <= 0)
+                        //{
+                        //    inventoryInfosDel.Add(inventoryInfo);
+                        //    inventoryInfo = null;
+                        //}
+                        //else
+                        //{
+                        //    inventoryInfo.AvailableQuantity = inventoryInfo.StockQuantity;
+                        //    inventoryInfo.SupplyQuantity = 0;
+                        //    inventoryInfo.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt();
+                        //}
                     }
                     else
                     {
                         inventory_Batch.SupplyQuantity += SupplyQuantitys;
                     }
                     #endregion
+
+                    #region 澶勭悊鍏朵粬搴撳瓨鎵规鍜屽簱瀛�
+                    foreach (var detail in cabinOrder.Details.Where(x => x.Id != cabinOrderDetail.Id))
+                    {
+                        SupplyQuantitys = inventoryInfos.Where(x => x.BatchNo == detail.Batch_num && x.MaterielCode == detail.Goods_no).Sum(x => x.SupplyQuantity);//璁$畻鐩樼偣鎬绘暟
+                        inventory_Batch = inventory_Batchs.First(x => x.BatchNo == detail.Batch_num && x.MaterielCode == detail.Goods_no);
+                        #region 搴撳瓨鎵规
+                        if (SupplyQuantitys == 0)
+                        {
+                            foreach (var item in inventoryInfos.Where(x => x.BatchNo == detail.Batch_num && x.MaterielCode == detail.Goods_no))
+                            {
+                                item.StockQuantity += item.SupplyQuantity;
+                                if (item.StockQuantity <= 0)
+                                {
+                                    inventoryInfosDel.Add(item);
+                                    continue;
+                                }
+                                item.AvailableQuantity = item.StockQuantity;
+                                item.SupplyQuantity = 0;
+                                item.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt();
+                            }
+                        }
+                        else
+                        {
+                            inventory_Batch.SupplyQuantity += SupplyQuantitys;
+                        }
+                        #endregion
+                    }
+                    #endregion
                 }
+                #endregion
                 _unitOfWorkManage.BeginTran();
                 #region 澶勭悊鐩樼偣鍗�
                 if (supplyTasks.Count < 1)
@@ -315,12 +369,12 @@
                     _deliveryOrderDetailServices.Repository.UpdateData(cabinOrderDetail);
                 }
                 #endregion
-                _inventoryInfoService.UpdateData(inventoryInfo);
+                //_inventoryInfoService.UpdateData(inventoryInfo);
                 _supplyTaskService.Repository.DeleteAndMoveIntoHty(supplyTask, OperateTypeEnum.鑷姩瀹屾垚);
                 _inventoryInfoService.UpdateData(inventoryInfos);
-                _inventory_BatchServices.UpdateData(inventory_Batch);
+                if (inventoryInfosDel.Count > 0) _inventoryInfoService.DeleteData(inventoryInfosDel);
+                _inventory_BatchServices.UpdateData(inventory_Batchs);
                 _unitOfWorkManage.CommitTran();
-                #endregion
                 content.OK();
             }
             catch (Exception ex)

--
Gitblit v1.9.3