2011年9月30日 星期五

[BAPI] BAPI_CONTRACT_CREATEFROMDATA

使用 BAPI 建立 Contract

定義
* BAPI Define
  DATA: gw_so_header          LIKE bapisdhd1,
        gw_so_headerx         LIKE bapisdhd1x,
        gw_so_logic_switch    LIKE bapisdls,
        gv_so_doc_in          TYPE bapivbeln-vbeln,
        gv_so_doc_num         TYPE bapivbeln-vbeln,
        gt_so_return2         TYPE bapiret2,
        gt_so_partners        TYPE bapiparnr  OCCURS 0 WITH HEADER LINE,
        gt_so_items_in        TYPE bapisditm  OCCURS 0 WITH HEADER LINE,
        gt_so_items_inx       TYPE bapisditmx OCCURS 0 WITH HEADER LINE,
        gt_so_return1         TYPE bapiret2   OCCURS 0 WITH HEADER LINE,
        gt_so_conditions_in   TYPE bapicond   OCCURS 0 WITH HEADER LINE,
        gt_so_conditions_inx  TYPE bapicondx  OCCURS 0 WITH HEADER LINE.

夥伴功能
*>> data_fill_partners
    CLEAR: gt_so_partners, gt_so_partners[].
    gt_so_partners-partn_role  = 'AG'.
    gt_so_partners-partn_numb  = itabsd-kunnr.
    APPEND: gt_so_partners.
    gt_so_partners-partn_role  = 'WE'. 
    APPEND: gt_so_partners.


表頭
*>> data_fill_header
    CLEAR: gw_so_header, gt_so_items_in[],
           gt_so_conditions_in[], gt_so_conditions_inx[],
           gw_so_logic_switch.
    gw_so_header-doc_type   = itabsd-auarts.
    gw_so_header-sales_org  = itabsd-vkorg. 
    gw_so_header-distr_chan = itabsd-vtweg. 
    gw_so_header-division   = itabsd-spart. 
    gw_so_header-sales_off  = itabsd-vkbur. 
    gw_so_header-sales_grp  = itabsd-vkgrp. 
    gw_so_header-purch_no_c = itabsd-bstnk. 
    gw_so_header-purch_date = itabsd-bstdk. 
    gw_so_header-pp_search  = itabsd-ktext. 
    gw_so_header-ct_valid_f = itabsd-user7. 
    gw_so_header-ct_valid_t = itabsd-user8. 
    gw_so_header-currency   = itabsd-waers. 
    gw_so_header-exchg_rate = v_kursf.      
    gw_so_logic_switch-cond_handl = 'X'.

項目
*>> data_fill_items
    CLEAR: gt_so_items_in, gt_so_items_inx.
    gt_so_items_in-item_categ = 'ZTAN'.
    gt_so_items_inx-itm_number =
    gt_so_items_in-itm_number  = '10'.
    gt_so_items_in-material    = 'MAT1'. 
    gt_so_items_inx-material   = 'X'.
    gt_so_items_in-target_qty  = v_psamt_0.
    gt_so_items_inx-target_qty = 'X'.
    gt_so_items_in-sales_unit  =
    gt_so_items_in-target_qu   =
    gt_so_items_in-t_unit_iso  = 'EA'.
    gt_so_items_inx-sales_unit =
    gt_so_items_inx-t_unit_iso =
    gt_so_items_inx-target_qu  = 'X'.
    APPEND: gt_so_items_in, gt_so_items_inx.

價格
*>> data_fill_price
    gt_so_conditions_in-cond_value  = '1'.         
    gt_so_conditions_inx-cond_value = 'X'.
    gt_so_conditions_in-cond_p_unt  = '1'.         
    gt_so_conditions_inx-cond_p_unt = 'X'.
    gt_so_conditions_in-currency    = itabsd-waers.
    gt_so_conditions_inx-currency   = 'X'.
    gt_so_conditions_in-itm_number  =              
    gt_so_conditions_inx-itm_number = '10'.
    gt_so_conditions_in-cond_st_no  =              
    gt_so_conditions_inx-cond_st_no = '10'.
    gt_so_conditions_in-cond_type   =
    gt_so_conditions_inx-cond_type  = 'ZPR0'.
    gt_so_conditions_in-cond_unit   = 'EA'.        
    gt_so_conditions_inx-cond_unit  = 'X'.
    gt_so_conditions_inx-updateflag = 'I'.
    gt_so_conditions_in-cond_count  =              
    gt_so_conditions_inx-cond_count = '00'.
    APPEND: gt_so_conditions_in, gt_so_conditions_inx.

呼叫 BAPI
*   Call BAPI
    gv_so_doc_in = itabsd-aufnr.
    CALL FUNCTION 'BAPI_CONTRACT_CREATEFROMDATA'
      EXPORTING
        salesdocumentin         = gv_so_doc_in
        contract_header_in      = gw_so_header
        logic_switch            = gw_so_logic_switch
      IMPORTING
        salesdocument           = gv_so_doc_num
      TABLES
        return                  = gt_so_return1
        contract_items_in       = gt_so_items_in
        contract_items_inx      = gt_so_items_inx
        contract_partners       = gt_so_partners
        contract_conditions_in  = gt_so_conditions_in
        contract_conditions_inx = gt_so_conditions_inx.

    IF  gt_so_return1-type EQ 'S'.
*    Commit BAPI
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait   = 'X'
        IMPORTING
          return = gt_so_return2.
    ENDIF.