利用SD_SALESDOCUMENT_CREATE 批导动态数据SO

        期初上线时,SO作为动态数据,是批导入系统必须做的一步,好多朋友利用bdc、lsmw、scatt等工具都可以做,下面是项目中利用SD_SALESDOCUMENT_CREATE 进行批导的一些代码,分享一下,希望对用到的朋友有帮助。


*&---------------------------------------------------------------------*

*& Report  ZSD_BATCH_SO
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*


REPORT zsd_batch_so.


TABLES:sscrfields.
TYPES:BEGIN OF ty_up,
      auart    TYPE vbak-auart,
      vkorg    TYPE vbak-vkorg,
      vtweg    TYPE vbak-vtweg,
      spart   TYPE vbak-spart  ,  "产品组
      kunnr_ag TYPE vbak-kunnr  ,  "客户编号
                                      "客户名称
      kunnr_re TYPE vbak-kunnr  ,  "开票方编号
                                      "开票方名称
      kunnr_rg TYPE vbak-kunnr  ,  "付款方
      kunnr_we TYPE vbak-kunnr  ,  "送达方编号
                                      "送达方名称
      kunnr_za TYPE vbak-kunnr  ,  "业务员编码
                                      "业务员
      bstnk   TYPE  vbak-bstnk    ,  "采购订单编号/U8发
      augru    TYPE  vbak-augru   ,  "订单原因
      matnr TYPE   vbap-matnr     ,  "物料编码
      lgort TYPE   vbap-lgort     ,  "库存地点
      charg TYPE   vbap-charg     ,  "批次
      kwmeng TYPE vbap-kwmeng     ,  "订单数量
      vrkme TYPE   vbap-vrkme     ,  "订单数量单位
      kschl TYPE   konv-kschl     ,  "
      netpr TYPE   vbap-netpr     ,  "订单单价
      kpein TYPE   vbap-kpein     ,  "金额单位
      kmein TYPE   vbap-kmein     ,  "基本单位
      END OF ty_up.
DATA:gs_up TYPE ty_up,
     gt_up TYPE TABLE OF ty_up WITH HEADER LINE.


DATA:gt_log LIKE TABLE OF zsdbatchso WITH HEADER LINE.


DATA BEGIN OF gt_log1 OCCURS 0.
        INCLUDE STRUCTURE  zsdbatchso.
DATA sno TYPE i.
DATA END OF gt_log1.


DATA: con_but(20) TYPE c.
DATA: v_code TYPE sy-ucomm.
DATA: wwwdata_new LIKE wwwdatatab.


SELECTION-SCREEN FUNCTION KEY 1.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-b01.
PARAMETERS: p_file(80).
SELECTION-SCREEN SKIP.
PARAMETER:p_testr TYPE bapiflag-bapiflag AS CHECKBOX.
SELECTION-SCREEN END OF BLOCK b1.


SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE text-003.
SELECTION-SCREEN: BEGIN OF LINE,
 COMMENT 1(20) text-c01.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: BEGIN OF LINE,
 COMMENT 1(60) text-c02.
SELECTION-SCREEN: END OF LINE.


SELECTION-SCREEN END OF BLOCK b3.


INITIALIZATION.
  CONCATENATE space space '@49@' '下载EXCEL模板' INTO con_but.
  sscrfields-functxt_01 = con_but.


AT SELECTION-SCREEN.
  PERFORM get_template.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM get_excel USING p_file.


START-OF-SELECTION.
  PERFORM upload_data.
  PERFORM process_data.
  PERFORM create_so.
  PERFORM save_log.


*&---------------------------------------------------------------------*
*&      Form  get_excel
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_FILE   text
*----------------------------------------------------------------------*
FORM get_excel  USING    p_p_file.
  DATA: l_filetab   TYPE filetable,
        l_rc        TYPE i.
  CLEAR l_filetab.
  REFRESH l_filetab.
* Open dialog
  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
*     WINDOW_TITLE         = 'SAP Custom - Open File'
*     DEFAULT_EXTENSION    =
      default_filename     = '*.xls'
*     FILE_FILTER          = '*.xls'
      initial_directory    = 'd:\'
      multiselection       = ''
    CHANGING
      file_table           = l_filetab
      rc                   = l_rc
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
* Get file path
  CHECK l_rc EQ 1.
  READ TABLE l_filetab INDEX 1 INTO p_file.
ENDFORM.                    " GET_EXCEL






*&---------------------------------------------------------------------*
*&      Form  upload_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM upload_data .
  DATA: BEGIN OF i_excel OCCURS 0.
          INCLUDE STRUCTURE alsmex_tabline.
  DATA: END OF i_excel.
  DATA: l_answer(1) TYPE c.
  DATA: l_column TYPE i.
  FIELD-SYMBOLS: <fs>.
  DATA: l_pathname LIKE rlgrap-filename.


  MOVE p_file TO l_pathname.
  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = l_pathname
      i_begin_col             = '1'
      i_begin_row             = '1'
      i_end_col               = '99'
      i_end_row               = '9999'
    TABLES
      intern                  = i_excel
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    IF i_excel[] IS INITIAL.
      CALL FUNCTION 'POPUP_TO_CONFIRM_WITH_MESSAGE'
        EXPORTING
*         DEFAULTOPTION  = 'Y'
          diagnosetext1  = '没有数据'
*         DIAGNOSETEXT2  = ' '
*         DIAGNOSETEXT3  = ' '
          textline1      = '没有数据'
*         TEXTLINE2      = ' '
          titel          = '错误'
*         START_COLUMN   = 25
*         START_ROW      = 6
*         CANCEL_DISPLAY = 'X'
        IMPORTING
          answer         = l_answer.
    ELSE.
      DELETE i_excel WHERE row = '0001'.
      SORT i_excel BY row col.
      LOOP AT i_excel.
        MOVE i_excel-col TO l_column.
        ASSIGN COMPONENT l_column OF STRUCTURE gs_up TO <fs>.
        MOVE i_excel-value TO <fs>.
        AT END OF row.
          APPEND gs_up TO gt_up.
          CLEAR gs_up.
        ENDAT.
      ENDLOOP.
    ENDIF.
  ENDIF.
ENDFORM.                    " UPLOAD_DATA
*&---------------------------------------------------------------------*
*&      Form  PROCESS_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM process_data .
  LOOP AT gt_up.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = gt_up-kunnr_ag
      IMPORTING
        output = gt_up-kunnr_ag.


    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = gt_up-kunnr_re
      IMPORTING
        output = gt_up-kunnr_re.


    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = gt_up-kunnr_rg
      IMPORTING
        output = gt_up-kunnr_rg.


    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = gt_up-kunnr_za
      IMPORTING
        output = gt_up-kunnr_za.


    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = gt_up-kunnr_we
      IMPORTING
        output = gt_up-kunnr_we.


    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = gt_up-matnr
      IMPORTING
        output = gt_up-matnr.


    MODIFY gt_up.
  ENDLOOP.
ENDFORM.                    " PROCESS_DATA
*&---------------------------------------------------------------------*
*&      Form  CREATE_SO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM create_so .
  DATA:
   wa_order_header_in      LIKE bapisdhd1,
   wa_order_items_in       LIKE bapisditm,
   wa_order_partners       LIKE bapiparnr,
   wa_order_schedules_in   LIKE bapischdl,
   wa_order_conditions_in  LIKE bapicond,
   wa_order_conditions_inx  LIKE bapicondx,
   wa_order_text           LIKE bapisdtext ,
   wa_return               LIKE bapiret2,
   wa_return2              LIKE bapiret2.
  DATA:
     order_header_in       LIKE  TABLE OF  bapisdhd1,
     order_items_in        LIKE  TABLE OF  bapisditm,
     order_partners        LIKE  TABLE OF  bapiparnr,
     order_schedules_in    LIKE  TABLE OF  bapischdl,
     order_conditions_in   LIKE  TABLE OF  bapicond,
     order_conditions_inx   LIKE  TABLE OF  bapicondx,
     order_text            LIKE  TABLE OF  bapisdtext,
     return1              LIKE  TABLE OF  bapiret2 WITH HEADER LINE,
     return2               LIKE  TABLE OF  bapiret2.
  DATA l_vbeln TYPE vbak-vbeln.


  LOOP AT gt_up.
    CLEAR wa_order_header_in.
    wa_order_header_in-doc_type   =  gt_up-auart.
    wa_order_header_in-sales_org  =  gt_up-vkorg.
    wa_order_header_in-distr_chan = gt_up-vtweg.
    wa_order_header_in-division =     gt_up-spart.
    wa_order_header_in-doc_date = sy-datum.
    wa_order_header_in-purch_no_c = gt_up-bstnk.
    wa_order_header_in-ord_reason = gt_up-augru.


    CLEAR:wa_order_items_in,order_items_in.
    wa_order_items_in-itm_number = 10.
    wa_order_items_in-material = gt_up-matnr.
    wa_order_items_in-plant = '8100'.
    wa_order_items_in-store_loc = gt_up-lgort.
    wa_order_items_in-batch = gt_up-charg.
    wa_order_items_in-sales_unit = gt_up-vrkme.


    APPEND wa_order_items_in TO order_items_in.


    CLEAR order_partners.
    CLEAR wa_order_partners.
    wa_order_partners-partn_role = 'AG'.
*    wa_order_partners-itm_number = 10.
    wa_order_partners-partn_numb = gt_up-kunnr_ag.
    APPEND wa_order_partners TO order_partners.


    CLEAR wa_order_partners.
    wa_order_partners-partn_role = 'RE'.
*    wa_order_partners-itm_number = 10.
    wa_order_partners-partn_numb = gt_up-kunnr_re.
    APPEND wa_order_partners TO order_partners.


    CLEAR wa_order_partners.
    wa_order_partners-partn_role = 'RG'.
*    wa_order_partners-itm_number = 10.
    wa_order_partners-partn_numb = gt_up-kunnr_rg.
    APPEND wa_order_partners TO order_partners.


    CLEAR wa_order_partners.
    wa_order_partners-partn_role = 'WE'.
*    wa_order_partners-itm_number = 10.
    wa_order_partners-partn_numb = gt_up-kunnr_we.
    APPEND wa_order_partners TO order_partners.


    CLEAR wa_order_partners.
    wa_order_partners-partn_role = 'ZA'.
*    wa_order_partners-itm_number = 10.
    wa_order_partners-partn_numb = gt_up-kunnr_za.
    APPEND wa_order_partners TO order_partners.


    CLEAR: wa_order_schedules_in,order_schedules_in.


    wa_order_schedules_in-itm_number = 10.
    wa_order_schedules_in-req_qty = gt_up-kwmeng.
    APPEND wa_order_schedules_in TO order_schedules_in.


    CLEAR: wa_order_conditions_in,order_conditions_in.
    wa_order_conditions_in-itm_number = 10.
    wa_order_conditions_in-cond_st_no = 10.
    wa_order_conditions_in-cond_count = 1.
    wa_order_conditions_in-cond_type = 'PR00'.
    wa_order_conditions_in-cond_value = gt_up-netpr.
    wa_order_conditions_in-currency = 'CNY'.
    wa_order_conditions_in-cond_unit = gt_up-kmein.
    wa_order_conditions_in-cond_p_unt = gt_up-kpein.
    APPEND wa_order_conditions_in TO order_conditions_in.


    CLEAR: wa_order_conditions_inx,order_conditions_inx.
    wa_order_conditions_inx-itm_number = 10.
    wa_order_conditions_inx-cond_st_no = 10.
    wa_order_conditions_inx-cond_count = 1.
    wa_order_conditions_inx-updateflag = 'U'.


    wa_order_conditions_inx-cond_type = 'X'.
    wa_order_conditions_inx-cond_value = 'X'.
    wa_order_conditions_inx-currency = 'X'.
    wa_order_conditions_inx-cond_unit = 'X'.
    wa_order_conditions_inx-cond_p_unt = 'X'.
    APPEND wa_order_conditions_inx TO order_conditions_inx.




    CLEAR l_vbeln.
*    CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
*      EXPORTING
*        order_header_in      = wa_order_header_in
*      IMPORTING
*        salesdocument        = l_vbeln
*      TABLES
*        return               = return1
*        order_items_in       = order_items_in
**       ORDER_ITEMS_INX      =
*        order_partners       = order_partners
*        order_schedules_in   = order_schedules_in
**       ORDER_SCHEDULES_INX  =
*        order_conditions_in  = order_conditions_in
*        order_conditions_inx = order_conditions_inx.


    CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'
      EXPORTING
        sales_header_in      = wa_order_header_in
        testrun              = p_testr
      IMPORTING
        salesdocument_ex     = l_vbeln
      TABLES
        return               = return1
        sales_items_in       = order_items_in
*       ORDER_ITEMS_INX      =
        sales_partners       = order_partners
        sales_schedules_in   = order_schedules_in
*       ORDER_SCHEDULES_INX  =
        sales_conditions_in  = order_conditions_in
        sales_conditions_inx = order_conditions_inx.
    IF l_vbeln IS NOT INITIAL .
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
      gt_log-vbeln = l_vbeln.
      gt_log-gname = sy-uname.
      gt_log-gdate = sy-datum.
      gt_log-gtime = sy-uzeit.
      APPEND gt_log.
      CLEAR  gt_log.
    ELSE.
      IF p_testr = 'X'.
        READ TABLE return1 WITH KEY type = 'E'.
        IF sy-subrc = 0.
          gt_log-log = return1-message.
          APPEND gt_log.
        ELSE.
          gt_log-log = '可以创建销售订单'.
          APPEND gt_log.
        ENDIF.
      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        READ TABLE return1 WITH KEY type = 'E'.
        gt_log-log = return1-message.
        APPEND gt_log.
      ENDIF.
    ENDIF.
    CLEAR gt_up.
    CLEAR return1.
    CLEAR gt_log.
  ENDLOOP.


ENDFORM.                    " CREATE_SO
*&---------------------------------------------------------------------*
*&      Form  SAVE_LOG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM save_log .
  DATA lv_sno TYPE i.


  IF p_testr = ''.
    MODIFY zsdbatchso FROM TABLE gt_log .
    COMMIT WORK AND WAIT.
  ENDIF.


*  LOOP AT gt_log.
*    lv_sno = lv_sno + 1.
*    MOVE-CORRESPONDING gt_log TO gt_log1.
*    gt_log1-sno = lv_sno.
*    APPEND gt_log1.
*    CLEAR:gt_log1,gt_log.
*  ENDLOOP.
  lv_sno = 0.
  LOOP AT gt_log.
    lv_sno = lv_sno + 1.
    MOVE-CORRESPONDING gt_log TO gt_log1.
    gt_log1-sno = lv_sno.
    IF gt_log1-vbeln NE ''.
      WRITE:/ gt_log1-sno, '销售订单' ,gt_log-vbeln,'创建成功'.
    ELSE.
      WRITE:/ gt_log1-sno,gt_log-log.
    ENDIF.
    CLEAR:gt_log1,gt_log.
  ENDLOOP.


ENDFORM.                    " SAVE_LOG


*&---------------------------------------------------------------------*
*&      Form  get_template
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM get_template .
  v_code = sscrfields-ucomm.
  CASE v_code.
    WHEN 'FC01'.
      PERFORM download_excel_template.
  ENDCASE.


ENDFORM.                    " GET_TEMPLATE
*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD_EXCEL_TEMPLATE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM download_excel_template .


  DATA: rc TYPE sy-subrc,
            temp.


  wwwdata_new-relid = 'MI'.
  wwwdata_new-objid = 'Z_BATCH_SO'.              "SMW0定义的对象名称
*下载模板
  CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
    EXPORTING
      key         = wwwdata_new
      destination = 'D:\批量创建销售订单.XLS'
    IMPORTING
      rc          = rc
    CHANGING
      temp        = temp.
  IF rc NE 0.
    MESSAGE '模板下载失败' TYPE 'E'.
  ELSE.
    MESSAGE '模板下载到D盘根目录成功' TYPE 'S'.
  ENDIF.


ENDFORM.                    " DOWNLOAD_EXCEL_TEMPLATE


*Text elements
*----------------------------------------------------------
* 003 备注
* B01 文件路径
* C01 注意事项:
* C02 严格按照模板字段填写。所有字母必须大写,否则会导入失败。





你可能感兴趣的:(利用SD_SALESDOCUMENT_CREATE 批导动态数据SO)