lzh
2025-06-19 3a6436e0c88042c6ce8dca2fe8adb0109f0ad9e4
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
--[[
 编码: GT-40-22
 名称: 任务完成
 作者:
 入口函数:TaskFinish
 功能说明:
    -- 判断任务类型是否为立库搬运任务,是则创建二段任务,判断物料是否要放在恒温库,是则从出库口到三楼恒温库,不是则到平库
    -- 任务类型为AGV搬运任务则设置作业完成,增加仓库量表
 变更历史:
 --]]
require("WMS-BASE")
wms_op = require("wms_operation")
wms_cntr = require("wms_container")
require("GT_InAndOutboundPolicies")
wms_task = require("wms_task")
function TaskFinish(strLuaDEID)
    local nRet, strRetInfo, end_loc, area
 
    -- 获取当前作业对象
    local operation
    nRet, operation = m3.GetSysCurEditDataObj(strLuaDEID, "Operation")
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), operation) end
 
    -- 获取任务对象
    local task
    nRet, task = m3.SysInputParamToDataObj(strLuaDEID, "Task")
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), task) end
 
    -- 如果完成的任务是 立库搬运任务,创建二段任务,从出库口到粉料库
    if (task.type == wms_base.Get_nConst(strLuaDEID, "任务类型-立库入库搬运")) then
        local ext_data, success
        success, ext_data = pcall(json.decode, operation.ext_data)
        if (success == false) then
            lua.Error(strLuaDEID, debug.getinfo(1), "operation_obj.ext_data 中内容JSON格式不合法!")
        end
        lua.Debug(strLuaDEID, debug.getinfo(1), '入库作业扩展参数!', ext_data)
 
        -- 创建AGV叉车搬运任务
        local new_task           = m3.AllocObject(strLuaDEID, "Task")
        new_task.code            = ext_data.task_no
        new_task.op_code         = operation.code
        new_task.op_name         = operation.op_def_name
        new_task.factory         = operation.factory
        new_task.type            = wms_base.Get_nConst(strLuaDEID, "任务类型-AGV入库搬运")
        new_task.cntr_code       = operation.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, "调度类型-AGV")
        nRet, new_task           = m3.CreateDataObj(strLuaDEID, new_task)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "创建AGV搬运任务失败!" .. new_task) end
 
        -- 通过容器获取物料信息
        local cg_detail_list, cg_detail
        nRet, cg_detail_list = wms_cntr.Get_Container_Goods(strLuaDEID, task.cntr_code)
        nRet, cg_detail = m3.ObjAttrStrToLuaObj("CG_Detail", lua.table2str(cg_detail_list[1].attrs))
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), cg_detail) end
 
        local label_crad
        local strCondition = "S_SERIAL_NO = '" .. cg_detail.serial_no .. "'"
        if(tonumber(cg_detail.is_tl) == 1)then
            nRet, label_crad = m3.GetDataObjByCondition(strLuaDEID, "GT_ROM", strCondition, "T_CREATE DESC")
            if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1),"获取流水号信息失败!" .. label_crad) end
        else
            nRet, label_crad = m3.GetDataObjByCondition(strLuaDEID, "GT_Label_Crad", strCondition, "T_CREATE DESC")
            if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1),"获取流水号信息失败!" .. label_crad) end
        end
 
        -- 获取入库单据头信息
        local incoming_Info
        strCondition = "S_DELIVERY_NO = '" ..
        label_crad.delivery_no .. "' AND N_DELIVERY_ROW_NO = '" .. label_crad.delivery_row_no .. "'"
        nRet, incoming_Info = m3.GetDataObjByCondition(strLuaDEID, "GT_Incoming_Info", strCondition, "T_CREATE DESC")
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取入库单据头信息失败!") end
 
        local data = {
            taskData = {
                taskNum = new_task.code,
                pickStation = new_task.start_loc_code,
                dropStation = new_task.end_loc_code,
                taskType = 3,
                carrierType = 1,
                priority = 1,
                wmsTaskNo = "", -- 出库才给
                grade = "", -- 入库等级是默认的
                produceTime = incoming_Info.product_date,
                tyreType = "",
                supplier = incoming_Info.vendor,
                receiveLot = cg_detail.batch_no,
                subpool = ""
            },
            partData = {
                rfid = task.cntr_code,
                lotNumber = nil,
                partNumber = cg_detail.item_code,
                partDesc = cg_detail.item_name,
                partType = nil,
                weight = cg_detail.qty,
                unit = cg_detail.wu,
                maturityTime = nil,
                productionTime = nil,
                stewingTime = nil,
                overdueTime = nil
            }
        }
        -- 调用AGV任务下发
        local url = wms_base.Get_sConst(strLuaDEID, "AGV-url")
        local strurl = url .. "/CreateTask"
        local strHeader = ""
        local strBody = data
        lua.Debug(strLuaDEID, debug.getinfo(1), 'strurl:', strurl)
        lua.Debug(strLuaDEID, debug.getinfo(1), 'strBody:', strBody)
        nRet, strRetInfo = CreateInterfaceExc(strLuaDEID, strurl, strHeader, strBody, "AGV", "任务创建")
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), "调用WCS接口失败!" .. strRetInfo)
        end
 
        -- 设置状态未推送
        wms_task.SetStateByCode(strLuaDEID, new_task.code, "任务状态-已推送")
    else
        -- 如果完成的是AGV搬运任务,设置作业完成,增加仓库量表
        nRet, strRetInfo = wms_op.SetFinish(strLuaDEID, operation.code)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), "设置作业编号='" .. operation.code .. "' 的作业完成失败!" .. strRetInfo)
        end
        -- 获取容器货品明细
        local cg_detail
        local strCondition = "S_CNTR_CODE = '" .. operation.cntr_code .. "'"
        nRet, cg_detail = m3.GetDataObjByCondition(strLuaDEID, "CG_Detail", strCondition)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "m3.GetDataObjByCondition 失败!" .. cg_detail) end
 
        if (tonumber(cg_detail.is_fj) == 0) then
            -- 加库区/库量表 (通过CG_Detail)
            wms_base.Add_WHAreaQty_ByCGDetail(strLuaDEID, task.end_wh_code, task.end_area_code, operation.cntr_code)
 
            -- 获取入库单据行信息
            local label_crad
            strCondition = "S_SERIAL_NO = '" .. cg_detail.serial_no .. "'"
            nRet, label_crad = m3.GetDataObjByCondition(strLuaDEID, "GT_Label_Crad", strCondition, "T_CREATE DESC")
            if (nRet == 1) then
                nRet, label_crad = m3.GetDataObjByCondition(strLuaDEID, "GT_ROM", strCondition, "T_CREATE DESC")
                if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取流水号信息失败!" .. label_crad) end
            elseif (nRet > 1) then
                lua.Error(strLuaDEID, debug.getinfo(1), "获取入库单据行信息失败!" .. label_crad)
            end
 
            -- 获取入库单据头信息
            local incoming_Info
            strCondition = "S_DELIVERY_NO = '" ..label_crad.delivery_no.. "' AND N_DELIVERY_ROW_NO = '" .. label_crad.delivery_row_no .. "'"
            nRet, incoming_Info = m3.GetDataObjByCondition(strLuaDEID, "GT_Incoming_Info", strCondition, "T_CREATE DESC")
            if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取入库单据头信息失败!") end
 
            -- 如果入库单是手工录入的则
            if (incoming_Info.type == '手工录入') then
                -- 创建【GTWMS同步记录】
                local sys_record = m3.AllocObject(strLuaDEID, "GTWMS_SYNC_RECORD")
                sys_record.delivery_no = incoming_Info.delivery_no
                sys_record.delivery_row_no = incoming_Info.delivery_row_no
                sys_record.lpn = cg_detail.serial_no
                sys_record.rfid = operation.cntr_code
                sys_record.lpnweight = cg_detail.qty
                sys_record.sku = cg_detail.item_code
                sys_record.batch_no = cg_detail.batch_no
                sys_record.remark3 = cg_detail.serial_no
                sys_record.type = "入库回传"
                lua.Debug(strLuaDEID, debug.getinfo(1), 'sys_record', sys_record)
                nRet, sys_record = m3.CreateDataObj(strLuaDEID, sys_record)
                if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), 'mobox 创建【GTWMS同步记录】对象失败!' .. sys_record) end
            else
                -- 入库结果回传
                local data
                local source = "GTWMS入库回传"
                nRet, data = StorageResult(strLuaDEID, operation.code, source)
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "入库结果回传失败!" .. data)
                end
                data.loc = "LK001"
                lua.Debug(strLuaDEID, debug.getinfo(1), 'data', data)
                -- 调用GT-WMS的回传接口
                local url = wms_base.Get_sConst(strLuaDEID, "GTWMS-url")
                local strurl = url
                local strHeader = ""
                local strBody = {
                    application = "GITI",
                    code = "WCS_ASN_WMS",
                    data = data
                }
 
                nRet, strRetInfo = CreateInterfaceExc(strLuaDEID, strurl, strHeader, strBody, "GTWMS", source)
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "调用接口失败!" .. strRetInfo)
                end
            end
        else
            -- 修改容器货品明细的标识为复检出库
            local condition = "S_CNTR_CODE = '" .. operation.cntr_code .. "'"
            local strSetSQL_update = "N_IS_FJ = 0"
            nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "CG_Detail", condition, strSetSQL_update)
            if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "修改容器货品明细失败!" .. strRetInfo) end
        end
 
        -- 如果是退料单则修改退料单状态为完成
        if (tonumber(cg_detail.is_tl) == 1) then
            local condition = "S_CNTR_CODE = '" .. operation.cntr_code .. "'"
            local strSetAttr = "S_STATE = '完成'"
            nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_ROM", condition, strSetAttr)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "设置状态失败!" .. strRetInfo)
            end
        end
    end
end