DATA: wa_item TYPE zpurorder_tab.
DATA: wa_services TYPE zpurorder_tab2.
DATA:ext_line TYPE n LENGTH 10.
DATA:line_no TYPE n LENGTH 10.
DATA:pckg_no TYPE n LENGTH 10.
DATA:serial_no TYPE n LENGTH 2 .
pckg_no = '0000000000'. "软件包号
line_no = '0000000001'. "内部行编号
ext_line = '0000000000'. "行号
serial_no = '00' . "帐户分配的顺序编号
"抬头数据
gs_poheader-doc_type = 'NB' . "i_header-BSART.". 订单类型(采购)
gs_poheader-comp_code = i_header-bukrs. "公司代码 bukrs 公司代码
gs_poheader-purch_org = i_header-ekorg. "采购组织 ekorg 采购组织
gs_poheader-pur_group = i_header-ekgrp. "采购组 ekgrp 采购组
gs_poheader-vendor = i_header-lifnr. "供应商帐户号 superfield 供应商/供应工厂
IF i_header-bldat IS INITIAL.
gs_poheader-doc_date = sy-datum.
ELSE.
gs_poheader-doc_date = i_header-bldat."凭证中的凭证日期 bedat 采购凭证日期
ENDIF.
gs_poheader-sales_pers = i_header-verkf ."供应商办公室的责任销售员(外部合同号)
gs_poheaderx-doc_type = 'X'.
gs_poheaderx-comp_code = 'X'."公司代码
gs_poheaderx-purch_org = 'X'."采购组织
gs_poheaderx-pur_group = 'X'."采购组
gs_poheaderx-vendor = 'X'."供应商帐户号
gs_poheaderx-doc_date = 'X'."凭证中的凭证日期
gs_poheaderx-sales_pers = 'X'.
"行项目数据
LOOP AT it_items INTO wa_item.
pckg_no = pckg_no + 1 .
ext_line = '0000000000' ."行号
line_no = '0000000001' ."内部行编号
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = pckg_no
IMPORTING
output = pckg_no.
gwa_item-po_item = wa_item-ebelp ."采购凭证的项目编号 ebelp 采购凭证的项目编号
gwa_item-material = wa_item-matnr ."物料号
gwa_item-plant = wa_item-mwerks . "收货工厂 name1 工厂
gwa_item-stge_loc = wa_item-mlgort . "收货库存地点 lgobe 库存地点
gwa_item-quantity = wa_item-erfmg . "采购订单数量 menge 采购订单数量
gwa_item-tax_code = wa_item-mwskz . "税码 mwskz 销售/购买税代码
gwa_item-net_price = wa_item-netpr."净价
gwa_item-po_price = '2'. "价格采纳:1 = 总值, 2 = 净值
gwa_item-price_unit = wa_item-peinh. "价格单位
"服务订单类型
gwa_item-acctasscat = wa_item-knttp ." 'F' KNTTP 科目分配类别
gwa_item-item_cat = wa_item-epstp ." 'D' epstp 采购凭证中的项目类别
gwa_item-short_text = wa_item-ktext1 ."'加入新的冷冻液' 短文本
gwa_item-matl_group = wa_item-matkl ." 'Z003' 物料组
gwa_item-pckg_no = pckg_no.
APPEND gwa_item TO gt_item.
gwa_itemx-po_item = wa_item-ebelp ."采购凭证的项目编号
gwa_itemx-material = 'X' ."物料号
gwa_itemx-plant = 'X'. "收货工厂
gwa_itemx-stge_loc = 'X'. "收货库存地点
gwa_itemx-quantity = 'X'. "采购订单数量
gwa_itemx-tax_code = 'X'. "税码
gwa_itemx-po_unit = 'X'.
gwa_itemx-net_price = 'X'.
gwa_itemx-po_price = 'X'.
gwa_itemx-price_unit = 'X'.
gwa_itemx-acctasscat = 'X'. "
gwa_itemx-item_cat = 'X'.
gwa_itemx-short_text = 'X'.
gwa_itemx-matl_group = 'X'.
gwa_itemx-pckg_no = 'X'.
APPEND gwa_itemx TO gt_itemx.
"服务订单概要行
gwa_poservices-pckg_no = pckg_no .
gwa_poservices-line_no = line_no ."内部行编号
gwa_poservices-ext_line = ext_line ."行号
pckg_no = pckg_no + 1 .
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = pckg_no
IMPORTING
output = pckg_no.
gwa_poservices-subpckg_no = pckg_no .
APPEND gwa_poservices TO gt_poservices.
"服务包数据
LOOP AT it_services INTO wa_services WHERE ebelp = wa_item-ebelp ."采购凭证的项目编号
"开始处理服务明细
line_no = line_no + 1 ."内部行编号
ext_line = ext_line + 10 ."行号
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = ext_line
IMPORTING
output = ext_line.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = line_no
IMPORTING
output = line_no.
gwa_poservices-pckg_no = pckg_no .
gwa_poservices-line_no = line_no ."内部行编号
gwa_poservices-ext_line = ext_line ."行号
gwa_poservices-subpckg_no = '0000000000' .
gwa_poservices-short_text = wa_services-ktext1." 短文本
gwa_poservices-quantity = wa_services-menge ." 数量带标记
gwa_poservices-base_uom = wa_services-meins ." 基本计量单位
gwa_poservices-gr_price = wa_services-brtwr ." 总价
gwa_poservices-tax_code = wa_item-mwskz ." 销售/购买税代码
gwa_poservices-matl_group = wa_item-matkl ." 物料组
APPEND gwa_poservices TO gt_poservices.
serial_no = serial_no + 1 ."账户分配规范序号:服务行
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = serial_no
IMPORTING
output = serial_no.
gwa_povalues-pckg_no = pckg_no .
gwa_povalues-line_no = line_no ."内部行编号
gwa_povalues-serno_line = serial_no ."账户分配规范序号:服务行
gwa_povalues-serial_no = serial_no ."帐户分配的顺序编号
APPEND gwa_povalues TO gt_povalues .
*------总账科目
gwa_poaccount-po_item = wa_item-ebelp."采购凭证的项目编号 .
gwa_poaccount-serial_no = serial_no . "账户分配规范序号:服务行
gwa_poaccount-gl_account = wa_item-sakto."总账科目编号
gwa_poaccount-costcenter = wa_item-kostl."成本中心
gwa_poaccount-co_area = 'XF00' .
gwa_poaccount-orderid = wa_item-aufnr.
APPEND gwa_poaccount TO gt_poaccount.
gwa_poaccountx-po_item = wa_item-ebelp."采购凭证的项目编号
gwa_poaccountx-serial_no = serial_no ."帐户分配的顺序编号
gwa_poaccountx-gl_account = 'X' .
gwa_poaccountx-costcenter = 'X' .
gwa_poaccountx-co_area = 'X' .
gwa_poaccountx-orderid = 'X' .
APPEND gwa_poaccountx TO gt_poaccountx.
CLEAR:wa_services,gwa_poservices,gwa_povalues,gwa_poaccount,gwa_poaccountx.
ENDLOOP.
*—— 交货日期的设定
gwa_poschedule-po_item = wa_item-ebelp ." 采购凭证的项目编号
gwa_poschedule-del_datcat_ext = wa_item-elpei ."'D'交货日期的类别
gwa_poschedule-delivery_date = wa_item-eeind ."'20200101' 交货日期
APPEND gwa_poschedule TO gt_poschedule.
gwa_poschedulex-po_item = wa_item-ebelp ." 采购凭证的项目编号
gwa_poschedulex-del_datcat_ext = 'X'." 交货日期的类别
gwa_poschedulex-delivery_date = 'X'." 交货日期
APPEND gwa_poschedulex TO gt_poschedulex.
CLEAR: gwa_item, gwa_itemx, wa_item,gwa_povalues,gwa_poservices,
gwa_poschedule,gwa_poschedulex,
gwa_poaccount,gwa_poaccountx.
ENDLOOP.
PERFORM create_po. "创建采购订单
msg_type = g_msg_type.
message = g_message.
e_ebeln = g_po_number.
*----------------------------------------------------------------------*
***INCLUDE LZFG_ITF20F01.
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form CREATE_PO
*&---------------------------------------------------------------------*
* 创建采购订单
*----------------------------------------------------------------------*
FORM create_po.
CLEAR: g_msg_type, g_po_number, gwa_return, gt_return.
CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
poheader = gs_poheader
poheaderx = gs_poheaderx
IMPORTING
exppurchaseorder = g_po_number
TABLES
return = gt_return
poitem = gt_item
poitemx = gt_itemx
poschedule = gt_poschedule
poschedulex = gt_poschedulex
poaccount = gt_poaccount
poaccountx = gt_poaccountx
poservices = gt_poservices
posrvaccessvalues = gt_povalues .
* 处理返回结果
LOOP AT gt_return INTO gwa_return WHERE type = 'E' OR type = 'A'.
g_msg_type = 'E'.
EXIT.
ENDLOOP.
IF g_test_run = 'X'. "测试运行
IF g_msg_type = 'E'.
g_message = '采购订单创建测试失败:'.
CLEAR: gwa_return.
LOOP AT gt_return INTO gwa_return WHERE type = 'E' OR type = 'A'.
CONCATENATE g_message gwa_return-message INTO g_message SEPARATED BY space.
CLEAR gwa_return.
ENDLOOP.
ELSE.
g_msg_type = 'S'.
g_message = '采购订单创建测试成功'.
ENDIF.
ELSEIF g_test_run NE 'X'.
IF g_msg_type = 'E'.
g_message = '采购订单创建失败:'.
CLEAR: gwa_return.
LOOP AT gt_return INTO gwa_return WHERE type = 'E' OR type = 'A'.
CONCATENATE g_message gwa_return-message INTO g_message SEPARATED BY space.
CLEAR gwa_return.
ENDLOOP.
ELSE.
* 提交所做得更改
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
IF sy-subrc = 0.
g_msg_type = 'S'.
g_message = '采购订单创建成功'.
ELSE.
g_msg_type = 'E'.
g_message = '采购订单创建失败: 未知异常'.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
EXIT.
ENDIF.
ENDIF.
ENDIF.
ENDFORM. "CREATE_PO
DATA:g_po_number TYPE bapimepoheader-po_number,
gs_poheader TYPE bapimepoheader,
gs_poheaderx TYPE bapimepoheaderx,
gt_item TYPE STANDARD TABLE OF bapimepoitem,
gwa_item TYPE bapimepoitem,
gt_itemx TYPE STANDARD TABLE OF bapimepoitemx,
gwa_itemx TYPE bapimepoitemx,
gt_poschedule TYPE STANDARD TABLE OF bapimeposchedule,
gwa_poschedule TYPE bapimeposchedule,
gt_poschedulex TYPE STANDARD TABLE OF bapimeposchedulx,
gwa_poschedulex TYPE bapimeposchedulx,
gt_pocond TYPE STANDARD TABLE OF bapimepocond,
gwa_pocond TYPE bapimepocond,
gt_pocondx TYPE STANDARD TABLE OF bapimepocondx,
gwa_pocondx TYPE bapimepocondx,
gt_pocomponents TYPE STANDARD TABLE OF bapimepocomponent,
gwa_pocomponents TYPE bapimepocomponent,
gwa_pocomponents_temp TYPE bapimepocomponent,
gt_pocomponentsx TYPE STANDARD TABLE OF bapimepocomponentx,
gwa_pocomponentsx TYPE bapimepocomponentx,
gt_poaccount TYPE STANDARD TABLE OF bapimepoaccount,
gwa_poaccount TYPE bapimepoaccount,
gt_poaccountx TYPE STANDARD TABLE OF bapimepoaccountx,
gwa_poaccountx TYPE bapimepoaccountx,
gt_poservices TYPE STANDARD TABLE OF bapiesllc,
gwa_poservices TYPE bapiesllc,
gwa_povalues TYPE bapiesklc,
gt_povalues TYPE STANDARD TABLE OF bapiesklc,
gt_extension TYPE TABLE OF bapiparex WITH HEADER LINE.
DATA: gt_return TYPE TABLE OF bapiret2,
gwa_return TYPE bapiret2,
g_test_run TYPE bapi2017_gm_gen-testrun.