1
Jianw
9 天以前 70f29da38121b9a467841253e3268feb5df02902
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
--[[
编码: JX-API-XX
名称: DeleteInboundOrderDetail
作者: kun
入口函数: AfterDataObjDelete
功能说明:
    删除指定入库单下指定的入库明细,前提是入库单状态为“新建”(b_state == 0)
    多条明细通过 ITEMS 数组传入,根据 S_IO_NO、S_BS_NO、N_BS_ROW_NO、S_ITEM_CODE 匹配
参数格式:
        {
         "Name": "DeleteInboundOrderDetail",
         "Source": "ERP",
         "Data": {
                "S_NO": "RKD001",
                "SourceKey": "",
                "ITEMS": [{
                   "N_BS_ROW_NO": 1,
                   "S_BS_NO": "1",
                   "S_ITEM_CODE": "01.11.30.345"
                     }]
              }
        }
--]]
 
json = require("json")
mobox = require("OILua_JavelinExt")
m3 = require("oi_base_mobox")
 
 
function AfterDataObjDelete(strLuaDEID)
    local err = {}
    local nRet, inputData = m3.GetSysDataJson(strLuaDEID)
    if nRet ~= 0 then
        local result = {
            SourceKey = "",
            err_code = 1,
            err_msg = "无法获取数据包!" .. inputData,
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
 
    local items = inputData.ITEMS 
    local s_no = inputData.S_NO  --来源单号
    
 
    if not s_no or s_no == "" or #items == 0 then
        local result = {
            SourceKey = "",
            err_code = 1,
            err_msg = "入库单号为空或无明细!" .. s_no,
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
 
    -- 查询入库单信息是否存在
    local nRetl , strRetInfop
    local OrderstrCondition = "S_BS_NO = '" .. s_no .. "'"
    nRetl, strRetInfop = mobox.existThisData(strLuaDEID, "Inbound_Order", OrderstrCondition)
    if (nRetl ~= 0) then
        local result = {
            SourceKey = "",
            err_code = 1,
            err_msg = "调用existThisData方法失败" .. s_no,
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
    if (strRetInfop ~= 'yes') then 
        local result = {
            SourceKey = "",
            err_code = 1,
            err_msg = "查询对应的入库单失败" .. s_no,
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
    
    -- 下面两个判断,1、判断料箱库的入库单是否有非新建状态的单子,2、判断非料箱库的入库单,明细中是否有已入库数量的明细(代表非新增状态)
    local inbound_date
    local 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
        local result = {
            SourceKey = "",
            err_code = 1,
            err_msg = "查询对应的入库单失败" .. s_no,
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
    if (inbound_date ~= "") then
        local result = {
            SourceKey = "",
            err_code = 1,
            err_msg = "该来源单号对应的入库单有非新建状态,不可删除" .. s_no,
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
    local container_data, nRet
    local 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
        local result = {
            SourceKey = "",
            err_code = 1,
            err_msg = "查询对应的入库单失败" .. s_no,
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
    if (container_data ~= "") then
        local result = {
            SourceKey = "",
            err_code = 1,
            err_msg = "该来源单号对应的入库单明细有已入库的数据,不可删除" .. s_no,
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
    
 
    -- 查询入库单明细是否有对应的值
    local bs_no ,bs_row, item_code
    local nRet , strRetInfo
    for _, item in ipairs(items) do
        bs_no = item.S_BS_NO
        bs_row = item.N_BS_ROW_NO 
        item_code = item.S_ITEM_CODE 
        
        if(bs_no == nil or bs_no == "") then
            table.insert(err, "来源单号不能为空" ..item_code)
        end
        if(item_code == nil or item_code == "") then
            table.insert(err, "商品编码不能为空" ..item_code)
            end
        if(bs_row == nil or bs_row == "") then
            table.insert(err, "来源单行号不能为空" ..item_code)
        end
        if(type(item.N_BS_ROW_NO) ~= "number") then
            table.insert(err, "来源单行号非数字型" ..item_code)
        end
 
        local Condition = " S_IO_NO IN (SELECT S_NO FROM TN_Inbound_Order WHERE S_BS_NO = '"..s_no.."')  AND S_BS_NO = '" .. bs_no .. "' AND N_BS_ROW_MO = " .. bs_row .. " AND S_ITEM_CODE = '" .. item_code .. "'"
        nRet, strRetInfo = mobox.existThisData( strLuaDEID, "ERP_Inbound_Detail", Condition )
        if ( nRet ~= 0 ) then 
            table.insert(err, "调用existThisData方法失败"..item_code)
        end
        if ( strRetInfo ~= "yes" ) then 
            table.insert(err, "该入库单没有这条数据"..item_code)
        end   
        
    end
    
    if (#err > 0) then
        local result = {
            SourceKey = "",
            err_code = 1,
            err_msg = "明细校验失败:" .. table.concat(err, ","),
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
 
    -- 遍历删除每一项明细
    for _, item in ipairs(items) do
        local bs_no = item.S_BS_NO 
        local bs_row = item.N_BS_ROW_NO 
        local item_code = item.S_ITEM_CODE 
 
        
        local deleteCondition = " S_IO_NO IN (SELECT S_NO FROM TN_ERP_Inbound_Order WHERE S_BS_NO = '"..s_no.."')  AND S_BS_NO = '" .. bs_no .. "' AND N_BS_ROW_MO = " .. bs_row .. " AND S_ITEM_CODE = '" .. item_code .. "'"
        local nRet, ERP_inbound_order = m3.GetDataObjByCondition(strLuaDEID, "ERP_Inbound_Detail", deleteCondition)
        if (nRet ~= 0 or not ERP_inbound_order) then
            lua.Stop(strLuaDEID, "查询失败: "..item.S_ITEM_CODE )
            return
        end
        local qty = ERP_inbound_order.qty
        local no = ERP_inbound_order.io_no
        
        lua.Debug( strLuaDEID, debug.getinfo(1), "ERP_inbound_order", ERP_inbound_order)
        
        nRet, strRetInfo = mobox.deleteDataObject(strLuaDEID, "ERP_Inbound_Detail", deleteCondition)
        if nRet ~= 0 then
            lua.Stop(strLuaDEID, "删除失败:" .. strRetInfo)
            return
        end
        
        local sqlCondition = " S_IO_NO ='" ..no.."'  AND S_ITEM_CODE = '" .. item_code .. "'"
        local nRet, inbound_order = m3.GetDataObjByCondition(strLuaDEID, "Inbound_Detail", sqlCondition)
        if (nRet ~= 0 or not inbound_order) then
            lua.Stop(strLuaDEID, "查询失败: "..item.S_ITEM_CODE )
            return
        end
        
        local old_qty = tonumber(inbound_order.qty or 0)
        local new_qty = old_qty - qty
        if(new_qty ~= 0) then
            local strUpdateSql = "F_QTY = " .. new_qty
            local retUpdate, strInfo = mobox.updateDataAttrByCondition(strLuaDEID, "Inbound_Detail", sqlCondition, strUpdateSql)
            if retUpdate ~= 0 then
                lua.Stop(strLuaDEID, "更新 Inbound_Detail 失败:" .. strInfo)
                return
            end
        else
            local nRet, strRetInfo = mobox.dbdeleteData(strLuaDEID, "Inbound_Detail", sqlCondition)
            if nRet ~= 0 then
                lua.Stop(strLuaDEID, "删除Inbound_Detail失败:" .. strInfo)
                return
            end
        end
        
    end
    
        -- 查询所有主表记录(可能多条)
    local queryOrderSql = "S_BS_NO = '"..s_no.."'"
    local nRet, orderData = m3.QueryDataObject(strLuaDEID, "ERP_Inbound_Order", queryOrderSql)
    if nRet ~= 0 then
        local result = {
            SourceKey = inputData.SourceKey or "",
            err_code = 1,
            err_msg = "查询入库单主表失败:" .. (orderData or ""),
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
 
    -- orderData 是一条或多条,逐条处理
    if orderData ~= "" then
        for n = 1, #orderData do
            local order = m3.KeyValueAttrsToObjAttr(orderData[n].attrs)
            local s_no = order.S_NO
            -- 查询当前主单下面还有没有明细
            local detailCondition = "S_IO_NO = '" .. s_no .. "'"
            local nRet, detailData = m3.QueryDataObject(strLuaDEID, "ERP_Inbound_Detail", detailCondition)
            if nRet ~= 0 then
                local result = {
                    SourceKey = inputData.SourceKey or "",
                    err_code = 1,
                    err_msg = "查询入库明细失败:" .. (detailData or ""),
                    result = nil
                }
                mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
                return
            end
            
            -- 如果没有明细了,删除这张主单
            if detailData == "" then
                local deleteOrderCondition = "S_NO = '" .. s_no .. "'"
                local nRet, strRetInfo = mobox.dbdeleteData(strLuaDEID, "ERP_Inbound_Order", deleteOrderCondition)
                if nRet ~= 0 then
                    lua.Stop(strLuaDEID, "删除ERP主表失败:" .. strInfo)
                    return
                end
                local nRet, strRetInfo = mobox.dbdeleteData(strLuaDEID, "Inbound_Order", deleteOrderCondition)
                if nRet ~= 0 then
                    lua.Stop(strLuaDEID, "删除主表失败:" .. strInfo)
                    return
                end
                local sqldeleteCondition = "S_IO_NO = '" .. s_no .. "'"
                nRet, strRetInfo = mobox.dbdeleteData(strLuaDEID, "Inbound_Detail", sqldeleteCondition)
                if (nRet ~= 0) then
                    lua.Stop(strLuaDEID, "删除入库单明细失败:" ..s_no)
                    return
                end
            end
        end
    end
 
 
    -- 成功返回
    local result = {
        SourceKey = inputData.SourceKey or "",
        err_code = 0,
        err_msg = "删除入库单明细成功",
        result = ""
    }
    mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
end