Sunday, March 17, 2013

MRP TOTAL STOCK REPORT

*&---------------------------------------------------------------------*
*& Report  ZPP_PLANNING_REPORT
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZPP_PLANNING_REPORT.

TABLES : afko,mard,makt.
type-pools: slis.


TYPES : BEGIN OF tY_afko,
         aufnr TYPE afko-aufnr,
         plnbez TYPE afko-plnbez,
         gmein TYPE afko-gmein,
         gamng TYPE afko-gamng,
        END OF TY_AFKO.
TYPES : BEGIN OF tY_afko1,
         aufnr TYPE afko-aufnr,
         plnbez TYPE afko-plnbez,
         gmein TYPE afko-gmein,
         gamng TYPE afko-gamng,
        END OF TY_AFKO1.


TYPES : BEGIN OF TY_makt,
          matnr TYPE makt-matnr,
          maktx TYPE makt-maktx,
        END OF ty_makt.
TYPES : BEGIN OF ty_mard,
          matnr TYPE mard-matnr,
          labst TYPE mard-labst,
          insme TYPE mard-insme,
        END OF ty_mard.
TYPES : BEGIN OF ty_march,
          matnr TYPE march-matnr,
          trame TYPE march-trame,
        END OF ty_march.
TYPES : BEGIN OF tY_aufk,
*          gamng TYPE aufk-gamng,
          aufnr TYPE aufk-aufnr,
          objnr TYPE aufk-objnr,
        END OF ty_aufk.
TYPES : BEGIN OF ty_jest,
          objnr TYPE jest-objnr,
          STAT TYPE jest-STAT,
        END OF ty_jest.

DATA : it_aufk TYPE STANDARD TABLE OF ty_aufk,
       wa_aufk TYPE ty_aufk,
       it_aufk1 TYPE STANDARD TABLE OF ty_aufk,
       wa_aufk1 TYPE ty_aufk,
       it_jest TYPE STANDARD TABLE OF ty_jest,
       wa_jest TYPE ty_jest.


*TYPES : BEGIN OF ty_final,
*         aufnr TYPE afko-aufnr, ""Order No.
*         plnbez TYPE afko-plnbez, ""Material No
*         gmein TYPE afko-gmein, "" UOM
*         gamng TYPE afko-gamng,  ""op. stock
*         maktx TYPE makt-maktx, "" Material Desc.
*         labst TYPE mard-labst,
*        END OF ty_final.

TYPES : BEGIN OF ty_final,
         aufnr TYPE afko-aufnr, ""Order No.
         plnbez TYPE afko-plnbez, ""Material No
         gmein TYPE afko-gmein, "" UOM
         gamng TYPE afko-gamng,  ""op. stock
         matnr TYPE mard-matnr,
         labst TYPE mard-labst,
         insme TYPE mard-insme,
         trame TYPE march-trame,
         maktx TYPE makt-maktx,
         labst1 TYPE mard-labst,
         STAT TYPE jest-STAT,
  END OF ty_final.

  DATA : it_final TYPE STANDARD TABLE OF ty_final,
         wa_final TYPE ty_final.

 DATA : it_afko  TYPE STANDARD TABLE OF ty_afko1,
        wA_afko  TYPE ty_afko1,
        it_afko1  TYPE STANDARD TABLE OF ty_afko,
        wA_afko1  TYPE ty_afko,
        it_mard  TYPE STANDARD TABLE OF ty_mard,
        wa_mard  TYPE ty_mard,
        it_march TYPE STANDARD TABLE OF ty_march,
        wa_march TYPE ty_march,
        it_makt TYPE STANDARD TABLE OF ty_makt,
        wa_makt TYPE ty_makt.
*        it_final TYPE STANDARD TABLE OF ty_final,
*        wa_final TYPE ty_final.

  data: fieldcatalog  type slis_t_fieldcat_alv with header line,
        gd_tab_group  type slis_t_sp_group_alv,
        gd_layout     type slis_layout_alv,
        gd_repid      like sy-repid,
        gt_events     type slis_t_event,
        gd_prntparams type slis_print_alv,
        wa_event      TYPE slis_alv_event.

************************************************************************
*Start-of-selection.
START-OF-SELECTION.

  BREAK 10115.
  perform data_retrieval.
  if it_final[] IS INITIAL.
    MESSAGE 'Data is not found' TYPE 'I'.
    LEAVE LIST-PROCESSING.
  ENDIF.
  perform build_FS_FCAT.
  perform build_layout.
  perform build_events.
  perform build_print_params.
  perform display_alv_report.


    SELECTION-SCREEN BEGIN OF BLOCK a1 WITH FRAME TITLE text-001 .
*    SELECT-OPTIONS s_purch for ekbe-ebeln.  ""Purchase Document
     SELECT-OPTIONS s_mat  FOR wa_afko-plnbez."" OBLIGATORY.      "Posting Date
  SELECTION-SCREEN END OF BLOCK a1.


*  *&---------------------------------------------------------------------*
*&      Form  BUILD_FS_FCAT
*&---------------------------------------------------------------------*
*       Build FS_FCAT for ALV Report
*----------------------------------------------------------------------*
form build_FS_FCAT.
*
*aufnr TYPE afko-aufnr,
*          aufpl TYPE afru-aufpl,
*          rueck TYPE afru-rueck,
*          gmnga TYPE afru-gmnga,
*          vornr
  DATA : RS_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
*  fieldcatalog-fieldname   = 'AUFNR'.
*  fieldcatalog-seltext_m   = 'Order'.
*  fieldcatalog-col_pos     = 1.
*  fieldcatalog-emphasize   = 'X'.
*  fieldcatalog-key         = 'X'.
*  APPEND fieldcatalog TO fieldcatalog.
*  clear  fieldcatalog.


  fieldcatalog-fieldname   = 'PLNBEZ'.
  fieldcatalog-seltext_M   = 'Part Code'.
  fieldcatalog-col_pos     = 1.
  fieldcatalog-emphasize   = 'X'.
  APPEND fieldcatalog TO fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'MAKTX'.
  fieldcatalog-seltexT_M   = 'Part Desc.'.
  fieldcatalog-col_pos     = 2.
  fieldcatalog-emphasize   = 'X'.
  fieldcatalog-key         = 'X'.
*  fieldcatalog-do_sum      = 'X'.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'GAMNG'.
  fieldcatalog-seltext_M   = 'Target QTY'.
  fieldcatalog-col_pos     = 3.
  fieldcatalog-emphasize   = 'X'.
  fieldcatalog-key         = 'X'.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.


  fieldcatalog-fieldname   = 'LABST'.
  fieldcatalog-seltext_M   = 'Op. Stock'.
  fieldcatalog-col_pos     =  4.
  fieldcatalog-emphasize   = 'X'.
  fieldcatalog-key         = 'X'.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

*    fieldcatalog-fieldname   = 'PLNBEZ'.
*  fieldcatalog-seltext_M   = 'MATERIAL'.
*  fieldcatalog-col_pos     =  6.
*  fieldcatalog-emphasize   = 'X'.
*  fieldcatalog-key         = 'X'.
*  append fieldcatalog to fieldcatalog.
*  clear  fieldcatalog.
*
*  fieldcatalog-fieldname   = 'PLNME'.
*  fieldcatalog-seltext_M   = 'UNIT'.
*  fieldcatalog-col_pos     =  7.
*  fieldcatalog-emphasize   = 'X'.
*  fieldcatalog-key         = 'X'.
*  append fieldcatalog to fieldcatalog.
*  clear  fieldcatalog.
*
*    fieldcatalog-fieldname   = 'ERNAM'.
*  fieldcatalog-seltext_M   = 'ENTERED BY'.
*  fieldcatalog-col_pos     =  8.
*  fieldcatalog-emphasize   = 'X'.
*  fieldcatalog-key         = 'X'.
*  append fieldcatalog to fieldcatalog.
*  clear  fieldcatalog.




endform.                    " BUILD_FS_FCAT


*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
*       Build layout for ALV grid report
*----------------------------------------------------------------------*
form build_layout.
  wa_event-name = slis_ev_top_of_page.
  wa_event-form = 'TOP_OF_PAGE'.
  APPEND wa_event TO gt_events.
  CLEAR wa_event.
  wa_event-name = slis_ev_user_command.
  wa_event-form = 'USER_COMMAND'.
  APPEND wa_event TO gt_events.
  CLEAR wa_event.
  gd_layout-colwidth_optimize = 'X'.
  gd_layout-zebra = 'X'.
endform.                    " BUILD_LAYOUT


*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*       Display report using ALV grid
*----------------------------------------------------------------------*
form display_alv_report.
  gd_repid = sy-repid.
  call function 'REUSE_ALV_GRID_DISPLAY'
       exporting
            i_callback_program      = gd_repid
            i_callback_top_of_page   = 'TOP-OF-PAGE'  "see FORM
            i_callback_user_command = 'USER_COMMAND'
*            i_callback_pf_status_set = 'SET_PF_STATUS'
*            i_grid_title           = outtext
            is_layout               = gd_layout
            it_fieldcat             = fieldcatalog[]
*            it_special_groups       = gd_tabgroup
            it_events               = gt_events
            is_print                = gd_prntparams
            i_save                  = 'X'
*            is_variant              = z_template
       tables
            t_outtab                = it_final
       exceptions
            program_error           = 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.
endform.                    " DISPLAY_ALV_REPORT
*----------------------------------------------------------*
*       FORM SET_PF_STATUS                                 *
*----------------------------------------------------------*
*FORM set_pf_status USING rt_extab TYPE slis_t_extab.
*  SET PF-STATUS 'SAVE'.
*                  "Copy of 'STANDARD' pf_status from fgroup SALV
*ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*       Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
form data_retrieval.

BREAK 10115.

*SELECT A~AUFNR A~PLNBEZ A~GMEIN A~GAMNG B~MATNR B~LABST B~INSME C~TRAME D~MAKTX INTO TABLE IT_final
*        FROM AFKO  AS A INNER JOIN MARD AS B ON A~PLNBEZ = B~MATNR
*                        INNER JOIN MARCH AS C ON A~PLNBEZ = C~MATNR
*                        INNER JOIN MAKT AS D ON A~PLNBEZ = D~MATNR
*                        WHERE PLNBEZ IN S_MAT.
BREAK 10115.
   SELECT aufnr
          plnbez
          gmein
          gamng
          FROM afko
          INTO TABLE it_afko1
          WHERE plnbez in s_mat.
  SORT it_afko DESCENDING by  plnbez.



IF  IT_AFKO1[] IS NOT INITIAL.
  SELECT aufnr
         objnr
         FROM aufk
         INTO TABLE it_aufk1
         FOR ALL ENTRIES IN it_afko1
         WHERE aufnr = it_afko1-aufnr.
  SORT it_aufk DESCENDING by objnr.
ENDIF.


IF  it_aufk1[] IS NOT INITIAL.
  SELECT objnr
         stat
         FROM jest
         INTO TABLE it_jest
         FOR ALL ENTRIES IN it_aufk1
         WHERE objnr  = it_aufk1-objnr
         AND   stat = 'I0002' AND CHGNR = '001' AND INACT ne 'X'. ""
  SORT it_aufk DESCENDING by objnr.
ENDIF.
*LOOP AT it_jest INTO wa_jest.
IF  it_jest[] IS NOT INITIAL.
  SELECT aufnr
         objnr
         FROM aufk
         INTO TABLE  it_aufk
         FOR ALL ENTRIES IN it_jest
         WHERE objnr = it_jest-objnr.
ENDif.
*    APPEND wa_aufk to  it_aufk.
*    ENDSELECT.
*ENDloop.

*LOOP AT it_aufk INTO wa_aufk.
IF  it_aufk[] IS NOT INITIAL.
   SELECT aufnr
          plnbez
          gmein
          gamng
          FROM afko
          INTO TABLE it_afko
          FOR ALL ENTRIES IN it_aufk
          WHERE aufnr = it_aufk-aufnr.
 ENDif.
*     APPEND wa_afko to it_afko.
*     ENDSELECT.
*ENDloop.
SORT it_afko DESCENDING by  plnbez.
*DELETE ADJACENT DUPLICATES FROM it_afko.

*
*IF  it_aufk[] IS NOT INITIAL.
*LOOP AT it_aufk INTO wa_aufk.
*  READ TABLE it_afko INTO wa_afko INDEX sy-tabix.
*SELECT  sum( gamng ) AS gamng
*        FROM afko
*        INTO wa_afko-gamng
*        WHERE aufnr = wa_aufk-aufnr.
*    MODIFY it_afko INDEX sy-tabix FROM wa_afko
*  TRANSPORTING gamng.
*endloop.
*ENDIF.
*DELETE ADJACENT DUPLICATES FROM it_afko COMPARING plnbez.

BREAK 10115.
IF  it_afko[] IS NOT INITIAL.
LOOP AT it_afko INTO wa_afko.
SELECT SUM( labst ) AS labst
       SUM( insme ) AS insme
       FROM mard
       INTO CORRESPONDING FIELDS OF wa_mard
       WHERE matnr = wa_afko-plnbez.
APPEND wa_mard to  it_mard.
ENDloop.


*SELECT matnr
*       labst
*       insme
*       FROM mard INTO TABLE it_mard
*       FOR ALL ENTRIES IN it_afko
*       WHERE matnr = it_afko-plnbez.
LOOP AT it_afko INTO wa_afko.
SELECT SUM( trame ) as trame
       FROM march INTO CORRESPONDING FIELDS of wa_march
       WHERE matnr = wa_afko-plnbez.
  APPEND wa_march to it_march.
ENDloop.
ENDIF.


IF  it_mard[] IS NOT INITIAL.
LOOP AT it_mard INTO wa_mard.
  READ TABLE it_march INTO wa_march INDEX sy-tabix.
      wa_mard-labst = wa_mard-labst + wa_mard-insme + wa_march-trame.
      MODIFY it_mard  INDEX sy-tabix FROM wa_mard
   TRANSPORTING labst.
ENDLOOP.
ENDIF.

SELECT matnr
       maktx
       FROM makt INTO TABLE it_makt
       FOR ALL ENTRIES IN it_afko
       WHERE matnr = it_afko-plnbez.



LOOP AT it_afko INTO wa_afko.
    MOVE wa_afko-aufnr to  wa_final-aufnr.
    MOVE wa_afko-plnbez to  wa_final-plnbez.
    MOVE wa_afko-gmein to  wa_final-gmein.
    MOVE wa_afko-gamng to  wa_final-gamng.
 READ TABLE it_mard INTO wa_mard INDEX sy-tabix.
    MOVE wa_mard-labst to  wa_final-labst.
READ TABLE it_makt INTO wa_makt INDEX sy-tabix.
    MOVE wa_makt-maktx to   wa_final-maktx.
READ TABLE it_jest INTO wa_jest INDEX sy-tabix.
    MOVE wa_jest-stat to   wa_final-stat.
    APPEND wa_final to   it_final.
  ENDLOOP.
*DElete ADJACENT DUPLICATES FROM it_final COMPARING plnbez.


IF  it_final[] IS NOT INITIAL.
LOOP AT it_final INTO wa_final.
*  READ TABLE it_afko INTO wa_afko INDEX sy-tabix.
SELECT  sum( gamng ) AS gamng
        FROM afko
        INTO wa_final-gamng
        WHERE plnbez = wa_final-plnbez AND aufnr = ''.
*        WHERE wa_final-stat = 'I0002'.
    MODIFY it_final INDEX sy-tabix FROM wa_final
  TRANSPORTING gamng.
endloop.
ENDIF.
SORT it_final DESCENDING by plnbez.
DElete ADJACENT DUPLICATES FROM it_final COMPARING plnbez.
*LOOP AT it_mkpf INTO wa_mkpf.
*    wa_final-CPUDT = wa_mkpf-CPUDT.
*    MODIFY it_final INDEX sy-tabix FROM wa_final
*    TRANSPORTING CPUDT.
*    APPEND wa_final to it_final.
* ENDLOOP.



endform.                    " DATA_RETRIEVAL


*-------------------------------------------------------------------*
* Form  TOP-OF-PAGE                                                 *
*-------------------------------------------------------------------*
* ALV Report Header                                                 *
*-------------------------------------------------------------------*
Form top-of-page.
*ALV Header declarations
  data: t_header type slis_t_listheader,
        wa_header type slis_listheader,
        t_line like wa_header-info,
        ld_lines type i,
        ld_linesc(10) type c.

* Title
  wa_header-typ  = 'H'.

  wa_header-info = 'Daily Production Report'.
  append wa_header to t_header.
  clear wa_header.

* Date
  wa_header-typ  = 'S'.
  wa_header-key = 'Date: '.
  CONCATENATE  sy-datum+6(2) '.'
               sy-datum+4(2) '.'
               sy-datum(4) INTO wa_header-info.   "todays date
  append wa_header to t_header.
  clear: wa_header.

* Total No. of Records Selected
  describe table it_final lines ld_lines.
  ld_linesc = ld_lines.
  concatenate 'Total No. of Records Selected: ' ld_linesc
                    into t_line separated by space.
  wa_header-typ  = 'A'.
  wa_header-info = t_line.
  append wa_header to t_header.
  clear: wa_header, t_line.

  call function 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = t_header.
*            i_logo             = 'Z_LOGO'.
endform.                    "top-of-page


*------------------------------------------------------------------*
*       FORM USER_COMMAND                                          *
*------------------------------------------------------------------*
*       --> R_UCOMM                                                *
*       --> RS_SELFIELD                                            *
*------------------------------------------------------------------*
*************************Double click Event**************************
*********************************************************************
FORM user_command USING r_ucomm LIKE sy-ucomm
                  rs_selfield TYPE slis_selfield.
  CASE r_ucomm.
    WHEN '&IC1'.
*   Check field clicked on within ALVgrid report
      IF  rs_selfield-fieldname = 'AWKEY'.
  BREAK 10115.
  data: r_belnr type range of awkey,
      r_belnr_l like line of r_belnr.
*now in the code  replace1 your loop by this:
*loop at it_final into wa_final.
* r_belnr_l-sign = 'I'.
* r_belnr_l-OPTION = 'EQ'.
* r_belnr_l-LOW = wa_final-awkey.
* r_belnr_l-high = ' '.
* append r_belnr_l to r_belnr.
*endloop.
*SUBMIT RM08RELEASE VIA SELECTION-SCREEN WITH SO_BELNR IN r_belnr AND RETURN.
* LOOP AT it_final INTO wa_final.
   ENDIF.
 ENDCASE.
ENDFORM.



*&---------------------------------------------------------------------*
*&      Form  BUILD_EVENTS
*&---------------------------------------------------------------------*
*       Build events table
*----------------------------------------------------------------------*
form build_events.
  data: ls_event type slis_alv_event.

  call function 'REUSE_ALV_EVENTS_GET'
    EXPORTING
      i_list_type = 0
    IMPORTING
      et_events   = gt_events[].
  read table gt_events with key name =  slis_ev_end_of_page
                           into ls_event.
  if sy-subrc = 0.
    move 'END_OF_PAGE' to ls_event-form.
    append ls_event to gt_events.
  endif.

  read table gt_events with key name =  slis_ev_end_of_list
                         into ls_event.
  if sy-subrc = 0.
    move 'END_OF_LIST' to ls_event-form.
    append ls_event to gt_events.
  endif.
endform.                    " BUILD_EVENTS


*&---------------------------------------------------------------------*
*&      Form  BUILD_PRINT_PARAMS
*&---------------------------------------------------------------------*
*       Setup print parameters
*----------------------------------------------------------------------*
form build_print_params.
  gd_prntparams-reserve_lines = '3'.   "Lines reserved for footer
  gd_prntparams-no_coverpage = 'X'.
endform.                    " BUILD_PRINT_PARAMS


*&---------------------------------------------------------------------*
*&      Form  END_OF_PAGE
*&---------------------------------------------------------------------*
form END_OF_PAGE.
  data: listwidth type i,
        ld_pagepos(10) type c,
        ld_page(10)    type c.

  write: sy-uline(50).
  skip.
  write:/40 'Page:', sy-pagno .
endform.                    "END_OF_PAGE

*&---------------------------------------------------------------------*
*&      Form  END_OF_LIST
*&---------------------------------------------------------------------*
form END_OF_LIST.
  data: listwidth type i,
        ld_pagepos(10) type c,
        ld_page(10)    type c.

  skip.
  write:/40 'Page:', sy-pagno .
endform.

No comments:

Post a Comment