--[[
|
编码: 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
|