fy36
15 小时以前 f1a64ab92120ffc0f1b1cf2c4848528a0d6b5d18
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
--[[
    编码:   GK-API-05
    名称:   小件任务创建接口
    作者:   Peter fang
    日期:   2025-05-13
 
    入口函数: Inbound_Sync
    来源项目: 国科项目
    
    功能说明:
        1. 接收来自上游系统的XML格式数据,并解析该数据,创建Inbound Order及Inbound Detail
        2. 创建Inbound Palletization及Inbound Pallet Detail
]]
 
wms_base = require( "wms_base" )
xml=require( "oi_base_xml" )
wms_pallet = require( "wms_palletizing" )
 
-- 创建统一返回结果
local function create_result(flag, code, msg)
    return {
        flag = flag or "success",
        code = code or "0",
        message = msg or ""
    }
end
 
local function return_value(strLuaDEID, success,err_msg)
    if success==false then
        lua.Stop( strLuaDEID, err_msg )
    end
    local flag = "success"
    local code="0"
    if success==true then
        flag = "success"
        code="0"
    else
        flag = "failure"
        code="1"
    end
    local result= create_result( flag, code, err_msg )
    local xml_result = xml.json_to_xml(result, "response")
    local value=0
    if flag=="success" then
        value=0
    else
        value=3
    end 
    mobox.returnValue(strLuaDEID,1,xml_result,value)
end
 
local function count_cell_list(t)
    local count = 0
    for cell,group in pairs(t) do
        if(cell~=nil and cell~="") then
            count = count + 1
        end
    end
    return count
end
 
local function group_by(t, key)
    local result = {}
    local getKey = type(key) == "function" and key or function(item)
        return item[key]
    end
    for _, item in ipairs(t) do
        local groupKey = getKey(item)
        if groupKey ~= nil then
            if not result[groupKey] then
                result[groupKey] = {}
            end
            table.insert(result[groupKey], item)
        end
    end
    return result
end
 
local function goods_count(items)
    local group_table = group_by(items, "skuId")
    local count = 0
    for _ in pairs(group_table) do
        count = count + 1
    end
    return count
end
 
local function sku_count(items)
    local qty=0
    for _,v in ipairs(items) do
        if v.qty~=nil then
            qty=qty+lua.Get_NumAttrValue(v.qty)
        end
    end
    return qty
end
 
local function get_celltype(group,cid)
    local spec="A"
    local cellItems={}
    for _,cell in ipairs(group) do
        table.insert(cellItems,cell)
    end
    local by_cell=group_by(cellItems,"boxCode")
    local cellCount=count_cell_list(by_cell)
    if (cellCount==1) then
        spec="A"
    elseif (cellCount==2) then
        spec="B"
    elseif (cellCount==3) then
        spec="C"    
    elseif (cellCount==4) then
        spec="D"
    elseif (cellCount==6) then
        spec="E"
    elseif (cellCount==8) then
        spec="F"
    else
        return 1, "料格数量"..cellCount.. "料箱规格超出系统定义!cntr_code= "..cid
    end
    return 0, spec
end
 
local function create_inbound_pallet(strLuaDEID,inboundData,items)
    local nRet,strRetInfo
    local by_pallet=group_by(items,"cid")
    for cid, group in pairs(by_pallet) do
        if cid~=nil and cid~="" then
            if(group[1].boxCode~=nil and group[1].boxCode~="") then
                local spec
                nRet, spec = get_celltype(group,cid)
                if nRet ~= 0 then
                    return 1, spec
                end
                local detail_item_data = {}
                for _,cell in ipairs(group) do
                    local strTableName = "TN_SKU"
                    local strFieldList = '["S_UDF01"]'
                    local strCondition =string.format("S_STORER='%s' AND S_ITEM_CODE='%s'",inboundData.storerId,cell.skuId)
                    nRet, strRetInfo=mobox.queryTable(strLuaDEID,strTableName, strFieldList,1, strCondition, "")
                    if (nRet ~= 0) then
                        return 1,"查询SKU信息失败!"..strRetInfo
                    end
                    local sku_info = json.decode(strRetInfo)
                    if not sku_info or #sku_info == 0 then
                        return 1,"SKU信息不存在"..cell.skuId
                    end
                    local sudf01 = sku_info[1].S_UDF01
 
                    local item_detail 
                    item_detail = m3.AllocObject2( strLuaDEID, "INB_Pallet_Detail")
                    item_detail.S_CNTR_CODE = cid
                    local seg = lua.split(cell.boxCode, '-' )
                    if ( #seg ~= 2 ) then
                        return 1,"料格号格式错误"..cell.boxCode
                    end
                    item_detail.S_CELL_NO =spec.."-".. lua.StrToNumber(seg[2])
                    item_detail.S_CELL_CODE=cell.boxCode
                    item_detail.S_ITEM_CODE = cell.skuId
                    item_detail.S_ITEM_NAME = cell.skuId
                    item_detail.S_ITEM_STATE=cell.skuStatus
                    item_detail.S_STORER=inboundData.storerId
                    item_detail.S_OWNER=inboundData.ownerId
                    item_detail.F_QTY = cell.qty
                    item_detail.S_UDF01=sudf01
                    item_detail.S_UDF02=cell.registerNo
                    item_detail.S_WMS_BN = cell.batchNo
                    item_detail.S_BATCH_NO = cell.produceCode
                    item_detail.D_PRD_DATE = cell.productDate
                    item_detail.D_EXP_DATE = cell.expiryDate
                    item_detail.S_BS_TYPE="GK_IN_TASK"
                    item_detail.S_BS_NO=inboundData.taskId
                    item_detail.N_BS_ROW_NO=cell.row_no
 
                    table.insert(detail_item_data, item_detail)
                end
                nRet, strRetInfo = wms_pallet.Add_INB_Pallet_Detail( strLuaDEID, cid, detail_item_data,"GK_IN_TASK",inboundData.taskId)
                if nRet ~= 0 then
                    return 1,"生成码盘信息失败".. strRetInfo
                end                     
            end
        end
    end
    return 0, ""
end
local function create_container(strLuaDEID,inboundData,items)
    local nRet,strRetInfo
    local by_pallet=group_by(items,"cid")
    for cid, group in pairs(by_pallet) do
        if cid~=nil and cid~="" then
            if(group[1].boxCode~=nil and group[1].boxCode~="") then
                local spec
                nRet, spec = get_celltype(group,cid)
                if nRet ~= 0 then
                    lua.DebugEx(strLuaDEID, "create_container", group[1].boxCode.. "cid="..cid)
                    return 1, spec
                end
            nRet, strRetInfo = wms.wms_GetConst("WMS_Default_CNTR_Type")
                if (nRet ~= 0) then
                    return 1, "获取默认容器类型失败!"..strRetInfo
                end
                local container_type_info = strRetInfo
                local strObjID
                local strCondition =string.format("S_CODE='%s'",cid)
                nRet,strObjID,strRetInfo=mobox.getDataObjAttrByKeyAttr(strLuaDEID,"Container",strCondition)
                if nRet ~= 0 then   
                    return 1,cid.."容器不存在!"..strRetInfo
                end
                local update_container_obj={
                {
                    id = strObjID,
                    attrs = {
                        { attr = "S_CTD_CODE", value =container_type_info }, -- 料箱类型
                        { attr = "S_SPEC", value = spec }, -- 料箱规格
                    }
                }}
                nRet, strRetInfo = mobox.updateDataObj( strLuaDEID, "Container", lua.table2str(update_container_obj) )
                if (nRet ~= 0 ) then 
                    return 1,"更新【Container】属性失败!"..strRetInfo
                end    
            end
        end
    end
    return 0, ""
end
 
local function create_inbound_detail(strLuaDEID,detail)
    local nRet, strRetInfo
 
    local inbound_detail = m3.AllocObject( strLuaDEID,"Inbound_Detail")
    inbound_detail.row_no=detail.row_no
    inbound_detail.io_no=detail.io_no
    inbound_detail.storer=detail.storer
    inbound_detail.owner=detail.owner
    inbound_detail.bs_row_no=detail.bs_row_no
    inbound_detail.item_code=detail.item_code
    inbound_detail.item_name=detail.item_name
    inbound_detail.item_state=detail.item_state
    inbound_detail.qty=detail.qty
    inbound_detail.wms_bn=detail.wms_bn
    inbound_detail.batch_no=detail.batch_no
    inbound_detail.prd_date=detail.prd_date
    inbound_detail.exp_date=detail.exp_date
    inbound_detail.ext_attr1=detail.registerNo
    inbound_detail.note=detail.note
 
    local strTableName = "TN_SKU"
    local strFieldList = '["S_UDF01"]'
    local strCondition =string.format("S_STORER='%s' AND S_ITEM_CODE='%s'",detail.storer,detail.item_code)
    nRet, strRetInfo=mobox.queryTable(strLuaDEID,strTableName, strFieldList,1, strCondition, "")
    if (nRet ~= 0) then
        return 1,"查询SKU信息失败!"..strRetInfo
    end
    local sku_info = json.decode(strRetInfo)
    if not sku_info or #sku_info == 0 then
        return 1,"SKU信息不存在"..detail.item_code
    end
    inbound_detail.udf01 = sku_info[1].S_UDF01
    inbound_detail.udf19=detail.sn
    inbound_detail.udf20=detail.cntr_code
 
    nRet, strRetInfo = m3.CreateDataObj(strLuaDEID, inbound_detail)
    if (nRet==nil or nRet ~= 0 ) then
        return 1, "创建系统入库单明细失败!"..strRetInfo.."no= "..detail.io_no
    end
    return 0, ""
end 
 
local function gk_create_inbound_detail(strLuaDEID,inboundData, inboundItem,index)
    local nRet, strRetInfo
 
    local inbound_detail = m3.AllocObject( strLuaDEID,"GK_IN_TASK_Detail")
    inbound_detail.row_no=index
    inbound_detail.io_no=inboundData.taskId
    inbound_detail.storer=inboundData.storerId
    inbound_detail.owner=inboundData.ownerId
    inbound_detail.bs_row_no=inboundItem.orderItemId
    inbound_detail.item_code=inboundItem.skuId
    inbound_detail.item_name=inboundItem.skuId
    inbound_detail.item_state=inboundItem.skuStatus
    inbound_detail.qty=lua.Get_NumAttrValue(inboundItem.qty)
    inbound_detail.wms_bn=inboundItem.batchNo
    inbound_detail.batch_no=inboundItem.produceCode
    inbound_detail.prd_date=inboundItem.productDate
    inbound_detail.exp_date=inboundItem.expiryDate
    inbound_detail.register_no=inboundItem.registerNo
    inbound_detail.cntr_code=inboundItem.cid
    inbound_detail.cell_no=inboundItem.boxCode
    inbound_detail.sn=inboundItem.sn
 
    nRet, strRetInfo = m3.CreateDataObj(strLuaDEID, inbound_detail)
    if (nRet==nil or nRet ~= 0 ) then
        return 1, "创建入库单明细失败!"..strRetInfo.."no= "..inboundData.taskId
    end
 
    if inboundItem.boxCode==nil or inboundItem.boxCode=="" then
         nRet, strRetInfo=create_inbound_detail(strLuaDEID,inbound_detail)
        if (nRet==nil or nRet ~= 0 ) then
            return 1, "创建系统入库单明细失败!"..strRetInfo.."no= "..inboundData.taskId
        end
    end
   
    return 0, ""
end 
 
local function create_inbound(strLuaDEID, gk_inbound)
   local nRet, strRetInfo
 
   local inbound = m3.AllocObject( strLuaDEID,"Inbound_Order")
   inbound.no=gk_inbound.no
   inbound.b_state=0
   inbound.factory=gk_inbound.factory
   inbound.wh_code=gk_inbound.wh_code
   inbound.bs_type=gk_inbound.bs_type
   inbound.bs_no=gk_inbound.bs_no
   inbound.asn_no=gk_inbound.asn_no
   inbound.op_date=gk_inbound.op_date
   inbound.priority=gk_inbound.priority
   inbound.note=gk_inbound.note
   
    nRet, strRetInfo = m3.CreateDataObj(strLuaDEID, inbound)
   if (nRet==nil or nRet ~= 0 ) then
       return 1,  "创建系统入库单失败!"..strRetInfo.."no= "..gk_inbound.no
   end
 
    return 0,""
end
 
local function gk_create_inbound(strLuaDEID, inboundData,emptyCellCount)
   local nRet, strRetInfo
 
   local inbound = m3.AllocObject( strLuaDEID,"GK_IN_TASK")
   inbound.no=inboundData.taskId
   inbound.b_state=0
   nRet, strRetInfo = wms.wms_GetConst("WMS_Default_Factory")
    if (nRet ~= 0) then
         return 1, "获取默认工厂失败!"..strRetInfo
    end
   inbound.factory=strRetInfo
   nRet, strRetInfo = wms.wms_GetConst("WMS_Default_Warehouse")
   if (nRet ~= 0) then
        return 1, "获取默认仓库失败!"..strRetInfo
   end
   inbound.wh_code=strRetInfo
   inbound.bs_type=inboundData.taskType
   inbound.bs_no=inboundData.orderNo
   inbound.asn_no=inboundData.asnNo
   inbound.op_date=inboundData.orderDate
   inbound.priority=inboundData.priority
   inbound.source_no=inboundData.sourceBillNo
   inbound.note=inboundData.memo
   
   local detailItems = inboundData.TaskCreation_TB_ITEM
   local items={}
   if detailItems==nil then
       return 1, "入库单明细为空"
   end
   if detailItems[1]==nil then
        items={detailItems}
   else
        items=detailItems
   end
   if items==nil or items[1]==nil then
       return 1, "入库单明细为空"
    else
        local index=1
        for index, item in ipairs(items) do
            --创建入库单明细
            item.row_no=index
             nRet, strRetInfo = gk_create_inbound_detail(strLuaDEID,inboundData,item,index)
             if (nRet==nil or nRet ~= 0 ) then
                return 1, "创建入库单明细失败!"..strRetInfo.."no= "..inboundData.taskId
             end
        end
    end
 
    inbound.good_type_num=goods_count(items)
    inbound.total_qty=sku_count(items)
    nRet, inbound = m3.CreateDataObj(strLuaDEID, inbound)
   if (nRet==nil or nRet ~= 0 ) then
       return 1,  "创建入库单失败!"..inbound.."no= "..inboundData.taskId
   end
 
    --创建系统入库单
    if emptyCellCount>0 then
         nRet, strRetInfo =create_inbound(strLuaDEID,inbound)
        if (nRet==nil or nRet ~= 0 ) then
            return 1, "创建系统入库单失败!"..strRetInfo.."no= "..inboundData.taskId
        end
    end
 
    --创建入库单码盘
    nRet, strRetInfo =create_inbound_pallet(strLuaDEID,inboundData,items)
    if (nRet==nil or nRet ~= 0 ) then
        return 1, "创建码盘明细失败!"..strRetInfo.."no= "..inboundData.taskId
    end
    
    --创建容器料格信息
    nRet, strRetInfo = create_container(strLuaDEID,inboundData,items)
    if (nRet==nil or nRet ~= 0 ) then
        return 1, "创建容器失败!"..strRetInfo.."no= "..inboundData.taskId
    end
 
    return 0,""
end
 
function Inbound_Sync(strLuaDEID)
    local nRet, strRetInfo
    local err_msg
    
    --m3.PrintLuaDEInfo(strLuaDEID)
 
    local soap_xml
    nRet,soap_xml=mobox.getCurEditDataPacket(strLuaDEID)
    if ( nRet ~=0 ) then 
        return_value(strLuaDEID, false,  "无法获取数据包 !"..soap_xml)
        return
    end
    local parsed_data
    nRet, parsed_data = xml.parse(soap_xml)
    if ( nRet ~= 0 ) then
        return_value(strLuaDEID, false, "接口输入的XML格式非法!")
        return
    end
    
    local inboundItems=parsed_data.Envelope.Body.InTaskCreationReq.TaskCreation_Input.InputParameters.TaskCreation_TB
    local inboundSet={}
    if inboundItems==nil then
        return_value(strLuaDEID, false, "接口输入的XML格式非法!")
        return
    end
    if inboundItems[1]==nil then
        inboundSet={inboundItems}
    else
        inboundSet=inboundItems
    end
 
    for i, inboundData in ipairs(inboundSet) do
         if inboundData.TaskCreation_TB_ITEM==nil then
            err_msg = "入库明细为空"
            return_value(strLuaDEID, false, err_msg)
            return
        end
 
          --判断InBound是否存在,取消状态后面考虑
        local id
        local strCondition =string.format("S_NO='%s' AND N_B_STATE<>91",inboundData.taskId)
        nRet,id,strRetInfo=mobox.getDataObjAttrByKeyAttr(strLuaDEID,"GK_IN_TASK",strCondition)
        if nRet==0 then
            err_msg = "入库单号已存在!"..inboundData.taskId
            return_value(strLuaDEID, true, err_msg)
            return
        end
 
        --判断料箱是否存在,不存在拒绝
        local detailItems = inboundData.TaskCreation_TB_ITEM
        local items={}
        if detailItems==nil then
            return 1, "入库单明细为空"
        end
        if detailItems[1]==nil then
            items={detailItems}
        else
            items=detailItems
        end
        local by_pallet=group_by(items,"cid")
        local emptyCellCount=0
        for cid, group in pairs(by_pallet) do
            for _,cell in ipairs(group) do
                if cell.boxCode==nil or cell.boxCode=="" then
                    emptyCellCount=emptyCellCount+1
                end
            end
            if cid~=nil and cid~="" then
                --判断料箱是否存在
                local strCondition =string.format("S_CODE='%s'",cid)
                nRet,strRetInfo=mobox.existThisData(strLuaDEID,"Container",strCondition)
                if nRet~=0 then
                    err_msg = "查询数据出错!"..cid
                    return_value(strLuaDEID, false, err_msg)
                    return
                end
                if strRetInfo=="no" then
                    err_msg = "料箱不存在!"..cid
                    return_value(strLuaDEID, false, err_msg)
                    return
                end
                --判断料箱是否完成入库
                 local strCondition =string.format("S_CNTR_CODE='%s' AND N_B_STATE NOT IN(30,40)",cid)
                nRet,strRetInfo=mobox.existThisData(strLuaDEID,"Inbound_Palletization",strCondition)
                if nRet~=0 then
                    err_msg = "查询数据出错!"..cid
                    return_value(strLuaDEID, false, err_msg)
                    return
                end
                if strRetInfo=="yes" then
                    err_msg = "该料箱码盘数据未完成!"..cid
                    return_value(strLuaDEID, false, err_msg)
                    return
                end
                --判断料箱是否存在库存
                strCondition=string.format("S_CNTR_CODE='%s' AND F_QTY>0",cid)
                nRet,strRetInfo=mobox.existThisData(strLuaDEID,"INV_Detail",strCondition)
                if nRet~=0 then
                    err_msg = "查询数据出错!"..cid
                    return_value(strLuaDEID, false, err_msg..strRetInfo)
                    return
                end
                if strRetInfo=="yes" then
                    lua.DebugEx(strLuaDEID, "料箱存在库存")
                    err_msg = "料箱存在库存"..cid
                    return_value(strLuaDEID, false, err_msg..strRetInfo)
                    return
                end
            end
        end
 
        nRet,err_msg= gk_create_inbound(strLuaDEID, inboundData,emptyCellCount)
        if (nRet==nil or nRet~=0) then
            return_value(strLuaDEID, false, err_msg)
        else
            return_value(strLuaDEID, true, "")
        end
    end
end