1. 程式人生 > >動態內表 動態ALV顯示

動態內表 動態ALV顯示

動態內表及動態ALV顯示

最近寫了一個forecast,要求按使用者輸入的日期分周或月顯示,我把它貼出來,給有需要的人蔘考一下

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

*& Report  ZRSD041

*&

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

*&Description : Forecast Report (MD61)

*&Created By  : Michael Chow 2008.04.29

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

REPORT  ZRSD041.

TYPE-POOLS: slis.

TABLES: PBED,   "Independent Requirements Data

        PBHI,   "Independent Requirements History

        PBIM,   "Independent Requirements for Material

        MDKP,  "Header Data for MRP Document

        MDTB,  "MRP table

        MDTC,  "Aggregated MRP table items

        T457T. "Description of MRP elements

********************Global Data definitation*************************************

FIELD-SYMBOLS:<t_dyntable> TYPE STANDARD TABLE ,  "??? Dynamic internal table name

               <fs_dyntable> type any ,                    " Field symbol to create work area

               <fs_fldval> type any,              " Field symbol to assign values

               <l_field> type any.

DATA:        G_cols TYPE I ,                     " number of columns

             G_Rows TYPE I,                      " Number of Lines

             g_dc(5) TYPE C,                     " Date format

             g_count TYPE I,                     "Period Value Count

             G_sta_col(2) TYPE N.                   "Start Column

DATA:   t_newtable TYPE REF TO data,

        t_newline  TYPE REF TO data,

        MY_TABLEREF type ref to DATA ,

        MY_WAREF type ref to DATA ,

        fs_fldcat   TYPE slis_t_fieldcat_alv,

        l_fldcat   TYPE lvc_t_fcat,

        wa_it_fldcat TYPE lvc_s_fcat,

        wa_colno(2) TYPE n,

        wa_flname(10) TYPE c.

DATA:  BEGIN OF IT_MD61 OCCURS 0,   "Independent Requirements for Material

                        MATNR   like PBIM-MATNR,  "Material

                        WERKS   LIKE PBIM-WERKS,  "Plant

                        BEDAE   LIKE PBIM-BEDAE,  "Requirement type

                        VERSB   LIKE PBIM-VERSB,  "Version number

                        PBDNR   LIKE PBIM-PBDNR,  "Requirement Number

                        BDZEI   LIKE PBIM-BDZEI,  "Independent requirements pointer

                        PDATU   LIKE PBED-PDATU,  "Delivery/order finish date

                        WDATU   LIKE PBED-WDATU,  "Reqstd date

                        LAEDA   LIKE PBED-LAEDA,  "Date of Last Change

                        MEINS   LIKE PBED-MEINS,  "Base Unit of Measure

                        PLNMG   LIKE PBED-PLNMG,  "Planned quantity

                        UPLMG   LIKE PBED-UPLMG,  "Originally planned quantity

                        ENTLI   LIKE PBED-ENTLI,  "Date type (day, week, month, interval)

                        ENTLU   LIKE PBED-ENTLU,  "Period indicator (day, week, month, posting period)

                        PERXX   LIKE PBED-PERXX.  "Planning period

DATA: END OF  IT_MD61.

DATA: BEGIN OF IT_PBHI  OCCURS 0. "Independent Requirements History

      include structure PBHI.

DATA:       MATNR LIKE PBIM-MATNR.

DATA:       PERXX LIKE PBED-PERXX.

DATA: END OF  IT_PBHI.

DATA: BEGIN OF IT_TOTAL OCCURS 0, "Totoal requirement

                        MATNR LIKE PBIM-MATNR,  "Material

                        PLNMG LIKE PBED-PLNMG,  "Total

                        LAEDA LIKE PBED-LAEDA.  "Doc date

DATA: END OF   IT_TOTAL.

DATA: BEGIN OF IT_HI_TOTAL OCCURS 0, "Totoal requirement

                        MATNR LIKE PBIM-MATNR,  "Material

                        PLNMG LIKE PBED-PLNMG,  "Total

                        LAEDA LIKE PBED-LAEDA.  "Doc date

DATA: END OF   IT_HI_TOTAL.

DATA: BEGIN OF MDTBX OCCURS 0.

        INCLUDE STRUCTURE MDTB.

DATA:   MATNR   LIKE MARC-MATNR.

DATA:   PERXX   LIKE PBED-PERXX.

DATA: END   OF MDTBX.

DATA: BEGIN OF IT_MDTB OCCURS 0.

        INCLUDE STRUCTURE MDTB.

DATA:          DELB1 LIKE T457T-DELB1.

DATA: END OF   IT_MDTB.

********************End of Global Data definitation********************************

SELECTION-SCREEN  BEGIN OF BLOCK rpt1 with frame title text-t01.

  PARAMETERS: P_werks  like MARC-WERKS OBLIGATORY DEFAULT '7231'.

  SELECT-OPTIONS: s_MATNR FOR PBIM-MATNR. "OBLIGATORY.

  PARAMETERS: p_year   LIKE bkpf-gjahr DEFAULT sy-datum+0(4) OBLIGATORY.

  PARAMETERS: p_entlu  LIKE PBED-ENTLI OBLIGATORY DEFAULT 'W',

              p_Frm(2) TYPE N OBLIGATORY,

              P_TO(2)  TYPE N OBLIGATORY.

  SELECTION-SCREEN skip.

  PARAMETERS: P_WHIS AS CHECKBOX DEFAULT 'X'.

SELECTION-SCREEN END OF BLOCK rpt1.

INITIALIZATION.

*  G_Cols = 10.     "Initialize the Column Numbers.

  G_Rows = 20.     "Initialize the Row Numbers

Start-of-SELECTION.

PERFORM Input_Analyst.

PERFORM GET_FC_DATA.     "Get sales forecast data.

PERFORM GET_FC_HIS.      "Get Forecast History

PERFORM Dny_field_Creation.

PERFORM Dny_Table_Creation.

PERFORM Dny_field_value_update.

PERFORM Add_alv_fields.

PERFORM Show_Alv.

end-of-SELECTION.

Form Dny_Field_Creation.    "Step 1

* Create fields .

   wa_it_fldcat-fieldname = 'MATNR'.

   wa_it_fldcat-datatype = 'CHAR'.

   wa_it_fldcat-intlen = 18.

   APPEND wa_it_fldcat TO l_fldcat.

   wa_it_fldcat-fieldname = 'Remark'.

   wa_it_fldcat-datatype = 'CHAR'.

   wa_it_fldcat-intlen = 30.

   APPEND wa_it_fldcat TO l_fldcat.

  g_sta_col = p_frm.

  DO G_cols TIMES.

    CLEAR wa_it_fldcat.

*    move sy-index to wa_colno.

    move g_sta_col to wa_colno.

    concatenate G_DC wa_colno into wa_flname.

    wa_it_fldcat-fieldname = wa_flname.

    wa_it_fldcat-datatype = 'CHAR'.

    wa_it_fldcat-intlen = 10.

    APPEND wa_it_fldcat TO l_fldcat.

    g_sta_col = g_sta_col + 1.

  ENDDO.

   wa_it_fldcat-fieldname = 'Total'.

   wa_it_fldcat-datatype = 'N'.

   wa_it_fldcat-intlen = 13.

   APPEND wa_it_fldcat TO l_fldcat.

   wa_it_fldcat-fieldname = 'Docdt'.

   wa_it_fldcat-datatype = 'D'.

   wa_it_fldcat-intlen = 10.

   APPEND wa_it_fldcat TO l_fldcat.

ENDFORM.

FORM Dny_Table_Creation. "Step 2

* Create dynamic internal table and assign to FS

  CALL METHOD cl_alv_table_create=>create_dynamic_table

    EXPORTING

      it_fieldcatalog = l_fldcat

    IMPORTING

      ep_table        = t_newtable.

  ASSIGN t_newtable->* TO <t_dyntable>.

* Create dynamic work area and assign to FS

  CREATE DATA t_newline LIKE LINE OF <t_dyntable>.

  ASSIGN t_newline->* TO <fs_dyntable>.

ENDFORM.

"Update field value dynamically

Form Dny_Field_value_Update.  "Step 3

  DATA: fieldname(20) TYPE c.

  DATA: fieldvalue(10) TYPE c.

  DATA: index(3) TYPE c.

  DATA: L_WK(2) TYPE C.

  DATA: L_Period(6) TYPE C.

"Setup the field value

    ASSIGN t_newtable->* TO <t_dyntable>.

* Create dynamic work area and assign to FS

  CREATE DATA t_newline LIKE LINE OF <t_dyntable>.

  ASSIGN t_newline->* TO <l_field>.

g_sta_col = p_frm.

Loop at it_total.

  if P_WHIS EQ 'X'.

   PERFORM Dny_Field_FC_HIS_Update USING it_total-matnr. "Insert Fc History Record

   endif.

  ASSIGN COMPONENT  'MATNR'

        OF STRUCTURE <fs_dyntable> TO <fs_fldval>.

    <fs_fldval> =  it_total-matnr.

  ASSIGN COMPONENT  'REMARK'

        OF STRUCTURE <fs_dyntable> TO <fs_fldval>.

    <fs_fldval> =  'Current'.

     ASSIGN COMPONENT  'TOTAL'

        OF STRUCTURE <fs_dyntable> TO <fs_fldval>.

      <fs_fldval> =  it_total-plnmg.

     ASSIGN COMPONENT  'DOCDT'

        OF STRUCTURE <fs_dyntable> TO <fs_fldval>.

    <fs_fldval> = it_total-LAEDA.

G_Cols = p_frm.

g_sta_col = G_Cols.

While G_Cols < p_To.

  clear fieldvalue.

  CONCATENATE p_year g_sta_col into l_period.

    MOVE g_sta_col TO wa_colno.

    CONCATENATE G_DC wa_colno INTO wa_flname.

* Set up fieldvalue

read TABLE it_md61 with key matnr = it_total-matnr PERXX  = l_period.

if sy-subrc eq 0.

      fieldvalue = it_md61-PLNMG.

      else.

      fieldvalue = ''.

    endif.

   g_sta_col = g_sta_col + 1.

   G_Cols = G_Cols + 1.

   ASSIGN COMPONENT  wa_flname

     OF STRUCTURE <fs_dyntable> TO <fs_fldval>.

    <fs_fldval> =  fieldvalue.

ENDWHILE.

    append <fs_dyntable> to <t_dyntable>.

    clear <fs_dyntable> .

    PERFORM Extract_MRP_DATA using IT_TOTAL-MATNR. "Get MRP data

endloop.

**************************************************

*      INSERT <fs_dyntable>  INTO TABLE <t_dyntable>.

*    append <fs_dyntable> to <t_dyntable>.

ENDFORM.

Form add_alv_fields. "Step 4

* Append fields to the dynamic internal table

  APPEND <fs_dyntable> TO <t_dyntable>.

DATA: wa_cat LIKE LINE OF fs_fldcat,

      L_TXT(20) TYPE C.

"Added Special Field into ALV FCAT.

    wa_cat-fieldname = 'MATNR'.

    wa_cat-seltext_s = 'Material'.

    wa_cat-outputlen = '18'.

    APPEND wa_cat TO fs_fldcat.

    wa_cat-fieldname = 'REMARK'.

    wa_cat-seltext_s = 'REMARK'.

    wa_cat-outputlen = '30'.

    APPEND wa_cat TO fs_fldcat.

g_sta_col = p_frm.

  DO G_cols TIMES.

    CLEAR wa_cat.

    MOVE g_sta_col TO wa_colno.

    CONCATENATE G_DC wa_colno INTO wa_flname.

  CONCATENATE wa_colno '.' P_Year into L_TXT.

IF P_ENTLU EQ 'W'.

  CONCATENATE 'W ' L_TXT into L_TXT SEPARATED BY SPACE.

else.

  CONCATENATE 'M ' L_TXT into L_TXT SEPARATED BY SPACE.

endif.

    wa_cat-fieldname = wa_flname.

    wa_cat-seltext_s = L_TXT.

    wa_cat-outputlen = '10'.

    APPEND wa_cat TO fs_fldcat.

  g_sta_col = g_sta_col + 1.

  ENDDO.

      wa_cat-fieldname = 'TOTAL'.

    wa_cat-seltext_s = 'Total'.

    wa_cat-outputlen = '17'.

    APPEND wa_cat TO fs_fldcat.

    wa_cat-fieldname = 'DOCDT'.

    wa_cat-seltext_s = 'Doc. Date'.

    wa_cat-outputlen = '10'.

    APPEND wa_cat TO fs_fldcat.

ENDFORM.

Form Show_Alv.

* Call ABAP List Viewer (ALV)

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

    EXPORTING

      it_fieldcat = fs_fldcat

    TABLES

      t_outtab    = <t_dyntable>.

Endform.

FORM  Input_Analyst.

If P_ENTLU NE 'W' AND P_ENTLU NE 'M'.

  Message 'Invaild Date Type,only (Week or Month) are available currently' type 'S' DISPLAY LIKE 'E'.

  Stop.

endif.

    g_dc = P_ENTLU.

    CONDENSE g_dc NO-GAPS.

  CASE  G_DC.

        WHEN 'W'.

        G_DC = 'WEEK'.

        WHEN 'M'.

        G_DC = 'MONTH'.

ENDCASE.

G_count = p_to - p_frm.

IF G_count <= 0.

  Message 'Invaild Date planning period' type 'S' DISPLAY LIKE 'E'.

  Stop.

elseif G_DC EQ 'WEEK' and p_to > 52.

    Message 'Invaild Date planning period' type 'S' DISPLAY LIKE 'E'.

   stop.

elseif  G_DC EQ 'MONTH' and p_to > 12.

    Message 'Invaild Date planning period' type 'S' DISPLAY LIKE 'E'.

   stop.

elseif G_DC EQ 'WEEK' and G_Count > 51.

   Message 'Invaild Date planning period' type 'S' DISPLAY LIKE 'E'.

   stop.

elseif G_DC EQ 'MONTH' and G_Count > 11.

   Message 'Invaild Date planning period' type 'S' DISPLAY LIKE 'E'.

   stop.

endif.

g_sta_col = p_frm.

g_cols = g_count.

G_cols = G_cols + 1.

ENDFORM.

Form Get_FC_DATA. "Get forecast data.

DATA: l_year(5) type c,

      v_index like sy-tabix,

      v_ENTLI like pbed-ENTLI.

if P_ENTLU EQ 'W'.

    v_entli = '2'.

   else.

    v_entli = '3'.

endif.

CONCATENATE p_year '%' into l_year.

SELECT * INTO CORRESPONDING FIELDS OF TABLE IT_MD61

  FROM PBIM AS a

  INNER JOIN PBED as b

  ON a~BDZEI = b~BDZEI

  WHERE a~matnr in s_matnr

  AND b~PERXX like l_year

  AND B~ENTLI = v_entli

  and a~LOEVR NE 'X'.

"Get total requirement

  Loop at IT_MD61.

      read TABLE IT_TOTAL WITH KEY MATNR = IT_MD61-MATNR.

      if sy-subrc eq 0.

      v_index = sy-tabix.

      IT_TOTAL-PLNMG = IT_TOTAL-PLNMG + IT_MD61-PLNMG.

      modify it_total index v_index.

      else.

      it_total-matnr = it_md61-matnr.

      it_total-plnmg = it_md61-plnmg.

      it_total-LAEDA = it_md61-LAEDA.

      append it_total.

      ENDIF.

endloop.

ENDFORM.

FORM GET_FC_HIS. "Get Forecast History

DATA: v_index like sy-tabix,

      l_index like sy-tabix,

      v_DATE  like sy-datum,

      V_WEEK  LIKE SCAL-WEEK.

SELECT * INTO CORRESPONDING FIELDS OF TABLE IT_PBHI

  FROM PBIM AS a

  INNER JOIN PBHI as b

  ON a~bdzei = b~bdzei

  FOR ALL ENTRIES IN it_total

  where a~MATNR = it_total-matnr

  and a~LOEVR NE 'X'

  And B~DBMNG > 0

  and B~plnmg NE b~DBMNG.

Loop at IT_PBHI.

     l_index = sy-tabix.

  read TABLE it_hi_total WITH KEY MATNR = IT_PBHI-MATNR.

      if sy-subrc eq 0.

      v_index = sy-tabix.

      it_hi_total-PLNMG = it_hi_total-PLNMG + IT_PBHI-PLNMG.

      modify it_hi_total index v_index.

      else.

      it_hi_total-matnr = IT_PBHI-matnr.

      it_hi_total-plnmg = IT_PBHI-plnmg.

      it_hi_total-LAEDA = IT_PBHI-LAEDA.

      append it_hi_total.

      ENDIF.

V_date = IT_PBHI-PDATU.

CALL FUNCTION 'GET_WEEK_INFO_BASED_ON_DATE'

EXPORTING

   DATE          = V_date

IMPORTING

   WEEK          = V_WEEK

*   MONDAY        =

*   SUNDAY        =

          .

if sy-subrc eq 0.

  it_pbhi-PERXX = v_week.

  modify it_pbhi index l_index.

ENDIF.

ENDLOOP.

*DELETE ADJACENT DUPLICATES FROM it_pbhi COMPARING  MATNR PERXX PDATU  LAEDA AENAM.

ENDFORM.

Form Dny_Field_FC_HIS_Update USING V_MATNR.

  DATA: fieldname(20) TYPE c.

  DATA: fieldvalue(10) TYPE c.

  DATA: index(3) TYPE c.

  DATA: L_WK(2) TYPE C.

  DATA: L_Period(6) TYPE C.

"Setup the field value

    ASSIGN t_newtable->* TO <t_dyntable>.

* Create dynamic work area and assign to FS

  CREATE DATA t_newline LIKE LINE OF <t_dyntable>.

  ASSIGN t_newline->* TO <l_field>.

g_sta_col = p_frm.

Loop at it_hi_total where matnr = v_matnr.

  ASSIGN COMPONENT  'MATNR'

        OF STRUCTURE <fs_dyntable> TO <fs_fldval>.

    <fs_fldval> =  it_hi_total-matnr.

  ASSIGN COMPONENT  'REMARK'

        OF STRUCTURE <fs_dyntable> TO <fs_fldval>.

    <fs_fldval> =  'History'.

     ASSIGN COMPONENT  'TOTAL'

        OF STRUCTURE <fs_dyntable> TO <fs_fldval>.

      <fs_fldval> =  it_hi_total-plnmg.

     ASSIGN COMPONENT  'DOCDT'

        OF STRUCTURE <fs_dyntable> TO <fs_fldval>.

    <fs_fldval> = it_hi_total-LAEDA.

G_Cols = p_frm.

g_sta_col = G_Cols.

While G_Cols < p_To.

  clear fieldvalue.

  CONCATENATE p_year g_sta_col into l_period.

    MOVE g_sta_col TO wa_colno.

    CONCATENATE G_DC wa_colno INTO wa_flname.

* Set up fieldvalue

read TABLE it_PBHI with key matnr = it_hi_total-matnr PERXX  = l_period.

if sy-subrc eq 0.

      fieldvalue = it_PBHI-PLNMG.

      else.

      fieldvalue = ''.

    endif.

   g_sta_col = g_sta_col + 1.

   G_Cols = G_Cols + 1.

   ASSIGN COMPONENT  wa_flname

     OF STRUCTURE <fs_dyntable> TO <fs_fldval>.

    <fs_fldval> =  fieldvalue.

ENDWHILE.

    append <fs_dyntable> to <t_dyntable>.

    clear <fs_dyntable> .

endloop.

**************************************************

*      INSERT <fs_dyntable>  INTO TABLE <t_dyntable>.

*    append <fs_dyntable> to <t_dyntable>.

ENDFORM.

Form Extract_MRP_DATA using v_matnr.

DATA:

      l_index like sy-tabix,

      v_DATE  like sy-datum,

      V_WEEK  LIKE SCAL-WEEK.

CLEAR: MDKP, MDTB.

SELECT * FROM MDKP WHERE DTART EQ 'MD'

                     AND MATNR EQ v_MATNR

                     AND PLWRK EQ p_werks.

*    WRITE:/ MDKP-MATNR, MDKP-PLWRK.

    IF MDKP-CFLAG EQ 'X'.

       CLEAR MDTBX. REFRESH MDTBX.

       IMPORT MDTBX FROM DATABASE MDTC(AR) ID MDKP-DTNUM.

       LOOP AT MDTBX.

         L_index = sy-tabix.

         MOVE MDTBX TO MDTB.

         SELECT SINGLE * FROM T457T WHERE SPRAS = 'E'

                                      AND DELKZ = MDTB-DELKZ.

         IF MDTB-PLUMI = '-'. MULTIPLY MDTB-MNG01 BY -1. ENDIF.

V_date = MDTBX-DAT00.

CALL FUNCTION 'GET_WEEK_INFO_BASED_ON_DATE'

EXPORTING

   DATE          = V_date

IMPORTING

   WEEK          = V_WEEK

*   MONDAY        =

*   SUNDAY        =

          .

if sy-subrc eq 0.

   MDTBX-PERXX = v_week.

   MDTBX-MATNR = v_matnr.

  modify MDTBX index l_index.

ENDIF.

*         WRITE:/ MDTB-DAT00, T457T-DELB1, MDTB-VSTAT,

*                                          MDTB-MNG01, MDTB-LGORT.

       ENDLOOP.

    ELSE.

       SELECT * FROM MDTB

                     WHERE DTNUM EQ MDKP-DTNUM

                     ORDER BY PRIMARY KEY.

         SELECT SINGLE * FROM T457T WHERE SPRAS = 'E'

                                      AND DELKZ = MDTB-DELKZ.

         IF MDTB-PLUMI = '-'. MULTIPLY MDTB-MNG01 BY -1. ENDIF.

*         WRITE:/ MDTB-DAT00, T457T-DELB1, MDTB-VSTAT,

*                                          MDTB-MNG01, MDTB-LGORT.

       ENDSELECT.

    ENDIF.

ENDSELECT.

ENDFORM.