11
啊鑫
2024-12-30 250ae3deeabd8e0818f4adf57f747b71fefd5ef3
service/Warehouse/MesItemQtManager.cs
@@ -24,7 +24,7 @@
    }
    /// <summary>
    /// 获取待处理的退料单明细列表
    ///     获取待处理的退料单明细列表
    /// </summary>
    /// <param name="query">仓库查询参数,包含用户名和单据号</param>
    /// <returns>待处理的退料单明细列表</returns>
@@ -40,22 +40,15 @@
            .First();
        // 如果未找到退料单,抛出异常
        if (mesItemQt == null)
        {
            throw new Exception($"未查询到此其他入库申请单 {p_bill_no}");
        }
        if (mesItemQt == null) throw new Exception($"未查询到此其他入库申请单 {p_bill_no}");
        // 检查退料单的审核状态(Qt015),未审核则抛出异常
        if (mesItemQt.Qt015 == false)
        {
            throw new Exception($"其他出库申请单 {p_bill_no} 未审核,请确认!");
        }
        // 检查退料单的完结状态(Qt014),已完结则抛出异常
        if (mesItemQt.Qt014 == true)
        {
            throw new Exception($"其他出库申请单 {p_bill_no} 已完结,请确认!");
        }
        // 联表查询获取未完成的明细列表
        // 关联表:
@@ -97,10 +90,7 @@
        var p_transaction_no = 202;
        // 检查单据号是否为空
        if (string.IsNullOrEmpty(p_bill_no))
        {
            throw new Exception("请选取单据号!");
        }
        if (string.IsNullOrEmpty(p_bill_no)) throw new Exception("请选取单据号!");
        // 检查是否重复扫描
        var existingBarcode = Db.Queryable<MesInvItemOutCDetails>()
@@ -108,10 +98,7 @@
                it.ItemBarcode == p_item_barcode && it.PbillNo == p_bill_no)
            .Any();
        if (existingBarcode)
        {
            throw new Exception("此条码已经扫码出库,勿重复扫描!");
        }
        if (existingBarcode) throw new Exception("此条码已经扫码出库,勿重复扫描!");
        // 获取条码信息
        var barcode = Db.Queryable<MesInvItemBarcodes>()
@@ -119,9 +106,7 @@
            .First();
        if (barcode == null)
        {
            throw new Exception($"mes中不存在此条码,请核对!{p_item_barcode}");
        }
        // 获取库存信息
        var stock = Db.Queryable<MesInvItemStocks>()
@@ -129,20 +114,14 @@
            .First();
        if (stock == null)
        {
            throw new Exception($"库存中无此条码,请检查条码是否未入库或已出库!{p_item_barcode}");
        }
        if (string.IsNullOrEmpty(stock.DepotsCode))
        {
            throw new Exception($"调拨中的条码不可发料,请先完成调拨单据{p_item_barcode}");
        }
        if (stock.DepotsCode is "S006" or "S005")
        {
            throw new Exception($"条码在不良品仓下 不可发料{p_item_barcode}");
        }
        // 检查是否在寄存仓位
        // var isDepotSection = Db.Queryable<MesJcDepot>()
@@ -160,16 +139,12 @@
            .First();
        if (mesItemQt == null)
        {
            throw new Exception($"其他出库申请单 {p_bill_no} 不存在,请确认!");
        }
        // 检查仓库是否一致
        if (mesItemQt.Qt011 != stock.DepotsCode)
        {
            throw new Exception(
                $"扫码出货仓库{stock.DepotsCode}与其他出库申请仓库{mesItemQt.Qt011}不一致,请核对!");
        }
        // 获取出库单明细
        var qtDetail = Db.Queryable<MesItemQtDatall>()
@@ -178,17 +153,12 @@
                it.ItemId == stock.ItemId.ToString())
            .First();
        if (qtDetail == null)
        {
            throw new Exception("扫码物料非本次出库申请物料,请核对!");
        }
        if (qtDetail == null) throw new Exception("扫码物料非本次出库申请物料,请核对!");
        // 检查剩余数量
        var remainingQty = (qtDetail.Qd007 ?? 0) - (qtDetail.Qd008 ?? 0);
        if (remainingQty <= 0)
        {
            throw new Exception($"申请物料 {barcode.ItemNo} 已出库完成!");
        }
        // 检查数量是否超出
        if (stock.Quantity > remainingQty)
@@ -295,10 +265,7 @@
                    it.ItemId == stock.ItemId.ToString())
                .First();
            if (qtDetail == null)
            {
                throw new Exception($"未找到对应的退料单明细信息");
            }
            if (qtDetail == null) throw new Exception("未找到对应的退料单明细信息");
            // 检查是否已存在出库物料记录
            var existingOutItem = Db.Queryable<MesInvItemOutItems>()
@@ -329,7 +296,7 @@
                    ItemId = !string.IsNullOrEmpty(qtDetail.ItemId)
                        ? long.Parse(qtDetail.ItemId)
                        : null,
                    FType = 0,
                    FType = 0
                    // Unit = qtDetail.Qd009
                };
@@ -429,12 +396,10 @@
            // 检查明细是否完成,如果完成则更新状态
            if ((qtDetail.Qd007 ?? 0) - (qtDetail.Qd008 ?? 0) == stock.Quantity)
            {
                commit += db.Updateable<MesItemQtDatall>()
                    .SetColumns(it => it.Qd011 == 1)
                    .Where(it => it.Guid == qtDetail.Guid)
                    .ExecuteCommand();
            }
            mesItemQtDatalls = Db
                .Queryable<MesItemQt, MesItemQtDatall, MesItems>(
@@ -456,12 +421,10 @@
            // 如果没有待处理明细,更新退料单状态为已完成
            if (CollectionUtil.IsNullOrEmpty(mesItemQtDatalls))
            {
                db.Updateable<MesItemQt>()
                    .SetColumns(it => it.Qt014 == true)
                    .Where(it => it.Qtck == p_bill_no)
                    .ExecuteCommand();
            }
            // 构建返回消息
            mess = $"扫码成功!条码 {p_item_barcode} 数量 {stock.Quantity} 已出库";
@@ -471,10 +434,7 @@
            query.Num = stock.Quantity;
            query.Fum = null;
            if (commit < 5)
            {
                throw new Exception("更新失败");
            }
            if (commit < 5) throw new Exception("更新失败");
            return commit;
        });
@@ -494,16 +454,10 @@
        var p_transaction_no = 202;
        // 检查单据号是否为空
        if (string.IsNullOrEmpty(p_bill_no))
        {
            throw new Exception("请选取单据号!");
        }
        if (string.IsNullOrEmpty(p_bill_no)) throw new Exception("请选取单据号!");
        // 检查数量是否有效
        if (p_qty <= 0)
        {
            throw new Exception("请输入正确的发料数量!");
        }
        if (p_qty <= 0) throw new Exception("请输入正确的发料数量!");
        // 获取库存条码信息
        var stock = Db.Queryable<MesInvItemStocks>()
@@ -511,19 +465,13 @@
            .First();
        if (stock == null)
        {
            throw new Exception($"库存中无此条码,请检查条码是否未入库或已出库!{p_old_barcode}");
        }
        if (string.IsNullOrEmpty(stock.DepotsCode))
        {
            throw new Exception($"调拨中的条码不可发料,请先完成调拨单据{p_old_barcode}");
        }
        if (stock.DepotsCode is "S006" or "S005")
        {
            throw new Exception($"条码在不良品仓下 不可发料{p_old_barcode}");
        }
        // 获取条码信息
        var barcode = Db.Queryable<MesInvItemBarcodes>()
@@ -531,9 +479,7 @@
            .First();
        if (barcode == null)
        {
            throw new Exception($"mes中不存在此条码,请核对!{p_old_barcode}");
        }
        // 获取退料单信息
        var mesItemQt = Db.Queryable<MesItemQt>()
@@ -541,15 +487,11 @@
            .First();
        if (mesItemQt == null)
        {
            throw new Exception($"其他出库申请单 {p_bill_no} 不存在,请确认!");
        }
        if (mesItemQt.Qt011 != stock.DepotsCode)
        {
            throw new Exception(
                $"扫码出货仓库{stock.DepotsCode}与其他出库申请仓库{mesItemQt.Qt011}不一致,请核对!");
        }
        // 获取退料单明细
        var qtDetail = Db.Queryable<MesItemQtDatall>()
@@ -558,32 +500,23 @@
                it.ItemId == stock.ItemId.ToString())
            .First();
        if (qtDetail == null)
        {
            throw new Exception("扫码物料非本次出库申请物料,请核对!");
        }
        if (qtDetail == null) throw new Exception("扫码物料非本次出库申请物料,请核对!");
        var remainingQty = (qtDetail.Qd007 ?? 0) - (qtDetail.Qd008 ?? 0);
        if (remainingQty <= 0)
        {
            throw new Exception($"申请物料 {barcode.ItemNo} 已出库完成!");
        }
        if (p_qty > remainingQty)
        {
            throw new Exception(
                $"输入的拆分数量 {p_qty} 不可大于剩余需发数量 {remainingQty} 请修改");
        }
        var totalQty = Db.Queryable<MesInvItemStocks>()
            .Where(it => it.ItemBarcode == p_old_barcode && it.Quantity > 0)
            .Sum(it => it.Quantity);
        if (totalQty < p_qty)
        {
            throw new Exception($"输入的发料数量 {p_qty} 不可大于条码数量 {totalQty} 请修改");
        }
        var message = string.Empty;
        var mesItemQtDatalls = new List<MesItemQtDatall>();
@@ -633,7 +566,7 @@
                    ComeFlg = 5,
                    EbelnK3id = barcode.EbelnK3id,
                    LineK3id = barcode.LineK3id,
                    ItemId = barcode.ItemId,
                    ItemId = barcode.ItemId
                }).IgnoreColumns(true).ExecuteCommand();
                // 更新原条码数量
@@ -667,7 +600,7 @@
                    BillNo = stock.BillNo,
                    EbelnK3id = stock.EbelnK3id,
                    LineK3id = stock.LineK3id,
                    ItemId = stock.ItemId,
                    ItemId = stock.ItemId
                }).IgnoreColumns(true).ExecuteCommand();
@@ -702,7 +635,7 @@
                    LineK3id = stock.LineK3id,
                    SuppId = stock.SuppId,
                    SuppNo = stock.SuppNo,
                    ItemId = stock.ItemId,
                    ItemId = stock.ItemId
                }).IgnoreColumns(true).ExecuteCommand();
            }
            else
@@ -733,7 +666,7 @@
                commit += db.Insertable(new MesInvItemOuts
                {
                    Guid = outId,
                    ItemOutNo = $"OUT-{DateTime.Now:yyyyMMddHHmmss}",
                    ItemOutNo = outNo,
                    TaskNo = p_bill_no,
                    Status = 0,
                    CreateBy = c_user,
@@ -751,9 +684,8 @@
                    PbillNo = p_bill_no,
                    OutDate = DateTime.Now,
                    Nflag = 0,
                    Reason = mesItemQt.Qt010,
                    Reason = mesItemQt.Qt010
                }).ExecuteReturnIdentity();
                outNo = $"OUT-{DateTime.Now:yyyyMMddHHmmss}";
            }
            else
            {
@@ -768,7 +700,6 @@
                .First();
            if (outItem == null)
            {
                // 插入新明细
                db.Insertable(new MesInvItemOutItems
                {
@@ -789,12 +720,10 @@
                    ItemId = !string.IsNullOrEmpty(qtDetail.ItemId)
                        ? long.Parse(qtDetail.ItemId)
                        : null,
                    FType = 0,
                    FType = 0
                    // Unit = qtDetail.Qd009
                }).ExecuteCommand();
            }
            else
            {
                // 更新明细数量
                db.Updateable<MesInvItemOutItems>()
                    .SetColumns(it =>
@@ -804,7 +733,6 @@
                        it.ItemId == stock.ItemId &&
                        it.QtOutId == qtDetail.Guid)
                    .ExecuteCommand();
            }
            // 插入出库明细
            db.Insertable(new MesInvItemOutCDetails
@@ -887,12 +815,10 @@
            // 如果所有明细完成,更新退料单状态
            if (!hasUnfinished)
            {
                db.Updateable<MesItemQt>()
                    .SetColumns(it => it.Qt014 == true)
                    .Where(it => it.Qtck == p_bill_no)
                    .ExecuteCommand();
            }
            // 获取剩余待处理明细
            mesItemQtDatalls = db