lzh
2025-06-24 13c4a636539584ab977fddacfae884b3ec250aee
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
ÿþ-- š[INýQpe(uŽNÆbRW[&{2Nv^lbc:NpeW[ƖT
local function splitAndCollectNumbers(str)
    local numbers = {}
    for num in string.gmatch(str, "%d+") do
        numbers[tonumber(num)] = true
    end
    return numbers
end
 
-- š[INýQpe(uŽN~búQ$N*NƖT-N͑T„vpeW[v^üb¥cbW[&{2N
local function findCommonNumbers(strA, strB)
    local setA = splitAndCollectNumbers(strA)
    local setB = splitAndCollectNumbers(strB)
    local commonNumbers = {}
    for num in pairs(setA) do
        if setB[num] then
            table.insert(commonNumbers, tostring(num))
        end
    end
    return table.concat(commonNumbers, ",")
end
-- [
-- StockInquiry  “^X[ågâ‹
-- 10·ƒÖS÷]S'MO;`pe
-- 20·ƒÖSán'MO;`pe
-- 30ÿán'MO;`pe / ÷]S;`pe    ÿ* 100 = án'MO~vRÔk
-- 40$R­eán'MO“^X[/f&T¾0R80%,¡l¾0RRԏÞVïSeQ“^÷]S
-- param:
--      str_roadway   ÷]0R
-- ]
local function StockInquiry(strLuaDEID)
    -- ·ƒÖS*g»QÓ~„v÷]S
    local strCondition = "S_AREA_CODE = 'LK' AND N_LOCK_STATE = 0"
    local nRet, roadway = m3.QueryDataObject(strLuaDEID, "Roadway", strCondition)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), 'ågâ‹÷]SáOo`1Y%' .. roadway)
    end
    if (roadway == nil or roadway == '') then
        lua.Error(strLuaDEID, debug.getinfo(1), '¡l    gïSeQ“^„v÷]S!')
    end
 
    local str = ''
    for i = 1, #roadway do
        local attrs = roadway[i].attrs
        attrs = m3.KeyValueAttrsToObjAttr(attrs)
        if (attrs == nil) then
            goto coroutine
        end
 
        str = str .. attrs.N_ROADWAY .. ","
        ::coroutine::
    end
    -- »Sd–gTN*N,
    str = lua.trim_laster_char(str)
    lua.Debug(strLuaDEID, debug.getinfo(1), "str", str)
    return 0, str
end
 
-- Rú^ûN¡R
local function GT_CreateTask(strLuaDEID, operation, task, end_loc, type, schedule_type, roadway)
    local nRet, strRetInfo
    local new_task = m3.AllocObject(strLuaDEID, "Task")
    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, type)
    new_task.cntr_code = operation.cntr_code
    -- w¹p:N
NN*NûN¡R„vÈ~¹p
    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 = end_loc.wh_code
    new_task.end_area_code = end_loc.area_code
    new_task.end_loc_code = end_loc.code
    new_task.roadway = roadway
    new_task.schedule_type = wms_base.Get_nConst(strLuaDEID, schedule_type)
    nRet, new_task = m3.CreateDataObj(strLuaDEID, new_task)
    if (nRet ~= 0) then
        return 2, "Rú^ûN¡R1Y%!" .. new_task
    end
    return 0, new_task
end
--[[
 x: GT-40-27
  Tðy: ¨R\OÑv,T
 \O€:
 eQãSýQpeÿTaskFinish
 ŸRý€ô‹f:
 
 ØSôf†SòS:
 --]]
wms_op = require("wms_operation")
m3 = require("oi_base_mobox")
require("GT-Base")
require("GT_InAndOutboundPolicies")
function TaskFinish(strLuaDEID)
    local nRet, strRetInfo, objs
 
    local operation
    nRet, operation = m3.GetSysCurEditDataObj(strLuaDEID, "Operation")
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), operation)
    end
 
    -- ·ƒÖSûN¡Rù[aŒ
    local task
    nRet, task = m3.SysInputParamToDataObj(strLuaDEID, "Task")
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), task)
    end
 
    -- ã‰g\ON-N„vibU\ÂSpe
    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 -N…Q¹[JSON<h_ NTÕl!")
    end
    local type = ext_data.type -- 1 ‰|™eŒèb,dЏ 2 Øpö€,dЏ
    lua.Debug(strLuaDEID, debug.getinfo(1), "type", type)
    lua.Debug(strLuaDEID, debug.getinfo(1), "task", task)
 
    -- 1 ‰|™eŒèb,dЏ 2 Øpö€ûy“^ 3  T÷]Sûy“^
    if (tonumber(type) == 1) then
        local end_loc
        -- ûN¡R{|‹W :NAGVúQ“^,dЏ RRú^ŒNµkýVêûN¡R
        if (task.type == wms_base.Get_nConst(strLuaDEID, "ûN¡R{|‹W-AGVúQ“^,dЏ")) then
            -- ÇûN¡R„vÈ~¹p·ƒÖSæSN¹„vlÐ/ÞV“^ãS
            if (task.end_loc_code == wms_base.Get_sConst(strLuaDEID, "sO-    N|iJS¢”ÞV“^ãS")) then
                nRet, end_loc = wms_wh.Location_GetInfo(strLuaDEID, "THREE-QGCKK-QY")
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "WMS_Location_GetInfo1Y%! " .. end_loc)
                end
            else
                nRet, end_loc = wms_wh.Location_GetInfo(strLuaDEID, "THREE-BGCKK-QY")
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "WMS_Location_GetInfo1Y%! " .. end_loc)
                end
            end
 
            -- ·ƒÖSïSeQ“^÷]S
            local op_num, roadway
            local str
            nRet, str = StockInquiry(strLuaDEID)
            str = findCommonNumbers(str, "3,4")
            if (str == '') then
                lua.Error(strLuaDEID, debug.getinfo(1), "‰|™eŒèb¡l    gïSRM‘„v÷]S!")
            end
            local roadway_list = lua.split(str, ",")
            for i = 1, #roadway_list do
                -- ·ƒÖS÷]SûN¡Rpeϑ
                local strCondition = "N_ROADWAY = " .. roadway_list[i] .. " AND N_B_STATE = 1"
                nRet, strRetInfo = mobox.getDataObjCount(strLuaDEID, "Task", strCondition)
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "getDataObjCount 1Y%! " .. strRetInfo)
                end
                if (op_num == nil) then
                    op_num = lua.StrToNumber(strRetInfo)
                    roadway = roadway_list[i]
                elseif (tonumber(op_num) >= lua.StrToNumber(strRetInfo)) then
                    op_num = lua.StrToNumber(strRetInfo)
                    roadway = roadway_list[i]
                end
            end
            if (roadway == nil) then
                lua.Error(strLuaDEID, debug.getinfo(1), "‰|™eŒèb¡l    gïSRM‘„v÷]S!")
            end
 
            -- Rú^ûN¡R
            local new_task
            nRet, new_task = GT_CreateTask(strLuaDEID, operation, task, end_loc, "ûN¡R{|‹W-Ëz“^eQ“^,dЏ",
                "Œ¦^{|‹W-ýVê", roadway)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), new_task)
            end
 
            -- Ç¹[hV·ƒÖSir™eáOo`
            local cg_detail_list, cg_detail
            nRet, cg_detail_list = wms_cntr.Get_Container_Goods(strLuaDEID, new_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
 
            -- ·ƒÖSw¹pÑ~š[„vWCSÙz¹p
            local condition = "S_VALUE = '" .. new_task.start_loc_code .. "' AND S_NOTE NOT LIKE '%ؚ¦^ĉ<hÙzðS%'"
            nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "WMS_Const", condition)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "·ƒÖS08^ϑ0áOo`1Y%! " .. strRetInfo)
            end
            local start_loc_code_zd = strRetInfo.name
            condition = "S_VALUE = '" .. new_task.end_loc_code .. "' AND S_NOTE NOT LIKE '%ؚ¦^ĉ<hÙzðS%'"
            nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "WMS_Const", condition)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "·ƒÖS08^ϑ0áOo`1Y%! " .. strRetInfo)
            end
            local end_loc_code_zd = strRetInfo.name
 
            -- Œ(uýVꁄvûN¡R NÑS¥cãS
            local strCode = lua.guid() -- u§NN*NGUIDW[&{2N
            local str_day_time = os.date("%Y-%m-%d %H:%M:%S")
            local url = wms_base.Get_sConst(strLuaDEID, "WCS-url")
            local strurl = url .. "/create"
            local strHeader = ""
            local strBody = {}
            local data = {
                req_no = strCode,
                task_type = 5, -- 1='ireQ“^ÿ2='irúQ“^ÿ3=XbØvÄ~eQ“^ÿ4=XbØvÄ~úQ“^ÿ5=ûy¨Rÿ NǏ“^MO    ÿÿ6= T÷]Sûy“^;7= N T÷]Sûy“^
                task_no = new_task.code,
                tunnel_no = roadway,
                from_pos = start_loc_code_zd,
                to_pos = end_loc_code_zd,
                mat_code = operation.cntr_code,
                mat_type = cg_detail.item_code,
                mat_memo = cg_detail.item_name,
                req_time = str_day_time
            }
            strBody[1] = data
            lua.Debug(strLuaDEID, debug.getinfo(1), 'strBody', strBody)
            nRet, strRetInfo = CreateInterfaceExc(strLuaDEID, strurl, strHeader, strBody, "WCS", "ûN¡RRú^")
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "Œ(u¥cãS1Y%!" .. strRetInfo)
            end
            -- ¾‹n¶r`*g¨c
            wms_task.SetStateByCode(strLuaDEID, new_task.code, "ûN¡R¶r`-ò]¨c")
 
            -- w¹pã‰Ñ~
            nRet, strRetInfo = wms_wh.Loc_Container_Unbinding(strLuaDEID, task.start_loc_code, operation.cntr_code,
                "Ñ~š[ã‰Ñ~¹eÕl-û|ß~", "Œ[b")
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), ''MO¹[hVã‰Ñ~1Y%!' .. strRetInfo)
            end
 
            -- w¹p㉕
            nRet, strRetInfo = wms.wms_UnlockLocation(strLuaDEID,task.start_loc_code ,operation.bs_no)
            if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "wms_UnlockLocation 1Y%! " .. strRetInfo) end
            
            -- ûN¡R{|‹W :NAGVeQ“^,dЏ R¾‹nûN¡RŒ[bŒTsOûN¡RŒ[b
        elseif (task.type == wms_base.Get_nConst(strLuaDEID, "ûN¡R{|‹W-AGVeQ“^,dЏ")) then
            -- ¾‹n\ONŒ[b
            nRet, strRetInfo = wms_op.SetFinish(strLuaDEID, operation.code)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1),
                    "¾‹n\ON÷S='" .. operation.code .. "' „v\ONŒ[b1Y%!" .. strRetInfo)
            end
 
            -- ¾‹nsOûN¡RŒ[b
            local condition = " S_CNTR_CODE = '" .. operation.cntr_code .. "'"
            local update_sql = " N_B_STATE = 3,S_B_STATE = 'Œ[b'"
            nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_Task", condition, update_sql)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "îO9eûy“^USfÆ~¶r`1Y%ÿ" .. strRetInfo)
            end
 
            -- $R­esOûN¡R„vegnUS÷S@b(W„vsOûN¡R/f&Týò]Œ[b,Œ[bR¾‹n‰|™eŒèb¶r`:NŒ[b
            strCondition = "S_B_STATE <> 'Œ[b' AND S_BS_NO = (SELECT S_BS_NO FROM TN_GT_Task where S_CODE = '" ..
                               operation.bs_no .. "')"
            nRet, strRetInfo = mobox.getDataObjCount(strLuaDEID, "GT_Task", strCondition)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), strRetInfo)
            end
            lua.Debug(strLuaDEID, debug.getinfo(1), "nRet", nRet)
            if (tonumber(strRetInfo) == 0) then
                condition = "S_NO = (SELECT S_BS_NO FROM TN_GT_Task where S_CODE = '" .. operation.bs_no .. "')"
                update_sql = "S_B_STATE = 3"
                nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_Allot_Order", condition, update_sql)
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "îO9e‰|™eŒèbUS¶r`1Y%ÿ" .. strRetInfo)
                end
            end
 
            -- w¹pã‰Ñ~
            nRet, strRetInfo = wms_wh.Loc_Container_Unbinding(strLuaDEID, task.start_loc_code, operation.cntr_code,
                "Ñ~š[ã‰Ñ~¹eÕl-û|ß~", "Œ[b")
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), ''MO¹[hVã‰Ñ~1Y%!' .. strRetInfo)
            end
            -- È~¹pÑ~š[
            nRet, strRetInfo = wms_wh.Loc_Container_Binding(strLuaDEID, task.end_loc_code, operation.cntr_code,
                "Ñ~š[ã‰Ñ~¹eÕl-û|ß~", "Œ[b")
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), ''MO¹[hVã‰Ñ~1Y%!' .. strRetInfo)
            end
 
            -- 9hnc\ON„vÈ~¹p“^:SÞV O“^MO
            local toloc, fromloc
            if (operation.end_area_code == 'PFL' or operation.end_area_code == 'HWPFL') then
                toloc = 'LK007'
            elseif (operation.end_area_code == 'TFL' or operation.end_area_code == 'HWTFL') then
                toloc = 'LK005'
            end
            if (operation.start_area_code == 'PFL' or operation.start_area_code == 'HWPFL') then
                fromloc = 'LK007'
            elseif (operation.start_area_code == 'TFL' or operation.start_area_code == 'HWTFL') then
                fromloc = 'LK005'
            end
 
            -- ·ƒÖS¹[hV'ÁTfÆ~
            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 1Y%!" .. cg_detail)
            end
 
            -- ÞV OGTWMS
            local url = wms_base.Get_sConst(strLuaDEID, "GTWMS-url")
            local strurl = url
            local strHeader = ""
            local strBody = {
                application = "GITI",
                code = "WCS_FREEZE_WMS",
                data = {
                    wh_code = "wmwhse1",
                    batch_no = cg_detail.batch_no,
                    sku = cg_detail.item_code,
                    qty = cg_detail.qty,
                    fromloc = fromloc,
                    toloc = toloc,
                    loctype = nil,
                    type = 3 -- 1.»QÓ~/2.㉻Q/3.,dЏ
                }
            }
            local source = "GTWMSŒèbÞV O"
            nRet, strRetInfo = CreateInterfaceExc(strLuaDEID, strurl, strHeader, strBody, "GTWMS", source)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "Œ(u¥cãS1Y%!" .. strRetInfo)
            end
 
            -- ûN¡R{|‹W ûN¡R{|‹W-Ëz“^eQ“^,dЏ RRú^    NµkAGVûN¡R
        elseif (task.type == wms_base.Get_nConst(strLuaDEID, "ûN¡R{|‹W-Ëz“^eQ“^,dЏ")) then
            nRet, end_loc = wms_wh.Location_GetInfo(strLuaDEID, operation.end_loc_code)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "WMS_Location_GetInfo1Y%! " .. end_loc)
            end
 
            -- Rú^ûN¡R
            local new_task
            nRet, new_task = GT_CreateTask(strLuaDEID, operation, task, end_loc, "ûN¡R{|‹W-AGVeQ“^,dЏ",
                "Œ¦^{|‹W-AGV")
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), new_task)
            end
            -- Ç¹[hV·ƒÖSir™eáOo`
            local cg_detail
            nRet, cg_detail = wms_cntr.Get_Container_Goods(strLuaDEID, new_task.cntr_code)
            nRet, cg_detail = m3.ObjAttrStrToLuaObj("CG_Detail", lua.table2str(cg_detail[1].attrs))
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), cg_detail)
            end
 
            local data = {
                taskData = {
                    taskNum = new_task.code,
                    pickStation = new_task.start_loc_code,
                    dropStation = new_task.end_loc_code,
                    taskType = 5,
                    carrierType = 1,
                    priority = 1,
                    source = "YCL"
                },
                partData = {
                    rfid = operation.cntr_code,
                    lotNumber = nil,
                    partNumber = cg_detail.item_code,
                    partDesc = cg_detail.item_code,
                    partType = nil,
                    weight = cg_detail.qty,
                    unit = cg_detail.wu,
                    maturityTime = nil,
                    productionTime = nil,
                    stewingTime = nil,
                    overdueTime = nil
                }
            }
            -- Œ(uAGVûN¡R NÑS
            local url = wms_base.Get_sConst(strLuaDEID, "AGV-url")
            local strurl = url .. "/CreateTask"
            local strHeader = ""
            local strBody = data
            nRet, strRetInfo = CreateInterfaceExc(strLuaDEID, strurl, strHeader, strBody, "AGV", "ûN¡RRú^")
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "Œ(uWCS¥cãS1Y%!" .. strRetInfo)
            end
 
            -- ¾‹n¶r`*g¨c
            wms_task.SetStateByCode(strLuaDEID, new_task.code, "ûN¡R¶r`-ò]¨c")
        end
    elseif (tonumber(type) == 3) then
        -- ¾‹n\ONŒ[b
        nRet, strRetInfo = wms_op.SetFinish(strLuaDEID, operation.code)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1),
                "¾‹n\ON÷S='" .. operation.code .. "' „v\ONŒ[b1Y%!" .. strRetInfo)
        end
 
        -- ¾‹nsOûN¡RŒ[b
        local condition = " S_CNTR_CODE = '" .. operation.cntr_code .. "'"
        local update_sql = " N_B_STATE = 3,S_B_STATE = 'Œ[b'"
        nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_Task", condition, update_sql)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), "îO9eûy“^USfÆ~¶r`1Y%ÿ" .. strRetInfo)
        end
    end
end