1
Jianw
9 天以前 70f29da38121b9a467841253e3268feb5df02902
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
--[[
   编码: AMS-102-03
   名称: 
   作者:
   日期:2025-05-29
 
   函数: ClickOutbound
   功能:
 
   更改记录:
 
--]]
 
json  = require ("json")
mobox = require ("OILua_JavelinExt")
m3 = require ("oi_base_mobox")
 
function ClickOutbound( strLuaDEID )
    local nRet,strRetInfo
    local data_json
    nRet,data_json = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), data_json)
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), 'data_json',data_json)
    
    local function contains(table, value)
       for _, v in ipairs(table) do
            if v == value then
                return true 
            end
       end
       return false
    end
 
    local uniqueCntrCodes = {}  -- 存储唯一的料箱编号
    
    --检查插入料箱代码到uniqueCntrCodes中
    local Data_attrs = {}
    for _, v in ipairs(data_json) do
        local attrs = v.attrs
        local Data_attrs1 = m3.KeyValueAttrsToObjAttr(attrs)
        lua.Debug(strLuaDEID,debug.getinfo(1),'Data_attrs1',Data_attrs1)
 
        -- 值存在不插入
        if( not contains(uniqueCntrCodes,Data_attrs1.S_CNTR_CODE)) then 
            table.insert(uniqueCntrCodes,Data_attrs1.S_CNTR_CODE)
        end
        
        table.insert(Data_attrs, Data_attrs1)
    end
    lua.Debug(strLuaDEID,debug.getinfo(1),'Data_attrs',Data_attrs)
 
    -- 收集 所有不同的 S_CNTR_CODE
    lua.Debug(strLuaDEID, debug.getinfo(1), 'uniqueCntrCodes',uniqueCntrCodes)
       
    -- 统计数量
    local nCount = #uniqueCntrCodes
    lua.Debug(strLuaDEID,debug.getinfo(1),'nCount',nCount)
    if(nCount == 0)then
        return
    elseif(nCount > 5)then
        mobox.setInfo(strLuaDEID,"禁止选择超过五种料箱")
        return
    end
    
    local area_groups = {}
    for i,item in ipairs(Data_attrs) do
        --获取库区编码
        local area = item.S_AREA_CODE
        if not area_groups[area] then
            area_groups[area] = {}  -- 确保 area_groups[area] 是表
        end
        table.insert(area_groups[area],item)
    end
    lua.Debug(strLuaDEID,debug.getinfo(1),'area_groups',area_groups)
    
    
        --创建出库单(按area_code拆分)
    for area_code,items in pairs(area_groups) do
 
        --生成出库单号
        local header = 'CK'..os.date("%y%m%d")..'-'
        local nRet,order_no = mobox.getSerialNumber("出库单",header,4)
        if(nRet ~= 0)then
            mobox.setInfo(strLuaDEID,"申请出库单单号失败:"..order_no)
            return
        end
        lua.Debug(strLuaDEID,debug.getinfo(1),'order_no',order_no)
 
        for i,item in ipairs(Data_attrs) do
            -- local cntr_code = item.S_CNTR_CODE 
            -- local item_code = item.S_ITEM_CODE 
            -- local strCondition = "S_CNTR_CODE = '" .. cntr_code .. "' AND S_ITEM_CODE = '" .. item_code .. "'"
            local oo_no = item.S_OO_NO
            local strCondition = "S_OO_NO = '"..oo_no.."'"
            local strSetAttr = "S_NO = '" .. order_no .. "', N_B_STATE = '1' "
 
            lua.Debug(strLuaDEID,debug.getinfo(1),'strCondition',strCondition)
            lua.Debug(strLuaDEID,debug.getinfo(1),'strSetAttr',strSetAttr)
 
 
            if( item.S_NO and  item.S_NO ~= "") or  item.N_B_STATE == "1" then
                mobox.setInfo(strLuaDEID,"请勿重复创建出库单号!")
                return
            elseif( item.S_NO and  item.S_NO == "") or  item.N_B_STATE == "0" then
                nRet,strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "Outbound_Request", strCondition, strSetAttr)            
                if(nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "更新数据失败: "..strRetInfo)
                end
                -- if(nRet == 0) then
                --     mobox.setInfo(strLuaDEID,"出库单号创建成功!")
                -- end
            end 
        end
        
        
        -- --库区编码和出库方向都来自POU
        -- local order = m3.AllocObject(strLuaDEID, "Outbound_Order")
        -- order.state = "发布"              --单据状态
        -- order.no = order_no               --出库单号
        -- order.bs_no = order_no            --来源单号
        -- order.box_qty = nCount           --发货箱数量
        -- order.area_code = area_code 
 
        -- for _,item in ipairs(items) do
        --     local detail = m3.AllocObject(strLuaDEID, "Outbound_Detail")
        --     detail.oo_no = order_no
        --     detail.item_code = item.S_ITEM_CODE
        --     detail.item_name = item.S_ITEM_NAME
        --     detail.qty = item.F_QTY
 
            
        --     local ret1, msg1 = m3.CreateDataObj(strLuaDEID, detail)
        --     if ret1 ~= 0 then
        --         lua.Stop(strLuaDEID, "创建出库单明细失败:" .. msg1)
        --         return
        --     end 
 
        -- end
 
        -- local ret, msg = m3.CreateDataObj(strLuaDEID, order)
        -- if ret ~= 0 then
        --     lua.Stop(strLuaDEID, "创建出库单失败:" .. msg)
        --     return
        -- end   
    end
    
    local action = {
        {
            action_type = "refresh",
            value = ""
        }
    }
    local nRetAction, strRetInfo8 = mobox.setAction(strLuaDEID, lua.table2str(action))
    if nRetAction ~= 0 then
        lua.Error(strLuaDEID, debug.getinfo(1), 'setAction错误: ' .. strRetInfo8)
    end
    mobox.setInfo(strLuaDEID,"出库单号创建成功!")
end