Jianw
2025-05-14 29f8b36ebb718d2051bf0e7e701973ec4419ee80
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
--[[
    编码: WMS-17-12
    名称: 计划盘点容器-Create_Count_CG_Detail
    作者:HAN  
    日期:2025-1-29
 
    级别:固定 (说明本段代码在项目中不太会变化)
    
    函数: CreateCountCGDetail
 
    功能:
       -- 外部触发的事件,输入paramter = {
                                reject_emptybox
                                "count_type":1/2,
                                "count_order_no":"盘点单号",
                                "count_method":1/2 盘点方法, 
                                station, 
                                diff_hand_method }
       -- 根据 CG_Detail 生成 Count_CG_Detail
       -- 先清空 Count_CG_Detail
       -- 成功后需要对容器加 盘点锁
 
    更改记录:
          V2.0 HAN 2025-2-9
               相同货品的CG_Detail 数量合并
          V3.0 HAN 2025-2-13
               新增容器盘点类型
          V4.0 HAN 2025-2-15
               空料箱是否出库盘点判断
 
--]]
wms_base = require( "wms_base" )
 
function CreateCountCGDetail ( strLuaDEID ) 
    local nRet, strRetInfo
    local paramter
 
    -- step1: 获取输入参数
    nRet, paramter = m3.GetSysDataJson( strLuaDEID ) 
    if ( nRet ~= 0 )  then lua.Error( strLuaDEID, debug.getinfo(1), "获取输入参数失败! "..paramter ) end 
    local count_type = lua.Get_NumAttrValue( paramter.count_type )
    if ( count_type == 0 ) then
        lua.Error( strLuaDEID, debug.getinfo(1), "输入参数中必须要有 count_type ! " )
    end
    local count_order_no = lua.Get_StrAttrValue( paramter.count_order_no )
    if ( count_order_no == "" ) then
        lua.Error( strLuaDEID, debug.getinfo(1), "输入参数中必须要有 count_order_no ! " )
    end
    local count_method = lua.Get_NumAttrValue( paramter.count_method )
    if ( count_method <= 0 or count_method > 2 ) then
        lua.Error( strLuaDEID, debug.getinfo(1), "输入参数中 count_method 不合法! " )
    end
    local station = lua.Get_StrAttrValue( paramter.station )        -- 盘点站台
    local diff_hand_method = lua.Get_NumAttrValue( paramter.diff_hand_method )
 
    local count_container
    nRet, count_container = m3.GetSysCurEditDataObj( strLuaDEID, "CP_Count_Container" )
    if ( nRet ~= 0 )  then lua.Error( strLuaDEID, debug.getinfo(1), "获取当前编辑属性失败! "..count_container ) end 
    local strClsID, strObjID
    nRet, strClsID, strObjID = mobox.getCurEditDataObjID( strLuaDEID )
    if ( nRet ~= 0 )  then lua.Error( strLuaDEID, debug.getinfo(1), "getCurEditDataObjID失败! " ) end
    count_container.id = strObjID
 
    -- step2: 先删除【Count_CG_Detail】
    local strCondition = "S_COUNT_NO = '"..count_container.count_no.."' AND S_CNTR_CODE = '"..count_container.cntr_code.."'"
    nRet, strRetInfo = mobox.dbdeleteData(strLuaDEID, "Count_CG_Detail", strCondition)
    if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), "删除【Count_CG_Detail】失败!"..strRetInfo) end  
    
    -- step3: 根据盘点类型生成盘点货品明细
    local strOrder = ''
    local count_cg_detail
 
    -- 根据盘点类型不同设定不同查询条件
    if ( count_type == 1 ) then
        -- 货品盘点
        -- 获取容器需要盘点什么货品,["A","B"]
        if ( count_container.good_codes == nil or count_container.good_codes == '') then
            lua.Debug( strLuaDEID, debug.getinfo(1),"count_container", count_container )
            lua.Error( strLuaDEID, debug.getinfo(1), "货品查询条件为空! " )
        end
 
        local str_good_codes = lua.strArray2string(json.decode( count_container.good_codes ))
        if ( str_good_codes ~= nil and str_good_codes ~= '' ) then
            -- str_good_codes = lua.trim_str_head_end( str_good_codes )
            -- 从CG_Detail获取货品
            strCondition = "S_ITEM_CODE IN ("..str_good_codes..") AND S_CNTR_CODE = '"..count_container.cntr_code.."'"
        else
            lua.Debug( strLuaDEID, debug.getinfo(1),"count_container", count_container )
            lua.Error( strLuaDEID, debug.getinfo(1), "货品查询条件为空! " )
        end
    elseif ( count_type == 2 ) then
        -- 货位盘点
        strCondition = "S_CNTR_CODE = '"..count_container.cntr_code.."'"
    -- V3.0
    elseif ( count_type == 3 ) then  
        -- 容器盘点    
        strCondition = "S_CNTR_CODE = '"..count_container.cntr_code.."'"  
    else
        lua.Error( strLuaDEID, debug.getinfo(1), "输入参数count_type值非法! count_type = "..count_type )
    end
 
    local data_objs, i, n
    nRet, data_objs = m3.QueryDataObject(strLuaDEID, "CG_Detail", strCondition, strOrder )
    if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), "QueryDataObject失败!"..data_objs) end
 
    -- V2.0
    local cg_detail_list = {}
    local find
 
    if ( data_objs ~= '') then
        for i = 1, #data_objs do
            object_attr = m3.KeyValueAttrsToObjAttr(data_objs[i].attrs)
 
            -- V2.0 判断cg_detail_list 
            find = false
            for n = 1, #cg_detail_list do
                if ( cg_detail_list[n].item_code == object_attr.S_ITEM_CODE ) then
                    cg_detail_list[n].qty = cg_detail_list[n].qty + lua.StrToNumber( object_attr.F_QTY )
                    cg_detail_list[n].cg_detail_id = ""         -- 说明是有多条记录合并的,因此需要批分
                    find = true
                    break
                end
            end
 
            if ( find == false ) then
                count_cg_detail = m3.AllocObject(strLuaDEID,"Count_CG_Detail")
                count_cg_detail.cg_detail_id = data_objs[i].id
                count_cg_detail.cc_no = count_container.cc_no
                count_cg_detail.count_no = count_order_no
                count_cg_detail.station = station  
                count_cg_detail.cntr_code = object_attr.S_CNTR_CODE
                count_cg_detail.cell_no = object_attr.S_CELL_NO
                count_cg_detail.serial_no = object_attr.S_SERIAL_NO
                count_cg_detail.item_code = object_attr.S_ITEM_CODE
                count_cg_detail.item_name = object_attr.S_ITEM_NAME
                count_cg_detail.batch_no = object_attr.S_BATCH_NO
                count_cg_detail.item_spec = object_attr.S_ITEM_SPEC
                count_cg_detail.item_state = object_attr.N_ITEM_STATE
                count_cg_detail.end_user = object_attr.S_END_USER
                count_cg_detail.owner = object_attr.S_OWNER       
                count_cg_detail.supplier = object_attr.S_SUPPLIER_NO  
                count_cg_detail.erp_wh_code = object_attr.S_ERP_WH_CODE 
                count_cg_detail.ext_attr1 = object_attr.S_EXT_ATTR1
                count_cg_detail.ext_attr2 = object_attr.S_EXT_ATTR2
                count_cg_detail.ext_attr3 = object_attr.S_EXT_ATTR3
                count_cg_detail.ext_attr4 = object_attr.S_EXT_ATTR4
                count_cg_detail.ext_attr5 = object_attr.S_EXT_ATTR5
                count_cg_detail.qty = lua.StrToNumber( object_attr.F_QTY )                                  
                count_cg_detail.uom = object_attr.S_UOM  
                table.insert( cg_detail_list, count_cg_detail )
            end
        end
    end 
 
    --V4.0
    local nCount = #cg_detail_list
    if ( nCount == 0 and paramter.reject_emptybox ) then
        -- 空料箱不需要出库盘点
        -- 更新【计划盘点容器】中的属性
        strCondition = "S_ID = '"..strObjID.."'"
        -- 设置 状态=4/已回库(完成)
        local strSetSQL = "N_B_STATE = 4"
        nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "CP_Count_Container", strCondition, strSetSQL)
        if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), "设置【计划盘点容器】信息失败!"..strRetInfo ) end
 
        -- 增加一个后台进程对盘点单进行处理,检查盘点单是否可以完成
        local add_wfp = {
            wfp_type = 1,
            cls = "CP_Count_Container",
            obj_id = strObjID,
            obj_name = "盘点容器流水号'"..count_container.cc_no.."'-->盘点完成后处理",
            trigger_event = "盘点完成后处理"
        }
        nRet, strRetInfo = m3.AddSysWFP( strLuaDEID, add_wfp )
        if ( nRet ~= 0 ) then 
            lua.Error( strLuaDEID, debug.getinfo(1), "AddSysWFP失败!"..strRetInfo )  
        end     
        return         
    end
 
    for n = 1, #cg_detail_list do
        nRet, count_cg_detail = m3.CreateDataObj( strLuaDEID, cg_detail_list[n] )   
        if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), "创建【Count_CG_Detail】失败!"..count_cg_detail) end 
    end
    
    -- step4: 更新【计划盘点容器】中的属性
    strCondition = "S_ID = '"..strObjID.."'"
    -- 设置 盘点单号 及 状态=1/已锁定
    local strSetSQL = "S_COUNT_NO = '"..count_order_no.."', N_B_STATE = 1, N_COUNT_METHOD = "..count_method..", S_STATION_NO = '"..station.."', N_DIFF_HAND_METHOD = "..diff_hand_method
    nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "CP_Count_Container", strCondition, strSetSQL)
    if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), "设置【计划盘点容器】信息失败!"..strRetInfo ) end  
    
    -- step5: 如果【计划盘点容器】的盘点方法=2 (自动出库到站点)就需要在后台创建作业进【盘点出库】作业
    if ( count_method == 2 ) then
        -- 增加一个后台进程进行入库单完工回报
        local add_wfp = {
            wfp_type = 1,                  -- 触发数据对象事件(指定数据对象标识)
            cls = "CP_Count_Container",
            obj_id = count_container.id,
            obj_name = "Container No '"..count_container.cntr_code.."'-->Create stocktaking outbound operation",
            trigger_event = "CreateStocktakingOutOperation"
        }
        nRet, strRetInfo = m3.AddSysWFP( strLuaDEID, add_wfp )
        if ( nRet ~= 0 ) then 
            lua.Error( strLuaDEID, debug.getinfo(1), "AddSysWFP失败!"..strRetInfo )  
        end         
    end
 
    -- step6: 给容器加盘点锁 4 -- 盘点锁
    nRet, strRetInfo = wms.wms_LockCntr( count_container.cntr_code, 4, count_order_no )
    if ( nRet ~= 0 ) then
        lua.Error( strLuaDEID, debug.getinfo(1), "给容器'"..operation_obj.cntr_code.."'加盘点锁失败!")   
    end  
    wms.wms_CommitCntrLockTrans( count_order_no )      
end