wsz
2025-06-10 4be5e6f4ae34bcb7cb47f05816421459bbfaedf1
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
--[[
    版本:     Version 2.1
    创建日期: 2025-1-28
    创建人:   HAN
 
    WMS-Basis-Model-Version: V15.5
 
    功能:
        WMS 过程中一些常用功能封装
 
        -- Get_sConst 获取常量返回字符串类型
        -- Get_nConst 获取常量返回数值类型
        -- GetDictItemName 获取字典项的附加值,输入的 nItemValue 必须是数值
        -- Warning 创建一条WMS警告信息
        -- GetFuncArea 获取功能区定义列表
        -- GetWHCGLocByRule 得到仓库中指定货品或空托的货位
        -- Add_WHAreaQty_ByCGDetail 通过CG_Detail增加仓库量表
        -- Add_WHAreaQty_By_PAC_Detail 通过 Pre_Alloc_CNTR_Detail 增加仓库量表
--]]
 
json  = require ("json")
mobox = require ("OILua_JavelinExt")
wms   = require ("OILua_WMS")
lua   = require ("oi_base_func")
m3    = require ("oi_base_mobox")
 
local wms_base = {_version = "0.2.1"}
 
-- 容器类型
WMS_CNTR_TYPE_NAME = {
    ["Pallet"] = "托盘",
    ["Normal"] = "常规",
    ["Cell_Box"] = "料格",
    ["Picking_Box"] = "拣料箱"
}
WMS_CNTR_TYPE = {
    ["托盘"] = "Pallet",
    ["常规"] = "Normal",
    ["料格"] = "Cell_Box",
    ["P拣料箱"] = "icking_Box"
}
 
--//////////////////////////////////////////////////////常量相关///////////////////////////////////////////////////////////
-- 获取常量返回字符串类型 (建议用这个函数)
function wms_base.Get_sConst2( strLuaDEID, strConstName )
    local nRet, strValue
    if ( strConstName == nil or strConstName == '' ) then return 1, "Get_sConst 参数必须有2个,并且第二个参数不能为空字符串!" end
    strConstName = lua.trim(strConstName)
    nRet, strValue = wms.wms_GetConst(strConstName)
    if (nRet ~= 0 ) then return 2, strValue end
    return 0, strValue
end
 
-- 如果没有返回一个巨大的值(不建议使用)
function wms_base.Get_nConst( strLuaDEID, strConstName )
    local nRet, strValue
 
    if ( strConstName == nil or strConstName == '' ) then return 2^53 end
    strConstName = lua.trim(strConstName)
    nRet, strValue = wms.wms_GetConst(strConstName)
    if (nRet ~= 0 ) then return 2^53 end
    return lua.StrToNumber( strValue )
end
 
-- 获取常量返回数值类型
function wms_base.Get_nConst2( strLuaDEID, strConstName )
    local nRet, strValue
 
    if ( strConstName == nil or strConstName == '' ) then return 1, "Get_nConst 参数必须有2个,并且第二个参数不能为空字符串!" end
    strConstName = lua.trim(strConstName)
    nRet, strValue = wms.wms_GetConst(strConstName)
    if (nRet ~= 0 ) then return 2, strValue end
    return 0, lua.StrToNumber( strValue )
end
 
-- 获取常量返回bool类型
function wms_base.Get_bConst2( strLuaDEID, strConstName )
    local nRet, strValue
 
    if ( strConstName == nil or strConstName == '' ) then return 1, "Get_nConst 参数必须有2个,并且第二个参数不能为空字符串!" end
    strConstName = lua.trim(strConstName)
    nRet, strValue = wms.wms_GetConst(strConstName)
    if (nRet ~= 0 ) then return 2, strValue end
    if ( strValue == '1' or strValue == 'Y' or strValue == 'y' ) then
        return 0, true
    end
    return 0, false
end
 
-- 获取字典项的附加值,输入的 nItemValue 必须是数值
function wms_base.GetDictItemName( strLuaDEID, strDictName, nItemValue )
    local str_item_name = ''
    local nRet
 
    if ( nItemValue == nil ) then 
        lua.Error( strLuaDEID, 0, "wms_base.GetDictItemName 的输入参数 nItemValue 必须有值!请检查 wms_base.GetDictItemName 函数的输入参数!" ) 
    end
    if ( type(nItemValue) ~= "number" ) then 
        lua.Warning( strLuaDEID, 0, "wms_base.GetDictItemName 的输入参数 nItemValue 必须要是一个数值类型!" )
        return ""
    end
 
    nRet, str_item_name = wms.wms_GetDictTypeName( strDictName, nItemValue ) 
    if  ( nRet ~= 0 ) then 
        lua.Warning( strLuaDEID, 0, strDictName.."不存在或者字典定义中不存在值 = "..nItemValue.." 的字典项!" )
        return ""
    end
    return str_item_name
end
 
--[[ 
    Warning 是未来方便使用者了解系统执行情况的
    nErrCode -- 错误码 
    strInfo -- 错误信息
    factory -- 工厂
    strFrom -- 来源
    nLvl -- 错误等级
    strExtData -- 扩展数据
    strNote -- 备注
--]]
function wms_base.Warning( strLuaDEID, nLvl, nErrCode, strMsg, strExtData, strNote, strFrom, factory )
    local nRet, strRetInfo, strErr
 
     if ( factory == nil or factory == "") then factory = "0000" end
     if ( strFrom == nil or strFrom == "") then strFrom = "Unkonw" end 
     if ( strExtData == nil ) then strExtData = "" end   
     if ( strNote == nil ) then strNote = "" end 
 
    local warning = m3.AllocObject(strLuaDEID,"WMS_Warning")
    warning.factory = factory
    warning.msg = strMsg
    warning.from = strFrom
    warning.lvl = nLvl
    warning.err_code = nErrCode
    warning.ext_data = strExtData
    warning.note = strNote
 
    -- 获取创建容器时需要的属性
    nRet, warning = m3.CreateDataObj( strLuaDEID, warning )
    if ( nRet ~= 0 ) then
        lua.Error( strLuaDEID, debug.getinfo(1), "创建 wms_base.Warning 失败! "..warning )
    end
end
 
--[[ 仓库货品数量变化处理
        -- strChangeOp 库存变化动作
        -- strItemInfo 货品信息
--]]
function wms_base.InventoryChangeAction( strLuaDEID, strChangeOp,  strItemInfo )
    local nRet, strRetInfo, change_op
    
    if ( type(strChangeOp) == "string" ) then
        change_op = wms_base.Get_nConst(strLuaDEID, strChangeOp)
    else
        change_op = strChangeOp
    end
    nRet, strRetInfo = wms.wms_AddWHInventoryChange(strLuaDEID, change_op, strItemInfo )
    if ( nRet ~= 0 ) then 
        return 2, "wms_AddWHInventoryChange 失败! "..strRetInfo  
    end    
    return 0
end
 
--[[
    获取位置(Postion)相关的功能区定义列表,这里的位置是一个比较宽泛的范围,库区,货位,仓库,逻辑库区
    参数:
        strPosClsID    位置类型:Area、Zone、Warehouse、Location
        strPosCode     位置编码
        strFuncType    功能类型
    返回
        -- nRet  非零失败
        -- func_pos  功能区/位列表对象,是一个table类型
                     [{"type":0~2, "class":"Zone", "code":"xxx"}]
--]]
function wms_base.GetFuncArea( strLuaDEID, strPosClsID, strPosCode, strFuncType )
    local nRet, strRetInfo, nFuncType
 
    if ( type(strFuncType) == "string") then
        nFuncType = wms_base.Get_nConst(strLuaDEID, strFuncType)
    else
        nFuncType = strFuncType
    end
    nRet, strRetInfo = wms.wms_GetFuncArea( strPosClsID, strPosCode, nFuncType )
    if (nRet ~= 0) then return 1, "wms_GetFuncArea 失败!"..strRetInfo  end
    if ( strRetInfo == '' ) then 
        return 1, strPosClsID.."编码 = '"..strPosCode.."' 没有定义类型值 = "..nFuncType.."名称 = '"..strFuncType.."'的功能区"  
    end
    local success, ret_pos
    success, ret_pos = pcall( json.decode, strRetInfo )
    if ( success == false ) then return 1, "wms_GetFuncArea返回值为非法的JSON格式!"..ret_pos end
    return 0, ret_pos
end
 
function wms_base.Get_Warehouse_FuncArea( strLuaDEID, strWHCode, strFuncType )
    local nRet, strRetInfo
    nRet, strRetInfo = wms_base.GetFuncArea( strLuaDEID, "Warehouse", strWHCode, strFuncType )
    if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), "wms_base.GetFuncArea 失败!"..strRetInfo ) end
    return strRetInfo
end
 
function wms_base.Get_Area_FuncArea( strLuaDEID, strAreaCode, strFuncType )
    local nRet, strRetInfo
    nRet, strRetInfo = wms_base.GetFuncArea( strLuaDEID, "Area", strAreaCode, strFuncType )
    if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), "wms_base.GetFuncArea 失败!"..strRetInfo ) end
    return strRetInfo
end
 
function wms_base.Get_Zone_FuncArea( strLuaDEID, strZoneCode, strFuncType )
    local nRet, strRetInfo
    nRet, strRetInfo = wms_base.GetFuncArea( strLuaDEID, "Zone", strZoneCode, strFuncType )
    if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), "wms_base.GetFuncArea 失败!"..strRetInfo ) end
    return strRetInfo
end
 
function wms_base.Get_Loc_FuncArea( strLuaDEID, strLocCode, strFuncType )
    local nRet, strRetInfo
    nRet, strRetInfo = wms_base.GetFuncArea( strLuaDEID, "Location", strLocCode, strFuncType )
    if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), "wms_base.GetFuncArea 失败!"..strRetInfo ) end
    return strRetInfo
end
 
--[[
    获取逻辑库区中可用的货位
    参数:
        strZoneCode
    返回
        -- nRet  0 成功
        -- location 货位对象,是一个table类型
        {"wh_code":"XXX",  "area_code":"XXX",  "loc_code":"XX",  "agv_site":"XXX",  "agv_site_layer": X  }
--]]
function wms_base.GetZoneAvaliableLoc( strLuaDEID, strZoneCode, strExtInfo  )
    local nRet, strRetInfo
    if (strExtInfo == nil) then strExtInfo = '' end
 
    nRet, strRetInfo = wms.wms_GetZoneAvaliableLoc( strLuaDEID, strZoneCode, strExtInfo )
    if (nRet ~= 0) then return 1, "wms_GetZoneAvaliableLoc 失败!"..strRetInfo  end
    if ( strRetInfo == '' ) then  return  0, "" end
    local success, ret_info
    success, ret_info = pcall( json.decode, strRetInfo )
    if ( success == false ) then return 1, "wms_GetZoneAvaliableLoc 返回值为非法的JSON格式!"..ret_info end
    return 0, ret_info
end
 
--[[
    获取库区中可用的货位
    参数:
        strAreaCode
--]]
function wms_base.GetAreaAvaliableLoc( strLuaDEID, strAreaCode, strExtInfo )
    local nRet, strRetInfo
    if (strExtInfo == nil) then strExtInfo = '' end
 
    nRet, strRetInfo = wms.wms_GetAreaAvaliableLoc( strLuaDEID, strAreaCode, strExtInfo )
    if (nRet ~= 0) then return 1, "wms_GetAreaAvaliableLoc 失败! 物理库区:'"..strAreaCode.."'  "..strRetInfo  end
    if ( strRetInfo == '' ) then  return  0, "" end
    local success, ret_info
    success, ret_info = pcall( json.decode, strRetInfo )
    if ( success == false ) then return 1, "wms_GetAreaAvaliableLoc 返回值为非法的JSON格式!"..ret_info end
    return 0, ret_info
end
 
--[[
    得到仓库中指定货品或空托的货位对象
--]]
function wms_base.GetWHCGLocByRule( strLuaDEID, strWHCode, strPolicyName, strExtInfo, bIsEmptyCntr  )
    local nRet, strRetInfo
    if ( bIsEmptyCntr == nil ) then bIsEmptyCntr = 0 end
 
    nRet, strRetInfo = wms.wms_GetWHCGLocByRule( strLuaDEID, strWHCode, strPolicyName, strExtInfo, bIsEmptyCntr )
    if ( nRet ~= 0 ) then return nRet, "wms_GetWHCGLocByRule 失败!"..strRetInfo end
    if ( strRetInfo == '' ) then  return 0, "" end
    local success, ret_info
    success, ret_info = pcall( json.decode, strRetInfo )
    if ( success == false ) then return 1, "wms_GetWHCGLocByRule 返回值为非法的JSON格式!"..ret_info end
    return 0, ret_info    
end
 
--[[
    得到仓库库区中指定货品或空托的货位
--]]
function wms_base.GetAreaCGLoc( strLuaDEID, strAreaCode, strExtInfo, bIsEmptyCntr )
    local nRet, strRetInfo
    if ( bIsEmptyCntr == nil ) then bIsEmptyCntr = 0 end
 
    nRet, strRetInfo = wms.wms_GetAreaCGLoc( strLuaDEID, strAreaCode, strExtInfo, bIsEmptyCntr )
    if ( nRet ~= 0 ) then return nRet, "wms_base.GetAreaCGLoc 失败!"..strRetInfo end
    if ( strRetInfo == '' ) then  return 0, "" end
    local success, ret_info
    success, ret_info = pcall( json.decode, strRetInfo )
    if ( success == false ) then return 1, "WGetAreaCGLoc 返回值为非法的JSON格式!"..ret_info end
    return 0, ret_info    
end
 
-- 根据CG_Detail生成量表变化信息
local function get_inventorychg_bycgdetail( strLuaDEID, wh_code, area_code, cntr_code )
    local nRet, strRetInfo
 
    if ( cntr_code == nil or cntr_code == '' ) then return 1, "get_inventorychg_bycgdetail 函数中容器编码必须有值!" end
    if ( wh_code == '' or wh_code == nil ) then 
        return 1, "get_inventorychg_bycgdetail 函数中仓库编码必须有值!"
    end
    if ( area_code == nil ) then area_code = '' end
 
    -- 获取 CG_Detail
    local strOrder = ''
    local strCondition = "S_CNTR_CODE = '"..cntr_code.."'"
    
    nRet, strRetInfo = mobox.queryDataObjAttr( strLuaDEID, "CG_Detail", strCondition, strOrder )
    if ( nRet ~= 0 ) then return 1, "获取【容器货品明细】失败! "..strRetInfo  end
    if ( strRetInfo == '' ) then return 0, "" end
 
    local retObjs = json.decode( strRetInfo )
    local nObjs =  #retObjs    
    local n
    local cg_detail = {}
    local inventory_list = {}
 
    for n = 1, nObjs do
        nRet, cg_detail = m3.ObjAttrStrToLuaObj( "CG_Detail", lua.table2str(retObjs[n].attrs) )
        if ( nRet ~= 0 ) then return 1, "m3.ObjAttrStrToLuaObj(CG_Detail) 失败! "..cg_detail end
 
        -- 生成仓库量变化输入参数
        --[[
        inventory_info = '{"item_code": "'..cg_detail.item_code..'",'
        inventory_info = inventory_info..'"item_name": "'..lua.FormatJsonString(cg_detail.item_name)..'",'
        inventory_info = inventory_info..'"qty": "'..cg_detail.qty..'"'..chg_target..'}'
        inventory_change = inventory_change .. inventory_info..","
        ]]
        local inventory = {
            item_code = cg_detail.item_code,
            item_name = cg_detail.item_name,
            qty = cg_detail.qty,
            wh_code = wh_code,
            area_code = area_code
        }
        table.insert( inventory_list, inventory )
    end    
 
    return 0, lua.table2str(inventory_list)
end
 
-- 根据CG_Detail生成量表变化信息
local function create_on_shelves_by_cgdetail( strLuaDEID, wh_code, area_code, cntr_code, loc_code )
    local nRet, strRetInfo
 
    if ( cntr_code == nil or cntr_code == '' ) then return 1, "create_on_shelves_by_cgdetail  函数中容器编码必须有值!" end
    if ( loc_code == nil ) then loc_code = '' end
 
    if ( wh_code == '' or wh_code == nil ) then 
        return 1, "create_on_shelves_by_cgdetail 函数中仓库编码必须有值!" 
    end
    if ( area_code == nil ) then area_code = '' end
 
    -- 获取 CG_Detail
    local strOrder = ''
    local strCondition = "S_CNTR_CODE = '"..cntr_code.."'"
    
    nRet, strRetInfo = mobox.queryDataObjAttr( strLuaDEID, "CG_Detail", strCondition, strOrder )
    if ( nRet ~= 0 ) then return  1, "获取【容器货品明细】失败! "..strRetInfo end
    if ( strRetInfo == '' ) then return 0, "" end
 
    local retObjs = json.decode( strRetInfo )
    local nObjs =  #retObjs    
    local n
    local cg_detail = {}
    local days = os.date("%Y%m%d")
    local inventory_list = {}
 
    for n = 1, nObjs do
        nRet, cg_detail = m3.ObjAttrStrToLuaObj( "CG_Detail", lua.table2str(retObjs[n].attrs) )
        if ( nRet ~= 0 ) then return 1, "m3.ObjAttrStrToLuaObj(CG_Detail) 失败! "..cg_detail end
 
        -- 生成仓库量变化输入参数
        --[[
        inventory_info = '{"item_code": "'..cg_detail.item_code..'",'
        inventory_info = inventory_info..'"item_name": "'..lua.FormatJsonString(cg_detail.item_name)..'",'
        inventory_info = inventory_info..'"qty": "'..cg_detail.qty..'"'..chg_target..'}'
        inventory_change = inventory_change .. inventory_info..","
        ]]
        local inventory = {
            item_code = cg_detail.item_code,
            item_name = cg_detail.item_name,
            qty = cg_detail.qty,
            wh_code = wh_code,
            area_code = area_code
        }
        table.insert( inventory_list, inventory )
        if ( loc_code ~= '' ) then
            -- 生成上架记录【OnOff_Shelves】
            onoff_shelves = m3.AllocObject(strLuaDEID,"OnOff_Shelves")
            onoff_shelves.action = "+"
            onoff_shelves.d_action = days
 
            onoff_shelves.wh_code = wh_code
            onoff_shelves.area_code = area_code
            onoff_shelves.loc_code = loc_code
            
            onoff_shelves.item_code = cg_detail.item_code
            onoff_shelves.item_name = cg_detail.item_name  
            onoff_shelves.batch_no = cg_detail.batch_no
            onoff_shelves.serial_no = cg_detail.serial_no
            onoff_shelves.item_spec = cg_detail.item_spec
            onoff_shelves.item_state_name = cg_detail.item_state_name
            onoff_shelves.item_state = cg_detail.item_state
 
            onoff_shelves.end_user = cg_detail.end_user
            onoff_shelves.owner = cg_detail.owner
            onoff_shelves.supplier = cg_detail.supplier
            onoff_shelves.supplier_name = cg_detail.supplier_name        
 
            onoff_shelves.cntr_code = cg_detail.cntr_code
            onoff_shelves.cell_no = cg_detail.cell_no
 
            onoff_shelves.qty = cg_detail.qty
            onoff_shelves.uom = cg_detail.uom
 
            onoff_shelves.bs_no = cg_detail.bs_no
            onoff_shelves.bs_type = cg_detail.bs_type        
        
            nRet, onoff_shelves = m3.CreateDataObj( strLuaDEID, onoff_shelves )
            if ( nRet ~= 0 ) then 
                return 1, 'mobox 创建【上下架记录】对象失败!'..onoff_shelves
            end    
 
            -- 根据业务来源加 F_ACC_I_QTY/累计入库数量
            if ( cg_detail.bs_type == "Inbound_Order" ) then
                if ( cg_detail.bs_no ~= nil and cg_detail.bs_no ~= '' ) then
                    strCondition = "S_IO_NO = '"..cg_detail.bs_no.."' AND S_ITEM_CODE = '"..cg_detail.item_code.."'"
                    local strSetAttr = "F_ACC_I_QTY = F_ACC_I_QTY +"..cg_detail.qty
                    nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "Inbound_Detail", strCondition, strSetAttr )
                    if (nRet ~= 0) then return 1, "设置【Inbound_Detail】累计入库数量失败!"..strRetInfo end
                end
            end
            if ( cg_detail.bs_type == 'Inbound_Wave' ) then
                if ( cg_detail.bs_no ~= nil and cg_detail.bs_no ~= '' ) then
                    strCondition = "S_WAVE_NO = '"..cg_detail.bs_no.."' AND S_ITEM_CODE = '"..cg_detail.item_code.."'"
                    local strSetAttr = "F_ACC_I_QTY = F_ACC_I_QTY +"..cg_detail.qty
                    nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "IW_Detail", strCondition, strSetAttr )
                    if (nRet ~= 0) then return 1, "设置【IW_Detail】累计入库数量失败!"..strRetInfo end                
                end
            end               
        end
    end    
    return 0, lua.table2str( inventory_list )
end
 
--[[ 通过CG_Detail增加仓库/库区量表 ]]
function wms_base.Add_WHAreaQty_ByCGDetail( strLuaDEID, wh_code, area_code, cntr_code, loc_code )
    local nRet, strRetInfo, inventory_change
 
    nRet, inventory_change = create_on_shelves_by_cgdetail( strLuaDEID, wh_code, area_code, cntr_code, loc_code )
    if ( nRet ~= 0 ) then return 1, inventory_change end
    if ( loc_code ~= nil and loc_code ~= '' ) then
        -- 创建上架记录
    end
 
    if ( inventory_change ~= '' ) then
        if ( wh_code ~= '' and wh_code ~= nil ) then
            -- 5 仓库量表-仓库加存储量
            nRet, strRetInfo = wms.wms_AddWHInventoryChange(strLuaDEID, 5, inventory_change )
            if ( nRet ~= 0 ) then return 1, "wms_AddWHInventoryChange 失败! "..strRetInfo end
        end
 
        if ( area_code ~= '' and area_code ~= nil ) then
            -- 6 仓库量表-库区加存储量
            nRet, strRetInfo = wms.wms_AddWHInventoryChange(strLuaDEID, 6, inventory_change)
            if ( nRet ~= 0 ) then return 1, "wms_AddWHInventoryChange 失败! "..strRetInfo end
        end        
    end   
    return 0 
end
 
-- 从【Pre_Alloc_CNTR_Detail】获取存储量新增参数并且创建 上下架记录
-- pac_no 组盘号
local function create_on_shelves_by_pac( strLuaDEID, wh_code, area_code, loc_code, pac_no )
    local nRet, strRetInfo
 
    if ( pac_no == nil or pac_no == '' ) then 
        return 1, "get_Inventory_chg_by_orgc 函数中 pac_no 必须有值!"
    end
 
    local chg_target = ''       -- 量表变化对象
    if ( wh_code == '' or wh_code == nil ) then 
        return 1, "get_Inventory_chg_by_orgc 函数中仓库编码必须有值!"
    end
    if ( area_code == nil ) then area_code = '' end
    if ( loc_code == nil ) then loc_code = '' end
 
    -- 获取 Pre_Alloc_CNTR_Detail
    local strOrder = ''
    local strCondition = "S_PAC_NO = '"..pac_no.."'"
 
    nRet, strRetInfo = mobox.queryDataObjAttr( strLuaDEID, "Pre_Alloc_CNTR_Detail", strCondition, strOrder )
    if ( nRet ~= 0 ) then return 1, "获取【组盘明细】失败! "..strRetInfo end
    if ( strRetInfo == '' ) then 
        lua.Warning( strLuaDEID, debug.getinfo(1), "create_on_shelves_by_pac 存在异常 组盘号'"..pac_no.."'的明细为空!" )
        return 0, "" 
    end
 
    local retObjs = json.decode( strRetInfo )
    local n
    local inventory_change = {}
    local pac_detail = {}
    local onoff_shelves = {}    -- 上下架记录
    local days = os.date("%Y%m%d")
 
    for n = 1, #retObjs do
        nRet, pac_detail = m3.ObjAttrStrToLuaObj( "Pre_Alloc_CNTR_Detail", lua.table2str(retObjs[n].attrs) )
        if ( nRet ~= 0 ) then return 1, "m3.ObjAttrStrToLuaObj(Pre_Alloc_CNTR_Detail) 失败! "..pac_detail end
 
        if ( pac_detail.act_qty > 0 ) then
            -- 生成仓库量变化输入参数
            local inventory_info = {
                item_code = pac_detail.item_code,
                item_name = pac_detail.item_name,
                qty = pac_detail.act_qty,
                wh_code = wh_code,
                area_code = area_code
            }
            table.insert( inventory_change, inventory_info )
 
            -- 生成上架记录【OnOff_Shelves】
            onoff_shelves = m3.AllocObject(strLuaDEID,"OnOff_Shelves")
            onoff_shelves.action = "+"
            onoff_shelves.d_action = days
 
            onoff_shelves.wh_code = wh_code
            onoff_shelves.area_code = area_code
            onoff_shelves.loc_code = loc_code
            
            onoff_shelves.item_code = pac_detail.item_code
            onoff_shelves.item_name = pac_detail.item_name  
            onoff_shelves.batch_no = pac_detail.batch_no
            onoff_shelves.serial_no = pac_detail.serial_no
            onoff_shelves.item_spec = pac_detail.item_spec
            onoff_shelves.item_state_name = pac_detail.item_state_name
            onoff_shelves.item_state = pac_detail.item_state
 
            onoff_shelves.end_user = pac_detail.end_user
            onoff_shelves.owner = pac_detail.owner
            onoff_shelves.supplier = pac_detail.supplier
            onoff_shelves.supplier_name = pac_detail.supplier_name        
 
            onoff_shelves.cntr_code = pac_detail.cntr_code
            onoff_shelves.cell_no = pac_detail.cell_no
 
            onoff_shelves.qty = pac_detail.act_qty
            onoff_shelves.uom = pac_detail.uom
 
            onoff_shelves.bs_no = pac_detail.bs_no
            onoff_shelves.bs_type = pac_detail.bs_type        
        
            nRet, onoff_shelves = m3.CreateDataObj( strLuaDEID, onoff_shelves )
            if ( nRet ~= 0 ) then 
                return 1, 'mobox 创建【上下架记录】对象失败!'..onoff_shelves
            end    
        end
        -- 根据业务来源加 F_ACC_I_QTY/累计入库数量
        if ( pac_detail.bs_type == "Inbound_Order" ) then
            if ( pac_detail.bs_no ~= nil and pac_detail.bs_no ~= '' ) then
                strCondition = "S_IO_NO = '"..pac_detail.bs_no.."' AND S_ITEM_CODE = '"..pac_detail.item_code.."'"
                local strSetAttr = "F_ACC_I_QTY = F_ACC_I_QTY +"..pac_detail.act_qty
                if ( lua.equation( 0, pac_detail.cancel_qty ) == false ) then
                    strSetAttr = strSetAttr..", F_ACC_C_QTY = F_ACC_C_QTY + "..pac_detail.cancel_qty
                end
                nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "Inbound_Detail", strCondition, strSetAttr )
                if (nRet ~= 0) then return 1, "设置【Inbound_Detail】累计入库数量失败!"..strRetInfo end
            end
        end
        if ( pac_detail.bs_type == 'Inbound_Wave' ) then
            if ( pac_detail.bs_no ~= nil and pac_detail.bs_no ~= '' ) then
                strCondition = "S_WAVE_NO = '"..pac_detail.bs_no.."' AND S_ITEM_CODE = '"..pac_detail.item_code.."'"
                local strSetAttr = "F_ACC_I_QTY = F_ACC_I_QTY +"..pac_detail.act_qty
                if ( lua.equation( 0, pac_detail.cancel_qty ) == false ) then
                    strSetAttr = strSetAttr..", F_ACC_C_QTY = F_ACC_C_QTY + "..pac_detail.cancel_qty
                end
                nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "IW_Detail", strCondition, strSetAttr )
                if (nRet ~= 0) then return 1, "设置【IW_Detail】累计入库数量失败!"..strRetInfo end                
            end
        end        
    end    
    return 0, inventory_change
end
 
--[[ 
    通过 Pre_Alloc_CNTR_Detail 增加仓库/库区量表 
    pac_no -- 组盘号
]]
function wms_base.Add_WHAreaQty_By_PAC_Detail( strLuaDEID, wh_code, area_code, loc_code, pac_no )
    local nRet, strRetInfo, inventory_change
 
    nRet, inventory_change = create_on_shelves_by_pac( strLuaDEID, wh_code, area_code, loc_code,  pac_no )
    if ( nRet ~= 0 ) then return nRet, inventory_change end
    --[[
    if ( #inventory_change > 0 ) then
        local str_inventory_change = lua.table2str(inventory_change)
 
        if ( wh_code ~= '' and wh_code ~= nil ) then
            -- 5 仓库量表-仓库加存储量
            nRet, strRetInfo = wms.wms_AddWHInventoryChange(strLuaDEID, 5, str_inventory_change )
            if ( nRet ~= 0 ) then return 1, "wms_AddWHInventoryChange 失败! "..strRetInfo end
        end
 
        if ( area_code ~= '' and area_code ~= nil ) then
            -- 6 仓库量表-库区加存储量
            nRet, strRetInfo = wms.wms_AddWHInventoryChange(strLuaDEID, 6, str_inventory_change )
            if ( nRet ~= 0 ) then return 1, "wms_AddWHInventoryChange 失败! "..strRetInfo end
        end        
    end 
    ]] 
    return  0  
end
 
function wms_base.Reduce_WHAreaQty_ByCGDetail( strLuaDEID, wh_code, area_code, cntr_code )
    local nRet, strRetInfo, inventory_change
    nRet, inventory_change = get_inventorychg_bycgdetail( strLuaDEID, wh_code, area_code, cntr_code )
    if  ( nRet ~= 0 ) then return nRet, inventory_change end
 
    if ( inventory_change ~= '' ) then
        if ( wh_code ~= '' and wh_code ~= nil ) then
            nRet, strRetInfo = wms.wms_AddWHInventoryChange(strLuaDEID, 8, inventory_change)
            if ( nRet ~= 0 ) then return 1, "wms_AddWHInventoryChange 失败! "..strRetInfo end
        end
 
        if ( area_code ~= '' and area_code ~= nil ) then
            nRet, strRetInfo = wms.wms_AddWHInventoryChange(strLuaDEID, 9, inventory_change)
            if ( nRet ~= 0 ) then return 1, "wms_AddWHInventoryChange 失败! "..strRetInfo end
        end        
    end
    return 0    
end
 
--[[只对一个货品计算分配量,仓库量表\库区量表加分配量 ]]
function wms_base.IPA_Star( wh_code, area_code, strItemInfo, strAddKey, bForceSorting )
    local nRet, strRetInfo
    local nForceSorting = 0
 
    if ( wh_code == nil or wh_code == '' ) then 
        return 1, "wms_base.IPA_Star 函数中 wh_code 必须有值!"
    end
    if ( area_code == nil or area_code == '' ) then 
        return 1, "wms_base.IPA_Star 函数中 area_code 必须有值!" 
    end
    if (bForceSorting) then nForceSorting = 1 end
 
    nRet, strRetInfo = wms.wms_IPA_Start( wh_code, area_code, "", strItemInfo, strAddKey, nForceSorting )
    if ( nRet ~= 0 ) then return nRet, "wms_IPA_Start失败! "..strRetInfo end
    local success, ret_info
    success, ret_info = pcall( json.decode, strRetInfo )
    if ( success == false ) then return 1, "wms_IPA_Start 返回值为非法的JSON格式!"..ret_info end
    return 0, ret_info       
end
 
----IPA_Star2 内部调用函数------------------------------------------------------------------------------------------
local function get_oos_info( item_info, oos_info_set )
    local n
 
    for n = 1, #oos_info_set do
        if ( item_info.N_ROW_NO == oos_info_set[n].N_ROW_NO and
             item_info.S_ITEM_CODE == oos_info_set[n].S_ITEM_CODE ) then
                return oos_info_set[n].F_OOS_QTY
            end
    end
    return 0
end
 
-- 把 from中的信息复制到 target
local function copy_item_info( target, from )
    target.N_ROW_NO = from.N_ROW_NO
    target.S_ITEM_CODE = from.S_ITEM_CODE
    target.S_ITEM_NAME = from.S_ITEM_NAME
    target.F_QTY = from.F_QTY
end
 
--[[ 
    和IPA_Star不同这个是对多个货品进行分配
    仓库量表\库区量表加分配量 对一个 item_set 的物料进行批分,根据 wms_IPA_Start 的结果休整 root_item_set 的分配量
        wh_code         仓库编码
        area_code       库区编码
        item_set        本次物料领用的全部明细
        strItemInfo     等待分配存储量的物料
    返回:
        nRet
        ret_result
                {
                    "trans_id":""
                    "outbound_items":[{"item_code":"qty":"","item_name":"xx"},...]  -- 在area_code 匹配到的物料,这些物料可以出库
                    "to_be_alloc_items":
                            [{"item_code":"qty":"","item_name":"xx"},...]           -- 等待后面再继续进行分配的物料
                }
    
--]]
function wms_base.IPA_Star2( strLuaDEID, wh_code, area_code, item_set, strItemInfo )
    local nRet, strRetInfo
    local nForceSorting = 0
 
    if ( wh_code == nil or wh_code == '' ) then 
        return 1, "wms_base.IPA_Star2 函数中 wh_code 必须有值!"
    end
    if ( area_code == nil or area_code == '' ) then 
        return 1, "wms_base.IPA_Star2 函数中 area_code 必须有值!"
    end
    if ( item_set == nil or type(item_set) ~= "table" ) then 
        return 1, "wms_base.IPA_Star2 函数中 item_set 必须是一个table对象!"
    end
    
    nRet, strRetInfo = wms.wms_IPA_Start( wh_code, area_code, "", strItemInfo, "", 1 )
    if ( nRet ~= 0 ) then return nRet, "wms_IPA_Start失败! "..strRetInfo end
 
    local success, ret_info
    success, ret_info = pcall( json.decode, strRetInfo )
    if ( success == false ) then return 1, "wms_IPA_Start 返回值为非法的JSON格式!"..ret_info end
 
    local alloc_item_set = json.decode( strItemInfo )       -- 待分配的物料
 
    local ret_result = {}
    local n
    local oos_qty   -- 缺件数量
    local n_outbound = 1
    local n_to_be_alloc = 1
 
    ret_result.trans_id = ret_info.trans_id
    ret_result.outbound_items_set = {}
    ret_result.to_be_alloc_items = {}
    if ( ret_info.oos_info == nil ) then
        -- 表示没有缺件
        ret_result.outbound_items_set = alloc_item_set
    else
        -- 判断哪个item_code缺件,缺多少
        for n = 1, #alloc_item_set do
            qty = alloc_item_set[n].F_QTY
            oos_qty = get_oos_info( alloc_item_set[n], ret_info.oos_info )
            if ( oos_qty == 0 ) then
                -- 没有缺件
                local items_out = {}
                copy_item_info( items_out, alloc_item_set[n] )
                ret_result.outbound_items_set[n_outbound] = items_out
                n_outbound = n_outbound + 1
            else
                -- 有缺件
                if (oos_qty < alloc_item_set[n].F_QTY) then
                    local items_out = {}
                    
                    copy_item_info( items_out, alloc_item_set[n] )
                    items_out.F_QTY = items_out.F_QTY - oos_qty
                    ret_result.outbound_items_set[n_outbound] = items_out
                    n_outbound = n_outbound + 1
    
                    local items_to_be_alloc = {}  
                    copy_item_info( items_to_be_alloc, alloc_item_set[n] )
                    
                    items_to_be_alloc.F_QTY = oos_qty
                    ret_result.to_be_alloc_items[n_to_be_alloc] = items_to_be_alloc
                    n_to_be_alloc = n_to_be_alloc + 1   
                else
                    local items_to_be_alloc = {}  
 
                    copy_item_info( items_to_be_alloc, alloc_item_set[n] )
                    ret_result.to_be_alloc_items[n_to_be_alloc] = items_to_be_alloc
                    n_to_be_alloc = n_to_be_alloc + 1  
                end
            end
        end
    end
    
    -- 更新item_set 中的已分配量
    if (#ret_result.outbound_items_set > 0) then
        for n = 1, #ret_result.outbound_items_set do
            local i
            for i = 1, #item_set do
                if (item_set[i].N_ROW_NO == ret_result.outbound_items_set[n].N_ROW_NO and
                    item_set[i].S_ITEM_CODE == ret_result.outbound_items_set[n].S_ITEM_CODE ) then
                    item_set[i].F_ALLOC_QTY = item_set[i].F_ALLOC_QTY + ret_result.outbound_items_set[n].F_QTY
                end
            end
        end
    end
    return 0, ret_result               
end
 
-- 获取某物料的仓库量表
-- 返回 nRet, qty, alloc_qty
 
function wms_base.GetWHInventory( strLuaDEID, wh_code, item_code )
    local nRet, strRetInfo 
    local parameter
 
    if ( wh_code == nil or wh_code == '' ) then 
        return 1, "GetWHInventory 函数中 wh_code 必须有值!" 
    end
    if ( item_code == nil or item_code == '' ) then 
        return 1, "WGetWHInventory 函数中 item_code 必须有值!"
    end
 
    parameter = {}
    parameter.wh_code = wh_code
    parameter.item_list = {}
    local item = {}
    item.item_code = item_code
    parameter.item_list[1] = item
 
    nRet, strRetInfo = wms.wms_GetWHInventory( lua.table2str(parameter) )
    if (nRet == 0) then
        local ret_info = json.decode( strRetInfo )
        qty = ret_info[1].qty_storage
        alloc_qty = ret_info[1].qty_alloc
        return 0, qty, alloc_qty
    end
    return 1, "获取物料仓库存储量失败!"..strRetInfo
end
 
-- 获取某物料的库区量表 取消
-- 返回 nRet, qty, alloc_qty
-- 获取某物料的库区量表
-- 返回 nRet, qty, alloc_qty
function wms_base.GetAreaInventory( strLuaDEID, area_code, item_code, item_state, item_route )
    local nRet, strRetInfo 
    local parameter
 
    if ( area_code == nil or area_code == '' ) then 
        return 1, "wms_base.GetAreaInventory 函数中 area_code 必须有值!"
    end
    if ( item_code == nil or item_code == '' ) then 
        return 1, "wms_base.GetAreaInventory 函数中 item_code 必须有值!"
    end
 
    parameter = {}
    parameter.area_code = area_code
    parameter.item_list = {}
    local item = {}
    item.item_code = item_code
    parameter.item_list[1] = item
 
    nRet, strRetInfo = wms.wms_GetAreaInventory( lua.table2str(parameter) )
    if (nRet == 0) then
        local ret_info = json.decode( strRetInfo )
        qty = ret_info[1].qty_storage
        alloc_qty = ret_info[1].qty_alloc
        return 0, qty, alloc_qty
    end
    return 1, "获取物料库区存储量失败!"..strRetInfo
end
 
--[[
    入库波次完成后需要把 入库波次明细中的入库数量批分 到入库明细中的 F_ACC_I_QTY,F_ACC_C_QTY
    否则以入库单进行回报时会没有入库数量
    输入参数: wave_no 波次号
]]
function wms_base.InboundWave_Finish_PostProce( strLuaDEID, wave_no )
    local nRet, strRetInfo, n, m
 
    --lua.Debug( strLuaDEID, debug.getinfo(1), "wms_base.InboundWave_Finish_PostProce --> begin ", wave_no )
 
    if ( lua.StrIsEmpty( wave_no ) ) then
        return 1, "wms_base.InboundWave_Finish_PostProce 函数中 wave_no 不能为空!" 
    end
 
    local iw_detail_objs
    local strCondition = "S_WAVE_NO = '"..wave_no.."'"
    nRet, iw_detail_objs = m3.QueryDataObject(strLuaDEID, "IW_Detail", strCondition, "N_ROW_NO" )
    if ( nRet ~= 0 ) then return 1,"QueryDataObject失败!"..iw_detail_objs end
 
    --lua.Debug( strLuaDEID, debug.getinfo(1), "2 iw_detail_objs ", iw_detail_objs )
 
    if ( iw_detail_objs == '' ) then return 0 end  
    
    -- 获取入库单明细里的数量
    local inbound_order_objs
    nRet, inbound_order_objs = m3.QueryDataObject(strLuaDEID, "Inbound_Order", strCondition, "S_NO" )
    if ( nRet ~= 0 ) then return 1,"QueryDataObject失败!"..inbound_order_objs end
 
    --lua.Debug( strLuaDEID, debug.getinfo(1), "3 inbound_order_objs ", inbound_order_objs )
 
    if ( inbound_order_objs == '' ) then return 0 end  
    local io_no_set = {}        -- 入库单号
    local obj_attrs
    for n = 1, #inbound_order_objs do
        obj_attrs  = m3.KeyValueAttrsToObjAttr(inbound_order_objs[n].attrs)
        table.insert( io_no_set, obj_attrs.S_NO )
    end
 
    --lua.Debug( strLuaDEID, debug.getinfo(1), "4 io_no_set ", io_no_set )
 
    local io_detail_objs
    strCondition = "S_IO_NO IN ("..lua.strArray2string( io_no_set )..")"
    nRet, io_detail_objs = m3.QueryDataObject(strLuaDEID, "Inbound_Detail", strCondition, "S_ITEM_CODE" )
    if ( nRet ~= 0 ) then return 1,"QueryDataObject失败!"..io_detail_objs end
 
    --lua.Debug( strLuaDEID, debug.getinfo(1), "5 io_detail_objs ", io_detail_objs )
 
    if ( io_detail_objs == '' ) then return 0 end  
    local nCount = #io_detail_objs
    local io_detail_list = {}
    for n = 1, nCount do
        obj_attrs  = m3.KeyValueAttrsToObjAttr(io_detail_objs[n].attrs)  
        local io_detail = {
            id = io_detail_objs[n].id,
            item_code = obj_attrs.S_ITEM_CODE,
            qty = lua.Get_NumAttrValue( obj_attrs.F_QTY ),
            in_qty = 0, cancel_qty = 0, ok = false
        }
        table.insert( io_detail_list, io_detail )
    end
 
    --lua.Debug( strLuaDEID, debug.getinfo(1), "6 io_detail_list ", io_detail_list )
 
    local cancel_qty, in_qty
    for n = 1, #iw_detail_objs do
        obj_attrs  = m3.KeyValueAttrsToObjAttr(iw_detail_objs[n].attrs)  
        -- 入库数量
        in_qty = lua.Get_NumAttrValue( obj_attrs.F_ACC_I_QTY )  
        -- 取消数量
        cancel_qty = lua.Get_NumAttrValue( obj_attrs.F_ACC_C_QTY )  
        -- 把 in_qty, cancel_qty 批分到 Inbound_Detail
        for m = 1, nCount do
            if ( io_detail_list[m].ok == false ) then
                if ( io_detail_list[m].item_code == obj_attrs.S_ITEM_CODE ) then
                    -- 批分入库数量
                    if ( in_qty > 0 ) then                        
                        x_value = io_detail_list[m].qty - io_detail_list[m].in_qty - io_detail_list[m].cancel_qty  -- 可以批分的入库数量
                        if ( in_qty > x_value ) then
                            qty = x_value
                        else
                            qty = in_qty
                        end
                        in_qty = in_qty - qty
                        io_detail_list[m].in_qty = io_detail_list[m].in_qty + qty 
                    end
                    -- 批分取消数量
                    if ( cancel_qty > 0 ) then                        
                        x_value = io_detail_list[m].qty - io_detail_list[m].in_qty - io_detail_list[m].cancel_qty  -- 可以批分的取消数量
                        if ( cancel_qty > x_value ) then
                            qty = x_value
                        else
                            qty = cancel_qty
                        end
                        cancel_qty = cancel_qty - qty
                        io_detail_list[m].cancel_qty = io_detail_list[m].cancel_qty + qty 
                    end
                    if ( io_detail_list[m].qty == (io_detail_list[m].in_qty+io_detail_list[m].cancel_qty) ) then
                        io_detail_list[m].ok = true
                    end
                end
            end
            if ( in_qty == 0 and cancel_qty == 0 ) then break end
        end
    end
 
    --lua.Debug( strLuaDEID, debug.getinfo(1), "7 io_detail_list ", io_detail_list )
 
    -- 更新入库单明细中的入库数量,取消数量
    local strSetAttr
    for n = 1, nCount do
        strCondition = "S_ID = '"..io_detail_list[n].id.."'"
        strSetAttr = "F_ACC_I_QTY = "..io_detail_list[n].in_qty..", F_ACC_C_QTY = "..io_detail_list[n].cancel_qty
        nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Inbound_Detail", strCondition, strSetAttr )
        if ( nRet ~= 0 ) then return 1, "updateDataAttrByCondition(Inbound_Detail)失败"..strRetInfo end   
    end
    return 0
end
 
--[[
    出库波次完成后需要把 出库波次明细中的出库数量批分 到出库明细中的 F_ACC_O_QTY,F_ACC_C_QTY
    否则以入库单进行回报时会没有入库数量
    输入参数: wave_no 波次号
]]
function wms_base.OutboundWave_Finish_PostProce( strLuaDEID, wave_no )
    local nRet, strRetInfo, n, m
 
    --lua.Debug( strLuaDEID, debug.getinfo(1), "wms_base.OutboundWave_Finish_PostProce --> begin ", wave_no )
 
    if ( lua.StrIsEmpty( wave_no ) ) then
        return 1, "WMS_OuboundWave_Finish_PostProce 函数中 wave_no 不能为空!" 
    end
 
    local ow_detail_objs
    local strCondition = "S_WAVE_NO = '"..wave_no.."'"
    nRet, ow_detail_objs = m3.QueryDataObject(strLuaDEID, "OW_Detail", strCondition, "N_ROW_NO" )
    if ( nRet ~= 0 ) then return 1,"QueryDataObject失败!"..ow_detail_objs end
 
    --lua.Debug( strLuaDEID, debug.getinfo(1), "2 ow_detail_objs ", ow_detail_objs )
 
    if ( ow_detail_objs == '' ) then return 0 end  
    
    -- 获取出库单明细里的数量
    local outbound_order_objs
    nRet, outbound_order_objs = m3.QueryDataObject(strLuaDEID, "Inbound_Order", strCondition, "S_NO" )
    if ( nRet ~= 0 ) then return 1,"QueryDataObject失败!"..outbound_order_objs end
 
    --lua.Debug( strLuaDEID, debug.getinfo(1), "3 outbound_order_objs ", outbound_order_objs )
 
    if ( outbound_order_objs == '' ) then return 0 end  
    local oo_no_set = {}        -- 出库单号
    local obj_attrs
    for n = 1, #outbound_order_objs do
        obj_attrs  = m3.KeyValueAttrsToObjAttr(outbound_order_objs[n].attrs)
        table.insert( oo_no_set, obj_attrs.S_NO )
    end
 
    --lua.Debug( strLuaDEID, debug.getinfo(1), "4 oo_no_set ", oo_no_set )
 
    local oo_detail_objs
    strCondition = "S_IO_NO IN ("..lua.strArray2string( io_no_set )..")"
    nRet, oo_detail_objs = m3.QueryDataObject(strLuaDEID, "Outbound_Detail", strCondition, "S_ITEM_CODE" )
    if ( nRet ~= 0 ) then return 1,"QueryDataObject失败!"..oo_detail_objs end
 
    --lua.Debug( strLuaDEID, debug.getinfo(1), "5 oo_detail_objs ", oo_detail_objs )
 
    if ( oo_detail_objs == '' ) then return 0 end  
    local nCount = #oo_detail_objs
    local oo_detail_list = {}
    for n = 1, nCount do
        obj_attrs  = m3.KeyValueAttrsToObjAttr(oo_detail_objs[n].attrs)  
        local oo_detail = {
            id = oo_detail_objs[n].id,
            item_code = obj_attrs.S_ITEM_CODE,
            qty = lua.Get_NumAttrValue( obj_attrs.F_QTY ),
            out_qty = 0, cancel_qty = 0, ok = false
        }
        table.insert( oo_detail_list, oo_detail )
    end
 
    --lua.Debug( strLuaDEID, debug.getinfo(1), "6 oo_detail_list ", oo_detail_list )
 
    local cancel_qty, out_qty
    for n = 1, #ow_detail_objs do
        obj_attrs  = m3.KeyValueAttrsToObjAttr(ow_detail_objs[n].attrs)  
        -- 出库数量
        out_qty = lua.Get_NumAttrValue( obj_attrs.F_ACC_O_QTY )  
        -- 取消数量
        cancel_qty = lua.Get_NumAttrValue( obj_attrs.F_ACC_C_QTY )  
        -- 把 out_qty, cancel_qty 批分到 Outbound_Detail
        for m = 1, nCount do
            if ( oo_detail_list[m].ok == false ) then
                if ( oo_detail_list[m].item_code == obj_attrs.S_ITEM_CODE ) then
                    -- 批分出库数量
                    if ( out_qty > 0 ) then                        
                        x_value = oo_detail_list[m].qty - oo_detail_list[m].out_qty - oo_detail_list[m].cancel_qty  -- 可以批分的出库数量
                        if ( out_qty > x_value ) then
                            qty = x_value
                        else
                            qty = out_qty
                        end
                        out_qty = out_qty - qty
                        oo_detail_list[m].out_qty = io_detail_list[m].out_qty + qty 
                    end
                    -- 批分取消数量
                    if ( cancel_qty > 0 ) then                        
                        x_value = oo_detail_list[m].qty - oo_detail_list[m].out_qty - oo_detail_list[m].cancel_qty  -- 可以批分的取消数量
                        if ( cancel_qty > x_value ) then
                            qty = x_value
                        else
                            qty = cancel_qty
                        end
                        cancel_qty = cancel_qty - qty
                        oo_detail_list[m].cancel_qty = oo_detail_list[m].cancel_qty + qty 
                    end
                    if ( oo_detail_list[m].qty == (oo_detail_list[m].out_qty+oo_detail_list[m].cancel_qty) ) then
                        oo_detail_list[m].ok = true
                    end
                end
            end
            if ( out_qty == 0 and cancel_qty == 0 ) then break end
        end
    end
 
    --lua.Debug( strLuaDEID, debug.getinfo(1), "7 oo_detail_list ", oo_detail_list )
 
    -- 更新入库单明细中得入库数量,取消数量
    local strSetAttr
    for n = 1, nCount do
        strCondition = "S_ID = '"..oo_detail_list[n].id.."'"
        strSetAttr = "F_ACC_O_QTY = "..oo_detail_list[n].out_qty..", F_ACC_C_QTY = "..oo_detail_list[n].cancel_qty
        nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Outbound_Detail", strCondition, strSetAttr )
        if ( nRet ~= 0 ) then return  1, "updateDataAttrByCondition(Outbound_Detail)失败"..strRetInfo end   
    end
    return 0
end
 
--[[
    得到当前登录人员的账号信息,并且获取登录人员的PC MAC地址,根据这个地址获取绑定的站台
    返回一个json table
    nRet, ret_value { login, user_name, mac, station }
]]
function wms_base.Get_CurLoginUserInfo( strLuaDEID )
    local nRet, strRetInfo
    local ret_value = {}
 
    local strUserLogin, strUserName
    nRet, strUserLogin, strUserName = mobox.getCurUserInfo( strLuaDEID )
    if ( nRet ~= 0 ) then return 2, "获取当前操作人员信息失败! "..strUserLogin end
    
    ret_value.login = strUserLogin
    ret_value.user_name = strUserName
    ret_value.mac = ""
    ret_value.station = ""
 
    -- 获取当前登录账号的电脑MAC地址(如果在MBC上登录是可以获取的)
    nRet, strRetInfo = mobox.getCacheValue( strUserLogin, "__user_terminal__" )
    if ( nRet == 0 ) then
        local user_terminal = json.decode( strRetInfo ) 
        ret_value.mac = lua.Get_StrAttrValue( user_terminal.mac )
 
        -- 根据 MAC-站台 绑定常量获取 站台
        if ( ret_value.mac ~= '' ) then
            nRet, ret_value.station = wms_base.Get_sConst2( strLuaDEID, ret_value.mac )
            if ( nRet ~= 0 ) then
                return 1, "系统无法获取常量'"..ret_value.mac.."'"
            end
        end
    end
 
   return 0, ret_value
  
end
 
return wms_base