Jianw
2025-05-13 3b39fe3810c3ee2ec9ec97236c1769c5c85e062c
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
--[[
    编码: WMS-17-10
    名称: 计划盘点容器-新增盘点单-确定后
    作者:HAN  
    日期:2025-1-29
 
    级别:固定 (说明本段代码在项目中不太会变化)
    
    函数: AfterClickOK
 
    功能:
       -- 获取查面板中输入的 站点 和 数量 创建盘点单
    更改记录:
 
--]]
 
wms_base = require ("wms_base")
 
-- 创建盘点单
-- wh_code 仓库编码
-- area_code 库区编码如果为空表示所有几乎融券  num 盘点容器数量如果=0说明盘点所有容器
local function create_count_order( strLuaDEID, cp_no, count_type, wh_code, area_code )
    local nRet, strRetInfo
 
    if ( wh_code == nil or wh_code == '') then return 1, "wh_code 必须有值!" end
    if ( cp_no == nil or cp_no == '') then return 1, "cp_no 必须有值!" end
 
    if ( area_code == nil ) then area_code = '' end
 
    local strCondition = "S_CP_NO = '"..cp_no.."' AND (S_COUNT_NO = '' or S_COUNT_NO Is Null)"
    if ( area_code ~= '' ) then
        strCondition = strCondition.." AND S_AREA_CODE = '"..area_code.."'"
    end
    -- 获取待盘点的容器数量
    nRet, strRetInfo = mobox.getDataObjCount( strLuaDEID, "CP_Count_Container", strCondition )
    if ( nRet ~= 0 ) then return nRet, strRetInfo end 
    local num = lua.StrToNumber( strRetInfo )     
        
    -- 检测符合条件的这些计划盘点容器是否有锁,如果有锁报错,不继续
    -- 容器锁状态 不等于 0 
    local str_where = "N_LOCK_STATE <> 0 AND S_CODE IN ( Select S_CNTR_CODE From TN_CP_Count_Container Where "..strCondition..")"
    nRet, strRetInfo = mobox.existThisData( strLuaDEID, "Container", str_where )
    if ( nRet ~= 0 ) then return 1, strRetInfo  end
    if ( strRetInfo == "yes" ) then return 1, "计划盘点的容器中有些容器已经被锁定,不能继续盘点单创建操作!" end
 
    -- 创建盘点单
    local count_order = m3.AllocObject(strLuaDEID,"Count_Order")
    count_order.cp_no = cp_no
    count_order.wh_code = wh_code
    count_order.area_code = area_code
    count_order.method = 1                      -- 1 表示是人工盘点,这是新兴项目的要求
    count_order.num = num
    count_order.count_type = count_type
    nRet, count_order = m3.CreateDataObj( strLuaDEID, count_order )   
    if (nRet ~= 0) then return 1, "创建【盘点单】失败!"..count_order end 
    
    -- 容器加盘点锁
    str_where = "N_LOCK_STATE = 0 AND S_CODE IN ( Select S_CNTR_CODE From TN_CP_Count_Container Where "..strCondition..")"
    local strSetSQL = "N_LOCK_STATE = 3"
 
    nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "Container", str_where, strSetSQL)
    if (nRet ~= 0) then return 1, "设置【容器】锁信息失败!"..strRetInfo end
    -- 计划盘点容器加盘点单号
    strSetSQL = "S_COUNT_NO = '"..count_order.count_no.."'"
    nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "CP_Count_Container", strCondition, strSetSQL)
    if (nRet ~= 0) then return 1, "设置【容器】锁信息失败!"..strRetInfo end
 
    -- 生成【盘点容器货品明细】
    local strClsID
    local strOrder = ''
    strCondition = "S_COUNT_NO = '"..count_order.count_no.."'"
 
    -- 多页查询
    nRet, strRetInfo = mobox.queryDataObjAttr2( strLuaDEID, "CP_Count_Container", strCondition, strOrder, 100 )
    if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "queryDataObjAttr2: "..strRetInfo) end  
    if  ( strRetInfo == '' ) then return end
    local success
    local queryInfo
    success, queryInfo = pcall( json.decode, strRetInfo )
    if ( success == false ) then lua.Error( strLuaDEID, debug.getinfo(1), "queryDataObjAttr2 返回结果啊非法的JSON格式!" ) end
    local queryID = queryInfo.queryID
    local nPageCount = queryInfo.pageCount
    local nPage = 1
    local dataSet = queryInfo.dataSet       -- 查询出来的数据集
    local datajson = {}
    datajson.count_type = count_type
 
    while (nPage <= nPageCount) do
 
        for n = 1, #dataSet do
            -- 考虑到生成盘点货品明细的时间会较长用 WFP 来进行处理
            local add_wfp = {}
            add_wfp.wfp_type = 1                  -- 触发数据对象事件(指定数据对象标识)
            add_wfp.datajson = datajson
            add_wfp.cls = "CP_Count_Container"
            add_wfp.obj_id = dataSet[n].id
            add_wfp.trigger_event = "生成盘点货品明细"
            nRet, strRetInfo = m3.AddSysWFP( strLuaDEID, add_wfp )
            if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), strRetInfo )  end               
        end        
 
        nPage = nPage + 1
        if ( nPage <= nPageCount ) then
            -- 取下一页
            nRet, strRetInfo = mobox.queryDataObjAttr2( queryID, nPage)
            if ( nRet ~= 0 ) then
                lua.Error( strLuaDEID, debug.getinfo(1), "queryDataObjAttr2失败! nPage="..nPage.."  "..strRetInfo )
            end 
            queryInfo = json.decode(strRetInfo) 
            dataSet = queryInfo.dataSet              
        end
    end
 
    return 0
end
 
function AfterClickOK ( strLuaDEID ) 
    local nRet, strRetInfo
    local area_code
 
    -- 获取 库区编码,盘点类型
    nRet, strRetInfo = mobox.getCurEditDataObjAttr( strLuaDEID, "Method" ) 
 
    if ( nRet ~= 0 )  then lua.Error( strLuaDEID, debug.getinfo(1), "获取当前编辑属性失败! "..strRetInfo ) end 
    local obj_attrs = json.decode( strRetInfo ) 
    local method = lua.Get_StrAttrValue( obj_attrs[1].value )
 
    local runtime_paramter
    nRet, runtime_paramter = m3.GetRuntimeParam(strLuaDEID)
    if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "GetRuntimeParam失败! "..runtime_paramter ) end
  
    lua.Debug( strLuaDEID, debug.getinfo(1), "runtime_paramter", runtime_paramter )
 
    -- 获取主数据对象【盘点计划】
    local master = runtime_paramter.master
    if  ( master == nil ) then lua.Error( strLuaDEID, debug.getinfo(1), "运行时参数缺少master属性! " ) end
    local count_plan
    nRet, count_plan = m3.GetDataObject( strLuaDEID, master.clsId, master.objId ) 
    if ( nRet ~= 0 )  then lua.Error( strLuaDEID, debug.getinfo(1), count_plan ) end 
    local cp_no = count_plan.cp_no
    local count_type =  count_plan.type
    local wh_code = count_plan.wh_code
 
    if ( cp_no == nil or cp_no == '') then lua.Error( strLuaDEID, debug.getinfo(1), "无法获取计划盘点号! " ) end
    local b_state = lua.StrToNumber( master.objAttr["N_B_STATE"] )
    if ( b_state ~= 2 ) then lua.Error( strLuaDEID, debug.getinfo(1), "盘点计划的状态非执行状态,无法创建盘点单! " ) end
 
    lua.Debug( strLuaDEID, debug.getinfo(1), "count_type", count_type )
 
    -- 根据 method 来创建【盘点单】
    local strCondition
    local strOrder = ''
    if ( method == "根据库区分别生成盘点单" ) then
        -- 获取目前计划盘点容器的库区
        strCondition = "S_CP_NO ='"..cp_no.."' AND (S_COUNT_NO ='' or S_COUNT_NO Is Null)"
        nRet, strRetInfo = mobox.groupDataObjAttr( strLuaDEID, "CP_Count_Container", strCondition, "S_AREA_CODE", strOrder)
        if ( nRet ~= 0 ) then
            lua.Error( strLuaDEID, debug.getinfo(1), "无法从【CP_Count_Container】获取库区信息"..strRetInfo )
        end 
        if ( strRetInfo == '' ) then lua.Error( strLuaDEID, debug.getinfo(1), "检查一下计划盘点容器是否已经计算出货位?" ) end
        local area_group = json.decode( strRetInfo )  -- 库区
        local n
 
        for n = 1, #area_group do
            nRet, strRetInfo = create_count_order( strLuaDEID, cp_no, count_type, wh_code, area_group[n].value  )
            if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "create_count_order 失败! "..strRetInfo ) end            
        end
    else
        nRet, strRetInfo = create_count_order( strLuaDEID, cp_no, count_type, wh_code )
        if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "create_count_order 失败! "..strRetInfo ) end
    end
end