利用函数BAPI_REQUIREMENTS_CREATE开发计划独立需求批导程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/champaignwolf/article/details/45688649

       项目中,计划独立需求批导也是项目上线时,用到的程序,下面代码是在某项目中利用函数BAPI_REQUIREMENTS_CREATE计划独立需求批导程序,分享一下,希望对需要的兄弟有帮助。

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

*& Report  ZPP_CUS_JHXQ
*&
*&---------------------------------------------------------------------*
*& Program Name     : ZPP_CUS_JHXQ
*& Description      : 计划独立需求批导创建
*& Author           : 
*& Date Written     : 
*& Tcode            : 
*& Note             :
*&---------------------------------------------------------------------*


REPORT zpp_cus_jhxq.
INCLUDE <icon>.


TABLES:sscrfields.
TYPES:BEGIN OF ty_up,
     "&&-----header------------
      pbdnr TYPE am60x-pbdnr,
      werks TYPE werks_d,
      versb TYPE rm60x-versb, "00"
*      datve TYPE rm60x-datve,
*      datbe TYPE rm60x-datbe,
*      entlu TYPE rm60x-entlu,
      datve(10),
      datbe(10),
      dtype TYPE prgrs, "3"
      matnr TYPE matnr,
      maktx TYPE maktx,
      werks2 TYPE werks_d,
      versb2 TYPE rm60x-versb, "00"
      vervs TYPE vervs, "X"
      date2(10),
      plnmg TYPE plnmg,
      verid TYPE verid,
      text1 TYPE vers_text,
     END OF ty_up.


DATA: gt_up TYPE TABLE OF ty_up,
      gs_up TYPE ty_up.
DATA gt_up_d TYPE TABLE OF ty_up.


DATA:re_item        LIKE  bapisitemr,
     re_schedule_in LIKE TABLE OF bapisshdin WITH HEADER LINE,
     return        LIKE TABLE OF bapireturn1 WITH HEADER LINE,
     re_change      LIKE bapisitemr.


DATA: BEGIN OF gs_log,
      sno TYPE i,
      matnr TYPE matnr,
      werks TYPE werks_d,
      pbdnr TYPE pbdnr,
      msg(50) TYPE c,
      END OF gs_log.
DATA:gt_log LIKE TABLE OF gs_log.


DATA:con_but(20) TYPE c.
DATA:v_code TYPE sy-ucomm.
DATA:wwwdata_new LIKE wwwdatatab.
DATA:g_stat. "数据状态
TYPES BEGIN OF ty_up2.
TYPES  uniq(40).
        INCLUDE TYPE ty_up.
TYPES END OF ty_up2.
DATA:gs_up2 TYPE ty_up2,
     gt_up2 TYPE TABLE OF ty_up2.


SELECTION-SCREEN FUNCTION KEY 1.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECTION-SCREEN SKIP.
PARAMETERS: p_file(80).
SELECTION-SCREEN END OF BLOCK b1.


SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
PARAMETERS:p_delold AS CHECKBOX.
*    p_create RADIOBUTTON GROUP g1 DEFAULT 'X',
*    p_change RADIOBUTTON GROUP g1 .
SELECTION-SCREEN END OF BLOCK b2.


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 COMMENT 1(20) text-c02.
*SELECTION-SCREEN COMMENT 1(30) text-c03.


SELECTION-SCREEN END OF BLOCK b3.


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


AT SELECTION-SCREEN.
  PERFORM screen_check.
  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.
*  IF p_create = 'X'.
  PERFORM creat_req.
*  ENDIF.
*  IF p_change = 'X'.
*    PERFORM change_req.
*  ENDIF.
  PERFORM output_log.


*&---------------------------------------------------------------------*
*&      Form  GET_EXCEL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_FILE  text
*----------------------------------------------------------------------*
FORM get_excel  USING  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
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        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  CREAT_REQ
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM creat_req .
  DATA:lt_up TYPE TABLE OF ty_up,
       ls_up TYPE ty_up.
  DATA:lv_pbdnr TYPE pbdnr.
  DATA:ls_pbim TYPE pbim,
       ls_pbed TYPE pbed.
  DATA:lv_delold.


  LOOP AT gt_up2 INTO gs_up2.
*    IF gs_up2-dtype = 'D'.
*      APPEND gs_up2 TO gt_up2_d.
*      DELETE gt_up2.
*      CONTINUE.
*    ENDIF.


    CONDENSE:gs_up2-matnr NO-GAPS,
             gs_up2-werks NO-GAPS,
             gs_up2-pbdnr NO-GAPS,
             gs_up2-datve NO-GAPS.
*    CLEAR g_stat.
    AT NEW uniq.
      g_stat = 'N'. "NEW
    ENDAT.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = gs_up2-matnr
      IMPORTING
        output = gs_up2-matnr.
    IF g_stat = 'N'.
      CLEAR:ls_pbim,ls_pbed.
      SELECT SINGLE * INTO ls_pbim FROM pbim
        WHERE matnr = gs_up2-matnr
        AND   werks = gs_up2-werks
        AND   bedae = 'BSF'
        AND   versb = gs_up2-versb
        AND   pbdnr = gs_up2-pbdnr.
      IF ls_pbim IS NOT INITIAL.
        g_stat = 'M'.
      ENDIF.
    ENDIF.


    re_item-material     = gs_up2-matnr.
    re_item-plant        = gs_up2-werks.
    re_item-version      = gs_up2-versb.
    re_item-vers_activ   = 'X'.
    re_item-req_number   = gs_up2-pbdnr.
*    re_item-requ_type    = 'BSF'.


*    re_schedule_in-date_type = '3'.
    IF gs_up2-dtype = 'M'.
      re_schedule_in-date_type = '3'.
    ELSEIF gs_up2-dtype = 'D'.
      re_schedule_in-date_type = '1'.
    ENDIF.


    REPLACE ALL OCCURRENCES OF '.' IN gs_up2-datve WITH ''.
    REPLACE ALL OCCURRENCES OF '-' IN gs_up2-datve WITH ''.
    REPLACE ALL OCCURRENCES OF '/' IN gs_up2-datve WITH ''.
    REPLACE ALL OCCURRENCES OF '.' IN gs_up2-date2 WITH ''.
    REPLACE ALL OCCURRENCES OF '-' IN gs_up2-date2 WITH ''.
    REPLACE ALL OCCURRENCES OF '/' IN gs_up2-date2 WITH ''.
    re_schedule_in-req_date  = gs_up2-date2.
    re_schedule_in-req_qty   = gs_up2-plnmg.
    re_schedule_in-prod_ves  = gs_up2-verid.


    APPEND re_schedule_in.
    CLEAR  re_schedule_in.


    IF g_stat = 'N'.
      CLEAR lv_pbdnr.
      CALL FUNCTION 'BAPI_REQUIREMENTS_CREATE'
        EXPORTING
          requirements_item        = re_item
        IMPORTING
*         MATERIAL                 =
*         PLANT                    =
          reqmtsplannumber         = lv_pbdnr
        TABLES
          requirements_schedule_in = re_schedule_in
          return                   = return.
      IF lv_pbdnr IS NOT INITIAL.
        gs_log-matnr = gs_up2-matnr.
        gs_log-werks = gs_up2-werks.
        gs_log-pbdnr = gs_up2-pbdnr.
        gs_log-msg   = '计划独立需求创建成功.'.
        APPEND gs_log TO gt_log.
        CLEAR gs_log.
        COMMIT WORK.
        g_stat = 'M'. "MODIFY
        CLEAR:return,return[],lv_pbdnr,re_item,re_schedule_in[].
        CONTINUE.
      ELSE.
        READ TABLE return WITH KEY type = 'E'.
        gs_log-matnr = gs_up2-matnr.
        gs_log-werks = gs_up2-werks.
        gs_log-msg   = return-message.
        APPEND gs_log TO gt_log.
        CLEAR gs_log.
        g_stat = 'E'. "ERROR
      ENDIF.
    ELSEIF g_stat = 'M'.
      CLEAR:ls_pbim,ls_pbed.
      SELECT SINGLE * INTO ls_pbim FROM pbim
        WHERE matnr = gs_up2-matnr
        AND   werks = gs_up2-werks
        AND   bedae = 'BSF'
        AND   versb = gs_up2-versb
        AND   pbdnr = gs_up2-pbdnr.
      SELECT SINGLE * INTO ls_pbed FROM pbed WHERE bdzei = ls_pbim-bdzei AND pdatu = gs_up2-date2.
      IF ls_pbed-plnmg NE 0.
        IF p_delold = ''.
          gs_log-matnr = gs_up2-matnr.
          gs_log-werks = gs_up2-werks.
          gs_log-pbdnr = gs_up2-pbdnr.
          gs_log-msg   = '此日期下计划已存在,不允许覆盖.'.
          APPEND gs_log TO gt_log.
          CLEAR gs_log.
          CLEAR:return,return[],lv_pbdnr,re_item,re_schedule_in[].
          CONTINUE.
        ENDIF.
      ENDIF.
      CLEAR re_change.
      CALL FUNCTION 'BAPI_REQUIREMENTS_CHANGE'
        EXPORTING
          material                 = gs_up2-matnr
          plant                    = gs_up2-werks
          requirementstype         = space
          version                  = gs_up2-versb
          reqmtsplannumber         = gs_up2-pbdnr
          vers_activ               = 'X'
*         REQUIREMENT_PARAM        =
*         MRP_AREA                 =
*         DO_COMMIT                = 'X'
*         UPDATE_MODE              = 'X'
          delete_old               = ''
*         NO_WITHDR                = ' '
*         MATERIAL_EVG             =
        IMPORTING
          requirement_item_out     = re_change
        TABLES
          requirements_schedule_in = re_schedule_in
*         REQUIREMENTS_CHAR_IN     =
          return                   = return.
      IF re_change IS NOT INITIAL.
        gs_log-matnr = gs_up2-matnr.
        gs_log-werks = gs_up2-werks.
        gs_log-pbdnr = gs_up2-pbdnr.
        gs_log-msg   = '计划独立需求追加成功.'.
        IF p_delold = 'X'.
          gs_log-msg   = '计划独立需求修改成功.'.
        ENDIF.
        APPEND gs_log TO gt_log.
        CLEAR gs_log.
        COMMIT WORK.
      ELSE.
        READ TABLE return WITH KEY type = 'E'.
        gs_log-matnr = gs_up2-matnr.
        gs_log-werks = gs_up2-werks.
        gs_log-msg   = return-message.
        APPEND gs_log TO gt_log.
        CLEAR gs_log.
      ENDIF.
    ELSEIF g_stat = 'E'.
      gs_log-matnr = gs_up2-matnr.
      gs_log-werks = gs_up2-werks.
      gs_log-msg   = '创建失败'.
      APPEND gs_log TO gt_log.
      CLEAR gs_log.
      CLEAR:return,return[],lv_pbdnr,re_item,re_schedule_in[].
      CONTINUE.
    ENDIF.
    CLEAR:return,return[],lv_pbdnr,re_item,re_schedule_in[].
  ENDLOOP.


ENDFORM.                    " CREAT_REQ
*&---------------------------------------------------------------------*
*&      Form  OUTPUT_LOG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM output_log .
  DATA:lv_count TYPE i.
  LOOP AT gt_log INTO gs_log.
    lv_count = lv_count + 1.
    gs_log-sno = lv_count.
    WRITE:/ gs_log-sno, '物料', gs_log-matnr, '在工厂', gs_log-werks, gs_log-msg.
  ENDLOOP.
ENDFORM.                    " OUTPUT_LOG
*&---------------------------------------------------------------------*
*&      Form  GET_TEMPLATE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        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_PIR_TEMPLATE'.              "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
*&---------------------------------------------------------------------*
*&      Form  CHANGE_REQ
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM change_req .
  LOOP AT gt_up INTO gs_up.


    CONDENSE:gs_up-matnr NO-GAPS,
             gs_up-werks NO-GAPS,
             gs_up-pbdnr NO-GAPS,
             gs_up-datve NO-GAPS.


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


    re_item-material     = gs_up-matnr.
    re_item-plant        = gs_up-werks.
    re_item-version      = '00'.
    re_item-vers_activ   = 'X'.
    re_item-req_number   = gs_up-pbdnr.
*    re_item-requ_type    = 'BSF'.


    IF gs_up-dtype = 'M'.
      re_schedule_in-date_type = '3'.
    ELSEIF gs_up-dtype = 'D'.
      re_schedule_in-date_type = '1'.
    ENDIF.


    REPLACE ALL OCCURRENCES OF '.' IN gs_up-datve WITH ''.
    REPLACE ALL OCCURRENCES OF '-' IN gs_up-datve WITH ''.
    REPLACE ALL OCCURRENCES OF '/' IN gs_up-datve WITH ''.
    REPLACE ALL OCCURRENCES OF '.' IN gs_up-date2 WITH ''.
    REPLACE ALL OCCURRENCES OF '-' IN gs_up-date2 WITH ''.
    REPLACE ALL OCCURRENCES OF '/' IN gs_up-date2 WITH ''.
    re_schedule_in-req_date  = gs_up-date2.
    re_schedule_in-req_qty   = gs_up-plnmg.
    re_schedule_in-prod_ves  = gs_up-verid.


    APPEND re_schedule_in.
    CLEAR  re_schedule_in.


    CALL FUNCTION 'BAPI_REQUIREMENTS_CHANGE'
      EXPORTING
        material                 = gs_up-matnr
        plant                    = gs_up-werks
        requirementstype         = space
        version                  = '00'
        reqmtsplannumber         = gs_up-pbdnr
        vers_activ               = 'X'
*       REQUIREMENT_PARAM        =
*       MRP_AREA                 =
*       DO_COMMIT                = 'X'
*       UPDATE_MODE              = 'X'
        delete_old               = ''
*       NO_WITHDR                = ' '
*       MATERIAL_EVG             =
      IMPORTING
        requirement_item_out     = re_change
      TABLES
        requirements_schedule_in = re_schedule_in
*       REQUIREMENTS_CHAR_IN     =
        return                   = return.


    IF re_change IS NOT INITIAL.
      gs_log-matnr = gs_up-matnr.
      gs_log-werks = gs_up-werks.
      gs_log-pbdnr = gs_up-pbdnr.
      gs_log-msg   = '计划独立需求修改成功.'.
      APPEND gs_log TO gt_log.
      CLEAR gs_log.
      COMMIT WORK.
    ELSE.
      READ TABLE return WITH KEY type = 'E'.
      gs_log-matnr = gs_up-matnr.
      gs_log-werks = gs_up-werks.
      gs_log-msg   = return-message.
      APPEND gs_log TO gt_log.
      CLEAR gs_log.
    ENDIF.
    CLEAR:return,return[],re_schedule_in[],gs_up,re_change.
  ENDLOOP.
ENDFORM.                    " CHANGE_REQ
*&---------------------------------------------------------------------*
*&      Form  SCREEN_CHECK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM screen_check .
  IF sy-ucomm = 'ONLI' .
    IF p_file = ''.
      MESSAGE '请选择上传文件' TYPE 'E'.
    ENDIF.
  ENDIF.
ENDFORM.                    " SCREEN_CHECK
*&---------------------------------------------------------------------*
*&      Form  PROCESS_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM process_data .
  LOOP AT gt_up INTO gs_up.
    MOVE-CORRESPONDING gs_up TO gs_up2.
    CONCATENATE gs_up-pbdnr gs_up-werks gs_up-versb gs_up-matnr INTO gs_up2-uniq.
    APPEND gs_up2 TO gt_up2.
    CLEAR  gs_up2.
  ENDLOOP.
ENDFORM.                    " PROCESS_DATA


*Text elements
*----------------------------------------------------------
* 001 上传文件
* 002 选择操作
* 003 注释
* C01 注意事项:
* C02 如勾选覆盖,同一计划需求下同一物料同一日期同一版本的计划数量会被覆盖,请慎重选择




*Selection texts
*----------------------------------------------------------
* P_CHANGE         修改
* P_CREATE         创建
* P_DELOLD         覆盖现有数据
* P_FILE         选择上传文件




*Messages
*----------------------------------------------------------
*
* Message class: Hard coded
*   模板下载失败


----------------------------------------------------------------------------------
Extracted by Direct Download Enterprise version 1.3.1 - E.G.Mellodew. 1998-2005 UK. Sap Release 731
阅读更多
换一批

没有更多推荐了,返回首页