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
--[[
   编码: GT-119-19
   名称: 创建作业
   作者:LZH
   日期:2024/8/13
 
   函数: CreateOperation
   功能:
   step1 判断出库单据是否是启动状态
   step2 启动状态则通过出库策略获取作业起点
   step3 创建作业
 
    变更记录:
    V1.1 LZH 20241122 出库单修改为主子表,代码调整
    V1.2 LZH 20250326 新增采购退货流程,如果GTWMS下发的终点机台为固定的
 
--]]
 
json     = require("json")
mobox    = require("OILua_JavelinExt")
m3       = require("oi_base_mobox")
wms_base = require("wms_base")
require("GT-Base")
require("GT_InAndOutboundPolicies")
wms_wh = require("wms_wh")
function CreateOperation(strLuaDEID)
    local nRet, strRetInfo
 
    local stock_out
    nRet, stock_out = m3.GetSysCurEditDataObj(strLuaDEID, "GT_Stock_Out")
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), stock_out) end
    lua.Debug(strLuaDEID, debug.getinfo(1), 'stock_out', stock_out)
 
    local ret_loc
    if (stock_out.state == '启用') then
        -- 获取该物料的物料类型
        local item_type, material
        nRet, item_type, material = GT_Get_ItemType(strLuaDEID, stock_out.item_code)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), item_type) end
 
        -- 获取 物料+批号 所在库区
        local strCondition = "S_CNTR_CODE IN (SELECT S_CNTR_CODE FROM TN_CG_Detail WHERE S_ITEM_CODE = '" ..
            stock_out.item_code .. "' AND S_BATCH_NO = '" .. stock_out.batch_no .. "')"
        local loc_container
        nRet, loc_container = m3.GetDataObjByCondition(strLuaDEID, "Loc_Container", strCondition)
        if (nRet == 1) then
            lua.Error(strLuaDEID, debug.getinfo(1), "不存在批次号为" .. stock_out.batch_no .. "的物料!")
        elseif (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), "m3.GetDataObjByCondition 失败!" .. loc_container)
        end
        -- 获取货位信息
        local loc_info
        nRet, loc_info = wms_wh.Location_GetInfo(strLuaDEID, loc_container.loc_code)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "WMS_Location_GetInfo失败!" .. loc_info) end
 
        local op_def_name = ''
 
 
 
        local end_loc_code, loc_baseinfo
        if (stock_out.ms_code == 'ABC') then
            op_def_name = "采购退货"
            -- 获取机台下维护的出库库区
            local area_link
            strCondition = "S_MS_CODE = 'ABC'"
            nRet, area_link = m3.QueryDataObject(strLuaDEID, "MS_Area_Link", strCondition)
            if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), '获取采购退货机台信息失败!' .. area_link) end
            if (area_link == nil or area_link == '') then lua.Error(strLuaDEID, debug.getinfo(1), '请维护采购退货出库库区或位置!') end
            lua.Debug(strLuaDEID, debug.getinfo(1), 'area_link', area_link)
 
            -- 获取随机数,随机指定出库库区位置
            local randomInt = math.random(1, #area_link)
            local attr
            nRet, attr = m3.ObjAttrStrToLuaObj("MS_Area_Link", lua.table2str(area_link[randomInt].attrs))
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1),
                    "m3.ObjAttrStrToLuaObj(GT_Label_Crad) 失败! " .. attr)
            end
            lua.Debug(strLuaDEID, debug.getinfo(1), 'attr', attr)
 
            if (tonumber(attr.la_type) == 0) then
                end_loc_code = attr.la_code .. "-QY"
            elseif (tonumber(attr.la_type) == 1) then
                end_loc_code = attr.la_code
            end
        else
            if (loc_info.area_code == 'PFL' or loc_info.area_code == 'TFL' or loc_info.area_code == 'HWPFL' or loc_info.area_code == 'HWTFL') then
                nRet, ret_loc = FL_StockOut(strLuaDEID, stock_out)
                if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), ret_loc) end
                op_def_name = "粉料出库"
                end_loc_code = stock_out.ms_code
            elseif (loc_info.area_code == 'LK') then
                nRet, ret_loc = LK_StockOut(strLuaDEID, stock_out)
                if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), ret_loc) end
                op_def_name = "立库出库"
                nRet, end_loc_code = wms_base.GetAreaAvaliableLoc(strLuaDEID, stock_out.ms_code, "")
                if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "WMS_GetAreaAvaliableLoc失败!" .. end_loc_code) end
                end_loc_code = end_loc_code.loc_code
            end
 
            if (ret_loc == nil or ret_loc == '') then
                lua.Error(strLuaDEID, debug.getinfo(1), "没有可出库的货品!")
            end
            lua.Debug(strLuaDEID, debug.getinfo(1), 'ret_loc', ret_loc)
            nRet, loc_baseinfo = wms.wms_GetLocBaseInfo(ret_loc.start_loc_code)
            if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取货位基本信息失败!" .. loc_baseinfo) end
            lua.Debug(strLuaDEID, debug.getinfo(1), '起点货位信息', loc_baseinfo)
            loc_baseinfo = json.decode(loc_baseinfo)
        end
 
        lua.Debug(strLuaDEID, debug.getinfo(1), 'end_loc_code', end_loc_code)
        local end_loc
        nRet, end_loc = wms.wms_GetLocBaseInfo(end_loc_code)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取货位基本信息失败!" .. end_loc) end
        lua.Debug(strLuaDEID, debug.getinfo(1), '终点货位信息', end_loc)
        end_loc                   = json.decode(end_loc)
 
        -- step4: 创建搬运作业
        local operation           = m3.AllocObject(strLuaDEID, "Operation")
        operation.start_wh_code   = loc_baseinfo.wh_code
        operation.start_area_code = loc_baseinfo.area_code
        operation.start_loc_code  = loc_baseinfo.code
 
        -- 终点是佳通WMS传过来的
        operation.end_wh_code     = end_loc.wh_code
        operation.end_area_code   = end_loc.area_code
        operation.end_loc_code    = end_loc.code
 
        operation.bs_no           = stock_out.delivery_no -- 业务单号为出库任务号
 
        operation.cntr_code       = ret_loc.cntr_code
        operation.op_def_name     = op_def_name
        operation.op_type         = wms_base.Get_nConst(strLuaDEID, "作业类型-出库")
 
        local strCode
        local strHeader           = 'TA' .. os.date("%y%m%d") .. '-'
        nRet, strCode             = mobox.getSerialNumber("任务", strHeader, 5)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), '申请【任务】编码失败!' .. strCode) end
 
        -- 设置扩展参数带入作业启动,任务完成
        local ext_table       = {}
        ext_table.batch_no    = stock_out.batch_no
        ext_table.item_type   = item_type
        ext_table.delivery_no = stock_out.delivery_no
        ext_table.task_no     = strCode
        operation.ext_data    = lua.table2str(ext_table)
 
        nRet, operation       = m3.CreateDataObj(strLuaDEID, operation)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), '创建【作业】失败!' .. operation) end
 
        -- 作业的起点加锁
        nRet, strRetInfo = wms.wms_LockLocation(strLuaDEID, operation.start_loc_code,
        wms_base.Get_nConst(strLuaDEID, "锁类型-出库锁"), strCode, operation.code, operation.op_def_name)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "wms_LockLocation 失败!" .. strRetInfo) end
 
        -- 容器加锁
        local container
        nRet, container = wms_cntr.GetInfo(strLuaDEID, operation.cntr_code)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1),'获取容器对象失败!' .. container) end
        nRet, strRetInfo = wms_cntr.SetLock(strLuaDEID, container, "锁类型-出库锁", operation.code)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), '托盘容器添加出库锁失败!' .. strRetInfo) end
 
        -- 新增出库单明细
        local so_detail           = m3.AllocObject(strLuaDEID, "GT_SO_Detail")
        -- 起点信息
        so_detail.delivery_no     = stock_out.delivery_no
        so_detail.op_code         = operation.code
        so_detail.cntr_code       = ret_loc.cntr_code
        so_detail.start_area_code = loc_baseinfo.area_code
        so_detail.start_loc_code  = loc_baseinfo.code
        so_detail.end_area_code   = end_loc.area_code
        so_detail.end_loc_code    = end_loc.code
        nRet, so_detail           = m3.CreateDataObj(strLuaDEID, so_detail)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), '创建【出库单明细】失败!' .. so_detail) end
    end
end