dengjunjie
2025-10-24 d55ef610a530c2ab0a30d6ecdc1e7e4ab54cc896
н¨Îļþ¼Ð/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/DeliveryOrderServices.cs
@@ -34,7 +34,7 @@
{
    public partial class DeliveryOrderServices : ServiceBase<Dt_DeliveryOrder, IRepository<Dt_DeliveryOrder>>, IDeliveryOrderServices
    {
        static string SearchDate = "";
        private readonly IBasicService _basicService;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IInventory_BatchServices _inventory_BatchServices;
@@ -55,6 +55,9 @@
            _cabinOrderServices = cabinOrderServices;
            _tacticsService = tacticsService;
        }
        static string SearchDate = "2025-09-10 00:00:00";
        public WebResponseContent GetUpstreamOutOrder()
        {
            var responseContent = new WebResponseContent();
@@ -110,210 +113,60 @@
                    foreach (var outorder in newOutOrders)
                    {
                        bool skipOrder = false; // æ ‡è®°æ˜¯å¦è·³è¿‡è¯¥å‡ºåº“单
                        // å¦‚果这个出库单类型不等于1,属于出库退货(要入货上游传过来的数量为负数)
                        if (outorder.order_type != "1")
                        if (outorder.order_type == "1") // æ­£å¸¸å‡ºåº“单
                        {
                            CreateOutboundOrder(outorder);//创建出库单、处理库存、添加出库任务
                        }
                        else if (outorder.order_type == "2")//出库退货
                        {
                            // åˆ›å»ºå‡ºåº“退货单 - è¿™é‡Œéœ€è¦æ ¹æ®ä¸šåŠ¡é€»è¾‘å®žçŽ°
                            // æš‚时跳过或实现退货逻辑
                            continue;
                            //continue;
                            #region è½¬æ¢æˆå…¥åº“单
                            UpstreamOrderInfo order = new UpstreamOrderInfo()
                            {
                                order_no = outorder.order_no,
                                order_type = outorder.order_type,
                                warehouse_no = outorder.warehouse_no,
                                details = new List<UpstreamOrderDetail>()
                            };
                            foreach (var item in outorder.details)
                            {
                                UpstreamOrderDetail detail = new UpstreamOrderDetail()
                                {
                                    batch_num = item.batch_num,
                                    goods_no = item.goods_no,
                                    order_qty = item.order_qty,
                                    exp_date = item.exp_date,
                                };
                                order.details.Add(detail);
                            }
                            responseContent = _cabinOrderServices.CreateInboundOrder(order);
                            List<Dt_CabinOrder>? dt_CabinOrders = responseContent.Data as List<Dt_CabinOrder>;
                            if (dt_CabinOrders != null && dt_CabinOrders.Count > 0)
                                BaseDal.Db.InsertNav(dt_CabinOrders).Include(x => x.Details).ExecuteCommand();
                            #endregion
                        }
                        else if (outorder.order_type == "1") // æ­£å¸¸å‡ºåº“单
                        else if (outorder.order_type == "3")//报损出库
                        {
                            CreateOutboundOrder(outorder);//创建出库单、处理库存、添加出库任务
                            // æŽ’除特殊药品,特殊药品直接插入
                            if (outorder.warehouse_no != "001" && (outorder.warehouse_no == "010" || outorder.warehouse_no == "003" || outorder.warehouse_no == "017"))
                            {
                                var entityOrder = new Dt_DeliveryOrder
                                {
                                    Out_no = outorder.order_no,
                                    Out_type = outorder.order_type,
                                    Client_no = outorder.client_no,
                                    Client_name = outorder.client_name,
                                    Account_time = outorder.account_time,
                                    Warehouse_no = outorder.warehouse_no,
                                    OutStatus = "新建",
                                    Details = outorder.details.Select(d => new Dt_DeliveryOrderDetail
                                    {
                                        Goods_no = d.goods_no,
                                        Order_qty = Math.Abs(d.order_qty), // å‡ºåº“数量转为正数
                                        Batch_num = d.batch_num,
                                        Exp_date = d.exp_date,
                                        OotDetailStatus = "新建",
                                        Status = 2, // pad平库,无需同步
                                    }).ToList()
                                };
                                _DeliveryOrders.Add(entityOrder);
                            }
                            else  // å¸¸è§„药品001房给立库,要是有整箱就给平库
                            {
                                var entityOrder = new Dt_DeliveryOrder
                                {
                                    Out_no = outorder.order_no,
                                    Out_type = outorder.order_type,
                                    Client_no = outorder.client_no,
                                    Client_name = outorder.client_name,
                                    Account_time = outorder.account_time,
                                    Warehouse_no = outorder.warehouse_no,
                                    OutStatus = "新建",
                                    Details = new List<Dt_DeliveryOrderDetail>()
                                };
                                // éåŽ†å‡ºåº“æ˜Žç»†
                                foreach (var item in outorder.details)
                                {
                                    // å°†ä¸Šæ¸¸å‡ºåº“数量转为正数
                                    decimal orderQty = Math.Abs((decimal)item.order_qty);
                                    #region æ ¹æ®ç‰©æ–™ç¼–码查询物料信息
                                    var medication = BaseDal.Db.Queryable<Dt_MaterielInfo>()
                                        .Where(m => m.MaterielCode == item.goods_no)
                                        .First();
                                    // å¦‚果物料信息不存在,跳过整个出库单
                                    if (medication == null)
                                    {
                                        skipOrder = true;
                                        Console.WriteLine($"跳出处库单 {outorder.order_no},原因:未找到商品 [{item.goods_no}] çš„物料信息。");
                                        break;
                                    }
                                    // è‹¥ç‰©æ–™ä¸å­˜åœ¨æˆ– MaterielSourceType ä¸ºç©ºï¼Œåˆ™è·³è¿‡æ•´ä¸ªå‡ºåº“单
                                    if (medication == null || !Enum.IsDefined(typeof(MaterielSourceTypeEnum), medication.MaterielSourceType))
                                    {
                                        skipOrder = true;
                                        break;
                                    }
                                    #endregion
                                    /// æ ¹æ®ç‰©æ–™ä¿¡æ¯å¤§å°ä»¶å±žæ€§åŒºåˆ†æ˜¯å¦ä»Žç«‹åº“出库 // å¤§ä»¶ä»Žå¹³åº“出库
                                    if (medication.MaterielSourceType == MaterielSourceTypeEnum.PurchasePart) // å¦‚果物料是大件
                                    {
                                        var orderDetail = new Dt_DeliveryOrderDetail()
                                        {
                                            Reservoirarea = outorder.warehouse_no,
                                            Goods_no = item.goods_no,
                                            Order_qty = orderQty,
                                            Batch_num = item.batch_num,
                                            Exp_date = item.exp_date,
                                            OotDetailStatus = "新建",
                                            Status = 2 // å¹³åº“,无需同步
                                        };
                                        entityOrder.Details.Add(orderDetail);
                                    }
                                    else
                                    {
                                        Dt_DeliveryOrderDetail orderDetail = null;
                                        // è®¡ç®—整箱和散件数量
                                        var ys = orderQty % medication.BoxQty; // ä¸èƒ½æ•´é™¤ç®±è§„的散件数
                                        var xs = (int)(orderQty / medication.BoxQty); // æ•´ç®±æ•°é‡ï¼ˆä¿ç•™æ•´æ•°ï¼‰
                                        #region ä»Žç«‹åº“出库散件
                                        #region åˆ¤æ–­æ˜¯å¦æœ‰æ•£ä»¶
                                        if (ys > 0 && medication.Business_qty >= ys)
                                        {
                                            orderDetail = new Dt_DeliveryOrderDetail()
                                            {
                                                Reservoirarea = outorder.warehouse_no,
                                                Goods_no = item.goods_no,
                                                Order_qty = ys,
                                                Batch_num = item.batch_num,
                                                Exp_date = item.exp_date,
                                                OotDetailStatus = "新建",
                                                Status = 0 // ç«‹åº“,需要同步给WCS
                                            };
                                            medication.Business_qty -= ys;
                                        }
                                        #endregion
                                        #region åˆ¤æ–­ç«‹åº“库存是否足够出整箱
                                        while (medication.Business_qty >= medication.BoxQty && xs > 0)
                                        {
                                            xs--;
                                            if (orderDetail == null)
                                            {
                                                orderDetail = new Dt_DeliveryOrderDetail()
                                                {
                                                    Reservoirarea = outorder.warehouse_no,
                                                    Goods_no = item.goods_no,
                                                    Order_qty = medication.BoxQty,
                                                    Batch_num = item.batch_num,
                                                    Exp_date = item.exp_date,
                                                    OotDetailStatus = "新建",
                                                    Status = 0 // ç«‹åº“
                                                };
                                                medication.Business_qty -= medication.BoxQty;
                                            }
                                            else
                                            {
                                                orderDetail.Order_qty += medication.BoxQty;
                                                medication.Business_qty -= medication.BoxQty;
                                            }
                                        }
                                        if (orderDetail != null)
                                            entityOrder.Details.Add(orderDetail);
                                        #endregion
                                        #endregion
                                        #region å‰©ä½™æ•´ä»¶ä»Žå¹³åº“出库
                                        if (xs > 0)
                                        {
                                            orderDetail = new Dt_DeliveryOrderDetail()
                                            {
                                                Reservoirarea = outorder.warehouse_no,
                                                Goods_no = item.goods_no,
                                                Order_qty = medication.BoxQty * xs,
                                                Batch_num = item.batch_num,
                                                Exp_date = item.exp_date,
                                                OotDetailStatus = "新建",
                                                Status = 2 // å¹³åº“
                                            };
                                            entityOrder.Details.Add(orderDetail);
                                        }
                                        #endregion
                                    }
                                    // æ›´æ–°ç‰©æ–™ä¿¡æ¯
                                    _basicService.MaterielInfoService.Repository.UpdateData(medication);
                                }
                                // å¦‚果跳过该单,则直接continue,不保存
                                if (skipOrder)
                                {
                                    Console.WriteLine($"跳出处库单 {outorder.order_no},因为存在未定义物料属性的商品。");
                                    continue;
                                }
                                // ç¡®ä¿å‡ºåº“单有明细才添加
                                if (entityOrder.Details.Any())
                                {
                                    _DeliveryOrders.Add(entityOrder);
                                    orderNos.Add(outorder.order_no);
                                }
                            }
                        }
                    }
                    // æ‰¹é‡æ’入出库单和明细
                    if (_DeliveryOrders.Any())
                    {
                        BaseDal.Db.InsertNav(_DeliveryOrders).Include(x => x.Details).ExecuteCommand();
                    }
                    //if (_DeliveryOrders.Any())
                    //{
                    //    BaseDal.Db.InsertNav(_DeliveryOrders).Include(x => x.Details).ExecuteCommand();
                    //}
                    //下发出库单任务给wcs
                    Db.Ado.CommitTran();
                    var tex = CreateSupplyTask(order_no);
                    if (!tex)
                    {
                        return responseContent.Error("创建供应任务失败");
                    }
                    //var tex = CreateSupplyTask(order_no);
                    //if (!tex)
                    //{
                    //    return responseContent.Error("创建供应任务失败");
                    //}
                    return responseContent.OK($"同步出库单成功,共{_DeliveryOrders.Count}条");
                }
                catch (Exception ex)
@@ -1220,7 +1073,12 @@
                }
                #endregion
                if (supplyTask.WarehouseCode == WarehouseEnum.立库.ObjToInt().ToString("000"))
                    CreateAllocatInOut(materielInfo);//创建调拨任务
                {
                    materielInfo.Business_qty -= supplyTask.StockQuantity;
                    _basicService.MaterielInfoService.Repository.UpdateData(materielInfo);
                    if (materielInfo.Business_qty < materielInfo.MinQty)
                        CreateAllocatInOut(materielInfo);//创建调拨任务
                }
                _unitOfWorkManage.CommitTran();
                #endregion
                content.OK();