代码demo-内部订单批量投料

为了简化用户操作,开发内部订单批量投料功能
代码demo-内部订单批量投料_第1张图片
用户可以批量上传,或者选择对应的物料,输入库位和内部订单号后进行过账操作
代码demo-内部订单批量投料_第2张图片
对用户选择的内部订单做校验,内部订单是否正确
在这里插入图片描述内部订单的公司是否和工厂对应的公司一致等等
在这里插入图片描述

下面展示ZPPA0011代码

REPORT ZPPA0011 MESSAGE-ID zpp..

INCLUDE ZPPA0011_TOP.
INCLUDE ZPPA0011_FRM.

INITIALIZATION.
  DATA but_down(255) TYPE c.
  CALL FUNCTION 'ICON_CREATE' " 给按钮添加图标和文本
    EXPORTING
      name   = icon_xls   " 按钮的图片的名字
      text   = '下载模板'                   "按钮的文本
*     info   = '请下载模板来批导入数据'
    IMPORTING
      result = but_down
    EXCEPTIONS
      OTHERS = 0.
  sscrfields-functxt_01 = but_down.


AT SELECTION-SCREEN.

  CASE sscrfields-ucomm.
    WHEN 'FC01'.
      PERFORM frm_down_template.
  ENDCASE.

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



START-OF-SELECTION.
  PERFORM frm_run."主程序

下面展示ZPPA0011_TOP代码

TABLES: sscrfields.
TABLES:mara,mseg.
TYPE-POOLS: icon, slis.


DATA : BEGIN OF itab_sdata OCCURS 0,
         werks TYPE string, "工厂
         lgort TYPE string, "库位
         aufnr TYPE string, "内部订单
         matnr TYPE string, "物料代码
         menge TYPE string, "数量
       END OF itab_sdata.

"ALV显示数据结构
DATA : BEGIN OF itab_data OCCURS 0,
         sel(1),"选择
         icon         TYPE icon_d,     "状态标识
         bukrs        TYPE coas-bukrs, "公司
         butxt        TYPE t001-butxt,
         werks        TYPE marc-werks, "工厂
         name1        TYPE name1,
         matnr        TYPE marc-matnr, "物料编码
         maktx        TYPE makt-maktx, "物料描述
         meins        TYPE stko-bmein, "单位
         labst        TYPE mard-labst, "库存量
         bdmng        TYPE afpo-wemng, "已投料量
         menge        TYPE stpo-menge, "本次录入数量
         lgort        TYPE stpo-lgort, "库位
         lgobe        TYPE goitem-lgobe, "库位描述
         budat        TYPE mkpf-budat, "过账日期
         xlokez       TYPE char1, "冲销标识
         type(1),
         message(100),"处理结果
         aufnr        TYPE afpo-aufnr, "内部订单
         ktext        TYPE coas-ktext, "内部订单描述
         field_style  TYPE lvc_t_styl, " 为内表添加设置编辑状态所需的字段
       END OF itab_data.

*-------- ALV 结构设置
DATA: wa_fieldcat TYPE  lvc_s_fcat,
      it_fieldcat TYPE  lvc_t_fcat,
      g_layout    TYPE slis_layout_alv,
      g_title     TYPE lvc_title,
      pos         TYPE i,
      l_field     TYPE slis_fieldcat_alv,   "字段列结构
      l_fieldcat  TYPE slis_t_fieldcat_alv, "字段列内表
      l_status_01 TYPE slis_formname VALUE 'L_STATUS_01'.


DATA: gt_events TYPE slis_t_event,
      gs_event  LIKE LINE OF gt_events.
DATA: gt_event_exit TYPE slis_t_event_exit WITH HEADER LINE.

DATA:lv_gjahr TYPE gjahr,
     lv_monat TYPE monat,
     lv_poper TYPE poper,
     lv_begda TYPE d,
     lv_endda TYPE d.

DEFINE add_col.
  CLEAR wa_fieldcat.
  ADD 1 TO pos.
  wa_fieldcat-col_pos = pos.
  wa_fieldcat-fieldname = &1.
  wa_fieldcat-ref_field = &2.
  wa_fieldcat-ref_table = &3.
  wa_fieldcat-scrtext_m = &4.
  wa_fieldcat-tabname = &5.
  wa_fieldcat-outputlen = &6.
  wa_fieldcat-f4availabl = &7.
  wa_fieldcat-hotspot = &8.
  wa_fieldcat-checkbox = &9.

  IF wa_fieldcat-fieldname = 'MATNR'.
     wa_fieldcat-edit_mask = '==ALPHA'."隐藏前导零
  ENDIF.

  IF wa_fieldcat-fieldname = 'MENGE'.
     wa_fieldcat-edit = 'X'.
     wa_fieldcat-emphasize = 'C510'.
  ENDIF.

  IF wa_fieldcat-fieldname = 'AUFNR'.
     wa_fieldcat-edit = 'X'.
     wa_fieldcat-emphasize = 'C510'.
  ENDIF.

  IF wa_fieldcat-fieldname = 'LGORT'.
*     wa_fieldcat-edit = 'X'.
     wa_fieldcat-outputlen = '6'.
  ENDIF.
  wa_fieldcat-scrtext_l = wa_fieldcat-scrtext_m.
  wa_fieldcat-scrtext_s = wa_fieldcat-scrtext_m.
  wa_fieldcat-coltext   = wa_fieldcat-scrtext_m.
  APPEND wa_fieldcat TO it_fieldcat.
END-OF-DEFINITION.


CLASS lcl_event_receiver DEFINITION DEFERRED.

DATA: go_events_receiver TYPE REF TO lcl_event_receiver.
*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.

  PUBLIC SECTION.
    METHODS:
      handle_data_changed
        FOR EVENT data_changed OF cl_gui_alv_grid
        IMPORTING er_data_changed.
ENDCLASS.                    "lcl_event_receiver DEFINITION
*---------------------------------------------------------
CLASS lcl_event_receiver IMPLEMENTATION.
  METHOD handle_data_changed.
    PERFORM handle_data_changed USING er_data_changed.
  ENDMETHOD.                    "HANDLE_BUTTON_CLICK
ENDCLASS .                    "lcl_event_receiver IMPLEMENTATION


TABLES afpo.

SELECTION-SCREEN:FUNCTION KEY 1.
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-020.
  PARAMETERS:p_werks  TYPE  marc-werks.
  PARAMETERS:p_budat  TYPE matdoc-budat DEFAULT sy-datum.
  PARAMETERS:p_file   TYPE rlgrap-filename MODIF ID m2. "上传路径
  PARAMETERS:p_aufnr  TYPE aufk-aufnr      MODIF ID m1.
  SELECT-OPTIONS: s_mtart FOR mara-mtart   MODIF ID m1.
  SELECT-OPTIONS: s_matnr FOR mara-matnr   MODIF ID m1.
  SELECT-OPTIONS: s_lgort FOR mseg-lgort   MODIF ID m1.
SELECTION-SCREEN:END OF BLOCK b1.

SELECTION-SCREEN: BEGIN OF BLOCK a1 WITH FRAME TITLE TEXT-010.
  SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN POSITION 5.
    PARAMETERS : r1 RADIOBUTTON GROUP grp DEFAULT 'X' USER-COMMAND sele.
    SELECTION-SCREEN COMMENT 10(12) TEXT-011 FOR FIELD r1.


    SELECTION-SCREEN POSITION 30.
    PARAMETERS : r2 RADIOBUTTON GROUP grp.
    SELECTION-SCREEN COMMENT 35(12) TEXT-012 FOR FIELD r2.

  SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN: END OF BLOCK a1.



AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN .
    "屏幕组
    CASE screen-group1.
      WHEN 'M1'.
        IF r1 EQ 'X' .
          screen-active = 0.                "设置屏幕隐藏
        ELSE .
          screen-active = 1.                "设置屏幕显示
        ENDIF .
      WHEN 'M2'.
        IF r1 EQ 'X' .
          screen-active = 1.                "设置屏幕隐藏
        ELSE .
          screen-active = 0.                "设置屏幕显示
        ENDIF .
    ENDCASE .
    MODIFY SCREEN .                        "修改选择屏幕属性
  ENDLOOP .

下面展示ZPPA0011_FRM代码

FORM frm_run .
  PERFORM frm_get_data.
  PERFORM frm_display_data."展示数据
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_get_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_data .
  REFRESH itab_data[].
  "获取日期
  PERFORM frm_get_date.

  IF r1 = 'X '.
    PERFORM frm_get_from_excel."从EXCEL数据中读取
  ELSE.
    PERFORM frm_get_from_database."读取有库存数据并展示
  ENDIF.

  IF itab_data[] IS INITIAL.
    MESSAGE '没有满足条件的记录' TYPE 'S'.
    STOP.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_display_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_display_data .
  REFRESH: it_fieldcat.
  CLEAR g_layout.

  DATA: x_layout TYPE lvc_s_layo.

*  g_layout-zebra       = 'X'.   "ALV行的颜色间隔更换(间隔色带)
  x_layout-stylefname  = 'FIELD_STYLE'. " 将内表中的字段名存入显示格式
  x_layout-zebra = 'X'.   "ALV行的颜色间隔更换(间隔色带)

  add_col 'SEL'        space space '选择'                   'ITAB_DATA' '3' space 'X'   'X'.
  add_col 'ICON'       space space '状态'                   'ITAB_DATA' '4' space space space.
  add_col 'NAME1'      'NAME1'  'GOITEM'  '工厂名称'        'ITAB_DATA' '15' space space space.
  add_col 'MAKTX'      space space '物料描述'               'ITAB_DATA' '20' space space space.
  add_col 'BDMNG'     space space '本月已投料数量'          'ITAB_DATA' '8' space space space.
  add_col 'MEINS'      space space '单位'                   'ITAB_DATA' '4' space space space.
  add_col 'LABST'      space space '当前库存'               'ITAB_DATA' '8' space space space.
  add_col 'MENGE'      'MENGE'  'GOITEM'    '领用数量'      'ITAB_DATA' '10' space space space.
  add_col 'LGORT'      'LGORT'  'GOITEM'   '库位代码'       'ITAB_DATA' '4' space space space.
  add_col 'LGOBE'      'LGOBE'  'GOITEM'   '库位描述'       'ITAB_DATA' '10' space space space.
  add_col 'AUFNR'      'AUFNR'  'AUFK'     '内部订单'       'ITAB_DATA' '12' space space space.
  add_col 'KTEXT'      space space '内部订单描述'           'ITAB_DATA' '20' space space space.
  add_col 'XLOKEZ'     space space '冲销'                   'ITAB_DATA' '3' space 'X'   'X'.
  add_col 'MESSAGE'    space space '处理消息'               'ITAB_DATA' '40' space space space.
  add_col 'WERKS'      'WERKS'  'GOITEM'  '工厂代码'        'ITAB_DATA' '4' space space space.
  add_col 'BUKRS'      space space '公司代码'               'ITAB_DATA' '4' space space space.
  add_col 'BUTXT'      space space '公司名称'               'ITAB_DATA' '14' space space space.
  add_col 'MATNR'      space space '物料代码'               'ITAB_DATA' '10' space space space.


  gs_event-name = 'CALLER_EXIT'.
  gs_event-form = 'FRM_CELL_CHANGE'.
  APPEND gs_event TO gt_events.CLEAR gs_event.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'L_STATUS100'
      i_callback_user_command  = 'FRM_USER_COMMAND'
      is_layout_lvc            = x_layout
      it_fieldcat_lvc          = it_fieldcat
      i_save                   = 'A'
      it_events                = gt_events              "注册事件
    TABLES
      t_outtab                 = itab_data
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
  IF sy-subrc NE 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.


ENDFORM.

FORM l_status100 USING extab TYPE slis_t_extab.
  SET  PF-STATUS  'MAIN'.
ENDFORM.                    "L_STATUS100


*&---------------------------------------------------------------------*
*&      Form  frm_user_command
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->R_UCOMM      text
*      -->RS_SELFIELD  text
*----------------------------------------------------------------------*
FORM frm_user_command USING r_ucomm LIKE sy-ucomm rs_selfield TYPE slis_selfield.
  DATA: ref TYPE REF TO cl_gui_alv_grid.
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = ref.
  CALL METHOD ref->check_changed_data.
  CASE r_ucomm.
    WHEN '&IC1'.
      IF rs_selfield-fieldname = 'SEL'.
        READ TABLE itab_data INDEX rs_selfield-tabindex.
        IF itab_data-sel = ''.
          itab_data-sel = 'X'.
        ELSE.
          itab_data-sel = ''.
        ENDIF.
        MODIFY itab_data INDEX rs_selfield-tabindex.
        CLEAR itab_data.
      ENDIF.
      IF rs_selfield-fieldname = 'XLOKEZ'.
        READ TABLE itab_data INDEX rs_selfield-tabindex.
        IF itab_data-xlokez = ''.
          itab_data-xlokez = 'X'.
        ELSE.
          itab_data-xlokez = ''.
        ENDIF.
        MODIFY itab_data INDEX rs_selfield-tabindex.
        CLEAR itab_data.
      ENDIF.


    WHEN 'ALL'.
      itab_data-sel = 'X'.
      MODIFY itab_data FROM itab_data  TRANSPORTING sel   WHERE sel = '' .
    WHEN 'SAL'.
      itab_data-sel = ''.
      MODIFY itab_data FROM itab_data  TRANSPORTING sel   WHERE sel = 'X' .
    WHEN '&IMP'.
      PERFORM frm_excute.
    WHEN '&QUERY'.
      PERFORM frm_query.
    WHEN '&REF'.
      PERFORM frm_get_data.
    WHEN '&CHG'.
      PERFORM frm_change_date.
  ENDCASE.

  PERFORM frm_re_alv.
ENDFORM.                    "FRM_USER_COMMAND
*&---------------------------------------------------------------------*
*& Form frm_excute
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_excute .

  DATA:ls_goodsmvt_head TYPE bapi2017_gm_head_01.
  DATA:lt_goodsmvt_item    TYPE TABLE OF bapi2017_gm_item_create,
       ls_goodsmvt_item    TYPE bapi2017_gm_item_create,
       lt_extensionin      TYPE TABLE OF bapiparex,
       ls_extensionin      TYPE bapiparex,
       lv_materialdocument TYPE bapi2017_gm_head_ret-mat_doc,
       lv_matdocumentyear  TYPE bapi2017_gm_head_ret-doc_year,
       lv_matzeile         TYPE mseg-zeile,
       lv_goodsmvt_code    TYPE bapi2017_gm_code.
  DATA:lt_return TYPE TABLE OF bapiret2,
       ls_return LIKE LINE OF lt_return.


  DATA ls_xmseg  TYPE bapi_te_xmseg.
  DATA lv_index TYPE sy-tabix.

  LOOP AT itab_data WHERE sel = 'X' AND type NE 'S'.
    IF itab_data-menge =< 0.
      itab_data-icon = icon_red_light.
      itab_data-type = 'E'.
      itab_data-message = '请填写数量'.
      MODIFY itab_data.
      CLEAR itab_data.
      CONTINUE.
    ENDIF.

    IF itab_data-lgort IS INITIAL.
      itab_data-icon = icon_red_light.
      itab_data-type = 'E'.
      itab_data-message = '请填写库位'.
      MODIFY itab_data.
      CLEAR itab_data.
      CONTINUE.
    ENDIF.

    CLEAR lv_index.
    lv_index = lv_index + 1.
    CLEAR:ls_goodsmvt_item, ls_goodsmvt_head,ls_return,ls_extensionin.
    REFRESH:lt_return,lt_goodsmvt_item,lt_extensionin.
    "赋值逻辑
    ls_goodsmvt_head-pstng_date = p_budat."记账日期
    ls_goodsmvt_head-doc_date   = sy-datum."凭证日期
    lv_goodsmvt_code = '03'.
    ls_goodsmvt_item-material   = itab_data-matnr."物料代码
    ls_goodsmvt_item-plant      = itab_data-werks."工厂代码
    ls_goodsmvt_item-stge_loc   = itab_data-lgort."库位
    ls_goodsmvt_item-orderid    = itab_data-aufnr.
    ls_goodsmvt_item-entry_qnt  = itab_data-menge."数量
    "冲销处理逻辑
    IF itab_data-xlokez IS NOT INITIAL .
      ls_goodsmvt_item-move_type = '262'.
    ELSE.
      ls_goodsmvt_item-move_type  = '261'.
    ENDIF.
    APPEND ls_goodsmvt_item TO lt_goodsmvt_item.
    CLEAR: ls_goodsmvt_item.

    CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
      EXPORTING
        goodsmvt_header  = ls_goodsmvt_head
        goodsmvt_code    = lv_goodsmvt_code
      IMPORTING
        materialdocument = lv_materialdocument
        matdocumentyear  = lv_matdocumentyear
      TABLES
        goodsmvt_item    = lt_goodsmvt_item
        extensionin      = lt_extensionin
        return           = lt_return.
    READ TABLE lt_return INTO ls_return INDEX 1.
    IF ls_return-type = 'E' OR ls_return-type = 'A'.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      itab_data-type = 'E'.
      itab_data-icon = icon_red_light.
      itab_data-message = |处理失败:|.
      LOOP AT lt_return INTO ls_return WHERE type CA 'EAX'.
        itab_data-message = |{ itab_data-message },{ ls_return-message }|.
      ENDLOOP.
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
      itab_data-type = 'S'.
      itab_data-icon = icon_green_light.
      itab_data-message = |录入成功,生成凭证{ lv_materialdocument }|.

      SELECT SUM( CASE bwart
        WHEN '262' THEN menge * -1
        ELSE menge
        END
        ) AS bdmng
       INTO @itab_data-bdmng
       FROM nsdm_e_mseg
        WHERE bwart IN ('261','262')
          AND werks = @itab_data-werks
          AND lgort = @itab_data-lgort
          AND matnr = @itab_data-matnr
          AND aufnr = @itab_data-aufnr
          AND budat_mkpf BETWEEN @lv_begda AND @lv_endda.

      "更新库存
      SELECT SINGLE labst
        FROM mard
        INTO @itab_data-labst
        WHERE matnr = @itab_data-matnr
          AND werks = @itab_data-werks
          AND lgort = @itab_data-lgort.
      MODIFY itab_data TRANSPORTING labst WHERE matnr = itab_data-matnr AND werks = itab_data-werks AND lgort = itab_data-lgort.
    ENDIF.
    MODIFY itab_data.
    CLEAR  itab_data.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_re_alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_re_alv .
  DATA:lv_grid   TYPE REF TO cl_gui_alv_grid,
       is_stable TYPE lvc_s_stbl,
       ls_layout TYPE lvc_s_layo.
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lv_grid.
* 获取ALV变化过后的布局参数
  CALL METHOD lv_grid->get_frontend_layout
    IMPORTING
      es_layout = ls_layout.

* 重新回写自适应宽度
  ls_layout-zebra      = 'X'.
*  ls_layout-cwidth_opt = 'X'.


* 回写ALV变化过后的布局参数
  CALL METHOD lv_grid->set_frontend_layout
    EXPORTING
      is_layout = ls_layout.
* 刷新ALV布局
  is_stable-col = 'X'.
*  is_stable-row = 'X'.
  CALL METHOD lv_grid->refresh_table_display
    EXPORTING
      is_stable = is_stable.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_query
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_query .
  DATA: lv_va TYPE slis_vari.   " Tcode MB51对应程序变式,将变式清空
  FIELD-SYMBOLS:  TYPE ANY TABLE,
                      TYPE ANY TABLE,
                 .
  DATA lr_pay_data TYPE REF TO data.
  RANGES:s_bwart FOR resb-bwart,
         s_werks FOR resb-werks,
         s_budat FOR mseg-budat_mkpf,
         s_aufnr FOR resb-aufnr,
         s_lgort FOR resb-lgort,
         s_matnr FOR resb-matnr.

  lv_va = ''.
  "工厂
  s_werks-low = p_werks.
  s_werks-option = 'EQ'.
  s_werks-sign   = 'I'.
  APPEND s_werks.
  CLEAR  s_bwart.

  s_bwart-low = '261'.
  s_bwart-option = 'EQ'.
  s_bwart-sign   = 'I'.
  APPEND s_bwart.
  CLEAR  s_bwart.

  s_bwart-low = '262'.
  s_bwart-option = 'EQ'.
  s_bwart-sign   = 'I'.
  APPEND s_bwart.
  CLEAR  s_bwart.

  s_budat-low  = lv_begda.
  s_budat-high = lv_endda.
  s_budat-option = 'BT'.
  s_budat-sign   = 'I'.
  APPEND s_budat.
  CLEAR s_budat.

  LOOP AT itab_data WHERE sel = 'X'.
    s_matnr-low = itab_data-matnr.
    s_matnr-option = 'EQ'.
    s_matnr-sign   = 'I'.
    APPEND s_matnr.
    CLEAR  s_matnr.


    IF itab_data-aufnr IS NOT INITIAL.
      s_aufnr-low    = itab_data-aufnr.
      s_aufnr-option = 'EQ'.
      s_aufnr-sign   = 'I'.
      APPEND s_aufnr.
      CLEAR s_aufnr.
    ENDIF.

    IF itab_data-lgort IS NOT INITIAL.
      s_lgort-low    = itab_data-lgort.
      s_lgort-option = 'EQ'.
      s_lgort-sign   = 'I'.
      APPEND s_lgort.
      CLEAR s_lgort.
    ENDIF.
    CLEAR itab_data.
  ENDLOOP.

  cl_salv_bs_runtime_info=>set(
       EXPORTING
         display  = abap_true
         metadata = abap_false
         data     = abap_true ).

  SUBMIT rm07docs                " Tcode MB51对应程序
    WITH matnr IN s_matnr   "物料
    WITH werks IN s_werks   "工厂
    WITH bwart IN s_bwart"移动类型
    WITH budat IN s_budat "过账日期
    WITH lgort IN s_lgort
    WITH aufnr IN s_aufnr
    WITH rhier_l  = ''
    WITH rflat_l  = abap_true"扁平结构
    WITH database = abap_true "读取数据库
    WITH alv_def  = lv_va"变式清空,否则会影响取数内容
    AND RETURN.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  CALLER_EXIT
*&---------------------------------------------------------------------*
*& ALV单元格事件
*&---------------------------------------------------------------------*
*&      -->E_GRID     text
*&---------------------------------------------------------------------*
FORM frm_cell_change USING e_grid TYPE slis_data_caller_exit.
  DATA:lr_grid TYPE REF TO cl_gui_alv_grid.
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lr_grid.
  CREATE OBJECT go_events_receiver.
  SET HANDLER go_events_receiver->handle_data_changed FOR lr_grid.
  lr_grid->register_edit_event( cl_gui_alv_grid=>mc_evt_enter ).
  lr_grid->register_edit_event( cl_gui_alv_grid=>mc_evt_modified ).
ENDFORM.                    "CALLER_EXIT

*&---------------------------------------------------------------------*
*& Form handle_data_changed
*&---------------------------------------------------------------------*
*& ALV单元格数据修改事件
*&---------------------------------------------------------------------*
*&      --> ER_DATA_CHANGED
*&      --> SENDER
*&---------------------------------------------------------------------*
FORM handle_data_changed  USING er_data_changed TYPE REF TO cl_alv_changed_data_protocol.

  DATA:lv_msg       TYPE char255,
       lv_condition TYPE char10.

  DATA:ls_good_cells TYPE lvc_s_modi.
  FIELD-SYMBOLS:  TYPE any .
  DATA:lr_grid TYPE REF TO cl_gui_alv_grid.
  DATA:ls_stable TYPE lvc_s_stbl .


  ls_stable-col = 'X'.
  ls_stable-row = 'X'.
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lr_grid.

  LOOP AT er_data_changed->mt_good_cells INTO ls_good_cells .
    READ TABLE itab_data ASSIGNING FIELD-SYMBOL() INDEX ls_good_cells-row_id.
    IF sy-subrc = 0.
      ASSIGN COMPONENT ls_good_cells-fieldname OF STRUCTURE  TO .
       = ls_good_cells-value.
      IF ls_good_cells-fieldname = 'LGORT'.
        SELECT SINGLE lgobe
          FROM t001l
          INTO @-lgobe
          WHERE werks = @-werks
            AND lgort = @-lgort.

        IF -lgort IS INITIAL.
          SELECT SUM( labst )
            FROM mard
            INTO @-labst
            WHERE matnr = @-matnr
              AND werks = @-werks.
        ELSE.
          SELECT SINGLE labst
            INTO @-labst
            FROM mard
            WHERE werks = @-werks
              AND matnr = @-matnr
              AND lgort = @-lgort.
        ENDIF.

        IF sy-subrc <> 0.
          CLEAR -labst.
        ENDIF.
      ELSEIF ls_good_cells-fieldname = 'AUFNR'."当更改内部订单号时,进行校验
        IF -aufnr IS NOT INITIAL.
          "判断内部订单公司与当前工厂公司是否一致
          SELECT SINGLE ktext,bukrs,autyp
            FROM coas
            INTO @DATA(lw_coas)
            WHERE aufnr = @-aufnr
              AND autyp = '01'.
          IF lw_coas IS INITIAL.
            -icon = icon_red_light.
            -message = '输入的订单号不是内部订单,请确认'.
          ELSEIF lw_coas IS NOT INITIAL AND lw_coas-bukrs <> -bukrs.
            -icon = icon_red_light.
            -message = '所选内部订单的公司代码与工厂所属公司代码不一致'.
          ELSE.
            "内部订单描述
            -ktext = lw_coas-ktext.
            "获取已投料信息  订单号+ 物料 + 261 -262
            SELECT SUM( CASE bwart
              WHEN '262' THEN menge * -1
              ELSE menge
              END
              ) AS bdmng
             INTO @-bdmng
             FROM nsdm_e_mseg
              WHERE bwart IN ('261','262')
                AND werks = @-werks
                AND lgort = @-lgort
                AND matnr = @-matnr
                AND aufnr = @-aufnr
                AND budat_mkpf BETWEEN @lv_begda AND @lv_endda.
            CLEAR:-icon,-message.
          ENDIF.
        ELSE.
          CLEAR:-icon,-message.
        ENDIF.

      ENDIF.
    ENDIF.
  ENDLOOP.

  CALL METHOD lr_grid->refresh_table_display
    EXPORTING
      is_stable      = ls_stable
      i_soft_refresh = 'X'
    EXCEPTIONS
      finished       = 1.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_auth_chk
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_auth_chk.
  AUTHORITY-CHECK OBJECT 'M_BEST_WRK'
    ID 'WERKS' FIELD p_werks
    ID 'ACTVT' DUMMY.

  IF sy-subrc <> 0.
    MESSAGE s000 WITH '没有工厂' p_werks '的操作权限' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.
ENDFORM.
FORM frm_call_sm30.

  DATA:gs_vimsellist LIKE vimsellist,
       gt_vimsellist LIKE TABLE OF gs_vimsellist.

  gs_vimsellist-viewfield  = 'WERKS'.
  gs_vimsellist-operator   = 'EQ'.
  gs_vimsellist-value      = p_werks.
  APPEND gs_vimsellist TO gt_vimsellist.


  CALL FUNCTION 'VIEW_MAINTENANCE_CALL'
    EXPORTING
      action                       = 'U'
      view_name                    = 'ZPPA0010_A'
    TABLES
      dba_sellist                  = gt_vimsellist
    EXCEPTIONS
      client_reference             = 1
      foreign_lock                 = 2
      invalid_action               = 3
      no_clientindependent_auth    = 4
      no_database_function         = 5
      no_editor_function           = 6
      no_show_auth                 = 7
      no_tvdir_entry               = 8
      no_upd_auth                  = 9
      only_show_allowed            = 10
      system_failure               = 11
      unknown_field_in_dba_sellist = 12
      view_not_found               = 13
      maintenance_prohibited       = 14
      OTHERS                       = 15.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_get_date
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_date .
  CALL FUNCTION 'FI_PERIOD_DETERMINE'
    EXPORTING
      i_budat = p_budat
      i_bukrs = '3000'
    IMPORTING
      e_gjahr = lv_gjahr
      e_monat = lv_monat.

  lv_poper = lv_monat.

  CALL FUNCTION 'FIRST_DAY_IN_PERIOD_GET'
    EXPORTING
      i_gjahr        = lv_gjahr
      i_periv        = 'Y4'
      i_poper        = lv_poper
    IMPORTING
      e_date         = lv_begda
    EXCEPTIONS
      input_false    = 1
      t009_notfound  = 2
      t009b_notfound = 3
      OTHERS         = 4.

  CALL FUNCTION 'LAST_DAY_IN_PERIOD_GET'
    EXPORTING
      i_gjahr        = lv_gjahr
      i_periv        = 'Y4'
      i_poper        = lv_poper
    IMPORTING
      e_date         = lv_endda
    EXCEPTIONS
      input_false    = 1
      t009_notfound  = 2
      t009b_notfound = 3
      OTHERS         = 4.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_change_date
*&---------------------------------------------------------------------*
*& 更改过账日期
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_change_date .
  "弹出可以填写字段的函数方法
  DATA : lt_sval TYPE  TABLE  OF  sval,
         ls_sval LIKE  LINE  OF  lt_sval.
  DATA:l_returncode TYPE string .

  "过账日期
  CLEAR ls_sval .
  ls_sval-tabname = 'BKPF' .  "表名
  ls_sval-fieldname = 'BUDAT' . "字段名
  ls_sval-field_obl = 'X' .          "是否为必填
  APPEND ls_sval TO lt_sval.


  CALL FUNCTION 'POPUP_GET_VALUES'
    EXPORTING
*     NO_VALUE_CHECK  = ' '
      popup_title     = '请填写'
*     START_COLUMN    = '5'
*     START_ROW       = '5'
    IMPORTING
      returncode      = l_returncode
    TABLES
      fields          = lt_sval
    EXCEPTIONS
      error_in_fields = 1
      OTHERS          = 2.

  LOOP AT lt_sval INTO DATA(lw_sval) WHERE tabname = 'BKPF' AND fieldname = 'BUDAT'.
    p_budat = lw_sval-value.
    CLEAR lw_sval.
    EXIT.
  ENDLOOP.
  REFRESH lt_sval.



  PERFORM frm_get_date."重新获取日期

  "更新投料数据
  LOOP AT itab_data.
    SELECT SUM( CASE bwart
      WHEN '262' THEN menge * -1
      ELSE menge
      END
      ) AS bdmng
     INTO @itab_data-bdmng
     FROM nsdm_e_mseg
      WHERE bwart IN ('261','262')
        AND werks = @itab_data-werks
        AND lgort = @itab_data-lgort
        AND matnr = @itab_data-matnr
        AND aufnr = @itab_data-aufnr
        AND budat_mkpf BETWEEN @lv_begda AND @lv_endda.
    MODIFY itab_data.
    CLEAR itab_data.
  ENDLOOP.

ENDFORM.



*&---------------------------------------------------------------------*
*& Form FRM_DOWN_TEMPLATE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_down_template .
  DATA: lo_objdata LIKE wwwdatatab,
        p_name     TYPE string,
        p_path     TYPE string,
        p_fullname TYPE string,
        p_dest     TYPE rlgrap-filename,
        li_rc      TYPE sy-subrc,
        lv_objid   TYPE w3objid,
        ls_str     TYPE string.

  lv_objid = 'ZPPA0011'.
  ls_str = 'ZPPA0011_内部订单消耗领料登记模板.xlsx'.

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title      = '选择下载路径'
      default_extension = 'XLSX'
      default_file_name = ls_str
    CHANGING
      filename          = p_name
      path              = p_path
      fullpath          = p_fullname.

  IF p_fullname IS INITIAL.
    STOP.
  ENDIF.

  SELECT SINGLE relid objid FROM wwwdata
       INTO CORRESPONDING FIELDS OF  lo_objdata
       WHERE srtf2 = 0
         AND relid = 'MI'
         AND objid = lv_objid.
  IF sy-subrc <> 0.
    MESSAGE '下载模板失败!' TYPE 'S' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.

  p_dest = p_fullname.
  CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
    EXPORTING
      key         = lo_objdata
      destination = p_dest
    IMPORTING
      rc          = li_rc.
  IF li_rc = 0.
    MESSAGE '模版下载成功' TYPE 'S'.
  ELSE.
    MESSAGE '模版下载失败' TYPE 'S' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.
ENDFORM.


*&---------------------------------------------------------------------*
*& Form FRM_GET_FILE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM frm_get_file  USING  p_file.
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      def_filename     = p_file
      mask             = ',*.xlsx,*.XLSX,*.XLS,*.xls,*.txt,*.TXT.'
*'',*.xls,*.XLS.'如果读入txt文件,在后边就需要用函数
      mode             = 'O'
      title            = 'File Name'
    IMPORTING
      filename         = p_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_get_from_excel
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_from_excel .
  TYPE-POOLS: truxs.
  DATA: p_text_data TYPE truxs_t_text_data,
        pp_file     TYPE string,
        i_file      TYPE ibipparms-path.


  pp_file = p_file.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = pp_file
      filetype                = 'ASC'
      has_field_separator     = 'X'
      read_by_line            = 'X'
    TABLES
      data_tab                = p_text_data
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      OTHERS                  = 17.
  IF sy-subrc <> 0.
    CASE sy-subrc.
      WHEN 1.MESSAGE 'FILE_OPEN_ERROR ' TYPE 'E'.
      WHEN 2.MESSAGE 'FILE_READ_ERROR ' TYPE 'E'.
      WHEN 3.MESSAGE 'NO_BATCH' TYPE 'E'.
      WHEN 4.MESSAGE 'GUI_REFUSE_FILETRANSFER  ' TYPE 'E'.
      WHEN 5.MESSAGE 'INVALID_TYPE ' TYPE 'E'.
      WHEN 6.MESSAGE ' NO_AUTHORITY' TYPE 'E'.
      WHEN 7.MESSAGE 'UNKNOWN_ERROR' TYPE 'E'.
      WHEN 8.MESSAGE 'BAD_DATA_FORMAT ' TYPE 'E'.
      WHEN 9.MESSAGE 'HEADER_NOT_ALLOWED' TYPE 'E'.
      WHEN 10.MESSAGE 'SEPARATOR_NOT_ALLOWED' TYPE 'E'.
      WHEN 11.MESSAGE 'HEADER_TOO_LONG ' TYPE 'E'.
      WHEN 12.MESSAGE 'UNKNOWN_DP_ERROR' TYPE 'E'.
      WHEN 13.MESSAGE ' ACCESS_DENIED ' TYPE 'E'.
      WHEN 14.MESSAGE 'DP_OUT_OF_MEMORY ' TYPE 'E'.
      WHEN 15.MESSAGE 'DISK_FULL  ' TYPE 'E'.
      WHEN 16.MESSAGE 'DP_TIMEOUT' TYPE 'E'.
      WHEN 17.MESSAGE ' OTHERS  ' TYPE 'E'.
      WHEN OTHERS.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDCASE.
  ENDIF.

  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
*     I_FIELD_SEPERATOR    =
*     I_LINE_HEADER        =
      i_tab_raw_data       = p_text_data
      i_filename           = p_file
    TABLES
      i_tab_converted_data = itab_sdata
    EXCEPTIONS
      conversion_failed    = 1
      OTHERS               = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
  DELETE itab_sdata INDEX 1.
*
  LOOP AT itab_sdata.
    MOVE-CORRESPONDING itab_sdata TO itab_data.
    "物料代码补零
    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        input        = itab_data-matnr
      IMPORTING
        output       = itab_data-matnr
      EXCEPTIONS
        length_error = 1
        OTHERS       = 2.
    IF sy-subrc <> 0.
*       Implement suitable error handling here
    ENDIF.
    "物料描述
    SELECT SINGLE maktx
      FROM makt
      INTO @itab_data-maktx
      WHERE matnr = @itab_data-matnr.

    "获取内部订单描述、公司代码、工厂代码
    SELECT SINGLE ktext,bukrs
      FROM coas
      INTO ( @itab_data-ktext, @itab_data-bukrs )
      WHERE aufnr = @itab_data-aufnr.

    "判断内部订单的公司与库存工厂对公司是否一致
    SELECT SINGLE t001k~bukrs,t001~butxt,t001w~name1
      FROM t001 INNER JOIN t001k ON t001~bukrs = t001k~bukrs
                INNER JOIN t001w ON t001w~bwkey = t001k~bwkey
      INTO @DATA(lw_t001)
      WHERE t001w~werks = @itab_data-werks.

    IF lw_t001-bukrs <> itab_data-bukrs.
      itab_data-icon = icon_red_light.
      itab_data-type = 'E'.
      itab_data-message = '内部订单对应公司与库存工厂所属公司不一致'.
    ELSE.
      itab_data-name1 = lw_t001-name1.
      itab_data-butxt = lw_t001-butxt.
    ENDIF.

    SELECT SUM( CASE bwart
      WHEN '262' THEN menge * -1
      ELSE menge
      END
      ) AS bdmng
     INTO @itab_data-bdmng
     FROM nsdm_e_mseg
      WHERE bwart IN ('261','262')
        AND werks = @itab_data-werks
        AND lgort = @itab_data-lgort
        AND matnr = @itab_data-matnr
        AND aufnr = @itab_data-aufnr
        AND budat_mkpf BETWEEN @lv_begda AND @lv_endda.

    "更新库存
    SELECT SINGLE labst
      FROM mard
      INTO @itab_data-labst
      WHERE matnr = @itab_data-matnr
        AND werks = @itab_data-werks
        AND lgort = @itab_data-lgort.

    itab_data-sel  = 'X'.
    itab_data-icon = icon_light_out.
    APPEND itab_data.
    CLEAR: itab_sdata,itab_data .
  ENDLOOP.
  REFRESH itab_sdata[].
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_get_from_mb52
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_from_database .
  DATA: lv_va TYPE slis_vari.   " Tcode MB51对应程序变式,将变式清空
  FIELD-SYMBOLS:  TYPE ANY TABLE,
                 .
  DATA lr_pay_data  TYPE REF TO data.

  IF p_aufnr IS NOT INITIAL.
    "获取内部订单描述、公司代码、工厂代码
    SELECT SINGLE ktext,bukrs
      FROM coas
      INTO @DATA(lw_coas)
      WHERE aufnr = @p_aufnr
        AND autyp = '01'.
    IF lw_coas IS INITIAL.
      MESSAGE '输入的订单号不是内部订单号!' TYPE 'I'.
      EXIT.
    ELSE.
      "判断内部订单的公司与库存工厂对公司是否一致
      SELECT SINGLE t001k~bukrs,t001~butxt,t001w~name1
        FROM t001 INNER JOIN t001k ON t001~bukrs = t001k~bukrs
                  INNER JOIN t001w ON t001w~bwkey = t001k~bwkey
        INTO @DATA(lw_t001)
        WHERE t001w~werks = @p_werks.
    ENDIF.
  ELSE.
      "判断内部订单的公司与库存工厂对公司是否一致
      SELECT SINGLE t001k~bukrs,t001~butxt,t001w~name1
        FROM t001 INNER JOIN t001k ON t001~bukrs = t001k~bukrs
                  INNER JOIN t001w ON t001w~bwkey = t001k~bwkey
        INTO @lw_t001
        WHERE t001w~werks = @p_werks.
  ENDIF.


  lv_va = '/YT01'.

  RANGES:s_werks FOR resb-werks.

  "工厂
  s_werks-low = p_werks.
  s_werks-option = 'EQ'.
  s_werks-sign   = 'I'.
  APPEND s_werks.

  REFRESH:itab_data.

  "设置执行时不显示,只获取ALV数据
  cl_salv_bs_runtime_info=>set(
      display = abap_false
      metadata = abap_false
      data  = abap_true ).
  "MB52的程序
  SUBMIT rm07mlbs
    WITH matnr IN s_matnr
    WITH werks IN s_werks
    WITH lgort IN s_lgort
    WITH negativ = ''
    WITH pa_sond = 'X'
    WITH xmchb = 'X'
    WITH nozero = 'X'"0库存标识
    WITH novalues = ''
    WITH pa_hsq = ''
    WITH pa_flt = 'X'
    WITH p_vari = lv_va
      AND RETURN.

  TRY ."可以获取MB52
      cl_salv_bs_runtime_info=>get_data_ref(
        IMPORTING r_data = lr_pay_data ).

      ASSIGN lr_pay_data->* TO .

    CATCH  cx_salv_bs_sc_runtime_info.
      MESSAGE '无法获取ALV数据'  TYPE  'E'.
  ENDTRY.
  cl_salv_bs_runtime_info=>clear_all( ).

  IF  IS ASSIGNED.
    LOOP AT  ASSIGNING .
      MOVE-CORRESPONDING  TO itab_data.
      IF lw_coas IS NOT INITIAL.
        itab_data-aufnr = p_aufnr.
        itab_data-ktext = lw_coas-ktext.
      ENDIF.
      itab_data-bukrs = lw_t001-bukrs.
      itab_data-butxt = lw_t001-butxt.
      APPEND itab_data.
      CLEAR:  itab_data.
    ENDLOOP.
    CLEAR: lw_coas,lw_t001.
  ENDIF.
ENDFORM.

你可能感兴趣的:(ERP-开发总结,ABAP,内部订单投料,代码demo)