【ABAP】常用模板二

*&------------------------------------------------------------*
*&程序名:YH_DEMO2                                                *
*&程序描述:                                                  *
*&创建人:                                                    *
*&创建时间:                                                  *
*&------------------------------------------------------------*
REPORT yh_demo2 MESSAGE-ID id_ficn.
TABLES:t001k,t001w,mara,mbew,mkpf,mseg,bsis.

CONSTANTS:c_max_date TYPE i VALUE '9998'.
DATA:gr_datum1 TYPE RANGE OF datum WITH HEADER LINE,
     gr_datum2 TYPE RANGE OF datum WITH HEADER LINE,
     gr_datum3 TYPE RANGE OF datum WITH HEADER LINE,
     gr_datum4 TYPE RANGE OF datum WITH HEADER LINE,
     gr_datum5 TYPE RANGE OF datum WITH HEADER LINE,
     gr_datum6 TYPE RANGE OF datum WITH HEADER LINE,
     gr_datum7 TYPE RANGE OF datum WITH HEADER LINE,
     gr_datum8 TYPE RANGE OF datum WITH HEADER LINE.
DATA:gv_lastday TYPE datum.

TYPES:BEGIN OF ts_pdate,
        index  TYPE i,
        pdates TYPE p LENGTH 7 DECIMALS 0,
        punit  TYPE timeunit,
      END OF ts_pdate.

TYPES:BEGIN OF ts_pdate_intvl,
        num  TYPE p LENGTH 7 DECIMALS 0,
        unit TYPE timeunit,
      END OF ts_pdate_intvl.

DATA:BEGIN OF gs_date_intvl,
       num TYPE p LENGTH 4,
     END OF gs_date_intvl.
DATA gt_date_intvl LIKE TABLE OF gs_date_intvl.

DATA:BEGIN OF gs_date_intvl_new,
       num  TYPE p LENGTH 7 DECIMALS 0,
       unit TYPE timeunit,
     END OF gs_date_intvl_new.
DATA gt_date_intvl2 LIKE TABLE OF gs_date_intvl_new.
DATA gt_date_intvl_new LIKE TABLE OF gs_date_intvl_new.


TYPES:tt_pdate TYPE TABLE OF ts_pdate.
DATA gt_pdates TYPE tt_pdate.
DATA gt_pdates_new TYPE tt_pdate.


SELECTION-SCREEN BEGIN OF BLOCK blk01 WITH FRAME TITLE TEXT-bl1.
SELECT-OPTIONS:s_bukrs FOR t001k-bukrs,
                          s_hkont FOR bsis-hkont.
PARAMETERS:s_kdate LIKE sy-datum DEFAULT sy-datum OBLIGATORY.
SELECTION-SCREEN END OF BLOCK blk01.



SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE TEXT-002.
SELECTION-SCREEN BEGIN OF BLOCK b4.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 01(30) TEXT-003 FOR FIELD p_date_1 MODIF ID new.
SELECTION-SCREEN POSITION POS_LOW.
PARAMETERS:p_date_1 TYPE idcn_dec3 DEFAULT 10 VISIBLE LENGTH 4 MODIF ID new.
PARAMETERS:p_unit1 TYPE idcn_timeunit DEFAULT '1' AS LISTBOX VISIBLE LENGTH 6 OBLIGATORY MODIF ID new USER-COMMAND m1.
PARAMETERS:p_date_2 TYPE idcn_dec3 DEFAULT 20  MODIF ID new. "like rfpdo1-allgrogr default '020'.
PARAMETERS:p_unit2 TYPE idcn_timeunit DEFAULT '1' AS LISTBOX VISIBLE LENGTH 6 OBLIGATORY MODIF ID new USER-COMMAND m2.
PARAMETERS:p_date_3 TYPE idcn_dec3 DEFAULT 30  MODIF ID new. "like rfpdo1-allgrogr default '020'.
PARAMETERS:p_unit3 TYPE idcn_timeunit DEFAULT '1' AS LISTBOX VISIBLE LENGTH 6 OBLIGATORY MODIF ID new USER-COMMAND m3.
PARAMETERS:p_date_4 TYPE idcn_dec3 DEFAULT 60  MODIF ID new. "like rfpdo1-allgrogr default '020'.
PARAMETERS:p_unit4 TYPE idcn_timeunit DEFAULT '1' AS LISTBOX VISIBLE LENGTH 6 OBLIGATORY MODIF ID new USER-COMMAND m4.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 01(31) TEXT-029 FOR FIELD p_date_5 MODIF ID new.
SELECTION-SCREEN POSITION POS_LOW.
PARAMETERS:p_date_5 TYPE idcn_dec3 DEFAULT 90 VISIBLE LENGTH 4 MODIF ID new.
PARAMETERS:p_unit5 TYPE idcn_timeunit DEFAULT '1' AS LISTBOX VISIBLE LENGTH 6 OBLIGATORY MODIF ID new USER-COMMAND m5.
PARAMETERS:p_date_6 TYPE idcn_dec3 DEFAULT 180  MODIF ID new. "like rfpdo1-allgrogr default '020'.
PARAMETERS:p_unit6 TYPE idcn_timeunit DEFAULT '1' AS LISTBOX VISIBLE LENGTH 6 OBLIGATORY MODIF ID new USER-COMMAND m6.
PARAMETERS:p_date_7 TYPE idcn_dec3 DEFAULT 360  MODIF ID new. "like rfpdo1-allgrogr default '020'.
PARAMETERS:p_unit7 TYPE idcn_timeunit DEFAULT '1' AS LISTBOX VISIBLE LENGTH 6 OBLIGATORY MODIF ID new USER-COMMAND m7.
PARAMETERS:p_date_8 TYPE idcn_dec3 DEFAULT 720  MODIF ID new. "like rfpdo1-allgrogr default '020'.
PARAMETERS:p_unit8 TYPE idcn_timeunit DEFAULT '1' AS LISTBOX VISIBLE LENGTH 6 OBLIGATORY MODIF ID new USER-COMMAND m8.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN END OF BLOCK b4.
SELECTION-SCREEN END OF BLOCK b2.

AT SELECTION-SCREEN.
  PERFORM frm_input_check.

START-OF-SELECTION.
  PERFORM ficn_collectparams.
  PERFORM ficn_transformdate.
  PERFORM ficn_check_date_interval_new.
  IF 1 = 2.

  ENDIF.
*&---------------------------------------------------------------------*
*&      Form  FICN_TRANSFORMDATE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM ficn_transformdate .
  DATA ls_pdate TYPE ts_pdate.
  DATA ls_pdate_new TYPE ts_pdate.
  DATA lv_flag TYPE char1.
  DATA lv_stratflag TYPE char1.

  SORT gt_pdates BY index DESCENDING.
  LOOP AT gt_pdates INTO ls_pdate.
    IF ls_pdate-pdates IS NOT INITIAL .
      lv_stratflag = 'X'.
      CLEAR ls_pdate_new.
      PERFORM transformtodays  USING ls_pdate CHANGING ls_pdate_new.
      APPEND ls_pdate_new TO gt_pdates_new.
    ELSEIF lv_stratflag = 'X' AND lv_flag <> 'X'.
      lv_flag = 'X'.
      APPEND ls_pdate TO gt_pdates_new.
    ELSEIF  lv_stratflag = 'X' AND lv_flag = 'X'.
*          MESSAGE E002.
    ENDIF.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FICN_COLLECTPARAMS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM ficn_collectparams .
  CLEAR gt_pdates.
  CLEAR gt_pdates_new.

  DATA ls_pdate1 TYPE ts_pdate.
  ls_pdate1-index = 1.
  ls_pdate1-pdates = p_date_1.
  ls_pdate1-punit = p_unit1.
  APPEND ls_pdate1 TO gt_pdates.

  DATA ls_pdate2 TYPE ts_pdate.
  ls_pdate2-index = 2.
  ls_pdate2-pdates = p_date_2.
  ls_pdate2-punit = p_unit2.
  APPEND ls_pdate2 TO gt_pdates.

  DATA ls_pdate3 TYPE ts_pdate.
  ls_pdate3-index = 3.
  ls_pdate3-pdates = p_date_3.
  ls_pdate3-punit = p_unit3.
  APPEND ls_pdate3 TO gt_pdates.

  DATA ls_pdate4 TYPE ts_pdate.
  ls_pdate4-index = 4.
  ls_pdate4-pdates = p_date_4.
  ls_pdate4-punit = p_unit4.
  APPEND ls_pdate4 TO gt_pdates.

  DATA ls_pdate5 TYPE ts_pdate.
  ls_pdate5-index = 5.
  ls_pdate5-pdates = p_date_5.
  ls_pdate5-punit = p_unit5.
  APPEND ls_pdate5 TO gt_pdates.

  DATA ls_pdate6 TYPE ts_pdate.
  ls_pdate6-index = 6.
  ls_pdate6-pdates = p_date_6.
  ls_pdate6-punit = p_unit6.
  APPEND ls_pdate6 TO gt_pdates.

  DATA ls_pdate7 TYPE ts_pdate.
  ls_pdate7-index = 7.
  ls_pdate7-pdates = p_date_7.
  ls_pdate7-punit = p_unit7.
  APPEND ls_pdate7 TO gt_pdates.

  DATA ls_pdate8 TYPE ts_pdate.
  ls_pdate8-index = 8.
  ls_pdate8-pdates = p_date_8.
  ls_pdate8-punit = p_unit8.
  APPEND ls_pdate8 TO gt_pdates.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_INPUT_CHECK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_input_check .
  CASE sy-ucomm.
    WHEN 'M1'.
      p_unit2 = p_unit3 = p_unit4 = p_unit5 = p_unit6 = p_unit7 = p_unit8 = p_unit1.
    WHEN 'M2'.
      p_unit3 = p_unit4 = p_unit5 = p_unit6 = p_unit7 = p_unit8 = p_unit1 = p_unit2 .
    WHEN 'M3'.
      p_unit4 = p_unit5 = p_unit6 = p_unit7 = p_unit8 = p_unit1 = p_unit2  =   p_unit3.
    WHEN 'M4'.
      p_unit5 = p_unit6 = p_unit7 = p_unit8 = p_unit1 = p_unit2  = p_unit3 = p_unit4.
    WHEN 'M5'.
      p_unit6 = p_unit7 = p_unit8 = p_unit1 = p_unit2 =   p_unit3 = p_unit4 = p_unit5.
    WHEN 'M6'.
      p_unit7 = p_unit8 = p_unit1 = p_unit2 = p_unit3 = p_unit4 = p_unit5 = p_unit6.
    WHEN 'M7'.
      p_unit8 = p_unit1 = p_unit2 = p_unit3 = p_unit4 = p_unit5 = p_unit6 = p_unit7 .
    WHEN 'M8'.
      p_unit1 = p_unit2 = p_unit3 = p_unit4 = p_unit5 = p_unit6 = p_unit7 = p_unit8.
    WHEN OTHERS.
  ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FICN_CHECK_DATE_INTERVAL_NEW
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM ficn_check_date_interval_new .
*&**************************************************************************************************
* &Step1.0:Define local variable
*&**************************************************************************************************
  DATA ls_date_intvl TYPE ts_pdate_intvl.
  DATA lt_date_intvl LIKE TABLE OF ls_date_intvl.
  DATA lv_pre TYPE i.
  DATA ls_pdate TYPE ts_pdate.
  DATA lv_flag TYPE c.
  DATA lv_startflag TYPE c.
*&**************************************************************************************************
* &Step2.0:collect into internal table
*&**************************************************************************************************
  CLEAR gt_date_intvl_new.
  SORT gt_pdates_new BY index DESCENDING.
  LOOP AT gt_pdates_new INTO ls_pdate.
    CLEAR ls_date_intvl.
    ls_date_intvl-num = ls_pdate-pdates.
    ls_date_intvl-unit = ls_pdate-punit.
    INSERT ls_date_intvl INTO lt_date_intvl INDEX 1.
  ENDLOOP .
  CLEAR gt_date_intvl2.
  CLEAR ls_pdate.
  SORT gt_pdates BY index DESCENDING.
  LOOP AT gt_pdates INTO ls_pdate.
    IF ls_pdate-pdates IS NOT INITIAL.
      lv_startflag = 'X'.
      CLEAR ls_date_intvl.
      ls_date_intvl-num = ls_pdate-pdates.
      ls_date_intvl-unit = ls_pdate-punit.
      INSERT ls_date_intvl INTO gt_date_intvl2 INDEX 1.
    ELSEIF lv_startflag = 'X' AND lv_flag <> 'X'.
      lv_flag = 'X'.
      CLEAR ls_date_intvl.
      ls_date_intvl-num = ls_pdate-pdates.
      ls_date_intvl-unit = ls_pdate-punit.
      INSERT ls_date_intvl INTO gt_date_intvl2 INDEX 1.
    ENDIF.
  ENDLOOP .

*&**************************************************************************************************
* &Step3.0:Input data validity check
*&**************************************************************************************************
*&**************************************************************************************************
* &Step3.1:Check: No more than c_max_date
  LOOP AT lt_date_intvl INTO ls_date_intvl WHERE num GT c_max_date.
    MESSAGE e881 WITH c_max_date.
  ENDLOOP.
*&**************************************************************************************************
*&**************************************************************************************************
* &Step3.2:Check: Date Sequence
  LOOP AT lt_date_intvl INTO ls_date_intvl.
    IF sy-tabix = 1.
      lv_pre = ls_date_intvl-num.
      CONTINUE.
    ENDIF.
    IF ls_date_intvl-num <= lv_pre.
      MESSAGE e802.
      EXIT.
    ENDIF.
    lv_pre = ls_date_intvl-num.
  ENDLOOP .
*&**************************************************************************************************
*&**************************************************************************************************
* &Step4.0:Copy into Global Table
  gt_date_intvl_new = lt_date_intvl.
*&**************************************************************************************************
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  TRANSFORMTODAYS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LS_PDATE  text
*      <--P_LS_PDATE_NEW  text
*----------------------------------------------------------------------*
FORM transformtodays  USING  date TYPE   ts_pdate
                      CHANGING new_date TYPE ts_pdate.
  DATA lv_month TYPE dlymo.
  DATA lv_year TYPE dlyyr.
  DATA lv_year4 TYPE i.
  DATA lv_duedate TYPE begda .
  DATA lv_duedatec TYPE char8.
  DATA lv_duedate2 TYPE syst_datum.
  DATA lv_keydate TYPE d .
  DATA lv_yearc TYPE char4.
  DATA lv_monthday TYPE char4.
  DATA lv_feb TYPE begda.
  DATA lv_febc TYPE char8.

  IF date-pdates >= 0.
    CASE date-punit.
      WHEN '1'." day
        new_date = date.
      WHEN '2'.  " month
        lv_year = date-pdates DIV 12.
        lv_month = date-pdates MOD 12 .
        CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
          EXPORTING
            date      = s_kdate
            days      = 0
            months    = lv_month
            signum    = '-'
            years     = lv_year
          IMPORTING
            calc_date = lv_duedate.
        new_date-pdates = s_kdate - lv_duedate.
        new_date-punit = '1'.
        new_date-index = date-index.
      WHEN '3'. "year
        lv_keydate = s_kdate.
        lv_year4 = lv_keydate(4) - date-pdates.
        lv_yearc = lv_year4.
        lv_monthday = s_kdate+4(4).
        CONCATENATE lv_yearc lv_monthday INTO lv_duedatec.
        lv_duedate = lv_duedatec.
        IF lv_duedate+4(2) = '02'.
          CONCATENATE lv_duedate(6)  '01' INTO lv_febc.
          lv_feb = lv_febc.
          CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'
            EXPORTING
              day_in            = lv_feb
            IMPORTING
              last_day_of_month = lv_duedate2
*            EXCEPTIONS
*             day_in_no_date    = 1
*             OTHERS            = 2.
            .
          IF sy-subrc <> 0.
            RETURN.
          ENDIF.
          IF lv_duedate > lv_duedate2.
            lv_duedate = lv_duedate2.
          ENDIF.
        ENDIF.
        new_date-pdates = s_kdate - lv_duedate.
        new_date-punit = '1'.
        new_date-index = date-index.
    ENDCASE.
  ELSE.
    CASE date-punit.
      WHEN '1'." day
        new_date = date.
      WHEN '2'." month
        date-pdates = abs( date-pdates ).
        lv_year = date-pdates DIV 12.
        lv_month = date-pdates MOD 12 .
        CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
          EXPORTING
            date      = s_kdate
            days      = 0
            months    = lv_month
            signum    = '+'
            years     = lv_year
          IMPORTING
            calc_date = lv_duedate.

        new_date-pdates = lv_duedate - s_kdate.
        new_date-pdates = 0 - new_date-pdates.
        new_date-punit = '1'.
        new_date-index = date-index.

      WHEN '3'." year
        date-pdates = abs( date-pdates ).
        lv_keydate = s_kdate.
        lv_year4 = lv_keydate(4) + date-pdates.
        lv_yearc = lv_year4.
        lv_monthday = s_kdate+4(4).
        CONCATENATE lv_yearc lv_monthday INTO lv_duedatec.
        lv_duedate = lv_duedatec." determine basedate
        IF lv_duedate+4(2) = '02'.
          CONCATENATE lv_duedate(6) '01' INTO lv_febc.
          lv_feb = lv_febc.
          CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'
            EXPORTING
              day_in            = lv_feb
            IMPORTING
              last_day_of_month = lv_duedate2
*            EXCEPTIONS
*             day_in_no_date    = 1
*             others            = 2.
            .
          IF sy-subrc <> 0.
            RETURN.
          ENDIF.

          IF lv_duedate > lv_duedate2.
            CONCATENATE lv_duedate(4) '0301' INTO lv_duedatec.
            lv_duedate  = lv_duedatec.
          ENDIF.
        ENDIF.
        new_date-pdates = lv_duedate - s_kdate.
        new_date-pdates = 0 - new_date-pdates.
        new_date-index = date-index.
    ENDCASE.
  ENDIF.
ENDFORM.

给TA买糖
共{{data.count}}人
人已赞赏
ABAPSAP

【ABAP】常用模板代码一(选择屏幕多页签)

2021-7-14 14:19:03

ABAPSAP

【ABAP】常用模板三(选择屏幕下拉框)

2021-7-14 17:31:37

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索