Docstoc

REPORT_NEW285811304660156.docx 58.72 KB - SAP Techies

Document Sample
REPORT_NEW285811304660156.docx 58.72 KB - SAP Techies Powered By Docstoc
					REPORT YR_MM_GRN_STOCK_ZEDEK_HOS MESSAGE-ID XX.
************************************************************************
* REPORT NAME : GRN Stock in hand
* Developed by : A.MELCHIZEDEK
* Location : Chennai
* Date : 24/11/2003
* change history : A.Melchizedek
* Date : 19/01/2004
************************************************************************
*-----------------------Change History---------------------------------*
**Modified by : Omprasath
**Date         : 17.02.2010
**Reason       :1)Company Code Authority Check
*               2)Plant Code Authority Check
**Mod-Id       : 1.1   For Hospira
*----------------------------------------------------------------------*
*-----------------------Change History---------------------------------*
**Modified by : sujatha
**Date         : 08.03.2010
**Reason       :Plant Code Authority Check
**Mod-Id       : 1.2   For Hospira
*----------------------------------------------------------------------*

************************************************************************
* Database Tables And Declarations
************************************************************************
TABLES :

         MKPF,      "Header: Material Document
         MSEG,      "Document Segment: Material
         MARA,      "General Material Data
         MARC,      "Plant Data for Material
         MARD,      "Storage Location Data for Material
         MCHB,      "Batch Stocks
         MKOL,      "Special Stocks from Vendor
         MSKU,      "Special Stocks with Customer
         MSLB,      "Special Stocks with Vendor
         MSKA,      "Sales Order Stock
         MSSA,      "Total Customer Orders on Hand
         MSPR,      "Project Stock
         MSSQ,      "Project Stock Total
         EKBE,      "History per Purchasing Document
         BKPF.      " Document Header.


*----------------------------------------------------------------------*
*        AlV Data Declaration
*----------------------------------------------------------------------*
****
TYPE-POOLS : SLIS, IMREP.
*CONSTANTS:
*GC_FORMNAME_TOP_OF_PAGE TYPE SLIS_FORMNAME VALUE 'TOP_OF_PAGE'.
*DATA: G_REPID LIKE SY-REPID.
*DATA :
* "       used for individual column
*         GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
*         G_TABNAME   TYPE SLIS_TABNAME VALUE 'ITAB_OPEN_H',
* "       used for layout type
*         GS_LAYOUT   TYPE SLIS_LAYOUT_ALV,
*         GT_VARIANT TYPE SLIS_VARI,
* "       used for subtotal
*         GS_SUB       TYPE SLIS_SUBTOT_TEXT,
*         GT_SP_GROUP TYPE SLIS_T_SP_GROUP_ALV,
* "       used for events in the report
*         GT_EVENTS    TYPE SLIS_T_EVENT,
*         GT_COL       TYPE SLIS_COLOR, "used for layout color
* "       for sorting the table
**         gt_sort      TYPE slis_t_sortinfo_alv,
*          GT_SORT TYPE SLIS_T_SORTINFO_ALV WITH HEADER LINE,
*
*         GT_LIST_TOP_OF_PAGE TYPE SLIS_T_LISTHEADER,
* "       contains the user event
*         G_USER_COMMAND LIKE SLIS_EV_USER_COMMAND VALUE 'USER_COMMAND',
*         ALV_EVENT TYPE SLIS_ALV_EVENT, "structure event handling
*         GT_PRINT TYPE SLIS_PRINT_ALV.
* Data for listviewer
DATA: G_REPID      LIKE SY-REPID.
DATA: FIELDCAT TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE.
DATA: KEYINFO   TYPE SLIS_KEYINFO_ALV.
DATA: COLOR     TYPE SLIS_T_SPECIALCOL_ALV WITH HEADER LINE.
DATA: GS_LAYOUT     TYPE SLIS_LAYOUT_ALV.

DATA: GT_SORT      TYPE SLIS_T_SORTINFO_ALV WITH HEADER LINE.
DATA: EXCLUDING TYPE SLIS_T_EXTAB WITH HEADER LINE.

* Variants
DATA: VARIANTE          LIKE   DISVARIANT,
      VARIANTE_FLAT     LIKE   DISVARIANT,
      DEF_VARIANTE      LIKE   DISVARIANT,
      DEF_VARIANTE_F4   LIKE   DISVARIANT,
      VARIANT_EXIT(1)   TYPE   C.

DATA : G_F_VARI_HSQ             LIKE   DISVARIANT-VARIANT,
       G_F_VARI_FLT             LIKE   DISVARIANT-VARIANT.

* working fields to save the initial display variants          "n579976
DATA : G_F_VARI_HSQ_INITIAL LIKE DISVARIANT-VARIANT,           "n579976
       G_F_VARI_FLT_INITIAL LIKE DISVARIANT-VARIANT.           "n579976

DATA : G_CNT_COL_POS         TYPE I,
       G_CNT_SPOS            TYPE I.
* Global variables for handling ALV functionality
TABLES: MMIM_REP_PRINT.

DATA:   ALV_KEYINFO      TYPE   SLIS_KEYINFO_ALV.
DATA:   ALV_VARIANT      LIKE   DISVARIANT.
DATA:   ALV_LAYOUT       TYPE   SLIS_LAYOUT_ALV.
DATA:   ALV_REPID        LIKE   SY-REPID.
DATA:   ALV_PRINT        TYPE   SLIS_PRINT_ALV.
DATA:   ALV_DETAIL_FUNC(30)      TYPE C,
        ALV_COLOR                LIKE      MMIM_REP_PRINT-COLOR.




* User settings for the checkboxes
DATA: OREF_SETTINGS TYPE REF TO CL_MMIM_USERDEFAULTS.

************************************************************************
* Internal Tables
************************************************************************
****
TYPES : BEGIN OF STYPE_MARD_LV,
          MATNR              LIKE       MARD-MATNR,
          WERKS              LIKE       MARD-WERKS,
          LGORT              LIKE       MARD-LGORT,
          LVORM              LIKE       MARD-LVORM,
        END OF STYPE_MARD_LV,

       HTAB_MARD_LV            TYPE HASHED TABLE OF
                               STYPE_MARD_LV
                     WITH UNIQUE KEY MATNR WERKS LGORT.

DATA : G_S_MARD_LV             TYPE     STYPE_MARD_LV,
       G_T_MARD_LV             TYPE     HTAB_MARD_LV.

DATA : PA_SOND    LIKE   RMMMB-KZLSO.

DATA: BEGIN OF   COLLECTOR OCCURS 0,
         MATNR   LIKE MARA-MATNR,
         WERKS   LIKE T001W-WERKS,
         LGORT   LIKE MARD-LGORT,
         SOBKZ   LIKE MKOL-SOBKZ,
         PSPNR                 LIKE     MSPR-PSPNR,
         VBELN                 LIKE     MSKA-VBELN,
         POSNR                 LIKE     MSKA-POSNR,
         LIFNR   LIKE MSLB-LIFNR,
         KUNNR   LIKE MSKU-KUNNR,
         LVORM                 LIKE     MARD-LVORM,

        KZBWS LIKE MSSA-KZBWS,
        CHARG LIKE MCHB-CHARG,
        LABST LIKE MARD-LABST,
        INSME LIKE MARD-INSME,
        SPEME LIKE MARD-SPEME,
        EINME LIKE MARD-EINME,
        RETME LIKE MARD-RETME,
        UMLME LIKE MARD-UMLME,
      END OF COLLECTOR.

TYPES : BEGIN OF STYPE_T001L,
          WERKS              LIKE       T001L-WERKS,
          LGORT              LIKE       T001L-LGORT,
          LGOBE              LIKE       T001L-LGOBE,
        END OF STYPE_T001L,

       HTAB_T001L              TYPE HASHED TABLE OF
                               STYPE_T001L
                               WITH UNIQUE KEY WERKS LGORT.

TYPES : BEGIN OF STYPE_ORGAN,
          WERKS              LIKE       T001W-WERKS,
          BWKEY              LIKE       T001W-BWKEY,
          NAME1              LIKE       T001W-NAME1,
          BUKRS              LIKE       T001-BUKRS,
          WAERS              LIKE       T001-WAERS,
        END OF STYPE_ORGAN,

       STAB_ORGAN              TYPE STANDARD TABLE OF
                               STYPE_ORGAN
                               WITH DEFAULT KEY.
DATA: G_T_ORGAN                TYPE STAB_ORGAN,
     G_S_ORGAN               TYPE   STYPE_ORGAN.

DATA : G_S_T001L             TYPE   STYPE_T001L,
       G_T_T001L             TYPE   HTAB_T001L.

DATA : G_FLAG_OK(01)         TYPE C,
       G_FLAG_MESS_333(01)   TYPE C,
       G_FLAG_T001L(01)      TYPE C.


****
DATA : BEGIN OF ITAB_MAT OCCURS 0,
          WERKS LIKE MCHB-WERKS,
          MATNR LIKE MARA-MATNR,
          CLABS LIKE MCHB-CLABS,
          MAKTX LIKE MAKT-MAKTX,
          MTART LIKE MARA-MTART,
          MATKL LIKE MARA-MATKL,
          EXTWG LIKE MARA-EXTWG,
          LGPBE LIKE MARD-LGPBE,
          MEINS LIKE MARA-MEINS,
*          werks LIKE marc-werks,
          CHARG LIKE MCHB-CHARG,
          LGORT LIKE MCHB-LGORT,
     END OF ITAB_MAT.
DATA: BEGIN OF BESTAND OCCURS 0,
*        Key fields
         MATNR LIKE MARA-MATNR,
         WERKS LIKE T001W-WERKS,
         LGORT LIKE MARD-LGORT,
         SOBKZ LIKE MKOL-SOBKZ,
         SSNUM               LIKE BICKEY-SSNUM,    "n531604
         PSPNR               LIKE MSPR-PSPNR,      "n531604
         VBELN               LIKE MSKA-VBELN,      "n531604
         POSNR               LIKE MSKA-POSNR,      "n531604
         LIFNR LIKE MKOL-LIFNR,
         KUNNR LIKE MSKU-KUNNR,
         KZBWS LIKE MSSA-KZBWS,
         CHARG LIKE MCHB-CHARG,
*        Additional data (texts, unit, ...)
         MAKTX LIKE MARAV-MAKTX,
         BWKEY LIKE MBEW-BWKEY,
         MTART LIKE MARAV-MTART,
         MATKL LIKE MARAV-MATKL,
         MEINS LIKE MARAV-MEINS,
         BWTTY LIKE MARC-BWTTY,
         XCHAR LIKE MARC-XCHAR,
         LGOBE LIKE T001L-LGOBE,
         BWTAR LIKE MCHA-BWTAR,
         WAERS LIKE T001-WAERS,
         NAME1 LIKE T001W-NAME1,
*        Quantities and currencies
         LABST LIKE MARD-LABST,
         WLABS LIKE MBEW-SALK3,
         INSME LIKE MARD-INSME,
         WINSM LIKE MBEW-SALK3,
         SPEME LIKE MARD-SPEME,
         WSPEM LIKE MBEW-SALK3,
         EINME LIKE MARD-EINME,
         WEINM LIKE MBEW-SALK3,
         RETME LIKE MARD-RETME,
         WRETM LIKE MBEW-SALK3,
         UMLME LIKE MARD-UMLME,
         WUMLM LIKE MBEW-SALK3,
*        Dummy field
         DUMMY               TYPE ALV_DUMMY,
*        Colour
         FARBE TYPE SLIS_T_SPECIALCOL_ALV,
         LVORM               LIKE MARD-LVORM,
       END OF BESTAND.
DATA : BEGIN OF G_FLAG_SOBKZ,
         VBELN(01)           TYPE C,
         PSPNR(01)           TYPE C,
         LIFNR(01)           TYPE C,
         KUNNR(01)           TYPE C,
       END OF G_FLAG_SOBKZ.
DATA : BEGIN OF ITAB_MVT OCCURS 0,
        WERKS LIKE MSEG-WERKS,
        MATNR LIKE MARA-MATNR,
*        maktx LIKE makt-maktx,
*        meins LIKE mara-meins,
        LGORT LIKE MCHB-LGORT,
        BWTAR LIKE MSEG-BWTAR,
        MBLNR LIKE MKPF-MBLNR,
        ZEILE LIKE MSEG-ZEILE,
        EBELN LIKE MSEG-EBELN,
        SMBLN LIKE MSEG-SMBLN,
        SMBLP LIKE MSEG-SMBLP,
        SHKZG LIKE MSEG-SHKZG,
        LFBNR LIKE MSEG-LFBNR,
        LFPOS LIKE MSEG-LFPOS,
        MJAHR LIKE MSEG-MJAHR,
        BUDAT LIKE MKPF-BUDAT,
        CPUDT LIKE MKPF-CPUDT,
        CPUTM LIKE MKPF-CPUTM,
        MENGE LIKE MSEG-MENGE,
        ABLAD LIKE MSEG-ABLAD,
        WEMPF LIKE MSEG-WEMPF,
        BEDNR LIKE EBAN-BEDNR,
        AFNAM LIKE EBAN-AFNAM,
        BANFN LIKE EBAN-BANFN,
        BADAT LIKE EBAN-BADAT,
        BWART LIKE MSEG-BWART,
        AUFNR LIKE MSEG-AUFNR,
        KOSTL LIKE MSEG-KOSTL,
        PS_PSP_PNR LIKE MSEG-PS_PSP_PNR,
  END OF ITAB_MVT.
DATA : BEGIN OF ITAB_MVT1 OCCURS 0.
        INCLUDE STRUCTURE ITAB_MVT.
DATA : END OF ITAB_MVT1.
DATA : BEGIN OF ITAB_MVT2 OCCURS 0,
       MATNR LIKE MARA-MATNR,
       MAKTX LIKE MAKT-MAKTX,
       BWTAR LIKE MSEG-BWTAR,
       LGORT LIKE MCHB-LGORT,
       MEINS LIKE MARA-MEINS,
       MBLNR LIKE MKPF-MBLNR,
       EBELN LIKE MSEG-EBELN,
       WERKS LIKE MSEG-WERKS,
       SHKZG LIKE MSEG-SHKZG,
       LFBNR LIKE MSEG-LFBNR,
       MJAHR LIKE MSEG-MJAHR,
       BUDAT LIKE MKPF-BUDAT,
       CPUDT LIKE MKPF-CPUDT,
       CPUTM LIKE MKPF-CPUTM,
       MENGE LIKE MSEG-MENGE,
       ABLAD LIKE MSEG-ABLAD,
       WEMPF LIKE MSEG-WEMPF,
       BEDNR LIKE EBAN-BEDNR,
       AFNAM LIKE EBAN-AFNAM,
       BANFN LIKE EBAN-BANFN,
       BADAT LIKE EBAN-BADAT,
       BWART LIKE MSEG-BWART,
END OF ITAB_MVT2.
DATA : BEGIN OF ITAB_FINAL OCCURS 0,
          MBLNR LIKE MSEG-MBLNR,
          EBELN LIKE MSEG-EBELN,
          MATNR LIKE MARA-MATNR,
          MAKTX LIKE MAKT-MAKTX,
          MEINS LIKE MARA-MEINS,
          MTART LIKE MARA-MTART,
          MATKL LIKE MARA-MATKL,
          EXTWG LIKE MARA-EXTWG,
          WERKS LIKE MARC-WERKS,
          BWTAR LIKE MSEG-BWTAR,
          LGORT LIKE MCHB-LGORT,
          BWART LIKE MSEG-BWART,
          BKTXT LIKE MKPF-BKTXT,
          BTEXT LIKE T156HT-BTEXT,
          MJAHR LIKE MSEG-MJAHR,
          LGPBE LIKE MARD-LGPBE,
          BUDAT LIKE MKPF-BUDAT,
          MENGE LIKE MSEG-MENGE,
          YMENGE LIKE MSEG-MENGE,
          SHKZG LIKE MSEG-SHKZG,
          CLABS LIKE MCHB-CLABS,
          ABLAD LIKE MSEG-ABLAD,
          WEMPF LIKE MSEG-WEMPF,
          BEDNR LIKE EBAN-BEDNR,
          AFNAM LIKE EBAN-AFNAM,
          BANFN LIKE EBAN-BANFN,
          BADAT LIKE EBAN-BADAT,
          ZEILE LIKE MSEG-ZEILE,
*added by lakshya on 16/02/2007 to include total price of valuated stock
          TPRICE LIKE MBEW-SALK3,
*end of addition by lakshya
          PTXT(132) ,
     END OF ITAB_FINAL.
DATA : BEGIN OF ITAB_DIFF OCCURS 0,
        MATNR LIKE MARA-MATNR,
        CLABS LIKE MCHB-CLABS,
        MENGE LIKE MSEG-MENGE,
END OF ITAB_DIFF.
DATA : BEGIN OF ITAB_DIFF2 OCCURS 0,
        MATNR LIKE MARA-MATNR,
        CLABS LIKE MCHB-CLABS,
        MENGE LIKE MSEG-MENGE,
END OF ITAB_DIFF2.

DATA   :   XMENGE LIKE MSEG-MENGE VALUE 1,MATNO LIKE MARA-MATNR.
DATA   :   G_FLAG_SUPPRESS_INIT_LGORT(01) TYPE C.
DATA   :   YMENGE LIKE MSEG-MENGE VALUE 1.
DATA   :   FLAG TYPE I .
DATA   :   FLAG1 TYPE I.
DATA   :   W_COL TYPE I.
DATA   :   X TYPE C VALUE 'z'.
DATA   :   X1 TYPE I.
DATA   :   PMENGE LIKE MSEG-MENGE,
           QMENGE LIKE MSEG-MENGE,
           RMENGE LIKE MSEG-MENGE.

* zedek - includes on 19/01/2004
DATA W_QTY LIKE MSEG-MENGE.
* zedek - end of include on 19/01/2004

* Start zedek - includes on 04/03/2004
DATA W_REMARKS LIKE THEAD-TDNAME.
DATA : ITLINE LIKE TLINE OCCURS 0 WITH HEADER LINE.
* End zedek - includes on 04/03/2004

DATA : N TYPE I.

*added by lakshya on 16/02/2007 for unit price of material
data: uprice like mbew-verpr.
*end of addition by lakshya
*Authority check declarations for plant
DATA : BEGIN OF ITAB_PLANT OCCURS 0,
       WERKS LIKE T001W-WERKS,
       END OF ITAB_PLANT.



DATA: AUT_FLAG(1)       TYPE C,
      SEL_FLAG(1)       TYPE C.

*Selection screen declarations

AT SELECTION-SCREEN.
  PERFORM                         F0200_SETTINGS_SAVE.

*---------------------------------------------------------------------*
*                      Initialization                                 *
*---------------------------------------------------------------------*
INITIALIZATION.
  G_REPID = SY-REPID.
* PERFORM : sort_init USING gt_sort[].
  PERFORM   ALV_EVENT_INIT. "using alv_events[]
  PERFORM                    F0000_GET_PRINT_SETTINGS.

  PERFORM                         F0100_SETTINGS_INIT.

************************************************************************
* Selection Screen
************************************************************************
  SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
  SELECT-OPTIONS :
* Added for Commpany code in selection screen -Mod-Id 1.1 Starts
                   bukreis FOR bkpf-bukrs NO INTERVALS NO-EXTENSION OBLIGATORY,
* Added for Commpany code in selection screen -Mod-Id 1.1-Ends
                   S_PLANT FOR MARC-WERKS OBLIGATORY,
                   S_STLOC FOR MCHB-LGORT,
                   S_MATYP FOR MARA-MTART,
                   S_MATNR FOR MARA-MATNR,
                   S_MATGP FOR MARA-MATKL,
                   S_EXMGP FOR MARA-EXTWG,
                   S_VALTP FOR MSEG-BWTAR.

    SELECT-OPTIONS : SO_SOBKZ FOR MKOL-SOBKZ NO-DISPLAY.
    PARAMETERS:       NOVALUES LIKE AM07M-MB52_NOVAL NO-DISPLAY.
    SELECTION-SCREEN END OF BLOCK B1.


AT SELECTION-SCREEN ON bukreis.
*** company code level authorization check

 CALL FUNCTION 'BUKRS_AUTHORITY_CHECK'
      EXPORTING
           xdatabase = 'B'
      TABLES
           xbukreis = bukreis
EXCEPTIONS
  NOT_AUTHORIZED       = 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.

*   Added for Plant Authorisation Check by sujatha-For Mod-Id 1.2-Ends

IF S_PLANT[] IS NOT INITIAL.

  SELECT WERKS FROM T001W INTO CORRESPONDING FIELDS OF TABLE ITAB_PLANT WHERE WERKS IN
s_plant.

AUT_FLAG = '0'.
SEL_FLAG = '0'.

LOOP AT ITAB_PLANT.
  SEL_FLAG = '1'.

CALL FUNCTION 'WERKS_AUTHORITY_CHECK'
EXPORTING
AKTYP              = 'A'
WERKS              = ITAB_PLANT-WERKS
EXCEPTIONS
NO_AUTHORITY       = 1
OTHERS             = 2.
IF SY-SUBRC NE 0.
  delete itab_plant.
  ENDIF.
ENDLOOP.


DESCRIBE TABLE ITAB_PLANT.
IF SY-TFILL = 0.
  MESSAGE 'No authorization for Plant' TYPE 'E'.
ENDIF.

IF SEL_FLAG = '0'.
  MESSAGE 'THE PLANT SPECIFIED DO NOT EXIST' TYPE 'E'. "E028.
ENDIF.

REFRESH s_plant.
CLEAR   s_plant.
loop at itab_plant.
  move : itab_plant-werks to s_plant-low.
  move : 'I'   TO s_plant-SIGN.
  MOVE : 'EQ' TO s_plant-OPTION.
  append s_plant.
endloop.

ENDIF.
**Ended for Plant Authorisation Check by sujatha-For Mod-Id 1.2-Ends
*Added for Company Code and Plant Authorisation Check by Om-For Mod-Id 1.1-Ends

************************************************************************
* Start-of-selection
************************************************************************

START-OF-SELECTION.
**Added for Plant Authorisation Check by sujatha-For Mod-Id 1.2-Ends


IF S_PLANT[] IS INITIAL.

  SELECT WERKS FROM T001W INTO CORRESPONDING FIELDS OF TABLE ITAB_PLANT WHERE WERKS IN
s_plant.



LOOP AT ITAB_PLANT.

CALL FUNCTION 'WERKS_AUTHORITY_CHECK'
EXPORTING
AKTYP              = 'A'
WERKS              = ITAB_PLANT-WERKS
EXCEPTIONS
NO_AUTHORITY       = 1
OTHERS             = 2.
IF SY-SUBRC NE 0.

  delete itab_plant.
  ENDIF.
ENDLOOP.

DESCRIBE TABLE ITAB_PLANT.
IF SY-TFILL = 0.
  MESSAGE 'No authorization for Plant' TYPE 'E'.
ENDIF.


REFRESH s_plant.
CLEAR   s_plant.
loop at itab_plant.
  move : itab_plant-werks to s_plant-low.
  move : 'I'   TO s_plant-SIGN.
  MOVE : 'EQ' TO s_plant-OPTION.
  append s_plant.
endloop.

ENDIF.

**Ended for Plant Authorisation Check by sujatha-For Mod-Id 1.2-Ends
*** Start - Bhu for stock details as per MB52 21.01.03
  CLEAR                      COLLECTOR-LGORT.            "n577268
                                                         "n577268
 IF COLLECTOR-LGORT IN S_STLOC.                          "n577268
   CLEAR                   G_FLAG_SUPPRESS_INIT_LGORT.   "n577268
 ELSE.                                                   "n577268
   MOVE 'X'                TO G_FLAG_SUPPRESS_INIT_LGORT."n577268
 ENDIF.                                                  "n577268


  PERFORM GET_STOCK.
*** End - Bhu for stock details as per MB52 21.01.03
*** Start - Zedek add to move authorized records to itab_mat 08.03.04
  LOOP AT BESTAND WHERE LABST GT 0.
    AUTHORITY-CHECK OBJECT 'M_MSEG_WWA' ID 'WERKS' FIELD
                                       BESTAND-WERKS.
    IF SY-SUBRC = 0.
      AUTHORITY-CHECK OBJECT 'M_MSEG_LGO' ID 'LGORT' FIELD
                                               BESTAND-LGORT.
      IF SY-SUBRC = 0.
        MOVE-CORRESPONDING BESTAND TO ITAB_MAT.
        MOVE : BESTAND-LABST TO ITAB_MAT-CLABS.
        APPEND ITAB_MAT.
        CLEAR ITAB_MAT.
      ENDIF.
    ENDIF.
  ENDLOOP.
*** End- Zedek add to move authorized records to itab_mat 08.03.04
*Start- Zedek added For checking no. records 08.03.2004
  DESCRIBE TABLE ITAB_MAT LINES N.
  IF N = 0 .
    MESSAGE I001(ZAT).
    STOP.
  ENDIF.
*End - Zedek added For checking no. records 08.03.2004
*coede inserted by senthil
*for all positive indicators 'S'
  SELECT MBLNR ZEILE MATNR EBELN WERKS MJAHR SHKZG MENGE PS_PSP_PNR
         ABLAD AUFNR KOSTL WEMPF BWART SMBLN SMBLP BWTAR LGORT
         INTO CORRESPONDING FIELDS OF TABLE ITAB_MVT
         FROM MSEG FOR ALL ENTRIES IN ITAB_MAT
         WHERE MATNR = ITAB_MAT-MATNR AND
                WERKS = ITAB_MAT-WERKS AND
                CHARG = ITAB_MAT-CHARG AND
*                BWTAR = ITAB_MAT-CHARG AND
                LGORT = ITAB_MAT-LGORT AND
                SHKZG = 'S' AND SOBKZ NE 'Q'."AND
*                BUKRS IN BUKREIS. " Added for Company code selection Mod-IDd1.1.
*LOOP AT ITAB_MAT.
*    SELECT * FROM MSEG
*          WHERE MATNR = ITAB_MAT-MATNR AND
*                WERKS = ITAB_MAT-WERKS AND
*                BWTAR = ITAB_MAT-CHARG AND
*                LGORT = ITAB_MAT-LGORT AND
*                SHKZG = 'S' AND SOBKZ NE 'Q'.
*    MOVE : MSEG-MBLNR TO ITAB_MVT-MBLNR,
*            MSEG-ZEILE TO ITAB_MVT-ZEILE,
*            MSEG-MATNR TO ITAB_MVT-MATNR ,
*            MSEG-EBELN TO ITAB_MVT-EBELN ,
*            MSEG-WERKS TO ITAB_MVT-WERKS ,
*            MSEG-MJAHR TO ITAB_MVT-MJAHR ,
*          MSEG-SHKZG TO ITAB_MVT-SHKZG ,
*          MSEG-MENGE TO ITAB_MVT-MENGE ,
*          MSEG-PS_PSP_PNR TO ITAB_MVT-PS_PSP_PNR,
*          MSEG-ABLAD TO ITAB_MVT-ABLAD   ,
*          MSEG-AUFNR TO ITAB_MVT-AUFNR ,
*          MSEG-KOSTL TO ITAB_MVT-KOSTL ,
*          MSEG-WEMPF TO ITAB_MVT-WEMPF ,
*          MSEG-BWART TO ITAB_MVT-BWART ,
*          MSEG-SMBLN TO ITAB_MVT-SMBLN ,
*          MSEG-SMBLP TO ITAB_MVT-SMBLP ,
*          MSEG-BWTAR TO ITAB_MVT-BWTAR ,
*          MSEG-LGORT TO ITAB_MVT-LGORT.
*          APPEND ITAB_MVT.
* ENDSELECT.
*ENDLOOP.


*for all Negative indicators 'H'
  SELECT MBLNR ZEILE MATNR EBELN WERKS MJAHR SHKZG MENGE PS_PSP_PNR
         ABLAD AUFNR KOSTL WEMPF BWART SMBLN SMBLP BWTAR LGORT
         INTO CORRESPONDING FIELDS OF TABLE ITAB_MVT1
         FROM MSEG FOR ALL ENTRIES IN ITAB_MAT
         WHERE MATNR = ITAB_MAT-MATNR AND
                WERKS = ITAB_MAT-WERKS AND
                CHARG = ITAB_MAT-CHARG AND
*                BWTAR = ITAB_MAT-CHARG AND
                LGORT = ITAB_MAT-LGORT AND
                SHKZG = 'H' AND SOBKZ NE 'Q' AND
                BUKRS IN BUKREIS. " Added for Company code selection Mod-Id-1.1.
*1. Loop itab_mvt.
*2. getthe corresponding adjustment document smbln smblp from MVT1
*if qunatity is equal in both cases knock off the entrie document
*if qunatity is not same knock of to the reversal qunatity only
*if found knock of the corresponding qunatity for the reversal document
  DATA CNT TYPE I.
  CNT = 0.
  LOOP AT ITAB_MVT WHERE NOT SMBLN IS INITIAL
                       OR NOT AUFNR IS INITIAL
                       OR NOT KOSTL IS INITIAL
                       OR NOT PS_PSP_PNR IS INITIAL.
    IF CNT = 0.
      LOOP AT ITAB_MVT1 WHERE MATNR = ITAB_MVT-MATNR AND
                               SMBLN = ITAB_MVT-MBLNR AND
                               SMBLP = ITAB_MVT-ZEILE AND
                               BWTAR = ITAB_MVT-BWTAR AND
                               LGORT = ITAB_MVT-LGORT.
*      ' this is to find out the reference from h to S
        CNT = 1.
        PMENGE = ITAB_MVT-MENGE - ITAB_MVT1-MENGE.
        IF PMENGE = 0.
           DELETE ITAB_MVT.
           CLEAR ITAB_MVT.
           DELETE ITAB_MVT1.
           CLEAR ITAB_MVT1.
           EXIT.
        ELSEIF PMENGE > 0.
           MOVE PMENGE TO ITAB_MVT-MENGE.
           MODIFY ITAB_MVT.
           DELETE ITAB_MVT1.
           CLEAR ITAB_MVT1.
        ELSEIF PMENGE < 0.
           MOVE PMENGE TO ITAB_MVT1-MENGE.
           MODIFY ITAB_MVT1.
           DELETE ITAB_MVT.
           CLEAR ITAB_MVT.
           EXIT.
        ENDIF.
        CLEAR ITAB_MVT1.
      ENDLOOP.
    ENDIF.
*      THis is to find out the reference from S TO H
    IF CNT = 0.
      LOOP AT ITAB_MVT1 WHERE MATNR = ITAB_MVT-MATNR AND
                                MBLNR = ITAB_MVT-SMBLN AND
                                ZEILE = ITAB_MVT-SMBLP AND
                                BWTAR = ITAB_MVT-BWTAR AND
                                LGORT = ITAB_MVT-LGORT.
        CNT = 1.
        PMENGE = 0.
        PMENGE = ITAB_MVT-MENGE - ITAB_MVT1-MENGE.
        IF PMENGE = 0.
           DELETE ITAB_MVT.
           CLEAR ITAB_MVT.
           DELETE ITAB_MVT1.
           CLEAR ITAB_MVT1.
           EXIT.
        ELSEIF PMENGE > 0.
           MOVE PMENGE TO ITAB_MVT-MENGE.
           MODIFY ITAB_MVT.
           DELETE ITAB_MVT1.
           CLEAR ITAB_MVT1.
        ELSEIF PMENGE < 0.
           MOVE PMENGE TO ITAB_MVT1-MENGE.
           MODIFY ITAB_MVT1.
           DELETE ITAB_MVT.
           CLEAR ITAB_MVT.
           EXIT.
        ENDIF.
        CLEAR ITAB_MVT1.
      ENDLOOP.
    ENDIF.
* start - zedek for aufnr on 17/03/2004
* for chcekuing the document with the reference document and the order
    IF CNT = 0 .
      IF ITAB_MVT-AUFNR NE SPACE.
        LOOP AT ITAB_MVT1 WHERE MATNR = ITAB_MVT-MATNR AND
                              AUFNR = ITAB_MVT-AUFNR AND
                              SMBLN = '' AND
                              SMBLP = '' AND
                              BWTAR = ITAB_MVT-BWTAR AND
                              LGORT = ITAB_MVT-LGORT.
           CNT = 1.
           PMENGE = 0.
           PMENGE = ITAB_MVT-MENGE - ITAB_MVT1-MENGE.
           IF PMENGE = 0.
             DELETE ITAB_MVT.
             CLEAR ITAB_MVT.
             DELETE ITAB_MVT1.
             CLEAR ITAB_MVT1.
             EXIT.
           ELSEIF PMENGE > 0.
             MOVE PMENGE TO ITAB_MVT-MENGE.
             MODIFY ITAB_MVT.
             DELETE ITAB_MVT1.
             CLEAR ITAB_MVT1.
           ELSEIF PMENGE < 0.
             MOVE PMENGE TO ITAB_MVT1-MENGE.
             MODIFY ITAB_MVT1.
             DELETE ITAB_MVT.
             CLEAR ITAB_MVT.
             EXIT.
           ENDIF.
           CLEAR ITAB_MVT1.
        ENDLOOP.
      ENDIF.
    ENDIF.
*** start - Project Reference Item        23.03.04
    IF CNT = 0 .
      IF ITAB_MVT-PS_PSP_PNR NE SPACE.
        LOOP AT ITAB_MVT1 WHERE MATNR = ITAB_MVT-MATNR AND
                              PS_PSP_PNR = ITAB_MVT-PS_PSP_PNR AND
                              SMBLN = '' AND
                              SMBLP = '' AND
                              BWTAR = ITAB_MVT-BWTAR AND
                              LGORT = ITAB_MVT-LGORT.
           CNT = 1.
           PMENGE = 0.
           PMENGE = ITAB_MVT-MENGE - ITAB_MVT1-MENGE.
           IF PMENGE = 0.
             DELETE ITAB_MVT.
             CLEAR ITAB_MVT.
             DELETE ITAB_MVT1.
             CLEAR ITAB_MVT1.
             EXIT.
           ELSEIF PMENGE > 0.
             MOVE PMENGE TO ITAB_MVT-MENGE.
             MODIFY ITAB_MVT.
             DELETE ITAB_MVT1.
             CLEAR ITAB_MVT1.
           ELSEIF PMENGE < 0.
             MOVE PMENGE TO ITAB_MVT1-MENGE.
             MODIFY ITAB_MVT1.
             DELETE ITAB_MVT.
             CLEAR ITAB_MVT.
             EXIT.
           ENDIF.
           CLEAR ITAB_MVT1.
        ENDLOOP.
      ENDIF.
    ENDIF.
*** End - Project Reference Item        23.03.04
*End - zedek for aufnr on 17/03/2004
* for incorporation of cost center in the material movement
    IF CNT = 0 .
      IF ITAB_MVT-KOSTL NE SPACE.
        LOOP AT ITAB_MVT1 WHERE MATNR = ITAB_MVT-MATNR AND
                                KOSTL = ITAB_MVT-KOSTL AND
                                SMBLN = '' AND
                                SMBLP = '' AND
*ADDED BY SUJITHA
                                BWART = ITAB_MVT-BWART AND
                                BWTAR = ITAB_MVT-BWTAR AND
                                LGORT = ITAB_MVT-LGORT.
           CNT = 1.
           PMENGE = 0.
           PMENGE = ITAB_MVT-MENGE - ITAB_MVT1-MENGE.
           IF PMENGE = 0.
             DELETE ITAB_MVT.
             CLEAR ITAB_MVT.
             DELETE ITAB_MVT1.
             CLEAR ITAB_MVT1.
             EXIT.
           ELSEIF PMENGE > 0.
             MOVE PMENGE TO ITAB_MVT-MENGE.
             MODIFY ITAB_MVT.
             DELETE ITAB_MVT1.
             CLEAR ITAB_MVT1.
           ELSEIF PMENGE < 0.
             MOVE PMENGE TO ITAB_MVT1-MENGE.
             MODIFY ITAB_MVT1.
             DELETE ITAB_MVT.
             CLEAR ITAB_MVT.
             EXIT.
           ENDIF.
           CLEAR ITAB_MVT1.
        ENDLOOP.
      ENDIF.
    ENDIF.
    CNT = 0.
    CLEAR ITAB_MVT.
  ENDLOOP.
***start - zedek commented and changed on 16/03/2004
* zedek - end of change 19/01/2004
  LOOP AT ITAB_MVT.
    SELECT BUDAT CPUDT CPUTM FROM MKPF
           INTO CORRESPONDING FIELDS OF ITAB_MVT
           WHERE MBLNR = ITAB_MVT-MBLNR AND
                 MJAHR = ITAB_MVT-MJAHR AND
                 BUDAT LE SY-DATUM.
      MODIFY ITAB_MVT.
      CLEAR ITAB_MVT.
    ENDSELECT.
  ENDLOOP.
  SORT ITAB_MVT DESCENDING BY BWTAR BUDAT CPUDT CPUTM .
* zedek included on 12/02/04
  LOOP AT ITAB_MAT.
    YMENGE = ITAB_MAT-CLABS.
    FLAG = 0.
    MOVE ITAB_MAT-CLABS TO ITAB_FINAL-CLABS.
    LOOP AT ITAB_MVT WHERE MATNR = ITAB_MAT-MATNR AND
                             WERKS = ITAB_MAT-WERKS AND
                             LGORT = ITAB_MAT-LGORT AND
                             BWTAR = ITAB_MAT-CHARG.
* zedek - removes bwtar = itab_mat-charg lgort = itab_mat-lgort
* on 19/01/2004
      IF FLAG1 = 1.
        IF ITAB_MVT-SHKZG = 'S'.
           X1 = X1 + ITAB_MVT-MENGE.
           IF X1 > 0.
             IF YMENGE > X1.
               YMENGE = YMENGE - X1.
               MOVE YMENGE TO ITAB_FINAL-MENGE.
             ELSE.
               MOVE X1 TO ITAB_FINAL-MENGE.
            ENDIF.
          ENDIF.
        ENDIF.
        FLAG1 = 0.
      ENDIF.
      IF YMENGE > 0 AND FLAG = 0.
        IF ITAB_MVT-SHKZG = 'S'.
          IF YMENGE < ITAB_MVT-MENGE.
            MOVE YMENGE TO ITAB_FINAL-MENGE.
            FLAG = 1.
          ELSE.
            YMENGE = YMENGE - ITAB_MVT-MENGE.
            MOVE ITAB_MVT-MENGE TO ITAB_FINAL-MENGE.
          ENDIF.
** changing + to -
        ELSEIF ITAB_MVT-SHKZG = 'H'.
          X1 = X1 - ITAB_MVT-MENGE.
          FLAG1 = 1.
*          ymenge = ymenge - itab_mvt-menge.
*          MOVE itab_mvt-menge TO itab_final-menge.
        ENDIF.
** end of change
        IF FLAG1 = 0.
*         MOVE itab_mat-labst TO itab_final-labst.
          MOVE ITAB_MVT-MBLNR TO ITAB_FINAL-MBLNR.
          MOVE ITAB_MVT-EBELN TO ITAB_FINAL-EBELN.
          MOVE ITAB_MVT-BWART TO ITAB_FINAL-BWART.
          MOVE ITAB_MVT-MATNR TO ITAB_FINAL-MATNR.
          MOVE ITAB_MAT-MAKTX TO ITAB_FINAL-MAKTX.
          MOVE ITAB_MAT-MEINS TO ITAB_FINAL-MEINS.
          MOVE ITAB_MAT-MTART TO ITAB_FINAL-MTART.
          MOVE ITAB_MAT-MATKL TO ITAB_FINAL-MATKL.
          MOVE ITAB_MAT-EXTWG TO ITAB_FINAL-EXTWG.
          MOVE ITAB_MAT-LGPBE TO ITAB_FINAL-LGPBE.
          MOVE ITAB_MAT-LGORT TO ITAB_FINAL-LGORT.
          MOVE ITAB_MVT-BUDAT TO ITAB_FINAL-BUDAT.
          MOVE ITAB_MVT-WERKS TO ITAB_FINAL-WERKS.
          MOVE ITAB_MVT-SHKZG TO ITAB_FINAL-SHKZG.
          MOVE ITAB_MVT-ABLAD TO ITAB_FINAL-ABLAD.
          MOVE ITAB_MVT-WEMPF TO ITAB_FINAL-WEMPF.
          MOVE ITAB_MVT-BEDNR TO ITAB_FINAL-BEDNR.
          MOVE ITAB_MVT-AFNAM TO ITAB_FINAL-AFNAM.
          MOVE ITAB_MVT-BANFN TO ITAB_FINAL-BANFN.
          MOVE ITAB_MVT-BADAT TO ITAB_FINAL-BADAT.
          MOVE ITAB_MVT-BWTAR TO ITAB_FINAL-BWTAR.
          MOVE ITAB_MVT-SHKZG TO ITAB_FINAL-SHKZG.
          MOVE ITAB_MVT-ZEILE TO ITAB_FINAL-ZEILE.
          IF ITAB_FINAL-CLABS = '0.000'.
            ITAB_FINAL-CLABS = '' .
          ENDIF.
*        MOVE ymenge TO itab_final-menge.
          APPEND ITAB_FINAL.
          CLEAR ITAB_FINAL.
        ENDIF.
      ENDIF.


   ENDLOOP.

* zedek -   Includeed on 12/02/04
* start - zedek on 11/03/2004

*     READ TABLE itab_mat WITH KEY matnr = itab_mvt-matnr
*                              werks = itab_mvt-werks
*                              charg = itab_mvt-bwtar.

     READ TABLE ITAB_MVT WITH KEY MATNR = ITAB_MAT-MATNR
                                 WERKS = ITAB_MAT-WERKS
                                 BWTAR = ITAB_MAT-CHARG.

* end - zedek on 11/03/2004


     IF SY-SUBRC <> 0.


        MOVE-CORRESPONDING ITAB_MAT TO ITAB_FINAL.
        APPEND ITAB_FINAL.


     ENDIF.

* zedek - end of Include on 12/02/04


    ENDLOOP.

*zedek - end of include on 12/02/04

    LOOP AT ITAB_FINAL.

     SELECT   BEDNR AFNAM BANFN BADAT INTO
            (ITAB_FINAL-BEDNR, ITAB_FINAL-AFNAM,
             ITAB_FINAL-BANFN,ITAB_FINAL-BADAT)
         FROM EBAN WHERE EBELN = ITAB_FINAL-EBELN AND
                         MATNR = ITAB_FINAL-MATNR AND

*start - zedek included werks on 26/02/04

                          WERKS = ITAB_FINAL-WERKS.

*end - zedek included werks on 26/02/04

        IF ITAB_FINAL-EBELN <> ''.

         MODIFY ITAB_FINAL.
         CLEAR ITAB_FINAL.

        ENDIF.

     ENDSELECT.

    ENDLOOP.




    LOOP AT ITAB_FINAL.

*start - zedek - included into this loop for performance 12/03/04
   SELECT SINGLE MAKTX FROM MAKT
     INTO ITAB_FINAL-MAKTX
   WHERE MATNR EQ ITAB_FINAL-MATNR.


   SELECT SINGLE LGPBE FROM MARD
    INTO ITAB_FINAL-LGPBE
  WHERE MATNR = ITAB_FINAL-MATNR
      AND WERKS = ITAB_FINAL-WERKS
      AND LGORT = ITAB_FINAL-LGORT.


   SELECT SINGLE EXTWG FROM MARA
    INTO CORRESPONDING FIELDS OF ITAB_FINAL
  WHERE MATNR = ITAB_FINAL-MATNR.

* start - zedek - included plant material in conditions - 26/02/04
* start - zedek - included mjahr in itab_final - 26/02/04
*COMMENTED BY SUJITHA
*    SELECT SINGLE BWART MJAHR FROM MSEG
*      INTO (ITAB_FINAL-BWART , ITAB_FINAL-MJAHR )
*    WHERE MBLNR = ITAB_FINAL-MBLNR
*          AND WERKS = ITAB_FINAL-WERKS
*          AND MATNR = ITAB_FINAL-MATNR
*          AND CHARG = ITAB_FINAL-BWTAR
*          AND ZEILE = ITAB_FINAL-ZEILE.
* end - zedek - included plant material in conditions - 26/02/04


    SELECT SINGLE BKTXT FROM MKPF
      INTO (ITAB_FINAL-BKTXT)
    WHERE MBLNR = ITAB_FINAL-MBLNR
* start - zedek - included mjahr in conditions - 26/02/04
          AND MJAHR = ITAB_FINAL-MJAHR.
* end - zedek - included mjahr in conditions - 26/02/04

   SELECT SINGLE BTEXT FROM T156HT
    INTO (ITAB_FINAL-BTEXT)
   WHERE BWART = ITAB_FINAL-BWART AND SPRAS = 'EN'.

* End - zedek - included into this loop for performance 12/03/04

   MODIFY ITAB_FINAL.
   CLEAR ITAB_FINAL.

 ENDLOOP.


* Start zedek - Included on 04/03/04

 LOOP AT ITAB_FINAL.
   W_REMARKS = ITAB_FINAL-MATNR.
   CALL FUNCTION 'READ_TEXT'
        EXPORTING
             CLIENT    = SY-MANDT
             ID        = 'BEST'
             LANGUAGE = 'E'
             NAME      = W_REMARKS
               OBJECT    = 'MATERIAL'
          TABLES
               LINES     = ITLINE
          EXCEPTIONS
               NOT_FOUND = 1.

    IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*          WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

     LOOP AT ITLINE .
       CONCATENATE ITAB_FINAL-PTXT ITLINE-TDLINE INTO ITAB_FINAL-PTXT.
       MODIFY ITAB_FINAL.
     ENDLOOP.

     CLEAR ITAB_FINAL.


     CLEAR W_REMARKS.
     REFRESH ITLINE.
     CLEAR ITLINE.

    ENDLOOP.

*added by lakshya on 16/02/2007 to get the total price of valuated stock
loop at itab_final.
  select single verpr from mbew
                      into uprice
                      where matnr eq itab_final-matnr
                      and   bwkey eq itab_final-werks
                      and   bwtar eq itab_final-bwtar.
  if sy-subrc eq 0.
   itab_final-tprice = itab_final-menge * uprice.
   modify itab_final.
   clear itab_final.
  endif.
endloop.
*end of addition by lakshya

* End zedek - Included on 04/03/04

***Start Zedek - sort MATNR in Ascending Order wise 17/02/2004

    SORT ITAB_FINAL ASCENDING BY MATNR LGORT DESCENDING BUDAT MBLNR
     .

* SORT ITAB_FINAL DESCENDING BY MBLNR.
***End zedek - sort MATNR in Ascending Order wise 17/02/2004



    PERFORM               FIELDCATALOG.
    PERFORM : SORT_INIT USING GT_SORT[].
    DATA : L_F_CHECK(01)      TYPE C.

*   CALL FUNCTION ALV_DETAIL_FUNC
*        EXPORTING
*             I_INTERFACE_CHECK         = L_F_CHECK
*             I_CALLBACK_PROGRAM        = G_REPID
*             IS_LAYOUT                 = GS_LAYOUT
*               IT_FIELDCAT               =   FIELDCAT[]
*               IT_SORT                   =   SORT[]
*               I_CALLBACK_USER_COMMAND   =   'USER_COMMAND2'
*               I_DEFAULT                 =   'X'
*               I_SAVE                    =   'A'
*               IS_VARIANT                =   VARIANTE_FLAT
*               IS_PRINT                  =   ALV_PRINT
*          TABLES
*               T_OUTTAB                  = ITAB_FINAL
*          EXCEPTIONS
*               OTHERS                    = 2.

    CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
         EXPORTING
              I_CALLBACK_PROGRAM      = G_REPID
              IS_LAYOUT               = GS_LAYOUT
              IT_SORT                 = GT_SORT[]
              I_CALLBACK_USER_COMMAND = 'USER_COMMAND2'
              IT_FIELDCAT             = FIELDCAT[]
              IS_VARIANT              = VARIANTE_FLAT
              IS_PRINT                = ALV_PRINT
              I_DEFAULT               = 'X'
              I_SAVE                  = 'A'
         TABLES
              T_OUTTAB                = ITAB_FINAL.



*&---------------------------------------------------------------------*
*&      Form SORT_INIT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_SORT[] text
*----------------------------------------------------------------------*
FORM SORT_INIT USING RT_SORT TYPE SLIS_T_SORTINFO_ALV.

    DATA   LT_SORT TYPE SLIS_SORTINFO_ALV.

    LT_SORT-SPOS = 1.
    LT_SORT-FIELDNAME = 'MATNR'.
    LT_SORT-UP = 'X'.
    APPEND LT_SORT TO RT_SORT.

    LT_SORT-SPOS = 2.
    LT_SORT-FIELDNAME = 'LGORT'.
    LT_SORT-UP = 'X'.
    APPEND LT_SORT TO RT_SORT.

    LT_SORT-SPOS = 3.
    LT_SORT-FIELDNAME = 'BUDAT'.
    LT_SORT-DOWN = 'X'.
    APPEND LT_SORT TO RT_SORT.

    LT_SORT-SPOS = 4.
    LT_SORT-FIELDNAME = 'MBLNR'.
    LT_SORT-DOWN = 'X'.
    APPEND LT_SORT TO RT_SORT.

ENDFORM.                      " SORT_INIT
*&---------------------------------------------------------------------*
*&      Form FIELDCAT_INIT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_FIELDCAT[] text
*----------------------------------------------------------------------*
FORM FIELDCATALOG.

*   W_COL = 1.
*
*   DATA: fieldcat TYPE SLIS_FIELDCAT_ALV.

***Start - ADD FOR plant in display bhu/zedk 03.02.04
***END - ADD FOR plant in display bhu/zedk 03.02.04

    CLEAR FIELDCAT.
    FIELDCAT-TABNAME     = 'ITAB_FINAL'.
    FIELDCAT-FIELDNAME   = 'WERKS'.
    FIELDCAT-OUTPUTLEN   = 10.
    FIELDCAT-JUST        = 'L'.
    FIELDCAT-SELTEXT_L   = 'Plant'.
    APPEND FIELDCAT.


    FIELDCAT-TABNAME     = 'ITAB_FINAL'.
    FIELDCAT-FIELDNAME   = 'MATNR'.
    FIELDCAT-OUTPUTLEN   = 15.
    FIELDCAT-JUST        = 'L'.
    FIELDCAT-SELTEXT_L   = 'MATERIAL'.
    APPEND FIELDCAT.



    CLEAR FIELDCAT.
    FIELDCAT-TABNAME     = 'ITAB_FINAL'.
    FIELDCAT-FIELDNAME   = 'MAKTX'.
    FIELDCAT-OUTPUTLEN   = 40.
    FIELDCAT-JUST        = 'L'.
    FIELDCAT-SELTEXT_L   = 'M Desc'.
    APPEND FIELDCAT.



* start - zedek included on 12/02/04

    CLEAR FIELDCAT.
    FIELDCAT-TABNAME     = 'ITAB_FINAL'.
    FIELDCAT-FIELDNAME   = 'BWART'.
    FIELDCAT-OUTPUTLEN   = 15.
    FIELDCAT-JUST        = 'L'.
    FIELDCAT-SELTEXT_L   = 'Mvt'.
    APPEND FIELDCAT.

* end of include on 12/02/04

* start - zedek included on 13/02/04
 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'BTEXT'.
 FIELDCAT-OUTPUTLEN   = 20.
 FIELDCAT-JUST        = 'L'.
 FIELDCAT-SELTEXT_L   = 'Mov Type Text'.
 APPEND FIELDCAT.

* end of include on 13/02/04

 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'MTART'.
 FIELDCAT-OUTPUTLEN   = 10.
 FIELDCAT-JUST        = 'L'.
 FIELDCAT-SELTEXT_L   = 'M Typ'.
 APPEND FIELDCAT.

 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'MATKL'.
 FIELDCAT-OUTPUTLEN   = 15.
 FIELDCAT-JUST        = 'L'.
 FIELDCAT-SELTEXT_L   = 'M Gp'.
 APPEND FIELDCAT.

 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'EXTWG'.
 FIELDCAT-OUTPUTLEN   = 15.
 FIELDCAT-JUST        = 'L'.
 FIELDCAT-SELTEXT_L   = 'Ext Mat Gp'.
 APPEND FIELDCAT.

 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'LGORT'.
 FIELDCAT-OUTPUTLEN   = 15.
 FIELDCAT-JUST        = 'L'.
 FIELDCAT-SELTEXT_L   = 'SLoc'.
 APPEND FIELDCAT.


 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'LGPBE'.
 FIELDCAT-OUTPUTLEN   = 10.
 FIELDCAT-JUST        = 'L'.
 FIELDCAT-SELTEXT_L   = 'StBin'.
 APPEND FIELDCAT.

 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'BWTAR'.
 FIELDCAT-OUTPUTLEN   = 15.
 FIELDCAT-JUST        = 'L'.
 FIELDCAT-SELTEXT_L   = 'Val Type'.
 APPEND FIELDCAT.
 CLEAR FIELDCAT.
 FIELDCAT-TABNAME    = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME  = 'MEINS'.
 FIELDCAT-OUTPUTLEN  = 10.
 FIELDCAT-JUST       = 'L'.
 FIELDCAT-DO_SUM = 'X'.
 FIELDCAT-SELTEXT_L  = 'UOM'.
 APPEND FIELDCAT.


 CLEAR FIELDCAT.
 FIELDCAT-COL_POS    = W_COL.
 FIELDCAT-TABNAME    = 'ITAB_FINAL'.
 FIELDCAT-QFIELDNAME = 'MEINS'.
 FIELDCAT-FIELDNAME  = 'CLABS'.
 FIELDCAT-OUTPUTLEN  = 10.
 FIELDCAT-DO_SUM = 'X'.
 FIELDCAT-NO_ZERO = 'X'.
 FIELDCAT-SELTEXT_L  = 'Stk'.
 APPEND FIELDCAT.


 CLEAR FIELDCAT.
 FIELDCAT-TABNAME    = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME  = 'MBLNR'.
 FIELDCAT-OUTPUTLEN  = 12.
 FIELDCAT-JUST       = 'L'.
 FIELDCAT-NO_ZERO = 'X'.
 FIELDCAT-SELTEXT_L  = 'M Doc'.
 APPEND FIELDCAT.

* start - zedek included on 12/02/04

 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'BKTXT'.
 FIELDCAT-OUTPUTLEN   = 25.
 FIELDCAT-JUST        = 'L'.
 FIELDCAT-SELTEXT_L   = 'Doc Header Text'.
 APPEND FIELDCAT.

  CLEAR FIELDCAT.
  FIELDCAT-TABNAME     = 'ITAB_FINAL'.
  FIELDCAT-FIELDNAME   = 'MENGE'.
  FIELDCAT-OUTPUTLEN   = 10.
***start - zedek added total for qty - 19/03/2004
  FIELDCAT-DO_SUM = 'X'.
***End - zedek added total for qty - 19/03/2004
  FIELDCAT-SELTEXT_L   = 'Qty'.
  APPEND FIELDCAT.

*added by lakshya on 16/02/2007 to include price
  CLEAR FIELDCAT.
  FIELDCAT-TABNAME     = 'ITAB_FINAL'.
  FIELDCAT-FIELDNAME   = 'TPRICE'.
  FIELDCAT-OUTPUTLEN   = 16.
  FIELDCAT-DO_SUM = 'X'.
  FIELDCAT-SELTEXT_L   = 'Price'.
  APPEND FIELDCAT.
*end of addition by lakshya
 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'ABLAD'.
 FIELDCAT-OUTPUTLEN   = 15.
 FIELDCAT-JUST        = 'L'.
 FIELDCAT-SELTEXT_L   = 'UnLoading Point'.
 APPEND FIELDCAT.


 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'WEMPF'.
 FIELDCAT-OUTPUTLEN   = 17.
 FIELDCAT-JUST        = 'L'.
 FIELDCAT-SELTEXT_L   = 'Goods Recp'.
 APPEND FIELDCAT.

 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'EBELN'.
 FIELDCAT-OUTPUTLEN   = 12.
 FIELDCAT-SELTEXT_L   = 'Pur Doc'.
 APPEND FIELDCAT.

 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'BUDAT'.
 FIELDCAT-OUTPUTLEN   = 10.
 FIELDCAT-SELTEXT_L   = 'Post Date'.
 APPEND FIELDCAT.

 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'BANFN'.
 FIELDCAT-OUTPUTLEN   = 10.
 FIELDCAT-SELTEXT_L   = 'Pur Reqr'.
 APPEND FIELDCAT.


 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'BADAT'.
 FIELDCAT-OUTPUTLEN   = 10.
 FIELDCAT-SELTEXT_L   = 'PR Date'.
 APPEND FIELDCAT.

 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'AFNAM'.
 FIELDCAT-OUTPUTLEN   = 14.
 FIELDCAT-SELTEXT_L   = 'Reqnr'.
 APPEND FIELDCAT.

 CLEAR FIELDCAT.
 FIELDCAT-TABNAME     = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME   = 'BEDNR'.
 FIELDCAT-OUTPUTLEN   = 10.
 FIELDCAT-SELTEXT_L   = 'Track No'.
 APPEND FIELDCAT.

* Start zedek - Included on 04/03/04
 CLEAR FIELDCAT.
 FIELDCAT-TABNAME      = 'ITAB_FINAL'.
 FIELDCAT-FIELDNAME    = 'PTXT'.
 FIELDCAT-OUTPUTLEN    = 132.
 FIELDCAT-SELTEXT_L    = 'Material PO Text'.
 APPEND FIELDCAT.


* End zedek - Included on 04/03/04



ENDFORM.                    " FIELDCAT_INIT



*&---------------------------------------------------------------------*
*&      Form ALV_EVENT_INIT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
* --> p1          text
* <-- p2          text
*----------------------------------------------------------------------*
FORM ALV_EVENT_INIT.
* CLEAR ALV_EVENT.
* ALV_EVENT-NAME = SLIS_EV_TOP_OF_PAGE.
* ALV_EVENT-FORM = 'ALV_TOP_OF_PAGE'.
* APPEND ALV_EVENT TO GT_EVENTS.
*
* CLEAR ALV_EVENT.
* ALV_EVENT-NAME = SLIS_EV_TOP_OF_LIST.
* ALV_EVENT-FORM = 'ALV_TOP_OF_LIST'.
* APPEND ALV_EVENT TO GT_EVENTS.
*
* CLEAR ALV_EVENT.
* ALV_EVENT-NAME = SLIS_EV_END_OF_LIST.
* ALV_EVENT-FORM = 'ALV_END_OF_LIST'.
* APPEND ALV_EVENT TO GT_EVENTS.
*
* CLEAR ALV_EVENT.
* ALV_EVENT-NAME = SLIS_EV_END_OF_PAGE.
* ALV_EVENT-FORM = 'ALV_END_OF_PAGE'.
* CLEAR ALV_EVENT.
*
ENDFORM.                    " ALV_EVENT_INIT


*---------------------------------------------------------------------*
*       FORM user_command2                                            *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
* --> UCOMM                                                           *
* --> SELFIELD                                                        *
*---------------------------------------------------------------------*
FORM USER_COMMAND2 USING UCOMM LIKE SY-UCOMM SELFIELD TYPE SLIS_SELFIELD
                                                              .
  READ TABLE ITAB_FINAL INDEX SELFIELD-TABINDEX.
 CASE SELFIELD-SEL_TAB_FIELD.
   WHEN 'ITAB_FINAL-MBLNR'.
     SET PARAMETER ID 'MBN' FIELD ITAB_FINAL-MBLNR.
     CALL TRANSACTION 'MIGO' AND SKIP FIRST SCREEN.

   WHEN 'ITAB_FINAL-EBELN'.
     SET PARAMETER ID 'BES' FIELD ITAB_FINAL-EBELN.
     CALL TRANSACTION 'ME23N' AND SKIP FIRST SCREEN.

  ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form Get_stock
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
* --> p1          text
* <-- p2          text
*----------------------------------------------------------------------*
FORM GET_STOCK.
* Materials to be processed
  PA_SOND = ''.

 TYPES: BEGIN OF TY_MAT,
          MATNR LIKE MARA-MATNR,
          WERKS LIKE MARC-WERKS,
          XCHAR LIKE MARC-XCHAR,
          MTART LIKE MARA-MTART,
          MATKL LIKE MARA-MATKL,
          MEINS LIKE MARA-MEINS,
          TRAME LIKE MARC-TRAME,
          UMLMC LIKE MARC-UMLMC,
          LVORM_MARA        LIKE   MARA-LVORM,
          LVORM_MARC        LIKE   MARC-LVORM,
        END OF TY_MAT.

 DATA: T_MAT     TYPE TY_MAT OCCURS 0 WITH HEADER LINE,
       T_BATCH   TYPE TY_MAT OCCURS 0 WITH HEADER LINE,
       T_NOBATCH TYPE TY_MAT OCCURS 0 WITH HEADER LINE.

* buffer for reading working tables
  DATA : L_S_MAT     TYPE TY_MAT,
         L_F_MATNR   LIKE MAKT-MATNR,
         XMCHB       LIKE AM07M-MB52_XMCHB.
  RANGES: R_SOBKZ FOR MKOL-SOBKZ.
  XMCHB = 'X'.
************************************************************************
* Read material master data (MARA and MARC)
************************************************************************
  REFRESH COLLECTOR.

* take all matching entries, do not consider the deletion
* indicator
  SELECT MARA~MATNR WERKS XCHAR MTART MATKL MEINS TRAME UMLMC
         MARA~LVORM AS LVORM_MARA
         MARC~LVORM AS LVORM_MARC
         INTO CORRESPONDING FIELDS OF TABLE T_MAT
         FROM MARA INNER JOIN MARC
         ON MARA~MATNR = MARC~MATNR
         WHERE MARA~MATNR IN S_MATNR
           AND WERKS IN S_PLANT
           AND MTART IN S_MATYP
           AND MATKL IN S_MATGP
           AND EXTWG IN S_EXMGP.
****Commented by Sujitha.D for checking Material Types
****SR00259:Help desk request.
*           AND MARA~MTART IN
*     ('ZCSS','ZCON','ZESS','ZEQP','ZTOL',
*     'ZFUL').
**end comment.
************************************************************************
* Get "normal" stocks.
* If no detailed batch display is required,
* all data come from MARD. Otherwise, materials with batch
* management are extracted from MCHB, the rest from MARD.
* Dum-dibe-dum-dibe-dum. Dum.
************************************************************************
  REFRESH: T_BATCH, T_NOBATCH.
* Split the worklist into the parts for each table...
  IF XMCHB IS INITIAL.
    T_NOBATCH[] = T_MAT[].
  ELSE.
    LOOP AT T_MAT.
      IF T_MAT-XCHAR IS INITIAL.
        APPEND T_MAT TO T_NOBATCH.
      ELSE.
        APPEND T_MAT TO T_BATCH.
      ENDIF.
    ENDLOOP.
  ENDIF.

*Added by Mohan for picking from all tables
  IF S_VALTP IS INITIAL.
*End of addition by Mohan

*   Access MARD
*   I you think that instead of SELECT-APPEND we could have used
*   an array fetch, please wait for the table names to become different
*   from the internal fields. B.T.W.: The DB-interface also buffers.
      CLEAR COLLECTOR.
      READ TABLE T_NOBATCH INDEX 1 TRANSPORTING NO FIELDS.
      IF SY-SUBRC = 0.
        SELECT MATNR WERKS LGORT
               LABST UMLME INSME EINME SPEME RETME LVORM
               INTO (COLLECTOR-MATNR, COLLECTOR-WERKS, COLLECTOR-LGORT,
                     COLLECTOR-LABST, COLLECTOR-UMLME, COLLECTOR-INSME,
                     COLLECTOR-EINME, COLLECTOR-SPEME, COLLECTOR-RETME,
                     COLLECTOR-LVORM)
               FROM MARD
               FOR ALL ENTRIES IN T_NOBATCH
               WHERE MATNR = T_NOBATCH-MATNR
                 AND WERKS = T_NOBATCH-WERKS
                 AND LGORT IN S_STLOC.

*       save the MARD Key and deletion indicator for later
*       in table G_T_MARD_LV for use with special stocks
          IF NOT PA_SOND         IS INITIAL AND
             NOT COLLECTOR-LVORM IS INITIAL.
            MOVE-CORRESPONDING COLLECTOR
                                 TO G_S_MARD_LV.
            INSERT G_S_MARD_LV INTO TABLE G_T_MARD_LV.
          ENDIF.
        COLLECT                       COLLECTOR..
      ENDSELECT.
    ENDIF.

* Access MCHB
    CLEAR COLLECTOR.
    READ TABLE T_BATCH INDEX 1 TRANSPORTING NO FIELDS.
    IF SY-SUBRC = 0.
      SELECT MATNR WERKS LGORT CHARG
             CLABS CUMLM CINSM CEINM CSPEM CRETM LVORM
             INTO (COLLECTOR-MATNR, COLLECTOR-WERKS, COLLECTOR-LGORT,
                   COLLECTOR-CHARG,
                   COLLECTOR-LABST, COLLECTOR-UMLME, COLLECTOR-INSME,
                   COLLECTOR-EINME, COLLECTOR-SPEME, COLLECTOR-RETME,
                   COLLECTOR-LVORM)
             FROM MCHB
             FOR ALL ENTRIES IN T_BATCH
             WHERE MATNR = T_BATCH-MATNR
               AND WERKS = T_BATCH-WERKS
               AND LGORT IN S_STLOC.
*             AND charg IN charg.

        COLLECT COLLECTOR.
      ENDSELECT.
    ENDIF.

************************************************************************
* Transfer stocks from MARC (TRAME and UMLMC)
************************************************************************
    CLEAR COLLECTOR.

    LOOP AT T_MAT WHERE UMLMC <> 0 OR TRAME <> 0.
*     there are no lines with stock = zero

*        take the stocks from plant level only when the user   "n577268
*        does not restrict the storage location;               "n577268
         CHECK : G_FLAG_SUPPRESS_INIT_LGORT IS INITIAL.        "n577268

      COLLECTOR-MATNR =    T_MAT-MATNR.
      COLLECTOR-WERKS =    T_MAT-WERKS.
      COLLECTOR-UMLME =    T_MAT-TRAME + T_MAT-UMLMC.
      COLLECTOR-LVORM =    T_MAT-LVORM_MARC.
      APPEND COLLECTOR.
    ENDLOOP.

************************************************************************
* Consignment from vendor (MKOL)
* Read only if requested by one of the
* flags on the selection screen. Absolutely inconsistent, but
* due to compatibility...
* MKOL has a flag for deletion
************************************************************************

    IF     NOT PA_SOND IS INITIAL AND
           NOT T_MAT[] IS INITIAL.
         IF 'K' IN SO_SOBKZ OR
             'M' IN SO_SOBKZ.
           CLEAR                  COLLECTOR.

          SELECT MATNR WERKS LGORT CHARG SOBKZ LIFNR
                  SLABS SINSM SEINM SSPEM LVORM
                  INTO (COLLECTOR-MATNR, COLLECTOR-WERKS, COLLECTOR-LGORT,
                        COLLECTOR-CHARG, COLLECTOR-SOBKZ, COLLECTOR-LIFNR,
                        COLLECTOR-LABST, COLLECTOR-INSME, COLLECTOR-EINME,
                        COLLECTOR-SPEME, COLLECTOR-LVORM)
                  FROM MKOL
                  FOR ALL ENTRIES IN T_MAT
                  WHERE MATNR = T_MAT-MATNR
                    AND WERKS = T_MAT-WERKS
                    AND LGORT IN S_STLOC.
*                  AND charg IN charg
*                  and sobkz in so_sobkz.

           COLLECT COLLECTOR.
        ENDSELECT.
      ENDIF.
    ENDIF.

************************************************************************
* Special stocks at customer side (MSKU)
* MSKU has no flag for deletion
************************************************************************

    IF     NOT PA_SOND IS INITIAL AND
           NOT T_MAT[] IS INITIAL.
         IF 'V' IN SO_SOBKZ OR
         'W' IN SO_SOBKZ.
           CLEAR COLLECTOR.

          SELECT MATNR WERKS CHARG SOBKZ KUNNR
               KULAB KUINS KUEIN
               INTO (COLLECTOR-MATNR, COLLECTOR-WERKS, COLLECTOR-CHARG,
                     COLLECTOR-SOBKZ, COLLECTOR-KUNNR,
                     COLLECTOR-LABST, COLLECTOR-INSME, COLLECTOR-EINME)
               FROM MSKU
               FOR ALL ENTRIES IN T_MAT
               WHERE MATNR = T_MAT-MATNR
                 AND WERKS = T_MAT-WERKS
*               AND charg IN charg
                 AND SOBKZ IN SO_SOBKZ.

           COLLECT COLLECTOR.
        ENDSELECT.
      ENDIF.
    ENDIF.

************************************************************************
* Special stocks at vendor provision (MSLB)
* MSLB has no flag for deletion
************************************************************************

    IF    NOT   PA_SOND IS INITIAL AND
          NOT   T_MAT[] IS INITIAL AND
                   'O'     IN SO_SOBKZ.

         SELECT MATNR WERKS CHARG SOBKZ LIFNR
                LBLAB LBINS LBEIN
                INTO (COLLECTOR-MATNR, COLLECTOR-WERKS, COLLECTOR-CHARG,
                      COLLECTOR-SOBKZ, COLLECTOR-LIFNR,
                      COLLECTOR-LABST, COLLECTOR-INSME,
                      COLLECTOR-EINME)
                FROM MSLB
                FOR ALL ENTRIES IN T_MAT
                WHERE MATNR = T_MAT-MATNR
                  AND WERKS = T_MAT-WERKS
*                AND charg IN charg
                  AND SOBKZ IN SO_SOBKZ.

        COLLECT COLLECTOR.
      ENDSELECT.
    ENDIF.

************************************************************************
* Customer order stock (MSKA) and sum segment (MSSA) for valuation.
* Sum on the database and FOR ALL ENTRIES is not allowed from
* release 4.5 onwards, so the summation has to be done
* on the application server (here!).
* MSKA has no flag for deletion
************************************************************************

    IF    NOT   PA_SOND IS INITIAL AND
          NOT   T_MAT[] IS INITIAL AND
                  'E'     IN SO_SOBKZ.

         CLEAR COLLECTOR.

         SELECT MSKA~MATNR MSKA~WERKS LGORT CHARG MSKA~SOBKZ
                MSKA~VBELN MSKA~POSNR
                KALAB KAINS KASPE KAEIN KZBWS
                INTO (COLLECTOR-MATNR, COLLECTOR-WERKS, COLLECTOR-LGORT,
                      COLLECTOR-CHARG, COLLECTOR-SOBKZ,
                      COLLECTOR-VBELN, COLLECTOR-POSNR,
                      COLLECTOR-LABST, COLLECTOR-INSME, COLLECTOR-SPEME,
                      COLLECTOR-EINME, COLLECTOR-KZBWS)
                FROM MSKA INNER JOIN MSSA
                ON   MSKA~MATNR = MSSA~MATNR
                 AND MSKA~WERKS = MSSA~WERKS
                 AND MSKA~SOBKZ = MSSA~SOBKZ
                 AND MSKA~VBELN = MSSA~VBELN
                 AND MSKA~POSNR = MSSA~POSNR
                FOR ALL ENTRIES IN T_MAT
                WHERE MSKA~MATNR = T_MAT-MATNR
                  AND MSKA~WERKS = T_MAT-WERKS
                  AND MSKA~LGORT IN S_STLOC.
*                AND mska~charg IN charg.

           COLLECT COLLECTOR.
         ENDSELECT.

*   Transfer stocks for customer order (SATRA in MSSA)
      CLEAR COLLECTOR.
      SELECT MATNR WERKS SOBKZ VBELN POSNR KZBWS SATRA
             INTO (COLLECTOR-MATNR, COLLECTOR-WERKS, COLLECTOR-SOBKZ,
                   COLLECTOR-VBELN, COLLECTOR-POSNR,
                   COLLECTOR-KZBWS, COLLECTOR-UMLME)
             FROM MSSA
             FOR ALL ENTRIES IN T_MAT
             WHERE MATNR = T_MAT-MATNR
               AND WERKS = T_MAT-WERKS
               AND SOBKZ IN SO_SOBKZ
               AND SATRA <> 0.
        COLLECT COLLECTOR.
      ENDSELECT.
    ENDIF.

************************************************************************
* The same game for project stocks (MSPR/MSSQ).
* MSPR has no flag for deletion
************************************************************************

    IF    NOT   PA_SOND IS INITIAL AND
          NOT   T_MAT[] IS INITIAL AND
                'Q'     IN SO_SOBKZ.

         CLEAR COLLECTOR.

         SELECT MSPR~MATNR MSPR~WERKS LGORT CHARG MSPR~SOBKZ MSPR~PSPNR
                PRLAB PRINS PRSPE PREIN KZBWS
                INTO (COLLECTOR-MATNR, COLLECTOR-WERKS, COLLECTOR-LGORT,
                      COLLECTOR-CHARG, COLLECTOR-SOBKZ,
                      COLLECTOR-PSPNR,
                      COLLECTOR-LABST, COLLECTOR-INSME, COLLECTOR-SPEME,
                      COLLECTOR-EINME, COLLECTOR-KZBWS)
                FROM MSPR INNER JOIN MSSQ
                ON   MSPR~MATNR = MSSQ~MATNR
                 AND MSPR~WERKS = MSSQ~WERKS
                 AND MSPR~SOBKZ = MSSQ~SOBKZ
                 AND MSPR~PSPNR = MSSQ~PSPNR
                FOR ALL ENTRIES IN T_MAT
                WHERE MSPR~MATNR = T_MAT-MATNR
                  AND MSPR~WERKS = T_MAT-WERKS
                  AND MSPR~LGORT IN S_STLOC.
*                AND mspr~charg IN charg.

           COLLECT COLLECTOR.
         ENDSELECT.

*   Transfer stocks for projects (SQTRA in MSSQ)
      CLEAR COLLECTOR.
      SELECT MATNR WERKS SOBKZ PSPNR KZBWS SQTRA
             INTO (COLLECTOR-MATNR, COLLECTOR-WERKS, COLLECTOR-SOBKZ,
                   COLLECTOR-PSPNR,
                   COLLECTOR-KZBWS, COLLECTOR-UMLME)
             FROM MSSQ
             FOR ALL ENTRIES IN T_MAT
             WHERE MATNR = T_MAT-MATNR
               AND WERKS = T_MAT-WERKS
               AND SOBKZ IN SO_SOBKZ
               AND SQTRA <> 0.

        COLLECT COLLECTOR.
      ENDSELECT.
    ENDIF.

*Added by Mohan for Picking only from MCHB - 03.02.2004
  ELSE.
* Access MCHB
    CLEAR COLLECTOR.
    READ TABLE T_BATCH INDEX 1 TRANSPORTING NO FIELDS.
    IF SY-SUBRC = 0.
      SELECT MATNR WERKS LGORT CHARG
             CLABS CUMLM CINSM CEINM CSPEM CRETM LVORM
            INTO (COLLECTOR-MATNR, COLLECTOR-WERKS, COLLECTOR-LGORT,
                  COLLECTOR-CHARG,
                  COLLECTOR-LABST, COLLECTOR-UMLME, COLLECTOR-INSME,
                  COLLECTOR-EINME, COLLECTOR-SPEME, COLLECTOR-RETME,
                  COLLECTOR-LVORM)
            FROM MCHB
            FOR ALL ENTRIES IN T_BATCH
            WHERE MATNR = T_BATCH-MATNR
              AND WERKS = T_BATCH-WERKS
              AND LGORT IN S_STLOC
              AND CHARG IN S_VALTP.

        COLLECT COLLECTOR.
      ENDSELECT.
    ENDIF.

  ENDIF.
*End of addition by Mohan - 03.02.2004

************************************************************************
* Extract key-data for other tables.
************************************************************************
  DATA: BEGIN OF T_MAKTKEY OCCURS 0,
          MATNR LIKE MAKT-MATNR,
        END OF T_MAKTKEY,

* working area for the material description
        BEGIN OF L_S_MAKT,
          MATNR LIKE MAKT-MATNR,
          MAKTX LIKE MAKT-MAKTX,
        END OF L_S_MAKT,

       BEGIN OF T_MAKT OCCURS 0,
         MATNR LIKE MAKT-MATNR,
         MAKTX LIKE MAKT-MAKTX,
       END OF T_MAKT,

       BEGIN OF T_MCHAKEY OCCURS 0,
         MATNR LIKE MCHA-MATNR,
         WERKS LIKE MCHA-WERKS,
         CHARG LIKE MCHA-CHARG,
       END OF T_MCHAKEY,
       BEGIN OF T_MCHA OCCURS 0,
         MATNR LIKE MCHA-MATNR,
         WERKS LIKE MCHA-WERKS,
         CHARG LIKE MCHA-CHARG,
         BWTAR LIKE MCHA-BWTAR,
       END OF T_MCHA.

  REFRESH: T_MAKTKEY, T_MCHAKEY. "bestand.
* remove all items in bestand with wrong batch number. If we would
* not remove this, report will e.g. show materials which has
* only MARD entries, too.
  LOOP AT COLLECTOR. "WHERE charg IN charg.               "note 311770
    MOVE-CORRESPONDING COLLECTOR TO BESTAND.

*   fill the key of the special stocks into the field
*   assigment
    CASE    COLLECTOR-SOBKZ.
      WHEN 'E'.
        MOVE : 'X'                TO G_FLAG_SOBKZ-VBELN.
         WRITE : COLLECTOR-VBELN    TO BESTAND-SSNUM.
         MOVE : '/'                 TO BESTAND-SSNUM+10(01).
         WRITE : COLLECTOR-POSNR    TO BESTAND-SSNUM+12(08)
                                    NO-ZERO.
         CONDENSE                   BESTAND-SSNUM.

        WHEN 'K'.
          MOVE : 'X'                TO   G_FLAG_SOBKZ-LIFNR.
          WRITE : COLLECTOR-LIFNR   TO   BESTAND-SSNUM.

        WHEN 'M'.
          MOVE : 'X'                TO   G_FLAG_SOBKZ-LIFNR.
          WRITE : COLLECTOR-LIFNR   TO   BESTAND-SSNUM.

        WHEN 'O'.
          MOVE : 'X'                TO   G_FLAG_SOBKZ-LIFNR.
          WRITE : COLLECTOR-LIFNR   TO   BESTAND-SSNUM.

        WHEN 'Q'.
          MOVE : 'X'                TO   G_FLAG_SOBKZ-PSPNR.
          WRITE : COLLECTOR-PSPNR   TO   BESTAND-SSNUM.

        WHEN 'V'.
          MOVE : 'X'                TO   G_FLAG_SOBKZ-KUNNR.
          WRITE : COLLECTOR-KUNNR   TO   BESTAND-SSNUM.

        WHEN 'W'.
          MOVE : 'X'                TO   G_FLAG_SOBKZ-KUNNR.
          WRITE : COLLECTOR-KUNNR   TO   BESTAND-SSNUM.

     WHEN OTHERS.
       CLEAR                        BESTAND-SSNUM.
   ENDCASE.

   APPEND BESTAND.

   T_MAKTKEY-MATNR = BESTAND-MATNR.
   COLLECT T_MAKTKEY.

   IF BESTAND-CHARG <> SPACE.
     T_MCHAKEY-MATNR = BESTAND-MATNR.
     T_MCHAKEY-WERKS = BESTAND-WERKS.
     T_MCHAKEY-CHARG = BESTAND-CHARG.
     COLLECT T_MCHAKEY.
   ENDIF.
 ENDLOOP.

 FREE                         COLLECTOR.

************************************************************************
* Read additional tables
************************************************************************
  READ TABLE T_MAKTKEY INDEX 1 TRANSPORTING NO FIELDS.
  IF SY-SUBRC = 0.
    SELECT MATNR MAKTX INTO CORRESPONDING FIELDS OF TABLE T_MAKT
           FROM MAKT
           FOR ALL ENTRIES IN T_MAKTKEY
           WHERE MATNR = T_MAKTKEY-MATNR
             AND SPRAS = SY-LANGU.
    SORT T_MAKT BY MATNR.
  ENDIF.
* Read batch data only if values are requested
  READ TABLE T_MCHAKEY INDEX 1 TRANSPORTING NO FIELDS.
  IF SY-SUBRC = 0 AND NOVALUES IS INITIAL.
    SELECT MATNR WERKS CHARG BWTAR
           INTO CORRESPONDING FIELDS OF TABLE T_MCHA
           FROM MCHA
           FOR ALL ENTRIES IN T_MCHAKEY
           WHERE MATNR = T_MCHAKEY-MATNR
             AND WERKS = T_MCHAKEY-WERKS
             AND CHARG = T_MCHAKEY-CHARG.
    SORT T_MCHA BY MATNR WERKS CHARG.
  ENDIF.

************************************************************************
* Data definitions for the valuation extraction
************************************************************************
  DATA: BEGIN OF T_MBEWKEY OCCURS 0,
          MATNR LIKE MBEW-MATNR,
          BWKEY LIKE MBEW-BWKEY,
          BWTAR LIKE MBEW-BWTAR,
        END OF T_MBEWKEY,

*           workin table for the material stock valuation
            BEGIN OF T_MBEW OCCURS 0,
              MATNR LIKE MBEW-MATNR,
              BWKEY LIKE MBEW-BWKEY,
              BWTAR LIKE MBEW-BWTAR,
*             consider the valuation of the special stocks E, Q "n531604
              SOBKZ              LIKE EBEW-SOBKZ,               "n531604
              VBELN              LIKE EBEW-VBELN,               "n531604
              POSNR              LIKE EBEW-POSNR,               "n531604
              PSPNR              LIKE QBEW-PSPNR,               "n531604

              LBKUM(12) TYPE P DECIMALS 3,                                      "407810
              SALK3(12) TYPE P DECIMALS 2,                                      "388735
              VPRSV LIKE MBEW-VPRSV,                                            "353428
              VERPR LIKE MBEW-VERPR,                                            "353428
              STPRS LIKE MBEW-STPRS,                                            "353428
              PEINH LIKE MBEW-PEINH,                                            "353428
            END OF T_MBEW.

    DATA:   T_EBEWKEY   LIKE   T_MBEWKEY   OCCURS   0   WITH   HEADER   LINE.
    DATA:   T_QBEWKEY   LIKE   T_MBEWKEY   OCCURS   0   WITH   HEADER   LINE.
    DATA:   T_EBEW      LIKE   T_MBEW      OCCURS   0   WITH   HEADER   LINE.
    DATA:   T_QBEW      LIKE   T_MBEW      OCCURS   0   WITH   HEADER   LINE.

    DATA: BEGIN OF T_T134MKEY OCCURS 0,
            BWKEY LIKE T134M-BWKEY,
            MTART LIKE T134M-MTART,
          END OF T_T134MKEY,
          BEGIN OF T_T134M OCCURS 0,
            BWKEY LIKE T134M-BWKEY,
            MTART LIKE T134M-MTART,
            WERTU LIKE T134M-WERTU,
          END OF T_T134M.

************************************************************************
* Fill in additional data (first round) and extract the data
* for the access to the valuation tables.
************************************************************************
  SORT T_MAT BY MATNR WERKS.
    SORT G_T_ORGAN            BY WERKS.
    CLEAR : G_S_T001L,        G_S_ORGAN.

  LOOP AT BESTAND.
*   get the information per plant and storage location
*   with buffer
    IF G_FLAG_T001L = 'X'.
      IF BESTAND-WERKS = G_S_T001L-WERKS AND
          BESTAND-LGORT = G_S_T001L-LGORT.
      ELSE.
*       read with plant and storage location
        READ TABLE G_T_T001L   INTO G_S_T001L
          WITH TABLE KEY WERKS = BESTAND-WERKS
                         LGORT = BESTAND-LGORT.

         MOVE : BESTAND-WERKS TO G_S_T001L-WERKS,
                BESTAND-LGORT TO G_S_T001L-LGORT.

         IF SY-SUBRC <> 0.
            CLEAR             G_S_T001L-LGOBE.
         ENDIF.
       ENDIF.
     ENDIF.

*    take the storage bin from the buffer
     MOVE : G_S_T001L-LGOBE   TO BESTAND-LGOBE.


*    get the information per plant with buffer
     IF BESTAND-WERKS NE G_S_ORGAN-WERKS.
       READ TABLE G_T_ORGAN   INTO G_S_ORGAN
           WITH KEY WERKS = BESTAND-WERKS
                              BINARY SEARCH.

       IF SY-SUBRC <> 0.
*        sorry nothing found
         CLEAR                G_S_ORGAN.
         MOVE : BESTAND-WERKS TO G_S_ORGAN-WERKS.
       ENDIF.
     ENDIF.

*    take the following fields from the buffer
     MOVE : G_S_ORGAN-NAME1   TO BESTAND-NAME1,
            G_S_ORGAN-WAERS   TO BESTAND-WAERS,
            G_S_ORGAN-BWKEY   TO BESTAND-BWKEY.


*    get the information from the material master MARC
*    with buffer
     IF BESTAND-MATNR = L_S_MAT-MATNR AND
         BESTAND-WERKS = L_S_MAT-WERKS.
*      results are in the buffer
     ELSE.
       CLEAR                  L_S_MAT.
       MOVE : BESTAND-MATNR   TO L_S_MAT-MATNR,
              BESTAND-WERKS   TO L_S_MAT-WERKS.

        READ TABLE T_MAT       INTO L_S_MAT
           WITH KEY MATNR = BESTAND-MATNR
                    WERKS = BESTAND-WERKS
                               BINARY SEARCH.
      IF SY-SUBRC <> 0.
*       sorry nothing found
        CLEAR                L_S_MAT.
        MOVE : BESTAND-MATNR TO L_S_MAT-MATNR,
               BESTAND-WERKS TO L_S_MAT-WERKS.
      ENDIF.
    ENDIF.

*   take the results the buffer
    MOVE : L_S_MAT-MTART     TO   BESTAND-MTART,
           L_S_MAT-MATKL     TO   BESTAND-MATKL,
           L_S_MAT-MEINS     TO   BESTAND-MEINS.


*   if this entry has no deletion flag, take the
*   deletion flag from a higher level like MARA, MARC,
*   or MARDA
    IF BESTAND-LVORM IS INITIAL.
      IF      NOT L_S_MAT-LVORM_MARC IS INITIAL.
        MOVE L_S_MAT-LVORM_MARC
                             TO BESTAND-LVORM.
      ELSEIF NOT L_S_MAT-LVORM_MARA IS INITIAL.
        MOVE L_S_MAT-LVORM_MARA
                             TO BESTAND-LVORM.

      ELSEIF NOT G_T_MARD_LV[] IS INITIAL AND
             NOT BESTAND-LGORT IS INITIAL AND
             NOT BESTAND-SOBKZ IS INITIAL.
*      look for deletion flag in working table
*      g_t_mard_lv for a line with special stock
       IF BESTAND-MATNR = G_S_MARD_LV-MATNR AND
           BESTAND-WERKS = G_S_MARD_LV-WERKS AND
           BESTAND-LGORT = G_S_MARD_LV-LGORT.
       ELSE.
*        read table only after the key has changed
         READ TABLE G_T_MARD_LV INTO G_S_MARD_LV
         WITH TABLE KEY MATNR = BESTAND-MATNR
                        WERKS = BESTAND-WERKS
                        LGORT = BESTAND-LGORT.

          IF SY-SUBRC <> 0.
*           fill the buffer in case the entry does not exist
            MOVE : BESTAND-MATNR TO G_S_MARD_LV-MATNR,
                   BESTAND-WERKS TO G_S_MARD_LV-WERKS,
                   BESTAND-LGORT TO G_S_MARD_LV-LGORT.
            CLEAR                 G_S_MARD_LV-LVORM.
          ENDIF.

*          take the result from the buffer
           MOVE G_S_MARD_LV-LVORM    TO BESTAND-LVORM.
        ENDIF.
      ENDIF.
    ENDIF.


*   read the material short description after the material
*   number has changed
    IF BESTAND-MATNR NE L_S_MAKT-MATNR.
      READ TABLE T_MAKT      INTO L_S_MAKT
                   WITH KEY MATNR = BESTAND-MATNR
                              BINARY SEARCH.

      IF SY-SUBRC <> 0.
*       sorry nothing found
        CLEAR                 L_S_MAKT-MAKTX.
        MOVE BESTAND-MATNR    TO L_S_MAKT-MATNR.
      ENDIF.
    ENDIF.

*   take the results the buffer
    MOVE : L_S_MAKT-MAKTX    TO   BESTAND-MAKTX.


    IF BESTAND-CHARG <> SPACE AND NOVALUES IS INITIAL.
      READ TABLE T_MCHA WITH KEY MATNR = BESTAND-MATNR
                                  WERKS = BESTAND-WERKS
                                  CHARG = BESTAND-CHARG
                                  BINARY SEARCH.
      IF SY-SUBRC = 0.
        BESTAND-BWTAR = T_MCHA-BWTAR.
      ENDIF.
    ENDIF.
    MODIFY BESTAND.
* Valuation keys
    IF NOVALUES IS INITIAL.
      IF BESTAND-SOBKZ = ' ' OR BESTAND-SOBKZ = 'O' OR
           BESTAND-SOBKZ = 'W' OR BESTAND-SOBKZ = 'V' OR
           BESTAND-KZBWS = 'A'.
        T_MBEWKEY-MATNR = BESTAND-MATNR.
        T_MBEWKEY-BWKEY = BESTAND-BWKEY.
        T_MBEWKEY-BWTAR = BESTAND-BWTAR.
        COLLECT T_MBEWKEY.
      ELSEIF BESTAND-SOBKZ = 'E' AND BESTAND-KZBWS = 'M'.
        T_EBEWKEY-MATNR = BESTAND-MATNR.
        T_EBEWKEY-BWKEY = BESTAND-BWKEY.
        T_EBEWKEY-BWTAR = BESTAND-BWTAR.
        COLLECT T_EBEWKEY.
      ELSEIF BESTAND-SOBKZ = 'Q' AND BESTAND-KZBWS = 'M'.
        T_QBEWKEY-MATNR = BESTAND-MATNR.
        T_QBEWKEY-BWKEY = BESTAND-BWKEY.
        T_QBEWKEY-BWTAR = BESTAND-BWTAR.
        COLLECT T_QBEWKEY.
      ENDIF.
      T_T134MKEY-BWKEY = BESTAND-BWKEY.
      T_T134MKEY-MTART = BESTAND-MTART.
      COLLECT T_T134MKEY.
    ENDIF.                              " novalues is initial
  ENDLOOP.

* release the space of global working tables after use
  FREE : G_T_MARD_LV, G_T_T001L, G_T_ORGAN.

************************************************************************
* Read the valuation tables
************************************************************************
  IF NOVALUES IS INITIAL.
    READ TABLE T_MBEWKEY INDEX 1 TRANSPORTING NO FIELDS.
    IF SY-SUBRC = 0.
      SELECT MATNR BWKEY BWTAR LBKUM SALK3
             VPRSV VERPR STPRS PEINH                        "353428
             INTO CORRESPONDING FIELDS OF TABLE T_MBEW
             FROM MBEW
             FOR ALL ENTRIES IN T_MBEWKEY
             WHERE MATNR = T_MBEWKEY-MATNR
               AND BWKEY = T_MBEWKEY-BWKEY
               AND BWTAR = T_MBEWKEY-BWTAR.
      SORT T_MBEW BY MATNR BWKEY BWTAR.
    ENDIF.

    READ TABLE T_EBEWKEY INDEX 1 TRANSPORTING NO FIELDS.
    IF SY-SUBRC = 0.
*     "Unfortunately", EBEW and QBEW do not have sum segments over
*     the valuation types. Therefore, without batch data, another
*     SELECT-statement is needed.
      IF XMCHB = 'X'.
        SELECT MATNR BWKEY BWTAR SOBKZ VBELN POSNR LBKUM SALK3
               VPRSV VERPR STPRS PEINH
               INTO (T_EBEW-MATNR, T_EBEW-BWKEY, T_EBEW-BWTAR,
                     T_EBEW-SOBKZ, T_EBEW-VBELN, T_EBEW-POSNR,
                     T_EBEW-LBKUM, T_EBEW-SALK3,
                     T_EBEW-VPRSV, T_EBEW-VERPR,
                     T_EBEW-STPRS, T_EBEW-PEINH)
               FROM EBEW
               FOR ALL ENTRIES IN T_EBEWKEY
               WHERE MATNR = T_EBEWKEY-MATNR
                 AND BWKEY = T_EBEWKEY-BWKEY
                 AND BWTAR = T_EBEWKEY-BWTAR.
          COLLECT T_EBEW.
        ENDSELECT.
      ELSE.
        SELECT MATNR BWKEY BWTAR SOBKZ VBELN POSNR LBKUM SALK3
               VPRSV VERPR STPRS PEINH
               INTO (T_EBEW-MATNR, T_EBEW-BWKEY, T_EBEW-BWTAR,
                     T_EBEW-SOBKZ, T_EBEW-VBELN, T_EBEW-POSNR,
                     T_EBEW-LBKUM, T_EBEW-SALK3,
                     T_EBEW-VPRSV, T_EBEW-VERPR,
                     T_EBEW-STPRS, T_EBEW-PEINH)
               FROM EBEW
               FOR ALL ENTRIES IN T_EBEWKEY
               WHERE MATNR = T_EBEWKEY-MATNR
                 AND BWKEY = T_EBEWKEY-BWKEY.
          COLLECT T_EBEW.
        ENDSELECT.
      ENDIF.

      SORT T_EBEW BY MATNR BWKEY BWTAR SOBKZ VBELN POSNR.
    ENDIF.

    READ TABLE T_QBEWKEY INDEX 1 TRANSPORTING NO FIELDS.
    IF SY-SUBRC = 0.
      IF XMCHB = 'X'.
        SELECT MATNR BWKEY BWTAR SOBKZ PSPNR LBKUM SALK3
               VPRSV VERPR STPRS PEINH
               INTO (T_QBEW-MATNR, T_QBEW-BWKEY, T_QBEW-BWTAR,
                     T_QBEW-SOBKZ, T_QBEW-PSPNR,
                     T_QBEW-LBKUM, T_QBEW-SALK3,
                     T_QBEW-VPRSV, T_QBEW-VERPR,
                     T_QBEW-STPRS, T_QBEW-PEINH)
               FROM QBEW
               FOR ALL ENTRIES IN T_QBEWKEY
               WHERE MATNR = T_QBEWKEY-MATNR
                 AND BWKEY = T_QBEWKEY-BWKEY
               AND BWTAR = T_QBEWKEY-BWTAR.
         COLLECT T_QBEW.
       ENDSELECT.
     ELSE.
       SELECT MATNR BWKEY BWTAR SOBKZ PSPNR LBKUM SALK3
              VPRSV VERPR STPRS PEINH
              INTO (T_QBEW-MATNR, T_QBEW-BWKEY, T_QBEW-BWTAR,
                    T_QBEW-SOBKZ, T_QBEW-PSPNR,
                    T_QBEW-LBKUM, T_QBEW-SALK3,
                    T_QBEW-VPRSV, T_QBEW-VERPR,
                    T_QBEW-STPRS, T_QBEW-PEINH)
              FROM QBEW
              FOR ALL ENTRIES IN T_QBEWKEY
              WHERE MATNR = T_QBEWKEY-MATNR
                AND BWKEY = T_QBEWKEY-BWKEY.
         COLLECT T_QBEW.
       ENDSELECT.
     ENDIF.

     SORT T_QBEW BY MATNR BWKEY BWTAR SOBKZ PSPNR.
   ENDIF.

    READ TABLE T_T134MKEY INDEX 1 TRANSPORTING NO FIELDS.
    IF SY-SUBRC = 0.
      SELECT BWKEY MTART WERTU
              INTO CORRESPONDING FIELDS OF TABLE T_T134M
              FROM T134M
              FOR ALL ENTRIES IN T_T134MKEY
              WHERE BWKEY = T_T134MKEY-BWKEY
                AND MTART = T_T134MKEY-MTART.
      SORT T_T134M BY BWKEY MTART.
    ENDIF.
************************************************************************
* Fill the valuation data
************************************************************************
    DATA: FACTOR TYPE F.
    LOOP AT BESTAND.
      CHECK BESTAND-WAERS <> SPACE. "Do nothing for failed Auth-Checks
      READ TABLE T_T134M WITH KEY BWKEY = BESTAND-BWKEY
                                   MTART = BESTAND-MTART
                                   BINARY SEARCH.
      CHECK SY-SUBRC = 0 AND T_T134M-WERTU = 'X'.
*     Set SY-SUBRC = 4. A successful table read resets it an starts
*     the value filling.
      SY-SUBRC = 4.
      IF BESTAND-SOBKZ = ' ' OR BESTAND-SOBKZ = 'O' OR
           BESTAND-SOBKZ = 'W' OR BESTAND-SOBKZ = 'V' OR
           BESTAND-KZBWS = 'A'.
        READ TABLE T_MBEW WITH KEY MATNR = BESTAND-MATNR
                                    BWKEY = BESTAND-BWKEY
                                    BWTAR = BESTAND-BWTAR
                                    BINARY SEARCH.

     ELSEIF BESTAND-SOBKZ = 'E' AND BESTAND-KZBWS = 'M'.
       READ TABLE T_EBEW WITH KEY MATNR = BESTAND-MATNR
                                  BWKEY = BESTAND-BWKEY
                                  BWTAR = BESTAND-BWTAR
                                  SOBKZ = BESTAND-SOBKZ
                                  VBELN = BESTAND-VBELN    "n531604
                                  POSNR = BESTAND-POSNR    "n531604
                                  BINARY SEARCH.
         MOVE-CORRESPONDING T_EBEW TO T_MBEW.

        ELSEIF BESTAND-SOBKZ = 'Q' AND BESTAND-KZBWS = 'M'.
          READ TABLE T_QBEW WITH KEY MATNR = BESTAND-MATNR
                                     BWKEY = BESTAND-BWKEY
                                     BWTAR = BESTAND-BWTAR
                                     SOBKZ = BESTAND-SOBKZ
                                     PSPNR = BESTAND-PSPNR
                                     BINARY SEARCH.
          MOVE-CORRESPONDING T_QBEW TO T_MBEW.
        ENDIF.

        IF SY-SUBRC = 0.
          IF T_MBEW-LBKUM = 0.
*           Cannot happen, but in R/3 this does not hold in all cases...
            IF T_MBEW-PEINH = 0.                               "353428
              T_MBEW-PEINH = 1.                                "353428
            ENDIF.                                             "353428
*           Calculation of value in case of LBKUM = 0 only possible
*           for MBEW. EBEW and QBEW are collected over all subitems
*           (VBELN...), so the data are not available.
            IF BESTAND-SOBKZ = 'E' OR BESTAND-SOBKZ = 'Q'.     "388735
              FACTOR = 0.                                      "388735
              CLEAR BESTAND-WAERS.                             "388735
            ELSE.                                              "388735
              CASE T_MBEW-VPRSV.
                 WHEN 'V'. FACTOR = T_MBEW-VERPR / T_MBEW-PEINH.
                 WHEN 'S'. FACTOR = T_MBEW-STPRS / T_MBEW-PEINH.
              ENDCASE.
            ENDIF.                                             "388735
          ELSE.
            FACTOR = T_MBEW-SALK3 / T_MBEW-LBKUM.
          ENDIF.

          BESTAND-WLABS =   BESTAND-LABST   *   FACTOR.
          BESTAND-WINSM =   BESTAND-INSME   *   FACTOR.
          BESTAND-WSPEM =   BESTAND-SPEME   *   FACTOR.
          BESTAND-WEINM =   BESTAND-EINME   *   FACTOR.
          BESTAND-WUMLM =   BESTAND-UMLME   *   FACTOR.
          MODIFY BESTAND.
        ENDIF.
      ENDLOOP.
    ENDIF.                                  "novalues is initial

ENDFORM.                    " Get_stock
*&---------------------------------------------------------------------*
*&      Form f0100_settings_init
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
* --> p1          text
* <-- p2          text
*----------------------------------------------------------------------*
FORM F0100_SETTINGS_INIT.
  CHECK : SY-BATCH IS INITIAL.

    IF OREF_SETTINGS IS INITIAL.
      CREATE OBJECT OREF_SETTINGS
                           EXPORTING I_ACTION = 'YR_MM_GRN_STOCK_SENT'.
    ENDIF.
* get the parameters from the last run
  NOVALUES = OREF_SETTINGS->GET( 'NOVALUES' ).

ENDFORM.                    " f0100_settings_init
*&---------------------------------------------------------------------*
*&      Form F0200_SETTINGS_SAVE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
* --> p1          text
* <-- p2          text
*----------------------------------------------------------------------*
FORM F0200_SETTINGS_SAVE.
* only in dialog mode
  CHECK : SY-BATCH IS INITIAL.

* Save the settings
  CALL METHOD OREF_SETTINGS->SET( I_ELEMENT = 'NOVALUES'
                                  I_ACTIVE = NOVALUES ).

    CALL METHOD OREF_SETTINGS->FLUSH.
    COMMIT                     WORK.

ENDFORM.                    " F0200_SETTINGS_SAVE
*&---------------------------------------------------------------------*
*&      Form f0000_get_print_settings
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
* --> p1          text
* <-- p2          text
*----------------------------------------------------------------------*
FORM F0000_GET_PRINT_SETTINGS.
* get the settings for the SAP-LIST-VIEWER
  SELECT SINGLE * FROM MMIM_REP_PRINT
                             WHERE REPORT = SY-REPID.

    IF SY-SUBRC <> 0.
*     if this entry is missing, set default values and insert
*     a new entry into database tabel MMIM_REP_PRINT
      CLEAR                    MMIM_REP_PRINT.
      MOVE : SY-REPID          TO MMIM_REP_PRINT-REPORT,
             'X'               TO MMIM_REP_PRINT-SELINFO.
      INSERT                   MMIM_REP_PRINT.

     IF SY-SUBRC IS INITIAL.
      COMMIT                 WORK.
    ELSE.
*     error during insert into table MMIN_RE_PRINT
*      MESSAGE S895           WITH TEXT-895.
    ENDIF.
  ENDIF.

* set the parameter for this run
  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'.
   ALV_COLOR       = 'X'.
 ELSE.
   ALV_COLOR       = SPACE.
 ENDIF.


ENDFORM.                    " f0000_get_print_settings
*&---------------------------------------------------------------------*
*&      Form authority_check
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
* --> p1          text
* <-- p2          text
*----------------------------------------------------------------------*

**Start Zedek added For authorization check. 08.03.2004

*FORM authority_check.
*
* AUTHORITY-CHECK OBJECT 'M_MSEG_WWA' ID 'WERKS' FIELD
*                                        bestand-werks.
* IF sy-subrc = 0.
*    AUTHORITY-CHECK OBJECT 'M_MSEG_LGO' ID 'LGORT' FIELD
*                                            bestand-lgort.
*
*    IF sy-subrc = 0.
*      MOVE-CORRESPONDING bestand TO itab_mat.
*      MOVE : bestand-labst TO itab_mat-clabs.
*    ENDIF.
* ENDIF.
*
* CLEAR bestand.
*
*
*ENDFORM.                    " authority_check
**End Zedek added For authorization check. 08.03.2004

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:52
posted:11/21/2011
language:English
pages:41