首页 > 其他分享 >APAP:销售订单创建BAPI

APAP:销售订单创建BAPI

时间:2022-12-16 17:00:50浏览次数:46  
标签:BAPI inx header zsdt001 lv 订单 ls APAP order

销售订单创建BAPI: SD_SALESDOCUMENT_CREATE

 DATA:lt_zsdt008 LIKE TABLE OF zsdt008,
       ls_zsdt008 LIKE LINE OF lt_zsdt008,
       ls_zsdt001 TYPE zsdt001,
       lt_ekko    LIKE TABLE OF ekko,
       ls_ekko    LIKE LINE OF lt_ekko,
       lt_ekpo    LIKE TABLE OF ekpo,
       ls_ekpo    LIKE LINE OF lt_ekpo.

  DATA: lv_str   TYPE string,
        lv_len   TYPE i,
        lv_fdpos TYPE sy-fdpos,
        l_waers  TYPE ekko-waers.

  DATA:lv_zscale TYPE zsdt006-zscale.

  CLEAR:lt_zsdt008,ls_zsdt008.
  SELECT * INTO TABLE lt_zsdt008 FROM zsdt008 WHERE zrfc_logid = i_id AND objtype = 'PO'.

  DATA:lv_canum TYPE zsdt008-canum.

  lv_canum = i_canum - 10.

  SORT lt_zsdt008 BY zrfc_logid canum.
  READ TABLE lt_zsdt008 INTO ls_zsdt008 INDEX 1.

  CLEAR:ls_zsdt001.
  SELECT SINGLE *
    INTO ls_zsdt001
    FROM zsdt001
   WHERE zrfcid = ls_zsdt008-zrfcid
     AND canum = i_canum.

  CLEAR:lt_ekko.
  SELECT *
    INTO TABLE lt_ekko
    FROM ekko
   WHERE ebeln = ls_zsdt008-objkey.

  CLEAR:ls_ekko.
  READ TABLE lt_ekko INTO ls_ekko INDEX 1.

  CLEAR:lt_ekpo,ls_ekpo.
  SELECT *
    INTO TABLE lt_ekpo
    FROM ekpo
   WHERE ebeln = ls_zsdt008-objkey.
*  CLEAR:lv_zscale.
*  SELECT SINGLE zscale INTO lv_zscale FROM zsdt006 WHERE bukrs = ls_zsdt001-bukrs AND werks = ls_zsdt001-werks
*          AND zbukrs = ls_zsdt001-zscbks AND zwerks = ls_zsdt001-zscwrk AND datab <= sy-datum AND datbi >= sy-datum.
*  "判断货币,屏幕输入币别
*  DATA:lv_zkoein TYPE zsdt007-zkoein.
*  CLEAR:lv_zkoein.
*  SELECT SINGLE zkoein INTO lv_zkoein FROM zsdt007 WHERE zrfc_logid = i_id.
*  IF lv_zkoein IS NOT INITIAL.
*    CLEAR:lv_str,lv_fdpos,l_waers.
*    CONCATENATE ls_zsdt001-zrfcid ls_zsdt001-canum INTO lv_str.
*    lv_len = cl_abap_list_utilities=>dynamic_output_length( lv_str ).
*    SEARCH lv_zkoein FOR lv_str.
*    IF sy-subrc = 0.
*      lv_fdpos = sy-fdpos.
*      lv_len = lv_len + lv_fdpos.
*      l_waers = lv_zkoein+lv_len(3).
**      IF l_waers <> ls_ekko-waers.
**        SELECT SINGLE ukurs INTO lv_ukurs FROM zsdt005 WHERE erdat = sy-datum+0(6)
**                 AND fcuur = ls_ekko-waers AND tcuur = l_waers.
**        IF lv_ukurs IS INITIAL.
**          e_code = 'E'.
**          e_msg  = '没有找到汇率'.
**          RETURN.
**        ENDIF.
**      ENDIF.
*    ENDIF.
*  ENDIF.

  PERFORM create_so TABLES lt_ekko lt_ekpo et_return USING i_id i_canum ls_zsdt001 lv_zscale l_waers CHANGING e_code e_msg e_flow_end.


FORM create_so  TABLES  p_lt_ekko   STRUCTURE ekko
                         p_lt_ekpo    STRUCTURE ekpo
                         p_et_return  STRUCTURE  zrfct005
                USING    p_i_id       TYPE zrfc_logid
                         p_i_canum    TYPE canum
                         p_ls_zsdt001 TYPE zsdt001
*                         p_ukurs TYPE zsdt005-ukurs
                         p_zscale TYPE zsdt006-zscale
                         p_waers TYPE ekko-waers
                CHANGING p_e_code     TYPE  msgty
                         p_e_msg      TYPE  msgtxt
                         p_e_flow_end TYPE  char01.

  DATA:salesdocument        TYPE bapivbeln-vbeln,
       order_header_in      TYPE bapisdhd1,
       order_header_inx     TYPE bapisdhd1x,
       order_partners       LIKE TABLE OF bapiparnr WITH HEADER LINE,
       return               LIKE TABLE OF bapiret2 WITH HEADER LINE,
       order_items_in       LIKE TABLE OF bapisditm WITH HEADER LINE,
       order_items_inx      LIKE TABLE OF bapisditmx WITH HEADER LINE,
       order_schedules_in   LIKE TABLE OF bapischdl WITH HEADER LINE,
       order_schedules_inx  LIKE TABLE OF bapischdlx WITH HEADER LINE,
       lt_extensionin       TYPE TABLE OF bapiparex,
       ls_extensionin       TYPE bapiparex,
       order_conditions_in  LIKE TABLE OF bapicond WITH HEADER LINE,
       order_conditions_inx LIKE TABLE OF bapicondx WITH HEADER LINE,
       lt_zsdt008           LIKE TABLE OF zsdt008,
       ls_zsdt008           LIKE LINE OF lt_zsdt008,
       logic_switch         TYPE bapisdls   OCCURS 0 WITH HEADER LINE,
       lv_waerk             TYPE waerk.


  CLEAR:order_header_in,
        order_header_inx,
        order_partners[],
        order_partners,
        order_items_in[],
        order_items_in,
        order_items_inx[],
        order_items_inx,
        order_schedules_in[],
        order_schedules_in,
        order_schedules_inx[],
        order_schedules_inx,
        return[],
        return,lv_waerk,
        salesdocument,
        lt_zsdt008,
        ls_zsdt008.
  DATA:ls_ekko TYPE ekko,
       ls_ekpo TYPE ekpo.

  DATA:ls_zrfct002 TYPE zrfct002.
  DATA:ls_source TYPE TABLE OF zrfcs001.
  READ TABLE p_lt_ekko INTO ls_ekko INDEX 1.
*  DATA:ls_zsdt002 TYPE zsdt002.
  DATA:lv_bsart TYPE zsdt007-bsart,
       lv_bstnk TYPE zsdt007-bstnk.
  CLEAR:lv_bsart,lv_bstnk.
  DATA:lv_num   TYPE i,
       lv_posnr TYPE posnr.
  DATA:lt_zsdt007 TYPE TABLE OF zsdt007,
       ls_zsdt007 TYPE zsdt007.

  DATA:lv_kunnr TYPE kunnr.
  DATA:lv_kunag TYPE kunag.
  CLEAR:lv_num.
  CLEAR:lt_zsdt007.
  SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_zsdt007 FROM zsdt007 WHERE zrfc_logid = p_i_id.

  SELECT SINGLE bsart bstnk INTO (lv_bsart,lv_bstnk) FROM zsdt007 WHERE zrfc_logid = p_i_id.
*  SELECT SINGLE *  INTO ls_zsdt002 FROM zsdt002
*      WHERE zrfcid = p_ls_zsdt001-zrfcid AND canum = p_ls_zsdt001-canum AND bsart = lv_bsart..

  "
*  IF p_ukurs IS INITIAL.
*    p_ukurs = 1.
*  ENDIF.
  IF p_zscale IS INITIAL.
    p_zscale = 1.
  ENDIF.
  "销售订单抬头
  order_header_in-doc_type    = p_ls_zsdt001-auart."订单类型
  order_header_inx-doc_type   = 'X'.

  order_header_in-distr_chan  = p_ls_zsdt001-vtweg."分销渠道
  order_header_inx-distr_chan = 'X'.

  order_header_in-division    = p_ls_zsdt001-spart."产品组
  order_header_inx-division   = 'X'.

  order_header_in-sales_org   = p_ls_zsdt001-vkorg."销售组织
  order_header_inx-sales_org  = 'X'.
***ADD BY JT 20221211 S凭证日期
  order_header_in-doc_date = ls_ekko-bedat.
  order_header_inx-doc_date = 'X'.
***ADD BY JT 20221211 E凭证日期
  SELECT SINGLE eindt INTO order_header_in-req_date_h FROM eket WHERE ebeln = ls_ekko-ebeln.
*  ORDER_HEADER_IN-REQ_DATE_H = LS_VBAK-VDATU."请求交货日期
  order_header_inx-req_date_h = 'X'.

*  SELECT SINGLE eindt INTO order_header_in-purch_date FROM eket WHERE ebeln = ls_ekko-ebeln.
  order_header_in-purch_date = ls_ekko-bedat."客户参考日期
* order_header_in-purch_date = ls_vbak-vdatu."客户参考日期
  order_header_inx-purch_date = 'X'."客户参考日期

  IF lv_bstnk IS NOT INITIAL.
    order_header_in-purch_no_c = lv_bstnk.. "客户采购订单订单
  ELSE.
    order_header_in-purch_no_c = ls_ekko-ebeln. "客户采购订单订单
  ENDIF.
  order_header_inx-purch_no_c = 'X'.

  "售达方/付款方/开票方/送达方
*  DATA:lv_kunnr TYPE kunnr.
*  SELECT SINGLE kunnr INTO lv_kunnr FROM zsdt004 WHERE bukrs = p_ls_zsdt001-bukrs AND werks = p_ls_zsdt001-werks
*    AND waers = order_header_in-currency AND canum = p_i_canum AND zrfcid = p_ls_zsdt001-zrfcid..

  IF p_ls_zsdt001-kunag IS NOT INITIAL.
    lv_kunag = p_ls_zsdt001-kunag.
  ELSE.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = ls_ekko-bukrs
      IMPORTING
        output = lv_kunag.
  ENDIF.
  READ TABLE lt_zsdt007 INTO ls_zsdt007 INDEX 1.

*  IF ls_zsdt007-bsart = 'ZITO' OR ls_zsdt007-bsart = 'ZFTO'.
*
*    "*==行项目条件货币
*    IF p_ls_zsdt001-waers_l IS NOT INITIAL.
*      lv_waerk = p_ls_zsdt001-waers_l.  "行项目条件货币
*    ELSE.
*      lv_waerk = ls_zsdt007-koein_y.  "原货币码
*    ENDIF.

  "*==抬头货币
*    IF p_ls_zsdt001-waerk IS NOT INITIAL.
*      order_header_in-currency = p_ls_zsdt001-waerk.  "货币码
*    ELSE.
*      order_header_in-currency = ls_zsdt007-koein_y."原货币
*    ENDIF.
*    lv_waerk = order_header_in-currency.
  IF p_ls_zsdt001-waerk IS NOT INITIAL.
    order_header_in-currency = p_ls_zsdt001-waerk.
  ELSE.
    SELECT SINGLE waers INTO order_header_in-currency
                        FROM knvv
                       WHERE kunnr = lv_kunag
                         AND vkorg = p_ls_zsdt001-vkorg
                         AND vtweg = p_ls_zsdt001-vtweg
                         AND spart = p_ls_zsdt001-spart.
*    ENDIF.
*  ELSE.
****UPDATE JT 20221128 S 抬头币别
*    lv_waerk = ls_zsdt007-koein_y.  "原货币
*    IF p_ls_zsdt001-waerk IS NOT INITIAL.
*      order_header_in-currency = p_ls_zsdt001-waerk.  "货币码
*    ELSE.
*      order_header_in-currency = lv_waerk.  "原货币
*    ENDIF.
****UPDATE JT 20221128 E 抬头币别
  ENDIF.

  order_header_inx-currency = 'X'.

  order_header_in-ship_type = '01'.
  order_header_inx-ship_type = 'X'.

  DATA:ls_zsdt009 TYPE zsdt009.
  SELECT SINGLE * INTO ls_zsdt009 FROM zsdt009 WHERE kunnr = p_ls_zsdt001-kunnr.
  IF ls_zsdt009-augru IS  NOT INITIAL.
    order_header_in-ord_reason = ls_zsdt009-augru."订单原因
    order_header_inx-ord_reason = 'X'.
  ENDIF.

  IF p_ls_zsdt001-kunnr IS NOT INITIAL.
    lv_kunnr = p_ls_zsdt001-kunnr.
  ELSE.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = ls_ekko-bukrs
      IMPORTING
        output = lv_kunnr.
  ENDIF.


  order_partners-partn_role = 'AG'.
  order_partners-partn_numb = lv_kunnr.
  APPEND order_partners.
  order_partners-partn_role = 'WE'.
  order_partners-partn_numb = lv_kunag.
  APPEND order_partners.
  order_partners-partn_role = 'RE'.
  order_partners-partn_numb =  lv_kunnr.
  APPEND order_partners.
  order_partners-partn_role = 'RG'.
  order_partners-partn_numb = lv_kunnr.
  APPEND order_partners.
  CLEAR:order_partners.

  "销售订单行项目
  LOOP AT p_lt_ekpo INTO ls_ekpo.
****ADD BY ZJ 20221204 S 免费行项目不显示
    IF ls_ekpo-uebpo <> '' AND ls_ekpo-uptyp = '4'.
      CONTINUE.
    ENDIF.
****ADD BY ZJ 20221204 E 免费行项目不显示
    order_items_in-itm_number  = ls_ekpo-ebelp."行项目
    order_items_inx-itm_number = ls_ekpo-ebelp.

    order_items_in-material    = ls_ekpo-matnr."物料
    order_items_inx-material   = 'X'.

    order_items_in-target_qty  = ls_ekpo-menge."数量
    order_items_inx-target_qty = 'X'.
****ADD BY ZJ 20221203 S 数量单位
    order_items_in-sales_unit = ls_ekpo-meins. "单位
    order_items_inx-sales_unit = 'X'.
****ADD BY ZJ 20221203 E 数量单位
    order_items_in-plant = p_ls_zsdt001-werks. "工厂
    order_items_inx-plant = 'X'.

    order_items_in-price_date = sy-datum. "定价日期
    order_items_inx-price_date = 'X'.

    order_items_in-po_itm_no = ls_ekpo-ebelp.
    order_items_inx-po_itm_no = 'X'.
****ADD BY ZJ 20221203 S 转换因子
    order_items_in-salqtynum = ls_ekpo-umrez.
    order_items_in-salqtyden = ls_ekpo-umren.

    order_items_inx-salqtynum = 'X'.
    order_items_inx-salqtyden = 'X'.
****ADD BY ZJ 20221203 E 转换因子

    order_items_in-po_method = ls_ekko-bsart.
    order_items_inx-po_method = 'X'.

    APPEND:order_items_in,order_items_inx.
    CLEAR:order_items_in,order_items_inx.

    order_schedules_in-itm_number  = ls_ekpo-ebelp.
    order_schedules_inx-itm_number = ls_ekpo-ebelp.
    order_schedules_in-req_qty     = ls_ekpo-menge.
    order_schedules_inx-req_qty    = 'X'.
    SELECT SINGLE eindt "行项目计划日期
      INTO order_schedules_in-req_date
      FROM eket
     WHERE ebeln = ls_ekpo-ebeln
       AND ebelp = ls_ekpo-ebelp
       .
    order_schedules_inx-req_date   = 'X'.
    APPEND:order_schedules_in,order_schedules_inx.
    CLEAR:order_schedules_in,order_schedules_inx.

    "重新定价条件
    IF p_ls_zsdt001-kschl IS NOT INITIAL.
      lv_num = lv_num + 1.
      CLEAR:ls_zsdt007.
      READ TABLE lt_zsdt007 INTO ls_zsdt007 INDEX lv_num.
      order_conditions_in-itm_number = ls_ekpo-ebelp.  "itm_number
      order_conditions_in-cond_type  = p_ls_zsdt001-kschl.            "定价条件
*      order_conditions_in-cond_value = ( ls_ekpo-brtwr / ls_ekpo-menge * ls_ekpo-peinh )." * p_ukurs * p_zscale . "定价值
*      order_conditions_in-currency   = order_header_in-currency.    "

****UPDATE JT 20221128 S 条件币别
*      order_conditions_in-currency = lv_waerk.
      SELECT SINGLE waers kmein kbetr
               INTO ( order_conditions_in-currency,
                      order_conditions_in-cond_unit,
                      order_conditions_in-cond_value )
               FROM konv
              WHERE knumv = ls_ekko-knumv
                AND kposn = ls_ekpo-ebelp
                AND kinak = ''
                AND ( kschl = 'PB00' OR kschl = 'PBXX' ) .
****UPDATE JT 20221128 E 条件币别
*====================================================================*
      "*== add Tuq 16.11.2022 10:05:41
*      IF order_header_in-currency <> ls_ekko-waers.     "汇率转换
*      IF order_conditions_in-currency <> ls_ekko-waers.     "汇率转换
*        CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
*          EXPORTING
*            date             = sy-datum
*            foreign_amount   = order_conditions_in-cond_value
*            foreign_currency = ls_ekko-waers
**           local_currency   = order_header_in-currency
*            local_currency   = order_conditions_in-currency "*==行项目条件货币
*            type_of_rate     = 'M'
*          IMPORTING
*            local_amount     = order_conditions_in-cond_value
*          EXCEPTIONS
*            no_rate_found    = 1
*            OTHERS           = 2.
*      ENDIF.
      "*== end Tuq 16.11.2022 10:05:41
      order_conditions_in-cond_p_unt  = ls_ekpo-peinh..  "条件单位
      APPEND order_conditions_in.
      CLEAR order_conditions_in.

      order_conditions_inx-itm_number = ls_ekpo-ebelp.
      order_conditions_inx-cond_type  = p_ls_zsdt001-kschl.
      order_conditions_inx-cond_value = 'X'.
      order_conditions_inx-currency   = 'X'.
      order_conditions_inx-cond_unit  = 'X'.
      order_conditions_inx-cond_p_unt = 'X'.
      APPEND order_conditions_inx.
      CLEAR  order_conditions_inx.
    ENDIF.
    CLEAR:ls_ekpo.
  ENDLOOP.

  logic_switch-pricing = 'G'.
  APPEND logic_switch.
  CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'
    EXPORTING
      sales_header_in      = order_header_in
      sales_header_inx     = order_header_inx
      logic_switch         = logic_switch
    IMPORTING
      salesdocument_ex     = salesdocument
    TABLES
      return               = return[]
      sales_items_in       = order_items_in[]
      sales_items_inx      = order_items_inx[]
      sales_partners       = order_partners[]
      sales_schedules_in   = order_schedules_in[]
      sales_schedules_inx  = order_schedules_inx[]
      sales_conditions_in  = order_conditions_in[]
      sales_conditions_inx = order_conditions_inx[]
      extensionin          = lt_extensionin.

  LOOP AT return WHERE type = 'E' OR type = 'A'.
    CONCATENATE p_e_msg return-message INTO p_e_msg SEPARATED BY '/'.
  ENDLOOP.
  IF sy-subrc <> '0'.
    CALL METHOD zcl_rfc=>get_source
      EXPORTING
        i_id       = p_i_id
        i_canum    = p_i_canum
      IMPORTING
        e_zrfct002 = ls_zrfct002.
*      RECEIVING
*        R_SOURCE   = LS_SOURCE.

    CLEAR:ls_zsdt008.
    ls_zsdt008-canum      = ls_zrfct002-canum.
    ls_zsdt008-objtype    = ls_zrfct002-objtype_save.
    ls_zsdt008-objkey     = salesdocument.
    ls_zsdt008-zrfcid     = ls_zrfct002-zrfcid.
    ls_zsdt008-zrfc_logid = p_i_id.
    APPEND ls_zsdt008 TO lt_zsdt008.

    MODIFY zsdt008 FROM TABLE lt_zsdt008.

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
    p_e_code = 'S'.
    CONCATENATE 'SO:' salesdocument '创建成功' INTO p_e_msg.
    DATA:ls_zrfct005 TYPE zrfct005.
    CLEAR:ls_zrfct005.
    ls_zrfct005-status = '53'.
    ls_zrfct005-msgty = p_e_code.
    ls_zrfct005-msgid = '00'.
    ls_zrfct005-msgno = '001'.
    ls_zrfct005-msgv1 = p_e_msg.
    ls_zrfct005-msgv4 = salesdocument.
    APPEND ls_zrfct005 TO p_et_return.
*    P_ET_RETURN[] = VALUE #( (
*                            STATUS = '53'  "通过状态53来保存KEY数据
*                            MSGTY = P_E_CODE
*                            MSGID = '00'
*                            MSGNO = '001'
*                            MSGV1 = P_E_MSG
*                            MSGV4 = SALESDOCUMENT ) ).
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    p_e_code = 'E'.
  ENDIF.


  CLEAR:order_header_in,
        order_header_inx,
        order_partners[],
        order_partners,
        order_items_in[],
        order_items_in,
        order_items_inx[],
        order_items_inx,
        order_schedules_in[],
        order_schedules_in,
        order_schedules_inx[],
        order_schedules_inx,
        return[],
        return,
        salesdocument,
        lt_zsdt008,
        ls_zsdt008,
        logic_switch[].

ENDFORM.    
 DATA:lt_zsdt008 LIKE TABLE OF zsdt008,
       ls_zsdt008 LIKE LINE OF lt_zsdt008,
       ls_zsdt001 TYPE zsdt001,
       lt_ekko    LIKE TABLE OF ekko,
       ls_ekko    LIKE LINE OF lt_ekko,
       lt_ekpo    LIKE TABLE OF ekpo,
       ls_ekpo    LIKE LINE OF lt_ekpo.

  DATA: lv_str   TYPE string,
        lv_len   TYPE i,
        lv_fdpos TYPE sy-fdpos,
        l_waers  TYPE ekko-waers.

  DATA:lv_zscale TYPE zsdt006-zscale.

  CLEAR:lt_zsdt008,ls_zsdt008.
  SELECT * INTO TABLE lt_zsdt008 FROM zsdt008 WHERE zrfc_logid = i_id AND objtype = 'PO'.

  DATA:lv_canum TYPE zsdt008-canum.

  lv_canum = i_canum - 10.

  SORT lt_zsdt008 BY zrfc_logid canum.
  READ TABLE lt_zsdt008 INTO ls_zsdt008 INDEX 1.

  CLEAR:ls_zsdt001.
  SELECT SINGLE *
    INTO ls_zsdt001
    FROM zsdt001
   WHERE zrfcid = ls_zsdt008-zrfcid
     AND canum = i_canum.

  CLEAR:lt_ekko.
  SELECT *
    INTO TABLE lt_ekko
    FROM ekko
   WHERE ebeln = ls_zsdt008-objkey.

  CLEAR:ls_ekko.
  READ TABLE lt_ekko INTO ls_ekko INDEX 1.

  CLEAR:lt_ekpo,ls_ekpo.
  SELECT *
    INTO TABLE lt_ekpo
    FROM ekpo
   WHERE ebeln = ls_zsdt008-objkey.
*  CLEAR:lv_zscale.
*  SELECT SINGLE zscale INTO lv_zscale FROM zsdt006 WHERE bukrs = ls_zsdt001-bukrs AND werks = ls_zsdt001-werks
*          AND zbukrs = ls_zsdt001-zscbks AND zwerks = ls_zsdt001-zscwrk AND datab <= sy-datum AND datbi >= sy-datum.
*  "判断货币,屏幕输入币别
*  DATA:lv_zkoein TYPE zsdt007-zkoein.
*  CLEAR:lv_zkoein.
*  SELECT SINGLE zkoein INTO lv_zkoein FROM zsdt007 WHERE zrfc_logid = i_id.
*  IF lv_zkoein IS NOT INITIAL.
*    CLEAR:lv_str,lv_fdpos,l_waers.
*    CONCATENATE ls_zsdt001-zrfcid ls_zsdt001-canum INTO lv_str.
*    lv_len = cl_abap_list_utilities=>dynamic_output_length( lv_str ).
*    SEARCH lv_zkoein FOR lv_str.
*    IF sy-subrc = 0.
*      lv_fdpos = sy-fdpos.
*      lv_len = lv_len + lv_fdpos.
*      l_waers = lv_zkoein+lv_len(3).
**      IF l_waers <> ls_ekko-waers.
**        SELECT SINGLE ukurs INTO lv_ukurs FROM zsdt005 WHERE erdat = sy-datum+0(6)
**                 AND fcuur = ls_ekko-waers AND tcuur = l_waers.
**        IF lv_ukurs IS INITIAL.
**          e_code = 'E'.
**          e_msg  = '没有找到汇率'.
**          RETURN.
**        ENDIF.
**      ENDIF.
*    ENDIF.
*  ENDIF.

  PERFORM create_so TABLES lt_ekko lt_ekpo et_return USING i_id i_canum ls_zsdt001 lv_zscale l_waers CHANGING e_code e_msg e_flow_end.


FORM create_so  TABLES  p_lt_ekko   STRUCTURE ekko
                         p_lt_ekpo    STRUCTURE ekpo
                         p_et_return  STRUCTURE  zrfct005
                USING    p_i_id       TYPE zrfc_logid
                         p_i_canum    TYPE canum
                         p_ls_zsdt001 TYPE zsdt001
*                         p_ukurs TYPE zsdt005-ukurs
                         p_zscale TYPE zsdt006-zscale
                         p_waers TYPE ekko-waers
                CHANGING p_e_code     TYPE  msgty
                         p_e_msg      TYPE  msgtxt
                         p_e_flow_end TYPE  char01.

  DATA:salesdocument        TYPE bapivbeln-vbeln,
       order_header_in      TYPE bapisdhd1,
       order_header_inx     TYPE bapisdhd1x,
       order_partners       LIKE TABLE OF bapiparnr WITH HEADER LINE,
       return               LIKE TABLE OF bapiret2 WITH HEADER LINE,
       order_items_in       LIKE TABLE OF bapisditm WITH HEADER LINE,
       order_items_inx      LIKE TABLE OF bapisditmx WITH HEADER LINE,
       order_schedules_in   LIKE TABLE OF bapischdl WITH HEADER LINE,
       order_schedules_inx  LIKE TABLE OF bapischdlx WITH HEADER LINE,
       lt_extensionin       TYPE TABLE OF bapiparex,
       ls_extensionin       TYPE bapiparex,
       order_conditions_in  LIKE TABLE OF bapicond WITH HEADER LINE,
       order_conditions_inx LIKE TABLE OF bapicondx WITH HEADER LINE,
       lt_zsdt008           LIKE TABLE OF zsdt008,
       ls_zsdt008           LIKE LINE OF lt_zsdt008,
       logic_switch         TYPE bapisdls   OCCURS 0 WITH HEADER LINE,
       lv_waerk             TYPE waerk.


  CLEAR:order_header_in,
        order_header_inx,
        order_partners[],
        order_partners,
        order_items_in[],
        order_items_in,
        order_items_inx[],
        order_items_inx,
        order_schedules_in[],
        order_schedules_in,
        order_schedules_inx[],
        order_schedules_inx,
        return[],
        return,lv_waerk,
        salesdocument,
        lt_zsdt008,
        ls_zsdt008.
  DATA:ls_ekko TYPE ekko,
       ls_ekpo TYPE ekpo.

  DATA:ls_zrfct002 TYPE zrfct002.
  DATA:ls_source TYPE TABLE OF zrfcs001.
  READ TABLE p_lt_ekko INTO ls_ekko INDEX 1.
*  DATA:ls_zsdt002 TYPE zsdt002.
  DATA:lv_bsart TYPE zsdt007-bsart,
       lv_bstnk TYPE zsdt007-bstnk.
  CLEAR:lv_bsart,lv_bstnk.
  DATA:lv_num   TYPE i,
       lv_posnr TYPE posnr.
  DATA:lt_zsdt007 TYPE TABLE OF zsdt007,
       ls_zsdt007 TYPE zsdt007.

  DATA:lv_kunnr TYPE kunnr.
  DATA:lv_kunag TYPE kunag.
  CLEAR:lv_num.
  CLEAR:lt_zsdt007.
  SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_zsdt007 FROM zsdt007 WHERE zrfc_logid = p_i_id.

  SELECT SINGLE bsart bstnk INTO (lv_bsart,lv_bstnk) FROM zsdt007 WHERE zrfc_logid = p_i_id.
*  SELECT SINGLE *  INTO ls_zsdt002 FROM zsdt002
*      WHERE zrfcid = p_ls_zsdt001-zrfcid AND canum = p_ls_zsdt001-canum AND bsart = lv_bsart..

  "
*  IF p_ukurs IS INITIAL.
*    p_ukurs = 1.
*  ENDIF.
  IF p_zscale IS INITIAL.
    p_zscale = 1.
  ENDIF.
  "销售订单抬头
  order_header_in-doc_type    = p_ls_zsdt001-auart."订单类型
  order_header_inx-doc_type   = 'X'.

  order_header_in-distr_chan  = p_ls_zsdt001-vtweg."分销渠道
  order_header_inx-distr_chan = 'X'.

  order_header_in-division    = p_ls_zsdt001-spart."产品组
  order_header_inx-division   = 'X'.

  order_header_in-sales_org   = p_ls_zsdt001-vkorg."销售组织
  order_header_inx-sales_org  = 'X'.
***ADD BY JT 20221211 S凭证日期
  order_header_in-doc_date = ls_ekko-bedat.
  order_header_inx-doc_date = 'X'.
***ADD BY JT 20221211 E凭证日期
  SELECT SINGLE eindt INTO order_header_in-req_date_h FROM eket WHERE ebeln = ls_ekko-ebeln.
*  ORDER_HEADER_IN-REQ_DATE_H = LS_VBAK-VDATU."请求交货日期
  order_header_inx-req_date_h = 'X'.

*  SELECT SINGLE eindt INTO order_header_in-purch_date FROM eket WHERE ebeln = ls_ekko-ebeln.
  order_header_in-purch_date = ls_ekko-bedat."客户参考日期
* order_header_in-purch_date = ls_vbak-vdatu."客户参考日期
  order_header_inx-purch_date = 'X'."客户参考日期

  IF lv_bstnk IS NOT INITIAL.
    order_header_in-purch_no_c = lv_bstnk.. "客户采购订单订单
  ELSE.
    order_header_in-purch_no_c = ls_ekko-ebeln. "客户采购订单订单
  ENDIF.
  order_header_inx-purch_no_c = 'X'.

  "售达方/付款方/开票方/送达方
*  DATA:lv_kunnr TYPE kunnr.
*  SELECT SINGLE kunnr INTO lv_kunnr FROM zsdt004 WHERE bukrs = p_ls_zsdt001-bukrs AND werks = p_ls_zsdt001-werks
*    AND waers = order_header_in-currency AND canum = p_i_canum AND zrfcid = p_ls_zsdt001-zrfcid..

  IF p_ls_zsdt001-kunag IS NOT INITIAL.
    lv_kunag = p_ls_zsdt001-kunag.
  ELSE.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = ls_ekko-bukrs
      IMPORTING
        output = lv_kunag.
  ENDIF.
  READ TABLE lt_zsdt007 INTO ls_zsdt007 INDEX 1.

*  IF ls_zsdt007-bsart = 'ZITO' OR ls_zsdt007-bsart = 'ZFTO'.
*
*    "*==行项目条件货币
*    IF p_ls_zsdt001-waers_l IS NOT INITIAL.
*      lv_waerk = p_ls_zsdt001-waers_l.  "行项目条件货币
*    ELSE.
*      lv_waerk = ls_zsdt007-koein_y.  "原货币码
*    ENDIF.

  "*==抬头货币
*    IF p_ls_zsdt001-waerk IS NOT INITIAL.
*      order_header_in-currency = p_ls_zsdt001-waerk.  "货币码
*    ELSE.
*      order_header_in-currency = ls_zsdt007-koein_y."原货币
*    ENDIF.
*    lv_waerk = order_header_in-currency.
  IF p_ls_zsdt001-waerk IS NOT INITIAL.
    order_header_in-currency = p_ls_zsdt001-waerk.
  ELSE.
    SELECT SINGLE waers INTO order_header_in-currency
                        FROM knvv
                       WHERE kunnr = lv_kunag
                         AND vkorg = p_ls_zsdt001-vkorg
                         AND vtweg = p_ls_zsdt001-vtweg
                         AND spart = p_ls_zsdt001-spart.
*    ENDIF.
*  ELSE.
****UPDATE JT 20221128 S 抬头币别
*    lv_waerk = ls_zsdt007-koein_y.  "原货币
*    IF p_ls_zsdt001-waerk IS NOT INITIAL.
*      order_header_in-currency = p_ls_zsdt001-waerk.  "货币码
*    ELSE.
*      order_header_in-currency = lv_waerk.  "原货币
*    ENDIF.
****UPDATE JT 20221128 E 抬头币别
  ENDIF.

  order_header_inx-currency = 'X'.

  order_header_in-ship_type = '01'.
  order_header_inx-ship_type = 'X'.

  DATA:ls_zsdt009 TYPE zsdt009.
  SELECT SINGLE * INTO ls_zsdt009 FROM zsdt009 WHERE kunnr = p_ls_zsdt001-kunnr.
  IF ls_zsdt009-augru IS  NOT INITIAL.
    order_header_in-ord_reason = ls_zsdt009-augru."订单原因
    order_header_inx-ord_reason = 'X'.
  ENDIF.

  IF p_ls_zsdt001-kunnr IS NOT INITIAL.
    lv_kunnr = p_ls_zsdt001-kunnr.
  ELSE.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = ls_ekko-bukrs
      IMPORTING
        output = lv_kunnr.
  ENDIF.


  order_partners-partn_role = 'AG'.
  order_partners-partn_numb = lv_kunnr.
  APPEND order_partners.
  order_partners-partn_role = 'WE'.
  order_partners-partn_numb = lv_kunag.
  APPEND order_partners.
  order_partners-partn_role = 'RE'.
  order_partners-partn_numb =  lv_kunnr.
  APPEND order_partners.
  order_partners-partn_role = 'RG'.
  order_partners-partn_numb = lv_kunnr.
  APPEND order_partners.
  CLEAR:order_partners.

  "销售订单行项目
  LOOP AT p_lt_ekpo INTO ls_ekpo.
****ADD BY ZJ 20221204 S 免费行项目不显示
    IF ls_ekpo-uebpo <> '' AND ls_ekpo-uptyp = '4'.
      CONTINUE.
    ENDIF.
****ADD BY ZJ 20221204 E 免费行项目不显示
    order_items_in-itm_number  = ls_ekpo-ebelp."行项目
    order_items_inx-itm_number = ls_ekpo-ebelp.

    order_items_in-material    = ls_ekpo-matnr."物料
    order_items_inx-material   = 'X'.

    order_items_in-target_qty  = ls_ekpo-menge."数量
    order_items_inx-target_qty = 'X'.
****ADD BY ZJ 20221203 S 数量单位
    order_items_in-sales_unit = ls_ekpo-meins. "单位
    order_items_inx-sales_unit = 'X'.
****ADD BY ZJ 20221203 E 数量单位
    order_items_in-plant = p_ls_zsdt001-werks. "工厂
    order_items_inx-plant = 'X'.

    order_items_in-price_date = sy-datum. "定价日期
    order_items_inx-price_date = 'X'.

    order_items_in-po_itm_no = ls_ekpo-ebelp.
    order_items_inx-po_itm_no = 'X'.
****ADD BY ZJ 20221203 S 转换因子
    order_items_in-salqtynum = ls_ekpo-umrez.
    order_items_in-salqtyden = ls_ekpo-umren.

    order_items_inx-salqtynum = 'X'.
    order_items_inx-salqtyden = 'X'.
****ADD BY ZJ 20221203 E 转换因子

    order_items_in-po_method = ls_ekko-bsart.
    order_items_inx-po_method = 'X'.

    APPEND:order_items_in,order_items_inx.
    CLEAR:order_items_in,order_items_inx.

    order_schedules_in-itm_number  = ls_ekpo-ebelp.
    order_schedules_inx-itm_number = ls_ekpo-ebelp.
    order_schedules_in-req_qty     = ls_ekpo-menge.
    order_schedules_inx-req_qty    = 'X'.
    SELECT SINGLE eindt "行项目计划日期
      INTO order_schedules_in-req_date
      FROM eket
     WHERE ebeln = ls_ekpo-ebeln
       AND ebelp = ls_ekpo-ebelp
       .
    order_schedules_inx-req_date   = 'X'.
    APPEND:order_schedules_in,order_schedules_inx.
    CLEAR:order_schedules_in,order_schedules_inx.

    "重新定价条件
    IF p_ls_zsdt001-kschl IS NOT INITIAL.
      lv_num = lv_num + 1.
      CLEAR:ls_zsdt007.
      READ TABLE lt_zsdt007 INTO ls_zsdt007 INDEX lv_num.
      order_conditions_in-itm_number = ls_ekpo-ebelp.  "itm_number
      order_conditions_in-cond_type  = p_ls_zsdt001-kschl.            "定价条件
*      order_conditions_in-cond_value = ( ls_ekpo-brtwr / ls_ekpo-menge * ls_ekpo-peinh )." * p_ukurs * p_zscale . "定价值
*      order_conditions_in-currency   = order_header_in-currency.    "

****UPDATE JT 20221128 S 条件币别
*      order_conditions_in-currency = lv_waerk.
      SELECT SINGLE waers kmein kbetr
               INTO ( order_conditions_in-currency,
                      order_conditions_in-cond_unit,
                      order_conditions_in-cond_value )
               FROM konv
              WHERE knumv = ls_ekko-knumv
                AND kposn = ls_ekpo-ebelp
                AND kinak = ''
                AND ( kschl = 'PB00' OR kschl = 'PBXX' ) .
****UPDATE JT 20221128 E 条件币别
*====================================================================*
      "*== add Tuq 16.11.2022 10:05:41
*      IF order_header_in-currency <> ls_ekko-waers.     "汇率转换
*      IF order_conditions_in-currency <> ls_ekko-waers.     "汇率转换
*        CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
*          EXPORTING
*            date             = sy-datum
*            foreign_amount   = order_conditions_in-cond_value
*            foreign_currency = ls_ekko-waers
**           local_currency   = order_header_in-currency
*            local_currency   = order_conditions_in-currency "*==行项目条件货币
*            type_of_rate     = 'M'
*          IMPORTING
*            local_amount     = order_conditions_in-cond_value
*          EXCEPTIONS
*            no_rate_found    = 1
*            OTHERS           = 2.
*      ENDIF.
      "*== end Tuq 16.11.2022 10:05:41
      order_conditions_in-cond_p_unt  = ls_ekpo-peinh..  "条件单位
      APPEND order_conditions_in.
      CLEAR order_conditions_in.

      order_conditions_inx-itm_number = ls_ekpo-ebelp.
      order_conditions_inx-cond_type  = p_ls_zsdt001-kschl.
      order_conditions_inx-cond_value = 'X'.
      order_conditions_inx-currency   = 'X'.
      order_conditions_inx-cond_unit  = 'X'.
      order_conditions_inx-cond_p_unt = 'X'.
      APPEND order_conditions_inx.
      CLEAR  order_conditions_inx.
    ENDIF.
    CLEAR:ls_ekpo.
  ENDLOOP.

  logic_switch-pricing = 'G'.
  APPEND logic_switch.
  CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'
    EXPORTING
      sales_header_in      = order_header_in
      sales_header_inx     = order_header_inx
      logic_switch         = logic_switch
    IMPORTING
      salesdocument_ex     = salesdocument
    TABLES
      return               = return[]
      sales_items_in       = order_items_in[]
      sales_items_inx      = order_items_inx[]
      sales_partners       = order_partners[]
      sales_schedules_in   = order_schedules_in[]
      sales_schedules_inx  = order_schedules_inx[]
      sales_conditions_in  = order_conditions_in[]
      sales_conditions_inx = order_conditions_inx[]
      extensionin          = lt_extensionin.

  LOOP AT return WHERE type = 'E' OR type = 'A'.
    CONCATENATE p_e_msg return-message INTO p_e_msg SEPARATED BY '/'.
  ENDLOOP.
  IF sy-subrc <> '0'.
    CALL METHOD zcl_rfc=>get_source
      EXPORTING
        i_id       = p_i_id
        i_canum    = p_i_canum
      IMPORTING
        e_zrfct002 = ls_zrfct002.
*      RECEIVING
*        R_SOURCE   = LS_SOURCE.

    CLEAR:ls_zsdt008.
    ls_zsdt008-canum      = ls_zrfct002-canum.
    ls_zsdt008-objtype    = ls_zrfct002-objtype_save.
    ls_zsdt008-objkey     = salesdocument.
    ls_zsdt008-zrfcid     = ls_zrfct002-zrfcid.
    ls_zsdt008-zrfc_logid = p_i_id.
    APPEND ls_zsdt008 TO lt_zsdt008.

    MODIFY zsdt008 FROM TABLE lt_zsdt008.

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
    p_e_code = 'S'.
    CONCATENATE 'SO:' salesdocument '创建成功' INTO p_e_msg.
    DATA:ls_zrfct005 TYPE zrfct005.
    CLEAR:ls_zrfct005.
    ls_zrfct005-status = '53'.
    ls_zrfct005-msgty = p_e_code.
    ls_zrfct005-msgid = '00'.
    ls_zrfct005-msgno = '001'.
    ls_zrfct005-msgv1 = p_e_msg.
    ls_zrfct005-msgv4 = salesdocument.
    APPEND ls_zrfct005 TO p_et_return.
*    P_ET_RETURN[] = VALUE #( (
*                            STATUS = '53'  "通过状态53来保存KEY数据
*                            MSGTY = P_E_CODE
*                            MSGID = '00'
*                            MSGNO = '001'
*                            MSGV1 = P_E_MSG
*                            MSGV4 = SALESDOCUMENT ) ).
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    p_e_code = 'E'.
  ENDIF.


  CLEAR:order_header_in,
        order_header_inx,
        order_partners[],
        order_partners,
        order_items_in[],
        order_items_in,
        order_items_inx[],
        order_items_inx,
        order_schedules_in[],
        order_schedules_in,
        order_schedules_inx[],
        order_schedules_inx,
        return[],
        return,
        salesdocument,
        lt_zsdt008,
        ls_zsdt008,
        logic_switch[].

ENDFORM.    

 

标签:BAPI,inx,header,zsdt001,lv,订单,ls,APAP,order
From: https://www.cnblogs.com/ap-ad-java-abap/p/16987791.html

相关文章