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
--[[
    编码: JX-API-OUTBOUND-01
    名称: 创建出库单及明细
    作者: 
    日期: 2025-1-29
 
    入口函数: main
    功能说明:
     
    输入数据格式:
    {
       "taskId": "361de469-8f6b-42c8-8cf1-054a8024ea5b",
        "taskType": "CGTH",
        "orderNo": "432536433214321",
        "customerId": "11",
        "orderDate": "2020-12-22",
        "priority": 0,
        "memo": "",
        "items": [
            {
                "orderItemId":"1",
                "storerId": "HC",
                "skuId": "2200011995005",
                "skuName": "",
                "qty": 20,
                "batchNo": "44",
                "contractNo": "",
                "memo":""
            }
        ]
    }
 
    响应示例:
    {
        "flag": "success",
        "code": "0",
        "message": "成功"
    }
--]]
 
json = require("json")
mobox = require("OILua_JavelinExt")
m3 = require( "oi_base_mobox" )
wms_base = require( "wms_base" )
 
function CreateOrderBound(strLuaDEID)
local nRet, inputData
 
    -- 获取接口传入的数据
    nRet, inputData = m3.GetSysDataJson(strLuaDEID)
    lua.DebugEx(strLuaDEID, "inputData-->", inputData)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "无法获取数据包 datajson!")
    end
 
    -- 验证来源类型、移库计划单号和单据类型
    if (inputData.orderNo == nil or inputData.orderNo == '') then
        lua.Error(strLuaDEID, debug.getinfo(1), "关联单号不能为空!")
    end
 
    if (inputData.taskType == nil or inputData.taskType == '') then
        lua.Error(strLuaDEID, debug.getinfo(1), "任务类型不能为空!")
    end
 
    if (inputData.customerId == nil or inputData.customerId == '') then
        lua.Error(strLuaDEID, debug.getinfo(1), "供应商不能为空!")
    end
 
 
    -- 使用移库计划单号查询是否已存在相同的出库单
    local strCondition = "S_NO = '" .. inputData.orderNo .. "'"
    local nRetl, strRetInfo = mobox.existThisData(strLuaDEID, "Outbound_Order", strCondition)
    
    if (nRetl ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "调用方法 existThisData 出错: " .. strRetInfo)
    end
 
    if (strRetInfo == 'yes') then
        lua.Error(strLuaDEID, debug.getinfo(1), "出库单已存在,移库计划单号:" .. inputData.order_no)
    end
 
    -- 创建出库单记录
    local outbound_order = m3.AllocObject(strLuaDEID, "Outbound_Order")
    
    outbound_order.no = inputData.orderNo    -- 出库单号
    outbound_order.task_no = inputData.taskId
    outbound_order.bs_type = inputData.taskType
    outbound_order.storer = inputData.customerId
    outbound_order.order_time = inputData.orderDate
    outbound_order.priority = inputData.priority
    outbound_order.memo = inputData.memo
    outbound_order.wh_code = "HCCK"
    outbound_order.area_code = "JBW"
    nRet, strRetInfop = m3.CreateDataObj(strLuaDEID, outbound_order)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "创建出库单记录失败!"..strRetInfop)
    else
        lua.Debug(strLuaDEID, debug.getinfo(1), "outbound_order", strRetInfop)
 
        local nRet, strRetIinfo
        local item
        local strCondition
        local outbound_detail
 
        -- 遍历商品明细数组
        for n = 1, #inputData.items do
 
            item = inputData.items[n]
 
            -- 检查商品编码和数量是否存在且有效
            if (item.skuId == nil or item.skuId == '') then
                lua.Error(strLuaDEID, debug.getinfo(1), "第 " .. n .. " 个商品的商品编码不能为空")
            end
            if (item.qty == nil or item.qty <= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "第 " .. n .. " 个商品的数量 qty 必须大于 0")
            end
 
            -- 检查物料信息是否存在
            strCondition = "S_ITEM_CODE = '" .. item.skuId .. "'"
            nRetl, strRetIinfo = mobox.existThisData(strLuaDEID, "Material", strCondition)
 
            if (nRetl ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "调用方法existThisData出错: " .. strRetIinfo)
            end
 
            if (strRetIinfo == 'yes') then
                lua.Error(strLuaDEID, debug.getinfo(1), "物料编码不存在,商品编码:" .. item.skuId)
            else
                
                -- 创建出库单明细记录
                outbound_detail = m3.AllocObject(strLuaDEID, "Outbound_Detail")
                outbound_detail.oo_no = inputData.orderNo         -- 设置出库单号
                outbound_detail.row_no = item.orderItemId
                outbound_detail.storer = item.storerId
                outbound_detail.item_code = item.skuId
                outbound_detail.item_name = item.skuName
                outbound_detail.qty = item.qty                      -- 设置物料数量
                outbound_detail.batch_no = item.batchNo
                outbound_detail.bs_no = "11"
                -- 销售合同号
                outbound_detail.udf01 = item.contractNo
                -- 备注
                outbound_detail.udf02 = item.memo
 
                -- 创建出库单明细记录
                nRet, strRetInfop = m3.CreateDataObj(strLuaDEID, outbound_detail)
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "创建出库单明细记录失败,商品编码:" .. strRetInfop)
                end
            end
        end
 
        local result = {
            flag = "success",
            code = "0",
            message = "成功"
        }
 
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
    end
end