Jianw
2025-05-13 3b39fe3810c3ee2ec9ec97236c1769c5c85e062c
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
--[[
    编码: JX-24-11
    名称: 点领用按钮后
    作者:HAN  
    日期:2025-1-29
 
    级别:项目
    
    函数: ClickOkButton
 
    功能:
        -- 点击领用按钮后通过选中的出库单,统计出库货品的总体积计算出需要多少个检料框
 
    更改记录:
        V2.0 HAN 20250312  增加对选中出库的的状态和波次号检测,检查是否已经有出库波次用到这个出库单
--]]
wms_out = require( "wms_outbound" )
wms_station = require( "wms_station" )
 
-- 排序 体积大的在前面
local function sum_detail_sort( item1, item2 )
    return item1.volume > item2.volume
end
-- 计算拣料箱的数量
-- sum_detail_list 出库单货品明细 { item_code, qty, volume }
local function count_picking_box_num( strLuaDEID, sum_detail_list, box_volume )
    local n
    local picking_box_list = {
        {
            x_volume = box_volume
        }
    }
 
    if ( box_volume == nil or box_volume <= 0 ) then 
        return 1, "拣料箱的体积不合法必须大于0"
    end
 
    -- sum_detail_list 排序一下,体积大的先放
    table.sort( sum_detail_list, sum_detail_sort )    
 
    local m, find, nCount
    for n = 1, #sum_detail_list do
        if ( sum_detail_list[n].volume <= 0 ) then
            return 1, "货品编码 = '"..sum_detail_list[n].item_code.."' 的体积不合法 !"
        end
        while ( sum_detail_list[n].qty > sum_detail_list[n].alloc_qty ) do
            -- 计算一个箱子能放多少个
            find = false
            nCount = #picking_box_list
            for m = 1,  nCount do
                if ( picking_box_list[m].x_volume >= sum_detail_list[n].volume ) then
                    picking_box_list[m].x_volume = picking_box_list[m].x_volume - sum_detail_list[n].volume
                    sum_detail_list[n].alloc_qty = sum_detail_list[n].alloc_qty + 1
                    find = true
                    break
                end
            end
            if ( find == false ) then
                -- 需要申请空的拣料箱
                if ( nCount == 10 ) then return 1, "拣料箱的数量不能超过10只!" end
                local picking_box = { x_volume = box_volume }
                table.insert( picking_box_list, picking_box )
            end
        end 
    end
  
    return 0, #picking_box_list
end
 
function ClickOkButton ( strLuaDEID ) 
    local nRet, strRetInfo, n
    local data_json
 
    -- 在此操作的数据对象主体 是【出库单】数据对象, data_json 是出库单
    nRet, data_json = m3.GetSysDataJson( strLuaDEID )
    if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), data_json ) end
    -- nCount 出库单数量
    local nCount = #data_json
    if ( nCount == 0 ) then 
        mobox.setInfo( strLuaDEID, "必须选中一个出库单!" )
        return 
    end
    -- 从入库单这里获取 仓库、库区编码,如果不一样报错
    local wh_code = ''
    local area_code = ''
    local compose = {}          -- 波次组成对象
    local sum_detail_list = {}
    local bWave = false
    local bs_no, bs_type
 
    -- 获取输入界面中的工作台属性
    nRet, strRetInfo = mobox.getCurEditDataObjAttr( strLuaDEID, "S_STATION_NO" ) 
    if ( nRet ~= 0 )  then 
        lua.Error( strLuaDEID, debug.getinfo(1), "获取当前编辑属性失败! "..strRetInfo ) 
    end 
 
    local input_value = json.decode( strRetInfo ) 
    local station = lua.Get_StrAttrValue( input_value[1].value )
 
    if ( station == '' or input_value == nil ) then
        mobox.setInfo( strLuaDEID, "必须选择一个工作站!" )
        return         
    end
    local strUserLogin, strUserName
    nRet, strUserLogin, strUserName = mobox.getCurUserInfo( strLuaDEID )
    if ( nRet ~= 0 ) then return 2, "获取当前操作人员信息失败! "..strUserLogin end
 
    local box_volume = wms_base.Get_nConst( strLuaDEID, "拣料箱体积")
    if ( box_volume <= 0 ) then
        mobox.setInfo( strLuaDEID, "'拣料箱体积'参数非法!" )
        return     
    end
 
    local box_num
    local loc_code
    nRet, loc_code = wms_station.Get_Station_Loc( strLuaDEID, station )
    if ( nRet ~= 0 ) then
        lua.Stop( strLuaDEID, loc_code )
        return
    end
 
    local exit_loc
    nRet, exit_loc = wms_wh.GetLocInfo( loc_code )
    if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), '获取站台货位信息失败! '..loc_code ) end  
 
    --【step1】获取出库单明细,如果是多个出库单合并出库单
    local outbound_sum_info         -- 出库单里货品的汇总信息比如体积
    local strUpdateSql, strCondition
    local nPickingBoxNum = 0
 
    bs_type = "Outbound_Wave"
 
    local outbound_obj = {}
    local msg = ""
    -- 多个出库单需要合并为一个出库波次进行出库获取出库波次明细
    for n = 1, nCount do
        -- V2.0
        -- 获取出库单对象
        nRet, outbound_obj = m3.GetDataObject( strLuaDEID, "Outbound_Order", data_json[n].id ) 
        if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), outbound_obj ) end 
        -- 如果选择的出库单已经有出库波次号
        if ( outbound_obj.wave_no ~= '' or outbound_obj.b_state ~= 0 ) then
            msg = msg.."\r\n单号='"..outbound_obj.no.."'的出库单已经有出库波次!"
        else
            if ( wh_code == '' ) then
                wh_code = lua.Get_StrAttrValue( outbound_obj.wh_code )
            else
                if ( wh_code ~= outbound_obj.wh_code ) then
                    mobox.setInfo( strLuaDEID, "选中的出库单仓库必须是一样的!" )
                    return  
                end
            end
            if ( area_code == '' ) then
                area_code = lua.Get_StrAttrValue( outbound_obj.area_code )
            else
                if ( area_code ~= outbound_obj.area_code ) then
                    mobox.setInfo( strLuaDEID, "选中的出库单库区必须是一样的!" )
                    return  
                end
            end
            -- 获取出库单明细
            nRet, outbound_sum_info = wms_out.Sum_outbound_detail( strLuaDEID, outbound_obj.no, sum_detail_list )
            if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), "wms_out.Sum_outbound_detail 失败!"..outbound_sum_info ) end
 
            -- 计算出库单需要多少个拣料箱,并且更新出库单
            nRet, box_num = count_picking_box_num( strLuaDEID, sum_detail_list, box_volume )
            if ( nRet ~= 0 ) then
                lua.Error( strLuaDEID, debug.getinfo(1), "count_picking_box_num 失败!"..box_num )
            end
 
            local item = {
                oo_no = outbound_obj.no,
                pick_box_num = box_num
            }
            table.insert( compose, item )
 
            strUpdateSql = "N_PICKING_BOX_NUM = "..box_num
            strCondition = "S_NO = '"..outbound_obj.no.."'"
            nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Outbound_Order", strCondition, strUpdateSql )
            if ( nRet ~= 0 ) then  lua.Error( strLuaDEID, debug.getinfo(1), "更新【出库单】信息失败!"..strRetInfo ) end 
            nPickingBoxNum = nPickingBoxNum + box_num       
        end       
    end
 
    if ( wh_code == '' ) then
        mobox.setInfo( strLuaDEID, "出库单仓库不能为空!" )
        return  
    end   
    local good_type_num = #sum_detail_list
    if ( good_type_num == 0 ) then
        mobox.setInfo( strLuaDEID, "出库货品不能为空!" )
        return  
    end 
    
    -- V2.0
    -- 如果有选中的出库单不符合要求,前端显示一下
    if ( msg ~= '' ) then 
        mobox.setInfo( strLuaDEID, msg )
    end
    -- 统计出库货品的总体积,总重量,种类,件数
    local total_qty = 0   
    local total_weight = 0
    local total_volume = 0
 
    if ( box_volume <= 0 ) then
        mobox.setInfo( strLuaDEID, "常量'拣料箱体积'必须大于0!")
        return
    end
 
    for n = 1, good_type_num do 
        total_qty = total_qty + sum_detail_list[n].qty  
        total_weight = total_weight +  sum_detail_list[n].qty*sum_detail_list[n].weight
        total_volume = total_volume +  sum_detail_list[n].qty*sum_detail_list[n].volume
    end
 
    lua.Debug( strLuaDEID, debug.getinfo(1), "STEP1 -- sum_detail_list ", sum_detail_list)
 
    -- 弹出 一个HTML界面显示 拣料箱的数量
    local action = 
    {
        {
            action_type = "refresh",
            value = ""
        },
        {
            action_type = "open_form_dlg",
            value = {
                dlg_name = "拣货料箱绑定",
                cls_id = "Outbound_Order",
                input_parameter =  {
                    bs_type = bs_type,
                    bs_no = bs_no,
                    login = strUserLogin,
                    user_name = strUserName,
                    good_type_num = good_type_num,
                    total_qty = total_qty,
                    total_weight = total_weight,
                    total_volume = total_volume,
                    picking_box_num = nPickingBoxNum,
                    o_detail_list = sum_detail_list,
                    wh_code = wh_code,
                    area_code = area_code,
                    exit_loc = exit_loc,
                    wave_compose = compose,
                    station = station
                },
                data_object = {
                    id = "",
                    attrs = {
                        Station = station,
                        PickingBoxNum = nPickingBoxNum,
                        GoodTypeNum = good_type_num,
                        Qty = total_qty
                    }
                }
            }
        }  
    }
    nRet, strRetInfo = mobox.setAction( strLuaDEID, lua.table2str(action) )
    if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "setAction失败! "..strRetInfo..' action = '..strAction ) end        
 
end