From 5b8bf171b9afda52fa366a72e54eedbd9e6d4d17 Mon Sep 17 00:00:00 2001
From: yanjinhui <3306209981@qq.com>
Date: 星期六, 01 十一月 2025 19:54:25 +0800
Subject: [PATCH] Merge branch 'master' of http://115.159.85.185:8098/r/ZhiHuiQiCe/FangCangZhiNeng

---
 新建文件夹/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/LKBusiness/CabinOrderServicesLK.cs |  231 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 231 insertions(+), 0 deletions(-)

diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/LKBusiness/CabinOrderServicesLK.cs" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/LKBusiness/CabinOrderServicesLK.cs"
new file mode 100644
index 0000000..1392201
--- /dev/null
+++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/LKBusiness/CabinOrderServicesLK.cs"
@@ -0,0 +1,231 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using WIDESEA_Common.OrderEnum;
+using WIDESEA_Common.StockEnum;
+using WIDESEA_Common.TaskEnum;
+using WIDESEA_Core;
+using WIDESEA_Core.Enums;
+using WIDESEA_Core.Helper;
+using WIDESEA_Model.Models;
+using static WIDESEA_DTO.SquareCabin.OrderDto;
+using static WIDESEA_DTO.SquareCabin.TowcsDto;
+
+namespace WIDESEA_SquareCabinServices
+{
+    public partial class CabinOrderServices
+    {
+        /// <summary>
+        /// 绔嬪簱鍏ュ簱浠诲姟瀹屾垚
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        public WebResponseContent CompleteLKInOrder(Dt_CabinOrder cabinOrder, EdiOrderCallbackRequest request)
+        {
+            WebResponseContent content = new WebResponseContent();
+            try
+            {
+                List<Dt_CabinOrderDetail> cabinOrderDetailsUp = new List<Dt_CabinOrderDetail>();
+                List<Dt_SupplyTask_Hty> supplyTask_Hties = new List<Dt_SupplyTask_Hty>();
+                List<Dt_InventoryInfo> infosUp = new List<Dt_InventoryInfo>();
+                List<Dt_InventoryInfo> infosAdd = new List<Dt_InventoryInfo>();
+                List<Dt_Inventory_Batch> batchesUp = new List<Dt_Inventory_Batch>();
+                List<Dt_Inventory_Batch> batchesAdd = new List<Dt_Inventory_Batch>();
+                List<Dt_MaterielInfo> materielInfos = _materielInfoService.Repository.QueryData(x => cabinOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
+                List<Dt_InventoryInfo> inventoryInfos = _inventoryInfoService.Repository.QueryData(x => cabinOrder.Warehouse_no == x.WarehouseCode && cabinOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
+                List<Dt_Inventory_Batch> inventory_Batches = _inventory_BatchServices.Repository.QueryData(x => cabinOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
+                #region 鐩樼泩鍏ュ簱鍗�
+                if (cabinOrder.Order_type == "5")
+                {
+                    foreach (var detail in request.details)
+                    {
+                        Dt_CabinOrderDetail? orderDetail = cabinOrder.Details.FirstOrDefault(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo);
+                        if (orderDetail == null) throw new Exception($"鏈壘鍒扮洏鐐瑰崟銆恵cabinOrder.Order_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;
+                        inventoryInfo.SupplyQuantity = 0;
+                        inventoryInfo.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt();
+                        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.SupplyQuantity = 0;
+                        batchesUp.Add(inventory_Batch);
+                        orderDetail.OrderDetailStatus = "宸插畬鎴�";
+                        cabinOrderDetailsUp.Add(orderDetail);
+                        #region 娣诲姞鐩樼泩鍏ュ簱浠诲姟
+                        Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
+                        {
+                            WarehouseCode = inventoryInfo.WarehouseCode,
+                            OperateType = OperateTypeEnum.鑷姩瀹屾垚.ToString(),
+                            InsertTime = DateTime.Now,
+                            TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
+                            BatchNo = inventoryInfo.BatchNo,
+                            MaterielName = inventoryInfo.MaterielName,
+                            MaterielCode = inventoryInfo.MaterielCode,
+                            MaterielSpec = inventoryInfo.MaterielSpec,
+                            TaskType = TaskTypeEnum.ChenckIn.ObjToInt(),
+                            CreateDate = DateTime.Now,
+                            Creater = App.User.UserName,
+                            LocationCode = inventoryInfo.LocationCode,
+                            OrderNo = cabinOrder.Order_no,
+                            StockQuantity = orderQty,
+                            SupplyQuantity = 0,
+                            Remark = "鐩樼泩鍏ュ簱"
+                        };
+                        supplyTask_Hties.Add(supplyTask_Hty);
+                        #endregion
+                    }
+                    cabinOrder.OdrderStatus = "宸插畬鎴�";
+                    if (cabinOrder.Details.Where(x => !cabinOrderDetailsUp.Select(x => x.Id).Contains(x.Id)).Any())
+                        cabinOrder.OdrderStatus = "寮�濮�";
+                    _unitOfWorkManage.BeginTran();
+                    _supplyTaskHtyService.AddData(supplyTask_Hties);
+                    _inventoryInfoService.UpdateData(infosUp);
+                    _inventory_BatchServices.UpdateData(batchesUp);
+                    _cabinOrderDetailServices.UpdateData(cabinOrderDetailsUp);
+                    BaseDal.UpdateData(cabinOrder);
+                    _unitOfWorkManage.CommitTran();
+                }
+                #endregion
+                else
+                {
+                    foreach (var detail in request.details)
+                    {
+                        Dt_CabinOrderDetail? orderDetail = cabinOrder.Details.FirstOrDefault(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo);
+                        if (orderDetail == null) throw new Exception($"鏈壘鍒扮洏鐐瑰崟銆恵cabinOrder.Order_no}銆戠墿鏂欑紪鍙枫�恵detail.productCode}銆戠墿鏂欏悕绉般�恵detail.batchNo}銆戠殑鏄庣粏淇℃伅");
+                        if (orderDetail.OrderDetailStatus == "宸插畬鎴�") continue;
+                        decimal orderQty = detail.orderDetails?.Sum(x => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0) ?? 0;
+                        orderDetail.Order_Inqty += orderQty;
+                        if (orderDetail.Order_Inqty > orderDetail.Order_qty) throw new Exception($"鍏ュ簱鏁伴噺涓嶅彲瓒呭嚭鍗曟嵁鏁伴噺");
+                        orderDetail.OrderDetailStatus = "寮�濮�";
+                        cabinOrder.OdrderStatus = orderDetail.OrderDetailStatus;
+                        if (orderDetail.Order_Inqty == orderDetail.Order_qty)
+                        {
+                            orderDetail.OrderDetailStatus = "宸插畬鎴�";
+                            cabinOrder.Details.Remove(orderDetail);
+                        }
+                        cabinOrderDetailsUp.Add(orderDetail);
+                        Dt_MaterielInfo? materielInfo = materielInfos.FirstOrDefault(x => orderDetail.Goods_no == x.MaterielCode);
+                        if (materielInfo == null) throw new Exception($"鏈壘鍒扮墿鏂欑紪鐮併�恵orderDetail.Goods_no}銆戠殑鐗╂枡淇℃伅");
+                        #region 搴撳瓨
+                        Dt_InventoryInfo? dt_InventoryInfo = inventoryInfos.FirstOrDefault(x => x.MaterielCode == orderDetail.Goods_no && x.BatchNo == orderDetail.Batch_num);
+                        if (dt_InventoryInfo != null)
+                        {
+                            dt_InventoryInfo.StockQuantity += orderQty;
+                            dt_InventoryInfo.AvailableQuantity += orderQty;
+                            dt_InventoryInfo.InDate = DateTime.Now;
+                            infosUp.Add(dt_InventoryInfo);
+                        }
+                        else
+                        {
+                            dt_InventoryInfo = new Dt_InventoryInfo
+                            {
+                                PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "",
+                                WarehouseCode = "001",
+                                LocationCode = "绔嬪簱",
+                                StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt(),
+                                MaterielCode = materielInfo.MaterielCode,
+                                MaterielName = materielInfo.MaterielName,
+                                MaterielSpec = materielInfo.MaterielSpec,
+                                BatchNo = detail.batchNo,
+                                StockQuantity = orderQty,
+                                OutboundQuantity = 0,
+                                SupplyQuantity = 0,
+                                AvailableQuantity = orderQty,
+                                InDate = DateTime.Now,
+                                ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
+                                ShelfLife = 0,
+                                ValidityPeriod = orderDetail.Exp_date,
+                                Remark = "鏅鸿兘绔嬪簱"
+                            };
+                            infosAdd.Add(dt_InventoryInfo);
+                        }
+                        #endregion
+
+                        #region 搴撳瓨鎵规
+                        if (cabinOrder.Order_type != InOrderTypeEnum.Allocat.ObjToInt().ToString())
+                        {
+                            Dt_Inventory_Batch? inventory_Batch = inventory_Batches.FirstOrDefault(x => x.MaterielCode == dt_InventoryInfo.MaterielCode && x.BatchNo == dt_InventoryInfo.BatchNo);
+                            if (inventory_Batch != null)
+                            {
+                                inventory_Batch.StockQuantity += orderQty;
+                                inventory_Batch.AvailableQuantity += orderQty;
+                                batchesUp.Add(inventory_Batch);
+                            }
+                            else
+                            {
+                                inventory_Batch = new Dt_Inventory_Batch
+                                {
+                                    MaterielCode = dt_InventoryInfo.MaterielCode,
+                                    MaterielName = dt_InventoryInfo.MaterielName,
+                                    MaterielSpec = dt_InventoryInfo.MaterielSpec,
+                                    BatchNo = dt_InventoryInfo.BatchNo,
+                                    StockQuantity = dt_InventoryInfo.StockQuantity,
+                                    OutboundQuantity = dt_InventoryInfo.OutboundQuantity,
+                                    AvailableQuantity = dt_InventoryInfo.AvailableQuantity,
+                                    SupplyQuantity = dt_InventoryInfo.SupplyQuantity,
+                                    ERPStockQuantity = 0,
+                                    Status = false,
+                                    ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
+                                    ValidityPeriod = dt_InventoryInfo.ValidityPeriod.ObjToDate(),
+                                    Remark = "鑷姩鍒涘缓"
+                                };
+                                batchesAdd.Add(inventory_Batch);
+                            }
+                        }
+                        #endregion
+
+                        #region 娣诲姞鍏ュ簱浠诲姟鍘嗗彶
+                        Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
+                        {
+                            WarehouseCode = dt_InventoryInfo.WarehouseCode,
+                            TaskNum = dt_InventoryInfo.Id,
+                            OperateType = OperateTypeEnum.鑷姩瀹屾垚.ToString(),
+                            InsertTime = DateTime.Now,
+                            TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
+                            BatchNo = dt_InventoryInfo.BatchNo,
+                            MaterielName = dt_InventoryInfo.MaterielName,
+                            MaterielCode = dt_InventoryInfo.MaterielCode,
+                            MaterielSpec = dt_InventoryInfo.MaterielSpec,
+                            TaskType = cabinOrder.Order_type == "1" ? TaskTypeEnum.In.ObjToInt() : TaskTypeEnum.OutReturn.ObjToInt(),
+                            CreateDate = DateTime.Now,
+                            Creater = App.User.UserName,
+                            LocationCode = dt_InventoryInfo.LocationCode,
+                            OrderNo = cabinOrder.Order_no,
+                            StockQuantity = orderQty,
+                            SupplyQuantity = 0,
+                            Remark = "鍏ュ簱"
+                        };
+                        if (cabinOrder.Order_type == InOrderTypeEnum.Allocat.ObjToInt().ToString()) supplyTask_Hty.TaskType = TaskTypeEnum.AllocatIn.ObjToInt();
+                        supplyTask_Hties.Add(supplyTask_Hty);
+                        #endregion
+                    }
+                    if (cabinOrder.Details == null || !cabinOrder.Details.Where(x => x.OrderDetailStatus != "宸插畬鎴�").Any())
+                        cabinOrder.OdrderStatus = "宸插畬鎴�";
+                    _unitOfWorkManage.BeginTran();
+                    _supplyTaskHtyService.AddData(supplyTask_Hties);
+                    if (infosUp.Count > 0) _inventoryInfoService.UpdateData(infosUp);
+                    if (infosAdd.Count > 0) _inventoryInfoService.AddData(infosAdd);
+                    if (batchesUp.Count > 0) _inventory_BatchServices.UpdateData(batchesUp);
+                    if (batchesAdd.Count > 0) _inventory_BatchServices.AddData(batchesAdd);
+                    _cabinOrderDetailServices.UpdateData(cabinOrderDetailsUp);
+                    BaseDal.UpdateData(cabinOrder);
+                    _unitOfWorkManage.CommitTran();
+                }
+                content.OK();
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                content.Error(ex.Message);
+            }
+            return content;
+        }
+    }
+}

--
Gitblit v1.9.3