南骏 池
2025-06-20 7b84eb64d11b12b6fb41ae7f7085452512ad5d3f
service/Warehouse/MesInvItemOutsManager.cs
@@ -2,6 +2,7 @@
using NewPdaSqlServer.Dto.service;
using NewPdaSqlServer.entity;
using NewPdaSqlServer.util;
using Newtonsoft.Json;
using SqlSugar;
namespace NewPdaSqlServer.service.Warehouse;
@@ -339,9 +340,7 @@
        }
        if (!UtilityHelper.CheckGuid(parsedGuid))
        {
            throw new ApplicationException("更新错误,数据是不合法的");
        }
        var userName = entity.userName ?? "system";
        return Db.Updateable<MesInvItemOuts>()
@@ -376,14 +375,12 @@
    {
        var message = MesToErpParam(query);
        // var executeReturnIdentity =
        //     Db.Insertable(message).ExecuteReturnIdentity();
        // if (executeReturnIdentity > 0)
        // {
        //     message.Id = executeReturnIdentity;
        //     message.Pid = executeReturnIdentity;
        //     return message;
        // }
        var newGuid = Guid.NewGuid();
        message.Guid = newGuid;
        message.Pid = newGuid;
        var executeReturnIdentity =
            Db.Insertable(message).IgnoreColumns(true).ExecuteCommand();
        if (executeReturnIdentity > 0) return message;
        throw new Exception("获取数据失败");
    }
@@ -408,14 +405,17 @@
        var dto = new OutItemDto();
        dto.SumItem = GetItems(query);
        //dto.SumItem = GetItems(query);
        var mesInvItemStocks = Db.Queryable<MesInvItemStocks>()
            .Where(a => a.ItemBarcode == query.barcode).Single();
        if (mesInvItemStocks == null) return dto;
        dto.ItemNo = mesInvItemStocks.ItemNo;
        var mesItems = Db.Queryable<MesItems>()
            .Where(s => s.Id == mesInvItemStocks.ItemId).Single();
        dto.ItemNo = mesItems.ItemNo;
        dto.Quantity = mesInvItemStocks.Quantity;
        return dto;
@@ -443,6 +443,7 @@
            .Where((b, bar, c, d) => b.ItemOutId == parsedGuid)
            .Select((b, bar, c, d) => new MesInvItemOutCDetails
            {
                Guid = b.Guid,
                Quantity = b.Quantity,
                DepotSectionCode = b.DepotSectionCode,
                DepotCode = b.DepotCode,
@@ -464,12 +465,12 @@
        var tableName = "MES_INV_ITEM_OUTS_" + query.Type;
        if ("A".Equals(query.Type))
        {
            //erpParameters = GetErpParameters(query.billNo);
            erpParameters = GetErpParameters(query.id);
            title = "采购退货单" + query.billNo + "审核";
        }
        else if ("B".Equals(query.Type))
        {
            //erpParameters = GetDeApprovePam(query.id);
            erpParameters = GetDeApprovePam(query.id);
            title = "采购退货单" + query.billNo + "反审核";
        }
@@ -492,6 +493,72 @@
            ContentType = "application/x-www-form-urlencoded"
        };
        return message;
    }
    private string GetErpParameters(string? id)
    {
        var guid = Guid.Empty;
        if (string.IsNullOrEmpty(id)) return "";
        var isValid = Guid.TryParse(id, out guid);
        if (!isValid)
            throw new ApplicationException("GUID转换错误");
        var materials = Db
            .Queryable<MesInvItemOuts, MesInvItemOutItems,
                MesItems, MesUnit, MesInvItemArnDetail, MesDepots>(
                (a, b, c, d, e, f) => new JoinQueryInfos(
                    JoinType.Left, a.Guid == b.ItemOutId,
                    JoinType.Left, b.ItemId == c.Id,
                    JoinType.Left, d.Id.ToString() == c.ItemUnit,
                    JoinType.Left,
                    e.CbillNo == b.WorkNo && e.ItemId == b.ItemId,
                    JoinType.Left, f.DepotId == a.DepotId
                )).Where((a, b, c, d, e, f) => b.Guid == guid)
            .Select<Material>((a, b, c, d, e, f) => new Material
            {
                FMaterialId = c.ItemNo,
                FRMREALQTY = b.Quantity.ToString(),
                FStockId = f.DepotCode,
                FUnitID = d.Fnumber,
                FLot = b.WorkNo,
                F_MES_ENTRYID = b.Guid,
                FsrcEntryId = e.Guid.ToString()
            }).ToList();
        var mm = Db.Queryable<MesInvItemOuts>()
            .Where(a => a.Guid == guid).First();
        var jsonEntries = materials.Select(d => new
        {
            d.FMaterialId,
            d.FRMREALQTY,
            d.FStockId,
            d.FUnitID,
            d.FLot,
            F_MES_ENTRYID = d.F_MES_ENTRYID.ToString(),
            d.FsrcEntryId
        }).ToList();
        var fdate = DateTime.Now.ToString("yyyy-MM-dd");
        var jsonString = JsonConvert.SerializeObject(jsonEntries);
        var encodedUrl = "taskname=CGTL&mesid=" + guid +
                         "&optype=create&datajson={\"F_MES_ID\":\"" + guid
                         + "\",\"FDate\":\"" + fdate
                         + "\",\"FSRCBillTypeId\":\"" + "采购入库单"
                         + "\",\"FDESCRIPTION\":\"" + mm.Remark
                         + "\",\"FMRMODE\":\"" + mm.Fmrmode
                         + "\",\"cgtlentry\":" + jsonString
                         + "}";
        return encodedUrl;
    }
    private string GetDeApprovePam(string? id)
    {
        var encodedUrl = "taskname=CGTL&mesid=" + id +
                         "&optype=delete&datajson={}";
        return encodedUrl;
    }
    public bool ScanCode(WarehouseQuery query)
@@ -570,10 +637,7 @@
        //调用存储过程 
        var prcRfPdaBarcodeCgth = PrcRfPdaBarcodeCgth(query);
        if (!prcRfPdaBarcodeCgth)
        {
            throw new Exception("执行失败");
        }
        if (!prcRfPdaBarcodeCgth) throw new Exception("执行失败");
        // 变量用于保存查询结果
        decimal? C_SQ_QTY = 0;
@@ -582,17 +646,15 @@
        sql = string.Format(@"
                SELECT SUM(C.QUANTITY) AS SQ_QTY, SUM(D.QUANTITY_OK) AS OK_QTY
                FROM MES_INV_ITEM_OUT_ITEMS C
                LEFT JOIN (
                    SELECT ITEM_OUT_ID, ITEM_NO, PBILL_NO, RK_NO, SUM(QUANTITY) AS QUANTITY_OK
            FROM MES_INV_ITEM_OUT_ITEMS C
         LEFT JOIN (SELECT ITEM_OUT_ID, ITEM_ID, PBILL_NO, RK_NO, SUM(QUANTITY) AS QUANTITY_OK
                    FROM MES_INV_ITEM_OUT_C_DETAILS
                    GROUP BY ITEM_OUT_ID, ITEM_NO, PBILL_NO, RK_NO
                ) D
                ON D.ITEM_OUT_ID = C.ITEM_OUT_ID
                AND D.ITEM_NO = C.ITEM_NO
                AND D.RK_NO = C.RK_NO
                AND isnull(C.PBILL_NO, '0') = isnull(D.PBILL_NO, '0')
                WHERE C.ITEM_OUT_ID = '{0}'", mesInvItemOuts.Guid);
                    GROUP BY ITEM_OUT_ID, ITEM_ID, PBILL_NO, RK_NO) D
                   ON D.ITEM_OUT_ID = C.ITEM_OUT_ID
                       AND D.ITEM_ID = C.ITEM_ID
                       AND isnull(C.PBILL_NO, '0') = isnull(D.PBILL_NO, '0')
                    WHERE C.ITEM_OUT_ID = '{0}'",
            mesInvItemOuts.Guid);
        var queryResult = Db.Ado.SqlQuerySingle<dynamic>(sql);
@@ -617,10 +679,7 @@
        var commit = UseTransaction(db =>
        {
            if (string.IsNullOrEmpty(p_bill_no))
            {
                throw new Exception("请选取单据号!");
            }
            if (string.IsNullOrEmpty(p_bill_no)) throw new Exception("请选取单据号!");
            // Retrieve mes_inv_item_outs
            var c_mes_inv_item_outs = db.Queryable<MesInvItemOuts>()
@@ -628,19 +687,13 @@
                .First();
            if (c_mes_inv_item_outs == null)
            {
                throw new Exception("请选择或扫描正确的采购退货单!");
            }
            if (c_mes_inv_item_outs.Status == 0)
            {
                throw new Exception("采购退货申请单未审核!");
            }
            if (c_mes_inv_item_outs.Nflag == 1)
            {
                throw new Exception("采购退货单已完结推送ERP,不能操作!");
            }
            // Check for duplicate barcode scan
            c_num = db.Queryable<MesInvItemOutCDetails>()
@@ -649,10 +702,7 @@
                    b.ItemOutId == c_mes_inv_item_outs.Guid)
                .Count();
            if (c_num > 0)
            {
                throw new Exception("此条码已采购退货扫码完成!");
            }
            if (c_num > 0) throw new Exception("此条码已采购退货扫码完成!");
            // Query item stock info
            var c_mes_inv_item_stocks = db.Queryable<MesInvItemStocks>()
@@ -660,17 +710,13 @@
                .First();
            if (c_mes_inv_item_stocks == null)
            {
                throw new Exception($"库存中无此条码,请核对!{p_item_barcode}");
            }
            // Validate depot codes
            if (c_mes_inv_item_outs.DepotCode !=
                c_mes_inv_item_stocks.DepotsCode)
            {
                throw new Exception(
                    $"此条码属于仓库 {c_mes_inv_item_stocks.DepotsCode} 与采购退货申请仓库 {c_mes_inv_item_outs.DepotCode} 不一致,请核对!");
            }
            C_STOCKNUM = c_mes_inv_item_stocks.Quantity.Value;
@@ -678,7 +724,7 @@
            var quantities = db.Queryable<MesInvItemOutItems, MesInvItemOuts>(
                    (s, d) => new object[]
                    {
                        JoinType.Left, s.ItemOutId == d.Guid,
                        JoinType.Left, s.ItemOutId == d.Guid
                    })
                .Where((s, d) =>
                    d.ItemOutNo == p_bill_no &&
@@ -693,9 +739,7 @@
                .First();
            if (quantities == null)
            {
                throw new Exception("采购退料单 '" + p_bill_no + "'未审核");
            }
            C_SQTY = quantities.TotalQuantity ?? 0;
            C_OKQTY = quantities.CompletedQuantity ?? 0;
@@ -704,7 +748,7 @@
            if (C_CQTY < C_STOCKNUM) return 1;
            // Handle barcode reduction
            decimal p_number2 = C_STOCKNUM;
            var p_number2 = C_STOCKNUM;
            var itemOutItems = db.Queryable<MesInvItemOutItems>()
                .Where(s =>
                    s.ItemOutId == c_mes_inv_item_outs.Guid &&
@@ -729,9 +773,7 @@
                    .First();
                if (c_mes_inv_item_arn_detail == null)
                {
                    throw new Exception("未找到采购退料来源单据");
                }
                if (CMI.Quantity - CMI.TlQty <= p_number2)
                {
@@ -792,15 +834,13 @@
                        TaskNo = CMI.WorkNo,
                        ItemId = c_mes_inv_item_stocks.ItemId,
                        EbelnK3id = c_mes_inv_item_stocks.EbelnK3id,
                        LineK3id = c_mes_inv_item_stocks.LineK3id,
                        LineK3id = c_mes_inv_item_stocks.LineK3id
                    }).IgnoreColumns(true).ExecuteCommand();
                    if (p_number2 == 0)
                    {
                        db.Deleteable<MesInvItemStocks>()
                            .Where(s => s.Guid == c_mes_inv_item_outs.Guid)
                            .ExecuteCommand();
                    }
                }
                else
                {
@@ -865,16 +905,13 @@
                        SuppId = c_mes_inv_item_outs.SuppId.ToString(),
                        ItemId = c_mes_inv_item_stocks.ItemId,
                        EbelnK3id = c_mes_inv_item_stocks.EbelnK3id,
                        LineK3id = c_mes_inv_item_stocks.LineK3id,
                        LineK3id = c_mes_inv_item_stocks.LineK3id
                    }).IgnoreColumns(true).ExecuteCommand();
                    p_number2 = 0;
                }
            }
            if (p_number2 > 0)
            {
                throw new Exception("物料数量将超采购退料申请单数量,无法出库");
            }
            if (p_number2 > 0) throw new Exception("物料数量将超采购退料申请单数量,无法出库");
            return 1;
        });