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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
ÿþ-- ÛV ‚”NeQýQpe
local function round(number, precision)
    precision = precision or 0
    local mult = 10 ^ (precision or 0)
    return math.floor(number * mult + 0.5) / mult
end
 
-- š[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
 
-- [
-- PackWorkGeneralOperation  Åˆ±{å]USeQ“^USeQ“^(uÍd\O
-- 10·ƒÖSîvƉaSáOo`
-- 20°ežX¹[hV'ÁTÑ~š[
-- 30°ežXň±{°‹U_
-- 40ôf°eň±{å]USpeϑ
-- 50ôf°eeQ“^USpeϑ
-- 60$R­eň±{å]US0eQ“^US/f&TsQí•
-- param:
--      serial_no   agx
--      cntr_code   ¹[hVx
--      pack_no     Åˆ±{å]US÷S
--      order_no    eQ“^US÷S
-- ]
local function PackWorkGeneralOperation(strLuaDEID, serial_no, cntr_code, pack_no, order_no, dispersoid)
    local nRet, strRetInfo, label_card, strCondition
 
    -- step1 ·ƒÖSîvƉaS-N„váOo` ÿ(uُ*NaS-N„váOo`Rú^ CG_Detail
    strCondition = "S_SERIAL_NO = '" .. serial_no .. "'"
    nRet, label_card = m3.GetDataObjByCondition(strLuaDEID, "GT_Label_Crad", strCondition)
    if (nRet ~= 0) then
        return 2, "m3.GetDataObjByCondition 1Y%!" .. label_card
    end
    -- îvƉaSúW,g^\'`$R­e
    if (label_card.qty <= 0) then
        return 1, "îvƉaS'" .. serial_no .. "'„vpeϑÅ_{˜    g<P Ný€/fpe!"
    end
 
    nRet, strRetInfo = wms_cntr.EmptyContainer_Verify(strLuaDEID, cntr_code)
    if (nRet ~= 0) then
        return 2, "¹[hVŒšÁ‹1Y%!" .. strRetInfo
    end
 
    -- step2 °ežX¹[hV'irÑ~š[
    local cg_detail = m3.AllocObject(strLuaDEID, "CG_Detail")
    cg_detail.cntr_code = cntr_code
    cg_detail.batch_no = label_card.batch_no
    cg_detail.item_code = label_card.item_code
    cg_detail.item_name = label_card.item_name
    cg_detail.qty = label_card.qty
    cg_detail.uom = 'kg'
    cg_detail.serial_no = label_card.serial_no
    lua.Debug(strLuaDEID, debug.getinfo(1), 'cg_detail', cg_detail)
    nRet, cg_detail = m3.CreateDataObj(strLuaDEID, cg_detail)
    if (nRet ~= 0) then
        return 2, 'mobox Rú^0¹[hV'ÁTfÆ~0ù[aŒ1Y%!' .. cg_detail
    end
 
    -- step3  °ežX0ň±{°‹U_0
    local packing_record = m3.AllocObject(strLuaDEID, "GT_Packing_Rec")
    packing_record.pack_no = pack_no
    packing_record.qty = label_card.qty
    packing_record.cntr_code = cntr_code
    packing_record.serial_no = label_card.serial_no
    nRet, packing_record = m3.CreateDataObj(strLuaDEID, packing_record)
    if (nRet ~= 0) then
        return 2, 'Rú^0ň±{°‹U_01Y%!' .. packing_record
    end
 
    -- step4 ôf°e0ň±{å]US0-N„v/}¡‹Åˆ±{peϑ
    -- Åˆ±{å]UShˆ„v/}¡‹peϑ+1
    strCondition = "S_PACK_NO = '" .. pack_no .. "'"
    local strSetSQL_update = " N_ACC_QTY = N_ACC_QTY + 1 "
    nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_Packing_Order", strCondition, strSetSQL_update)
    if (nRet ~= 0) then
        return 2, "updateDataAttrByCondition 1Y%! " .. strRetInfo
    end
 
    -- ‚Yœg/fceňö€RUSìr$R­eeQ“^USncLˆub„v;`͑ϑ/f&Tán³eQ“^USnc4Y„vyb!k÷S;`͑,án³RsQí•eQ“^USŒTň±{å]US
    if (dispersoid == 'Y') then
        -- ·ƒÖSeQ“^USnc4Y
        local incoming_Info
        strCondition = "S_ORDER_NO = '" .. label_card.order_no .. "'"
        nRet, incoming_Info = m3.GetDataObjByCondition(strLuaDEID, "GT_Incoming_Info", strCondition)
        if (nRet ~= 0) then
            return 2, "m3.GetDataObjByCondition 1Y%!" .. incoming_Info
        end
 
        -- ·ƒÖSêñ]ub„vceňö€„vpeϑ;`ŒT
        strCondition = "S_SERIAL_NO LIKE 'S%' AND S_ORDER_NO = '" .. incoming_Info.order_no .. "'"
        local sumQty
        nRet, sumQty = mobox.getDataObjAttrSum(strLuaDEID, "GT_Label_Crad", strCondition, "F_QTY")
        if (nRet ~= 0) then
            return 2, "·ƒÖSò]eQ“^ceňö€;`pe1Y%! " .. sumQty
        end
        lua.Debug(strLuaDEID, debug.getinfo(1), 'sumQty', sumQty)
        sumQty = json.decode(sumQty)
        if (sumQty[1] == nil or sumQty[1] == '') then
            sumQty = 0
        else
            sumQty = tonumber(sumQty[1])
        end
 
        -- $R­eò]eQ“^peϑ/f&T¾0R GTWMS NÑS„veQ“^USnc4Y„vyb!k÷S;`͑,¾0R†NR¾‹nň±{å]USŒTeQ“^US¶r`:NŒ[b
        if (tonumber(sumQty) >= tonumber(incoming_Info.actual_qty)) then
            strCondition = "S_ORDER_NO = '" .. label_card.order_no .. "'"
            local strSetSQL = "S_STATE = 'Œ[b'"
            nRet, strRetInfo = mobox.updateTableAttrByCondition(strLuaDEID, "TN_GT_Incoming_Info", strCondition,
                strSetSQL)
            if (nRet ~= 0) then
                return 2, strRetInfo
            end
 
            strCondition = "S_ORDER_NO = '" .. label_card.order_no .. "'"
            strSetSQL = "S_STATE = 'Œ[b'"
            nRet, strRetInfo = mobox.updateTableAttrByCondition(strLuaDEID, "TN_GT_Packing_Order", strCondition,
                strSetSQL)
        end
    else
        -- step5 ôf°e0eQ“^US0-N„v/}¡‹Åˆ±{peϑ
        strCondition = "S_ORDER_NO = '" .. order_no .. "'"
        strSetSQL_update = " N_ACC_PACK_QTY = N_ACC_PACK_QTY + 1 "
        nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_Incoming_Info", strCondition,
            strSetSQL_update)
        if (nRet ~= 0) then
            return 2, "updateDataAttrByCondition 1Y%! " .. strRetInfo
        end
 
        -- step6  $R­eň±{å]US0eQ“^US/f&TsQí•
        -- 10$R­eň±{å]US/f&T    gň±{öNpe ÿ‚Yœg    g$R­eň±{å]US/f&TŒ[b
        -- 20$R­eå‹Åˆ±{å]US
N8n„veQ“^US„v/}¡‹peϑŒTXbØvpeϑ/f&Tøv T ÿøv TR¾‹neQ“^US¶r`åNÊSeQ“^US N@b    g/T¨R¶r`„vň±{å]US ¶r`:NŒ[b
        local obj_display_name = packing_record.pack_no
        local wfp_list_show_name = "'" .. packing_record.pack_no .. "'°ežX0ň±{°‹U_0TYt z^"
        nRet, strRetInfo = mobox.addSysWFP(strLuaDEID, 1, "", packing_record.cls, packing_record.id, obj_display_name,
            0, "", "$R­e¶r`/f&TŒ[b", wfp_list_show_name, "¥cãS WCStoreCallback", 0)
        if (nRet ~= 0) then
            return 2, "addSysWFP 1Y%!" .. strRetInfo
        end
    end
 
    return 0, label_card
end
 
-- [
-- ManualBindGeneralOperation  ºNå]Ñ~š[eQ“^USeQ“^(uÍd\O
-- 10ŒšÁ‹¹[hV'irÑ~š[
-- 20·ƒÖSeQ“^US÷S
-- 30ôf°eeQ“^US¶r`0peϑ
-- 40$R­eeQ“^US/f&TsQí•
-- param:
--      cntr_code   ¹[hVx
-- ]
local function ManualBindGeneralOperation(strLuaDEID, cntr_code)
    local nRet, strRetInfo, cg_detail, strCondition
 
    -- step1 ŒšÁ‹¹[hV'irÑ~š[
    nRet, cg_detail = wms_cntr.Get_Container_Goods(strLuaDEID, cntr_code)
    if (cg_detail == nil or cg_detail == '' or #cg_detail == 0) then
        return 1, "*gۏLˆxØvÍd\O!"
    end
    cg_detail = cg_detail[1].attrs
    cg_detail = m3.KeyValueAttrsToObjAttr(cg_detail)
 
    -- step2 ÇAm4l÷S·ƒÖSeQ“^US÷S
    strCondition = "S_SERIAL_NO = '" .. cg_detail.S_SERIAL_NO .. "'"
    local label_card
    nRet, label_card = m3.GetDataObjByCondition(strLuaDEID, "GT_Label_Crad", strCondition)
    if (nRet ~= 0) then
        return 2, "m3.GetDataObjByCondition 1Y%!" .. label_card
    end
 
    -- CG_Detail̑b—„vW[µk N_IS_FJ :N YÀhhƋ,‚Yœgå‹penc/f YÀhR N—‰ôf°eeQ“^US„v¶r`
    if (tonumber(cg_detail.N_IS_FJ) == 1) then
        return 0, label_card
    end
 
    -- step3 ôf°eeQ“^US¶r`0peϑ
    -- ‚Yœg/fceňö€RUSìr$R­eeQ“^USncLˆub„v;`͑ϑ/f&Tán³eQ“^USnc4Y„vyb!k÷S;`͑,án³RsQí•eQ“^USŒTň±{å]US
    if (label_card.dispersoid == 'Y') then
        -- ·ƒÖSeQ“^USnc4Y
        local incoming_Info
        strCondition = "S_ORDER_NO = '" .. label_card.order_no .. "'"
        nRet, incoming_Info = m3.GetDataObjByCondition(strLuaDEID, "GT_Incoming_Info", strCondition)
        if (nRet ~= 0) then
            return 2, "m3.GetDataObjByCondition 1Y%!" .. incoming_Info
        end
 
        -- ·ƒÖSêñ]ub„vceňö€„vpeϑ;`ŒT
        strCondition = "S_SERIAL_NO LIKE 'S%' AND S_ORDER_NO = '" .. incoming_Info.order_no .. "'"
        local sumQty
        nRet, sumQty = mobox.getDataObjAttrSum(strLuaDEID, "GT_Label_Crad", strCondition, "F_QTY")
        if (nRet ~= 0) then
            return 2, "·ƒÖSò]eQ“^ceňö€;`pe1Y%! " .. sumQty
        end
        lua.Debug(strLuaDEID, debug.getinfo(1), 'sumQty', sumQty)
        sumQty = json.decode(sumQty)
        if (sumQty[1] == nil or sumQty[1] == '') then
            sumQty = 0
        else
            sumQty = tonumber(sumQty[1])
        end
 
        -- $R­eò]eQ“^peϑ/f&T¾0R GTWMS NÑS„veQ“^USnc4Y„vyb!k÷S;`͑,¾0R†NR¾‹nň±{å]USŒTeQ“^US¶r`:NŒ[b
        if (tonumber(sumQty) >= tonumber(incoming_Info.actual_qty)) then
            strCondition = "S_ORDER_NO = '" .. label_card.order_no .. "'"
            local strSetSQL = "S_STATE = 'Œ[b'"
            nRet, strRetInfo = mobox.updateTableAttrByCondition(strLuaDEID, "TN_GT_Incoming_Info", strCondition,
                strSetSQL)
            if (nRet ~= 0) then
                return 2, strRetInfo
            end
 
            strCondition = "S_ORDER_NO = '" .. label_card.order_no .. "'"
            strSetSQL = "S_STATE = 'Œ[b'"
            nRet, strRetInfo = mobox.updateTableAttrByCondition(strLuaDEID, "TN_GT_Packing_Order", strCondition,
                strSetSQL)
        end
    else
        strCondition = "S_ORDER_NO = '" .. label_card.order_no .. "'"
        nRet, strRetInfo = mobox.queryOneDataObjAttr(strLuaDEID, "GT_Incoming_Info", strCondition)
        if (nRet ~= 0) then
            return 2, "queryOneDataObjAttr 1Y%!" .. strRetInfo
        end
        strRetInfo = json.decode(strRetInfo)
 
        local strSetSQL_update = " N_ACC_PACK_QTY = N_ACC_PACK_QTY + 1 "
        if (strRetInfo.state == '°eú^') then
            strSetSQL_update = strSetSQL_update .. " ,S_STATE = '/T(u'"
        end
        nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_Incoming_Info", strCondition, strSetSQL_update)
        if (nRet ~= 0) then
            return 2, "updateDataAttrByCondition 1Y%! " .. strRetInfo
        end
 
        -- step4 $R­eeQ“^US/f&TsQí•
        -- $R­eeQ“^USnc„v /}¡‹peϑ = XbØvpeϑ ÿ‚YœgøvI{R¾‹neQ“^US:NŒ[b ÿ‚Yœg…úQ XbØvpe û|ß~¥b•
        local order
        nRet, order = m3.GetDataObjectByKey(strLuaDEID, "GT_Incoming_Info", "S_ORDER_NO", label_card.order_no)
        if (nRet ~= 0) then
            return 2, order
        end
        if (tonumber(order.acc_pack_qty) == tonumber(order.cntr_qty)) then
            strCondition = "S_ORDER_NO = '" .. order.order_no .. "'"
            local strSetSQL = "S_STATE = 'Œ[b'"
            nRet, strRetInfo = mobox.updateTableAttrByCondition(strLuaDEID, "TN_GT_Incoming_Info", strCondition, strSetSQL)
            if (nRet ~= 0) then
                return 2, strRetInfo
            end
        elseif (tonumber(order.acc_pack_qty) > tonumber(order.cntr_qty)) then
            return 2, "eQ“^USò]Œ[b ÿ NAQ¸‹eQ“^!"
        end
    end
 
    return 0, label_card
end
 
-- [
-- PackVerification  Åˆ±{!hŒš
-- 10ŒšÁ‹¹[hV'irÑ~š[
-- 20ŒšÁ‹å]MONň±{å]US/f&TNô
-- 30·ƒÖSeQ“^US÷S
-- param:
--      station_no   eQ“^Ùz¹p
-- ]
local function PackVerification(strLuaDEID, station_no)
    local nRet, strRetInfo
 
    -- step1·ƒÖS,gå]MOg°e„v 0ň±{ÀhŒš0
    local strCondition = "S_STATION = '" .. station_no .. "' AND S_STATE = '°eú^'"
    local strOrder = "T_CREATE desc"
    local packing_check
    nRet, packing_check = m3.GetDataObjByCondition(strLuaDEID, "GT_Packing_Check", strCondition, strOrder)
    if (nRet == 1) then
        return 1, "÷‹HQ(uPDAkbÏcîvƉaSۏLˆÀhŒš!"
    end
    if (nRet ~= 0) then
        return 2, "m3.GetDataObjByCondition 1Y%!" .. packing_check
    end
 
    -- step2  ŒšÁ‹å]MONň±{å]US/f&TNô
    local delivery_no = packing_check.delivery_no -- 6e'US÷S
    local delivery_row_no = packing_check.delivery_row_no -- 6e'USLˆ÷S
    -- ÀhŒšÅˆ±{å]US
N„vir™e/f&T(W,gå]MOň±{
    -- Ç 6e'US÷S06e'USLˆ÷S ÿ êS‰Åˆ±{å]US
N    g Ù*Nå]MO„v6e'US÷S06e'USLˆ÷S·ƒÖS„veQ“^US÷S 1\Ç
    -- åg⋠¶r`=/T¨R „vň±{å]US
    local sql = "SELECT S_ORDER_NO FROM TN_GT_Incoming_Info WHERE S_DELIVERY_NO = '" .. delivery_no ..
                    "' AND N_DELIVERY_ROW_NO = '" .. delivery_row_no .. "'"
    strCondition = "S_STATION = '" .. station_no .. "' AND S_STATE = '/T(u' AND S_ORDER_NO = (" .. sql .. ")"
    local packing_order
    nRet, packing_order = m3.GetDataObjByCondition(strLuaDEID, "GT_Packing_Order", strCondition, strOrder)
    if (nRet == 1) then
        return 1, "îvMR„vň±{å]MO Ný€ù[6e'US÷S:N'" .. delivery_no .. "'„vir™eۏLˆÅˆ±{!"
    end
    if (nRet ~= 0) then
        return 2, "m3.GetDataObjByCondition 1Y%!" .. packing_order
    end
 
    -- step3  ôf°e0ň±{!hŒš0¾‹n:N"Œ[b"
    strCondition = "S_SERIAL_NO = '" .. packing_check.serial_no .. "' AND S_STATION = '" .. station_no ..
                       "' AND S_STATE = '°eú^'"
    local strUpdateSql = "S_STATE = 'Œ[b'"
    nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_Packing_Check", strCondition, strUpdateSql)
    if (nRet ~= 0) then
        return 2, "ôf°e0ň±{!hŒš0áOo`1Y%!" .. strRetInfo
    end
 
    return 0, packing_check.serial_no
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
        return 2, 'ågâ‹÷]SáOo`1Y%' .. roadway
    end
    if (roadway == nil or roadway == '') then
        return 2, '¡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
 
        -- Œ(uWCS¥cãS·ƒÖS÷]S„v¶r`
        local data = {
            req_no = lua.guid(),
            roadway = attrs.N_ROADWAY
        }
        local url = wms_base.Get_sConst(strLuaDEID, "WCS-url")
        local strurl = url .. "/StackerState"
        local roadway_state
        local nRet, strRetInfo = mobox.sendHttpRequest(strurl, "", lua.table2str(data))
        if (nRet ~= 0) then
            return 2, "Œ(u¥cãS1Y%!" .. strRetInfo
        end
        local success, resJson = pcall(json.decode, roadway_state)
        if (success == 0) then
            roadway_state = resJson.data[1].roadway_state
            if (tonumber(roadway_state) == 0 or tonumber(roadway_state) == 3) then
                goto coroutine
            end
        end
 
        local strRetInfo
        -- ·ƒÖS÷]S'MO;`pe
        strCondition = "S_AREA_CODE = 'LK' AND N_ROADWAY = " .. attrs.N_ROADWAY
        nRet, strRetInfo = mobox.getDataObjCount(strLuaDEID, "Location", strCondition)
        if (nRet ~= 0) then
            return 2, "getDataObjCount 1Y%!" .. strRetInfo
        end
        local hw_count = tonumber(strRetInfo) -- cš[÷]S„v'MO;`pe
        lua.Debug(strLuaDEID, debug.getinfo(1), "hw_count", hw_count)
 
        strCondition = "S_AREA_CODE = 'LK' AND N_CURRENT_NUM <> 0 AND N_ROADWAY = " .. attrs.N_ROADWAY
        nRet, strRetInfo = mobox.getDataObjCount(strLuaDEID, "Location", strCondition)
        if (nRet ~= 0) then
            return 2, "getDataObjCount 1Y%!" .. strRetInfo
        end
        local mhw_count = tonumber(strRetInfo) -- cš[÷]S„ván'MO;`pe
        lua.Debug(strLuaDEID, debug.getinfo(1), "mhw_count", mhw_count)
        local mhw_ratio = (mhw_count / hw_count) * 100 -- án'MO`SÔk
        if (mhw_ratio == nil) then
            return 1,
                "¡‹—{1Y%!÷]S:" .. attrs.N_ROADWAY .. " án'MOpeϑ:" .. mhw_count .. "'MO;`pe:" ..
                    hw_count
        end
 
        -- ·ƒÖSW[xQš[IN„vûy“^Ôk‹O
        nRet, strRetInfo = mobox.getDictItemIInfo("GT_LK_RATIO")
        if (nRet ~= 0) then
            return 2, "getDictItemIInfo 1Y%!" .. strRetInfo
        end
        strRetInfo = json.decode(strRetInfo)
        local item_ratio = tonumber(strRetInfo[1].name) -- W[xQš[IN„v÷]S‰[hQ“^X[Ôk‹O
 
        if (mhw_ratio <= item_ratio) then
            str = str .. attrs.N_ROADWAY .. ","
        end
        ::coroutine::
    end
    -- »Sd–gTN*N,
    str = lua.trim_laster_char(str)
    lua.Debug(strLuaDEID, debug.getinfo(1), "str", str)
    return 0, str
end
 
-- [
-- RobotBoxing  :ghVºNň±{
-- 10·ƒÖSGTWMS Tek„vAm4l÷S
-- 20GTWMSAm4l÷SYLˆöenxš[wQSOYtLˆ
-- 30·ƒÖSÇir™exceňö€Ä~ØvĉRô~¤b„vir™e„vhÆQ͑ϑ
-- 409hncxØvĉR¡‹—{͑ϑ
-- 50°ežXeQ“^USncLˆáOo`
-- param:
--      order_no     eQ“^US÷S
--      qty          Åˆ±{peϑ
--      palletizing  xØvĉR
-- ]
local function RobotBoxing(strLuaDEID, order_no, qty, palletizing)
    local nRet, strRetInfo
 
    if (qty == nil) then
        return 2, "ceňö€peϑ^—peW[b:Nzz!"
    end
    if (qty ~= math.floor(qty) or qty <= 0) then
        return 2, "ceňö€peϑ^—cktepe"
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), 'qty', tonumber(qty))
 
    if (palletizing == '' or palletizing == nil) then
        return 2, "ceňö€xØvĉR Ný€:Nzz!"
    end
 
    -- step1 ·ƒÖSGTWMS Tek„vAm4l÷S(ïSý€X[(WN*NUSnc4Y ÿY*NAm4l÷S„vX[(W)
    local strCondition = "S_ORDER_NO = '" .. order_no .. "' AND S_SERIAL_NO NOT LIKE 'S%'"
    local label_crad
    nRet, label_crad = m3.QueryDataObject(strLuaDEID, "GT_Label_Crad", strCondition)
    if (nRet ~= 0) then
        return 2, "m3.GetDataObjByCondition 1Y%!" .. label_crad
    end
 
    local attr
    -- step2 GTWMSAm4l÷SYLˆöenxš[wQSOYtLˆ
    for i = 1, #label_crad do
        nRet, attr = m3.ObjAttrStrToLuaObj("GT_Label_Crad", lua.table2str(label_crad[i].attrs))
        if (nRet ~= 0) then
            return 2, "m3.ObjAttrStrToLuaObj(GT_Label_Crad) 1Y%! " .. attr
        end
        lua.Debug(strLuaDEID, debug.getinfo(1), 'attr', attr)
 
        -- 9hncAm4l÷S„v/UNxß~¡‹bìNêñ]ԏÞV„vceňö€ S_4Y„vx „vpeϑ;`ŒT
        strCondition = "S_REQ_NO = '" .. attr.req_no .. "' AND S_SERIAL_NO LIKE 'S%'"
        local sumQty
        nRet, sumQty = mobox.getDataObjAttrSum(strLuaDEID, "GT_Label_Crad", strCondition, "F_QTY")
        if (nRet ~= 0) then
            return 2, "·ƒÖSò]eQ“^ceňö€;`pe1Y% 1Y%! " .. sumQty
        end
        sumQty = json.decode(sumQty)
        if (sumQty[1] == nil or sumQty[1] == '') then
            sumQty = 0
        else
            sumQty = tonumber(sumQty[1])
        end
 
        if (tonumber(attr.qty) > tonumber(sumQty)) then
            goto coroutine
        end
 
        if (i == #label_crad) then
            return 2, "*gåg~b0Rù[”^„veQ“^USncLˆ°‹U_! "
        end
    end
 
    ::coroutine::
 
    -- label_crad = attr
    -- step3 ÇeQ“^USncLˆ„vXbØv͑ϑŒTWWpe¡‹—{hÆQ͑ϑ
    local weight = tonumber(attr.qty) / tonumber(attr.number)
    lua.Debug(strLuaDEID, debug.getinfo(1), "qty", qty)
    lua.Debug(strLuaDEID, debug.getinfo(1), "attr", attr)
    lua.Debug(strLuaDEID, debug.getinfo(1), "weight", weight)
 
    -- step4 9hncxØvĉR¡‹—{͑ϑ eQ“^USncLˆÍ‘Ï‘:N:g°hß~¡‹„vpeϑ * hÆQ͑ϑ
    local all_qty = tonumber(qty) * tonumber(weight)
    lua.Debug(strLuaDEID, debug.getinfo(1), "all_qty", all_qty)
    -- ê¨RubAm4l÷S(xĉR S + 7MOpeW[)
    local strCode
    nRet, strCode = mobox.getSerialNumber("ceňö€Am4l÷S", 'S', 7)
    if (nRet ~= 0) then
        return 2, "3u÷‹ceňö€eQ“^USncLˆx1Y%!" .. strCode
    end
 
    -- setep5 °ežXeQ“^USncLˆáOo` Rú^eQ“^USncLˆ
    -- ·ƒÖSN*NRËY„v0eQ“^USncLˆ0pencù[aŒ
    local new_label_crad = m3.AllocObject(strLuaDEID, "GT_Label_Crad")
    new_label_crad.delivery_no = attr.delivery_no
    new_label_crad.delivery_row_no = attr.delivery_row_no
    new_label_crad.item_code = attr.item_code
    new_label_crad.item_code_9 = attr.item_code_9
    new_label_crad.item_name = attr.item_name
    new_label_crad.product_date = attr.product_date
    new_label_crad.batch_no = attr.batch_no
    new_label_crad.item_state = attr.item_state
    new_label_crad.qty = all_qty
    new_label_crad.serial_no = strCode
    new_label_crad.wh_code = attr.wh_code
    new_label_crad.storage_loc = attr.storage_loc
    new_label_crad.order_no = attr.order_no
    new_label_crad.is_bonded = attr.is_bonded
    new_label_crad.wheel_type_rot = attr.wheel_type_rot
    new_label_crad.subpool = attr.subpool
    new_label_crad.req_no = attr.req_no
    new_label_crad.dispersoid = attr.dispersoid
    new_label_crad.palletizing = attr.palletizing
    lua.Debug(strLuaDEID, debug.getinfo(1), 'new_label_crad', new_label_crad)
    nRet, new_label_crad = m3.CreateDataObj(strLuaDEID, new_label_crad)
    if (nRet ~= 0) then
        return 2, "mobox Rú^0eQ“^USncLˆ0ù[aŒ1Y%!" .. new_label_crad
    end
 
    return 0, new_label_crad.serial_no
end
 
-- Rú^\ON
local function CreateOperation(strLuaDEID, item_code, batch_no, loc_start, inbound_policy, ext_table, cntr_code)
    lua.Debug(strLuaDEID, debug.getinfo(1), 'ۏeQRú^\ON', loc_start)
    lua.Debug(strLuaDEID, debug.getinfo(1), 'ۏeQRú^\ON', inbound_policy)
    lua.Debug(strLuaDEID, debug.getinfo(1), 'ۏeQRú^\ON', cntr_code)
    -- ·ƒÖSir™e{|‹W
    local nRet, item_type, material = GT_Get_ItemType(strLuaDEID, item_code)
    if (nRet ~= 0) then
        return 1, item_type
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), 'ir™eáOo`', material)
 
    -- ûm RibU\\ON
    ext_table.batch_no = batch_no -- yb!k÷S
    ext_table.item_code = item_code -- ir™ex
    ext_table.item_type = item_type -- ir™e{|‹W
    ext_table.is_insulate = material.is_insulate -- /f&TÝO)n
 
    -- ·ƒÖSW[xQš[IN„v)Y6qö€Ä‰R
    local is_trj
    nRet, is_trj = mobox.getDictItemIInfo("GT_TRJ_DATE")
    if (nRet ~= 0) then
        return 2, "getDictItemIInfo 1Y%!" .. is_trj
    end
    is_trj = json.decode(is_trj)
    is_trj = tonumber(is_trj[1].name) -- )Y6qö€Ä‰R 1 /T(u 2 y(u
 
    -- ·ƒÖS÷]S“^X[*g¾0R 80% „vïSeQ“^÷]S
    local strRetInfo
    nRet, strRetInfo = StockInquiry(strLuaDEID)
    if (nRet ~= 0) then
        return 2, strRetInfo
    end
    ext_table.roadway_inventory = strRetInfo -- \ONibU\pencX[eQُ*N÷]S“^X[W[&{2N ÿ̑b—/f“^X[*g…Ç80%„v÷]S
    lua.Debug(strLuaDEID, debug.getinfo(1), 'ïSeQ“^÷]S', strRetInfo)
 
    -- $R­ew¹p/f&T:NN|iæ]§OÞV“^ãSb    N|iÞV“^ãS,‚Yœg/fêSý€O(u3 ÿ4÷]S
    if (loc_start.code == wms_base.Get_sConst(strLuaDEID, "KL_ReturnRgvStation1") or loc_start.code ==
        wms_base.Get_sConst(strLuaDEID, "F3L_InWeight1") or loc_start.code ==
        wms_base.Get_sConst(strLuaDEID, "F3R_InWeight1")) then
        local b = wms_base.Get_sConst(strLuaDEID, "sO-N|iæ]§OÞV“^ãS÷]S")
        local str = findCommonNumbers(ext_table.roadway_inventory, b)
        if (str == nil or str == '') then
            return 2, "¡l    gïSeQ“^„v3,4÷]S!"
        end
        ext_table.roadway_inventory = str
    end
 
    -- step5  Rú^\ON
    local operation = m3.AllocObject(strLuaDEID, "Operation")
    operation.start_wh_code = loc_start.wh_code
    operation.start_area_code = loc_start.area_code
    operation.start_loc_code = loc_start.code
 
    operation.op_type = wms_base.Get_nConst(strLuaDEID, "\ON{|‹W-eQ“^")
 
    -- $R­e¢[7bcš[„vĉR
    -- ¢[7bcš[ö€™eeQ“^V{eu 1ÿeQ708÷]Sÿ 2ÿ NeQ708÷]Sÿ 3ÿû|ß~÷]SGWaˆRM‘ÿ ˆ”ù[)Y6qö€ir™eeQ“^„vöeP»SۏLˆV{eu„v$R­e0
    local end_loc
    if (tonumber(inbound_policy) == 1) then
        -- ·ƒÖSïSeQ“^÷]S
        local b = wms_base.Get_sConst(strLuaDEID, "sO-ÝO)n÷]S")
        local str = findCommonNumbers(ext_table.roadway_inventory, b)
        if (str == nil or str == '') then
            return 2, "¡l    gïSeQ“^„vÝO)n÷]S!"
        end
        ext_table.roadway_inventory = str
        operation.op_def_name = "ÝO)nPg™eeQ“^"
        -- ·ƒÖS\ONÈ~¹pMOn
        nRet, end_loc = BW_StorageTactics(strLuaDEID, ext_table)
        if (nRet ~= 0) then
            return 2, "¡‹—{È~¹p1Y%ÿ" .. end_loc
        end
        if (end_loc == nil or end_loc == '') then
            return 2, "Ëz“^-N¡l    gán³agöN„v'MOïSåNRM‘!"
        end
        -- ·ƒÖSÈ~¹páOo`
        nRet, end_loc = wms_wh.GetLocInfo(end_loc)
        if (nRet ~= 0) then
            return 2, 'WMS_GetLocInfo1Y%!' .. end_loc
        end
        operation.end_wh_code = end_loc.wh_code
        operation.end_area_code = end_loc.area_code
        operation.end_loc_code = end_loc.code
        ext_table.roadway = end_loc.roadway
        goto continue
    elseif (tonumber(inbound_policy) == 2) then
        local b = wms_base.Get_sConst(strLuaDEID, "sO-^—ÝO)n÷]S")
        local str = findCommonNumbers(ext_table.roadway_inventory, b)
        if (str == nil or str == '') then
            return 2, "¡l    gïSeQ“^„v^—ÝO)n÷]S!"
        end
        ext_table.roadway_inventory = str
        operation.op_def_name = "Ëz“^eQ“^"
        -- ·ƒÖSÈ~¹páOo`
        nRet, end_loc = JL_StorageTactics(strLuaDEID, ext_table)
        if (nRet ~= 0) then
            return 2, "¡‹—{È~¹p1Y%ÿ" .. end_loc
        end
        if (end_loc == nil or end_loc == '') then
            return 2, "Ëz“^-N¡l    gán³agöN„v'MOïSåNRM‘!"
        end
        -- ·ƒÖSÈ~¹páOo`
        nRet, end_loc = wms_wh.GetLocInfo(end_loc)
        if (nRet ~= 0) then
            return 2, 'WMS_GetLocInfo1Y%!' .. end_loc
        end
        operation.end_wh_code = end_loc.wh_code
        operation.end_area_code = end_loc.area_code
        operation.end_loc_code = end_loc.code
        ext_table.roadway = end_loc.roadway
        goto continue
    end
 
    if (is_trj == 1 and material.is_insulate == 'Y') then
        -- ·ƒÖSïSeQ“^÷]S
        local b = wms_base.Get_sConst(strLuaDEID, "sO-ÝO)n÷]S")
        local str = findCommonNumbers(ext_table.roadway_inventory, b)
        if (str == nil or str == '') then
            return 2, "¡l    gïSeQ“^„vÝO)n÷]S!"
        end
        ext_table.roadway_inventory = str
        operation.op_def_name = "ÝO)nPg™eeQ“^"
        -- ·ƒÖS\ONÈ~¹pMOn
        nRet, end_loc = BW_StorageTactics(strLuaDEID, ext_table)
        if (nRet ~= 0) then
            return 2, "¡‹—{È~¹p1Y%ÿ" .. end_loc
        end
        if (end_loc == nil or end_loc == '') then
            return 2, "Ëz“^-N¡l    gán³agöN„v'MOïSåNRM‘!"
        end
        -- ·ƒÖSÈ~¹páOo`
        nRet, end_loc = wms_wh.GetLocInfo(end_loc)
        if (nRet ~= 0) then
            return 2, 'WMS_GetLocInfo1Y%!' .. end_loc
        end
        operation.end_wh_code = end_loc.wh_code
        operation.end_area_code = end_loc.area_code
        operation.end_loc_code = end_loc.code
        ext_table.roadway = end_loc.roadway
        goto continue
    end
    if (item_type == '‰|™e') then
        operation.op_def_name = "‰|™eeQ“^"
        ext_table.isothermal = material.isothermal -- /f&TR`)n ÿ(ueg$R­e‰|™e/f&TeQR`)n“^
        ext_table.area_code = material.area_code -- “^:S ÿ(ueg$R­e‰|™eeQ“^ãS„vMOn
 
        -- GT_Area_Type W[xQ Tðy 1,2,3 „vD– R<PR+Rù[”^N‰|™e“^0‰‰|™e“^0‰|™eÔk‹Oô~¤b    NÍy
        local dictItem
        nRet, dictItem = mobox.getDictItemIInfo("GT_Area_Type")
        if (nRet ~= 0) then
            return 2, dictItem
        end
        dictItem = json.decode(dictItem)
        local loc_code, d_ratio, x_ratio
        local strCondition, area
        for m = 1, #dictItem do
            -- sOWMS O„v“^:SŒTW[xQš[IN„v<PøvI{R$R­eW[xQ Tðy/fêT*NÈ~¹p“^:S
            if (material.area_code == dictItem[m].value) then
                if (tonumber(dictItem[m].name) == 1) then
                    if (material.isothermal == 'Y') then
                        area = "HWPFL"
                    else
                        area = "PFL"
                    end
                elseif (tonumber(dictItem[m].name) == 2) then
                    if (material.isothermal == 'Y') then
                        area = "HWTFL"
                    else
                        area = "TFL"
                    end
                else
                    -- sOWMS¡l    gcš[N‰úQ“^ãS ÿRÇ‰|™eÔk‹Oô~¤beg$R­eeQ“^ãS
                    -- Øž¤‹Ôk‹O:N5:5
                    d_ratio = 5
                    x_ratio = 5
 
                    -- ·ƒÖSN‰‰|™e“^å‹'ÁT„vpeϑ
                    local d_count, x_count, count, d_num, x_num, num
                    strCondition = "S_CNTR_CODE IN (SELECT S_CNTR_CODE FROM TN_CG_Detail WHERE S_ITEM_CODE = '" ..
                                       item_code .. "')"
                    strCondition = strCondition ..
                                       " AND S_LOC_CODE IN (SELECT S_CODE FROM TN_Location WHERE S_AREA_CODE IN( 'PFL','HWPFL'))"
                    nRet, d_count = mobox.getDataObjCount(strLuaDEID, "Loc_Container", strCondition)
                    if (nRet ~= 0) then
                        return 2, d_count
                    end
                    -- ·ƒÖSck(WgbLˆ-N„vûN¡Rpeϑ
                    strCondition = "S_CNTR_CODE IN (SELECT S_CNTR_CODE FROM TN_CG_Detail WHERE S_ITEM_CODE = '" ..
                                       item_code .. "')"
                    strCondition = strCondition ..
                                       " AND S_END_LOC IN (SELECT S_CODE FROM TN_Location WHERE S_AREA_CODE IN( 'PFL','HWPFL'))"
                    nRet, num = mobox.getDataObjCount(strLuaDEID, "Operation", strCondition)
                    if (nRet ~= 0) then
                        return 2, num
                    end
                    if (tonumber(d_count) ~= nil) then
                        if (tonumber(num) ~= nil) then
                            d_count = tonumber(d_count) + tonumber(num)
                        end
                    else
                        if (tonumber(num) ~= nil) then
                            d_count = tonumber(num)
                        end
                    end
 
                    strCondition = "S_CNTR_CODE IN (SELECT S_CNTR_CODE FROM TN_CG_Detail WHERE S_ITEM_CODE = '" ..
                                       item_code .. "')"
                    strCondition = strCondition ..
                                       " AND S_LOC_CODE IN (SELECT S_CODE FROM TN_Location WHERE S_AREA_CODE IN( 'TFL','HWTFL'))"
                    nRet, x_count = mobox.getDataObjCount(strLuaDEID, "Loc_Container", strCondition)
                    if (nRet ~= 0) then
                        return 2, x_count
                    end
                    -- ·ƒÖSck(WgbLˆ-N„vûN¡Rpeϑ
                    strCondition = "S_CNTR_CODE IN (SELECT S_CNTR_CODE FROM TN_CG_Detail WHERE S_ITEM_CODE = '" ..
                                       item_code .. "')"
                    strCondition = strCondition ..
                                       " AND S_END_LOC IN (SELECT S_CODE FROM TN_Location WHERE S_AREA_CODE IN( 'TFL','HWTFL'))"
                    nRet, num = mobox.getDataObjCount(strLuaDEID, "Operation", strCondition)
                    if (nRet ~= 0) then
                        return 2, num
                    end
                    if (tonumber(x_count) ~= nil) then
                        if (tonumber(num) ~= nil) then
                            x_count = tonumber(x_count) + tonumber(num)
                        end
                    else
                        if (tonumber(num) ~= nil) then
                            x_count = tonumber(num)
                        end
                    end
 
                    -- N‰‰|™e“^@b    gå‹ir™ex„v;`pe
                    count = tonumber(d_count) + tonumber(x_count)
                    -- ¡‹—{N‰‰|™e“^`SÔk
                    if (count ~= 0) then
                        d_num = round(tonumber(d_count) / tonumber(count) * 10) -- N‰|™e“^`SÔk
                        x_num = round(tonumber(x_count) / tonumber(count) * 10) -- ‰‰|™e“^`SÔk
                    else
                        d_num = 0
                        x_num = 0
                    end
 
                    if (material.isothermal == 'Y') then
                        if (d_ratio >= d_num) then
                            area = "HWPFL"
                        end
 
                        if (x_ratio >= x_num) then
                            area = "HWTFL"
                        end
                    else
                        if (d_ratio >= d_num) then
                            area = "PFL"
                        end
 
                        if (x_ratio >= x_num) then
                            area = "TFL"
                        end
                    end
                end
            end
        end
        -- ågâ‹agöN:N“^:S:Ncš[“^:S„vzz'MO
        strCondition = "S_AREA_CODE = '" .. area .. "' AND N_CURRENT_NUM = 0 AND N_LOCK_STATE = 0 AND C_ENABLE = 'Y'"
        -- 9hncÈ~¹p“^:S·ƒÖSÈ~¹p'MO     cgq’cRB\’c^
        local strOrder = "N_ROW,N_COL,N_LAYER"
        -- gY·ƒÖS 10 ag
        nRet, strRetInfo = mobox.queryDataObjAttr3(strLuaDEID, "Location", strCondition, 10, strOrder)
        if (nRet ~= 0) then
            return 2, "·ƒÖS'MOáOo`•ï‹! " .. strRetInfo .. " SQLagöN: " .. strCondition
        end
 
        local loc
        if (strRetInfo ~= '') then
            local retObjs = json.decode(strRetInfo)
            nRet, loc = m3.ObjAttrStrToLuaObj("Location", lua.table2str(retObjs[1].attrs))
            if (nRet ~= 0) then
                return 2, "m3.ObjAttrStrToLuaObj(Location) 1Y%! " .. loc
            end
        else
            return 2, "¡l    gågâ‹0RïSeQ“^„vzz'MO!"
        end
 
        -- ·ƒÖSÈ~¹páOo`
        nRet, end_loc = wms_wh.GetLocInfo(loc.code)
        if (nRet ~= 0) then
            return 2, 'WMS_GetLocInfo1Y%!' .. end_loc
        end
        operation.end_wh_code = end_loc.wh_code
        operation.end_area_code = end_loc.area_code
        operation.end_loc_code = end_loc.code
    elseif (item_type == '¢”N') then
        local b = wms_base.Get_sConst(strLuaDEID, "sO-^—ÝO)n÷]S")
        local str = findCommonNumbers(ext_table.roadway_inventory, b)
        if (str == nil or str == '') then
            return 2, "¡l    gïSeQ“^„v^—ÝO)n÷]S!"
        end
        ext_table.roadway_inventory = str
        operation.op_def_name = "Ëz“^eQ“^"
        -- ·ƒÖSÈ~¹páOo`
        nRet, end_loc = GS_StorageTactics(strLuaDEID, ext_table)
        if (nRet ~= 0) then
            return 2, "¡‹—{È~¹p1Y%ÿ" .. end_loc
        end
        if (end_loc == nil or end_loc == '') then
            return 2, "Ëz“^-N¡l    gán³agöN„v'MOïSåNRM‘!"
        end
        -- ·ƒÖSÈ~¹páOo`
        nRet, end_loc = wms_wh.GetLocInfo(end_loc)
        if (nRet ~= 0) then
            return 2, 'WMS_GetLocInfo1Y%!' .. end_loc
        end
        operation.end_wh_code = end_loc.wh_code
        operation.end_area_code = end_loc.area_code
        operation.end_loc_code = end_loc.code
        ext_table.roadway = end_loc.roadway
    else
        operation.op_def_name = "Ëz“^eQ“^"
        -- ·ƒÖSÈ~¹páOo`
        nRet, end_loc = JL_StorageTactics(strLuaDEID, ext_table)
        if (nRet ~= 0) then
            return 2, "¡‹—{È~¹p1Y%ÿ" .. end_loc
        end
        if (end_loc == nil or end_loc == '') then
            return 2, "Ëz“^-N¡l    gán³agöN„v'MOïSåNRM‘!"
        end
        -- ·ƒÖSÈ~¹páOo`
        nRet, end_loc = wms_wh.GetLocInfo(end_loc)
        if (nRet ~= 0) then
            return 2, 'WMS_GetLocInfo1Y%!' .. end_loc
        end
        operation.end_wh_code = end_loc.wh_code
        operation.end_area_code = end_loc.area_code
        operation.end_loc_code = end_loc.code
        ext_table.roadway = end_loc.roadway
    end
 
    ::continue::
    lua.Debug(strLuaDEID, debug.getinfo(1), 'operation', operation)
    local strCode
    local strHeader = 'TA' .. os.date("%y%m%d") .. '-'
    nRet, strCode = mobox.getSerialNumber("ûN¡R", strHeader, 5)
    if (nRet ~= 0) then
        return 2, '3u÷‹0ûN¡R0x1Y%!' .. strCode
    end
    ext_table.task_no = strCode
    operation.ext_data = lua.table2str(ext_table)
    operation.cntr_code = cntr_code
    lua.Debug(strLuaDEID, debug.getinfo(1), "end_loc", end_loc)
    nRet, operation = m3.CreateDataObj(strLuaDEID, operation)
    if (nRet ~= 0) then
        return 2, 'Rú^0\ON01Y%!' .. operation
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), "\ONRú^TáOo`", operation)
 
    -- È~¹p R•Íd\O(w¹p:NÙz¹p    gWCS¡{§c)
    nRet, strRetInfo = wms.wms_LockLocation(strLuaDEID, operation.end_loc_code,
        wms_base.Get_nConst(strLuaDEID, "•{|‹W-eQ“^•"), ext_table.task_no, operation.code, operation.op_def_name)
    if (nRet ~= 0) then
        return 2, "wms_LockLocation 1Y%!" .. strRetInfo
    end
    return 0, ''
end
 
--[[
    x: GT-100-01
     Tðy: WCSeQ“^ÞVŒ
    \O€ÿLZH
    eQãSýQpeÿ WCSCallback
 
 
    ŸRý€ô‹f:
        Ç OÂS„v÷‹Bl{|‹W$R­e 1 = 'ireQ“^/“^ÿ2 = XbØvÄ~eQ“^ÿ3 = RFIDXbØvåg⋠ÿ4 = |TëSzzXbÿ
 
        Yt;‘
        -- step1 ·ƒÖS¥cãSpenc
        -- step2 ÀhŒšÂSpe
        -- step3 ·ƒÖSå]MOw¹páOo`
        -- step4 9hnc N T„v{|‹WZP N T„vÍd\O 1='ireQ“^/“^ÿ2=XbØvÄ~eQ“^ÿ3=RFIDXbØvåg⋠ÿ4 = |TëSzzXb
 
 
        “eQpencÿ
        {
            "cntr_code": "106EGRCA1078", -- ¹[hV÷S
            "station_no": "A",          -- å]MO
            "qty":0,                    -- peϑ
            "req_no": "11111111",       -- GUID/UNhƋ
            "req_type": "1",            -- ÷‹Bl{|‹W 1='ireQ“^/“^ÿ2=XbØvÄ~eQ“^ÿ3=RFIDXbØvåg⋠ÿ4 = |TëSzzXb
            "weight": "1",            -- Í‘Ï‘
            "req_time": "2024-12-11"    -- ÷‹Blöeô•
        }                        
 
 
    ØSôf°‹U_:
    v1.1 lzh 20250421 žX R¥cãS÷‹BlYt ÿÝOÁ‹(W TN*NÙz¹pÑSÐgN*N÷‹Bl{|‹W ÿgbLˆŒ[b¥b•MR NO    g+R„v¿~ zegYt
--]]
require("WMS-Equipment")
wms_cntr = require("wms_container")
wms_wh = require("wms_wh")
require("GT-Base")
require("GT_InAndOutboundPolicies")
function WCSCallback(strLuaDEID)
    local nRet, strRetInfo, in_date, sn_result, serialRet, label_card, pwgw_Ret, pvRet, pv_Restult, item_code,
        condition, msg
    local ext_table = {} -- \ONibU\ÂSpe
 
    -- step1 ·ƒÖS¥cãSpenc
    nRet, in_date = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "WCStoreCallback àeÕl·ƒÖSpencS!" .. in_date)
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), 'in_date', in_date)
    -- (uØSϑK<P
    local cntr_code = in_date.cntr_code
    local station_no = in_date.station_no
    local qty = tonumber(in_date.qty)
    local req_no = in_date.req_no -- /UNx
    local req_type = tonumber(in_date.req_type) -- ÷‹Bl{|‹W 1='ireQ“^/“^ÿ2=XbØvÄ~eQ“^ÿ3=RFIDXbØvågâ‹
    local req_time = in_date.req_time -- ÷‹Blöeô•
    local weight = in_date.weight -- 
N¥b„v͑ϑ
 
    -- step2 ¥cãSÂSpe    gHe'`ŒšÁ‹
    if (station_no == '') then
        lua.Error(strLuaDEID, debug.getinfo(1), "å]MO Ný€:Nzz!")
    end
    if (req_no == '') then
        lua.Error(strLuaDEID, debug.getinfo(1), "÷‹Blx Ný€:Nzz!")
    end
    if (req_type > 4 or req_type < 1) then
        lua.Error(strLuaDEID, debug.getinfo(1), "eQ“^{|‹W N/ec!")
    end
 
    -- ‚Yœg¹[hV(W\ON-NX[(WgbLˆ¶r`RúQ
    condition = "S_CNTR_CODE = '" .. cntr_code .. "' AND N_B_STATE IN( 0,1,3)"
    nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Operation", condition)
    if (nRet == 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "¹[hVò]«ˆ`S(u!")
    end
 
    -- step3 ·ƒÖSw¹páOo`
    local loc_start = wms_base.Get_sConst(strLuaDEID, station_no)
    nRet, loc_start = wms_wh.GetLocInfo(loc_start)
 
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), 'WMS_GetLocInfo1Y%!' .. loc_start)
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), 'loc_start', loc_start)
 
    -- step4 9hnc N T„v{|‹WZP N T„vÍd\O 1='ireQ“^/“^ÿ2=XbØvÄ~eQ“^ÿ3=RFIDXbØvåg⋠ÿ4 = |TëSzzXb
    if (req_type == 1) then
        lua.Debug(strLuaDEID, debug.getinfo(1), 'in_date', in_date)
        -- ŒšÁ‹¹[hV
        if (cntr_code == '') then
            lua.Error(strLuaDEID, debug.getinfo(1), "¹[hV Ný€:Nzz!")
        end
 
        -- ¹[hV    gHe'`ŒšÁ‹
        local container
        nRet, container = wms_cntr.GetInfo(strLuaDEID, cntr_code)
 
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), "·ƒÖS0¹[hV0áOo`1Y%! " .. container)
        end
        if (container == '') then
            lua.Error(strLuaDEID, debug.getinfo(1), "0¹[hV0" .. cntr_code .. " NX[(W!")
        end
 
        -- åg⋐™eUS agöNÿ¹[hVøv Tv^N¶r`:N‘ nRet == 1R™eUS:Nzzpck8^eQ“^;‘ ÿX[(W™eUSRp™eAm z
        local rom
        condition = "S_CNTR_CODE = '" .. cntr_code .. "' AND S_STATE IN( '‘','gbLˆ')"
        nRet, rom = m3.GetDataObjByCondition(strLuaDEID, "GT_ROM", condition)
        lua.Debug(strLuaDEID, debug.getinfo(1), 'rom', rom)
        if (nRet == 1) then
            -- $R­e/f&T/fzzXb,zzXbRpzzXbeQ“^Am z
            local is_kgz
            condition = "S_CNTR_CODE = '" .. cntr_code .. "' AND S_ITEM_CODE = 'KGZ'"
            nRet, is_kgz = m3.GetDataObjByCondition(strLuaDEID, "CG_Detail", condition)
            if (nRet == 1) then
                -- ‚Yœg N/fzzXbç~í~pck8^eQ“^Am z
                goto KGZ
            elseif (nRet == 0) then
                nRet, strRetInfo = StockInquiry(strLuaDEID)
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), strRetInfo)
                end
                ext_table.roadway_inventory = strRetInfo -- \ONibU\pencX[eQُ*N÷]S“^X[W[&{2N ÿ̑b—/f“^X[*g…Ç80%„v÷]S
 
                if (loc_start.code == wms_base.Get_sConst(strLuaDEID, "KL_ReturnRgvStation1") or loc_start.code ==
                    wms_base.Get_sConst(strLuaDEID, "F3L_InWeight1") or loc_start.code ==
                    wms_base.Get_sConst(strLuaDEID, "F3R_InWeight1")) then
                    local b = wms_base.Get_sConst(strLuaDEID, "sO-N|iæ]§OÞV“^ãS÷]S")
                    local str = findCommonNumbers(ext_table.roadway_inventory, b)
                    if (str == nil or str == '') then
                        lua.Error(strLuaDEID, debug.getinfo(1), "¡l    gïSeQ“^„v3,4÷]S!")
                    end
                    ext_table.roadway_inventory = str
                end
 
                -- Rú^\ON
                local operation = m3.AllocObject(strLuaDEID, "Operation")
                operation.start_wh_code = loc_start.wh_code
                operation.start_area_code = loc_start.area_code
                operation.start_loc_code = loc_start.code
                operation.op_type = wms_base.Get_nConst(strLuaDEID, "\ON{|‹W-eQ“^")
                operation.op_def_name = "ýVêzzXbÞV“^"
                operation.cntr_code = cntr_code
                operation.ext_data = lua.table2str(ext_table)
                nRet, operation = m3.CreateDataObj(strLuaDEID, operation)
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), 'Rú^0\ON01Y%!' .. operation)
                end
                goto BREAK -- pzzXbeQ“^Rú^\ONTóÇ Nb—„vgbLˆ;‘
            end
            ::KGZ::
 
            -- ·ƒÖSWCSÙz¹pÑ~š[„vå]MO
            condition = "S_START_LOC = '" .. loc_start.code .. "'"
            nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "GT_PDA_Station", condition)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "·ƒÖS0å]MO0áOo`1Y%! " .. strRetInfo)
            end
            station_no = strRetInfo.station
            lua.Debug(strLuaDEID, debug.getinfo(1), 'station_no', station_no)
 
            -- $R­eeQ“^ÙzðS/f&T    g/T¨R¶r`„vň±{å]US
            local strCondition = "S_STATION = '" .. station_no .. "' AND S_STATE = '/T(u'"
            local strOrder = "T_CREATE desc"
            local packing
            nRet, packing = m3.GetDataObjByCondition(strLuaDEID, "GT_Packing_Order", strCondition, strOrder)
            -- ¡l    gň±{å]USpºNå]Ñ~š[Am z
            if (nRet == 1) then
                -- pºNå]Ñ~š[Íd\O
                pwgw_Ret, label_card = ManualBindGeneralOperation(strLuaDEID, cntr_code)
                if (pwgw_Ret ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), 'ºNå]Ñ~š[Íd\O1Y%!' .. label_card)
                end
                -- pÅˆ±{å]USAm z
            elseif (nRet == 0) then
                -- local palletizing = packing.palletizing --lishiming 4Nöe(u ÿxØvĉRTí~ÎNň±{å]UShˆÌ‘ôv¥cÿb
                local palletizing = "1"
                local dispersoid = packing.dispersoid -- lishiming 4Nöe(u ÿceňö€hƋ Tí~ÎNň±{å]UShˆ-Nÿb
 
                -- :ghVºNň±{å]MO ÿ—‰ê¨RubAm4l÷S ÿ N—‰Åˆ±{!hŒš
                if (dispersoid == 'Y') then
                    -- ·ƒÖSeQ“^Am4l÷S
                    serialRet, sn_result = RobotBoxing(strLuaDEID, packing.order_no, qty, palletizing)
                    -- ·ƒÖS1Y% ÿ¥b•Ðc:y
                    if (serialRet ~= 0) then
                        lua.Error(strLuaDEID, debug.getinfo(1), 'ceňö€·ƒÖSeQ“^USLˆpenc1Y%!' .. sn_result)
                    end
                    -- pÅˆ±{å]US(uÍd\O
                    pwgw_Ret, label_card = PackWorkGeneralOperation(strLuaDEID, sn_result, cntr_code, packing.pack_no,
                        packing.order_no, dispersoid)
                    -- ·ƒÖS1Y% ÿ¥b•Ðc:y
                    if (pwgw_Ret ~= 0) then
                        lua.Error(strLuaDEID, debug.getinfo(1), 'ň±{å]USÍd\O1Y%!' .. label_card)
                    end
                else
                    -- ·ƒÖSň±{!hŒš_sQ/f&T_/T
                    local is_zxjy = wms_base.Get_sConst(strLuaDEID, "sO-ň±{!hŒš_sQ")
                    if (is_zxjy == 'Y') then
                        -- Åˆ±{!hŒš ÿbŸRԏÞVAm4l÷S
                        pvRet, pv_Restult = PackVerification(strLuaDEID, station_no)
                        -- ·ƒÖS1Y% ÿ¥b•Ðc:y
                        if (pvRet ~= 0) then
                            lua.Error(strLuaDEID, debug.getinfo(1), 'ň±{!hŒš1Y%!' .. pv_Restult)
                        end
                        sn_result = pv_Restult
                    else
                        -- ^—ň±{!hŒš ÿ·ƒÖSAm4l÷S
                        -- ·ƒÖS*gÑ~š[¹[hV„veQ“^US„vAm4l÷SۏLˆ’c^,ÖS,{N*NŒT¹[hVÑ~š[
                        local order_by = "S_SERIAL_NO"
                        strCondition =
                            " NOT EXISTS (SELECT S_SERIAL_NO FROM TN_CG_Detail WHERE TN_CG_Detail.S_SERIAL_NO = tn_GT_Label_Crad.S_SERIAL_NO) AND S_ORDER_NO = '" ..
                                packing.order_no .. "'"
                        pvRet, pv_Restult =
                            m3.GetDataObjByCondition(strLuaDEID, "GT_Label_Crad", strCondition, order_by)
                        if (pvRet ~= 0) then
                            lua.Error(strLuaDEID, debug.getinfo(1), '·ƒÖS*gÑ~š[„vAm4l÷S1Y%!' .. pv_Restult)
                        end
                        -- îvƉaSúW,g^\'`$R­e
                        if (pv_Restult.qty <= 0) then
                            lua.Error(strLuaDEID, debug.getinfo(1),
                                "îvƉaS'" .. pv_Restult.serial_no .. "'„vpeϑÅ_{˜    g<P Ný€/fpe!")
                        end
                        sn_result = pv_Restult.serial_no
                    end
 
                    -- pÅˆ±{å]US(uÍd\O
                    pwgw_Ret, label_card = PackWorkGeneralOperation(strLuaDEID, sn_result, cntr_code, packing.pack_no,
                        packing.order_no, dispersoid)
                    -- ·ƒÖS1Y% ÿ¥b•Ðc:y
                    if (pwgw_Ret ~= 0) then
                        lua.Error(strLuaDEID, debug.getinfo(1), 'ň±{å]USÍd\O1Y%!' .. label_card)
                    end
                end
                -- ·ƒÖS1Y% ÿ¥b•Ðc:y
            elseif (nRet == 2) then
                lua.Error(strLuaDEID, debug.getinfo(1), 'm3.GetDataObjByCondition 1Y%!' .. packing)
            end
 
            -- 9hncAm4l÷Sôf°eeQ“^USncLˆ„vðy͑áOo`
            strCondition = "S_SERIAL_NO = '" .. label_card.serial_no .. "'"
            local strUpdateSql = "S_WEIGHT = '" .. weight .. "'"
            nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_Label_Crad", strCondition, strUpdateSql)
            if (nRet ~= 0) then
                return 2, "ôf°e0eQ“^USncLˆ0áOo`1Y%!" .. strRetInfo
            end
 
            -- Rú^ù[”^„v\ON
            nRet, strRetInfo = CreateOperation(strLuaDEID, label_card.item_code, label_card.batch_no, loc_start,
                label_card.inbound_policy, ext_table, cntr_code)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), 'Rú^\ON1Y%!' .. strRetInfo)
            end
        elseif (nRet == 0) then
            -- $R­e™e;‘/f&TZP†N¹[hV'ÁTÑ~š[
            local cg_detail
            condition = "S_CNTR_CODE = '" .. cntr_code .. "'"
            nRet, cg_detail = m3.GetDataObjByCondition(strLuaDEID, "CG_Detail", condition)
            if (nRet == 1) then
                lua.Error(strLuaDEID, debug.getinfo(1), "XbØvir™e*gÑ~š[!")
            elseif (nRet > 1) then
                lua.Error(strLuaDEID, debug.getinfo(1), "GetDataObjByCondition1Y%!" .. cg_detail)
            end
            lua.Debug(strLuaDEID, debug.getinfo(1), 'cg_detail', cg_detail)
 
            local item_type, material
            nRet, item_type, material = GT_Get_ItemType(strLuaDEID, cg_detail.item_code)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), item_type)
            end
            lua.Debug(strLuaDEID, debug.getinfo(1), 'ir™eáOo`', material)
 
            if (item_type == '³xў') then
                lua.Error(strLuaDEID, debug.getinfo(1), "³xў NïSeQËz“^!")
            end
 
            nRet, strRetInfo = CreateOperation(strLuaDEID, cg_detail.item_code, cg_detail.batch_no, loc_start,
                rom.inbound_policy, ext_table, cntr_code)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), 'Rú^\ON1Y%!' .. strRetInfo)
            end
 
            -- 9hnc ¹[hV+¶r` ôf°eeQ“^USncLˆ„vðy͑áOo`ŒT¶r`
            local strSetAttr = "S_STATE = 'gbLˆ',S_WEIGHT = '" .. weight .. "' "
            condition = "S_CNTR_CODE = '" .. cntr_code .. "' AND S_STATE <> 'Œ[b'"
            nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_ROM", condition, strSetAttr)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "ôf°e0™eUS0¶r`1Y%!" .. strRetInfo)
            end
        else
            lua.Error(strLuaDEID, debug.getinfo(1), "GetDataObjByCondition1Y%!" .. rom)
        end
 
        ::BREAK::
        -- req_type = 2 XbØvÄ~eQ“^Am z
    elseif (req_type == 2) then
        if (cntr_code == '') then
            lua.Error(strLuaDEID, debug.getinfo(1), "RFID Ný€:Nzz!")
        end
        -- !hŒš¹[hV/f&Tò]Ñ~š['MO
        condition = "S_CNTR_CODE = '" .. cntr_code .. "'"
        nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Loc_Container", condition)
        if (nRet == 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), '¹[hVò]Ñ~š[' .. strRetInfo.loc_code .. ''MO!')
        end
 
        if (qty == nil or qty == 0) then
            strCondition = "S_CNTR_CODE = '" .. cntr_code .. "'"
            nRet, cg_detail = m3.QueryDataObject(strLuaDEID, "CG_Detail", strCondition)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "QueryDataObject1Y%!" .. cg_detail)
            end
            qty = cg_detail.qty
        else
            -- Ñ~š[zzXbir™e ir™ex = KGZ zzå]ň ãNhˆÙ*N¹[hV/fzzXb,¹[hV'ÁTfÆ~„vpeϑ„vUSMO:NXb
            local cg_detail = m3.AllocObject(strLuaDEID, "CG_Detail")
            cg_detail.cntr_code = cntr_code
            cg_detail.item_code = "KGZ"
            cg_detail.item_name = "zzå]ň"
            cg_detail.qty = qty
            cg_detail.uom = '*N'
            nRet, cg_detail = m3.CreateDataObj(strLuaDEID, cg_detail)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), 'Rú^0¹[hV'ÁTfÆ~0ù[aŒ1Y%!' .. cg_detail)
            end
        end
 
        nRet, strRetInfo = StockInquiry(strLuaDEID)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), strRetInfo)
        end
        ext_table.roadway_inventory = strRetInfo -- \ONibU\pencX[eQُ*N÷]S“^X[W[&{2N ÿ̑b—/f“^X[*g…Ç80%„v÷]S
 
        if (loc_start.code == wms_base.Get_sConst(strLuaDEID, "KL_ReturnRgvStation1") or loc_start.code ==
            wms_base.Get_sConst(strLuaDEID, "F3L_InWeight1") or loc_start.code ==
            wms_base.Get_sConst(strLuaDEID, "F3R_InWeight1")) then
            local b = wms_base.Get_sConst(strLuaDEID, "sO-N|iæ]§OÞV“^ãS÷]S")
            local str = findCommonNumbers(ext_table.roadway_inventory, b)
            if (str == nil or str == '') then
                lua.Error(strLuaDEID, debug.getinfo(1), "¡l    gïSeQ“^„v3,4÷]S!")
            end
            ext_table.roadway_inventory = str
        end
 
        -- $R­e'MO¹[hV/f&TÑ~š[,¡l    gÑ~š[RÑ~š[
        local loc_code = wms_wh.GetLocCodeByCNTR(strLuaDEID, cntr_code)
        if (loc_code == '') then
            nRet, strRetInfo = wms_wh.Loc_Container_Binding(strLuaDEID, loc_start.code, cntr_code,
                "Ñ~š[ã‰Ñ~¹eÕl-û|ß~", "Œ[b")
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), ''MO¹[hVã‰Ñ~1Y%!' .. strRetInfo)
            end
        end
        -- Rú^XbØvÄ~eQ“^\ON
        -- Rú^\ON
        local operation = m3.AllocObject(strLuaDEID, "Operation")
        operation.start_wh_code = loc_start.wh_code
        operation.start_area_code = loc_start.area_code
        operation.start_loc_code = loc_start.code
        operation.op_type = wms_base.Get_nConst(strLuaDEID, "\ON{|‹W-eQ“^")
        operation.op_def_name = "ýVêzzXbÞV“^"
        operation.cntr_code = cntr_code
        operation.ext_data = lua.table2str(ext_table)
        nRet, operation = m3.CreateDataObj(strLuaDEID, operation)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), 'Rú^0\ON01Y%!' .. operation)
        end
        lua.Debug(strLuaDEID, debug.getinfo(1), "\ONRú^TáOo`", operation)
 
        -- req_type = 3 RFIDXbØvågâ‹Am z
    elseif (req_type == 3) then
        if (cntr_code == '') then
            lua.Error(strLuaDEID, debug.getinfo(1), "RFID Ný€:Nzz!")
        end
        -- RFIDXbØvåg⋠9hnc¹[hV·ƒÖS CG_Detail
        -- 1 US*NXbØvÿۏxØv:g    ÿ 2 XbØvÄ~ÿºNå]
N™e„vXbØvÄ~    ÿ 3 ir™eÿ÷‹Bl{|‹W=3RFIDXbØvågâ‹öe—ԏÞV    ÿ
        local data = {}
        local cg_detail
        condition = "S_CNTR_CODE = '" .. cntr_code .. "'"
        nRet, cg_detail = m3.GetDataObjByCondition(strLuaDEID, "CG_Detail", condition)
        if (nRet ~= 0 and nRet ~= 1) then
            lua.Error(strLuaDEID, debug.getinfo(1), "·ƒÖS0¹[hV'ÁTfÆ~0áOo`1Y%! " .. cg_detail)
        end
        if (nRet == 1) then
            -- üb¥cԏÞV„vpenc
            data.mat_type = 1
            mobox.returnValue(strLuaDEID, 1, lua.table2str(data))
            return
        elseif (nRet == 0) then
            if (cg_detail.item_code == 'KGZ') then
                data.mat_type = 2
                mobox.returnValue(strLuaDEID, 1, lua.table2str(data))
                return
            else
                data.mat_type = 3
                mobox.returnValue(strLuaDEID, 1, lua.table2str(data))
                return
            end
        end
        -- req_type = 4|TëSzzXbAm z
    elseif (req_type == 4) then
        -- ·ƒÖS*g»QÓ~„v÷]S
        local strCondition = "S_AREA_CODE = 'LK' AND N_LOCK_STATE = 0"
        local nRet, roadway_list = m3.QueryDataObject(strLuaDEID, "Roadway", strCondition)
        lua.Debug(strLuaDEID, debug.getinfo(1), "roadway_list", roadway_list)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), 'ågâ‹÷]SáOo`1Y%' .. roadway_list)
        end
        if (roadway_list == nil or roadway_list == '') then
            lua.Error(strLuaDEID, debug.getinfo(1), '¡l    gïSúQ“^„v÷]S!')
        end
        local str = ''
        for i = 1, #roadway_list do
            local attrs = roadway_list[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)
        -- ·ƒÖSïSúQ“^÷]S
        if (loc_start.code == wms_base.Get_sConst(strLuaDEID, "KL_ReturnRgvStation1") or loc_start.code ==
            wms_base.Get_sConst(strLuaDEID, "F3L_InWeight1") or loc_start.code ==
            wms_base.Get_sConst(strLuaDEID, "F3R_InWeight1")) then
            local b = wms_base.Get_sConst(strLuaDEID, "sO-N|iæ]§OÞV“^ãS÷]S")
            str = findCommonNumbers(str, b)
        end
 
        roadway_list = lua.split(str, ",")
 
        local data
        local loc_Container = {}
        local flag = 0
        for i = 1, #roadway_list do
            local roadway = roadway_list[i] -- ÷]S
 
            -- ·ƒÖSå‹÷]S„v zz'±{/XbØvÄ~ OHQYñmMOvQ!k…QñmMO
            local str_cntr
            if (station_no == 'KQ_KTPPullCache1') then
                str_cntr = '106EGP'
            else
                str_cntr = '106EGR' -- ö€FhMR6*NW[&{ XbØv/f106EGP
            end
 
            -- ·ƒÖSS_MR÷]SáOo`
            strCondition = "N_ROADWAY = '" .. roadway .. "'"
            nRet, roadway = m3.GetDataObjByCondition(strLuaDEID, "Roadway", strCondition)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "·ƒÖS0÷]S0áOo`1Y%! " .. roadway)
            end
 
            local strOrder = "T_CREATE"
 
            -- ·ƒÖSYñmMO„vzzå]ň
            strCondition =
                "S_LOC_CODE IN (SELECT S_CODE FROM TN_Location WHERE S_AREA_CODE = 'LK' AND N_LOCK_STATE = 0 AND C_ENABLE = 'Y' AND N_POS = 1 AND N_ROADWAY = '" ..
                    roadway.roadway .. "')"
            strCondition = strCondition ..
                               " AND S_CNTR_CODE IN (SELECT S_CNTR_CODE FROM TN_CG_Detail WHERE S_ITEM_CODE = 'KGZ' AND S_CNTR_CODE LIKE '%" ..
                               str_cntr .. "%')"
            -- gY·ƒÖS 10 ag
            nRet, strRetInfo = mobox.queryDataObjAttr3(strLuaDEID, "Loc_Container", strCondition, 1, strOrder)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1),
                    "·ƒÖS'MOáOo`•ï‹! " .. strRetInfo .. " SQLagöN: " .. strCondition)
            end
            if (strRetInfo ~= '') then
                local attrs
                local retObjs = json.decode(strRetInfo)
                nRet, attrs = m3.ObjAttrStrToLuaObj("Loc_Container", lua.table2str(retObjs[1].attrs))
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "m3.ObjAttrStrToLuaObj(CG_Detail) 1Y%! " .. attrs)
                end
                data = {
                    loc_code = attrs.loc_code,
                    cntr_code = attrs.cntr_code,
                    roadway = roadway.roadway
                }
                break
            end
 
            -- ·ƒÖS…QñmMO„vzzå]ň
            strCondition =
                "S_LOC_CODE IN (SELECT S_CODE FROM TN_Location WHERE S_AREA_CODE = 'LK' AND N_LOCK_STATE = 0 AND C_ENABLE = 'Y' AND N_POS = 2 AND N_ROADWAY = '" ..
                    roadway.roadway .. "')"
            strCondition = strCondition ..
                               " AND S_CNTR_CODE IN (SELECT S_CNTR_CODE FROM TN_CG_Detail WHERE S_ITEM_CODE = 'KGZ' AND S_CNTR_CODE LIKE '%" ..
                               str_cntr .. "%')"
            -- gY·ƒÖS 10 ag
            nRet, strRetInfo = mobox.queryDataObjAttr3(strLuaDEID, "Loc_Container", strCondition, 1, strOrder)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1),
                    "·ƒÖS'MOáOo`•ï‹! " .. strRetInfo .. " SQLagöN: " .. strCondition)
            end
            if (strRetInfo ~= '') then
                local attrs
                local retObjs = json.decode(strRetInfo)
                for j = 1, #retObjs do
                    flag = tonumber(flag) + 1
 
                    nRet, attrs = m3.ObjAttrStrToLuaObj("Loc_Container", lua.table2str(retObjs[j].attrs))
                    if (nRet ~= 0) then
                        lua.Error(strLuaDEID, debug.getinfo(1), "m3.ObjAttrStrToLuaObj(CG_Detail) 1Y%! " .. attrs)
                    end
 
                    loc_Container[flag] = attrs
                end
            end
        end
 
        local start_loc
        if (data ~= nil) then
            lua.Debug(strLuaDEID, debug.getinfo(1), "data", data)
            cntr_code = data.cntr_code
            start_loc = data.loc_code
        elseif (tonumber(#loc_Container) > tonumber(0)) then
            lua.Debug(strLuaDEID, debug.getinfo(1), "loc_Container", loc_Container)
            for i = 1, #loc_Container do
                -- …QñmMO!hŒš
                local success, loc_code = GetPosLoc(strLuaDEID, loc_Container[i].loc_code)
                if (success == true) then
                    -- $R­e…QñmMO/f&T    g'ir
                    strCondition = "S_CODE = '" .. loc_code .. "' AND (N_LOCK_STATE = 0 OR N_CURRENT_NUM = 1)"
                    nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Location", strCondition)
                    if (nRet == 2) then
                        lua.Error(strLuaDEID, debug.getinfo(1), "m3.GetDataObjByCondition 1Y%!" .. strRetInfo)
                    elseif (nRet == 1) then
                        strCondition = "S_CODE = '" .. loc_code .. "' AND N_LOCK_STATE = 0"
                        nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Location", strCondition)
                        if (nRet == 2) then
                            lua.Error(strLuaDEID, debug.getinfo(1), "m3.GetDataObjByCondition 1Y%!" .. strRetInfo)
                        elseif (nRet == 1) then
                            goto coroutine
                        end
 
                        -- Rú^ûy“^ûN¡R
                        -- ·ƒÖSw¹páOo`
                        local start_loc_yk, end_loc_yk
                        nRet, start_loc_yk = wms_wh.GetLocInfo(loc_code)
                        if (nRet ~= 0) then
                            lua.Error(strLuaDEID, debug.getinfo(1), 'WMS_GetLocInfo1Y%!' .. start_loc_yk)
                        end
 
                        -- ·ƒÖSw¹p„vir™e
                        local cg_detail
                        condition = "S_CNTR_CODE ='" .. loc_Container[i].cntr_code .. "'"
                        nRet, cg_detail = m3.GetDataObjByCondition(strLuaDEID, "CG_Detail", condition)
                        if (nRet ~= 0) then
                            lua.Error(strLuaDEID, debug.getinfo(1), cg_detail)
                        end
                        -- ·ƒÖSir™e{|‹W
                        local item_type, material
                        nRet, item_type, material = GT_Get_ItemType(strLuaDEID, cg_detail.item_code)
                        if (nRet ~= 0) then
                            lua.Error(strLuaDEID, debug.getinfo(1), item_type)
                        end
 
                        -- ¢”NêSý€eQcš[B\
                        local str_yk
                        if (item_type == '¢”N') then
                            str_yk = wms_base.Get_sConst(strLuaDEID, "sO-Ëz“^¢”N'MOB\")
                            nRet, end_loc_yk = GetEndLoc(strLuaDEID, start_loc_yk.roadway, str_yk)
                            if (nRet ~= 0) then
                                lua.Error(strLuaDEID, debug.getinfo(1), end_loc)
                            end
                        else
                            str_yk = wms_base.Get_sConst(strLuaDEID, "sO-Ëz“^^—¢”N'MOB\")
                            nRet, end_loc_yk = GetEndLoc(strLuaDEID, start_loc_yk.roadway, str_yk)
                            if (nRet == 1) then
                                str_yk = wms_base.Get_sConst(strLuaDEID, "sO-Ëz“^¢”N'MOB\")
                                nRet, end_loc_yk = GetEndLoc(strLuaDEID, start_loc_yk.roadway, str_yk)
                                if (nRet ~= 0) then
                                    lua.Error(strLuaDEID, debug.getinfo(1), end_loc)
                                end
                            elseif (nRet > 1) then
                                lua.Error(strLuaDEID, debug.getinfo(1), end_loc)
                            end
                        end
 
                        -- Rú^sOûN¡R
                        local gt_task = m3.AllocObject(strLuaDEID, "GT_Task")
                        lua.Debug(strLuaDEID, debug.getinfo(1), 'gt_task', gt_task)
                        -- w¹páOo`
                        gt_task.start_wh_code = start_loc_yk.wh_code
                        gt_task.start_area_code = start_loc_yk.area_code
                        gt_task.start_loc_code = start_loc_yk.code
                        -- È~¹páOo`
                        gt_task.end_wh_code = end_loc_yk.wh_code
                        gt_task.end_area_code = end_loc_yk.area_code
                        gt_task.end_loc_code = end_loc_yk.code
                        gt_task.type = 3 -- 1 ‰|™eŒèb,dЏ 2 Øpö€,dЏ 3  T÷]Sûy“^
                        gt_task.priority = 2 -- 'Y„vOHQ
                        gt_task.cntr_code = loc_Container[i].cntr_code
                        nRet, gt_task = m3.CreateDataObj(strLuaDEID, gt_task)
                        if (nRet ~= 0) then
                            lua.Error(strLuaDEID, debug.getinfo(1), 'Rú^0sOûN¡R01Y%!' .. gt_task)
                        end
                    end
 
                    lua.Debug(strLuaDEID, debug.getinfo(1), "loc_Container[i].loc_code", loc_Container[i].loc_code)
                    cntr_code = loc_Container[i].cntr_code
                    start_loc = loc_Container[i].loc_code
                    break
                end
 
                ::coroutine::
            end
            if (start_loc == nil or start_loc == '') then
                lua.Error(strLuaDEID, debug.getinfo(1), '÷]S¡l    gïSúQ“^„vzzå]ň!')
            end
        else
            lua.Error(strLuaDEID, debug.getinfo(1), '÷]S¡l    gïSúQ“^„vzzå]ň!')
        end
        nRet, start_loc = wms_wh.GetLocInfo(start_loc)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), 'WMS_GetLocInfo1Y%!' .. start_loc)
        end
 
        -- step4: Rú^,dЏ\ON
        local operation = m3.AllocObject(strLuaDEID, "Operation")
        operation.start_wh_code = start_loc.wh_code
        operation.start_area_code = start_loc.area_code
        operation.start_loc_code = start_loc.code
 
        -- È~¹p/fsOWMS OǏeg„v
        operation.end_wh_code = loc_start.wh_code
        operation.end_area_code = loc_start.area_code
        operation.end_loc_code = loc_start.code
 
        local strCode
        local strHeader = 'TA' .. os.date("%y%m%d") .. '-'
        nRet, strCode = mobox.getSerialNumber("ûN¡R", strHeader, 5)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), '3u÷‹0ûN¡R0x1Y%!' .. strCode)
        end
        ext_table.task_no = strCode
        operation.ext_data = lua.table2str(ext_table)
 
        operation.cntr_code = cntr_code
        operation.op_def_name = "|TëSzzXb"
        operation.op_type = wms_base.Get_nConst(strLuaDEID, "\ON{|‹W-úQ“^")
        nRet, operation = m3.CreateDataObj(strLuaDEID, operation)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), 'Rú^0\ON01Y%!' .. operation)
        end
 
        -- w¹p R•Íd\O
        nRet, strRetInfo = wms.wms_LockLocation(strLuaDEID, operation.start_loc_code,
            wms_base.Get_nConst(strLuaDEID, "•{|‹W-úQ“^•"), strCode, operation.code, operation.op_def_name)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), "wms_LockLocation 1Y%!" .. strRetInfo)
        end
    end
end