wangxinhui
2025-06-05 17bca2d690f283ec110c0c327a8508ed8fba9d21
´úÂë¹ÜÀí/WMS/WIDESEA_WMSServer/WIDESEA_InboundService/InboundOrderService.cs
@@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using SqlSugar;
using System;
using System.Collections;
@@ -900,6 +901,220 @@
            }
            return content;
        }
        public WebResponseContent MaterielPPorGM(string materielBoxCode = "")
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                string SerNum = materielBoxCode.Substring(0, materielBoxCode.LastIndexOf("SC:")-1);
                MatSerNumAnalysisModel model = CodeAnalysisHelper.CodeAnalysis<MatSerNumAnalysisModel>(AnalysisCodeEnum.MatSerNumAnalysis, SerNum);
                //验证判断时间格式
                WebResponseContent IsValidContent = IsValidMCDates(new List<MatSerNumAnalysisModel>() { model });
                if (!IsValidContent.Status)
                {
                    return content.Error(IsValidContent.Message);
                }
                string result = materielBoxCode.Substring(materielBoxCode.LastIndexOf("SC:") + 3);
                //获取入库单明细
                Dt_InboundOrderDetail inboundOrderDetail = _inboundRepository.InboundOrderDetailRepository.QueryFirst(x=>x.BatchNo== model.LotNo && x.MaterielCode== model.MaterielCode);
                if (inboundOrderDetail == null)
                {
                    return InOtherGMOrPP(model, result);
                }
                Dt_InboundOrder inboundOrder = BaseDal.Db.Queryable<Dt_InboundOrder>().First(x => x.Id == inboundOrderDetail.OrderId);
                if (inboundOrder == null)
                {
                    return WebResponseContent.Instance.Error($"未找到入库单信息");
                }
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == inboundOrder.WarehouseId);
                //除采购入库单以外其他入库单组盘数据处理
                if (inboundOrder.OrderType != InOrderTypeEnum.Purchase.ObjToInt())
                {
                    return OtherInGroup(inboundOrder, model.LotNo, 0, warehouse, new List<MatSerNumAnalysisModel>() { model });
                }
                Dt_ReceiveOrder receiveOrder = _inboundRepository.ReceiveOrderRepository.Db.Queryable<Dt_ReceiveOrder>().Where(x => x.ReceiveOrderNo == inboundOrder.UpperOrderNo).Includes(x => x.Details).First();
                if (receiveOrder == null)
                {
                    return WebResponseContent.Instance.Error($"未找到对应的收货单");
                }
                if (receiveOrder.Details == null || receiveOrder.Details.Count <= 0)
                {
                    return WebResponseContent.Instance.Error($"未找到对应的收货单明细");
                }
                List<string> purchaseOrderNos = receiveOrder.Details.Select(x => x.PurchaseOrderNo).ToList();
                string materielCode = model?.MaterielCode ?? "";
                Dt_MaterielInfo materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterielCode == materielCode);
                if (materielInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该物料的信息");
                }
                float beforeQuantity = 0;
                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletCode == model.LotNo).Includes(x => x.Details).First();
                if (stockInfo == null)
                {
                    stockInfo = new Dt_StockInfo()
                    {
                        PalletCode = model.LotNo,
                        StockStatus = StockStatusEmun.组盘暂存.ObjToInt(),
                        WarehouseId = inboundOrder.WarehouseId,
                        PalletType = GetPalletTypeGMOrPP(warehouse, result.Split("*")[0]),
                        StockLength = result.Split("*")[0].ObjToInt(),
                        Details = new List<Dt_StockInfoDetail>()
                    };
                }
                else
                {
                    if (stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt())
                    {
                        return WebResponseContent.Instance.Error($"托盘号重复");
                    }
                }
                if (stockInfo.PalletType==-1)
                {
                    return WebResponseContent.Instance.Error($"托盘类型不存在");
                }
                List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>();
                if (purchaseOrderNos.FirstOrDefault(x => x == model.PurchaseOrderNo) == null)
                {
                    return WebResponseContent.Instance.Error($"未在收货明细中找到该采购单");
                }
                if (inboundOrderDetail.OrderDetailStatus > OrderDetailStatusEnum.Inbounding.ObjToInt() || inboundOrderDetail.OrderQuantity == inboundOrderDetail.ReceiptQuantity)
                {
                    return WebResponseContent.Instance.Error($"该物料在该入库单中已全部组盘完成");
                }
                Dt_StockInfoDetail stockInfoDetail = new Dt_StockInfoDetail()
                {
                    BatchNo = model.LotNo,
                    MaterielCode = materielInfo.MaterielCode,
                    MaterielName = materielInfo.MaterielName,
                    MaterielSpec = materielInfo.MaterielSpec,
                    OrderNo = inboundOrder.InboundOrderNo,
                    SerialNumber = model.SerialNumber,
                    StockQuantity = model.Quantity,
                    OutboundQuantity = 0,
                    Unit = materielInfo.MaterielUnit,
                    Status = StockStatusEmun.组盘暂存.ObjToInt(),
                    ProductionDate = model.ProductionDate,
                    EffectiveDate = model.EffectiveDate,
                    InboundOrderRowNo = inboundOrderDetail.RowNo,
                };
                stockInfo.Details.Add(stockInfoDetail);
                stockInfoDetails.Add(stockInfoDetail);
                inboundOrderDetail.ReceiptQuantity += model.Quantity;
                if (inboundOrderDetail.ReceiptQuantity > inboundOrderDetail.OrderQuantity)
                {
                    return WebResponseContent.Instance.Error($"组盘数量溢出{inboundOrderDetail.ReceiptQuantity - inboundOrderDetail.OrderQuantity}");
                }
                if (inboundOrderDetail.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt())
                {
                    inboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.GroupAndInbound.ObjToInt();
                }
                float totalQuantity = stockInfo.Details.Sum(x => x.StockQuantity);
                if (inboundOrder.OrderStatus == InOrderStatusEnum.未开始.ObjToInt())
                {
                    inboundOrder.OrderStatus = InOrderStatusEnum.入库中.ObjToInt();
                }
                _inboundRepository.InboundOrderDetailRepository.UpdateData(inboundOrderDetail);
                _inboundRepository.InboundOrderRepository.UpdateData(inboundOrder);
                _recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfo, stockInfoDetails, beforeQuantity, totalQuantity, StockChangeTypeEnum.MaterielGroup);
                content.OK(data: stockInfo);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        public WebResponseContent InOtherGMOrPP(MatSerNumAnalysisModel model,string request = "")
        {
            string materielCode = model?.MaterielCode ?? "";
            Dt_MaterielInfo materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterielCode == materielCode);
            if (materielInfo == null)
            {
                return WebResponseContent.Instance.Error($"未找到该物料的信息");
            }
            float beforeQuantity = 0;
            Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == materielInfo.WarehouseId);
            Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletCode == model.LotNo).Includes(x => x.Details).First();
            if (stockInfo == null)
            {
                stockInfo = new Dt_StockInfo()
                {
                    PalletCode = model.LotNo,
                    StockStatus = StockStatusEmun.组盘暂存.ObjToInt(),
                    WarehouseId = warehouse.WarehouseId,
                    PalletType = GetPalletTypeGMOrPP(warehouse, request.Split("*")[0]),
                    StockLength = request.Split("*")[0].ObjToInt(),
                    Details = new List<Dt_StockInfoDetail>()
                };
            }
            else
            {
                if (stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt())
                {
                    return WebResponseContent.Instance.Error($"托盘号重复");
                }
            }
            if (stockInfo.PalletType == -1)
            {
                return WebResponseContent.Instance.Error($"托盘类型不存在");
            }
            List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>();
            Dt_StockInfoDetail stockInfoDetail = new Dt_StockInfoDetail()
            {
                BatchNo = model.LotNo,
                MaterielCode = materielInfo.MaterielCode,
                MaterielName = materielInfo.MaterielName,
                MaterielSpec = materielInfo.MaterielSpec,
                OrderNo = "",
                SerialNumber = model.SerialNumber,
                StockQuantity = model.Quantity,
                OutboundQuantity = 0,
                Unit = materielInfo.MaterielUnit,
                Status = StockStatusEmun.组盘暂存.ObjToInt(),
                ProductionDate = model.ProductionDate,
                EffectiveDate = model.EffectiveDate,
                InboundOrderRowNo = 0,
            };
            stockInfo.Details.Add(stockInfoDetail);
            stockInfoDetails.Add(stockInfoDetail);
            float totalQuantity = stockInfo.Details.Sum(x => x.StockQuantity);
            _recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfo, stockInfoDetails, beforeQuantity, totalQuantity, StockChangeTypeEnum.MaterielGroup);
            return WebResponseContent.Instance.OK(data: stockInfo);
        }
        public int GetPalletTypeGMOrPP(Dt_Warehouse warehouse, string boxWidth)
        {
            if (warehouse.WarehouseCode == WarehouseEnum.HA152.ToString())
            {
                if (boxWidth.ObjToInt() <= 690 && boxWidth.ObjToInt()>=515)
                {
                    return 15;
                }
                else if (boxWidth.ObjToInt() > 690 && boxWidth.ObjToInt() <= 1400)
                {
                    return 16;
                }
            }
            else if (warehouse.WarehouseCode == WarehouseEnum.HA58.ToString())
            {
                Dt_PalletTypeInfo palletTypeInfo = _palletTypeInfoRepository.QueryFirst(x => x.WarehouseId == warehouse.WarehouseId);
                return palletTypeInfo.PalletType;
            }
            return -1;
        }
        public WebResponseContent OtherInGroup(Dt_InboundOrder inboundOrder,string? palletCode,int Initiallife,Dt_Warehouse warehouse,List<MatSerNumAnalysisModel> models)
        {
            WebResponseContent content = new WebResponseContent();