FORM frm_billingdoc_create TABLES tp_din_item STRUCTURE zrmxsds006
* tp_din_cond STRUCTURE zrmxsds007
tp_msg STRUCTURE zifsret01
tp_dou_item STRUCTURE zrmxsds008
CHANGING cp_eind TYPE c
cp_vbeln TYPE c.
DATA: lt_billingdatain TYPE TABLE OF bapivbrk,
ls_billingdatain TYPE bapivbrk,
* lt_conditiondatain TYPE TABLE OF bapikomv,
* ls_conditiondatain TYPE bapikomv,
lt_success TYPE TABLE OF bapivbrksuccess,
ls_success TYPE bapivbrksuccess,
lt_return TYPE TABLE OF bapiret1,
ls_return TYPE bapiret1.
DATA: lt_din_item TYPE TABLE OF zifrmx_datain_013p01-item,
ls_din_item TYPE LINE OF zifrmx_datain_013p01-item,
* lt_din_cond TYPE TABLE OF zifrmx_datain_013p01-conds,
* ls_din_cond TYPE LINE OF zifrmx_datain_013p01-conds,
lt_dou_item TYPE TABLE OF zifrmx_dataout_013p01-item,
ls_dou_item TYPE LINE OF zifrmx_dataout_013p01-item,
ls_msg TYPE zifsret01,
lv_fkart TYPE vbrk-fkart VALUE 'ZR1',
lv_vbtyp TYPE vbrk-vbtyp VALUE 'C',
lv_index TYPE i.
CLEAR: lv_index.
* BAPI赋值
LOOP AT tp_din_item INTO ls_din_item.
lv_index = lv_index + 1.
ls_billingdatain-doc_number = ls_din_item-vbeln.
ls_billingdatain-itm_number = ls_din_item-posnr.
ls_billingdatain-ref_doc = ls_din_item-vbeln.
ls_billingdatain-ref_item = ls_din_item-posnr.
ls_billingdatain-ordbilltyp = lv_fkart.
ls_billingdatain-ref_doc_ca = lv_vbtyp.
ls_billingdatain-salesorg = ls_din_item-vkorg.
ls_billingdatain-distr_chan = ls_din_item-vtweg.
ls_billingdatain-division = ls_din_item-spart.
ls_billingdatain-sold_to = ls_din_item-kunnr_ag. "售达方
ls_billingdatain-ship_to = ls_din_item-kunnr_we. "送达方
ls_billingdatain-bill_to = ls_din_item-kunnr_re. "开票方
ls_billingdatain-payer = ls_din_item-kunnr_rg. "付款方
ls_billingdatain-payment_terms = ls_din_item-zterm.
ls_billingdatain-incoterms1 = ls_din_item-inco1.
ls_billingdatain-incoterms2 = ls_din_item-inco2.
ls_billingdatain-plant = ls_din_item-werks.
ls_billingdatain-material = ls_din_item-matnr.
ls_billingdatain-req_qty = ls_din_item-menge.
ls_billingdatain-sales_unit = ls_din_item-meins.
APPEND ls_billingdatain TO lt_billingdatain.
* LOOP AT tp_din_cond INTO ls_din_cond WHERE posnr = ls_din_item-posnr.
* ls_conditiondatain-data_index = lv_index.
* ls_conditiondatain-cond_type = ls_din_cond-kschl.
* ls_conditiondatain-cond_value = ls_din_cond-kbetr.
* ls_conditiondatain-cond_curr = ls_din_cond-koein.
* ls_conditiondatain-cond_p_unt = ls_din_cond-kpein.
* ls_conditiondatain-cond_d_unt = ls_din_cond-kmein.
*
* APPEND ls_conditiondatain TO lt_conditiondatain.
* CLEAR: ls_conditiondatain.
* ENDLOOP.
CLEAR: ls_billingdatain.
ENDLOOP.
CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'
TABLES
billingdatain = lt_billingdatain
* conditiondatain = lt_conditiondatain
* CCARDDATAIN =
* TEXTDATAIN =
* ERRORS =
return = lt_return
success = lt_success
.
IF lt_success IS INITIAL.
cp_eind = 'X'.
ENDIF.
IF cp_eind EQ 'X'.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
"返回成功的发票号
LOOP AT lt_success INTO ls_success.
CLEAR: ls_dou_item.
ls_dou_item-vbeln = ls_success-bill_doc.
ls_dou_item-posnr = ls_success-bill_doc_item.
APPEND ls_dou_item TO tp_dou_item.
cp_vbeln = ls_success-bill_doc.
ENDLOOP.
ENDIF.
LOOP AT lt_return INTO ls_return.
ls_msg-class = 'BUS'.
ls_msg-msgtyp = ls_return-type.
ls_msg-msgno = ls_return-number.
ls_msg-msgtxt = ls_return-message.
APPEND ls_msg TO tp_msg.
CLEAR ls_msg.
ENDLOOP.
ENDFORM. " FRM_BILLINGDOC_CREATE
由于销售发票预制从销售订单复制数据及价格条件,若需要修改价格条件,则该BAPI会插入新的条件,无法直接修改复制过来的价格条件,因此,将配置设置为不从销售订单复制数据,使用BAPI可以价格价格条件行项目,但是价格为0,无法加入。
最终,BAPI创建时不使用价格条件,完成后使用BDC加入价格条件。
*&---------------------------------------------------------------------*
*& Form FRM_BILLINGDOC_COND_BDC
*&---------------------------------------------------------------------*
* 使用BDC修改价格条件
*----------------------------------------------------------------------*
* -->P_LT_DIN_COND text
* -->P_LT_DOU_ITEM text
* -->P_LT_MSG text
* <--P_LV_EIND text
*----------------------------------------------------------------------*
FORM frm_billingdoc_cond_bdc TABLES tp_item STRUCTURE zrmxsds006
tp_cond STRUCTURE zrmxsds007
* tp_item_out STRUCTURE zrmxsds008
tp_msg STRUCTURE zifsret01
USING up_vbeln TYPE c
CHANGING cp_eind TYPE c
.
DATA: ls_item TYPE LINE OF zifrmx_datain_013p01-item,
ls_cond TYPE LINE OF zifrmx_datain_013p01-conds,
* ls_item_out TYPE LINE OF zifrmx_dataout_013p01-item,
ls_msg TYPE zifsret01.
DATA:
lv_field(14) TYPE c,
lv_kbetr(16) TYPE c,
lv_kpein(5) TYPE c.
CONSTANTS: cns_lines_per_page TYPE i VALUE 15.
DATA: lv_total_lines TYPE i,
lv_curr_line(2) TYPE n,
lv_index TYPE i,
lv_top_line TYPE i,
lv_pages TYPE i,
lv_next TYPE i,
lv_total_page TYPE i,
lv_total_next TYPE i.
REFRESH: gt_bdcdata[],
gt_bdcmsg[].
CLEAR: vbrk.
DO 10 TIMES.
SELECT SINGLE * FROM vbrk
WHERE vbeln = up_vbeln.
IF sy-subrc EQ 0.
EXIT.
ELSE.
WAIT UP TO 1 SECONDS.
ENDIF.
ENDDO.
IF vbrk IS INITIAL.
CLEAR ls_msg.
ls_msg-class = 'BUS'.
ls_msg-msgtyp = 'W'.
ls_msg-msgno = '102'.
ls_msg-msgtxt = '发票的价格条件没有写入!'.
APPEND ls_msg TO tp_msg.
RETURN.
ENDIF.
"第一屏:输入发票号
PERFORM frm_fill_bdc USING: 'X' 'SAPMV60A' '0101',
'' 'VBRK-VBELN' up_vbeln,
'' 'BDC_OKCODE' '/00'. "回车
DESCRIBE TABLE tp_item LINES lv_total_lines.
lv_total_page = lv_total_lines DIV cns_lines_per_page. "总页码
lv_total_next = lv_total_lines MOD cns_lines_per_page. "
LOOP AT tp_item INTO ls_item.
lv_index = sy-tabix.
lv_pages = lv_index DIV cns_lines_per_page.
lv_next = lv_index MOD cns_lines_per_page.
lv_curr_line = lv_index - lv_top_line.
CONCATENATE 'VBRP-POSNR(' lv_curr_line ')' INTO lv_field.
* 第二屏:双击第n行
PERFORM frm_fill_bdc USING: 'X' 'SAPMV60A' '0104',
'' 'BDC_CURSOR' lv_field, "'VBRP-POSNR(01)',
'' 'BDC_OKCODE' '=PFDE'. "双击
* 选择价格调价标签页
PERFORM frm_fill_bdc USING: 'X' 'SAPMV60A' '6002',
'' 'BDC_OKCODE' '=PFKO'. "标签页:条件
* 循环添加价格条件行
LOOP AT tp_cond INTO ls_cond WHERE posnr = ls_item-posnr.
lv_kbetr = ls_cond-kbetr.
lv_kpein = ls_cond-kpein.
PERFORM frm_fill_bdc USING: 'X' 'SAPMV60A' '6002',
'' 'BDC_OKCODE' '=V69A_KOAN'. "新增行
PERFORM frm_fill_bdc USING: 'X' 'SAPMV60A' '6002',
'' 'KOMV-KSCHL(02)' ls_cond-kschl, "
'' 'KOMV-KBETR(02)' lv_kbetr, "
'' 'RV61A-KOEIN(02)' ls_cond-koein, "
'' 'KOMV-KPEIN(02)' lv_kpein, "
'' 'KOMV-KMEIN(02)' ls_cond-kmein, "
'' 'BDC_OKCODE' '/00'. "
ENDLOOP.
* 返回第一屏
PERFORM frm_fill_bdc USING: 'X' 'SAPMV60A' '6002',
'' 'BDC_OKCODE' '=BACK'.
IF lv_pages > 0 AND lv_next = 0.
"翻页
PERFORM frm_fill_bdc USING: 'X' 'SAPMV60A' '0104',
'' 'BDC_OKCODE' '=P+'.
ENDIF.
"翻页后,最后一行在TabControl的最底部
lv_top_line = lv_pages * cns_lines_per_page.
IF lv_pages = lv_total_page AND lv_pages > 0.
lv_top_line = lv_top_line - ( cns_lines_per_page - lv_total_next ).
ENDIF.
ENDLOOP.
* save
PERFORM frm_fill_bdc USING: 'X' 'SAPMV60A' '0104',
'' 'BDC_OKCODE' '=SICH'.
CALL TRANSACTION 'VF02' USING gt_bdcdata
MODE gv_mode
UPDATE 'S'
MESSAGES INTO gt_bdcmsg.
COMMIT WORK AND WAIT.
DATA: lv_msg TYPE string.
LOOP AT gt_bdcmsg.
IF gt_bdcmsg-msgtyp = 'A' OR gt_bdcmsg-msgtyp = 'E'.
gt_bdcmsg-msgtyp = 'W'.
ENDIF.
MESSAGE ID sy-msgid
TYPE gt_bdcmsg-msgtyp
NUMBER gt_bdcmsg-msgnr
WITH gt_bdcmsg-msgv1 gt_bdcmsg-msgv2 gt_bdcmsg-msgv3 gt_bdcmsg-msgv4
INTO lv_msg.
CLEAR ls_msg.
ls_msg-class = 'BUS'.
ls_msg-msgtyp = gt_bdcmsg-msgtyp.
ls_msg-msgno = gt_bdcmsg-msgnr.
ls_msg-msgtxt = lv_msg.
APPEND ls_msg TO tp_msg.
ENDLOOP.
ENDFORM. " FRM_BILLINGDOC_COND_BDC
*&---------------------------------------------------------------------*
*& Form frm_fill_bdc
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_1 text
* -->P_2 text
* -->P_3 text
*----------------------------------------------------------------------*
FORM frm_fill_bdc USING p_1 p_2 p_3.
IF p_1 = 'X'.
gt_bdcdata-program = p_2.
gt_bdcdata-dynpro = p_3.
gt_bdcdata-dynbegin = 'X'.
ELSE.
gt_bdcdata-fnam = p_2.
gt_bdcdata-fval = p_3.
ENDIF.
APPEND gt_bdcdata.
CLEAR gt_bdcdata.
ENDFORM. " frm_fill_bdc