Skip to end of metadata
Go to start of metadata

This sample code will give you day wise opening/closing stock of FG materials , so please check material type and Indexes while executing at your end.

This is not ready to use program, please modify  this logic as per your settings.

* Developer------ Prabhakara rao.Peram. *
*----------------------------------------------------------------------*
REPORT yw2stk MESSAGE-ID yw2 LINE-SIZE 231 LINE-COUNT 45
  NO STANDARD PAGE HEADING.
*----------------------------------------------------------------------*
* Type Declaration *
*----------------------------------------------------------------------*
TYPE-POOLS slis.
*----------------------------------------------------------------------*
* Tables *
*----------------------------------------------------------------------*
TABLES: mkpf, " Material Document: Header Data
        mseg, " Material Document: Item Data
        mara,
        mard,
        s031,
        ekko,
        ekpo,
        likp,
        makt,
        j_1iwrkcus,
        t001w,
        wb2_v_mkpf_mseg2,
        mmim_rep_print,
        yw2_stkmovements,
        yplnt,
        mardh.
*----------------------------------------------------------------------*
* Internal Tables *
*----------------------------------------------------------------------*
DATA: i_werks LIKE j_1iwrkcus OCCURS 0 WITH HEADER LINE.
DATA: BEGIN OF i_yplnt OCCURS 0,
  pplnt LIKE yplnt-pplnt,
  wplnt LIKE yplnt-wplnt,
END OF i_yplnt.
DATA: BEGIN OF i_mkpf OCCURS 0,
  mblnr LIKE mkpf-mblnr,
  mjahr LIKE mkpf-mjahr,
  budat LIKE mkpf-budat,
  vgart LIKE mkpf-vgart,
  bwart LIKE mseg-bwart,
  matnr LIKE mseg-matnr,
  werks LIKE mseg-werks,
  lgort LIKE mseg-lgort,
  menge LIKE mseg-menge,
  meins LIKE mseg-meins,
  kunnr LIKE mseg-kunnr,
  zeile LIKE mseg-zeile,
  xauto LIKE mseg-xauto,
  shkzg LIKE mseg-shkzg,
  matnr1 LIKE mseg-matnr,
END OF i_mkpf.
DATA: BEGIN OF i_mardh OCCURS 0,
  werks LIKE mardh-werks,
  meins LIKE mara-meins,
  matnr LIKE mardh-matnr,
  lgort LIKE mardh-lgort,
  labst LIKE mardh-labst,
  lfgja LIKE mardh-lfgja, "Added -MB
  lfmon LIKE mardh-lfmon, "Added -MB
  perio(6),
  insme LIKE mardh-labst,
  einme LIKE mardh-labst,
  speme LIKE mardh-labst,
  retme LIKE mardh-labst,
  o_stk LIKE mardh-labst, " Opening Stock
  c_stk LIKE mardh-labst, " Closing Stock
END OF i_mardh.
DATA: BEGIN OF i_mard OCCURS 0,
  werks LIKE mard-werks,
  matnr LIKE mard-matnr,
  lgort LIKE mard-lgort,
  labst LIKE mard-labst,
  insme LIKE mard-labst,
  meins LIKE mara-meins,
  einme LIKE mard-labst,
  speme LIKE mard-labst,
  retme LIKE mard-labst,
END OF i_mard.
DATA: i_mard1 LIKE i_mard OCCURS 0 WITH HEADER LINE.
DATA: i_mardh1 LIKE i_mardh OCCURS 0 WITH HEADER LINE.
DATA: BEGIN OF i_mkpf1 OCCURS 0,
  mblnr LIKE mkpf-mblnr,
  werks LIKE mseg-werks,
  matnr LIKE mseg-matnr,
  budat LIKE mkpf-budat,
  bwart LIKE mseg-bwart,
  mjahr LIKE mkpf-mjahr,
  vgart LIKE mkpf-vgart,
  lgort LIKE mseg-lgort,
  menge LIKE mseg-menge,
  meins LIKE mseg-meins,
  xauto LIKE mseg-xauto,
  shkzg LIKE mseg-shkzg,
END OF i_mkpf1.
DATA: BEGIN OF i_mkpf2 OCCURS 0,
  werks LIKE mseg-werks,
  matnr LIKE mseg-matnr,
  budat LIKE mkpf-budat,
  bwart LIKE mseg-bwart,
  mjahr LIKE mkpf-mjahr,
  vgart LIKE mkpf-vgart,
  lgort LIKE mseg-lgort,
  menge LIKE mseg-menge,
  meins LIKE mseg-meins,
  xauto LIKE mseg-xauto,
END OF i_mkpf2.
DATA: BEGIN OF i_final5 OCCURS 0,
  werks LIKE mseg-werks, " Plant
  matnr LIKE mseg-matnr, " Material
  lgort LIKE mseg-lgort, " Storage Location
  budat LIKE mkpf-budat, " Posting Date
  mtart LIKE mara-mtart, " Material Type
  spmon LIKE s031-spmon, " Month
  maktx LIKE makt-maktx, " Description
* meins(3), " UOM
  meins LIKE mseg-meins,
  o_stk LIKE mardh-labst, " opening stock
  trecep LIKE mardh-labst, " total receipts
  produ LIKE mardh-labst, " Net Receipts - Production
  recep LIKE mardh-labst, " Net Receipts - Receipts
  sal_ret LIKE mardh-labst, " Net Receipts - Sales Return
  tdisp LIKE mardh-labst, " total dispatches
  custmr LIKE mardh-labst, " Net Dispatches - Customers
  oplant LIKE mardh-labst, " Net Dispatches - To Other Plant
  tloss LIKE mardh-labst, " Total Loss
  trloss LIKE mardh-labst, " Transit Loss
  whloss LIKE mardh-labst, " Warehouse Loss
  c_stk LIKE mardh-labst, " Closing Stock
  trfstk LIKE mardh-labst, "Transfer stock
  menge LIKE mseg-menge,
  othadj LIKE mardh-labst,
END OF i_final5.
DATA: BEGIN OF i_final OCCURS 0,
  werks LIKE mseg-werks, " Plant
  matnr LIKE mseg-matnr, " Material
  budat LIKE mkpf-budat, " Posting Date
  mtart LIKE mara-mtart, " Material Type
  spmon LIKE s031-spmon, " Month
  maktx LIKE makt-maktx, " Description
*   meins(3), " UOM
  meins LIKE mseg-meins,
  o_stk LIKE mardh-labst, " opening stock
  trecep LIKE mardh-labst, " total receipts
  produ LIKE mardh-labst, " Net Receipts - Production
  recep LIKE mardh-labst, " Net Receipts - Receipts
  sal_ret LIKE mardh-labst, " Net Receipts - Sales Return
  tdisp LIKE mardh-labst, " total dispatches
  custmr LIKE mardh-labst, " Net Dispatches - Customers
  oplant LIKE mardh-labst, " Net Dispatches - To Other Plant
  trfstk LIKE mardh-labst, "Material Transfer stock
  trloss LIKE mardh-labst, " Transit Loss
  whloss LIKE mardh-labst, " Warehouse Loss
  tloss LIKE mardh-labst, " Total Loss
  c_stk LIKE mardh-labst, " Closing Stock
  othadj LIKE mardh-labst,
END OF i_final.
DATA: i_final1 LIKE i_final OCCURS 0 WITH HEADER LINE.
DATA: BEGIN OF i_final_temp OCCURS 0,
  werks LIKE mseg-werks, " Plant
  matnr LIKE mseg-matnr, " Material
  mtart LIKE mara-mtart, " Material Type
  maktx LIKE makt-maktx, " Description
  meins LIKE mseg-meins,
  o_stk LIKE mardh-labst, " opening stock
  trecep LIKE mardh-labst, " total receipts
  produ LIKE mardh-labst, " Net Receipts - Production
  recep LIKE mardh-labst, " Net Receipts - Receipts
  sal_ret LIKE mardh-labst, " Net Receipts - Sales Return
  tdisp LIKE mardh-labst, " total dispatches
  custmr LIKE mardh-labst, " Net Dispatches - Customers
  oplant LIKE mardh-labst, " Net Dispatches - To Other Plant
  trfstk LIKE mardh-labst, "Material Transfer stock
  trloss LIKE mardh-labst, " Transit Loss
  whloss LIKE mardh-labst, " Warehouse Loss
  tloss LIKE mardh-labst, " Total Loss
  c_stk LIKE mardh-labst, " Closing Stock
  othadj LIKE mardh-labst,
END OF i_final_temp.
* For Materials
DATA: BEGIN OF i_mara OCCURS 0,
  matnr TYPE mara-matnr,
  mtart TYPE mara-mtart,
  meins LIKE mara-meins,
  labst TYPE mard-labst,
  maktx LIKE makt-maktx,
END OF i_mara.
DATA: BEGIN OF i_stkmvmnts OCCURS 0,
  bwart LIKE mseg-bwart,
  shkzg LIKE mseg-shkzg,
  vzbew LIKE yw2_stkmovements-vzbew,
END OF i_stkmvmnts.
DATA: BEGIN OF i_finalt OCCURS 0,
  werks LIKE mseg-werks, " Plant
  matnr LIKE mseg-matnr, " Material
  budat LIKE mkpf-budat, " Posting Date
  mtart LIKE mara-mtart, " Material Type
  spmon LIKE s031-spmon, " Month
  maktx LIKE makt-maktx, " Description
*   meins(3), " UOM
  meins LIKE mseg-meins,
  o_stk LIKE mardh-labst, " opening stock
  trecep LIKE mardh-labst, " total receipts
  produ LIKE mardh-labst, " Net Receipts - Production
  recep LIKE mardh-labst, " Net Receipts - Receipts
  sal_ret LIKE mardh-labst, " Net Receipts - Sales Return
  tdisp LIKE mardh-labst, " total dispatches
  custmr LIKE mardh-labst, " Net Dispatches - Customers
  oplant LIKE mardh-labst, " Net Dispatches - To Other Plant
  trfstk LIKE mardh-labst, "Material Transfer stock
  trloss LIKE mardh-labst, " Transit Loss
  whloss LIKE mardh-labst, " Warehouse Loss
  tloss LIKE mardh-labst, " Total Loss
  c_stk LIKE mardh-labst, " Closing Stock
  othadj LIKE mardh-labst,
  month(8) ,
END OF i_finalt.
DATA: imkpft LIKE i_mkpf OCCURS 0 WITH HEADER LINE.
DATA: imkpft1 LIKE i_mkpf1 OCCURS 0 WITH HEADER LINE.
DATA: imardt LIKE i_mard OCCURS 0 WITH HEADER LINE.
DATA: imardht LIKE i_mardh OCCURS 0 WITH HEADER LINE.
DATA: t_final LIKE i_mkpf OCCURS 0 WITH HEADER LINE.
DATA: imkpft2 LIKE i_mkpf OCCURS 0 WITH HEADER LINE.
* FCAT is used for the field catalog
DATA: fcat TYPE TABLE OF slis_fieldcat_alv WITH NON-UNIQUE DEFAULT KEY
        WITH HEADER LINE INITIAL SIZE 0,
*     for excluding the ICONs from the application toolbar
      fexc TYPE TABLE OF slis_extab WITH NON-UNIQUE DEFAULT KEY
        WITH HEADER LINE INITIAL SIZE 0,
*     FS_LAYO is used for Grid Layout
      fs_layo TYPE slis_layout_alv,
*     FEVENTS to handle the events TOP OF PAGE & USER_COMMAND
      fevents TYPE TABLE OF slis_alv_event WITH NON-UNIQUE DEFAULT KEY
        WITH HEADER LINE INITIAL SIZE 0,
*     FHEADER is used for List header
      fheader TYPE TABLE OF slis_listheader WITH NON-UNIQUE DEFAULT KEY
        WITH HEADER LINE INITIAL SIZE 0,
*       sort is used for sorting
      fsort TYPE TABLE OF slis_sortinfo_alv WITH NON-UNIQUE DEFAULT KEY
        WITH HEADER LINE INITIAL SIZE 0,
      fcat1 TYPE TABLE OF slis_fieldcat_alv WITH NON-UNIQUE DEFAULT KEY
        WITH HEADER LINE INITIAL SIZE 0,
      fs_layo1 TYPE slis_layout_alv,
      gt_list_top_of_page1 TYPE slis_t_listheader,
      fevents1 TYPE TABLE OF slis_alv_event WITH NON-UNIQUE DEFAULT KEY
        WITH HEADER LINE INITIAL SIZE 0,
      fheader1 TYPE TABLE OF slis_listheader WITH NON-UNIQUE DEFAULT KEY
        WITH HEADER LINE INITIAL SIZE 0,
      g_statu_071 TYPE slis_formname VALUE 'Z_PFSTATUS',
      alv_variant1 LIKE disvariant.
*----------------------------------------------------------------------*
* Variable Declaration *
*----------------------------------------------------------------------*
TYPES: trff_type_dec_6_5(6) TYPE p DECIMALS 5.
DATA: fyear(4),
      mon(2),
      fyear1(4),
      mon1(2),
      obal LIKE mard-labst,
      cbal LIKE mard-labst,
      index TYPE i,
      count,
      count1 TYPE i,
      o_stk TYPE p DECIMALS 3,
      c_stk TYPE p DECIMALS 3,
      v_mjahr LIKE mkpf-mjahr,
      months TYPE trff_type_dec_6_5,
      month TYPE i.
* Global variables for handling ALV functionality
DATA: alv_keyinfo TYPE slis_keyinfo_alv,
      alv_variant LIKE disvariant,
      alv_layout TYPE slis_layout_alv,
      alv_repid LIKE sy-repid,
      alv_print TYPE slis_print_alv,
      alv_detail_func(30),
      alv_default_variant LIKE disvariant-variant,
      alv_colourize_fields LIKE mmim_rep_print-color.
RANGES: r_budat FOR mkpf-budat.
*Added by Prabhu for year on 26.4.05.
DATA: idate LIKE r_budat OCCURS 0 WITH HEADER LINE.
*----------------------------------------------------------------------*
* Selection Screen Elements *
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK blk WITH FRAME TITLE text-001.
SELECT-OPTIONS: s_werks FOR mard-werks OBLIGATORY
NO INTERVALS.
PARAMETER: p_spmon LIKE s031-spmon NO-DISPLAY .
SELECT-OPTIONS: s_matnr FOR mara-matnr OBLIGATORY,
                s_lgort FOR mseg-lgort NO-EXTENSION NO INTERVALS,
                s_mblnr FOR mkpf-mblnr,
                s_budat FOR mkpf-budat OBLIGATORY .
SELECTION-SCREEN END OF BLOCK blk.
SELECTION-SCREEN BEGIN OF BLOCK blk3 WITH FRAME TITLE text-004.
PARAMETER : mtart LIKE mara-mtart
DEFAULT 'FERT' NO-DISPLAY.
SELECTION-SCREEN END OF BLOCK blk3.
SELECTION-SCREEN BEGIN OF BLOCK blk2 WITH FRAME TITLE text-003.
SELECTION-SCREEN END OF BLOCK blk2.
* ADDED BY PRABHU FOR DAY-WISE REPORT.
SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE text-007.
PARAMETERS: d1 RADIOBUTTON GROUP p1 DEFAULT 'X',
            m1 RADIOBUTTON GROUP p1,
            y1 RADIOBUTTON GROUP p1.
SELECTION-SCREEN END OF BLOCK b3.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-006.
PARAMETERS: alv_def LIKE disvariant-variant.
SELECTION-SCREEN END OF BLOCK b2.
DATA: s_budat1 LIKE s_budat OCCURS 0 WITH HEADER LINE."prabhu
*----------------------------------------------------------------------*
* Initialization *
*----------------------------------------------------------------------*
INITIALIZATION.
  PERFORM alv_init.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR alv_def.
  PERFORM alv_f4.
*----------------------------------------------------------------------
*
* At Selection Screen
*
*----------------------------------------------------------------------
*
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_spmon.
* PERFORM monat_f4.
*
*----------------------------------------------------------------------*
* At Selection Screen *
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
* checking for the layout
  PERFORM alv_check.
* authorisation check for the Plant
* PERFORM auth_check.
* Validation for the Plant
  PERFORM validity_check.
  IF mtart NE 'FERT'.
    MESSAGE e041 WITH 'Material Type must be FERT Only...'.
  ENDIF.
  IF d1 = 'X'." On 26.4.05.
    p_spmon+0(4) = s_budat-low+0(4).
    p_spmon+4(2) = s_budat-low+4(2).
  ELSE.
    p_spmon+0(4) = s_budat-low+0(4).
    p_spmon+4(2) = s_budat-low+4(2).
  ENDIF.
  LOOP AT s_budat.
    IF s_budat-high IS INITIAL.
      s_budat-high = s_budat-low.
      MODIFY s_budat.
    ENDIF.
  ENDLOOP.
  idate-low = s_budat-low.
  idate-high = s_budat-high.
  CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
    EXPORTING
      i_date_from = idate-low
      i_date_to   = idate-high
    IMPORTING
      e_months    = month.
  DATA: i(3) TYPE c.
  i = s_budat-low+4(2).
  CLEAR: r_budat.
  REFRESH: r_budat.
*added by Prabhu for Only for Oneday.on 18.5.5
  IF month EQ '0'.
    month = month + 1.
  ENDIF.
*added by Prabhu for Only for Oneday.on 18.5.5
  DO month TIMES.
    r_budat-low = s_budat-low.
    APPEND r_budat.
  ENDDO.
  i = 0.
  LOOP AT r_budat.
    r_budat-low+4(2) = s_budat-low+4(2) + i.
    i = i + 1.
    r_budat-low+6(2) = '01'.
    MODIFY r_budat.
  ENDLOOP.
  LOOP AT r_budat.
    CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'
      EXPORTING
        day_in            = r_budat-low
      IMPORTING
        last_day_of_month = r_budat-high.
    MODIFY r_budat.
  ENDLOOP.
  LOOP AT r_budat.
    IF r_budat-low+4(2) = s_budat-low+4(2).
      r_budat-low = s_budat-low.
      MODIFY r_budat.
    ENDIF.
    IF r_budat-high+4(2) = s_budat-high+4(2).
      r_budat-high = s_budat-high.
      MODIFY r_budat.
    ENDIF.
* For Summary on 26.4.05.
    IF y1 = 'X'.
      CLEAR r_budat.
      REFRESH r_budat.
      r_budat-low = s_budat-low.
      r_budat-high = s_budat-high.
      APPEND r_budat.
      CLEAR r_budat.
    ENDIF.
  ENDLOOP.
*----------------------------------------------------------------------*
* At Selection Screen *
*----------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
*----------------------------------------------------------------------*
* Start of Selection *
*----------------------------------------------------------------------*
START-OF-SELECTION.
  v_mjahr = p_spmon+0(4).
* Get plant distinction warehouse/production
  PERFORM get_plant_distinction.
* Collect the data from various tables
  PERFORM getdata_fg_stock.
* here the number of rows in the output table is found
  PERFORM output_table_check.
* here the top of the page code is written, that is to be displayed
* in the output
  PERFORM z_top_of_page.
* here ALV layout properties are set
  PERFORM z_layout_settings.
* ALV EVENTS for TOP OF PAGE and for USER COMMAND
  PERFORM z_alv_events.
* The field catalog is defined for the Primary List is defined in
* the subroutine CREATE_FIELD_CATALOG include program ZPRRDOCR_FCAT
  PERFORM z_create_field_catalog.
* This is for displaying the output
  PERFORM z_reuse_alv_grid_display.
*&---------------------------------------------------------------------*
*& Form getdata_fg_stock
*&---------------------------------------------------------------------*
* Getting data from standard tables
*----------------------------------------------------------------------*
FORM getdata_fg_stock.
* For getting the Start date & end date of the month
* PERFORM get_month_dates.
* Getting the Opening Stock from MARDH table
  IF mon EQ '01'.
    mon1 = mon.
    fyear1 = fyear.
    mon = '12'.
    fyear = fyear - 1.
  ELSE.
    mon1 = mon.
    fyear1 = fyear.
    mon = mon - 1.
    fyear = fyear.
  ENDIF.
  PERFORM get_records_from_db.
*added for Month Summary on 26.4.05.
  LOOP AT r_budat.
    s_budat-low = r_budat-low.
    s_budat-high = r_budat-high.
*for Month
    p_spmon+0(4) = s_budat-low+0(4).
    p_spmon+4(2) = s_budat-low+4(2).
*for summary.
    IF y1 = 'X'.
      LOOP AT s_budat.
        s_budat1-sign = 'I'.
        s_budat1-option = 'NB'.
        s_budat1-low = s_budat-low.
        s_budat1-high = s_budat-high.
        APPEND s_budat1.
        CLEAR s_budat1.
      ENDLOOP.
    ENDIF.
    imkpft[] = i_mkpf[].
    imkpft2[] = i_mkpf[].
    imardt[] = i_mard[].
    imardht[] = i_mardh[].
    PERFORM month_wise.
    PERFORM process_movements.
    PERFORM calculate_opening_stock.
    PERFORM update_non_transaction_itms.
    PERFORM delete_empty_records.
    CLEAR: imardht,imardt,imkpft1,imkpft,i_final,i_final5.
    REFRESH: imardht,imardt,imkpft1,i_final,i_final5,imkpft.
  ENDLOOP.
  CLEAR: r_budat.
  REFRESH: r_budat.
*end of changes for month.
ENDFORM. " getdata_fg_stock
*---------------------------------------------------------------------*
* FORM MONAT_F4 *
*---------------------------------------------------------------------*
* F4-Hilfe für Monat *
*---------------------------------------------------------------------*
FORM monat_f4.
  DATA: BEGIN OF mf_dynpfields OCCURS 1.
          INCLUDE STRUCTURE dynpread.
  DATA: END OF mf_dynpfields.
  DATA: mf_returncode LIKE sy-subrc,
  mf_monat LIKE isellist-month,
  mf_hlp_repid LIKE sy-repid.
  FIELD-SYMBOLS: <mf_feld>.
* Wert von Dynpro lesen
  GET CURSOR FIELD mf_dynpfields-fieldname.
  APPEND mf_dynpfields.
  mf_hlp_repid = sy-repid.
  DO 2 TIMES.
    CALL FUNCTION 'DYNP_VALUES_READ'
      EXPORTING
        dyname               = mf_hlp_repid
        dynumb               = sy-dynnr
      TABLES
        dynpfields           = mf_dynpfields
      EXCEPTIONS
        invalid_abapworkarea = 01
        invalid_dynprofield  = 02
        invalid_dynproname   = 03
        invalid_dynpronummer = 04
        invalid_request      = 05
        no_fielddescription  = 06
        undefind_error       = 07.
    IF sy-subrc = 3.
* Aktuelles Dynpro ist Wertemengenbild
      mf_hlp_repid = 'SAPLALDB'.
    ELSE.
      READ TABLE mf_dynpfields INDEX 1.
* Unterstriche durch Blanks ersetzen
      TRANSLATE mf_dynpfields-fieldvalue USING '_ '.
      EXIT.
    ENDIF.
  ENDDO.
  IF sy-subrc = 0.
* Konvertierung ins interne Format
    CALL FUNCTION 'CONVERSION_EXIT_PERI_INPUT'
      EXPORTING
        input         = mf_dynpfields-fieldvalue
      IMPORTING
        output        = mf_monat
      EXCEPTIONS
        error_message = 1.
    IF mf_monat IS INITIAL.
* Monat ist initial => Vorschlagswert aus akt. Datum ableiten
      mf_monat = sy-datlo(6).
    ENDIF.
    CALL FUNCTION 'POPUP_TO_SELECT_MONTH'
      EXPORTING
        actual_month               = mf_monat
      IMPORTING
        selected_month             = mf_monat
        return_code                = mf_returncode
      EXCEPTIONS
        factory_calendar_not_found = 01
        holiday_calendar_not_found = 02
        month_not_found            = 03.
    IF sy-subrc = 0 AND mf_returncode = 0.
* ASSIGN (MF_DYNPFIELDS-FIELDNAME) TO <MF_FELD>. " ==>> note 148804
* <MF_FELD> = MF_MONAT.
      CALL FUNCTION 'CONVERSION_EXIT_PERI_OUTPUT'
        EXPORTING
          input  = mf_monat
        IMPORTING
          output = mf_dynpfields-fieldvalue.
      COLLECT mf_dynpfields.
      CALL FUNCTION 'DYNP_VALUES_UPDATE'
        EXPORTING
          dyname               = mf_hlp_repid
          dynumb               = sy-dynnr
        TABLES
          dynpfields           = mf_dynpfields
        EXCEPTIONS
          invalid_abapworkarea = 01
          invalid_dynprofield  = 02
          invalid_dynproname   = 03
          invalid_dynpronummer = 04
          invalid_request      = 05
          no_fielddescription  = 06
          undefind_error       = 07. "<<== note 148804
    ENDIF.
  ENDIF.
ENDFORM. "MONAT_F4
*&---------------------------------------------------------------------*
*& Form get_month_dates
*&---------------------------------------------------------------------*
* Calculating the Month Start & End Date
*----------------------------------------------------------------------*
FORM get_month_dates.
  IF m1 = 'X'.
    fyear = p_spmon+0(4).
    mon = p_spmon+4(2).
    CONCATENATE fyear mon '01' INTO r_budat-low.
    r_budat-sign = 'I'.
    r_budat-option = 'BT'.
    CALL FUNCTION 'BKK_GET_MONTH_LASTDAY'
      EXPORTING
        i_date = r_budat-low
      IMPORTING
        e_date = r_budat-high.
    APPEND r_budat.
    CLEAR s_budat.
    REFRESH s_budat.
    s_budat-sign = 'I'.
    s_budat-option = 'BT'.
    s_budat-low = r_budat-low.
    s_budat-high = r_budat-high.
    APPEND s_budat.
  ELSE.
    fyear = p_spmon+0(4).
    mon = p_spmon+4(2).
    CONCATENATE fyear mon '01' INTO r_budat-low.
    r_budat-sign = 'I'.
    r_budat-option = 'BT'.
    CALL FUNCTION 'BKK_GET_MONTH_LASTDAY'
      EXPORTING
        i_date = r_budat-low
      IMPORTING
        e_date = r_budat-high.
    APPEND r_budat.
  ENDIF.
ENDFORM. " get_month_dates
*&---------------------------------------------------------------------*
*& Form output_table_Check
*&---------------------------------------------------------------------*
* checking for records for output
*----------------------------------------------------------------------*
FORM output_table_check .
  DESCRIBE TABLE i_finalt LINES index.
  IF index EQ 0.
    MESSAGE i041 WITH text-005.
    LEAVE LIST-PROCESSING.
  ENDIF.
ENDFORM. "OUTPUT_TABLE_CHECK
*&---------------------------------------------------------------------*
*& Form Z_TOP_OF_PAGE
*&---------------------------------------------------------------------*
* for setting the details in the top of page *
*----------------------------------------------------------------------*
* has no formal paramters *
*----------------------------------------------------------------------*
FORM z_top_of_page.
  DATA: v_mon(2),
  v_yr(40),
  v_fin(18),
  v_fin1(48),
  low(10),
  high(10).
  v_mon = p_spmon+4(2).
  v_yr = p_spmon+0(4).
  fheader-typ = 'H'.
  fheader-info = 'Stock Register Report (FG Stock)'.
  APPEND fheader.
  CLEAR fheader.
*if m1 = 'X'.
* CONCATENATE 'Month = ' v_mon '.' v_yr INTO v_fin.
* fheader-typ = 'H'.
* fheader-info = v_fin.
* APPEND fheader.
* CLEAR fheader.
*endif."prabhu on 18.5.5
  IF d1 = 'X'.
    CLEAR s_budat.
    LOOP AT s_budat.
      CONCATENATE s_budat-low+6(2) '/' s_budat-low+4(2) '/'
      s_budat-low+0(4) INTO low.
      CONCATENATE s_budat-high+6(2) '/' s_budat-high+4(2) '/'
      s_budat-high+0(4) INTO high.
      CONCATENATE 'Date = ' low ' - ' high INTO v_fin1.
      fheader-typ = 'H'.
      fheader-info = v_fin1.
      APPEND fheader.
      CLEAR fheader.
    ENDLOOP.
  ENDIF.
ENDFORM. " Z_TOP_OF_PAGE
*&---------------------------------------------------------------------*
*& Form Z_LAYOUT_SETTINGS
*&---------------------------------------------------------------------*
* this is done for setting the properties for the layout of the *
* grid *
*----------------------------------------------------------------------*
* has no formal paramters *
*----------------------------------------------------------------------*
FORM z_layout_settings.
  fs_layo-zebra = 'X'. " Output in Zebra pattern
  fs_layo-detail_popup = 'X'. " A popup window appears to give
  fs_layo-detail_titlebar = text-022.
  fs_layo-colwidth_optimize = 'X'.
ENDFORM. " Z_LAYOUT_SETTINGS
*&---------------------------------------------------------------------*
*& Form Z_ALV_EVENTS
*&---------------------------------------------------------------------*
* This is used for handling the events TOP OF PAGE and the USER *
* COMMAND event *
*----------------------------------------------------------------------*
* has no formal paramters *
*----------------------------------------------------------------------*
FORM z_alv_events.
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    EXPORTING
      i_list_type = 0
    IMPORTING
      et_events   = fevents[].
  READ TABLE fevents WITH KEY name = 'TOP_OF_PAGE'.
  IF sy-subrc = 0.
    fevents-form = 'Z_TOPOFPAGE'.
    MODIFY fevents INDEX sy-tabix.
    CLEAR fevents.
  ENDIF.
  READ TABLE fevents WITH KEY name = 'USER_COMMAND'.
  IF sy-subrc = 0.
    fevents-form = 'Z_USER_COMMAND'.
    MODIFY fevents INDEX sy-tabix.
    CLEAR fevents.
  ENDIF.
ENDFORM. "Z_ALV_EVENTS
*&---------------------------------------------------------------------*
*& Form Z_CREATE_FIELD_CATALOG
*&---------------------------------------------------------------------*
* here the field catalog is created for the primary list *
*----------------------------------------------------------------------*
* no formal parameters *
*----------------------------------------------------------------------*
FORM z_create_field_catalog.
* for the Plant
  fcat-fieldname = 'WERKS'.
  fcat-key = 'X'.
  fcat-outputlen = '000005'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Plant'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'C'.
  fcat-datatype = 'CHAR'.
  APPEND fcat.
  CLEAR fcat.
* for the Material Type
  fcat-fieldname = 'MTART'.
  fcat-key = 'X'.
  fcat-outputlen = '000006'.
  fcat-just = 'L'.
  fcat-seltext_m = 'MatTyp'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'C'.
  fcat-datatype = 'CHAR'.
  APPEND fcat.
  CLEAR fcat.
* for the Material No.
  fcat-fieldname = 'MATNR'.
  fcat-key = 'X'.
* fcat-hotspot = 'X'.
  fcat-outputlen = '000018'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Material'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'C'.
  fcat-datatype = 'CHAR'.
  APPEND fcat.
  CLEAR fcat.
* for the Material Description
  fcat-fieldname = 'MAKTX'.
  fcat-key = ''.
  fcat-outputlen = '000040'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Description'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'C'.
  fcat-datatype = 'CHAR'.
  APPEND fcat.
  CLEAR fcat.
* for the Unit of Measure
  fcat-fieldname = 'MEINS'.
  fcat-key = ''.
  fcat-outputlen = '03'.
  fcat-just = 'C'.
  fcat-seltext_m = 'UOM'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'C'.
  fcat-datatype = 'UNIT'.
  APPEND fcat.
  CLEAR fcat.
* for the Plant
  fcat-fieldname = 'MONTH'.
  fcat-key = 'X'.
  fcat-outputlen = '08'.
  fcat-just = 'L'.
  fcat-seltext_m = 'MONTH'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'C'.
  fcat-datatype = 'CHAR'.
  APPEND fcat.
  CLEAR fcat.
* for the Opening Stock
  fcat-fieldname = 'C_STK'.
  fcat-hotspot = ' '.
  fcat-outputlen = '000016'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Opening Stock'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'Q'.
  fcat-datatype = 'QUAN'.
* fcat-do_sum = 'X'.
  fcat-just = 'R'.
  fcat-no_zero = 'X'.
  APPEND fcat.
  CLEAR fcat.
* for the Total Receipts
  fcat-fieldname = 'TRECEP'.
  fcat-hotspot = ' '.
  fcat-outputlen = '000016'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Total Receipts'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'Q'.
  fcat-datatype = 'QUAN'.
  fcat-do_sum = 'X'.
  fcat-just = 'R'.
  fcat-no_zero = 'X'.
  APPEND fcat.
  CLEAR fcat.
* for the Production
  fcat-fieldname = 'PRODU'.
  fcat-hotspot = ' '.
  fcat-outputlen = '000016'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Production'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'Q'.
  fcat-datatype = 'QUAN'.
  fcat-do_sum = 'X'.
  fcat-just = 'R'.
  fcat-no_zero = 'X'.
  APPEND fcat.
  CLEAR fcat.
* for the Other Plant Receipts
  fcat-fieldname = 'RECEP'.
  fcat-hotspot = ' '.
  fcat-outputlen = '000016'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Othr Plnt Recpts'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'Q'.
  fcat-datatype = 'QUAN'.
  fcat-do_sum = 'X'.
  fcat-just = 'R'.
  fcat-no_zero = 'X'.
  APPEND fcat.
  CLEAR fcat.
* Sales Return
  fcat-fieldname = 'SAL_RET'.
  fcat-hotspot = ' '.
  fcat-outputlen = '000016'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Sales Return'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'Q'.
  fcat-datatype = 'QUAN'.
  fcat-do_sum = 'X'.
  fcat-just = 'R'.
  fcat-no_zero = 'X'.
  APPEND fcat.
  CLEAR fcat.
* Total Dispatches
  fcat-fieldname = 'TDISP'.
  fcat-hotspot = ' '.
  fcat-outputlen = '000016'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Total Dispatches'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'Q'.
  fcat-datatype = 'QUAN'.
  fcat-do_sum = 'X'.
  fcat-just = 'R'.
  fcat-no_zero = 'X'.
  APPEND fcat.
  CLEAR fcat.
* Distributor Dispatches
  fcat-fieldname = 'CUSTMR'.
  fcat-hotspot = ' '.
  fcat-outputlen = '000016'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Distr Dispatches/HLL'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'Q'.
  fcat-datatype = 'QUAN'.
  fcat-do_sum = 'X'.
  fcat-just = 'R'.
  fcat-no_zero = 'X'.
  APPEND fcat.
  CLEAR fcat.
* Other Plants Dispatches
  fcat-fieldname = 'OPLANT'.
  fcat-hotspot = ' '.
  fcat-outputlen = '000020'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Othr Plnt Dispatches'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'Q'.
  fcat-datatype = 'QUAN'.
  fcat-do_sum = 'X'.
  fcat-just = 'R'.
  fcat-no_zero = 'X'.
  APPEND fcat.
  CLEAR fcat.
* Material Transfer
  fcat-fieldname = 'TRFSTK'.
  fcat-outputlen = '000016'.
  fcat-just = 'L'.
  fcat-seltext_m = 'M.Transfer'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'Q'.
  fcat-datatype = 'QUAN'.
  fcat-just = 'R'.
  fcat-no_zero = 'X'.
  APPEND fcat.
  CLEAR fcat.
* Transit Loss
  fcat-fieldname = 'TRLOSS'.
  fcat-hotspot = ' '.
  fcat-outputlen = '000016'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Transit Loss'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'Q'.
  fcat-datatype = 'QUAN'.
  fcat-do_sum = 'X'.
  fcat-just = 'R'.
  fcat-no_zero = 'X'.
  APPEND fcat.
  CLEAR fcat.
* Warehouse Loss
  fcat-fieldname = 'WHLOSS'.
  fcat-hotspot = ' '.
  fcat-outputlen = '000016'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Warehouse Loss'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'Q'.
  fcat-datatype = 'QUAN'.
  fcat-do_sum = 'X'.
  fcat-just = 'R'.
  fcat-no_zero = 'X'.
  APPEND fcat.
  CLEAR fcat.
* Total Loss
  fcat-fieldname = 'TLOSS'.
  fcat-hotspot = ' '.
  fcat-outputlen = '000016'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Total Loss'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'Q'.
  fcat-datatype = 'QUAN'.
  fcat-do_sum = 'X'.
  fcat-just = 'R'.
  fcat-no_zero = 'X'.
  APPEND fcat.
  CLEAR fcat.
* Closing Stock
  fcat-fieldname = 'O_STK'.
  fcat-hotspot = ' '.
  fcat-outputlen = '000016'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Closing Stock'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'Q'.
  fcat-datatype = 'QUAN'.
* fcat-do_sum = 'X'.
  fcat-just = 'R'.
  fcat-no_zero = 'X'.
  APPEND fcat.
  CLEAR fcat.
* for the Other Plant Receipts
  fcat-fieldname = 'OTHADJ'.
  fcat-hotspot = ' '.
  fcat-outputlen = '000016'.
  fcat-just = 'L'.
  fcat-seltext_m = 'Othr Adjst'.
  fcat-ddictxt = 'M'.
  fcat-inttype = 'Q'.
  fcat-datatype = 'QUAN'.
  fcat-do_sum = 'X'.
  fcat-just = 'R'.
  fcat-no_zero = 'X'.
  APPEND fcat.
  CLEAR fcat.
ENDFORM. " Z_CREATE_FIELD_CATALOG
*&---------------------------------------------------------------------*
*& Form Z_REUSE_ALV_GRID_DISPLAY
*&---------------------------------------------------------------------*
* here the function module REUSE_ALV_GRID_DISPLAY function module *
* is called. The inputs given to it are as follows: *
* W_REPID : contains the Report Name *
* USER_COMMAND : is set as default *
* FS_LAYO : Here the layout related properties are set *
* FCAT : here the field catalog is create for the output *
* fields *
* FEXCLUDE : Some Application toolbar links are deactivated *
* here *
* FEVENTS : Here top of Page & User command events are defined*
* IT_AFPO : It is the table which contains the data to be *
* displayed in the output *
*----------------------------------------------------------------------*
* has no formal paramters *
*----------------------------------------------------------------------*
FORM z_reuse_alv_grid_display.
  DATA: BEGIN OF i_maktx OCCURS 0,
    matnr LIKE makt-matnr,
    maktx LIKE makt-maktx,
  END OF i_maktx,
  v_temp(18).
  IF NOT i_finalt[] IS INITIAL.
    SELECT matnr
            maktx
      INTO TABLE i_maktx
      FROM makt
      FOR ALL ENTRIES IN i_finalt
      WHERE makt~matnr = i_finalt-matnr.
  ENDIF.
  SORT i_maktx BY matnr.
  LOOP AT i_finalt.
    READ TABLE i_maktx WITH KEY matnr = i_finalt-matnr
    BINARY SEARCH.
    IF sy-subrc = 0.
      i_finalt-maktx = i_maktx-maktx.
      MODIFY i_finalt.
    ENDIF.
  ENDLOOP.
  SORT i_finalt BY matnr.
  DELETE i_finalt WHERE matnr EQ ''.
  DATA: lv_repid LIKE sy-repid.
  lv_repid = sy-repid.
  DATA: v_matnr(18).
  LOOP AT i_finalt.
    CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT'
      EXPORTING
        input  = i_finalt-matnr
      IMPORTING
        output = v_matnr.
    i_finalt-matnr = v_matnr.
    MODIFY i_finalt.
  ENDLOOP.
  SORT i_finalt BY matnr month.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = lv_repid
      i_callback_user_command = 'Z_USER_COMMAND'
      is_layout               = fs_layo
      it_fieldcat             = fcat[]
      it_excluding            = fexc[]
      it_sort                 = fsort[]
      i_default               = 'X'
      i_save                  = 'U'
      is_variant              = alv_variant
      it_events               = fevents[]
    TABLES
      t_outtab                = i_finalt[].
ENDFORM. " Z_REUSE_ALV_GRID_DISPLAY
*&---------------------------------------------------------------------*
*& Form Z_TOPOFPAGE
*&---------------------------------------------------------------------*
* This Subroutine is called Dynamically *
* This is for creating the headings of the output fields *
*----------------------------------------------------------------------*
* has no formal paramters *
*----------------------------------------------------------------------*
FORM z_topofpage.                                           "#EC NEEDED
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = fheader[].
ENDFORM. " FORM Z_TOPOFPAGE
*&---------------------------------------------------------------------*
*& Form Z_USER_COMMAND
*&---------------------------------------------------------------------*
* This Subroutine is called Dynamically *
* if the user double clicks the Production Order Number or the *
* Material Number, the Complete transaction details will be *
* displayed *
*----------------------------------------------------------------------*
FORM z_user_command USING r_ucomm LIKE sy-ucomm
                          rs_selfield TYPE slis_selfield.   "#EC NEEDED
  CHECK r_ucomm = '&IC1'. "User Double Clicked on Some field
  CHECK NOT rs_selfield-value IS INITIAL.
  LOOP AT imkpft2.
    PERFORM remove_zeros USING imkpft2-matnr
                        CHANGING imkpft2-matnr1.
    MODIFY imkpft2.
  ENDLOOP.
  SORT imkpft2 BY budat matnr werks.
  CASE rs_selfield-fieldname.
    WHEN 'MATNR'.
      READ TABLE i_finalt INDEX rs_selfield-tabindex.
      IF d1 = 'X' OR m1 ='X'.
        LOOP AT imkpft2 WHERE matnr1 = i_finalt-matnr AND
                              werks = i_finalt-werks AND
                              ( budat+0(4) EQ i_finalt-month+0(4) AND
                              budat+4(2) EQ i_finalt-month+5(2) ) .
          MOVE imkpft2 TO t_final.
          APPEND t_final.
          CLEAR t_final.
        ENDLOOP.
      ELSEIF y1 = 'X'.
        LOOP AT imkpft2 WHERE matnr1 = i_finalt-matnr AND
        werks = i_finalt-werks AND
        budat IN s_budat.
          MOVE imkpft2 TO t_final.
          APPEND t_final.
          CLEAR t_final.
        ENDLOOP.
      ENDIF.
      SORT t_final BY budat matnr werks.
      PERFORM display_alv1 USING t_final.
  ENDCASE.
ENDFORM. " FORM Z_USER_COMMAND
*&---------------------------------------------------------------------*
*& Form ALV_F4
*&---------------------------------------------------------------------*
FORM alv_f4.
  alv_variant-report = alv_repid.
  CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
    EXPORTING
      is_variant = alv_variant
      i_save     = 'A'
    IMPORTING
      es_variant = alv_variant
    EXCEPTIONS
      not_found  = 2.
  IF sy-subrc = 2.
    MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    alv_def = alv_variant-variant.
  ENDIF.
ENDFORM. "alv_f4
*&---------------------------------------------------------------------*
*& Form ALV_INIT
*&---------------------------------------------------------------------*
FORM alv_init.
  CLEAR: alv_keyinfo, alv_variant, alv_layout, alv_print.
  CLEAR alv_def.
  alv_repid = sy-repid.
  CLEAR alv_variant.
  alv_variant-report = alv_repid.
  CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
    EXPORTING
      i_save     = 'A'
    CHANGING
      cs_variant = alv_variant
    EXCEPTIONS
      not_found  = 2.
  IF sy-subrc = 0.
    alv_def = alv_variant-variant.
    MOVE alv_variant-variant TO alv_default_variant.
  ENDIF.
* Printing settings
  alv_layout-get_selinfos = 'X'.
  alv_layout-group_change_edit = 'X'.
  SELECT SINGLE * FROM mmim_rep_print WHERE report = sy-repid.
  IF sy-subrc = 0.
    IF mmim_rep_print-selinfo = 'X'.
      alv_print-no_print_selinfos = ' '.
    ELSE.
      alv_print-no_print_selinfos = 'X'.
    ENDIF.
    IF mmim_rep_print-coverpage = 'X'.
      alv_print-no_coverpage = ' '.
    ELSE.
      alv_print-no_coverpage = 'X'.
    ENDIF.
    IF mmim_rep_print-listinfo = 'X'.
      alv_print-no_print_listinfos = ' '.
    ELSE.
      alv_print-no_print_listinfos = 'X'.
    ENDIF.
    IF mmim_rep_print-gridcontrol = 'X'.
      alv_detail_func = 'REUSE_ALV_GRID_DISPLAY'.
    ELSE.
      alv_detail_func = 'REUSE_ALV_LIST_DISPLAY'.
    ENDIF.
    IF mmim_rep_print-color = 'X'.
      CLEAR alv_colourize_fields.
    ELSE.
      MOVE 'X' TO alv_colourize_fields.
    ENDIF.
  ELSE.
    MOVE 'X' TO alv_colourize_fields.
    alv_print-no_print_selinfos = 'X'.
    alv_print-no_coverpage = ' '.
    alv_print-no_print_listinfos = 'X'.
    alv_detail_func = 'REUSE_ALV_LIST_DISPLAY'.
  ENDIF.
ENDFORM. "ALV_INIT
*--------------------------------------------------------------------*
*& Form AUTH_CHECK
*&---------------------------------------------------------------------*
* for checking authorisations
*----------------------------------------------------------------------*
FORM auth_check.
  DATA: BEGIN OF i_auth OCCURS 0,
    werks LIKE t001w-werks,
  END OF i_auth.
  SELECT werks
    INTO TABLE i_auth
    FROM t001w
    WHERE werks IN s_werks.
  LOOP AT i_auth.
    AUTHORITY-CHECK OBJECT 'Z_PLNT_AUT'
      ID 'ACTVT' FIELD '03'
      ID 'WERKS' FIELD i_auth-werks.
    IF sy-subrc NE 0 .
      MESSAGE e398(00) WITH
      'User' sy-uname 'not authorised for Plant' i_auth-werks.
    ENDIF.
  ENDLOOP.
ENDFORM. " AUTH_CHECK
*&---------------------------------------------------------------------*
*& Form VALIDITY_CHECK
*&---------------------------------------------------------------------*
FORM validity_check.
  LOOP AT s_werks.
    SELECT SINGLE * FROM t001w
      WHERE werks = s_werks-low.
    IF sy-subrc EQ 4 .
      MESSAGE e398(00) WITH
      'Plant' s_werks-low 'Does not Exist' .
    ENDIF.
  ENDLOOP .
ENDFORM. " VALIDITY_CHECK
*&---------------------------------------------------------------------*
*& Form alv_check
*&---------------------------------------------------------------------*
FORM alv_check.
  alv_variant-report = alv_repid.
  alv_variant-variant = alv_def.
  IF NOT alv_def IS INITIAL.
    CALL FUNCTION 'REUSE_ALV_VARIANT_EXISTENCE'
      EXPORTING
        i_save     = 'A'
      CHANGING
        cs_variant = alv_variant
      EXCEPTIONS
        OTHERS     = 1.
    IF sy-subrc <> 0.
      MESSAGE e321(m7) WITH alv_def alv_repid.
    ENDIF. " IF sy-subrc <> 0.
  ELSE.
* the user wants no initial display variant
    IF NOT alv_default_variant IS INITIAL.
* but the SAP-LIST-VIEWER will apply the existing
* initial display variant / emerge warning 393 ?
      CALL FUNCTION 'ME_CHECK_T160M'
        EXPORTING
          i_arbgb = 'M7'
          i_msgnr = '393'
        EXCEPTIONS
          nothing = 0
          OTHERS  = 1.
      IF sy-subrc <> 0.
        MESSAGE w393(m7) WITH alv_default_variant.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM. "alv_check
*&---------------------------------------------------------------------*
*& Form delete_empty_records
*&---------------------------------------------------------------------*
FORM delete_empty_records .
* deleting the records with empty quantities
  LOOP AT i_final.
    IF i_final-o_stk EQ 0 AND
    i_final-trecep EQ 0 AND
    i_final-produ EQ 0 AND
    i_final-recep EQ 0 AND
    i_final-sal_ret EQ 0 AND
    i_final-tdisp EQ 0 AND
    i_final-custmr EQ 0 AND
    i_final-oplant EQ 0 AND
    i_final-tloss EQ 0 AND
    i_final-trloss EQ 0 AND
    i_final-whloss EQ 0 AND
    i_final-othadj EQ 0 AND
    i_final-c_stk EQ 0.
      DELETE i_final.
      CLEAR i_final.
    ENDIF.
    MOVE-CORRESPONDING i_final TO i_finalt.
*For Month
    IF y1 NE 'X'.
      CONCATENATE p_spmon+0(4) '.' p_spmon+4(2)
      INTO i_finalt-month.
    ELSE.
      CONCATENATE p_spmon+0(4) '.' '00'
      INTO i_finalt-month.
    ENDIF.
    APPEND i_finalt.
    CLEAR i_finalt.
  ENDLOOP.
ENDFORM. " delete_empty_records
*&---------------------------------------------------------------------*
*& Form calculate_opening_stock
*&---------------------------------------------------------------------*
FORM calculate_opening_stock .
  LOOP AT imardht.
    AT NEW matnr.
      i_final-matnr = imardht-matnr.
      i_final-werks = imardht-werks.
      i_final-meins = imardht-meins.
      i_final-mtart = mtart.
      APPEND i_final.
    ENDAT.
  ENDLOOP.
  SORT i_final BY werks matnr.
  LOOP AT i_final.
    READ TABLE imardht WITH KEY matnr = i_final-matnr
    werks = i_final-werks.
    IF sy-subrc EQ 0.
      IF d1 = 'X'.
        obal = imardht-labst + imardht-o_stk + imardht-c_stk.
        cbal = imardht-labst + imardht-c_stk .
      ELSE.
        obal = imardht-labst .
        cbal = imardht-labst + imardht-c_stk .
      ENDIF.
      i_final-c_stk = cbal.
      i_final-o_stk = obal.
      MODIFY i_final.
    ENDIF.
  ENDLOOP.
  LOOP AT i_final.
    LOOP AT i_final5 WHERE matnr = i_final-matnr AND
    werks = i_final-werks.
      i_final-produ = i_final-produ + i_final5-produ.
      i_final-recep = i_final-recep + i_final5-recep.
      i_final-sal_ret = i_final-sal_ret + i_final5-sal_ret.
      i_final-custmr = i_final-custmr + i_final5-custmr.
      i_final-oplant = i_final-oplant + i_final5-oplant.
      i_final-whloss = i_final-whloss + i_final5-whloss.
      i_final-trloss = i_final-trloss + i_final5-trloss.
      i_final-trfstk = i_final-trfstk + i_final5-trfstk.
      i_final-othadj = i_final-othadj + i_final5-othadj.
      MODIFY i_final.
    ENDLOOP.
*-------------------Total Receipts------------*
    i_final-trecep = i_final-produ
    + i_final-recep
    + i_final-sal_ret.
*-------------------Total Dispatches-----------*
    i_final-tdisp = i_final-custmr
    + i_final-oplant.
*---------------------Total Loss----------------*
    i_final-tloss = i_final-trloss
    + i_final-whloss.
    MODIFY i_final.
  ENDLOOP.
ENDFORM. " calculate_opening_stock
*&---------------------------------------------------------------------*
*& Form update_non_transaction_itms
*&---------------------------------------------------------------------*
FORM update_non_transaction_itms .
* Pushing NON TRANSACTION ITEMS TO i_final table
  LOOP AT imardht.
    READ TABLE i_final WITH KEY matnr = imardht-matnr
    werks = imardht-werks .
    IF sy-subrc NE 0.
      CLEAR i_final. "Added by manisha for correction
      i_final-matnr = imardht-matnr.
      i_final-werks = imardht-werks.
      i_final-spmon = p_spmon.
      i_final-mtart = mtart.
      i_final-c_stk = imardht-labst + imardht-c_stk.
      i_final-o_stk = i_final-c_stk.
      i_final-meins = imardht-meins.
      APPEND i_final.
      CLEAR i_final.
    ENDIF.
  ENDLOOP.
ENDFORM. " update_non_transaction_itms
*&---------------------------------------------------------------------*
*& Form get_records_from_DB
*&---------------------------------------------------------------------*
FORM get_records_from_db .
  DATA: wa_mtart LIKE mara-mtart .
  DATA: i_mard1 LIKE i_mard OCCURS 0 WITH HEADER LINE.
  DATA: i_mardh2 LIKE i_mardh1 OCCURS 0 WITH HEADER LINE.
* added by prabhu .
  SELECT matnr mtart meins FROM mara
    INTO CORRESPONDING FIELDS OF TABLE i_mara
    WHERE matnr IN s_matnr AND
          mtart = 'FERT'.
*added by prabu for performance.
  SORT i_mara BY matnr.
  DELETE ADJACENT DUPLICATES FROM i_mara COMPARING matnr.
  SORT i_mara BY matnr.
* end of changes by prabhu for CPO.
  SELECT bwart shkzg vzbew
    FROM yw2_stkmovements
    INTO TABLE i_stkmvmnts.
* Getting the Stocks from the MARD table for the Previous Months
* for Slow Moving Materials
  SELECT mard~matnr
          mard~werks
          mard~lgort
          mard~labst
          mard~insme
          mard~einme "+
          mard~speme "+
          mard~retme "-
    FROM mard
    INTO CORRESPONDING FIELDS OF TABLE i_mard
    FOR ALL ENTRIES IN i_mara
    WHERE mard~matnr EQ i_mara-matnr
      AND mard~werks IN s_werks
      AND mard~lgort IN s_lgort.
* added by prabhu for CPO.
  SORT i_mard BY matnr.
  LOOP AT i_mard.
    READ TABLE i_mara WITH KEY matnr = i_mard-matnr.
    IF sy-subrc NE 0.
      DELETE i_mard.
    ELSE.
      i_mard-meins = i_mara-meins.
      MODIFY i_mard.
    ENDIF.
  ENDLOOP.
* getting the details from the MKPF & MSEG table
  IF NOT i_mard[] IS INITIAL.
    SELECT mkpf~mblnr
            mkpf~mjahr
            mkpf~vgart
            mkpf~budat
            mseg~zeile
            mseg~bwart
            mseg~xauto
            mseg~matnr
            mseg~werks
            mseg~lgort
            mseg~shkzg
            mseg~menge
            mseg~meins
      FROM mkpf AS mkpf INNER JOIN
            mseg AS mseg
      ON mkpf~mblnr = mseg~mblnr
      AND mkpf~mjahr = mseg~mjahr
      INTO CORRESPONDING FIELDS OF TABLE i_mkpf
      FOR ALL ENTRIES IN i_mard
      WHERE mkpf~budat GE s_budat-low
        AND mseg~matnr EQ i_mard-matnr
        AND mseg~werks EQ i_mard-werks
        AND mseg~lgort NE ''.
  ENDIF.
ENDFORM. " get_records_from_DB
*&---------------------------------------------------------------------*
*& Form process_movements
*&---------------------------------------------------------------------*
FORM process_movements .
  DATA: wa_maktx LIKE makt-maktx.
  LOOP AT imkpft WHERE budat IN s_budat.
    IF imkpft-shkzg = 'S'.
      imkpft-menge = imkpft-menge * ( -1 ).
      MODIFY imkpft.
    ELSE.
    ENDIF.
  ENDLOOP.
  LOOP AT imkpft WHERE budat IN s_budat.
    i_final5-werks = imkpft-werks.
    i_final5-lgort = imkpft-lgort.
    i_final5-matnr = imkpft-matnr.
* GET SIGN.
    READ TABLE i_stkmvmnts WITH KEY bwart = imkpft-bwart
    shkzg = imkpft-shkzg.
    IF sy-subrc = 0.
      IF i_stkmvmnts-vzbew = '+'.
        i_final5-menge = imkpft-menge .
      ELSE.
        i_final5-menge = imkpft-menge * ( -1 ).
      ENDIF.
* not to consider movements not found in y table
    ELSE.
* i_final5-menge = imkpft-menge.
      CLEAR i_final5-menge.
    ENDIF."prabhu.
    CASE imkpft-bwart.
* PRODUCTION - 1
      WHEN '101' OR '102' .
        i_final5-produ = i_final5-produ + i_final5-menge.
* SALES RETURNS - 3
      WHEN '653' OR '654'.
        i_final5-sal_ret = i_final5-sal_ret + i_final5-menge.
* CUSTOMER DISPATCHES - 5
      WHEN '624' OR '625' OR '601' OR '602' OR '951' OR '952'
      OR '645' OR '646' .
        i_final5-custmr = i_final5-custmr + i_final5-menge.
* OTHER PLANT DISPATCHES - 6 / RECEIPTS - 2
      WHEN '641' OR '941' OR '643'.
        IF imkpft-shkzg = 'H'.
          i_final5-oplant = i_final5-oplant + i_final5-menge.
        ELSE.
          i_final5-recep = i_final5-recep + i_final5-menge.
        ENDIF.
      WHEN '642' OR '942' OR '644'.
        IF imkpft-shkzg = 'S'.
          i_final5-oplant = i_final5-oplant + i_final5-menge.
        ELSE.
          i_final5-recep = i_final5-recep + i_final5-menge.
        ENDIF.
* TRANSIT LOSS
      WHEN '251' OR '252' OR 'Z51' OR 'Z52'.
        i_final5-trloss = i_final5-trloss + i_final5-menge.
* WAREHOUSE LOSS
      WHEN '551' OR '552' OR 'Y51' OR 'Y52' OR '555' OR '556'.
        i_final5-whloss = i_final5-whloss + i_final5-menge.
* TRANSFER STOCK
      WHEN '309' OR '310'.
        i_final5-trfstk = i_final5-trfstk + i_final5-menge.
* OTHER ADJUSTMENTS
      WHEN OTHERS.
        i_final5-othadj = i_final5-othadj + i_final5-menge.
    ENDCASE.
* Added by prabhu on 12.3.05 CPO.
*read table i_final5 with key matnr = imkpft-matnr.
*if sy-subrc ne 0.
    i_final5-meins = imkpft-meins.
    i_final5-budat = imkpft-budat.
    APPEND i_final5.
    CLEAR i_final5.
*endif.
  ENDLOOP.
  SORT i_final5 BY werks matnr budat ASCENDING.
ENDFORM. " process_movements
*&---------------------------------------------------------------------*
*& Form get_plant_distinction
*&---------------------------------------------------------------------*
FORM get_plant_distinction .
  SELECT pplnt wplnt FROM yplnt
    INTO CORRESPONDING FIELDS OF TABLE i_yplnt WHERE
    pplnt IN s_werks.
  SELECT pplnt wplnt FROM yplnt
    APPENDING CORRESPONDING FIELDS OF TABLE i_yplnt WHERE
    wplnt IN s_werks.
ENDFORM. " get_plant_distinction
*&---------------------------------------------------------------------*
*& Form month_wise
*&---------------------------------------------------------------------*
FORM month_wise .
  IF y1 = ''.
    LOOP AT imkpft WHERE (
    ( budat+4(2) GT p_spmon+4(2) AND
    mjahr EQ p_spmon+0(4) ) OR
    ( budat+4(2) LE p_spmon+4(2) AND
    mjahr NE p_spmon+0(4) ) OR
    ( budat+4(2) GE p_spmon+4(2) AND
    mjahr NE p_spmon+0(4) ) ).
      imkpft1-mjahr = imkpft-mjahr.
      imkpft1-mblnr = imkpft-mblnr.
      imkpft1-budat = imkpft-budat.
      imkpft1-vgart = imkpft-vgart.
      imkpft1-bwart = imkpft-bwart.
      imkpft1-matnr = imkpft-matnr.
      imkpft1-werks = imkpft-werks.
      imkpft1-lgort = imkpft-lgort.
      imkpft1-menge = imkpft-menge.
      imkpft1-meins = imkpft-meins.
      imkpft1-xauto = imkpft-xauto.
      imkpft1-shkzg = imkpft-shkzg.
      APPEND imkpft1.
      CLEAR imkpft1.
    ENDLOOP.
  ELSE.
    LOOP AT imkpft WHERE budat IN s_budat1.
      imkpft1-mjahr = imkpft-mjahr.
      imkpft1-mblnr = imkpft-mblnr.
      imkpft1-budat = imkpft-budat.
      imkpft1-vgart = imkpft-vgart.
      imkpft1-bwart = imkpft-bwart.
      imkpft1-matnr = imkpft-matnr.
      imkpft1-werks = imkpft-werks.
      imkpft1-lgort = imkpft-lgort.
      imkpft1-menge = imkpft-menge.
      imkpft1-meins = imkpft-meins.
      imkpft1-xauto = imkpft-xauto.
      imkpft1-shkzg = imkpft-shkzg.
      APPEND imkpft1.
      CLEAR imkpft1.
    ENDLOOP.
  ENDIF.
  SORT imkpft1 BY werks matnr budat bwart ASCENDING.
  LOOP AT imardt.
    AT NEW matnr .
      imardht-matnr = imardt-matnr.
      imardht-werks = imardt-werks.
      APPEND imardht.
      CLEAR imardht.
    ENDAT.
  ENDLOOP.
  LOOP AT imardht.
    LOOP AT imardt WHERE matnr = imardht-matnr AND
    werks = imardht-werks.
      imardht-matnr = imardt-matnr.
      imardht-werks = imardt-werks.
      imardht-lgort = imardt-lgort.
      imardht-labst = imardt-labst + imardt-einme +
      imardt-speme + imardt-retme + imardht-labst +
      imardt-insme.
      imardht-meins = imardt-meins.
      MODIFY imardht.
    ENDLOOP.
  ENDLOOP.
* sorting the data by matnr werks lgort.
  SORT imardht BY werks lgort matnr.
* to get closing stock.
  SORT imkpft1 BY matnr.
  DATA: BEGIN OF data OCCURS 0,
  lgort LIKE mard-lgort,
  matnr LIKE mard-matnr,
  labst LIKE mard-labst,
  werks LIKE mard-werks,
  END OF data.
  LOOP AT imardt.
    AT NEW matnr.
      data-lgort = imardt-lgort.
      data-matnr = imardt-matnr.
      data-werks = imardt-werks.
      APPEND data.
      CLEAR data.
    ENDAT.
  ENDLOOP.
  SORT imkpft1 BY matnr shkzg.
  LOOP AT imkpft1.
    IF imkpft1-shkzg = 'S'.
      imkpft1-menge = imkpft1-menge * ( -1 ).
      MODIFY imkpft1.
    ELSE.
    ENDIF.
  ENDLOOP.
  SORT imkpft1 BY matnr budat.
  LOOP AT imardht.
    LOOP AT imkpft1 WHERE matnr = imardht-matnr AND
    werks = imardht-werks .
      imardht-labst = imardht-labst + imkpft1-menge.
      MODIFY imardht.
    ENDLOOP.
    MODIFY imardht.
  ENDLOOP.
  SORT imkpft BY matnr budat.
  IF y1 =''.
    DELETE imkpft WHERE
    ( budat+4(2) NE p_spmon+4(2) OR
    ( budat+4(2) EQ p_spmon+4(2) AND
    mjahr NE p_spmon+0(4) ) ) OR
    ( budat+4(2) GT p_spmon AND
    mjahr NE p_spmon+0(4) ) .
  ELSE.
    DELETE imkpft WHERE budat IN s_budat1.
  ENDIF.
*for Closing stock.
  LOOP AT imkpft.
    IF imkpft-shkzg = 'S'.
      imkpft-menge = imkpft-menge * ( -1 ).
      MODIFY imkpft.
    ENDIF.
  ENDLOOP.
  IF d1 = 'X'.
    LOOP AT imardht.
      LOOP AT imkpft WHERE matnr = imardht-matnr AND
      werks = imardht-werks.
        imardht-c_stk = imardht-c_stk + imkpft-menge.
        MODIFY imardht.
      ENDLOOP.
    ENDLOOP.
*for day cal only on 19.4.5
    LOOP AT imardht.
      LOOP AT imkpft WHERE matnr = imardht-matnr AND
      budat IN s_budat AND
      werks = imardht-werks .
        imardht-o_stk = imardht-o_stk - imkpft-menge.
        MODIFY imardht.
      ENDLOOP.
    ENDLOOP.
  ELSE.
    LOOP AT imardht.
      LOOP AT imkpft WHERE matnr = imardht-matnr AND
      werks = imardht-werks.
        imardht-c_stk = imardht-c_stk + imkpft-menge.
        MODIFY imardht.
      ENDLOOP.
    ENDLOOP.
  ENDIF.
ENDFORM. " month_wise
*&---------------------------------------------------------------------*
*&      Form  z_alv_events1
*&---------------------------------------------------------------------*
FORM z_alv_events1.
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    EXPORTING
      i_list_type = 0
    IMPORTING
      et_events   = fevents1[].
  READ TABLE fevents WITH KEY name = 'TOP_OF_PAGE'.
  IF sy-subrc = 0.
    fevents-form = 'Z_TOPOFPAGE'.
* fevents-form = 'Z_LIST'.
    MODIFY fevents INDEX sy-tabix.
    CLEAR fevents.
  ENDIF. " IF SY-SUBRC = 0.
* Drill down
  READ TABLE fevents WITH KEY name = 'USER_COMMAND'.
  IF sy-subrc = 0.
    fevents-form = 'Z_USER_COMMAND1'.
    MODIFY fevents INDEX sy-tabix.
    CLEAR fevents1.
  ENDIF.
ENDFORM. "Z_ALV_EVENTS
*&---------------------------------------------------------------------*
*&      Form  z_user_command1
*&---------------------------------------------------------------------*
FORM z_user_command1 USING r_ucomm LIKE sy-ucomm
                            rs_selfield TYPE slis_selfield. "#EC NEEDED
*sel_tab_field type slis_sel_tab_field
  CHECK r_ucomm = '&IC1'. "User Double Clicked on Some field
  CHECK NOT rs_selfield-value IS INITIAL.
  CASE rs_selfield-fieldname.
  ENDCASE.
ENDFORM. "Z_USER_COMMAND
*&---------------------------------------------------------------------*
*& Form display_alv
*&---------------------------------------------------------------------*
FORM display_alv1 USING p_t_final.
  DATA:
  lv_repid LIKE sy-repid.
  CLEAR: fcat1.
  PERFORM create_fieldcat1.
  PERFORM z_alv_events1.
  lv_repid = sy-repid.
  SORT t_final BY matnr budat werks .
  DATA: w_matnr(18).
  CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
    EXPORTING
      i_callback_program      = lv_repid
      i_callback_user_command = 'Z_USER_COMMAND'
      is_layout               = fs_layo1
      it_fieldcat             = fcat1[]
      i_default               = 'X'
      i_save                  = 'U'
      it_events               = fevents1[]
    TABLES
      t_outtab                = t_final[].
  CLEAR: fcat1,t_final.
  REFRESH: fcat1,t_final.
ENDFORM. " display_alv
*&---------------------------------------------------------------------*
*& Form create_fieldcat
*&---------------------------------------------------------------------*
* Create a field catalogue for Alv Report
*----------------------------------------------------------------------*
FORM create_fieldcat1 .
* for the Material No.
  fcat1-fieldname = 'MATNR'.
  fcat1-key = 'X'.
* fcat-hotspot = 'X'.
  fcat1-outputlen = '00006'.
  fcat1-just = 'L'.
  fcat1-seltext_m = 'Material'.
  fcat1-ddictxt = 'M'.
  fcat1-inttype = 'C'.
  fcat1-datatype = 'CHAR'.
  APPEND fcat1.
  CLEAR fcat1.
* for the Plant
  fcat1-fieldname = 'WERKS'.
  fcat1-key = 'X'.
  fcat1-outputlen = '000005'.
  fcat1-just = 'L'.
  fcat1-seltext_m = 'Plant'.
  fcat1-ddictxt = 'M'.
  fcat1-inttype = 'C'.
  fcat1-datatype = 'CHAR'.
  APPEND fcat1.
  CLEAR fcat1.
* for the MatDoc
  fcat1-fieldname = 'MBLNR'.
  fcat1-outputlen = '000010'.
  fcat1-just = 'L'.
  fcat1-seltext_m = 'Mat.Doc'.
  fcat1-ddictxt = 'M'.
  fcat1-inttype = 'C'.
  fcat1-datatype = 'CHAR'.
  APPEND fcat1.
  CLEAR fcat1.
* for the Date
  fcat1-fieldname = 'BUDAT'.
  fcat1-outputlen = '000010'.
  fcat1-just = 'L'.
  fcat1-seltext_m = 'Mat.Date'.
  fcat1-ddictxt = 'M'.
  fcat1-inttype = 'D'.
  fcat1-datatype = 'DATS'.
  APPEND fcat1.
  CLEAR fcat1.
* for the MOVETYPE
  fcat1-fieldname = 'BWART'.
  fcat1-outputlen = '000003'.
  fcat1-just = 'L'.
  fcat1-seltext_m = 'Move.TyPE'.
  fcat1-ddictxt = 'M'.
  fcat1-inttype = 'C'.
  fcat1-datatype = 'CHAR'.
  APPEND fcat1.
  CLEAR fcat1.
* for the MOVETYPE
  fcat1-fieldname = 'MENGE'.
  fcat1-outputlen = '000013'.
  fcat1-just = 'R'.
  fcat1-seltext_m = 'Move.TyPE'.
  fcat1-ddictxt = 'M'.
  fcat1-inttype = 'Q'.
  fcat1-datatype = 'QUAN'.
  APPEND fcat1.
  CLEAR fcat1.
ENDFORM. " create_fieldcat
*&---------------------------------------------------------------------*
*&      Form  remove_zeros
*&---------------------------------------------------------------------*
FORM remove_zeros USING p_imkpft2-matnr
CHANGING p_imkpft2-matnr1.
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
    EXPORTING
      input  = imkpft2-matnr
    IMPORTING
      output = imkpft2-matnr1.
ENDFORM. " remove_zeros