dengjunjie
3 天以前 d2a1ccd801f942381723c5a6001989d8da8ba6f5
н¨Îļþ¼Ð/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;
@@ -29,26 +30,37 @@
            try
            {
                List<Dt_DeliveryOrder> deliveryOrders = new List<Dt_DeliveryOrder>();
                List<Dt_SupplyTask> supplyTasks = new List<Dt_SupplyTask>();
                //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));
                foreach (var item in inventory_Batches)
                {
                    supplyTasks = _supplyTaskService.Repository.QueryData(x => x.MaterielCode == item.MaterielCode && x.BatchNo == item.BatchNo && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt()).ToList();
                    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 (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}】已存在出库任务,请先完成当前出库任务");
                    List<Dt_InventoryInfo> inventoryInfos = _inventoryInfoService.Repository.QueryData(x => item.MaterielCode == x.MaterielCode && x.BatchNo == item.BatchNo);
                    if (inventoryInfos.Count > 0)
                    {
                        foreach (var items in inventoryInfos.GroupBy(x => x.WarehouseCode))
                        {
                            Dt_DeliveryOrder deliveryOrder = new Dt_DeliveryOrder()
                            Dt_DeliveryOrder? deliveryOrder = deliveryOrders.Where(x => x.Warehouse_no == items.Key).FirstOrDefault();
                            bool isNull = false;
                            if (deliveryOrder == null)
                            {
                                Out_no = item.MaterielCode + "_" + item.BatchNo,
                                Out_type = "3",
                                OutStatus = "新建",
                                Warehouse_no = items.Key,
                                Details = new List<Dt_DeliveryOrderDetail>()
                            };
                                isNull = true;
                                deliveryOrder = new Dt_DeliveryOrder()
                                {
                                    //Out_no = item.MaterielCode + "_" + item.BatchNo,
                                    Out_no = $"PD{items.Key}{DateTime.Now.ToString("yyMMddHHmmss")}",
                                    Out_type = "20",
                                    OutStatus = "新建",
                                    Warehouse_no = items.Key,
                                    Details = new List<Dt_DeliveryOrderDetail>()
                                };
                            }
                            var Status = items.Key == "001" ? 0 : 2;//判断是否为立库区域
                            Dt_DeliveryOrderDetail deliveryOrderDetail = new Dt_DeliveryOrderDetail()
                            {
@@ -63,10 +75,8 @@
                                Reservoirarea = items.Key
                            };
                            deliveryOrder.Details.Add(deliveryOrderDetail);
                            deliveryOrders.Add(deliveryOrder);
                            if (isNull) deliveryOrders.Add(deliveryOrder);
                            #region åˆ›å»ºç›˜ç‚¹ä»»åŠ¡
                            //if (items.Key != "001")// éžç«‹åº“区域需要创建盘点任务
                            //{
                            foreach (var inventory in items)
                            {
                                Dt_SupplyTask supplyTask = new Dt_SupplyTask()
@@ -86,7 +96,7 @@
                                    SupplyQuantity = 0,
                                    Remark = "盘点"
                                };
                                supplyTasks.Add(supplyTask);
                                supplyTasks1.Add(supplyTask);
                                _InventoryInfos.Add(inventory);
                            }
                            //}
@@ -100,7 +110,7 @@
                });
                _unitOfWorkManage.BeginTran();
                _inventoryInfoService.UpdateData(_InventoryInfos);
                _supplyTaskService.AddData(supplyTasks);
                _supplyTaskService.AddData(supplyTasks1);
                BaseDal.Db.InsertNav(deliveryOrders)
                        .Include(x => x.Details)
                        .ExecuteCommand();
@@ -133,11 +143,11 @@
                List<Dt_DeliveryOrder> dt_ReceiveOrders = new List<Dt_DeliveryOrder>();
                if (string.IsNullOrEmpty(orderNo))
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.OutStatus == "新建" || x.OutStatus == "开始") && x.Warehouse_no == warehouseCode && x.Out_type == "3").Includes(x => x.Details).OrderByDescending(x => x.CreateDate).ToPageList(pageNo, 5);
                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.OutStatus == "新建" || x.OutStatus == "开始") && x.Warehouse_no == warehouseCode && x.Out_type == "20").Includes(x => x.Details).OrderByDescending(x => x.CreateDate).ToPageList(pageNo, 5);
                }
                else
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.Out_no.Contains(orderNo) || x.Client_no.Contains(orderNo)) && (x.OutStatus == "新建" || x.OutStatus == "开始") && x.Out_type == "3" && x.Warehouse_no == warehouseCode).OrderByDescending(x => x.CreateDate).Includes(x => x.Details).ToPageList(pageNo, 5);
                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.Out_no.Contains(orderNo) || x.Client_no.Contains(orderNo)) && (x.OutStatus == "新建" || x.OutStatus == "开始") && x.Out_type == "20" && x.Warehouse_no == warehouseCode).OrderByDescending(x => x.CreateDate).Includes(x => x.Details).ToPageList(pageNo, 5);
                }
                content.OK(data: dt_ReceiveOrders);
@@ -218,48 +228,118 @@
            try
            {
                supplyTask.TaskStatus = SupplyStatusEnum.CheckFinish.ObjToInt();
                Dt_DeliveryOrder cabinOrder = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == supplyTask.OrderNo && x.Warehouse_no == supplyTask.WarehouseCode && x.Out_type == "3").Includes(x => x.Details).First();
                Dt_DeliveryOrder cabinOrder = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == supplyTask.OrderNo && x.Warehouse_no == supplyTask.WarehouseCode && x.Out_type == "20").Includes(x => x.Details).First();
                if (cabinOrder == null) return WebResponseContent.Instance.Error($"盘点单已完成");
                Dt_DeliveryOrderDetail cabinOrderDetail = cabinOrder.Details.Where(x => x.Batch_num == supplyTask.BatchNo && x.Reservoirarea == supplyTask.WarehouseCode && x.Goods_no == supplyTask.MaterielCode).First();
                if (cabinOrderDetail == null) return WebResponseContent.Instance.Error($"盘点单明细已完成");
                Dt_MaterielInfo materielInfo = _basicService.MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == cabinOrderDetail.Goods_no);
                Dt_MaterielInfo materielInfo = _materielInfoService.Repository.QueryFirst(x => x.MaterielCode == cabinOrderDetail.Goods_no);
                if (materielInfo == null) return WebResponseContent.Instance.Error($"请维护物料编号【{cabinOrderDetail.Goods_no}】的物料信息");
                cabinOrderDetail.Order_Outqty += supplyTask.SupplyQuantity;
                //if (cabinOrderDetail.Order_Outqty > cabinOrderDetail.Order_qty)
                //    return WebResponseContent.Instance.Error($"实盘数量不可超出账面数量");
                #region å¤„理出库单,货位,库存,库存批次信息
                _unitOfWorkManage.BeginTran();
                #region å¹³åº“盘点只能出现整箱差异
                if (supplyTask.WarehouseCode == WarehouseEnum.大件库.ObjToInt().ToString("000") && materielInfo.MaterielSourceType != MaterielSourceTypeEnum.PurchasePart)
                {
                    var Qty = supplyTask.SupplyQuantity % materielInfo.BoxQty;
                    if (Qty != 0) return WebResponseContent.Instance.Error($"大件库库存为整箱!盘点数量不能存在散件");
                }
                #endregion
                #region ä¿®æ”¹ç›˜ç‚¹å•
                cabinOrder.OutStatus = "开始";
                cabinOrderDetail.OotDetailStatus = "开始";
                Repository.UpdateData(cabinOrder);
                _deliveryOrderDetailServices.Repository.UpdateData(cabinOrderDetail);
                #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));
                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());
                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;
                _inventoryInfoService.UpdateData(inventoryInfo);
                if (inventoryInfo == null) return WebResponseContent.Instance.Error($"未找到立库的库存信息");
                inventoryInfo.SupplyQuantity = supplyTask.SupplyQuantity - inventoryInfo.StockQuantity;
                #endregion
                #region ä»»åŠ¡è®°å½•
                //_supplyTaskService.UpdateData(supplyTask);
                _supplyTaskService.Repository.DeleteAndMoveIntoHty(supplyTask, OperateTypeEnum.自动完成);
                #endregion
                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 å¤„理当前物料批次的库存和批次信息
                    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.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;
                        //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 åˆ¤æ–­å½“前物料批次的盘点任务是否全部完成
                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).ToList();
                    #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)
                {
                    #region å®Œæˆç›˜ç‚¹å•
@@ -268,42 +348,25 @@
                    {
                        x.OotDetailStatus = "已完成";
                    });
                    Repository.DeleteAndMoveIntoHty(cabinOrder, OperateTypeEnum.自动完成);
                    _deliveryOrderDetailServices.Repository.DeleteAndMoveIntoHty(cabinOrder.Details, OperateTypeEnum.自动完成);
                    cabinOrder.Details = null;
                    BaseDal.DeleteAndMoveIntoHty(cabinOrder, OperateTypeEnum.自动完成);
                    #endregion
                }
                supplyTasks = _supplyTaskService.Repository.QueryData(x => x.MaterielCode == supplyTask.MaterielCode && x.BatchNo == supplyTask.BatchNo && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt()).ToList();
                if (supplyTasks.Count < 1)
                else
                {
                    #region æŸ¥æ‰¾åº“å­˜
                    var inventoryInfos = _inventoryInfoService.Repository.QueryData(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no);
                    var SupplyQuantitys = inventoryInfos.Select(x => x.SupplyQuantity).ToList().Sum();
                    #endregion
                    #region åº“存批次
                    Dt_Inventory_Batch inventory_Batch = _inventory_BatchServices.Repository.QueryFirst(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no);
                    if (SupplyQuantitys == inventory_Batch.StockQuantity)
                    {
                        foreach (var item in inventoryInfos)
                        {
                            item.StockQuantity = item.SupplyQuantity;
                            item.SupplyQuantity = 0;
                            item.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                        }
                        _inventoryInfoService.UpdateData(inventoryInfos);
                    }
                    else
                    {
                        inventory_Batch.SupplyQuantity = SupplyQuantitys;
                        _inventory_BatchServices.UpdateData(inventory_Batch);
                    }
                #endregion
                    BaseDal.UpdateData(cabinOrder);
                    _deliveryOrderDetailServices.Repository.UpdateData(cabinOrderDetail);
                }
                #endregion
                //_inventoryInfoService.UpdateData(inventoryInfo);
                _supplyTaskService.Repository.DeleteAndMoveIntoHty(supplyTask, OperateTypeEnum.自动完成);
                _inventoryInfoService.UpdateData(inventoryInfos);
                if (inventoryInfosDel.Count > 0) _inventoryInfoService.DeleteData(inventoryInfosDel);
                _inventory_BatchServices.UpdateData(inventory_Batchs);
                _unitOfWorkManage.CommitTran();
                #endregion
                content.OK();
                #endregion
            }
            catch (Exception ex)
            {