fy36
2025-05-14 a37aca60ff9914b0abb710f04118b22420f4f398
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
--[[
    编码: WMS-04-03
    名称: 批量导入
    作者:HAN    
    日期:2025-1-29
 
    入口函数:ImportLocation
 
    功能说明:
        处理货位导入,在导入的时候判断一下仓库、库区等是否存在?如果不存在要新创建
        导入格式见《#HHWMS-001 仓库货位定义表.xlsx》
    变更历史:
        V2.0 HAN 20250330  适合 WMS Basis 版本V15.5以上的数据模型版本
             -- 增加对巷道数据对的创建
             -- Error 用 stopProgram 替换
 
--]]
wms_base = require ("wms_base")
 
local location_type = {}        -- 货位类型
local purpose = {}              -- 用途
 
-- 获取字典项中的Name,并且转成数值返回
local function GetLocationTyeValue( strLocTypeName )
    local n, nCount
    local nValue = 1
 
    nCount = #location_type
    for n = 1, nCount do
        if ( location_type[n].Value == strLocTypeName ) then
            nValue = lua.StrToNumber( location_type[n].Name )
            if ( nValue == 0 ) then nValue = 1 end
            return nValue
        end
    end
    return nValue
end
 
-- 获取字典项中的Name,并且转成数值返回
local function GetPurposeValue( strPurpose )
    local n, nCount
    local nValue = 0
 
    nCount = #purpose
    for n = 1, nCount do
        if ( purpose[n].Value == strPurpose ) then
            return lua.StrToNumber( purpose[n].Name )
        end
    end
    return nValue
end
 
function ImportLocation(strLuaDEID)
    local nRet, strRetInfo
    -- 获取导入的数据, 返回 [{"attr":"xx","value":""},...]
    local row_data = {}
    nRet, row_data = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0 or strRetInfo == '') then 
        mobox.stopProgram( strLuaDEID, "无法获取导入数据!") 
        return
    end
 
    local row_attrs
    local n, nCount, nValue, nRows
    nRows = #row_data
    if ( nRows == 0 ) then return end
 
    -- 获取操作者的工厂标识
    local factory
    nRet, factory = m3.GetMyFactory( strLuaDEID )
    if ( nRet ~= 0 ) then 
        mobox.stopProgram( strLuaDEID, "GetMyFactory失败! "..factory ) 
        return
    end
    if ( factory == '' ) then factory = '0000' end
 
    -- 一些关键属性
    local strWHCode = ''
    local strWHName = ''
    local strAreaCode = ''
    local strAreaName = ''
    local strLocCode = ''
    local strLocName = ''
    local location = {}
 
    -- 获取 货位类型 字典,把 输入 的“常规/堆叠” 转成 整数值
    nRet, strRetInfo = mobox.getDictItemIInfo( "WMS_LocationType" )
    if ( nRet ~= 0 or strRetInfo == '') then 
        mobox.stopProgram( strLuaDEID, "系统没有定义WMS_LocationType字典!" )
        return
    end
    location_type = json.decode( strRetInfo ) 
 
    nRet, strRetInfo = mobox.getDictItemIInfo( "WMS_LocationPurpose" )
    if ( nRet ~= 0 or strRetInfo == '') then 
        mobox.stopProgram( strLuaDEID, "系统没有定义WMS_LocationPurpose字典!") 
        return
    end
    purpose = json.decode( strRetInfo )     
    
    -- 步骤1 获取从excel导入的一行数据
    for row = 1, nRows do
        row_attrs = row_data[row]
        nCount = #row_attrs
        strWHCode = ''
        strWHName = ''
        strAreaCode = ''
        strAreaName = ''
        strLocCode = ''
        strLocName = ''  
        -- 初始化 货位 对象
        location = m3.AllocObject(strLuaDEID,"Location")
        for n = 1, nCount do
            strAttr = row_attrs[n].attr
            strValue = row_attrs[n].value
            if (strAttr ~= '') then
 
                -- 根据导入的excel列头名称进行判断
                -- 关键属性判断,如果属性不存在要报错
                if (strAttr == "仓库编码") then
                    if (strValue == '') then 
                        mobox.stopProgram( strLuaDEID, strAttr .. "不能为空!")
                        return
                    end
                    strWHCode = strValue
                    location.wh_code = strWHCode
                --V1.2 
                elseif (strAttr == "仓库名称") then
                    strWHName = strValue
                elseif (strAttr == "库区编码") then
                    if (strValue == '') then 
                        mobox.stopProgram( strLuaDEID, strAttr .. "不能为空!" )
                        return
                    end
                    strAreaCode = strValue
                    location.area_code = strAreaCode
                elseif (strAttr == "库区名称") then
                    strAreaName = strValue
                elseif (strAttr == "货位编码") then
                    -- 货位编码允许为空,如果空用创建前脚本实现编码
                    strLocCode = strValue
                    location.code = strValue
                elseif (strAttr == "货位名称") then
                    strLocName = strValue
                    location.name = strValue
                 -- 常规属性
                elseif (strAttr == "巷道") then
                    location.aisle = lua.StrToNumber( strValue )
                elseif (strAttr == "排") then
                    location.row = lua.StrToNumber( strValue )
                elseif (strAttr == "列") then
                    location.col = lua.StrToNumber( strValue )
                elseif (strAttr == "层") then
                    location.layer = lua.StrToNumber( strValue )
                elseif (strAttr == "货位类型") then
                    location.loc_type = GetLocationTyeValue( strValue )
                elseif (strAttr == "用途") then
                    location.purpose = GetPurposeValue( strValue )                    
                elseif (strAttr == "容量") then
                    location.capacity = lua.StrToNumber( strValue )
                elseif (strAttr == "长度") then
                    location.length = lua.StrToNumber( strValue )
                elseif (strAttr == "宽度") then
                    location.width = lua.StrToNumber( strValue )
                elseif (strAttr == "高度") then
                    location.height = lua.StrToNumber( strValue )
                --V1.2    
                elseif (strAttr == "AGV对照编码") then
                    location.agv_site = strValue
                end
            end
        end
 
        -- 步骤2 根据货位编码来判断导入的货位是否已经存在
        --       如果已经存在,根据导入的数据进行覆盖
        --       如果不存在需要创建
        local attrs
        local strCondition = "S_CODE='" .. strLocCode .. "'"
        nRet, strRetInfo = mobox.existThisData(strLuaDEID, "Location", strCondition)
        if (nRet ~= 0) then 
            mobox.stopProgram( strLuaDEID, "在检查货位是否存在时失败! " .. strRetInfo )
            return
        end
 
        if (strRetInfo == 'yes') then
            -- 已经存在,根据导入的数据进行覆盖
            strCondition = "S_CODE='" .. strLocCode .. "'"
            strSetSQL = "S_WH_CODE = '" ..strWHCode .."' , S_AREA_CODE = '" .. strAreaCode .. "' , S_CODE = '" ..strLocCode .."' , S_NAME = '" ..
                strLocName .. "' , N_AISLE = '" ..location.aisle.. "' , N_ROW = '" ..location.row.. "' , N_COL = '" ..
                location.col.. "' , N_LAYER = '" .. location.layer .. "' , N_TYPE = '" .. location.loc_type .. "' , N_CAPACITY ='" ..
                location.capacity .. "', N_LENGTH = '" .. location.length .. "' , N_WIDTH = '" .. location.width ..
                "' , N_HEIGHT = '" .. location.height .. "' , S_AGV_SITE = '" .. location.agv_site .. "' "
            nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "Location", strCondition, strSetSQL)
            if (nRet ~= 0) then 
                mobox.stopProgram( strLuaDEID, strRetInfo )
                return
            end
            return
        end
 
        -- 导入的货位不存在需要创建
        -- 在新增货位前先判断一下仓库、库区是否存在,如果不存在需要创建
        -- 判断仓库是否存在
        strCondition = "S_CODE='" .. strWHCode .. "'"
        nRet, strRetInfo = mobox.existThisData(strLuaDEID, "Warehouse", strCondition)
        if (nRet ~= 0) then 
            mobox.stopProgram( strLuaDEID, "在检查仓库是否存在时失败! " .. strRetInfo)
            return
        end
        if (strRetInfo == 'no') then
            -- 新增仓库
            if (strWHName == '') then strWHName = strWHCode end
            local warehouse = m3.AllocObject(strLuaDEID,"Warehouse")
            warehouse.code = strWHCode
            warehouse.name = strWHName
            warehouse.factory = factory
            nRet, strRetInfo = m3.CreateDataObj( strLuaDEID, warehouse )
            if ( nRet ~= 0 ) then 
                mobox.stopProgram( strLuaDEID, 'mobox 创建【仓库】对象失败!'..strRetInfo )
                return
            end 
        end
        -- 判断库区是否存在
        strCondition = "S_WH_CODE='" .. strWHCode .. "' AND S_CODE = '" .. strAreaCode .. "'"
        nRet, strRetInfo = mobox.existThisData(strLuaDEID, "Area", strCondition)
        if (nRet ~= 0) then 
            mobox.stopProgram( strLuaDEID, "在检查库区是否存在时失败! " .. strRetInfo) 
            return
        end
        --mobox.writeSysLog("strRetInfo",strRetInfo)
        if (strRetInfo == 'no') then
            -- 新增库区
            if (strAreaName == '') then strAreaName = strAreaCode end
 
            local area = m3.AllocObject(strLuaDEID,"Area")
            area.code = strAreaCode
            area.name = strAreaName
            area.wh_code = strWHCode
            area.factory = factory
            nRet, area = m3.CreateDataObj( strLuaDEID, area )
            if ( nRet ~= 0 ) then 
                mobox.stopProgram( strLuaDEID, 'mobox 创建【库区】对象失败!'..area ) 
                return
            end
            if ( location.loc_type == 0 ) then  
                location.loc_type = area.loc_type
            end
        end
        if  (location.loc_type == 0 ) then  location.loc_type = 1 end
        -- 如果没定义货位用途默认存储
        if  (location.purpose == 0 ) then  location.purpose = 1 end
        -- 如果有巷道需要创建巷道数据对象
        if ( location.aisle > 0 ) then
            nRet, strRetInfo = wms.wms_GetAisleInfo( strWHCode, strAreaCode, location.aisle )
            if ( nRet ~= 0 ) then
                mobox.stopProgram( strLuaDEID, '获取巷道信息时失败!'..strRetInfo ) 
                return                
            end
            local aisle_obj = {}
            if ( strRetInfo ~= '' ) then
                aisle_obj =  json.decode( strRetInfo )
 
            else
                -- 巷道不存在,需要创建一个巷道数据对象
                local aisle_obj = m3.AllocObject(strLuaDEID,"Aisle")
 
                aisle_obj.wh_code = strWHCode  
                aisle_obj.area_code = strAreaCode 
                aisle_obj.aisle = locatio.aisle
                nRet, aisle_obj = m3.CreateDataObj(strLuaDEID, aisle)
                if ( nRet ~= 0 ) then
                    mobox.stopProgram( strLuaDEID, '创建巷道时失败!'..aisle_obj ) 
                    return                
                end                                
            end
            location.aisle_code = aisle.aisle_code
        end
 
        -- 创建货位
        nRet, strRetInfo = m3.CreateDataObj( strLuaDEID, location )
        if (nRet ~= 0) then 
            mobox.stopProgram( strLuaDEID, "创建货位失败! " .. strRetInfo ) 
            return
        end
    end
end