From d4fc143136f77f0bb68e0ec7579d250a6ae550b6 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期五, 24 十月 2025 16:31:28 +0800
Subject: [PATCH] 优化出库单分配逻辑及平库出库任务生成

---
 新建文件夹/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/DeliveryOrderServices.cs |  313 +++++++++++++++++++++++++++++++++++++++++++++++++++
 新建文件夹/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/extend/Allocat.cs        |    2 
 2 files changed, 310 insertions(+), 5 deletions(-)

diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/DeliveryOrderServices.cs" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/DeliveryOrderServices.cs"
index 17c4d11..1df1691 100644
--- "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/DeliveryOrderServices.cs"
+++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/DeliveryOrderServices.cs"
@@ -365,31 +365,334 @@
                     };
                     BaseDal.Db.InsertNav(entityOrder).Include(x => x.Details).ExecuteCommand();
                     #endregion
-                    List<Dt_InventoryInfo> dt_InventoryInfos = new List<Dt_InventoryInfo>();
-                    #region 澶勭悊搴撳瓨銆佹坊鍔犲嚭搴撲换鍔�
+                    #region 澶勭悊搴撳瓨銆佸簱瀛樻壒娆°�佹坊鍔犲嚭搴撲换鍔�
                     foreach (var item in entityOrder.Details)
                     {
-                        dt_InventoryInfos = _inventoryInfoService.Repository.QueryData(x => x.MaterielCode == item.Goods_no && x.StockStatus == StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt() && x.StockQuantity > x.OutboundQuantity && x.WarehouseCode == item.Reservoirarea);
+                        Dt_Inventory_Batch inventory_Batch = _inventory_BatchServices.Repository.QueryFirst(x => x.MaterielCode == item.Goods_no && x.BatchNo == item.Batch_num);
+                        if (inventory_Batch == null) throw new Exception($"鏈壘鍒板嚭搴撳崟鍙枫�恵entityOrder.Out_no}銆戜腑鐗╂枡缂栧彿銆恵item.Goods_no}銆戠墿鏂欐壒娆°�恵item.Batch_num}銆戠殑搴撳瓨鎵规淇℃伅");
+                        if (inventory_Batch.AvailableQuantity < item.Order_qty) throw new Exception($"鍑哄簱鍗曞彿銆恵entityOrder.Out_no}銆戜腑鐗╂枡缂栧彿銆恵item.Goods_no}銆戠墿鏂欐壒娆°�恵item.Batch_num}銆戠殑搴撳瓨鎵规淇℃伅鍙敤鏁伴噺涓嶈冻");
+                        inventory_Batch.AvailableQuantity -= item.Order_qty;
+                        inventory_Batch.OutboundQuantity += item.Order_qty;
+                        List<Dt_InventoryInfo> dt_InventoryInfos = _inventoryInfoService.Repository.QueryData(x => x.MaterielCode == item.Goods_no && x.BatchNo == item.Batch_num && x.StockStatus == StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt() && x.StockQuantity > x.OutboundQuantity && x.WarehouseCode == item.Reservoirarea);
                         #region 鎸夊嚭搴撶瓥鐣ユ煡鎵惧簱瀛�
                         if (tactics.SelectTactice == TacticsEnum.ComeOutonFirstTime.ObjToInt())
                             dt_InventoryInfos = dt_InventoryInfos.OrderBy(x => x.ValidityPeriod).ToList();
                         else
                             dt_InventoryInfos = dt_InventoryInfos.OrderBy(x => x.InDate).ToList();
                         #endregion
-                        var Order_qty = item.Order_qty;
+                        var Order_qty = item.Order_qty;//鍑哄簱鍗曟暟閲�
                         foreach (var InventoryInfo in dt_InventoryInfos)
                         {
+                            if (Order_qty <= 0) break;
                             if (InventoryInfo.AvailableQuantity < Order_qty)
                             {
-
+                                InventoryInfo.AvailableQuantity = 0;
+                                Order_qty -= InventoryInfo.AvailableQuantity;
+                                InventoryInfo.OutboundQuantity += InventoryInfo.AvailableQuantity;
+                                InventoryInfo.StockStatus = StockStatusEmun.鍑哄簱閿佸畾.ObjToInt();
+                            }
+                            else
+                            {
+                                InventoryInfo.AvailableQuantity -= Order_qty;
+                                Order_qty = 0;
+                                InventoryInfo.OutboundQuantity += Order_qty;
+                                InventoryInfo.StockStatus = StockStatusEmun.鍑哄簱閿佸畾.ObjToInt();
                             }
                         }
+                        dt_InventoryInfos = dt_InventoryInfos.Where(X => X.StockStatus == StockStatusEmun.鍑哄簱閿佸畾.ObjToInt()).ToList();
+                        List<Dt_SupplyTask> supplyTasks = new List<Dt_SupplyTask>();
+                        foreach (var inventoryInfo in dt_InventoryInfos)
+                        {
+                            Dt_SupplyTask supplyTask = new Dt_SupplyTask()
+                            {
+                                WarehouseCode = inventoryInfo.WarehouseCode,
+                                BatchNo = inventoryInfo.BatchNo,
+                                MaterielName = inventoryInfo.MaterielName,
+                                MaterielCode = inventoryInfo.MaterielCode,
+                                MaterielSpec = inventoryInfo.MaterielSpec,
+                                TaskStatus = SupplyStatusEnum.NewOut.ObjToInt(),
+                                TaskType = TaskTypeEnum.OutPick.ObjToInt(),
+                                CreateDate = DateTime.Now,
+                                Creater = App.User.UserName ?? "System",
+                                LocationCode = inventoryInfo.LocationCode,
+                                OrderNo = entityOrder.Out_no,
+                                StockQuantity = inventoryInfo.OutboundQuantity,
+                                SupplyQuantity = 0,
+                                Remark = "鍑哄簱"
+                            };
+                            supplyTasks.Add(supplyTask);
+                        }
+                        _inventory_BatchServices.Repository.UpdateData(inventory_Batch);
+                        _inventoryInfoService.Repository.UpdateData(dt_InventoryInfos);
+                        _supplyTaskService.AddData(supplyTasks);
                     }
                     #endregion
                 }
                 else
                 {
+                    #region 鍒涘缓澶т欢搴撱�佺珛搴撳嚭搴撳ご琛�
+                    var entityOrder = new Dt_DeliveryOrder
+                    {
+                        Out_no = outorder.order_no,
+                        Out_type = outorder.order_type,
+                        OutStatus = "鏂板缓",
+                        Client_name = outorder.client_name,
+                        Account_time = outorder.account_time,
+                        Client_no = outorder.client_no,
+                        Warehouse_no = WarehouseEnum.澶т欢搴�.ObjToInt().ToString("000"),
+                        Details = new List<Dt_DeliveryOrderDetail>()
+                    };
+                    var entityOrderLK = new Dt_DeliveryOrder
+                    {
+                        Out_no = outorder.order_no,
+                        Out_type = outorder.order_type,
+                        OutStatus = "鏂板缓",
+                        Client_name = outorder.client_name,
+                        Account_time = outorder.account_time,
+                        Client_no = outorder.client_no,
+                        Warehouse_no = WarehouseEnum.绔嬪簱.ObjToInt().ToString("000"),
+                        Details = new List<Dt_DeliveryOrderDetail>()
+                    };
+                    #endregion
+                    #region 鏌ユ壘搴撳瓨
+                    foreach (var detail in outorder.details)
+                    {
+                        #region 鏌ヨ搴撳瓨鎵规鍜屽簱瀛�
+                        Dt_Inventory_Batch inventory_Batch = _inventory_BatchServices.Repository.QueryFirst(x => x.MaterielCode == detail.goods_no && x.BatchNo == detail.batch_num);
+                        if (inventory_Batch == null) throw new Exception($"鏈壘鍒板嚭搴撳崟鍙枫�恵outorder.order_no}銆戜腑鐗╂枡缂栧彿銆恵detail.goods_no}銆戠墿鏂欐壒娆°�恵detail.batch_num}銆戠殑搴撳瓨鎵规淇℃伅");
+                        if (inventory_Batch.AvailableQuantity < detail.order_qty) throw new Exception($"鍑哄簱鍗曞彿銆恵outorder.order_no}銆戜腑鐗╂枡缂栧彿銆恵detail.goods_no}銆戠墿鏂欐壒娆°�恵detail.batch_num}銆戠殑搴撳瓨鎵规淇℃伅鍙敤鏁伴噺涓嶈冻");
+                        inventory_Batch.AvailableQuantity -= detail.order_qty;
+                        inventory_Batch.OutboundQuantity += detail.order_qty;
+                        List<Dt_InventoryInfo> dt_InventoryInfos = _inventoryInfoService.Repository.QueryData(x => x.MaterielCode == inventory_Batch.MaterielCode && x.BatchNo == inventory_Batch.BatchNo && x.StockStatus == StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt() && x.StockQuantity > x.OutboundQuantity && (x.WarehouseCode == WarehouseEnum.澶т欢搴�.ObjToInt().ToString("000") || x.WarehouseCode == WarehouseEnum.绔嬪簱.ObjToInt().ToString("000")));
+                        #endregion
+                        #region 鎸夊嚭搴撶瓥鐣ユ煡鎵惧簱瀛�
+                        if (tactics.SelectTactice == TacticsEnum.ComeOutonFirstTime.ObjToInt())
+                            dt_InventoryInfos = dt_InventoryInfos.OrderBy(x => x.ValidityPeriod).ToList();
+                        else
+                            dt_InventoryInfos = dt_InventoryInfos.OrderBy(x => x.InDate).ToList();
+                        #endregion
+                        var Order_qty = Math.Abs(detail.order_qty);//鍑哄簱鍗曟暟閲�
+                        #region 鏍规嵁鐗╂枡缂栫爜鏌ヨ鐗╂枡淇℃伅
+                        Dt_MaterielInfo materielInfo = _basicService.MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == detail.goods_no);
+                        if (materielInfo == null) throw new Exception($"鏈壘鍒拌嵂鍝佺紪鐮併�恵detail.goods_no}銆戠殑淇℃伅");
+                        if (!Enum.IsDefined(typeof(MaterielSourceTypeEnum), materielInfo.MaterielSourceType))
+                            throw new Exception($"璇疯缃嵂鍝佺紪鍙枫�恵detail.goods_no}銆戠殑灞炴�у垎绫�");
+                        if (materielInfo.BoxQty < 1) throw new Exception($"璇疯缃嵂鍝佺紪鍙枫�恵detail.goods_no}銆戠殑绠辫鏁伴噺");
+                        #endregion
+                        #region 澶т欢
+                        if (materielInfo.MaterielSourceType == MaterielSourceTypeEnum.PurchasePart)//濡傛灉鐗╂枡鏄ぇ浠�
+                        {
+                            #region 娣诲姞鍑哄簱璇︽儏
+                            Dt_DeliveryOrderDetail orderDetail = new Dt_DeliveryOrderDetail()
+                            {
+                                Reservoirarea = entityOrder.Warehouse_no,
+                                Goods_no = detail.goods_no,
+                                Order_qty = detail.order_qty,
+                                Batch_num = detail.batch_num,
+                                Exp_date = detail.exp_date,
+                                OotDetailStatus = "鏂板缓",
+                                Order_Outqty = 0,
+                                Status = 2
+                            };
+                            entityOrder.Details.Add(orderDetail);
+                            #endregion
 
+                            #region 璁$畻搴撳瓨
+                            foreach (var InventoryInfo in dt_InventoryInfos)
+                            {
+                                if (Order_qty <= 0) break;
+                                if (InventoryInfo.AvailableQuantity < Order_qty)
+                                {
+                                    InventoryInfo.AvailableQuantity = 0;
+                                    Order_qty -= InventoryInfo.AvailableQuantity;
+                                    InventoryInfo.OutboundQuantity += InventoryInfo.AvailableQuantity;
+                                    InventoryInfo.StockStatus = StockStatusEmun.鍑哄簱閿佸畾.ObjToInt();
+                                }
+                                else
+                                {
+                                    InventoryInfo.AvailableQuantity -= Order_qty;
+                                    Order_qty = 0;
+                                    InventoryInfo.OutboundQuantity += Order_qty;
+                                    InventoryInfo.StockStatus = StockStatusEmun.鍑哄簱閿佸畾.ObjToInt();
+                                }
+                            }
+                            #endregion
+
+                            #region 鐢熸垚鍑哄簱浠诲姟
+                            dt_InventoryInfos = dt_InventoryInfos.Where(X => X.StockStatus == StockStatusEmun.鍑哄簱閿佸畾.ObjToInt()).ToList();
+                            List<Dt_SupplyTask> supplyTasks = new List<Dt_SupplyTask>();
+                            foreach (var inventoryInfo in dt_InventoryInfos)
+                            {
+                                Dt_SupplyTask supplyTask = new Dt_SupplyTask()
+                                {
+                                    WarehouseCode = inventoryInfo.WarehouseCode,
+                                    BatchNo = inventoryInfo.BatchNo,
+                                    MaterielName = inventoryInfo.MaterielName,
+                                    MaterielCode = inventoryInfo.MaterielCode,
+                                    MaterielSpec = inventoryInfo.MaterielSpec,
+                                    TaskStatus = SupplyStatusEnum.NewOut.ObjToInt(),
+                                    TaskType = TaskTypeEnum.OutPick.ObjToInt(),
+                                    CreateDate = DateTime.Now,
+                                    Creater = App.User.UserName ?? "System",
+                                    LocationCode = inventoryInfo.LocationCode,
+                                    OrderNo = entityOrder.Out_no,
+                                    StockQuantity = inventoryInfo.OutboundQuantity,
+                                    SupplyQuantity = 0,
+                                    Remark = "鍑哄簱"
+                                };
+                                supplyTasks.Add(supplyTask);
+                            }
+                            #endregion
+                            _inventory_BatchServices.Repository.UpdateData(inventory_Batch);
+                            _inventoryInfoService.Repository.UpdateData(dt_InventoryInfos);
+                            _supplyTaskService.AddData(supplyTasks);
+                        }
+                        #endregion
+                        else
+                        {
+                            Dt_DeliveryOrderDetail orderDetail = null;
+                            Dt_DeliveryOrderDetail orderDetailLK = null;
+                            var ys = Order_qty % materielInfo.BoxQty; //涓嶈兘鏁撮櫎绠辫鐨勬暎浠舵暟 
+                            var xs = (int)(Order_qty / materielInfo.BoxQty);//淇濈暀鏁存暟
+                            #region 鏁d欢浼樺厛鍒嗛厤绔嬪簱
+                            if (ys > 0)
+                            {
+                                orderDetailLK = new Dt_DeliveryOrderDetail()
+                                {
+                                    Reservoirarea = entityOrderLK.Warehouse_no,
+                                    Goods_no = detail.goods_no,
+                                    Order_qty = ys,
+                                    Batch_num = detail.batch_num,
+                                    Exp_date = detail.exp_date,
+                                    OotDetailStatus = "鏂板缓",
+                                    Order_Outqty = 0,
+                                    Status = 0
+                                };
+                            }
+                            #endregion
+
+                            #region 鏁翠欢浼樺厛鍒嗛厤澶т欢搴�
+                            foreach (var item in dt_InventoryInfos.Where(x => x.WarehouseCode == WarehouseEnum.澶т欢搴�.ObjToInt().ToString("000")))
+                            {
+                                if (xs <= 0) break;
+                                item.StockStatus = StockStatusEmun.鍑哄簱閿佸畾.ObjToInt();
+                                while (item.AvailableQuantity > 0 && xs > 0)
+                                {
+                                    xs--;
+                                    if (orderDetail == null)
+                                    {
+                                        orderDetail = new Dt_DeliveryOrderDetail()
+                                        {
+                                            Reservoirarea = entityOrder.Warehouse_no,
+                                            Goods_no = detail.goods_no,
+                                            Order_qty = materielInfo.BoxQty,
+                                            Batch_num = detail.batch_num,
+                                            Exp_date = detail.exp_date,
+                                            OotDetailStatus = "鏂板缓",
+                                            Order_Outqty = 0,
+                                            Status = 0
+                                        };
+                                        item.AvailableQuantity -= materielInfo.BoxQty;
+                                        item.OutboundQuantity += materielInfo.BoxQty;
+                                    }
+                                    else
+                                    {
+                                        orderDetail.Order_qty += materielInfo.BoxQty;
+                                        item.AvailableQuantity -= materielInfo.BoxQty;
+                                        item.OutboundQuantity += materielInfo.BoxQty;
+                                    }
+                                }
+                            }
+                            #endregion
+
+                            #region 鍒嗛厤瀹屽ぇ浠跺簱濡傛灉杩樻湁绠辨暟锛屽啀閫夋嫨鍒嗛厤绔嬪簱
+                            if (orderDetailLK == null)
+                            {
+                                orderDetailLK = new Dt_DeliveryOrderDetail()
+                                {
+                                    Reservoirarea = entityOrderLK.Warehouse_no,
+                                    Goods_no = detail.goods_no,
+                                    Order_qty = xs * materielInfo.BoxQty,
+                                    Batch_num = detail.batch_num,
+                                    Exp_date = detail.exp_date,
+                                    OotDetailStatus = "鏂板缓",
+                                    Order_Outqty = 0,
+                                    Status = 0
+                                };
+                            }
+                            else
+                            {
+                                orderDetailLK.Order_qty += xs * materielInfo.BoxQty;
+                            }
+                            #endregion
+                            List<Dt_SupplyTask> supplyTasks = new List<Dt_SupplyTask>();
+                            if (orderDetailLK != null)
+                            {
+                                #region 娣诲姞鍑哄簱浠诲姟銆佷慨鏀瑰簱瀛樹俊鎭�
+                                Dt_InventoryInfo inventoryInfo = dt_InventoryInfos.Where(x => x.WarehouseCode == WarehouseEnum.绔嬪簱.ObjToInt().ToString("000")).First();
+                                inventoryInfo.AvailableQuantity -= orderDetailLK.Order_qty;
+                                inventoryInfo.OutboundQuantity += orderDetailLK.Order_qty;
+                                inventoryInfo.StockStatus = StockStatusEmun.鍑哄簱閿佸畾.ObjToInt();
+                                _inventoryInfoService.Repository.UpdateData(inventoryInfo);
+                                Dt_SupplyTask supplyTask = new Dt_SupplyTask()
+                                {
+                                    WarehouseCode = inventoryInfo.WarehouseCode,
+                                    BatchNo = inventoryInfo.BatchNo,
+                                    MaterielName = inventoryInfo.MaterielName,
+                                    MaterielCode = inventoryInfo.MaterielCode,
+                                    MaterielSpec = inventoryInfo.MaterielSpec,
+                                    TaskStatus = SupplyStatusEnum.NewOut.ObjToInt(),
+                                    TaskType = TaskTypeEnum.OutPick.ObjToInt(),
+                                    CreateDate = DateTime.Now,
+                                    Creater = App.User.UserName ?? "System",
+                                    LocationCode = inventoryInfo.LocationCode,
+                                    OrderNo = entityOrder.Out_no,
+                                    StockQuantity = inventoryInfo.OutboundQuantity,
+                                    SupplyQuantity = 0,
+                                    Remark = "鍑哄簱"
+                                };
+                                supplyTasks.Add(supplyTask);
+                                #endregion
+                                entityOrderLK.Details.Add(orderDetailLK);
+                            }
+                            if (orderDetail != null)
+                            {
+                                #region 娣诲姞鍑哄簱浠诲姟銆佷慨鏀瑰簱瀛樹俊鎭�
+                                dt_InventoryInfos = dt_InventoryInfos.Where(X => X.StockStatus == StockStatusEmun.鍑哄簱閿佸畾.ObjToInt()).ToList();
+                                foreach (var inventoryInfo in dt_InventoryInfos)
+                                {
+                                    Dt_SupplyTask supplyTask = new Dt_SupplyTask()
+                                    {
+                                        WarehouseCode = inventoryInfo.WarehouseCode,
+                                        BatchNo = inventoryInfo.BatchNo,
+                                        MaterielName = inventoryInfo.MaterielName,
+                                        MaterielCode = inventoryInfo.MaterielCode,
+                                        MaterielSpec = inventoryInfo.MaterielSpec,
+                                        TaskStatus = SupplyStatusEnum.NewOut.ObjToInt(),
+                                        TaskType = TaskTypeEnum.OutPick.ObjToInt(),
+                                        CreateDate = DateTime.Now,
+                                        Creater = App.User.UserName ?? "System",
+                                        LocationCode = inventoryInfo.LocationCode,
+                                        OrderNo = entityOrder.Out_no,
+                                        StockQuantity = inventoryInfo.OutboundQuantity,
+                                        SupplyQuantity = 0,
+                                        Remark = "鍑哄簱"
+                                    };
+                                    supplyTasks.Add(supplyTask);
+                                }
+                                #endregion
+                                entityOrder.Details.Add(orderDetail);
+                            }
+                            _inventory_BatchServices.Repository.UpdateData(inventory_Batch);
+                            _inventoryInfoService.Repository.UpdateData(dt_InventoryInfos);
+                            _supplyTaskService.AddData(supplyTasks);
+                        }
+                    }
+                    if (entityOrder.Details.Count > 0)
+                        BaseDal.Db.InsertNav(entityOrder).Include(x => x.Details).ExecuteCommand();
+                    if (entityOrderLK.Details.Count > 0)
+                        BaseDal.Db.InsertNav(entityOrderLK).Include(x => x.Details).ExecuteCommand();
+                    #endregion
                 }
             }
             catch (Exception ex)
diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/extend/Allocat.cs" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/extend/Allocat.cs"
index ec5ff26..97a1ef4 100644
--- "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/extend/Allocat.cs"
+++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/extend/Allocat.cs"
@@ -51,7 +51,9 @@
                     }
                     item.AvailableQuantity = item.StockQuantity - item.OutboundQuantity;
                 }
+                #region 澶т欢搴撹ˉ绔嬪簱鍚庣珛搴撲笟鍔″簱瀛樻暟杩樻槸灏忎簬绔嬪簱鏈�灏忓簱瀛樻暟锛屾坊鍔犳彁绀轰俊鎭�
 
+                #endregion
                 #region 娣诲姞璋冩嫧鍑哄簱鍗�
                 Dt_DeliveryOrder deliveryOrder = new Dt_DeliveryOrder()
                 {

--
Gitblit v1.9.3