APAP:销售订单创建BAPI

销售订单创建BAPI: SD_SALESDOCUMENT_CREATE

创建销售订单时,没有字段存放成本中心:

 因此采用扩展字段更新成本中心:首先给四个结构中添加成本中心字段:BAPE_VBAK,BAPE_VBAKX,VBAKKOZ,VBAKKOZX

 下面代码中有使用范例,修改时同理

FUNCTION zsdfu012.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(IV_VBELN) TYPE  VBELN OPTIONAL
*"  EXPORTING
*"     VALUE(EV_TYPE) TYPE  BAPI_MTYPE
*"     VALUE(EV_MESSAGE) TYPE  BAPI_MSG
*"     VALUE(VBELN) TYPE  VBELN_VA
*"     VALUE(KBETR) TYPE  KBETR
*"     VALUE(KONWA) TYPE  KONWA
*"  TABLES
*"      IT_INFO STRUCTURE  ZSDS008 OPTIONAL
*"----------------------------------------------------------------------

  TYPES: BEGIN OF ty_data,
           bname     TYPE vbak-bname,      "旧销售订单号
           auart     TYPE vbak-auart,      "订单类型
           vkorg     TYPE vbak-vkorg,      "销售组织
           vtweg     TYPE vbak-vtweg,      "分销渠道
           spart     TYPE vbak-spart,      "产品组
           vkbur     TYPE vbak-vkbur,      "销售部门
           vkgrp     TYPE vbak-vkgrp,      "销售组
           kunnr     TYPE vbak-kunnr,      "售达方
           bstkd     TYPE vbkd-bstkd,      "客户PO
           bstdk     TYPE vbkd-bstdk,      "客户参考日期
           vdatu     TYPE vbak-vdatu,      "客户要求交期(抬头)
           augru     TYPE vbak-augru,      "订单原因
           kostl     TYPE vbak-kostl,      "成本中心
           zhtxt     TYPE char200,         "抬头文本
           matnr     TYPE vbap-matnr,      "物料编码
           arktx     TYPE vbap-arktx,      "物料描述
           kwmeng    TYPE vbap-kwmeng,     "订单数量
           vrkme     TYPE vbap-vrkme,      "销售单位
           kdmat     TYPE vbap-kdmat,      "客户物料号
           vdatu_ana TYPE vbap-vdatu_ana,  "客户交货日期(项目)
           werks     TYPE vbap-werks,      "交货工厂
           lgort     TYPE vbap-lgort,      "库存地点
           prctr     TYPE vbap-prctr,      "利润中心
           sgtxt     TYPE char200,         "行项目文本
           kbetr     TYPE konp-kbetr,      "金额
           condtype  TYPE zsds008-condtype, "类型
           currency  TYPE bapicond-currency, "货币

           vbeln     TYPE vbak-vbeln,      "销售订单

           zindex    TYPE int4, "序号
           type      TYPE char4,           " 返回类型
           msg       TYPE char200,          "消息
         END OF ty_data.

  DATA: gs_data TYPE ty_data,
        gt_data TYPE TABLE OF ty_data.
  DATA:ls_head  TYPE bapisdh1,
       ls_headx TYPE bapisdh1x.

  DATA: gs_table TYPE zsds008.

  DATA: gs_r TYPE zsdu012_r,
        gt_r TYPE TABLE OF zsdu012_r.

  DATA: ls_header               TYPE bapisdhd1,
        ls_logic                TYPE bapisdls,
        lv_vbeln                TYPE vbak-vbeln,
        lt_return               TYPE TABLE OF bapiret2,
        ls_items                TYPE bapisditm,
        lt_items                TYPE TABLE OF bapisditm,
        ls_partners             TYPE bapiparnr,
        lt_partners             TYPE TABLE OF bapiparnr,
        ls_schedules            TYPE bapischdl,
        lt_schedules            TYPE TABLE OF bapischdl,
        ls_conditions           TYPE bapicond,
        lt_conditions           TYPE TABLE OF bapicond,
        ls_sales_conditions_inx TYPE bapicondx,
        lt_sales_conditions_inx LIKE TABLE OF bapicondx,
        ls_text                 TYPE bapisdtext,
        lt_text                 TYPE TABLE OF bapisdtext,
        ls_extensionin          TYPE bapiparex,
        lt_extensionin          TYPE TABLE OF bapiparex,
        lv_index                TYPE i.
*****ADD BY ZJ 20230424 S更新成本中心
  DATA: ls_bape_vbak  TYPE bape_vbak,
        ls_bape_vbakx TYPE bape_vbakx.
*****ADD BY ZJ 20230424 E更新成本中心

  TYPES: BEGIN OF ty_sgtxt,
           posnr TYPE vbap-posnr,
           sgtxt TYPE char200,
         END OF ty_sgtxt.

  DATA: es_return TYPE zsreturn,
        ls_sgtxt  TYPE ty_sgtxt,
        lt_sgtxt  TYPE TABLE OF ty_sgtxt.

  DATA:
    lv_subrc TYPE sy-subrc,
    lv_msg   TYPE bapi_msg.


  DATA:
    gt_kbetr TYPE konp-kbetr,
    gt_konwa TYPE konp-konwa.

  FIELD-SYMBOLS: <fs_data> TYPE ty_data.



*-----接口日志处理
  zmac-rfc-log-b.
  DATA:lv_type     TYPE bapi_mtype,
       lv_message1 TYPE msgtxt.

  IF iv_vbeln IS INITIAL.
    LOOP AT it_info INTO DATA(ls_info).
      MOVE-CORRESPONDING ls_info TO gs_data.
      APPEND gs_data TO gt_data.
    ENDLOOP.




    LOOP AT gt_data ASSIGNING <fs_data>.
      DATA:lv_kschl TYPE a999-kschl,
           lv_kbetr TYPE konp-kbetr,
           lv_konwa TYPE konp-konwa.
      ADD 10 TO lv_index.

      ls_sgtxt-posnr = lv_index.
      ls_sgtxt-sgtxt = <fs_data>-sgtxt.
      APPEND ls_sgtxt TO lt_sgtxt.
      CLEAR ls_sgtxt.

      ls_items-itm_number   = lv_index.
      ls_items-material     = <fs_data>-matnr.     "物料编号
      ls_items-short_text   = <fs_data>-arktx.     "销售短文本
      ls_items-target_qty   = <fs_data>-kwmeng.    "数量
      ls_items-target_qu    = <fs_data>-vrkme.     "单位
      ls_items-cust_mat35   = <fs_data>-kdmat.     "客户CPN
      ls_items-purch_date   = <fs_data>-vdatu_ana. "要求交货日期
      ls_items-plant        = <fs_data>-werks.     "交货工厂
      ls_items-store_loc    = <fs_data>-lgort.     "库存地点
      ls_items-profit_ctr   = <fs_data>-prctr.     "利润中心
      APPEND ls_items TO lt_items.
      CLEAR ls_items.

      CLEAR: ls_header,       ls_logic,         lv_vbeln.

      ls_schedules-itm_number = lv_index.
      ls_schedules-req_qty    = <fs_data>-kwmeng.    "数量
      ls_schedules-req_date   = <fs_data>-vdatu_ana. "日期
      APPEND ls_schedules TO lt_schedules.
      CLEAR ls_schedules.

      SELECT SINGLE a~kschl,b~kbetr,b~konwa INTO ( @lv_kschl,@lv_KBETR,@lv_KONWA )
        FROM a999 AS a
        LEFT JOIN konp AS b ON a~knumh = b~knumh
        WHERE a~matnr = @<fs_data>-matnr
        AND a~datbi >= @sy-datum AND a~datab <= @sy-datum
        AND a~kunnr = @<fs_data>-kunnr
        AND a~kschl = @<fs_data>-condtype
        AND a~vkorg = @<fs_data>-vkorg.


      "合计金额、货币,返回给OA
      gt_kbetr = gt_kbetr + lv_kbetr.
      gt_konwa = lv_konwa.

      ls_conditions-itm_number  = lv_index.
      ls_conditions-cond_type   = lv_kschl.  "条件类型
      ls_conditions-cond_value  = lv_kbetr.    "单价
      ls_conditions-currency    = lv_konwa.  "货币


      APPEND ls_conditions TO lt_conditions.
      CLEAR ls_conditions.

      ls_sales_conditions_inx-itm_number = lv_index.

      ls_sales_conditions_inx-currency   = abap_true.
      ls_sales_conditions_inx-cond_value = abap_true.
      APPEND ls_sales_conditions_inx TO lt_sales_conditions_inx.
      CLEAR ls_sales_conditions_inx.

      CLEAR: lv_kschl,lv_kbetr,lv_konwa.

    ENDLOOP.
*    AT END OF zindex.
    READ TABLE it_info INTO gs_table INDEX 1.
    ls_header-name        = gs_table-bname.     "旧销售订单号
    ls_header-doc_type    = gs_table-auart.     "订单类型
    ls_header-sales_org   = gs_table-vkorg.     "销售组织
    ls_header-distr_chan  = gs_table-vtweg.     "分销渠道
    ls_header-division    = gs_table-spart.     "产品组
    ls_header-sales_off   = gs_table-vkbur.     "销售部门
    ls_header-sales_grp   = gs_table-vkgrp.     "销售组
    ls_header-purch_no_c  = gs_table-bstkd.     "客户参考
    ls_header-purch_date  = gs_table-bstdk.     "客户参考日期
    ls_header-ord_reason  = gs_table-augru.     "订单原因
    ls_header-req_date_h  = gs_table-vdatu.     "要求的交货日期

*****ADD BY ZJ 20230424 S更新成本中心
    IF gs_table-kostl IS NOT INITIAL.
      ls_bape_vbak-kostl = gs_table-kostl.
      ls_bape_vbakx-kostl = 'X'.
      ls_extensionin-structure = 'BAPE_VBAK'.
      ls_extensionin+30 = ls_bape_vbak.
      APPEND ls_extensionin TO lt_extensionin.
      CLEAR:ls_extensionin.

      ls_extensionin-structure = 'BAPE_VBAKX'.
      ls_extensionin+30 = ls_bape_vbakx.
      APPEND ls_extensionin TO lt_extensionin.
      CLEAR:ls_extensionin.
    ENDIF.

*****ADD BY ZJ 20230424 E更新成本中心

    ls_partners-partn_role  = 'AG'.
    ls_partners-partn_numb  = <fs_data>-kunnr.   "售达方
    APPEND ls_partners TO lt_partners.
    CLEAR: ls_partners,gs_table.

    BREAK jt-wuw.

    ls_logic-pricing = 'G'.   "复制不变定价因素且重定税款
    "创建订单
    CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'
      EXPORTING
        sales_header_in       = ls_header
        logic_switch          = ls_logic
        int_number_assignment = abap_true
      IMPORTING
        salesdocument_ex      = lv_vbeln
      TABLES
        return                = lt_return
        sales_items_in        = lt_items
        sales_partners        = lt_partners
        sales_schedules_in    = lt_schedules
        sales_conditions_in   = lt_conditions
        sales_conditions_inx  = lt_sales_conditions_inx
        sales_text            = lt_text
        extensionin           = lt_extensionin.

    IF lv_vbeln IS NOT INITIAL.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = abap_true.
      <fs_data>-vbeln = lv_vbeln.
      ev_type = 'S'.
      ev_message = '销售订单' && lv_vbeln && '创建成功'.
      vbeln = lv_vbeln.
      kbetr = gt_kbetr.
      konwa = gt_konwa.

      PERFORM frm_save_text(zpub_f01) IF FOUND USING '0001' lv_vbeln 'VBBK' abap_true <fs_data>-zhtxt.

      DO 20 TIMES.
        SELECT
          vbeln,
          posnr
        INTO TABLE @DATA(lt_vbap)
        FROM vbap
        WHERE vbeln = @lv_vbeln
          AND uepos = '000000'.
        IF sy-subrc = 0.
          EXIT.
        ELSE.
          WAIT UP TO '0.1' SECONDS.
        ENDIF.
      ENDDO.

      LOOP AT lt_vbap INTO DATA(ls_vbap).
        READ TABLE lt_sgtxt INTO ls_sgtxt INDEX sy-tabix.
        DATA(lv_name) = ls_vbap-vbeln && ls_vbap-posnr.

        PERFORM frm_save_text(zpub_f01) IF FOUND USING '0001' lv_name 'VBBP' abap_true ls_sgtxt-sgtxt.
      ENDLOOP.

    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

      PERFORM frm_get_bapiret(zpub_f01) TABLES lt_return USING '' '' CHANGING es_return.

      ev_type = 'E'.
      ev_message = es_return-msg.

    ENDIF.
  ELSE.
*****ADD BY ZJ 20230423 S 更新成本中心
    CLEAR:lt_return.

    READ TABLE it_info INTO gs_table INDEX 1.

    ls_headx-updateflag = 'U'.

    ls_bape_vbak-vbeln = iv_vbeln.
    ls_bape_vbak-kostl = gs_table-kostl.

    ls_bape_vbakx-vbeln = iv_vbeln.
    ls_bape_vbakx-kostl = 'X'.
    ls_extensionin-structure = 'BAPE_VBAK'.
    ls_extensionin+30 = ls_bape_vbak.
    APPEND ls_extensionin TO lt_extensionin.
    CLEAR:ls_extensionin.

    ls_extensionin-structure = 'BAPE_VBAKX'.
    ls_extensionin+30 = ls_bape_vbakx.
    APPEND ls_extensionin TO lt_extensionin.
    CLEAR:ls_extensionin.
    CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
      EXPORTING
        salesdocument    = iv_vbeln
        order_header_inx = ls_headx
      TABLES
        return           = lt_return
        extensionin      = lt_extensionin.
    LOOP AT lt_return INTO DATA(ls_return) WHERE type = 'E' OR type = 'A' OR type = 'X'.
      CONCATENATE ev_message ls_return-message INTO ev_message SEPARATED BY '/'.
    ENDLOOP.
    IF sy-subrc NE 0.
      ev_type = 'S'.
      ev_message = '销售订单:' && iv_vbeln && '成本中心修改成功'.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      ev_type = 'E'.
      ev_message = '销售订单:' && iv_vbeln && '成本中心修改失败,' && ev_message.
    ENDIF.
*****ADD BY ZJ 20230423 E 更新成本中心
  ENDIF.


  CLEAR:
     lt_return,       lt_items,         lt_partners,
     lt_schedules,    lt_conditions,    lt_text,
     lt_extensionin,  lt_sgtxt,         lv_index.



*    ENDAT.
*  ENDLOOP.

  lv_type = ev_type.
  lv_message1 = ev_message.

  zmac-rfc-log-e lv_type lv_message1 '' '' ''.
ENDFUNCTION.

 需要注意:借贷项销售订单需要给LOGIC_SWITCH-SCHEDULING打'X'标记

 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.    

 

posted @ 2022-12-16 16:54  阿胖的阿多  阅读(1280)  评论(0编辑  收藏  举报