Friday, November 23, 2007

sap abap ALV Reporting - Z_LIST_MATERIALS

REPORT Z_LIST_MATERIALS.

TYPE-POOLS: SLIS.
TABLES:MARC, MARD, VBAP, LIPS, EKPO, VBFA, EKBE, MARM, VBBE, MARA, MBEW.

SELECTION-SCREEN BEGIN OF BLOCK SEL WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS: S_WERKS FOR MARC-WERKS, " Plant
S_MATNR FOR MARC-MATNR, " Material
S_MTART FOR MARA-MTART. " Material Type SELECTION-SCREEN END OF BLOCK SEL.
PARAMETERS: P_VARI LIKE DISVARIANT-VARIANT. " ALV Variant

CONSTANTS: FORMNAME_TOP_OF_PAGE TYPE SLIS_FORMNAME VALUE 'TOP_OF_PAGE'.

DATA: BEGIN OF INV OCCURS 100,
WERKS LIKE MARD-WERKS, " Plant
MATNR LIKE MARD-MATNR, " Material
MTART LIKE MARA-MTART, " Material Type
STPRS LIKE MBEW-STPRS, " Standard Price
AVAIL LIKE MARD-LABST, " Available
LABST LIKE MARD-LABST, " Unrestricted use
INSME LIKE MARD-INSME, " Quality Inspection
RETME LIKE MARD-RETME, " Returns
TRANS LIKE MARC-UMLMC, " Stock in transit (calculated)
UMLMC LIKE MARC-UMLMC, " Stock Transfer (plant)
UMLME LIKE MARD-UMLME, " Transfer (SLoc)
WESBS LIKE EKBE-WESBS, " GR Blocked Stock
TRAME LIKE MARC-TRAME, " Stock in transit
SPEME LIKE MARD-SPEME, " Blocked
KWMENG LIKE VBAP-KWMENG, " Sales orders
LFIMG LIKE LIPS-LFIMG, " Scheduled for Delivery
MENGE LIKE EKPO-MENGE, " Open Purch. Orders
VALUE LIKE MBEW-SALK3, " Stock Value (Calculated)
MEINS LIKE MARA-MEINS, " Unit of measure
END OF INV.

DATA: FIELDTAB TYPE SLIS_T_FIELDCAT_ALV,
HEADING TYPE SLIS_T_LISTHEADER,
LAYOUT TYPE SLIS_LAYOUT_ALV,
EVENTS TYPE SLIS_T_EVENT,
REPNAME LIKE SY-REPID,
F2CODE LIKE SY-UCOMM VALUE '&ETA',
G_SAVE(1) TYPE C,
G_EXIT(1) TYPE C,
G_VARIANT LIKE DISVARIANT,
GX_VARIANT LIKE DISVARIANT.

INITIALIZATION.
REPNAME = SY-REPID.
PERFORM INITIALIZE_FIELDCAT USING FIELDTAB[].
PERFORM BUILD_EVENTTAB USING EVENTS[].
PERFORM BUILD_COMMENT USING HEADING[].
PERFORM INITIALIZE_VARIANT.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_VARI.
PERFORM F4_FOR_VARIANT.

AT SELECTION-SCREEN.
PERFORM PAI_OF_SELECTION_SCREEN.

START-OF-SELECTION.
PERFORM GET_MARD.
PERFORM GET_UNIT_OF_MEASURE.
PERFORM GET_MARC.
PERFORM GET_EKPO.
PERFORM GET_LIPS.
PERFORM GET_VBAP.
PERFORM GET_OPEN.
PERFORM GET_PRICE.

END-OF-SELECTION.

PERFORM BUILD_LAYOUT USING LAYOUT.
PERFORM WRITE_OUTPUT.


*&---------------------------------------------------------------------*
*& Form INITIALIZE_FIELDCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_FIELDTAB[] text *
*----------------------------------------------------------------------*
FORM INITIALIZE_FIELDCAT USING P_FIELDTAB TYPE SLIS_T_FIELDCAT_ALV.
DATA: L_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
* fixed columns (obligatory)
CLEAR L_FIELDCAT.
L_FIELDCAT-TABNAME = 'INV'.
L_FIELDCAT-FIX_COLUMN = 'X'.
L_FIELDCAT-NO_OUT = 'O'.
L_FIELDCAT-FIELDNAME = 'WERKS'.
APPEND L_FIELDCAT TO P_FIELDTAB.
L_FIELDCAT-FIELDNAME = 'MATNR'.
APPEND L_FIELDCAT TO P_FIELDTAB.

* totalized columns
CLEAR L_FIELDCAT.
L_FIELDCAT-TABNAME = 'INV'.
L_FIELDCAT-SP_GROUP = 'A'.
L_FIELDCAT-DO_SUM = 'X'.
L_FIELDCAT-FIELDNAME = 'LABST'.
APPEND L_FIELDCAT TO P_FIELDTAB.
L_FIELDCAT-FIELDNAME = 'INSME'.
APPEND L_FIELDCAT TO P_FIELDTAB.
L_FIELDCAT-FIELDNAME = 'RETME'.
APPEND L_FIELDCAT TO P_FIELDTAB.
L_FIELDCAT-FIELDNAME = 'UMLME'.
APPEND L_FIELDCAT TO P_FIELDTAB.
L_FIELDCAT-FIELDNAME = 'WESBS'.
APPEND L_FIELDCAT TO P_FIELDTAB.
L_FIELDCAT-FIELDNAME = 'SPEME'.
APPEND L_FIELDCAT TO P_FIELDTAB.
* columns with different description
L_FIELDCAT-FIELDNAME = 'KWMENG'.
L_FIELDCAT-SELTEXT_M = 'Sales Orders'.
L_FIELDCAT-SELTEXT_S = 'Sales Or'.
L_FIELDCAT-SELTEXT_L = 'Sales Orders Qty'.
APPEND L_FIELDCAT TO P_FIELDTAB.
L_FIELDCAT-FIELDNAME = 'LFIMG'.
L_FIELDCAT-SELTEXT_M = 'Sched. Delivery'.
L_FIELDCAT-SELTEXT_S = 'Schd. Del'.
L_FIELDCAT-SELTEXT_L = 'Scheduled for Delivery'.
APPEND L_FIELDCAT TO P_FIELDTAB.
L_FIELDCAT-FIELDNAME = 'TRANS'.
L_FIELDCAT-SELTEXT_M = 'Stk. in transit'.
L_FIELDCAT-SELTEXT_S = 'Stk. trns'.
L_FIELDCAT-SELTEXT_L = 'Stock in transit (sum)'.
APPEND L_FIELDCAT TO P_FIELDTAB.
L_FIELDCAT-FIELDNAME = 'AVAIL'.
L_FIELDCAT-SELTEXT_M = 'Available'.
L_FIELDCAT-SELTEXT_S = 'Avail.'.
L_FIELDCAT-SELTEXT_L = 'Stock Available'.
APPEND L_FIELDCAT TO P_FIELDTAB.
L_FIELDCAT-FIELDNAME = 'MENGE'.
L_FIELDCAT-SELTEXT_M = 'Open Orders'.
L_FIELDCAT-SELTEXT_S = 'Open Ord'.
L_FIELDCAT-SELTEXT_L = 'Open Purchase Orders'.
APPEND L_FIELDCAT TO P_FIELDTAB.

* columns not displayed
CLEAR L_FIELDCAT.
L_FIELDCAT-TABNAME = 'INV'.
L_FIELDCAT-SP_GROUP = 'A'.
L_FIELDCAT-NO_OUT = 'X'.
L_FIELDCAT-FIELDNAME = 'MEINS'.
APPEND L_FIELDCAT TO P_FIELDTAB.
L_FIELDCAT-FIELDNAME = 'UMLMC'.
APPEND L_FIELDCAT TO P_FIELDTAB.
L_FIELDCAT-FIELDNAME = 'TRAME'.
APPEND L_FIELDCAT TO P_FIELDTAB.
L_FIELDCAT-FIELDNAME = 'STPRS'.
APPEND L_FIELDCAT TO P_FIELDTAB.
L_FIELDCAT-FIELDNAME = 'VALUE'.
APPEND L_FIELDCAT TO P_FIELDTAB.
ENDFORM. " INITIALIZE_FIELDCAT

*&---------------------------------------------------------------------*
*& Form BUILD_EVENTTAB
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_EVENTS[] text *
*----------------------------------------------------------------------*
FORM BUILD_EVENTTAB USING P_EVENTS TYPE SLIS_T_EVENT.
DATA: LS_EVENT TYPE SLIS_ALV_EVENT.
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
I_LIST_TYPE = 0
IMPORTING
ET_EVENTS = P_EVENTS.
READ TABLE P_EVENTS WITH KEY NAME = SLIS_EV_TOP_OF_PAGE
INTO LS_EVENT.
IF SY-SUBRC = 0.
MOVE FORMNAME_TOP_OF_PAGE TO LS_EVENT-FORM.
APPEND LS_EVENT TO P_EVENTS.
ENDIF.
ENDFORM. " BUILD_EVENTTAB

*&---------------------------------------------------------------------*
*& Form BUILD_COMMENT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_HEADING[] text *
*----------------------------------------------------------------------*
FORM BUILD_COMMENT USING P_HEADING TYPE SLIS_T_LISTHEADER.
DATA: HLINE TYPE SLIS_LISTHEADER,
TEXT(60) TYPE C,
SEP(20) TYPE C.
CLEAR: HLINE, TEXT.
HLINE-TYP = 'H'.
WRITE: TEXT-101 TO TEXT+23.
HLINE-INFO = TEXT.
APPEND HLINE TO P_HEADING.
CLEAR TEXT.
WRITE: 'User: ' TO TEXT,
SY-UNAME TO TEXT+6,
'Date: ' TO TEXT+25,
SY-DATUM TO TEXT+31,
'Page: ' TO TEXT+50,
SY-PAGNO TO TEXT+56.
HLINE-INFO = TEXT.
APPEND HLINE TO P_HEADING.



ENDFORM. " BUILD_COMMENT

*---------------------------------------------------------------------*
* FORM TOP_OF_PAGE *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM TOP_OF_PAGE.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
IT_LIST_COMMENTARY = HEADING.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form INITIALIZE_VARIANT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM INITIALIZE_VARIANT.
G_SAVE = 'A'.
CLEAR G_VARIANT.
G_VARIANT-REPORT = REPNAME.
GX_VARIANT = G_VARIANT.
CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
EXPORTING
I_SAVE = G_SAVE
CHANGING
CS_VARIANT = GX_VARIANT
EXCEPTIONS
NOT_FOUND = 2.
IF SY-SUBRC = 0.
P_VARI = GX_VARIANT-VARIANT.
ENDIF.
ENDFORM. " INITIALIZE_VARIANT

*&---------------------------------------------------------------------*
*& Form F4_FOR_VARIANT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM F4_FOR_VARIANT.
CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
EXPORTING
IS_VARIANT = G_VARIANT
I_SAVE = G_SAVE
IMPORTING
E_EXIT = G_EXIT
ES_VARIANT = GX_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.
IF G_EXIT = SPACE.
P_VARI = GX_VARIANT-VARIANT.
ENDIF.
ENDIF.
ENDFORM. " F4_FOR_VARIANT

*&---------------------------------------------------------------------*
*& Form PAI_OF_SELECTION_SCREEN
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM PAI_OF_SELECTION_SCREEN.
*
IF NOT P_VARI IS INITIAL.
MOVE G_VARIANT TO GX_VARIANT.
MOVE P_VARI TO GX_VARIANT-VARIANT.
CALL FUNCTION 'REUSE_ALV_VARIANT_EXISTENCE'
EXPORTING
I_SAVE = G_SAVE
CHANGING
CS_VARIANT = GX_VARIANT.
G_VARIANT = GX_VARIANT.
ELSE.
PERFORM INITIALIZE_VARIANT.
ENDIF.
ENDFORM. " PAI_OF_SELECTION_SCREEN


*&---------------------------------------------------------------------*
*& Form GET_MARD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM GET_MARD.
SELECT MATNR WERKS LABST INSME RETME UMLME SPEME
FROM MARD
INTO CORRESPONDING FIELDS OF INV
WHERE MATNR IN S_MATNR
AND WERKS IN S_WERKS.
COLLECT INV.
ENDSELECT.
PERFORM FILTER_BY_MATERIAL_TYPE.
ENDFORM. " GET_MARD

*&---------------------------------------------------------------------*
*& Form FILTER_BY_MATERIAL_TYPE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM FILTER_BY_MATERIAL_TYPE.
LOOP AT INV.
CLEAR INV-MTART.
SELECT SINGLE MTART
INTO INV-MTART
FROM MARA
WHERE MATNR EQ INV-MATNR
AND MTART IN S_MTART.
IF SY-SUBRC EQ 0.
MODIFY INV.
ELSE.
DELETE INV.
ENDIF.
ENDLOOP.
ENDFORM. " FILTER_BY_MATERIAL_TYPE

*&---------------------------------------------------------------------*
*& Form GET_MARC
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM GET_MARC.
LOOP AT INV.
SELECT SINGLE UMLMC TRAME
FROM MARC
INTO CORRESPONDING FIELDS OF INV
WHERE MATNR EQ INV-MATNR
AND WERKS EQ INV-WERKS.
IF SY-SUBRC EQ 0.
INV-TRANS = INV-UMLMC + INV-TRAME.
MODIFY INV.
ENDIF.
ENDLOOP.
ENDFORM. " GET_MARC
*&---------------------------------------------------------------------*
*& Form GET_EKPO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM GET_EKPO.
DATA: WESBS LIKE INV-WESBS,
SHKZG LIKE EKBE-SHKZG,
MEINS LIKE EKPO-MEINS,
LMEIN LIKE EKPO-LMEIN.
LOOP AT INV.
CLEAR: WESBS, SHKZG, MEINS, LMEIN.
SELECT YWESBS YSHKZG XMEINS XLMEIN
INTO (WESBS, SHKZG, MEINS, LMEIN)
FROM EKPO AS X JOIN EKBE AS Y
ON XEBELN = YEBELN
AND XEBELP = YEBELP
WHERE XMATNR EQ INV-MATNR
AND XWERKS EQ INV-WERKS
AND XLOEKZ NE 'L'.
IF SHKZG EQ 'H'.
MULTIPLY WESBS BY -1.
ENDIF.
IF MEINS NE LMEIN.
PERFORM CONVERT_UNIT_OF_MEASURE CHANGING INV-MATNR MEINS WESBS.
ENDIF.
ADD WESBS TO INV-WESBS.
ENDSELECT.
MODIFY INV.
ENDLOOP.
ENDFORM. " GET_EKPO
*&---------------------------------------------------------------------*
*& Form GET_LIPS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM GET_LIPS.
DATA: LFIMG LIKE INV-LFIMG.
LOOP AT INV.
CLEAR: LFIMG, INV-LFIMG.
SELECT OMENG
INTO LFIMG
FROM VBBE
WHERE MATNR EQ INV-MATNR
AND WERKS EQ INV-WERKS
AND VBTYP EQ 'J'.
ADD LFIMG TO INV-LFIMG.
ENDSELECT.
MODIFY INV.
ENDLOOP.
ENDFORM. " GET_LIPS
*&---------------------------------------------------------------------*
*& Form GET_VBAP
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM GET_VBAP.
DATA: KWMENG LIKE INV-KWMENG.
LOOP AT INV.
CLEAR: KWMENG, INV-KWMENG.
SELECT OMENG
INTO KWMENG
FROM VBBE
WHERE MATNR EQ INV-MATNR
AND WERKS EQ INV-WERKS
AND VBTYP EQ 'C'.
ADD KWMENG TO INV-KWMENG.
ENDSELECT.
INV-AVAIL = INV-LABST - INV-INSME - INV-KWMENG - INV-LFIMG.
MODIFY INV.
ENDLOOP.
ENDFORM. " GET_VBAP

*&---------------------------------------------------------------------*
*& Form GET_UNIT_OF_MEASURE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GET_UNIT_OF_MEASURE.
LOOP AT INV.
SELECT SINGLE MEINS
FROM MARA
INTO INV-MEINS
WHERE MATNR EQ INV-MATNR.
MODIFY INV.
ENDLOOP.
ENDFORM. " GET_UNIT_OF_MEASURE

*&---------------------------------------------------------------------*
*& Form GET_OPEN
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM GET_OPEN.
DATA: BEGIN OF XTAB OCCURS 10, " Open orders table
WERKS LIKE EKPO-WERKS,
LGORT LIKE EKPO-LGORT,
MATNR LIKE EKPO-MATNR,
MENGE LIKE EKPO-MENGE,
MENGK LIKE EKPO-MENGE,
END OF XTAB.
RANGES: L_WERKS FOR MARD-WERKS.

LOOP AT INV.
REFRESH XTAB.
CLEAR: XTAB, L_WERKS.
MOVE INV-WERKS TO L_WERKS-LOW.
CALL FUNCTION 'MB_ADD_PURCHASE_ORDER_QUANTITY'
EXPORTING
X_MATNR = INV-MATNR
X_MEINS = INV-MEINS
X_ELIKZ = SPACE
X_LOEKZ = SPACE
TABLES
XTAB = XTAB
XWERKS = L_WERKS.
MOVE XTAB-MENGE TO INV-MENGE.
MODIFY INV.
ENDLOOP.
ENDFORM. " GET_OPEN

*&---------------------------------------------------------------------*
*& Form GET_PRICE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM GET_PRICE.
LOOP AT INV.
SELECT SINGLE STPRS
FROM MBEW
INTO INV-STPRS
WHERE MATNR EQ INV-MATNR
AND BWKEY EQ INV-WERKS
AND BWTAR EQ SPACE.
IF SY-SUBRC EQ 0.
INV-VALUE = INV-STPRS *
( INV-LABST + INV-INSME + INV-TRANS + INV-SPEME ).
MODIFY INV.
ENDIF.
ENDLOOP.
ENDFORM. " GET_PRICE

*---------------------------------------------------------------------*
* FORM CONVERT_UNIT_OF_MEASURE *
*---------------------------------------------------------------------*
* text *
*---------------------------------------------------------------------*
* --> P_MATNR *
* --> P_VRKME *
* --> P_QUANT *
*---------------------------------------------------------------------*
FORM CONVERT_UNIT_OF_MEASURE USING P_MATNR P_VRKME P_QUANT.
DATA: UMREZ LIKE MARM-UMREZ,
UMREN LIKE MARM-UMREN.
SELECT SINGLE UMREZ UMREN
INTO (UMREZ, UMREN)
FROM MARM
WHERE MATNR EQ P_MATNR
AND MEINH EQ P_VRKME.
IF SY-SUBRC EQ 0.
COMPUTE P_QUANT = ( P_QUANT * UMREZ ) / UMREN.
ENDIF.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form BUILD_LAYOUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_LAYOUT text *
*----------------------------------------------------------------------*
FORM BUILD_LAYOUT USING P_LAYOUT TYPE SLIS_LAYOUT_ALV.
P_LAYOUT-F2CODE = F2CODE.
P_LAYOUT-ZEBRA = 'X'.
P_LAYOUT-DETAIL_POPUP = 'X'.
ENDFORM. " BUILD_LAYOUT

*&---------------------------------------------------------------------*
*& Form WRITE_OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM WRITE_OUTPUT.
SORT INV BY WERKS MATNR.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = REPNAME
I_INTERNAL_TABNAME = 'INV'
I_INCLNAME = REPNAME
CHANGING
CT_FIELDCAT = FIELDTAB.
IF SY-SUBRC <> 0.
WRITE: 'SY-SUBRC: ', SY-SUBRC, 'REUSE_ALV_FIELDCATALOG_MERGE'. ENDIF. CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = REPNAME
I_STRUCTURE_NAME = 'INV'
IS_LAYOUT = LAYOUT
IT_FIELDCAT = FIELDTAB
I_DEFAULT = 'A'
I_SAVE = G_SAVE
IS_VARIANT = G_VARIANT
IT_EVENTS = EVENTS[]
TABLES
T_OUTTAB = INV.
IF SY-SUBRC <> 0.
WRITE: 'SY-SUBRC: ', SY-SUBRC, 'REUSE_ALV_LIST_DISPLAY'. ENDIF.
ENDFORM. " WRITE_OUTPUT

No comments:

Blog Archive