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
--[[ 
 编码: 
 名称: DeleteOutboundOrderDetail 
 作者: 
 入口函数:DeleteOutboundDetail 
 功能说明: 
 变更历史: 
     功能:
        - 校验出库单是否为新建状态;
        - 根据多字段删除对应明细(S_IO_NO、S_BS_NO、N_BS_ROW_NO、S_ITEM_CODE)
    参数格式:
        {
         "Name": "DeleteOutboundDetail",
         "Source": "ERP",
         "Data": {
                "S_NO": "CKD001",
                "SourceKey": "",
                "ITEMS": 
                [
                {
                "N_BS_ROW_NO": 10,
                "S_BS_NO": "10",
                "S_ITEM_CODE": "1"
                },
                {
                "N_BS_ROW_NO": 2,
                "S_BS_NO": "6",
                "S_ITEM_CODE": "999" 
                }
                ]
              }
        }
 --]] 
function DeleteOutboundDetail(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 data = inputData.Data
    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, "Outbound_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, "Outbound_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_OO_NO IN (SELECT S_NO FROM TN_Outbound_Order WHERE S_BS_NO = '"..s_no.."')  AND F_ACC_O_QTY <> 0 "
    nRet, container_data = m3.QueryDataObject(strLuaDEID, "Outbound_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_OO_NO IN (SELECT S_NO FROM TN_Outbound_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, "Outbound_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_OO_NO IN (SELECT S_NO FROM TN_Outbound_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 .. "'"
        lua.Debug(strLuaDEID, debug.getinfo(1), 'deleteCondition', deleteCondition)
        nRet, strRetInfo = mobox.dbdeleteData(strLuaDEID, "Outbound_Detail", deleteCondition)
        if nRet ~= 0 then
            lua.Stop(strLuaDEID, "删除失败:" .. strRetInfo)
            return
        end
    end
    
    -- 查询所有主表记录(可能多条)
    local queryOrderSql = "S_BS_NO = '"..s_no.."'"
    local nRet, orderData = m3.QueryDataObject(strLuaDEID, "Outbound_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_OO_NO = '" .. s_no .. "'"
            local nRet, detailData = m3.QueryDataObject(strLuaDEID, "Outbound_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, "Outbound_Order", deleteOrderCondition)
                if nRet ~= 0 then
                    lua.Stop(strLuaDEID, "删除失败:" .. strRetInfo)
                    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