1
Jianw
2025-07-09 f6f5e6b632d6649386a380558d84003f3de7ec6c
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
--[[
   编码: AMS-21-26
   名称: 
   作者:
   日期:2025-04-27
 
   函数: AfterQtyChange
   功能:
 
   更改记录:
 
--]]
 
json  = require ("json")
mobox = require ("OILua_JavelinExt")
m3 = require ("oi_base_mobox")
 
function AfterQtyChange( strLuaDEID )
    local nRet,attrs
    local strRetInfo
    local detail_records
    -- 获取表头界面中的输入条码
    nRet, attrs = m3.GetSysInputParameter(strLuaDEID)
    if (nRet ~= 0) then
        mobox.setInfo(strLuaDEID,  "获取当前输入面板里的属性失败! " .. attrs)
        return
    end
    
    local input_attr = m3.KeyValueAttrsToObjAttr(attrs)
    local io_no = input_attr.S_IO_NO            --入库单号
    local item_code = input_attr.S_ITEM_CODE    --物料编码
    
    local Condition = "S_ITEM_CODE = '" .. item_code .. "'"
    nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "SKU", Condition)
    if nRet ~= 0 then
        mobox.setInfo(strLuaDEID,  "查询 Material 表失败: " .. strRetInfo)
        return
    end
    local material_name = strRetInfo.item_name              -- 物料名称
    local area = strRetInfo.udf01                           -- 库区
    
    local Condition = "S_NO = '" .. io_no .. "'"
    nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Inbound_Order", Condition)
    if nRet ~= 0 then
        mobox.setInfo(strLuaDEID,  "查询 Material 表失败: " .. strRetInfo)
        return
    end
    local inbound_area = strRetInfo.area_code 
    
    if(area ~= inbound_area) then
        mobox.setInfo(strLuaDEID, "物料所在库区和入库单对应库区不一致!!" ..item_code)
        return
    end
    
    local nRet,whcode
    nRet,whcode = wms_base.Get_sConst2("人工库存储区" )
    if ( nRet ~= 0 ) then
        lua.Error(strLuaDEID, debug.getinfo(1), "获取系统常量:人工库存储区失败!" )
    end
    -- 获取物料对应的货位
    local loc_data
    local Condition = " S_ITEM_CODE = '" .. item_code .. "' AND S_AREA_CODE = '"..whcode.."'"
    nRet, loc_data = m3.QueryDataObject(strLuaDEID, "INV_Detail", Condition)
    if (nRet ~= 0) then
        mobox.setInfo(strLuaDEID, "查询物料对应的货位失败" ..item_code)
        return
    end
 
    local locations = {}
    local loc_record
    local unique_locations = {} -- 用于检查重复值的哈希表
 
    -- 如果查询结果为空,则返回"(无数据)"
    if #loc_data == 0 then
        table.insert(locations, " ")
    else
        for n = 1, #loc_data do
            loc_record = m3.KeyValueAttrsToObjAttr(loc_data[n].attrs)
            if loc_record.S_LOC_CODE and loc_record.S_LOC_CODE ~= "" then
                -- 检查是否已存在该位置代码
                if not unique_locations[loc_record.S_LOC_CODE] then
                    table.insert(locations, loc_record.S_LOC_CODE)
                    unique_locations[loc_record.S_LOC_CODE] = true 
                end
            end
        end
        
        -- 如果最终没有有效数据,也返回"(无数据)"
        if #locations == 0 then
            table.insert(locations, "")
        end
    end
 
    local strCondition = " S_IO_NO = '" ..io_no.. "' and S_ITEM_CODE = '" .. item_code .. "'"
    nRet, strRetInfo = mobox.existThisData( strLuaDEID, "ERP_Inbound_Detail", strCondition )
    if ( nRet ~= 0 ) then 
        mobox.setInfo(strLuaDEID,  "调用existThisData方法失败! " .. attrs)
        return
    end
    if ( strRetInfo ~= "yes" ) then 
        mobox.setInfo(strLuaDEID,  "该入库单没有这条数据! " .. attrs)
        return
    end  
    
    nRet, detail_records = m3.QueryDataObject(strLuaDEID, "ERP_Inbound_Detail", strCondition)
    if (nRet ~= 0) then
        mobox.setInfo(strLuaDEID, debug.getinfo(1), "查询入库单明细失败!"..detail_records)
        return
    end
    
    local total_qty = 0
    local acc_qty = 0
    for i = 1, #detail_records do
        local detail_obj = m3.KeyValueAttrsToObjAttr(detail_records[i].attrs)
        total_qty = total_qty + detail_obj.F_QTY    -- 入库数量
        acc_qty = acc_qty + detail_obj.F_ACC_I_QTY  -- 累计入库数量
    end
    local qty = total_qty - acc_qty
    if qty <= 0 then
        mobox.setInfo(strLuaDEID, "物料["..item_code.."]当前可入库数量为0!")
        return
    end
 
    local row_data = {{
    id = lua.guid(),
    attrs = {{
        attr = "S_ITEM_NAME",
        value = material_name
    }, {
        attr = "S_ITEM_CODE",
        value = item_code
    }, {
        attr = "F_QTY",
        value = total_qty
    }, {
        attr = "F_ACC_I_QTY",
        value = acc_qty
    }, {
        attr = "Qty",
        value = qty
    }
        
    }
    }}
    
    local action = {}
    
        -- 设置正在码盘页面数据
    action[1] = {
        action_type = "insert_subtable_page_row",
        value = {
            page_name = "物料信息",
            row = row_data
        }
    }
    
    action[2] = {
            action_type = "set_dlg_attr",
            value = {
                { attr = "S_EXT_ATTR1", value = locations  },
                { attr = "S_ITEM_CODE", value = item_code }--, enable = false 
                
                }
        }
    action[3] = {
            action_type = "set_dlg_current_edit_attr",
            value = "S_EXT_ATTR2"
                    }
 
    nRet, strRetInfo = mobox.setAction(strLuaDEID, lua.table2str(action))
    if (nRet ~= 0) then
        mobox.setInfo(strLuaDEID, "setAction失败! " .. strRetInfo)
        return
    end
 
end