SAP SD BAPI_SALESORDER_CREATEFROMDAT2 创建一次性客户的销售订单。

SAP SD BAPI_SALESORDER_CREATEFROMDAT2 创建一次性客户的销售订单。_第1张图片

对应的BAPI_SALESORDER_CREATEFROMDAT2 BAPI

SAP SD BAPI_SALESORDER_CREATEFROMDAT2 创建一次性客户的销售订单。_第2张图片

SAPnote:

*  note  2367755
*  FIELDS PARTNERADDRESSES-ADDR_NO AND ORDER_PARTNERS-ADDR_LINK are CHAR FIELDS, therefore, they should be filled WITH LEADING zeroes.
*   PARTNERADDRESSES-ADDR_NO 和 ORDER_PARTNERS-ADDR_LINK 是字符串,因此,需要前导0 填充
*  ORDER_PARTNERRS-ADDR_LINK should contain a temporary NUMBER, FOR example 0000000001 AND NOT a real NUMBER OF an existing address.
*   ORDER_PARTNERRS-ADDR_LINK 里面填充  零时数据,注意不要跟原有的重复
*  Enter the address DATA only IN PARTNERADDRESSES.
*   地址必须只在 PARTNERADDRESSES.
*  IN ORDER_PARTNERS enter only PARTN_ROLE , PARTN_NUMB AND ADDR_LINK (the link TO the address DATA IN TABLE PARTNERADDRESSES).
*   ORDER_PARTNERS 仅输入 合作伙伴角色 合作伙伴编号 和 链接  链接到 PARTNERADDRESSES里面的号码

上代码

FUNCTION ZIF_SALESORDER_CREATE_4Y0.
*"----------------------------------------------------------------------
*"*"局部接口:
*"  IMPORTING
*"     VALUE(VKORG) TYPE  VKORG DEFAULT 1000
*"     VALUE(VTWEG) TYPE  VTWEG DEFAULT 20
*"     VALUE(DOC_TYPE) TYPE  AUART DEFAULT 'ZTA'
*"     VALUE(BSTNK) TYPE  BSTNK DEFAULT '客户采购合同'
*"     VALUE(VDATU) TYPE  EDATU_VBAK DEFAULT 20230222
*"     VALUE(PRSDT) TYPE  PRSDT DEFAULT 20230222
*"     VALUE(WAERK) TYPE  WAERK DEFAULT 'RMB'
*"     VALUE(ERNAM) TYPE  ERNAM DEFAULT '8256'
*"     VALUE(KUNNR) TYPE  KUNNR DEFAULT 'Y0'
*"     VALUE(USELGORT) TYPE  CHAR1 DEFAULT 'X'
*"     VALUE(SYBKNR) TYPE  KUNNR DEFAULT '0000100002'
*"     VALUE(Y0ADDRESS) TYPE  ZY0ADDRESSINFO OPTIONAL
*"  EXPORTING
*"     VALUE(L_VBELN) TYPE  VBAK-VBELN
*"     VALUE(LMESSAGE) TYPE  BAPI_MSG
*"     VALUE(LSTATUS) TYPE  BAPI_MTYPE
*"  TABLES
*"      L_ITEMS STRUCTURE  ZSOITEMS OPTIONAL
*"      CONDITIONS STRUCTURE  ZSOCONDITION OPTIONAL
*"----------------------------------------------------------------------

  DATA:
        header_in            TYPE bapisdhd1,
        header_inx           TYPE bapisdhd1x,
        pardess TYPE TABLE OF BAPIADDR1 WITH HEADER LINE,
        ext_vbep             TYPE bape_vbep,
        ext_vbepx            TYPE bape_vbepx,
        header_text          TYPE TABLE OF bapisdtext WITH HEADER LINE,
        l_message            TYPE string,
        lt_return            TYPE TABLE OF bapiret2   WITH HEADER LINE,
        items_in             TYPE TABLE OF bapisditm  WITH HEADER LINE,
        items_inx            TYPE TABLE OF bapisditmx WITH HEADER LINE,
        partners             TYPE TABLE OF bapiparnr  WITH HEADER LINE,
        schedules_in         TYPE TABLE OF bapischdl  WITH HEADER LINE,
        order_conditions_in  TYPE bapicond   OCCURS 0 WITH HEADER LINE,
        order_conditions_inx TYPE bapicondx  OCCURS 0 WITH HEADER LINE,
        logic_switch         TYPE bapisdls   OCCURS 0 WITH HEADER LINE,
        lt_extensionin       TYPE TABLE OF bapiparex WITH HEADER LINE,
        L_SWITCH    TYPE BAPISDLS,
        vkbur         TYPE VKBUR,"销售部门
        wa_return     TYPE bapiret2,
        cp_eind       TYPE MATNR,
        wa_text       TYPE bapisdtext,  "文本
        lv_timestamp  TYPE timestamp,"时间

        TEMPTEXT TYPE TABLE OF ZWSTEXT WITH HEADER LINE,
        wa_vbsn TYPE TABLE OF VBSN WITH HEADER LINE,
        lmatnrNOZero TYPE mara-matnr,
        item TYPE POSNR.
  DATA:lT00lL LIKE T001l OCCURS 0 WITH HEADER LINE.
  CLEAR: header_in,
  header_inx,
  header_text,
  order_conditions_in,
  order_conditions_inx,
  l_vbeln,
  lt_extensionin,
  items_in[],items_in,
  items_inx[],items_inx,
  partners[],partners,
  schedules_in[],schedules_in.

  DATA:LASTYEARDATE TYPE sy-datum.

  "计算去年今日的日期
  CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
    EXPORTING
      DATE      = sy-datum
      DAYS      = 0
      MONTHS    = 0
      SIGNUM    = '-'
      YEARS     = 1
    IMPORTING
      CALC_DATE = LASTYEARDATE.

  DATA:lselops LIKE BAPI_ITOB_SEL_DESCRIPT OCCURS 0 WITH HEADER LINE.
  lselops-SIGN = 'I'.
  lselops-OPTION = 'BT'.
  lselops-LOW = 'Z011'.
  lselops-HIGH = 'Z053'.
  APPEND lselops.

  DATA:L_UUID32 TYPE SYSUUID_C32,
        ILOGS LIKE ZRFCLOGS OCCURS 0 WITH HEADER LINE,
        TXT(250) TYPE C,
        DTXT1(20) TYPE C,
        DTXT2(20) TYPE C.
*  ===============日志部分==============================
  TRY.
      CALL METHOD CL_SYSTEM_UUID=>IF_SYSTEM_UUID_STATIC~CREATE_UUID_C32
        RECEIVING
          UUID = L_UUID32.
    CATCH CX_UUID_ERROR .
  ENDTRY.

  ILOGS-TIMES = L_UUID32.
  ILOGS-RFCUSER = SY-UNAME.
  ILOGS-SAPRFCFUN = 'ZIF_SALESORDER_CREATE_4Y0'.
  CONCATENATE 'HEAD:' VKORG '@' ERNAM '@' KUNNR '@' BSTNK '@'  VDATU '@' PRSDT '@' VTWEG '@' WAERK '@' DOC_TYPE   INTO ILOGS-CALPARMI.

* *======================================================
* header

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      INPUT  = kunnr
    IMPORTING
      OUTPUT = kunnr.

  header_in-doc_type  = DOC_TYPE."'ZTA'."销售凭证类型
  L_SWITCH-PRICING = ''. "G B
  L_SWITCH-COND_HANDL = ''. "X
  header_in-sales_org   = VKORG.
  header_in-distr_chan  = VTWEG."分销渠道
  header_in-division    = '00'."部门
  IF ERNAM IS NOT INITIAL.
    HEADER_IN-CREATED_BY = ERNAM.
  ELSE.
    HEADER_IN-CREATED_BY = SY-UNAME.
  ENDIF.
  header_in-PURCH_NO_C = BSTNK."采购订单编号
  header_in-PURCH_DATE = SY-DATUM."采购订单日期
  header_in-REQ_DATE_H = VDATU."请求交货日期
  header_in-PRICE_DATE  = PRSDT.
  header_in-CURRENCY = WAERK.
* header_text

  header_text-itm_number          = space.
  header_text-text_id             = '0001'.
  header_text-langu               = SY-Langu.
  header_text-format_col          = '*'.
  header_text-text_line           = BSTNK.
  APPEND header_text.

* item
  CONCATENATE '@@BPARAMETER:' TXT INTO  TXT.
  LOOP AT L_ITEMS.
    CLEAR lmatnrNOZero.
    CLEAR: items_in,items_inx,schedules_in,lt_extensionin.
    item = L_ITEMS-POSNR.
    lmatnrNOZero = L_ITEMS-MATNR.
    SHIFT lmatnrNOZero LEFT DELETING LEADING '0'.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        INPUT  = L_ITEMS-MATNR
      IMPORTING
        OUTPUT = L_ITEMS-MATNR.

    items_in-itm_number = item."销售凭证行项目
    items_in-material   = L_ITEMS-MATNR."物料编号
    items_inx-PRICE_DATE = 'X'.
    ITEMS_IN-STORE_LOC = space.
    IF L_ITEMS-MAKTX IS NOT INITIAL.
      items_in-short_text = L_ITEMS-MAKTX.
    ENDIF.
    IF L_ITEMS-WERKS IS NOT INITIAL.
      items_in-plant      = L_ITEMS-WERKS.
    ELSE.
      items_in-plant      = VKORG."工厂
    ENDIF.

    IF L_ITEMS-LGORT IS NOT INITIAL AND USELGORT = 'X'.
      ITEMS_IN-STORE_LOC = L_ITEMS-LGORT.
      IF USELGORT NE 'X'."使用默认库位
        CLEAR ITEMS_IN-STORE_LOC .

      ENDIF.
    ENDIF.

    IF L_ITEMS-DEPICT IS NOT INITIAL.
      items_in-CUST_MAT35 = L_ITEMS-DEPICT.
    ENDIF.
    IF ERNAM IS NOT INITIAL.
      items_in-CREATED_BY = ERNAM.
    ELSE.
      items_in-CREATED_BY = sy-UNAME.
    ENDIF.
    items_in-LOG_SYSTEM_OWN = 'PRD800'."逻辑系统

    CONCATENATE TXT '@' item '@' lmatnrNOZero '@' items_in-plant  INTO TXT.

    APPEND items_in.

    items_inx-UPDATEFLAG   = 'I'."更新标志
    APPEND items_inx.

*    SCHEDULES_IN-REQ_DATE = BPARAMETER-REQ_DATE.

    schedules_in-itm_number   = item."销售凭证项目
    schedules_in-req_qty      = L_ITEMS-NUMBER."以销售单位计的订单数量
    schedules_in-SCHED_TYPE   = 'CN'."计划行类别默认CN
    IF L_ITEMS-REQ_DATE IS NOT INITIAL.
      SCHEDULES_IN-REQ_DATE = L_ITEMS-REQ_DATE.
    ELSE.
      SCHEDULES_IN-REQ_DATE = VDATU.
    ENDIF.
    APPEND schedules_in.

    "extensionin
    IF L_ITEMS-CODING IS NOT INITIAL.
      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
        EXPORTING
          INPUT  = L_ITEMS-MATNR
        IMPORTING
          OUTPUT = L_ITEMS-MATNR.
      CLEAR: ext_vbep,ext_vbepx.
      ext_vbep-posnr = item.
      CONCATENATE L_ITEMS-MATNR L_ITEMS-CODING INTO ext_vbep-aeskd SEPARATED BY '-'.
      lt_extensionin-STRUCTURE = 'BAPE_VBEP'.
      CALL METHOD cl_abap_container_utilities=>fill_container_c
        EXPORTING
          im_value     = ext_vbep
        IMPORTING
          ex_container = lt_extensionin-valuepart1.
      APPEND lt_extensionin.

      ext_vbepx-posnr = item.
      ext_vbepx-aeskd = 'X'.
      lt_extensionin-STRUCTURE = 'BAPE_VBEPX'.
      lt_extensionin-VALUEPART1 = ext_vbepx.
      APPEND lt_extensionin.
    ENDIF.

  ENDLOOP.
  CONCATENATE TXT '@@CONDITIONS:' INTO TXT.
* conditions
  LOOP AT CONDITIONS.

    order_conditions_in-itm_number = CONDITIONS-POSNR."条件项目号
    IF CONDITIONS-KSCHA IN lselops.
      order_conditions_in-cond_type = CONDITIONS-KSCHA."条件类型
      order_conditions_in-cond_value = CONDITIONS-KBETR."调整单体价格
      order_conditions_in-CURRENCY = CONDITIONS-KOEIN."货币码
    ENDIF.
    DTXT1 =  order_conditions_in-cond_value.
    CONCATENATE TXT '@' order_conditions_in-cond_type '@' DTXT1 '@' order_conditions_in-CURRENCY  INTO TXT.
    APPEND order_conditions_in.
    CLEAR order_conditions_in.

    order_conditions_inx-UPDATEFLAG = 'I'.
    APPEND order_conditions_inx.

  ENDLOOP.
  CONCATENATE ILOGS-CALPARMI TXT INTO ILOGS-CALPARMI.
  ILOGS-DATUMI  = SY-DATUM.
  ILOGS-UZEITI = SY-UZEIT.
  APPEND ILOGS.
  CALL FUNCTION 'ZSYS_RFC_LOG' STARTING NEW TASK 'RFCLOGS'
    TABLES
      LOGS = ILOGS.
* partener
  partners-partn_role = 'AG'."售达方
  partners-partn_numb = kunnr."客户编号
  partners-ADDR_LINK = '0000000001'.
  partners-ADDR_TYPE = '2'.
  APPEND partners.

  partners-partn_role = 'WE'."送达方
  partners-partn_numb = kunnr."客户编号
  partners-ADDR_LINK = '0000000001'.
  partners-ADDR_TYPE = '2'.
  APPEND partners.

  clear partners.
  partners-partn_role = 'Z3'."送达方
  partners-partn_numb = SYBKNR."客户编号
  APPEND partners.


*  note  2367755
*  FIELDS PARTNERADDRESSES-ADDR_NO AND ORDER_PARTNERS-ADDR_LINK are CHAR FIELDS, therefore, they should be filled WITH LEADING zeroes.
*   PARTNERADDRESSES-ADDR_NO 和 ORDER_PARTNERS-ADDR_LINK 是字符串,因此,需要前导0 填充
*  ORDER_PARTNERRS-ADDR_LINK should contain a temporary NUMBER, FOR example 0000000001 AND NOT a real NUMBER OF an existing address.
*   ORDER_PARTNERRS-ADDR_LINK 里面填充  零时数据,注意不要跟原有的重复
*  Enter the address DATA only IN PARTNERADDRESSES.
*   地址必须只在 PARTNERADDRESSES.
*  IN ORDER_PARTNERS enter only PARTN_ROLE , PARTN_NUMB AND ADDR_LINK (the link TO the address DATA IN TABLE PARTNERADDRESSES).
*   ORDER_PARTNERS 仅输入 合作伙伴角色 合作伙伴编号 和 链接  链接到 PARTNERADDRESSES里面的号码
 MOVE-CORRESPONDING Y0ADDRESS TO  PARDESS.
  PARDESS-NAME_2 = Y0address-NAME2.
  PARDESS-NAME_3 = Y0address-NAME3.
  PARDESS-NAME_4 = Y0address-NAME4.
  PARDESS-ADDR_NO = '0000000001'.
  APPEND  PARDESS.

  CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
    EXPORTING
      SALESDOCUMENTIN         = l_vbeln
      order_header_in         = header_in
*     order_header_inx        = header_inx
*     SENDER                  =
*     BINARY_RELATIONSHIPTYPE =
*     INT_NUMBER_ASSIGNMENT   =
*     BEHAVE_WHEN_ERROR       =
      logic_switch            = L_SWITCH
    IMPORTING
      salesdocument           = l_vbeln
    TABLES
      RETURN                  = lt_return
      order_items_in          = items_in
      order_items_inx         = items_inx
      order_partners          = partners
      order_schedules_in      = schedules_in
      order_conditions_in     = order_conditions_in
      order_conditions_inx    = order_conditions_inx
      order_text              = header_text
      PARTNERADDRESSES = pardess
                  "ZWSTEXT                   = TEMPTEXT.
      extensionin             = lt_extensionin.
* 处理错误消息:通过判断消息的类型,来判断BAPI是否成功
  LOOP AT lt_return WHERE TYPE = 'E' OR TYPE = 'A'. "E——错误 W——警告 I——信息 A——异常终止 S——成功
    "IF lt_return-type = 'E' OR lt_return-type = 'A' OR lt_return-type = 'W'.
    LMESSAGE = lt_return-MESSAGE.
    LSTATUS = lt_return-TYPE.

    EXIT.
    "ENDIF.
  ENDLOOP.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
    EXPORTING
      INPUT  = l_vbeln
    IMPORTING
      OUTPUT = l_vbeln.

  IF lt_return-TYPE = 'S'  OR lt_return-TYPE = 'W'.
    "WRITE:'订单创建成功'.
    LMESSAGE = '订单创建成功'.
    LSTATUS = 'S'.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
    LOOP AT L_ITEMS.
      item = sy-tabix.
      GET TIME STAMP FIELD lv_timestamp.
      cp_eind = lv_timestamp.
      CONCATENATE cp_eind item INTO TEMPTEXT-NUM.
      "TEMPTEXT-NUM = lv_timestamp+item.
      TEMPTEXT-BSTNK = l_vbeln.
      TEMPTEXT-CODING = L_ITEMS-CODING.
      TEMPTEXT-DEPICT = L_ITEMS-DEPICT.
      TEMPTEXT-ITM_NUM = L_ITEMS-POSNR.
      APPEND TEMPTEXT.

      IF L_ITEMS-MATNR IS NOT INITIAL AND L_ITEMS-CODING IS NOT INITIAL.
        CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
          EXPORTING
            INPUT  = l_vbeln
          IMPORTING
            OUTPUT = l_vbeln.
        CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
          EXPORTING
            INPUT  = L_ITEMS-MATNR
          IMPORTING
            OUTPUT = L_ITEMS-MATNR.
        wa_vbsn-MANDT = '800'.
        wa_vbsn-VBELN = l_vbeln.
        wa_vbsn-POSNR = L_ITEMS-POSNR.
        wa_vbsn-AENNR = '0001'.
        wa_vbsn-AKTKZ = 'X'.
        wa_vbsn-LIFFZ = '0.000'.
        wa_vbsn-LEDAT = '19000101'."sy-datum
        CONCATENATE L_ITEMS-MATNR L_ITEMS-CODING INTO wa_vbsn-AESKD SEPARATED BY '-'.
        APPEND wa_vbsn.
      ENDIF.
    ENDLOOP.

    MODIFY ZWSTEXT FROM TABLE TEMPTEXT.
    MODIFY VBSN FROM TABLE wa_vbsn.

  ELSE.
    "WRITE:'输入信息有误'.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ENDIF.

*  * 完成后日志更新
  CLEAR ILOGS[].
  ILOGS-TIMES = L_UUID32.
  CONCATENATE LSTATUS  ':'  LMESSAGE INTO ILOGS-CALPARMO.
  ILOGS-DATUMO  = SY-DATUM.
  ILOGS-UZEITO = SY-UZEIT.
  APPEND ILOGS.
  CALL FUNCTION 'ZSYS_RFC_LOG' STARTING NEW TASK 'RFCLOGS'
    TABLES
      LOGS = ILOGS.
  EXIT.

ENDFUNCTION.

你可能感兴趣的:(SAP,SD,ABAP,其他,经验分享)