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
--[[
   编码: AMS-105-1
   名称: 
   作者:
   日期:2025-05-23
 
   函数: Import
   功能:
 
   更改记录:
 
--]]
 
json = require("json")
mobox = require("OILua_JavelinExt")
m3 = require("oi_base_mobox")
wms_base = require("wms_base")
 
function Import(strLuaDEID)
 
    local nRet, row_data = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "无法获取入库单导入数据!") end
    lua.Debug(strLuaDEID, debug.getinfo(1), 'row_data', row_data)
 
    local row_attrs, nCount, nRows
    nRows = #row_data
    if (nRows == 0) then
        mobox.setInfo(strLuaDEID, "导入数据为空!")
        return
    end
 
    -- 按库区分组的入库单数据
    local area_groups = {}
    local err = {}
    -- 循环处理每一行数据
    for row = 1, nRows do
        row_attrs = row_data[row]
        nCount = #row_attrs
        lua.Debug(strLuaDEID, debug.getinfo(1), 'row_attrs', row_attrs)
        -- 初始化变量
        local wh_code = ''
        local item_code = ''
        local qty = ''
        local bs_no = ''
        local s_bs_no = ''
        local bs_row_no = ''
        local batch_no = ''
        local bs_type = ''
        local note = ''
        
        -- 子表扩展字段
        local stock_status = ''
        local production_date = ''
        local supplier_code = ''
        local supplier_name = ''
        local net_weight = ''
        local origin = ''
        local remark1 = ''
        local remark2 = ''
        local remark3 = ''
        local remark4 = ''
        local remark5 = ''
        
        -- 提取基础信息和扩展字段
        for n = 1, nCount do
            local strAttr = row_attrs[n].attr
            local strValue = row_attrs[n].value
            lua.Debug(strLuaDEID, debug.getinfo(1), 'strAttr', strAttr)
            -- 主表字段
            if strAttr == "货品编码" then
                item_code = strValue
            elseif strAttr == "数量" then
                qty = strValue
            elseif strAttr == "来源单号" then
                bs_no = strValue
            elseif strAttr == "物料来源单号" then
                s_bs_no = strValue
            elseif strAttr == "来源单行号" then
                bs_row_no = strValue
            elseif strAttr == "业务类型" then
                bs_type = strValue
            elseif strAttr == "备注" then
                note = strValue   
            elseif strAttr == "库存状态" or strAttr == "质量状态" then
                stock_status = strValue
            elseif strAttr == "生产日期" then
                production_date = strValue
            elseif strAttr == "供应商代码" then
                supplier_code = strValue
            elseif strAttr == "供应商名称" then
                supplier_name = strValue
            elseif strAttr == "净重" then
                net_weight = strValue
            elseif strAttr == "原产地" then
                origin = strValue
            elseif strAttr == "备注1" then
                remark1 = strValue
            elseif strAttr == "备注2" then
                remark2 = strValue
            elseif strAttr == "备注3" then
                remark3 = strValue
            elseif strAttr == "备注4" then
                remark4 = strValue
            elseif strAttr == "备注5" then
                remark5 = strValue
            end
        end
 
        -- 基础校验
        if item_code == '' then
            table.insert(err, "第"..row.."行:货品编码不能为空")
        elseif not tonumber(qty) or tonumber(qty) <= 0 then
            table.insert(err, "第"..row.."行:数量必须大于0")
        elseif bs_no == '' then
            table.insert(err, "第"..row.."行:来源单号不能为空")
        elseif s_bs_no == '' then
            table.insert(err, "第"..row.."行:物料来源单号不能为空")
        elseif bs_row_no == '' then
            table.insert(err, "第"..row.."行:来源单行号不能为空")
        else
            -- 查询物料获取库区信息
            local cond = "S_ITEM_CODE = '" .. item_code .. "'"
            local ret1, id, materialAttrs = mobox.getDataObjAttrByKeyAttr(strLuaDEID, "SKU", cond)
            lua.Debug(strLuaDEID, debug.getinfo(1), 'materialAttrs', materialAttrs)
            if ret1 ~= 0 or not materialAttrs then
                table.insert(err, "第"..row.."行:获取物料失败 - "..item_code)
            else
                local ret2, materialJson = mobox.objAttrsToLuaJson("SKU", materialAttrs)
                if ret2 ~= 0 then
                    table.insert(err, "第"..row.."行:物料JSON转换失败 - "..item_code)
                else
                    local ok, material = pcall(json.decode, materialJson)
                    if not ok or not material or not material.udf01 then
                        table.insert(err, "第"..row.."行:物料无有效area_code - "..item_code)
                    else
                        -- 添加到对应库区的分组
                        local area_code = material.udf01
                        if not area_groups[area_code] then
                            area_groups[area_code] = {
                                wh_code = wh_code,
                                bs_type = bs_type,
                                note = note,
                                items = {}
                            }
                        end
                        
                        -- 保存物料明细及扩展字段
                        table.insert(area_groups[area_code].items, {
                            wh_code = wh_code,
                            area_code = area_code,
                            item_code = item_code,
                            item_name = material.item_name,
                            qty = qty,
                            bs_no = bs_no,
                            s_bs_no = s_bs_no,
                            bs_row_no = bs_row_no,
                            stock_status = stock_status,
                            production_date = production_date,
                            supplier_code = supplier_code,
                            supplier_name = supplier_name,
                            net_weight = net_weight,
                            origin = origin,
                            remark1 = remark1,
                            remark2 = remark2,
                            remark3 = remark3,
                            remark4 = remark4,
                            remark5 = remark5,
                            material = material  -- 保存物料完整信息
                        })
                    end
                end
            end
        end
    end
    -- 如果有错误,直接返回
    if #err > 0 then
        mobox.setInfo(strLuaDEID, "导入失败:\n"..table.concat(err, "\n"))
        return
    end
    local nRet,wh_code = wms_base.Get_sConst2( strLuaDEID, "默认工厂标识" )
    if ( nRet ~= 0 ) then
        lua.Stop(strLuaDEID, "系统常量:默认工厂标识不存在:")
        return
    end
    if ( wh_code == '' ) then
        lua.Stop(strLuaDEID, "系统常量'默认工厂标识'必须有值!:")
        return
    end
    -- 按库区创建入库单
    local result_codes = {}
    for area_code, group in pairs(area_groups) do
        -- 生成入库单号
        local header = 'RK' .. os.date("%y%m%d") .. '-'
        local ret, order_no = mobox.getSerialNumber("ERP入库单", header, 4)
        if ret ~= 0 then
            mobox.setInfo(strLuaDEID, "申请入库单编码失败:" .. order_no)
            return
        end
 
        -- 创建入库单主表(包含扩展字段)
        local inbound_Order = m3.AllocObject(strLuaDEID, "ERP_Inbound_Order")
        inbound_Order.no = order_no
        inbound_Order.wh_code = wh_code
        inbound_Order.area_code = area_code
        inbound_Order.op_type = group.bs_type 
        inbound_Order.bs_no = group.items[1].bs_no 
        inbound_Order.note = group.note 
        
        -- 创建入库单明细(包含扩展字段)
        for _, item in ipairs(group.items) do
            local inbound_Detail = m3.AllocObject(strLuaDEID, "ERP_Inbound_Detail")
            inbound_Detail.io_no = order_no
            inbound_Detail.item_code = item.item_code
            inbound_Detail.item_name = item.item_name
            inbound_Detail.qty = item.qty
            inbound_Detail.bs_no = item.s_bs_no
            inbound_Detail.bs_row_no = item.bs_row_no
            
            -- 扩展字段
            inbound_Detail.item_state = item.stock_status or ""
            inbound_Detail.prd_date = item.production_date or ""
            inbound_Detail.supplier = item.supplier_code or ""
            inbound_Detail.supplier_name = item.supplier_name or ""
            inbound_Detail.weight = item.net_weight or 0
            --inbound_Detail.owner = item.origin 
            inbound_Detail.ext_attr1 = item.origin 
            inbound_Detail.ext_attr2 = item.remark2 
            inbound_Detail.ext_attr3 = item.remark3
            inbound_Detail.ext_attr4 = item.remark4 
            inbound_Detail.ext_attr5 = item.remark5 
            
            
            local nRet, inbound_Detail = m3.CreateDataObj(strLuaDEID, inbound_Detail)
            if (nRet ~= 0) then
                lua.Stop(strLuaDEID, "创建入库单明细失败:"..inbound_Detail)
                return
            end
        end
 
        -- 保存入库单主表
        local nRet, inbound_Order = m3.CreateDataObj(strLuaDEID, inbound_Order)
        if (nRet ~= 0) then
            lua.Stop(strLuaDEID, "创建入库单失败:"..inbound_Order)
            return
        end
        
        table.insert(result_codes, order_no)
    end
 
    -- 返回成功结果
    mobox.setInfo(strLuaDEID, "入库单创建成功!\n单号:"..table.concat(result_codes, ","))
end