1
Jianw
2025-07-09 88e26a2a960dbbc148332772448b79b9877102d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
--[[
 编码:
 名称: DeleteInboundOrder
 作者:
 入口函数:main
 功能说明:
 变更历史:
 输入数据格式:
    {
        "Name": "DeleteInboundOrder",
        "Source": "ERP",
        "Data":
            {
            "S_NO": "RKD001",
            "SourceKey": ""
            }
    }
 --]]
-- 引入基础模块
json = require("json")
mobox = require("OILua_JavelinExt")
m3 = require( "oi_base_mobox" )
wms_base = require( "wms_base" )
 
function main(strLuaDEID)
    
    
    -- 初始化变量
    local nRet, inputData
    local item
    local inbound_data
    local s_no
    
    local Condition
    
    local nRetl, strRetInfop,item
    local inboundOrder
    local strCondition
    local sqlstrCondition
    local container_data, nRet
    
    local err_code = 0
    local err_msg 
    local err = {}
 
    -- 获取接口传入的数据
    nRet, inputData = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0) then
        table.insert(err, "无法获取数据包:" .. inputData)
        goto continue
    end
 
    if (inputData == nil or inputData == "")then
        table.insert(err, "Data 不合法!:" .. inbound_data)
        goto continue
    end
    
    if (inputData.S_NO == nil or inputData.S_NO == "") then
        table.insert(err, "来源单号不能为空!:" .. inbound_data)
        goto continue
    end
    
    s_no = inputData.S_NO           -- 来源单号
 
    -- 用于校验入库单的状态值,料箱库的入库单看N_B_STATE,人工库的看累计入库数量是否有值
    strCondition = "S_BS_NO = '"..s_no.."' AND N_B_STATE <> 0  AND S_AREA_CODE = '料箱库' "
    nRet, inbound_date = m3.QueryDataObject(strLuaDEID, "Inbound_Order", strCondition)
    if (nRet ~= 0) then
        table.insert(err, "查询对应的入库单失败!:" .. s_no)
        goto continue
    end
    if (inbound_date ~= "") then
        table.insert(err, "该来源单号对应的入库单有非新建状态,不可删除!:" .. s_no)
        goto continue
    end
    
    
    Condition = "S_IO_NO IN (SELECT S_NO FROM TN_Inbound_Order WHERE S_BS_NO = '"..s_no.."')  AND F_ACC_I_QTY <> 0 "
    nRet, container_data = m3.QueryDataObject(strLuaDEID, "Inbound_Detail", Condition)
    if (nRet ~= 0) then
        table.insert(err, "查询对应的入库单失败!:" .. s_no)
        goto continue
    end
    
    if (container_data ~= "") then
        table.insert(err, "该来源单号对应的入库单明细有已入库的数据,不可删除!:" .. s_no)
        goto continue
    end
    
    -- 用来源单号查询入库单信息是否存在
    sqlstrCondition = "S_BS_NO = '" .. s_no .. "'"
    nRetl, strRetInfop = mobox.existThisData(strLuaDEID, "Inbound_Order", sqlstrCondition)
    if (nRetl ~= 0) then
        table.insert(err, "调用方法existThisData出错:" .. strRetInfop)
        goto continue
    end
    if (strRetInfop == 'yes') then
        -- 查询入库单信息
        local nRet, inbound_order = m3.QueryDataObject(strLuaDEID, "Inbound_Order", sqlstrCondition)
        if (nRet ~= 0 or not inbound_order) then
            lua.Stop(strLuaDEID, "查询失败: ".. s_no )
            return
        end
        
        
        nRet, strRetInfo = mobox.dbdeleteData(strLuaDEID, "Inbound_Order", sqlstrCondition)
        if (nRet ~= 0) then
            lua.Stop(strLuaDEID, "删除入库单失败:".. s_no)
            return
        end
        nRet, strRetInfo = mobox.dbdeleteData(strLuaDEID, "ERP_Inbound_Order", sqlstrCondition)
        if (nRet ~= 0) then
            lua.Stop(strLuaDEID, "删除ERP入库单失败:" .. s_no)
            return
        end
 
        for n = 1, #inbound_order do 
            local record = m3.KeyValueAttrsToObjAttr(inbound_order[n].attrs)
            local no = record.S_NO
            local sqlCondition = "S_IO_NO = '" .. no .. "'"
            nRet, strRetInfo = mobox.dbdeleteData(strLuaDEID, "ERP_Inbound_Detail", sqlCondition)
            if (nRet ~= 0) then
                lua.Stop(strLuaDEID, "删除入库单失败:"..no)
                return
            end
            nRet, strRetInfo = mobox.dbdeleteData(strLuaDEID, "Inbound_Detail", sqlCondition)
            if (nRet ~= 0) then
                lua.Stop(strLuaDEID, "删除ERP入库单失败:" ..no)
                return
            end
        end
        
    else 
        table.insert(err, "查询入库单失败")
    end
    
    -- 返回参数
    ::continue::
 
    if (#err > 0) then
        err_code = 1
        err_msg = "入库单删除失败!" .. table.concat(err, ",")
    else
        err_code = 0
        err_msg = "入库单删除成功!"
    end
 
    local result =
    {
        SourceKey = inputData.SourceKey,
        err_code = err_code,
        err_msg = err_msg,
        result = {
            S_NO = s_no
        }
    }
    mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
    
end