wsz
2025-06-20 19898bd66dec87b500b200d5d50961d0fb538ce5
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
--[[
    编码: WMS-40-21#2
    名称: 作业-入库-任务完成
    作者:HAN  
    日期:2025-1-29
 
    版本: V1.0
 
    场景:作业中的任务完成后触发这个脚本
          当前数据对象指针是 作业
          任务对象属性保存在 输入参数 InputParamter 
          需要判断一下这个任务完成是否可以关闭 作业
 
          料箱入库 和 巨星入库都不会影响存储量因此可以共用这段代码
       
    函数: TaskFinish
 
    功能:
        -- 根据任务类型判断是否结束作业
        -- 如果是任务类型 = 输送线搬运 任务完成后要把作业设置为 "等待/6" 状态等WCS发出堆垛机请求后再生成堆垛机任务
 
    更改记录:
        V2.0 HAN 20250218
             -- 原来的“码盘入库”改为“初始入库”,新增的“码盘入库”用于真正的码盘操作
 
--]]
wms_op = require( "wms_operation" )
 
function TaskFinish ( strLuaDEID ) 
    local nRet, strRetInfo
    local strErr = ''
 
    -- 获取 触发【作业】任务完成事件的场景参数
    -- step1  获取任务信息
    local task
    local input_paramters
    nRet, input_paramters = mobox.getInputParameter2(strLuaDEID)
    if (nRet ~= 0) then 
        lua.Stop( strLuaDEID, "getInputParameter2 失败!" ) 
        return
    end
    
    -- 把 [{"attr":"xxx","value":""},...] 转换成 task json object
    local strObjJson
    nRet, strObjJson = mobox.objAttrsToLuaJson( "Task", input_paramters )
    if ( nRet ~= 0  ) then 
        lua.Stop( strLuaDEID, "objAttrsToLuaJson Task 失败!"..strObjJson ) 
        return
    end
    local success
    success, task = pcall( json.decode, strObjJson )
    if ( success == false ) then 
        lua.Stop( strLuaDEID, "objAttrsToLuaJson (task) 返回的的JSON格式不合法 !"..task ) 
        return
    end
    if ( task.start_wh_code == '') then 
        lua.Stop( strLuaDEID, "作业中起点仓库为空!")
        return
    end
 
    local strUpdateSql, strCondition
    local operation
 
    -- 料箱入库 不会产生库存量的变化(分拣回)
    nRet, operation = m3.GetSysCurEditDataObj( strLuaDEID, "Operation" )
    if (nRet ~= 0 ) then 
        lua.Stop( strLuaDEID, operation )
        return
    end 
    -- 判断任务类型
    if ( task.type == wms_base.Get_nConst(strLuaDEID, "任务类型-输送线入库搬运") ) then
        -- 创建堆垛机入库任务
        local new_task = m3.AllocObject(strLuaDEID,"Task")
 
        new_task.op_code = operation.code                            -- 作业编码
        new_task.source_sys = operation.source_sys                   -- 来源系统
        new_task.op_name = operation.op_def_name
        new_task.factory = operation.factory                         -- 工厂
        new_task.bs_type = operation.bs_type                        
        new_task.bs_no = operation.bs_no  
        new_task.type = wms_base.Get_nConst(strLuaDEID, "任务类型-堆垛机入库搬运") 
        new_task.cntr_code = task.cntr_code
        -- 起点
        new_task.start_wh_code = task.end_wh_code
        new_task.start_area_code  = task.end_area_code
        new_task.start_loc_code  = task.end_loc_code
        -- 终点
        new_task.end_wh_code  = operation.end_wh_code
        new_task.end_area_code  = operation.end_area_code
        new_task.end_loc_code  = operation.end_loc_code
        new_task.schedule_type = wms_base.Get_nConst(strLuaDEID, "调度类型-堆垛机") -- 设置调度类型
 
        nRet, new_task = m3.CreateDataObj(strLuaDEID, new_task)
        if (nRet ~= 0 ) then 
            lua.Stop( strLuaDEID, "创建堆垛机任务失败!"..new_task)
            return
        end
 
        if ( nRet ~= 0 ) then 
            mobox.Stop( strLuaDEID, strRetInfo ) 
            return
        end 
    elseif ( task.type == wms_base.Get_nConst(strLuaDEID, "任务类型-堆垛机入库搬运") ) then
 
        nRet, strRetInfo = wms_op.SetFinish( strLuaDEID, operation )
        if ( nRet ~= 0 ) then
            lua.Stop( strLuaDEID, "设置作业编号='"..operation.code.."' 的作业完成失败!"..strRetInfo )
            return
        end   
 
        if ( operation.op_def_name == '料箱入库') then
            -- 拣货后回库
            local carry_cb_no = lua.Get_StrAttrValue( operation.carry_cb_no )
            if ( operation.carry_cb_cls == "Distribution_CNTR" ) then
                -- 如果作业的配盘后料箱回库,需要设置配盘对象的状态 = 6
                if ( carry_cb_no ~= '' ) then
                    strUpdateSql = "N_B_STATE = 6"
                    strCondition = "S_DC_NO = '"..carry_cb_no.."'"
                    nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Distribution_CNTR", strCondition, strUpdateSql )
                    if ( nRet ~= 0 ) then  
                        lua.Stop( strLuaDEID, "更新【配盘】信息失败!"..strRetInfo )
                        return
                    end              
                end
            end
           
        elseif ( operation.op_def_name == '空箱入库') then
            -- 立库空料箱初始化入库
            strCondition = "S_CNTR_CODE = '"..operation.cntr_code.."'"
            nRet, strRetInfo = mobox.deleteDataObject( strLuaDEID, "JX_ASRS_EmptyBox_In", strCondition )
            if ( nRet ~= 0) then return 
                lua.Stop( strLuaDEID, "删除【JX_ASRS_EmptyBox_In】失败!  "..strRetInfo ) 
                return
            end 
 
        elseif ( operation.op_def_name == '货品入库') then
            local carry_cb_no = lua.Get_StrAttrValue( operation.carry_cb_no )
 
            if ( operation.carry_cb_cls == "Pre_Alloc_Container" ) then
                -- 【预分配容器】状态设置=5 (完成)
                if ( carry_cb_no ~= '' ) then
                    strUpdateSql = "N_B_STATE = 5"
                    strCondition = "S_PAC_NO = '"..carry_cb_no.."'"
                    nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Pre_Alloc_Container", strCondition, strUpdateSql )
                    if ( nRet ~= 0 ) then  
                        lua.Stop( strLuaDEID, "更新【预分配容器】信息失败!"..strRetInfo ) 
                        return
                    end 
                    local org_cntr 
                    nRet, org_cntr = m3.GetDataObjByCondition( strLuaDEID, "Pre_Alloc_Container", strCondition )
                    if ( nRet ~= 0 ) then  
                        lua.Stop( strLuaDEID, "获取【预分配容器】信息失败!"..strRetInfo ) 
                        return
                    end              
                    -- 增加一个后台进程对组盘进行处理,触发配盘明细中的入库波次是否可以完成
                    local add_wfp = {
                        wfp_type = 1,
                        cls = "Pre_Alloc_Container",
                        obj_id = org_cntr.id,
                        obj_name = "组盘'"..operation.pac_no.."'-->入库后处理",
                        trigger_event = "入库后处理"
                    }
                    nRet, strRetInfo = m3.AddSysWFP( strLuaDEID, add_wfp )
                    if ( nRet ~= 0 ) then 
                        lua.Stop( strLuaDEID, "AddSysWFP失败!"..strRetInfo ) 
                        return 
                    end              
                end
                -- 加仓库和库区的存储量,注意这里要从预分配容器明细表获取 新增货品信息
                nRet, strRetInfo = wms_base.Add_WHAreaQty_By_PAC_Detail( strLuaDEID, operation.end_wh_code, operation.end_area_code, operation.end_loc_code, operation.pac_no )
                if ( nRet ~= 0 ) then 
                    lua.Stop( strLuaDEID, "Add_WHAreaQty_By_PAC_Detail 失败!  "..strRetInfo )
                    return
                end
            end
 
        elseif ( operation.op_def_name == '初始入库') then
            -- 巨沃料箱初始化入库,需要根据CG_Detail更新库存量
            nRet, strRetInfo = wms_base.Add_WHAreaQty_ByCGDetail( strLuaDEID, operation.end_wh_code, operation.end_area_code, operation.cntr_code, operation.end_loc_code )
            if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), strRetInfo ) end
 
            -- 删除【JW_CNTR_Goods_INIT】记录
            strCondition = "S_CNTR_CODE = '"..operation.cntr_code.."'"
            nRet, strRetInfo = mobox.deleteDataObject( strLuaDEID, "JW_CNTR_Goods_INIT", strCondition )
            if ( nRet ~= 0) then return lua.Error( strLuaDEID, debug.getinfo(1), "删除【JW_CNTR_Goods_INIT】失败!  "..strRetInfo ) end 
 
        elseif ( operation.op_def_name == '码盘入库') then
            -- 巨沃这边加了一个PDA码盘入库
            nRet, strRetInfo = wms_base.Add_WHAreaQty_ByCGDetail( strLuaDEID, operation.end_wh_code, operation.end_area_code, operation.cntr_code, operation.end_loc_code )
            if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), strRetInfo ) end
            
            -- 设置巨星任务状态 = 2 说明任务已经完成,并且更新
            strUpdateSql = "N_B_STATE = 2, S_END_LOC = '"..task.end_loc_code.."', S_END_AREA = '"..task.end_area_code.."'"
            strCondition = "S_SOURNO = '"..operation.bs_no.."'"
            nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "JX_Task", strCondition, strUpdateSql )
            if ( nRet ~= 0 ) then  
                lua.Error( strLuaDEID, debug.getinfo(1), "更新【巨星任务】信息失败!"..strRetInfo ) 
            end    
            -- 触发巨星任务报完工   
            local jx_task 
            nRet, jx_task = m3.GetDataObjByCondition( strLuaDEID, "JX_Task", strCondition )
            if ( nRet ~= 0 ) then  lua.Error( strLuaDEID, debug.getinfo(1), "获取【巨星任务】信息失败!"..jx_task ) end  
            
            lua.Debug(strLuaDEID, debug.getinfo(1), "jx_task-->", jx_task)
            -- 增加一个后台进程进行入库单完工回报
            local add_wfp = {
                            wfp_type = 1,                  -- 触发数据对象事件(指定数据对象标识)
                            cls = "JX_Task",
                            obj_id = jx_task.id,
                            obj_name = "任务号'"..operation.bs_no.."'巨星任务-->巨沃码盘入库完工回报",
                            trigger_event = "巨沃码盘入库完工回报"
            }
            nRet, strRetInfo = m3.AddSysWFP( strLuaDEID, add_wfp )
            if ( nRet ~= 0 ) then 
                lua.Error( strLuaDEID, debug.getinfo(1), "AddSysWFP失败!"..strRetInfo )  
            end 
            
            
        elseif ( operation.op_def_name == '盘点回库') then  
            if ( operation.cc_no ~= '' and operation.cc_no ~= '') then  
                -- 4 已回库
                strUpdateSql = "N_B_STATE = 4"
                strCondition = "S_CC_NO = '"..operation.cc_no.."'"
                nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "CP_Count_Container", strCondition, strUpdateSql )
                if ( nRet ~= 0 ) then  lua.Error( strLuaDEID, debug.getinfo(1), "更新【计划盘点容器】信息失败!"..strRetInfo ) end 
                
                local cc_cntr 
                nRet, cc_cntr = m3.GetDataObjByCondition( strLuaDEID, "CP_Count_Container", strCondition )
                if ( nRet ~= 0 ) then  lua.Error( strLuaDEID, debug.getinfo(1), "获取【计划盘点容器】信息失败!"..strRetInfo ) end   
                if ( cc_cntr.have_diff == "Y" ) then
                    --  容器设置禁用标签
                    strUpdateSql = "C_ENABLE = 'N'"
                    strCondition = "S_CODE = '"..cc_cntr.cntr_code.."'"
                    nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Container", strCondition, strUpdateSql )
                    if ( nRet ~= 0 ) then  lua.Error( strLuaDEID, debug.getinfo(1), "更新【容器】信息失败!"..strRetInfo ) end                     
                end                               
            end   
        elseif ( operation.op_def_name == '指定回库') then    
            -- 更新【指定出库】对象
            if ( operation.bs_no ~= '' and operation.bs_type == "Specify_Outbound") then
                -- 3 表示指定出库容器 已经回库
                strUpdateSql = "N_B_STATE = 4"
                strCondition = "S_SO_NO = '"..operation.bs_no.."' AND S_CNTR_CODE = '"..operation.cntr_code.."'"
                nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Specify_Outbound_CNTR", strCondition, strUpdateSql )
                if ( nRet ~= 0 ) then  
                    lua.Error( strLuaDEID, debug.getinfo(1), "更新【配盘明细】信息失败!"..strRetInfo ) 
                end     
            end 
        end
    end
end