场景说明:使用BAPI_PO_CREATE1创建PO,当存在信息记录时,也使用条件类型PBXX.
解决方法:在table:pocond;pocondx增加传参如下
pocond-cond_count = '02'.
pocond-change_id = 'I'.
pocond-cond_updat = 'X'.
pocond-condchaman = 'X'.
pocondx-change_id = c_x.
pocondx-cond_count = c_x.
示例:
DATA: poheader TYPE bapimepoheader.
DATA: poheaderx TYPE bapimepoheaderx.
DATA: return TYPE TABLE OF bapiret2 WITH HEADER LINE. "标准返回参数
DATA: poitem TYPE TABLE OF bapimepoitem WITH HEADER LINE. "采购订单项目
DATA: poitemx TYPE TABLE OF bapimepoitemx WITH HEADER LINE. "采购订单项目字段标识
DATA: pocond LIKE bapimepocond OCCURS 0 WITH HEADER LINE. "采购订单中的条件
DATA: pocondx LIKE bapimepocondx OCCURS 0 WITH HEADER LINE. "采购订单中的条件字段标识
DATA: posched TYPE TABLE OF bapimeposchedule WITH HEADER LINE. "采购订单交货计划行
DATA: poschedx TYPE TABLE OF bapimeposchedulx WITH HEADER LINE. "采购订单交货计划行的字段标识
DATA: poaccount TYPE TABLE OF bapimepoaccount WITH HEADER LINE. "采购订单的帐户分配字段
DATA: poaccountx TYPE TABLE OF bapimepoaccountx WITH HEADER LINE. "采购订单的帐户分配字段标识
DATA: extensionin LIKE TABLE OF bapiparex WITH HEADER LINE. "采购订单的扩展字段
DATA: potextitem TYPE TABLE OF bapimepotext WITH HEADER LINE .
DATA: potextheader TYPE TABLE OF bapimepotextheader WITH HEADER LINE.
DATA: po_no TYPE bapimepoheader-po_number. "生成订单号码
DATA: pocomponents TYPE TABLE OF bapimepocomponent WITH HEADER LINE.
DATA: pocomponentsx TYPE TABLE OF bapimepocomponentx WITH HEADER LINE.
CONSTANTS :c_x VALUE 'X'. "常量X
DATA:lv_index TYPE i.
DATA:lv_c.
DATA:lt_alv2 TYPE TABLE OF tp_alv2 WITH HEADER LINE.
DATA:lv_ebelp TYPE ekpo-ebelp.
DATA:lv_msg TYPE char200.
DATA:lv_esokz LIKE a018-esokz.
**检查
CHECK gt_alv2 IS NOT INITIAL.
CLEAR:gs_alv2,lv_c,lt_alv2[].
READ TABLE gt_alv2 INTO gs_alv2 WITH KEY menge = 0.
lv_index = sy-tabix.
IF gs_alv2 IS NOT INITIAL.
MESSAGE '数量不能为0' TYPE 'S' DISPLAY LIKE 'E'.
gs_alv2-msg = '数量不能为0'.
MODIFY gt_alv2 FROM gs_alv2 INDEX lv_index TRANSPORTING msg.
lv_c = 'X'.
ENDIF.
READ TABLE gt_alv2 INTO gs_alv2 WITH KEY matnr = ''.
lv_index = sy-tabix.
IF gs_alv2 IS NOT INITIAL.
MESSAGE '物料为空!特殊订单请用ME21N' TYPE 'S' DISPLAY LIKE 'E'.
gs_alv2-msg = '物料为空!特殊订单请用ME21N'.
MODIFY gt_alv2 FROM gs_alv2 INDEX lv_index TRANSPORTING msg.
lv_c = 'X'.
ENDIF.
LOOP AT gt_alv2 INTO gs_alv2 WHERE rbeln <> ''.
MESSAGE '请不要重复创建' TYPE 'S' DISPLAY LIKE 'E'.
lv_c = 'X'.
ENDLOOP.
CHECK lv_c = ''.
APPEND LINES OF gt_alv2 TO lt_alv2.
SORT lt_alv2 BY ebeln ebelp.
DELETE ADJACENT DUPLICATES FROM lt_alv2 COMPARING ebeln.
LOOP AT lt_alv2.
"订单抬头
CLEAR:poheader,poheaderx,po_no.
* poheader-po_number = . "采购订单
poheader-comp_code = lt_alv2-bukrs. "公司代码
IF lt_alv2-lifnr+4(1) = '0'.
poheader-doc_type = 'Z02'. "订单类型
ELSE.
poheader-doc_type = 'Z04'. "订单类型
ENDIF.
poheader-vendor = lt_alv2-lifnr. "供应商.
poheader-doc_date = sy-datum. "采购凭证日期
poheader-purch_org = lt_alv2-ekorg. "采购组织
poheader-pur_group = lt_alv2-ekgrp. "采购组
* poheader-pmnttrms = lt_alv2-zterm. "付款条件
poheader-currency = lt_alv2-waers. "币别
* poheader-ref_1 = lt_alv2-ihrez. "您的参考
* poheader-our_ref = lt_alv2-ebeln. "我们的参考
* poheader-collect_no = lt_alv2-submi. "汇总号
poheader-langu = sy-langu. "语言代码
poheader-pmnttrms = lt_alv2-zterm.
* poheaderx-po_number = c_x.
poheaderx-comp_code = c_x.
poheaderx-doc_type = c_x.
poheaderx-vendor = c_x.
poheaderx-doc_date = c_x.
poheaderx-purch_org = c_x.
poheaderx-pur_group = c_x.
* poheaderx-pmnttrms = c_x.
poheaderx-currency = c_x.
* poheaderx-ref_1 = c_x.
* poheaderx-sales_pers = c_x.
* poheaderx-our_ref = c_x.
* poheaderx-collect_no = c_x.
poheaderx-langu = c_x. "语言代码
poheaderx-pmnttrms = c_x.
**行项目/计划行
CLEAR:poitem[],poitemx[],lv_ebelp,posched[],poschedx[],pocond[],pocondx[],return[].
LOOP AT gt_alv2 INTO gs_alv2 WHERE ebeln = lt_alv2-ebeln.
lv_ebelp = lv_ebelp + 10.
CLEAR:poitem,poitemx,posched,poschedx,pocond,pocondx.
poitem-po_item = lv_ebelp. "采购订单行项目
poitemx-po_item = lv_ebelp. "采购订单行项目
poitem-acctasscat = gs_alv2-knttp. "科目分配类别
poitemx-acctasscat = c_x. "科目分配类别
poitem-item_cat = gs_alv2-pstyp. "项目类别
poitemx-item_cat = c_x. "项目类别
poitem-material = gs_alv2-matnr. "物料
poitemx-material = c_x . "物料
poitem-short_text = gs_alv2-txz01. "行项目文本
poitemx-short_text = c_x. "行项目文本
poitem-quantity = gs_alv2-menge. "采购订单数量
poitemx-quantity = c_x . "采购订单数量
poitem-po_unit = gs_alv2-meins. "订单单位
poitemx-po_unit = c_x. "订单单位
poitem-net_price = gs_alv2-netpr. "价格
poitemx-net_price = c_x. "价格
poitem-price_unit = gs_alv2-peinh ."价格单位
poitemx-price_unit = c_x . "价格单位
poitem-orderpr_un = gs_alv2-meins ."订单价格单位(采购).
poitemx-orderpr_un = c_x . "订单价格单位(采购).
poitem-plant = gs_alv2-werks. "工厂
poitemx-plant = c_x. "工厂
poitem-tax_code = gs_alv2-mwskz. "税码
poitemx-tax_code = c_x. "税码
poitem-ret_item = 'X'. "退货项目
poitemx-ret_item = c_x. "退货项目
APPEND poitem.
APPEND poitemx.
posched-po_item = lv_ebelp. "订单行项目
posched-sched_line = '0001'. "计划行
posched-delivery_date = sy-datum. "交货日期
posched-quantity = gs_alv2-menge. "交货数量
APPEND posched.
poschedx-po_item = lv_ebelp.
poschedx-sched_line = '0001'.
poschedx-po_itemx = c_x.
poschedx-sched_linex = c_x.
poschedx-delivery_date = c_x.
poschedx-quantity = c_x.
APPEND poschedx.
IF gs_alv2-netpr > 0 AND poheader-doc_type = 'Z04'.
pocond-itm_number = lv_ebelp.
pocond-cond_st_no = '001'.
pocond-cond_type = 'PBXX'. "定价条件
pocond-cond_value = gs_alv2-netpr. "采购订单货币的订单净值
pocond-cond_p_unt = gs_alv2-peinh .
pocond-currency = lt_alv2-waers.
CLEAR lv_esokz.
IF gs_alv2-pstyp = 'L' OR gs_alv2-pstyp = '3'.
lv_esokz = '3'.
ELSE.
lv_esokz = '0'.
ENDIF.
SELECT SINGLE kbetr INTO @DATA(lv_kbeter) FROM konp INNER JOIN a018 ON konp~knumh EQ a018~knumh
WHERE a018~matnr = @gs_alv2-matnr
AND a018~ekorg = @gs_alv2-ekorg
AND a018~lifnr = @gs_alv2-lifnr
AND a018~esokz = @lv_esokz
AND a018~datbi >= @sy-datum
AND a018~datab <= @sy-datum
AND konp~loevm_ko = ''
AND konp~kbetr > 0.
IF sy-subrc = 0.
pocond-cond_count = '02'.
pocond-change_id = 'I'.
pocond-cond_updat = 'X'.
pocond-condchaman = 'X'.
pocondx-change_id = c_x.
pocondx-cond_count = c_x.
ELSE.
SELECT SINGLE kbetr INTO @DATA(lv_kbeter2) FROM konp INNER JOIN a017 ON konp~knumh EQ a017~knumh
WHERE a017~matnr = @gs_alv2-matnr
AND a017~ekorg = @gs_alv2-ekorg
AND a017~lifnr = @gs_alv2-lifnr
AND a017~esokz = @lv_esokz
AND a017~datbi >= @sy-datum
AND a017~datab <= @sy-datum
AND konp~loevm_ko = ''
AND konp~kbetr > 0.
IF sy-subrc = 0.
pocond-cond_count = '02'.
pocond-change_id = 'I'.
pocond-cond_updat = 'X'.
pocond-condchaman = 'X'.
pocondx-change_id = c_x.
pocondx-cond_count = c_x.
ENDIF.
ENDIF.
APPEND pocond.
pocondx-itm_number = lv_ebelp.
pocondx-cond_st_no = '001'.
pocondx-cond_type = c_x. "定价条件
* pocondx-cond_count = c_x.
pocondx-cond_value = c_x.
pocondx-cond_p_unt = c_x.
pocondx-currency = c_x.
APPEND pocondx.
ENDIF.
gs_alv2-rbelp = lv_ebelp.
MODIFY gt_alv2 FROM gs_alv2 TRANSPORTING rbelp.
ENDLOOP.
CHECK poitem[] IS NOT INITIAL.
CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
poheader = poheader
poheaderx = poheaderx
* testrun = l_testrun
* no_price_from_po = 'X'
IMPORTING
exppurchaseorder = po_no
TABLES
return = return
poitem = poitem
poitemx = poitemx
poschedule = posched
poschedulex = poschedx
* poaccount = poaccount
* poaccountx = poaccountx
pocond = pocond
pocondx = pocondx.
* potextitem = potextitem
* potextheader = potextheader.
* pocomponents = pocomponents
* pocomponentsx = pocomponentsx.
* extensionin = extensionin.
CLEAR lv_msg.
LOOP AT return WHERE type = 'E'.
CONCATENATE lv_msg return-message INTO lv_msg SEPARATED BY ',' .
ENDLOOP.
IF sy-subrc EQ 0 .
CALL FUNCTION 'ICON_CREATE'
EXPORTING
name = icon_red_light
info = '有错误'
IMPORTING
result = lt_alv2-dr.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
LOOP AT gt_alv2 INTO gs_alv2 WHERE ebeln = lt_alv2-ebeln.
gs_alv2-rbelp = ''.
gs_alv2-dr = lt_alv2-dr.
gs_alv2-msg = lv_msg.
MODIFY gt_alv2 FROM gs_alv2 TRANSPORTING rbelp dr msg.
ENDLOOP.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
CALL FUNCTION 'ICON_CREATE'
EXPORTING
name = icon_okay
info = '创建成功 '
IMPORTING
result = lt_alv2-dr.
LOOP AT gt_alv2 INTO gs_alv2 WHERE ebeln = lt_alv2-ebeln.
gs_alv2-rbeln = po_no.
gs_alv2-dr = lt_alv2-dr.
MODIFY gt_alv2 FROM gs_alv2 TRANSPORTING rbeln dr.
ENDLOOP.
ENDIF.
ENDLOOP.
DATA:lt_zmmt013 TYPE TABLE OF zmmt013 WITH HEADER LINE.
DATA:lt_mt013 TYPE TABLE OF zmmt013 WITH HEADER LINE.
REFRESH:lt_mt013,lt_zmmt013.
SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_mt013 FROM zmmt013.
SORT lt_mt013 BY zno DESCENDING.
READ TABLE lt_mt013 INDEX 1.
LOOP AT gt_alv2 INTO gs_alv2 WHERE rbeln <> ''.
lt_mt013-zno = lt_mt013-zno + 1.
CLEAR lt_zmmt013.
MOVE-CORRESPONDING gs_alv2 TO lt_zmmt013.
lt_zmmt013-zno = lt_mt013-zno.
lt_zmmt013-crdat = sy-datum.
lt_zmmt013-crtim = sy-uzeit.
lt_zmmt013-crnam = sy-uname.
APPEND lt_zmmt013.
ENDLOOP.
IF lt_zmmt013[] IS NOT INITIAL.
MODIFY zmmt013 FROM TABLE lt_zmmt013.
COMMIT WORK.
ENDIF.
REFRESH: lt_alv2,return,poitem,poitemx,poaccount, poaccountx, potextitem,pocomponents,pocomponentsx,
posched,poschedx,pocond,pocondx.