ABAP:C223批量创建生产版本

采用BDC方式

*&---------------------------------------------------------------------*
*& Report ZPPU011
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZPPU011.


*---------------------------------------------------------------------*
* Tables Declarition
*---------------------------------------------------------------------*
TABLES: mkal,sscrfields.

*---------------------------------------------------------------------*
* Types
*---------------------------------------------------------------------*
TYPES:BEGIN OF ty_tab,
        werks TYPE mkal-werks, "工厂
        matnr TYPE mkal-matnr, "物料号
        verid TYPE mkal-verid, "生产版本号
        text1 TYPE mkal-text1, "生产版本描述
        bstmi TYPE mkal-bstmi, "从批量
        bstma TYPE mkal-bstma, "到批量
        adatu TYPE mkal-adatu, "生产版本开始日期
        bdatu TYPE mkal-bdatu, "生产版本有效结束日
        plnty TYPE mkal-plnty, "任务清单类型
        plnnr TYPE mkal-plnnr, "工艺路线组
        alnal TYPE mkal-alnal, "工艺路线组计数器
        stlal TYPE mkal-stlal, "可选BOM
        stlan TYPE mkal-stlan, "BOM用途
        elpro TYPE mkal-elpro, "发货存储位置
        alort TYPE mkal-alort, "收货地点

        maktx TYPE makt-maktx, "物料描述
        icon  TYPE icon_d,     "消息类型
        msg   TYPE bapi_msg,   "消息
      END OF ty_tab.

*内表定义
DATA: gt_tab TYPE TABLE OF ty_tab WITH HEADER LINE.

*ALV定义
DATA: gt_fieldcat TYPE lvc_t_fcat,
      gs_fieldcat TYPE lvc_s_fcat,
      gs_layout   TYPE lvc_s_layo.

*EXCEL文件定义
DATA: gv_objid     TYPE w3objid VALUE 'ZPPU011',
      gv_filename  TYPE string  VALUE '生产版本导入模板',
      gv_begin_col TYPE i VALUE 1,
      gv_begin_row TYPE i VALUE 2,
      gv_end_col   TYPE i VALUE 15,
      gv_end_row   TYPE i VALUE 65535.

*BDC定义
DATA: gt_bdcdata TYPE TABLE OF bdcdata WITH HEADER LINE,
      gt_messtab TYPE TABLE OF bdcmsgcoll WITH HEADER LINE,
      gt_return  TYPE TABLE OF bapiret2 WITH HEADER LINE.

CONSTANTS: c_global_applid TYPE applid VALUE 'TKW-4035'.

*---------------------------------------------------------------------*
* Selection Screen
*---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK bk1.
  PARAMETERS: p_file         LIKE rlgrap-filename MODIF ID m1.
  SELECT-OPTIONS: s_werks FOR mkal-werks MODIF ID m2,
                  s_matnr FOR mkal-matnr MODIF ID m2.
SELECTION-SCREEN END OF BLOCK bk1.

SELECTION-SCREEN BEGIN OF BLOCK bk2.
  PARAMETERS: p_dr RADIOBUTTON GROUP g1 DEFAULT 'X' USER-COMMAND u2,
              p_cx RADIOBUTTON GROUP g1.
SELECTION-SCREEN END OF BLOCK bk2.
SELECTION-SCREEN:FUNCTION KEY 1.

*---------------------------------------------------------------------*
* Initialization
*---------------------------------------------------------------------*
INITIALIZATION.
  sscrfields-functxt_01 = icon_export && '模版下载'.

*---------------------------------------------------------------------*
* At Selection Screen
*---------------------------------------------------------------------*
AT SELECTION-SCREEN.
  IF sscrfields-ucomm = 'FC01'.
    PERFORM frm_get_model USING gv_objid.
  ENDIF.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM frm_f4."路径搜索帮助打开

*---------------------------------------------------------------------*
* At Selection-Screen Output
*---------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
  PERFORM frm_set_screen.

*---------------------------------------------------------------------*
* Start-of-selection
*---------------------------------------------------------------------*
START-OF-SELECTION.
  IF p_dr = 'X'.
    IF p_file IS INITIAL.
      MESSAGE s055(00) DISPLAY LIKE 'E'.
      RETURN.
    ENDIF.
    PERFORM frm_upload_data. "上载数据
    PERFORM frm_setdata.

  ELSEIF p_cx = 'X'.
    PERFORM frm_get_data.
  ENDIF.

  PERFORM frm_layout.
  PERFORM frm_fieldcat.
  PERFORM frm_output.

*&---------------------------------------------------------------------*
*& Form frm_set_screen
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM frm_set_screen .
  LOOP AT SCREEN.
    IF p_dr = 'X'.
      IF screen-group1 = 'M2'.
        screen-active = 0.
      ELSE.
        screen-active = 1.
      ENDIF.
    ENDIF.
    IF p_cx = 'X'.
      IF screen-group1 = 'M1'.
        screen-active = 0.
      ELSE.
        screen-active = 1.
      ENDIF.
    ENDIF.
    MODIFY SCREEN.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form  FRM_SETDATA
*&---------------------------------------------------------------------*
*&       text
*&---------------------------------------------------------------------*
FORM frm_setdata .
  DATA: lv_bstmi TYPE string,
        lv_bstma TYPE string,
        lt_tab   TYPE TABLE OF ty_tab WITH HEADER LINE.

  lt_tab[] = gt_tab[].
  DELETE ADJACENT DUPLICATES FROM lt_tab COMPARING werks matnr.

  LOOP AT lt_tab.
    PERFORM bdc_dynpro USING 'SAPLCMFV'   '1000'.
    PERFORM bdc_field: USING 'BDC_OKCODE' '=ENTE',
                       USING 'BDC_CURSOR' 'MKAL-MATNR',
                       USING 'MKAL-WERKS' lt_tab-werks,
                       USING 'MKAL-MATNR' lt_tab-matnr.

    LOOP AT gt_tab WHERE werks = lt_tab-werks AND matnr = lt_tab-matnr.
      PERFORM bdc_dynpro USING 'SAPLCMFV'   '1000'.
      PERFORM bdc_field: USING 'BDC_OKCODE' '=CREA',
                         USING 'BDC_CURSOR' 'MKAL-WERKS',
                         USING 'MKAL-WERKS' gt_tab-werks,
                         USING 'MKAL-MATNR' gt_tab-matnr.

      PERFORM bdc_dynpro USING 'SAPLCMFV'   '2000'.
      PERFORM bdc_field: USING 'BDC_CURSOR' 'MKAL_EXPAND-STLAN',
                         USING 'BDC_OKCODE' '=CLOS',
                         USING 'MKAL_EXPAND-MATNR' gt_tab-matnr,
                         USING 'MKAL_EXPAND-VERID' gt_tab-verid,
                         USING 'MKAL_EXPAND-TEXT1' gt_tab-text1.

      CLEAR:lv_bstmi,lv_bstma.
      lv_bstmi = gt_tab-bstmi.
      lv_bstma = gt_tab-bstma.

      PERFORM bdc_field: USING 'MKAL_EXPAND-BSTMI' lv_bstmi,
                         USING 'MKAL_EXPAND-BSTMA' lv_bstma,
                         USING 'MKAL_EXPAND-ADATU' gt_tab-adatu,
                         USING 'MKAL_EXPAND-BDATU' gt_tab-bdatu.

      IF gt_tab-plnty IS NOT INITIAL.
        PERFORM bdc_field USING 'MKAL_EXPAND-PLNTY' gt_tab-plnty.
      ELSE.
        PERFORM bdc_field USING 'MKAL_EXPAND-PLNTY' 'N'.
      ENDIF.

      PERFORM bdc_field: USING 'MKAL_EXPAND-PLNNR' gt_tab-plnnr,
                         USING 'MKAL_EXPAND-ALNAL' gt_tab-alnal,
                         USING 'MKAL_EXPAND-STLAL' gt_tab-stlal,
                         USING 'MKAL_EXPAND-STLAN' gt_tab-stlan,
                         USING 'MKAL_EXPAND-ELPRO' gt_tab-elpro,
                         USING 'MKAL_EXPAND-ALORT' gt_tab-alort,
                         USING 'BDC_OKCODE'        '=PRFG'.   "点击检查按钮

      PERFORM bdc_dynpro USING 'SAPMSSY0'   '0120'.
      PERFORM bdc_field  USING 'BDC_OKCODE' '=RW'.     "退出检查窗口

      PERFORM bdc_dynpro USING 'SAPLCMFV'   '2000'.
      PERFORM bdc_field  USING 'BDC_OKCODE' '/ECANC'.  "退出生产版本明细窗口
    ENDLOOP.

    PERFORM bdc_dynpro USING 'SAPLCMFV'     '1000'.
    PERFORM bdc_field: USING 'BDC_OKCODE'   '=SAVE',
                       USING 'BDC_CURSOR' 'MKAL-WERKS'.

    CALL TRANSACTION 'C223' USING gt_bdcdata
                     MODE   'N'
                     UPDATE 'L'
                     MESSAGES INTO gt_messtab.

    CALL FUNCTION 'CONVERT_BDCMSGCOLL_TO_BAPIRET2'
      TABLES
        imt_bdcmsgcoll = gt_messtab
        ext_return     = gt_return.
    LOOP AT gt_return WHERE type CA 'EXA'.
      EXIT.
    ENDLOOP.
    IF sy-subrc = 0.
      LOOP AT gt_tab WHERE werks = lt_tab-werks AND matnr = lt_tab-matnr.
        gt_tab-icon = icon_red_light.
        gt_tab-msg = gt_return-message.
        MODIFY gt_tab.
      ENDLOOP.

    ELSE.
      LOOP AT gt_tab WHERE werks = lt_tab-werks AND matnr = lt_tab-matnr.
        gt_tab-icon = icon_green_light.
        gt_tab-msg = '创建成功'.
        MODIFY gt_tab.
      ENDLOOP.
    ENDIF.

    CLEAR: gt_bdcdata[],gt_messtab[],gt_return[].
  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form  FRM_GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM frm_get_data .
  SELECT mkal~werks mkal~matnr mkal~verid mkal~text1
         mkal~bstmi mkal~bstma mkal~adatu mkal~bdatu
         mkal~plnty mkal~plnnr mkal~alnal mkal~stlal
         mkal~stlan mkal~elpro mkal~alort
         makt~maktx
    FROM mkal
    INNER JOIN makt ON mkal~matnr = makt~matnr
                   AND makt~spras = sy-langu
    INTO CORRESPONDING FIELDS OF TABLE gt_tab
    WHERE mkal~werks IN s_werks
      AND mkal~matnr IN s_matnr.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form  FRM_LAYOUT
*&---------------------------------------------------------------------*
*&       text
*&---------------------------------------------------------------------*
FORM frm_layout .
  gs_layout-zebra = 'X' . " 使ALV界面呈现颜色交替
  gs_layout-sel_mode = 'A' . " 选择模式,“A”在最左端有选择按钮
  gs_layout-cwidth_opt = 'X' . " 自动优化列宽
  gs_layout-detailinit = 'X' . " 是否出现细节屏幕
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_FIELDCAT
*&---------------------------------------------------------------------*
*&       text
*&---------------------------------------------------------------------*
FORM frm_fieldcat .
  IF p_dr = 'X'.
    PERFORM frm_field_lvc TABLES gt_fieldcat USING 'ICON' '' '' '标识' '' '' '' '' ''.
    PERFORM frm_field_lvc TABLES gt_fieldcat USING 'MSG' '' '' '消息' '' '' '' '' ''.
  ENDIF.

  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'WERKS' 'MKAL' 'WERKS' '工厂' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'MATNR' 'MKAL' 'MATNR' '物料号' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'MAKTX' 'MAKT' 'MAKTX' '物料描述' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'VERID' 'MKAL' 'VERID' '生产版本号' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'TEXT1' 'MKAL' 'TEXT1' '生产版本描述' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'BSTMI' 'MKAL' 'BSTMI' '从批量' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'BSTMA' 'MKAL' 'BSTMA' '到批量' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'ADATU' 'MKAL' 'ADATU' '有效开始日期' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'BDATU' 'MKAL' 'BDATU' '有效结束日期' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'PLNTY' 'MKAL' 'PLNTY' '任务清单类型' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'PLNNR' 'MKAL' 'PLNNR' '工艺路线组' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'ALNAL' 'MKAL' 'ALNAL' '工艺路线组计数器' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'STLAL' 'MKAL' 'STLAL' '可选BOM' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'STLAN' 'MKAL' 'STLAN' 'BOM用途' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'ELPRO' 'MKAL' 'ELPRO' '发货存储位置' '' '' '' '' ''.
  PERFORM frm_field_lvc TABLES gt_fieldcat USING 'ALORT' 'MKAL' 'ALORT' '收货地点' '' '' '' '' ''.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_FIELD_LVC
*&---------------------------------------------------------------------*
*&      设置字段目录
*&---------------------------------------------------------------------*
FORM frm_field_lvc TABLES pt_fieldcat_lvc STRUCTURE lvc_s_fcat
                    USING pv_fieldname TYPE lvc_rfname
                          pv_ref_table TYPE lvc_tname
                          pv_ref_field TYPE lvc_rfname
                          pv_text TYPE string
                          pv_edit TYPE c
                          pv_nzero TYPE c
                          pv_f4 TYPE c
                          pv_no_out TYPE c
                          pv_outputlen TYPE i.

  DATA: ls_fieldcat_lvc TYPE lvc_s_fcat.

  ls_fieldcat_lvc-fieldname = pv_fieldname.
  ls_fieldcat_lvc-ref_field = pv_ref_field.
  ls_fieldcat_lvc-ref_table = pv_ref_table.
  ls_fieldcat_lvc-colddictxt = 'L'.
  ls_fieldcat_lvc-scrtext_l = pv_text.
  ls_fieldcat_lvc-edit = pv_edit.
  ls_fieldcat_lvc-no_zero = pv_nzero.
  ls_fieldcat_lvc-f4availabl = pv_f4.
  ls_fieldcat_lvc-no_out = pv_no_out.
  ls_fieldcat_lvc-outputlen = pv_outputlen.
  APPEND ls_fieldcat_lvc TO pt_fieldcat_lvc.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_OUTPUT
*&---------------------------------------------------------------------*
*&       text
*&---------------------------------------------------------------------*
FORM frm_output .
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program = sy-repid
      is_layout_lvc      = gs_layout
      it_fieldcat_lvc    = gt_fieldcat
    TABLES
      t_outtab           = gt_tab
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_F4
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM frm_f4 .

  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      mask             = '*.xls,*.xlsx.'
      mode             = '0'
    IMPORTING
      filename         = p_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_MODEL
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM frm_get_model USING p_objid TYPE wwwdata-objid.
  DATA: ls_objdata  TYPE wwwdatatab,
        lv_subrc    TYPE sy-subrc,
        lv_fullpath TYPE string,
        lv_fname    TYPE rlgrap-filename.

  PERFORM get_file_name CHANGING lv_fullpath.
  IF lv_fullpath = space.
    MESSAGE '请选择下载文件名' TYPE 'E'.

  ELSE.
    CONCATENATE lv_fullpath '' INTO lv_fname.
    SELECT SINGLE relid objid
      FROM wwwdata
      INTO CORRESPONDING FIELDS OF ls_objdata
      WHERE relid = 'MI'
        AND objid = p_objid .

    IF sy-subrc NE 0 OR ls_objdata-objid = space.
      MESSAGE e001(00) WITH '文件不存在!'.
    ELSE.
      CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
        EXPORTING
          key         = ls_objdata
          destination = lv_fname
        IMPORTING
          rc          = lv_subrc
        CHANGING
          temp        = lv_fname.
      IF lv_subrc NE 0.
        MESSAGE e001(00) WITH '文件下载失败!'.
      ENDIF.
    ENDIF.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  GET_FILE_NAME
*&---------------------------------------------------------------------*
*&       获取要下载的文件名字
*&---------------------------------------------------------------------*
FORM get_file_name CHANGING pv_fullpath.
  DATA: lv_path     TYPE string,
        lv_fullpath TYPE string.

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title         = '保存至'
      default_extension    = 'xlsx'
      default_file_name    = gv_filename
      file_filter          = 'Excel Files (*.xlsx)' "'.xls'
    CHANGING
      filename             = gv_filename
      path                 = lv_path
      fullpath             = lv_fullpath
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
  pv_fullpath = lv_fullpath.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_UPLOAD_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM frm_upload_data .
  DATA: lt_intern TYPE STANDARD TABLE OF alsmex_tabline,
        ls_excel  TYPE alsmex_tabline.
  FIELD-SYMBOLS:  <field> TYPE any.

  REFRESH lt_intern.

  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = p_file
      i_begin_col             = gv_begin_col
      i_begin_row             = gv_begin_row
      i_end_col               = gv_end_col
      i_end_row               = gv_end_row
    TABLES
      intern                  = lt_intern
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.

  LOOP AT lt_intern INTO ls_excel.
    CONDENSE ls_excel-value.

    ASSIGN COMPONENT ls_excel-col OF STRUCTURE gt_tab TO <field>.
    <field> = ls_excel-value.

    AT END OF row.
      CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
        EXPORTING
          input        = gt_tab-matnr
        IMPORTING
          output       = gt_tab-matnr
        EXCEPTIONS
          length_error = 1
          OTHERS       = 2.

      gt_tab-verid = |{ gt_tab-verid ALPHA = IN }|.

      IF gt_tab-plnnr IS INITIAL.
        SELECT SINGLE plnnr INTO gt_tab-plnnr FROM mapl
          WHERE matnr = gt_tab-matnr AND werks = gt_tab-werks
            AND plnty = 'N' AND loekz = ''.
      ENDIF.

      APPEND gt_tab.
      CLEAR gt_tab.
    ENDAT.
  ENDLOOP.

  SORT gt_tab BY werks matnr.

  IF gt_tab[] IS INITIAL.
    MESSAGE '没有导入数据' TYPE 'W'.
    STOP.
  ENDIF.

ENDFORM.
*----------------------------------------------------------------------*
* FORM bdc_dynpro                                       *
*----------------------------------------------------------------------*
FORM bdc_dynpro USING pv_program pv_dynpro.
  CLEAR gt_bdcdata.
  gt_bdcdata-program  = pv_program.
  gt_bdcdata-dynpro   = pv_dynpro.
  gt_bdcdata-dynbegin = 'X'.
  APPEND gt_bdcdata.
ENDFORM.                    "BDC_DYNPRO
*----------------------------------------------------------------------*
* FORM bdc_field                                                *
*----------------------------------------------------------------------*
FORM bdc_field USING pv_fnam pv_fval.
  CLEAR gt_bdcdata.
  gt_bdcdata-fnam = pv_fnam.
  gt_bdcdata-fval = pv_fval.
  APPEND gt_bdcdata.
ENDFORM.                    "BDC_FIELD

 

posted @ 2024-01-19 13:24  阿胖的阿多  阅读(274)  评论(0编辑  收藏  举报