yanjinhui
2025-10-26 e1383d0a6ee0558c34f013f4e2561063bc2353b3
н¨Îļþ¼Ð/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/InventoryServices.cs
@@ -101,7 +101,7 @@
                {
                    return new ApiResponse<Dt_InventoryInfo> { code = "500", msg = "请求参数无效" };
                }
                var reslut = WarehouseEnum.立库.ObjToInt().ToString("000");
                // 2️⃣ å¼€å¯äº‹åŠ¡
                BaseDal.Db.Ado.BeginTran();
                foreach (var detail in request.details)
@@ -122,154 +122,260 @@
                        case "1": //入库
                            {
                                #region å¤„理入库订单
                                var reslut = WarehouseEnum.立库.ObjToInt().ToString("000");
                                Dt_CabinOrder cabinOrder = Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == request.orderNo && x.Warehouse_no ==reslut).Includes(x => x.Details).First();
                                //Dt_CabinOrder cabinOrder = Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == request.orderNo && x.Warehouse_no == "001").Includes(x => x.Details).First();
                                if (cabinOrder == null || cabinOrder.OdrderStatus == "已完成")
                                    throw new Exception($"入库单已完成");
                                Dt_CabinOrderDetail cabinOrderDetail = cabinOrder.Details.Where(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo && x.Status == 1).First();
                                if (cabinOrderDetail == null || cabinOrderDetail.OrderDetailStatus == "已完成")
                                    throw new Exception($"入库单明细已完成");
                                cabinOrderDetail.Order_Inqty += orderQty;
                                if (cabinOrderDetail.Order_Inqty > cabinOrderDetail.Order_qty)
                                    throw new Exception($"入库数量不可超出单据数量");
                                cabinOrder.OdrderStatus = "开始";
                                cabinOrderDetail.OrderDetailStatus = "开始";
                                if (cabinOrderDetail.Order_Inqty == cabinOrderDetail.Order_qty)
                                    cabinOrderDetail.OrderDetailStatus = "已完成";
                                BaseDal.Db.Updateable(cabinOrderDetail).ExecuteCommand();
                                var cabinOrder1 = BaseDal.Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == cabinOrder.Order_no && x.Warehouse_no == reslut).Includes(x => x.Details).First();
                                if (!cabinOrder1.Details.Where(x => x.OrderDetailStatus != "已完成").Any())
                                    cabinOrder.OdrderStatus = "已完成";
                                BaseDal.Db.Updateable(cabinOrder).ExecuteCommand();
                                #endregion
                                #region åˆ›å»ºåº“存信息
                                // 5️⃣ æŸ¥è¯¢åº“存详情
                                var inventoryInfo = BaseDal.Db.Queryable<Dt_InventoryInfo>()
                                    .First(x => x.MaterielCode == cabinOrderDetail.Goods_no && x.BatchNo == cabinOrderDetail.Batch_num && x.LocationCode == "立库");
                                if (inventoryInfo != null)
                                Dt_CabinOrder cabinOrder = Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == request.orderNo && x.Warehouse_no == reslut).Includes(x => x.Details).First();
                                #region åˆ¤æ–­æ˜¯å¦ä¸ºç›˜ç›ˆå…¥åº“单
                                if (cabinOrder.Order_type == "5")
                                {
                                    inventoryInfo.StockQuantity += orderQty;
                                    inventoryInfo.AvailableQuantity += orderQty;
                                    inventoryInfo.InDate = DateTime.Now;
                                    BaseDal.Db.Updateable(inventoryInfo).ExecuteCommand();
                                }
                                else
                                {
                                    inventoryInfo = new Dt_InventoryInfo
                                    foreach (var item in cabinOrder.Details)
                                    {
                                        PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "",
                                        WarehouseCode = "001",
                                        LocationCode = "立库",
                                        StockStatus = StockStatusEmun.入库完成.ObjToInt(),
                                        MaterielCode = goods.MaterielCode,
                                        MaterielName = goods.MaterielName,
                                        MaterielSpec = goods.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 = cabinOrderDetail.Exp_date,
                                        Remark = "WCS回传创建"
                                    };
                                    BaseDal.Db.Insertable(inventoryInfo).ExecuteCommand();
                                        Dt_InventoryInfo inventoryInfo = BaseDal.Db.Queryable<Dt_InventoryInfo>()
                                        .First(x => x.MaterielCode == item.Goods_no && x.BatchNo == item.Batch_num && x.LocationCode == reslut);
                                        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();
                                        #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 = "盘盈入库"
                                        };
                                        #endregion
                                        var inventory_Batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
                                        .First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
                                        inventory_Batch.StockQuantity = inventory_Batch.SupplyQuantity;
                                        inventory_Batch.SupplyQuantity = 0;
                                        item.OrderDetailStatus = "已完成";
                                        BaseDal.Db.Updateable(item).ExecuteCommand();
                                        BaseDal.Db.Updateable(inventoryInfo).ExecuteCommand();
                                        BaseDal.Db.Updateable(inventory_Batch).ExecuteCommand();
                                        BaseDal.Db.Insertable(supplyTask_Hty).ExecuteCommand();
                                    }
                                    var cabinOrder1 = BaseDal.Db.Queryable<Dt_CabinOrder>().Where(x => x.Id == cabinOrder.Id).Includes(x => x.Details).First();
                                    if (!cabinOrder1.Details.Where(x => x.OrderDetailStatus != "已完成").Any())
                                        cabinOrder.OdrderStatus = "已完成";
                                    BaseDal.Db.Updateable(cabinOrder).ExecuteCommand();
                                }
                                #endregion
                                #region åˆ›å»ºåº“存批次信息
                                // 6️⃣ æŸ¥è¯¢åº“存批次信息
                                var inventory_Batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
                                    .First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
                                if (inventory_Batch != null)
                                {
                                    inventory_Batch.StockQuantity += orderQty;
                                    inventory_Batch.AvailableQuantity += orderQty;
                                    BaseDal.Db.Updateable(inventory_Batch).ExecuteCommand();
                                }
                                else
                                {
                                    inventory_Batch = new Dt_Inventory_Batch
                                    if (cabinOrder == null || cabinOrder.OdrderStatus == "已完成")
                                        throw new Exception($"入库单已完成");
                                    Dt_CabinOrderDetail cabinOrderDetail = cabinOrder.Details.Where(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo && x.Status == 1).First();
                                    if (cabinOrderDetail == null || cabinOrderDetail.OrderDetailStatus == "已完成")
                                        throw new Exception($"入库单明细已完成");
                                    cabinOrderDetail.Order_Inqty += orderQty;
                                    if (cabinOrderDetail.Order_Inqty > cabinOrderDetail.Order_qty)
                                        throw new Exception($"入库数量不可超出单据数量");
                                    cabinOrder.OdrderStatus = "开始";
                                    cabinOrderDetail.OrderDetailStatus = "开始";
                                    if (cabinOrderDetail.Order_Inqty == cabinOrderDetail.Order_qty)
                                        cabinOrderDetail.OrderDetailStatus = "已完成";
                                    BaseDal.Db.Updateable(cabinOrderDetail).ExecuteCommand();
                                    var cabinOrder1 = BaseDal.Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == cabinOrder.Order_no && x.Warehouse_no == reslut).Includes(x => x.Details).First();
                                    if (!cabinOrder1.Details.Where(x => x.OrderDetailStatus != "已完成").Any())
                                        cabinOrder.OdrderStatus = "已完成";
                                    BaseDal.Db.Updateable(cabinOrder).ExecuteCommand();
                                    #endregion
                                    #region åˆ›å»ºåº“存信息
                                    // 5️⃣ æŸ¥è¯¢åº“存详情
                                    var inventoryInfo = BaseDal.Db.Queryable<Dt_InventoryInfo>()
                                        .First(x => x.MaterielCode == cabinOrderDetail.Goods_no && x.BatchNo == cabinOrderDetail.Batch_num && x.LocationCode == "立库");
                                    if (inventoryInfo != null)
                                    {
                                        MaterielCode = inventoryInfo.MaterielCode,
                                        MaterielName = inventoryInfo.MaterielName,
                                        MaterielSpec = inventoryInfo.MaterielSpec,
                                        inventoryInfo.StockQuantity += orderQty;
                                        inventoryInfo.AvailableQuantity += orderQty;
                                        inventoryInfo.InDate = DateTime.Now;
                                        BaseDal.Db.Updateable(inventoryInfo).ExecuteCommand();
                                    }
                                    else
                                    {
                                        inventoryInfo = new Dt_InventoryInfo
                                        {
                                            PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "",
                                            WarehouseCode = "001",
                                            LocationCode = "立库",
                                            StockStatus = StockStatusEmun.入库完成.ObjToInt(),
                                            MaterielCode = goods.MaterielCode,
                                            MaterielName = goods.MaterielName,
                                            MaterielSpec = goods.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 = cabinOrderDetail.Exp_date,
                                            Remark = "WCS回传创建"
                                        };
                                        BaseDal.Db.Insertable(inventoryInfo).ExecuteCommand();
                                    }
                                    #endregion
                                    #region åˆ›å»ºåº“存批次信息
                                    // 6️⃣ æŸ¥è¯¢åº“存批次信息
                                    var inventory_Batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
                                        .First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
                                    if (inventory_Batch != null)
                                    {
                                        inventory_Batch.StockQuantity += orderQty;
                                        inventory_Batch.AvailableQuantity += orderQty;
                                        BaseDal.Db.Updateable(inventory_Batch).ExecuteCommand();
                                    }
                                    else
                                    {
                                        inventory_Batch = new Dt_Inventory_Batch
                                        {
                                            MaterielCode = inventoryInfo.MaterielCode,
                                            MaterielName = inventoryInfo.MaterielName,
                                            MaterielSpec = inventoryInfo.MaterielSpec,
                                            BatchNo = inventoryInfo.BatchNo,
                                            StockQuantity = inventoryInfo.StockQuantity,
                                            OutboundQuantity = inventoryInfo.OutboundQuantity,
                                            AvailableQuantity = inventoryInfo.AvailableQuantity,
                                            SupplyQuantity = inventoryInfo.SupplyQuantity,
                                            ERPStockQuantity = 0,
                                            Status = false,
                                            ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
                                            ValidityPeriod = inventoryInfo.ValidityPeriod,
                                            Remark = "自动创建"
                                        };
                                        BaseDal.Db.Insertable(inventory_Batch).ExecuteCommand();
                                    }
                                    #endregion
                                    #region æ·»åŠ å…¥åº“ä»»åŠ¡åŽ†å²
                                    Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                                    {
                                        WarehouseCode = inventoryInfo.WarehouseCode,
                                        TaskNum = cabinOrderDetail.Id,
                                        OperateType = OperateTypeEnum.自动完成.ToString(),
                                        InsertTime = DateTime.Now,
                                        TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
                                        BatchNo = inventoryInfo.BatchNo,
                                        StockQuantity = inventoryInfo.StockQuantity,
                                        OutboundQuantity = inventoryInfo.OutboundQuantity,
                                        AvailableQuantity = inventoryInfo.AvailableQuantity,
                                        SupplyQuantity = inventoryInfo.SupplyQuantity,
                                        ERPStockQuantity = 0,
                                        Status = false,
                                        ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
                                        ValidityPeriod = inventoryInfo.ValidityPeriod,
                                        Remark = "自动创建"
                                        MaterielName = inventoryInfo.MaterielName,
                                        MaterielCode = inventoryInfo.MaterielCode,
                                        MaterielSpec = inventoryInfo.MaterielSpec,
                                        TaskType = cabinOrder.Order_type == "1" ? TaskTypeEnum.In.ObjToInt() : TaskTypeEnum.OutReturn.ObjToInt(),
                                        CreateDate = DateTime.Now,
                                        Creater = App.User.UserName,
                                        LocationCode = inventoryInfo.LocationCode,
                                        OrderNo = cabinOrder.Order_no,
                                        StockQuantity = orderQty,
                                        SupplyQuantity = 0,
                                        Remark = "入库"
                                    };
                                    BaseDal.Db.Insertable(inventory_Batch).ExecuteCommand();
                                    BaseDal.Db.Insertable(supplyTask_Hty).ExecuteCommand();
                                    #endregion
                                }
                                #endregion
                                #region æ·»åŠ å…¥åº“ä»»åŠ¡åŽ†å²
                                Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                                {
                                    WarehouseCode = inventoryInfo.WarehouseCode,
                                    TaskNum = cabinOrderDetail.Id,
                                    OperateType = OperateTypeEnum.自动完成.ToString(),
                                    InsertTime = DateTime.Now,
                                    TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
                                    BatchNo = inventoryInfo.BatchNo,
                                    MaterielName = inventoryInfo.MaterielName,
                                    MaterielCode = inventoryInfo.MaterielCode,
                                    MaterielSpec = inventoryInfo.MaterielSpec,
                                    TaskType = TaskTypeEnum.InPick.ObjToInt(),
                                    CreateDate = DateTime.Now,
                                    Creater = App.User.UserName,
                                    LocationCode = inventoryInfo.LocationCode,
                                    OrderNo = cabinOrder.Order_no,
                                    StockQuantity = orderQty,
                                    SupplyQuantity = 0,
                                    Remark = "入库"
                                };
                                BaseDal.Db.Insertable(supplyTask_Hty).ExecuteCommand();
                                #endregion
                                //UpdateInboundOrderDetailStatus(request.orderNo, detail.batchNo, detail.productCode);
                                break;
                            }
                        case "2":
                            #region æ‰¾å‡ºåº“任务
                            Dt_SupplyTask supplyTask = Db.Queryable<Dt_SupplyTask>().Where(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo && x.OrderNo == request.orderNo && x.WarehouseCode == WarehouseEnum.立库.ObjToInt().ToString("000") && x.TaskStatus == SupplyStatusEnum.NewOut.ObjToInt()).First();
                            if (supplyTask == null) throw new Exception($"未找到订单号【{request.orderNo}】物料编号【{detail.productCode}】的出库任务");
                            #endregion
                            #region å¤„理出库单、库存信息、出库任务、调拨任务
                            _deliveryOrderServices.OutTaskFinish(supplyTask);
                            #endregion
                            break;
                        case "2":
                            {
                                Dt_DeliveryOrder cabinOrder = Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == request.orderNo && x.Warehouse_no == reslut).Includes(x => x.Details).First();
                                if (cabinOrder.Out_type == "6")
                                {
                                    foreach (var item in cabinOrder.Details)
                                    {
                                        Dt_InventoryInfo inventoryInfo = BaseDal.Db.Queryable<Dt_InventoryInfo>()
                                        .First(x => x.MaterielCode == item.Goods_no && x.BatchNo == item.Batch_num && x.LocationCode == reslut);
                                        var qty = inventoryInfo.StockQuantity - inventoryInfo.SupplyQuantity;
                                        if (orderQty != qty)
                                            throw new Exception($"盘亏出库数量不一致,上传数量【{orderQty}】,库存盘亏数量【{qty}】");
                                        inventoryInfo.StockQuantity = inventoryInfo.SupplyQuantity;
                                        inventoryInfo.SupplyQuantity = 0;
                                        inventoryInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                                        #region æ·»åŠ ç›˜äºå‡ºåº“ä»»åŠ¡
                                        Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                                        {
                                            WarehouseCode = inventoryInfo.WarehouseCode,
                                            OperateType = OperateTypeEnum.自动完成.ToString(),
                                            InsertTime = DateTime.Now,
                                            TaskStatus = SupplyStatusEnum.OutFinish.ObjToInt(),
                                            BatchNo = inventoryInfo.BatchNo,
                                            MaterielName = inventoryInfo.MaterielName,
                                            MaterielCode = inventoryInfo.MaterielCode,
                                            MaterielSpec = inventoryInfo.MaterielSpec,
                                            TaskType = TaskTypeEnum.ChenckOut.ObjToInt(),
                                            CreateDate = DateTime.Now,
                                            Creater = App.User.UserName,
                                            LocationCode = inventoryInfo.LocationCode,
                                            OrderNo = cabinOrder.Out_no,
                                            StockQuantity = orderQty,
                                            SupplyQuantity = 0,
                                            Remark = "盘亏出库"
                                        };
                                        #endregion
                                        var inventory_Batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
                                        .First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
                                        inventory_Batch.StockQuantity = inventory_Batch.SupplyQuantity;
                                        inventory_Batch.SupplyQuantity = 0;
                                        item.OotDetailStatus = "已完成";
                                        BaseDal.Db.Updateable(item).ExecuteCommand();
                                        BaseDal.Db.Updateable(inventoryInfo).ExecuteCommand();
                                        BaseDal.Db.Updateable(inventory_Batch).ExecuteCommand();
                                        BaseDal.Db.Insertable(supplyTask_Hty).ExecuteCommand();
                                    }
                                    var cabinOrder1 = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Id == cabinOrder.Id).Includes(x => x.Details).First();
                                    if (!cabinOrder1.Details.Where(x => x.OotDetailStatus != "已完成").Any())
                                        cabinOrder.OutStatus = "已完成";
                                    BaseDal.Db.Updateable(cabinOrder).ExecuteCommand();
                                }
                                else
                                {
                                    #region æ‰¾å‡ºåº“任务
                                    Dt_SupplyTask supplyTask = Db.Queryable<Dt_SupplyTask>().Where(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo && x.OrderNo == request.orderNo && x.WarehouseCode == reslut && x.TaskStatus == SupplyStatusEnum.NewOut.ObjToInt()).First();
                                    if (supplyTask == null) throw new Exception($"未找到订单号【{request.orderNo}】物料编号【{detail.productCode}】的出库任务");
                                    if (supplyTask.StockQuantity != orderQty) throw new Exception($"订单号【{request.orderNo}】物料编号【{detail.productCode}】的出库数量与出库任务数量不相等");
                                    #endregion
                                    #region å¤„理出库单、库存信息、出库任务、调拨任务
                                    _deliveryOrderServices.OutTaskFinish(supplyTask);
                                    #endregion
                                }
                            }
                            break;
                        case "3": // ç›˜ç‚¹
                            decimal diff = detail.ea ?? 0;             // å·®å¼‚æ•°
                            int flag = detail.isLossOrProfit ?? 3;     // 1=盘亏, 2=盘盈, 3=盘中
                            if (flag == 1) // ç›˜äº
                            {
                                //库存数-差异数=盘亏
                                //batch.SupplyQuantity += (batch.StockQuantity - diff);
                                //batch.Remark = "盘点单回传 - ç›˜äº";
                            }
                            else if (flag == 2) // ç›˜ç›ˆ
                            {
                                //库存数+差异数=盘盈
                                //batch.SupplyQuantity += (batch.StockQuantity + diff);
                                //batch.Remark = "盘点单回传 - ç›˜ç›ˆ";
                            }
                            else // ç›˜ä¸­
                            {
                                //batch.Remark = "盘点单回传 - ç›˜ä¸­";
                                var WarehouseLK = WarehouseEnum.立库.ObjToInt().ToString("000");
                                #region æ‰¾ç›˜ç‚¹ä»»åŠ¡
                                Dt_SupplyTask supplyTask = Db.Queryable<Dt_SupplyTask>().Where(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo && x.OrderNo == request.orderNo && x.WarehouseCode == WarehouseLK && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt()).First();
                                if (supplyTask == null) throw new Exception($"未找到订单号【{request.orderNo}】物料编号【{detail.productCode}】的盘点任务");
                                #endregion
                                if (detail.isLossOrProfit == 1) // ç›˜äº
                                {
                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity - detail.ea;
                                }
                                else if (detail.isLossOrProfit == 2) // ç›˜ç›ˆ
                                {
                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity + detail.ea;
                                }
                                else // ç›˜ä¸­
                                {
                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity;
                                }
                                _deliveryOrderServices.CheckTaskFinish(supplyTask);
                            }
                            break;
                    }