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
--[[
    编码: JX-116-10
    名称: 巨沃料箱初始化-导入
    作者:HAN    
    日期:2025-1-29    
    入口函数: Import
 
    功能说明:
 
    变更历史:
 
--]]
wms_base = require ("wms_base")
wms_cntr= require( "wms_container" )
 
function Import(strLuaDEID)
    local nRet, strRetInfo, strNo
 
    -- 获取导入的数据, 返回 [{"attr":"xx","value":""},...]
    local row_data = {}
    nRet, row_data = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0 or strRetInfo == '') then lua.Error( strLuaDEID, debug.getinfo(1), "无法获取导入数据!") end
 
    local row_attrs
    local n, nCount, nRows, nIndex
    local cntr_type, max_cell_num
 
    -- 导入是可分多页分批导入, 因此每次的起始行不一样
    nRet, strRetInfo = mobox.getGlobalAttrValue( strLuaDEID, "start_row" )
    if ( nRet ~= 0 ) then
        mobox.error( strLuaDEID, "获取批量导入全局参数 start_row 失败!"..strRetInfo )
        return
    end    
    local nStartRow = tonumber( strRetInfo )
    local err_msg_list = {}                          -- 用于写导入错误信息
    local str_err_msg
    local jw_cntr, cntr
    local strCondition, strUpdateSql
    local item_code, item_name, cell_no
    local qty
    local material
    local cg_detail
 
    -- 步骤1 获取从excel导入的一行数据
    nRow = nStartRow
    for row = 1, #row_data do
        row_attrs = row_data[row]
 
        -- 初始化空料箱对象
        item_code = ''
        item_name = ''
        cell_no = ''
        qty = 0
        jw_cntr = m3.AllocObject(strLuaDEID,"JW_CNTR_Goods_INIT")
        str_err_msg = ''
        for n = 1, #row_attrs do
            strAttr = row_attrs[n].attr
            strValue = row_attrs[n].value
            if (strAttr ~= '') then
                if (strAttr == "料箱编码") then
                    if (strValue == '') then 
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    jw_cntr.cntr_code = strValue
                elseif (strAttr == "料箱类型") then
                    if (strValue == '') then 
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end                  
                    jw_cntr.spec = strValue
                elseif (strAttr == "料格") then
                    cell_no = strValue
                elseif (strAttr == "料号") then
                    item_code = strValue                   
                elseif (strAttr == "品名") then
                    item_name = strValue
                elseif (strAttr == "数量") then
                    qty = lua.StrToNumber( strValue )  
                end
            end
        end
 
        -- 首先判断一下容器是否存在,容器已经存在不允许导入,可以考虑先在WMS中删除
        nRet, cntr = wms_cntr.GetInfo( strLuaDEID, jw_cntr.cntr_code )
        if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "获取容器信息失败!"..cntr ) end
 
        if ( cntr == '' ) then
            -- 没有容器需要创建
            nRet, jw_cntr = m3.CreateDataObj(strLuaDEID, jw_cntr)
            if (nRet ~= 0 ) then 
                str_err_msg = "创建巨沃初始化料箱货品对象失败!"..jw_cntr 
                goto err_msg_process
            end  
 
            cntr = m3.AllocObject(strLuaDEID,"Container")
            cntr.code = jw_cntr.cntr_code
            cntr.type = 3           -- 料箱
            cntr.spec = jw_cntr.spec
            cntr.source = "巨沃"
            nRet, cntr = m3.CreateDataObj(strLuaDEID, cntr)
            if (nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "创建料箱对象失败!"..cntr ) end                  
        else
            -- 容器已经存在需要判断容器是否和货位有绑定
            if ( cntr.position ~= '' ) then
                str_err_msg = "料箱'"..jw_cntr.cntr_code.."'已经在储藏区,不能导入!"
                goto err_msg_process
            end
        end
        -- 判断是否要导入 CG_Detail
        if ( item_code ~= '' and cell_no ~= '') then
            nRet, material = m3.GetDataObjectByKey(strLuaDEID, "SKU", "S_ITEM_CODE", item_code )
            if (nRet ~= 0 ) then 
                if ( nRet == 1 ) then
                    str_err_msg = material
                    goto err_msg_process
                end
                lua.Error( strLuaDEID, debug.getinfo(1), "获取货品信息失败!"..material )    
            end
 
            -- 判断一下CG_Detail 里相同的货品不能有(防止重复导入造成数据错误)
            strCondition = "S_CNTR_CODE = '"..jw_cntr.cntr_code.."' AND S_CELL_NO = '"..cell_no.."' AND S_ITEM_CODE = '"..item_code.."'"
            nRet, strRetInfo = mobox.existThisData( strLuaDEID, "CG_Detail", strCondition )
            if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), strRetInfo )  end
            if ( strRetInfo ~= "yes" ) then 
                cg_detail = m3.AllocObject(strLuaDEID,"CG_Detail")
                cg_detail.cntr_code = jw_cntr.cntr_code
                cg_detail.cell_no = cell_no
                cg_detail.item_code = item_code
                cg_detail.item_name = item_name
                cg_detail.weight = material.weight
                cg_detail.volume = material.volume
                cg_detail.qty = qty
                cg_detail.uom = "个"
                nRet, cg_detail = m3.CreateDataObj( strLuaDEID, cg_detail )
                if ( nRet ~= 0 ) then 
                    lua.Error( strLuaDEID, debug.getinfo(1), '创建拣料箱CG_Detail时失败!'..cg_detail ) 
                end   
            end           
        end
 
        ::err_msg_process::
        if ( str_err_msg ~= '' ) then
            local err_msg_row = {
                row_no = nRow,
                err_msg = str_err_msg
            }
            table.insert( err_msg_list, err_msg_row )            
        end
        nRow = nRow + 1
    end
    -- 把导入过程中的错误信息返回到前端
    local import_fail = {
        result = {
            fail = err_msg_list
        }
    }
    mobox.returnValue( strLuaDEID,1, lua.table2str(import_fail) )
    lua.Debug( strLuaDEID, debug.getinfo(1), "import_fail", import_fail )    
end