SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Getting opening stock based on material and batch



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ММ
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Fri Oct 05, 2007 11:45 pm    Post subject: Getting opening stock based on material and batch Reply with quote

Code:
REPORT ZMMTISCOSTOCK_ALV
LINE-SIZE 255
NO STANDARD PAGE HEADING
MESSAGE-ID S1.
************************************************************************
* Program name: ZMMTISCOSTOCK_ALV *
* *
* Description: RECEIPTS/ISSUES WITH OPENING BAL.AND CL.BAL. *
* *
* Date/Author: 02.07.2004 / Rajasekhar (Intelligroup) *
* *
************************************************************************
* M O D I F I C A T I O N L O G
************************************************************************
* Date Programmer Request # Description
* dd/mm/yyyy xxxxxxxxxxxxxx nnnnn New Program
*
************************************************************************
************************************************************************
* T A B L E S
************************************************************************

TABLES : MSEG,MKPF,MARA,MAKT,BEWART,MSLB.

TYPE-POOLS : SLIS.

TYPES : BEGIN OF ST_OPBAL,
MATNR LIKE MSEG-MATNR,
MAKTX LIKE MAKT-MAKTX,
BATCH LIKE MSEG-CHARG,
BWART LIKE MSEG-BWART,
MENGE LIKE MSEG-MENGE,
ZEILE LIKE MSEG-ZEILE,
WERKS LIKE MSEG-WERKS,
SOBKZ LIKE MSEG-SOBKZ,
BUDAT LIKE MKPF-BUDAT,
MTART LIKE MARA-MTART,
SHKZG LIKE MSEG-SHKZG,
DMBTR LIKE MSEG-DMBTR,
MBLNR LIKE MSEG-MBLNR,
LGORT LIKE MSEG-LGORT,
END OF ST_OPBAL.

************************************************************************
* I N T E R N A L T A B L E S
************************************************************************

DATA : IT_FLDCAT TYPE SLIS_T_FIELDCAT_ALV,
IT_LIST_HEADER TYPE SLIS_T_LISTHEADER,
IT_EVENTS TYPE SLIS_T_EVENT,
GS_LAYOUT TYPE SLIS_LAYOUT_ALV.

DATA: BEGIN OF G_T_MSEG_LEAN OCCURS 0,
MBLNR LIKE MKPF-MBLNR,
MJAHR LIKE MKPF-MJAHR,
BLART LIKE MKPF-BLART,
BUDAT LIKE MKPF-BUDAT,
CPUDT LIKE MKPF-CPUDT,
MATNR LIKE MSEG-MATNR,
WERKS LIKE MSEG-WERKS,
LGORT LIKE MSEG-LGORT,
CHARG LIKE MSEG-CHARG,
SHKZG LIKE MSEG-SHKZG,
MENGE LIKE MSEG-MENGE,
END OF G_T_MSEG_LEAN .

DATA : OPBAL TYPE ST_OPBAL OCCURS 0 WITH HEADER LINE,
TMP_OPBAL TYPE ST_OPBAL OCCURS 0 WITH HEADER LINE.

DATA : BEGIN OF OPBAL2 OCCURS 0,
SNO(9) TYPE N, " Serial #
MATNR LIKE MSEG-MATNR, " Material #
MAKTX LIKE MAKT-MAKTX, " Material Desc
OP_BAL LIKE MSEG-MENGE, " Opening Balance
OP_BAL_YR LIKE MSEG-MENGE, " Opening Balance Yr
REC LIKE MSEG-MENGE, " Receipts
REC_PLN LIKE MSEG-MENGE, " Receipts Planned
REC_YR LIKE MSEG-MENGE, " Receipts Yr
T_OPBAL_REC LIKE MSEG-MENGE, " Total Stock
ISS LIKE MSEG-MENGE, " Issues
ISS_PLN LIKE MSEG-MENGE, " Issues Planned
ISS_YR LIKE MSEG-MENGE, " Issues Yr
CL_BAL LIKE MSEG-MENGE, " Closing Balance
CL_BAL_YR LIKE MSEG-MENGE, " Closing Balance Yr
BATCH LIKE MSEG-CHARG, " Batch
SUBCON LIKE MSEG-MENGE,
END OF OPBAL2.

DATA: BEGIN OF IMARD OCCURS 100, "aktueller Materialbestand
WERKS LIKE MARD-WERKS, "Werk
MATNR LIKE MARD-MATNR, "Material
LGORT LIKE MARD-LGORT, "Lagerort
LABST LIKE MARD-LABST, "frei verwendbarer Bestand
UMLME LIKE MARD-UMLME, "Umlagerungsbestand
INSME LIKE MARD-INSME, "Qualitätsprüfbestand
EINME LIKE MARD-EINME, "nicht frei verwendbarer Bestand
SPEME LIKE MARD-SPEME, "gesperrter Bestand
RETME LIKE MARD-RETME, "gesperrter Bestand
KLABS LIKE MARD-KLABS, "frei verw. Konsignationsbestand
LBKUM LIKE MBEW-LBKUM, "bewerteter Bestand
SALK3(09) TYPE P DECIMALS 2, "n497992
WAERS LIKE T001-WAERS, "Währungseinheit
END OF IMARD.


************************************************************************
* R A N G E S
************************************************************************
RANGES: R_DATE FOR MKPF-BUDAT. " Date Range for Year

************************************************************************
* G L O B A L V A R I A B L E S
************************************************************************
DATA: FST_OF_MNTH LIKE SY-DATUM, " First of Month
FST_OF_YEAR LIKE SY-DATUM. " First of April
DATA: FLG_AFTER. " Flg for High after April 1st

DATA SERNO(9) TYPE C.

DATA OPTOT TYPE P DECIMALS 3.

DATA RECBAL TYPE P DECIMALS 3.
DATA RECTOT TYPE P DECIMALS 3.
DATA RECTOT_YR TYPE P DECIMALS 3.

DATA ISSBAL TYPE P DECIMALS 3.
DATA ISSTOT TYPE P DECIMALS 3.
DATA ISSTOT_YR TYPE P DECIMALS 3.

DATA TOTALL TYPE P DECIMALS 3.
DATA TOTAL_YR TYPE P DECIMALS 3.

DATA OPTOTALL TYPE P DECIMALS 3.

DATA RECTOTALL TYPE P DECIMALS 3. "'Receipts(101/102)'
DATA ISSTOTALL TYPE P DECIMALS 3." 'Issues(261/262)',

*- DATA TOTALL
DATA TOT_RECBAL TYPE P DECIMALS 3.
DATA TOT_ISSBAL TYPE P DECIMALS 3.

DATA FLG TYPE N.
DATA FLAG TYPE N.


DATA STOCKVAL TYPE MSEG-DMBTR.
DATA REC_STOCKVAL TYPE MSEG-DMBTR.
DATA ISS_STOCKVAL TYPE MSEG-DMBTR.
DATA STOCKVALALL TYPE MSEG-DMBTR.

DATA REC_STOCKVALALL TYPE MSEG-DMBTR.
DATA ISS_STOCKVALALL TYPE MSEG-DMBTR.
DATA CL_STKVAL TYPE MSEG-DMBTR.
DATA : CL_STKVALALL TYPE MSEG-DMBTR,
REPID LIKE SY-REPID,
FRM_DATE(10),TO_DATE(10),
DISPTEXT(255),DISPTEXT2(255).

************************************************************************
* S E L E C T I O N S C R E E N
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK X WITH FRAME TITLE TEXT-007.
SELECT-OPTIONS: P_MATNR FOR MSEG-MATNR, " Material
P_MGRP FOR MARA-MATKL, " Material Grp
P_DATE FOR MKPF-BUDAT, " Date
P_PLANT FOR MSEG-WERKS " Plant
DEFAULT 'NB01'
OBLIGATORY NO INTERVALS
NO-EXTENSION .
SELECTION-SCREEN END OF BLOCK X.

************************************************************************
* I N I T I A L I Z A T I O N
************************************************************************
INITIALIZATION.
REPID = SY-REPID.

************************************************************************
* A T S E L E C T I O N-S C R E E N V A L I D A T I O N S
************************************************************************
AT SELECTION-SCREEN.
IF P_DATE-LOW IS INITIAL OR P_DATE-HIGH IS INITIAL.
MESSAGE E902.
ENDIF.

AUTHORITY-CHECK OBJECT 'Z_WERKS_PL'
ID 'WERKS' FIELD P_PLANT-LOW.
IF SY-SUBRC NE 0.
MESSAGE E000(ZT) WITH
'You are not authorised to use this Plant'(006).
ENDIF.

************************************************************************
* S T A R T - O F - S E L E C T I O N
************************************************************************
START-OF-SELECTION.
*- Get data
PERFORM GET_DATA.

************************************************************************
* E N D - O F - S E L E C T I O N
************************************************************************
END-OF-SELECTION.
*- Display the Results
PERFORM WRITE_REPORT.

*&---------------------------------------------------------------------*
*& Form FILL_FLDCAT
*&---------------------------------------------------------------------*
* Populate ALV Header
*----------------------------------------------------------------------*
FORM FILL_FLDCAT USING P_FLDCAT TYPE SLIS_T_FIELDCAT_ALV.
PERFORM FILL_PARAM_FLDCAT USING 1 'Sl.no' 'SNO'
' ' 'X' 'OPBAL2' P_FLDCAT.
PERFORM FILL_PARAM_FLDCAT USING 2 'MATERIAL No.' 'MATNR'
' ' 'X' 'OPBAL2' P_FLDCAT.
PERFORM FILL_PARAM_FLDCAT USING 3 'Batch' 'BATCH'
' ' 'X' 'OPBAL2' P_FLDCAT.
PERFORM FILL_PARAM_FLDCAT USING 4 'OP.Stock' 'OP_BAL'
' ' ' ' 'OPBAL2' P_FLDCAT.
PERFORM FILL_PARAM_FLDCAT USING 5 'OP.Stock(Yr)' 'OP_BAL_YR'
' ' ' ' 'OPBAL2' P_FLDCAT.
PERFORM FILL_PARAM_FLDCAT USING 6 'Rcpts. Plan' 'REC_PLN'
' ' ' ' 'OPBAL2' P_FLDCAT.
PERFORM FILL_PARAM_FLDCAT USING 7 'Rcpts.' 'REC'
' ' ' ' 'OPBAL2' P_FLDCAT.
PERFORM FILL_PARAM_FLDCAT USING 8 'Rcpts.(Yr)' 'REC_YR'
' ' ' ' 'OPBAL2' P_FLDCAT.
PERFORM FILL_PARAM_FLDCAT USING 9 'Iss. Plan' 'ISS_PLN'
' ' ' ' 'OPBAL2' P_FLDCAT.
PERFORM FILL_PARAM_FLDCAT USING 10 'Issues' 'ISS'
' ' ' ' 'OPBAL2' P_FLDCAT.
PERFORM FILL_PARAM_FLDCAT USING 11 'Iss. (Yr)' 'ISS_YR'
' ' ' ' 'OPBAL2' P_FLDCAT.
PERFORM FILL_PARAM_FLDCAT USING 12 'Closing Stock.' 'CL_BAL'
' ' ' ' 'OPBAL2' P_FLDCAT.
PERFORM FILL_PARAM_FLDCAT USING 13 'Closing Stk. (Yr)' 'CL_BAL_YR'
' ' ' ' 'OPBAL2' P_FLDCAT.

ENDFORM. "FILL_FLDCAT

*&---------------------------------------------------------------------*
*& Form FILL_PARAM_FLDCAT
*&---------------------------------------------------------------------*
* FILL PARAM FIELDCATALOG
*----------------------------------------------------------------------*
FORM FILL_PARAM_FLDCAT USING VALUE(P_COL) VALUE(P_TEXT) VALUE(P_FLDNAME)
VALUE(P_DOSUM) VALUE(P_ZERO)
VALUE(P_TABNAME)
PP_FLDCAT TYPE SLIS_T_FIELDCAT_ALV.
DATA : WA_FLDCAT TYPE SLIS_FIELDCAT_ALV.
CLEAR WA_FLDCAT.
WA_FLDCAT-COL_POS = P_COL.
WA_FLDCAT-SELTEXT_M = P_TEXT.
WA_FLDCAT-FIELDNAME = P_FLDNAME.
WA_FLDCAT-TABNAME = P_TABNAME.
WA_FLDCAT-DO_SUM = P_DOSUM.
WA_FLDCAT-NO_ZERO = P_ZERO.
APPEND WA_FLDCAT TO PP_FLDCAT.
ENDFORM. "FILL_PARAM_FLDCAT


*&---------------------------------------------------------------------*
*& Form FILL_HEADER
*&---------------------------------------------------------------------*
* Fill Header
*----------------------------------------------------------------------*
FORM FILL_HEADER USING P_IT_LIST_HEADER TYPE SLIS_T_LISTHEADER.
DATA: LS_LINE TYPE SLIS_LISTHEADER.

CLEAR LS_LINE.
LS_LINE-TYP = 'S'.
IF P_PLANT-LOW EQ 'NB01' OR P_PLANT-LOW EQ 'NB02'.
LS_LINE-INFO = TEXT-002.
ELSEIF P_PLANT-LOW EQ 'NB11' OR P_PLANT-LOW EQ 'NB12'.
LS_LINE-INFO = TEXT-005.
ENDIF.
APPEND LS_LINE TO P_IT_LIST_HEADER.

*- Build Date Range Text
CONCATENATE P_DATE-LOW+6(2) '.' P_DATE-LOW+4(2) '.'
P_DATE-LOW+0(4) INTO FRM_DATE.

CONCATENATE P_DATE-HIGH+6(2) '.' P_DATE-HIGH+4(2) '.'
P_DATE-HIGH+0(4) INTO TO_DATE.

CONCATENATE TEXT-003 FRM_DATE ' To ' TO_DATE
INTO DISPTEXT SEPARATED BY SPACE.

*- Build Plant Text
CONCATENATE TEXT-004 P_PLANT-LOW
INTO DISPTEXT2 SEPARATED BY SPACE.

CLEAR LS_LINE.
LS_LINE-TYP = 'S'.
LS_LINE-INFO = DISPTEXT.
APPEND LS_LINE TO P_IT_LIST_HEADER.

CLEAR LS_LINE.
LS_LINE-TYP = 'S'.
LS_LINE-INFO = DISPTEXT2.
APPEND LS_LINE TO P_IT_LIST_HEADER.

ENDFORM. "FILL_HEADER

*&--------------------------------------------------------------------*
*& Form FILL_EVENTS
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->P_IT_EVENTStext
*---------------------------------------------------------------------*
FORM FILL_EVENTS USING P_IT_EVENTS TYPE SLIS_T_EVENT.
DATA LS_EVENT TYPE SLIS_ALV_EVENT.

MOVE 'TOP_OF_PAGE' TO LS_EVENT-FORM.
MOVE 'TOP_OF_PAGE' TO LS_EVENT-NAME.
APPEND LS_EVENT TO P_IT_EVENTS.
ENDFORM. "FILL_EVENTS

*&--------------------------------------------------------------------*
*& Form TOP_OF_PAGE
*&--------------------------------------------------------------------*
* TOP - OF - PAGE
*---------------------------------------------------------------------*
FORM TOP_OF_PAGE.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
IT_LIST_COMMENTARY = IT_LIST_HEADER.
ENDFORM. "TOP_OF_PAGE

*&---------------------------------------------------------------------*
*& Form FILL_LAYOUT
*&---------------------------------------------------------------------*
* Fill Layout
*----------------------------------------------------------------------*
FORM FILL_LAYOUT USING P_GS_LAYOUT TYPE SLIS_LAYOUT_ALV.
P_GS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
P_GS_LAYOUT-ZEBRA = 'X'.
P_GS_LAYOUT-FLEXIBLE_KEY = 'X'.
ENDFORM. "FILL_LAYOUT

*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* Get data
*----------------------------------------------------------------------*
FORM GET_DATA .
DATA: OPEN_STOCK LIKE BSIM-MENGE,
L_YEAR(4),
L_MONTH(2),
L_DATE LIKE BSIM-BUDAT,
L_STOCK LIKE BSIM-DMBTR,
L_PLANT LIKE T001W-WERKS.

DATA: SHKZG TYPE MSEG-SHKZG,
LQ TYPE QALS-LOSMENGE,
DQ TYPE LIPS-LFIMG,
DLQ TYPE QALS-LOSMENGE,
UNIT TYPE QALS-MENGENEINH.

*- Get dates: First of that month and
*- April 1st of Fiscal Year

*- First of that month
CONCATENATE P_DATE-LOW+0(4)
P_DATE-LOW+4(2)
'01' INTO FST_OF_MNTH.

*- First of April of that Year
L_YEAR = P_DATE-LOW+0(4).
L_MONTH = P_DATE-LOW+4(2).

*- If month is < 4 (April) then year is less by 1
IF L_MONTH < 4.
L_YEAR = L_YEAR - 1.
ELSE. " year is the same
ENDIF.

CONCATENATE L_YEAR
'04' " April
'01' INTO FST_OF_YEAR.

*- Build Date Range for Year if P_DATE-HIGH is after
*- April 1st of the same year
IF FST_OF_YEAR < P_DATE-HIGH.
*- Set Flag
FLG_AFTER = 'X'.
*- Build Range with Date Range
R_DATE-LOW = FST_OF_YEAR.
R_DATE-HIGH = P_DATE-HIGH.
R_DATE-OPTION = 'BT'.
R_DATE-SIGN = 'I'.

APPEND R_DATE.
CLEAR R_DATE.
ENDIF.

SELECT A~MATNR
F~MAKTX
A~CHARG
A~BWART
A~MENGE
A~ZEILE
A~WERKS
A~SOBKZ
D~BUDAT
E~MTART
A~SHKZG
A~DMBTR
A~MBLNR
A~LGORT
INTO TABLE OPBAL
FROM MKPF AS D JOIN MSEG AS A
ON D~MBLNR = A~MBLNR AND
D~MJAHR = A~MJAHR
JOIN MARA AS E
ON E~MATNR = A~MATNR
JOIN MAKT AS F
ON F~MATNR = A~MATNR
WHERE A~MATNR IN P_MATNR
AND A~WERKS IN P_PLANT
AND A~KZBEW IN (' ','B')
AND E~MATKL IN P_MGRP
AND A~BWART NOT IN ('541','542','321','322','121')
AND A~KZVBR NOT IN ('A')
AND D~BUDAT LE P_DATE-HIGH.


LOOP AT OPBAL WHERE BWART EQ '544' AND LGORT EQ ''.
TMP_OPBAL = OPBAL.
APPEND TMP_OPBAL.
ENDLOOP.

DELETE OPBAL WHERE ( BWART EQ '309' AND DMBTR EQ 0 ).
DELETE OPBAL WHERE ( BWART NE '543' AND LGORT EQ '' ).

LOOP AT TMP_OPBAL.
OPBAL = TMP_OPBAL.
APPEND OPBAL.
ENDLOOP.

STOCKVAL = 0.

IF NOT OPBAL[] IS INITIAL.
SORT OPBAL BY MATNR BATCH BUDAT.
ELSE.
MESSAGE I005(ZT).
STOP.
ENDIF.

************************************************************************
LOOP AT OPBAL.

SELECT SINGLE LBLAB INTO MSLB-LBLAB FROM MSLB
WHERE MATNR = OPBAL-MATNR.

************ For the duration II part *****************

IF OPBAL-BUDAT IN P_DATE.

IF OPBAL-SHKZG = 'H'.
ISSTOT = ISSTOT + OPBAL-MENGE.
ELSEIF OPBAL-SHKZG = 'S'.
RECTOT = RECTOT + OPBAL-MENGE.
ENDIF.

ENDIF.

*- For calculating the Year Receipts/Issues when flag is set
IF FLG_AFTER = 'X'.
IF OPBAL-BUDAT IN R_DATE.

IF OPBAL-SHKZG = 'H'.
ISSTOT_YR = ISSTOT_YR + OPBAL-MENGE.
ELSEIF OPBAL-SHKZG = 'S'.
RECTOT_YR = RECTOT_YR + OPBAL-MENGE.
ENDIF.

ENDIF.
ENDIF.

AT END OF BATCH. " modified for chek

*- Get opening Stocks for the Material based on
*- 1st of the Month and 1st of April of that year

*- For 1st of that month
* CLEAR OPEN_STOCK.
* L_PLANT = P_PLANT-LOW.
*
* L_DATE = FST_OF_MNTH.
*
* CALL FUNCTION 'ZMM_OPBAL_QTY_BASED_KEYDATE'
* EXPORTING
* I_MATNR = OPBAL-MATNR
* I_PLANT = L_PLANT
* I_KEYDT = L_DATE
* IMPORTING
* E_STK_OPBAL = OPEN_STOCK
** E_STK_VAL =
* .
*
* IF SY-SUBRC = 0.
* OPTOT = OPEN_STOCK.
* ENDIF.


PERFORM GET_OPENING_STOCK.
*- Opening Stock (Batch)


*- For 1st of April of that Year
CLEAR OPEN_STOCK.
L_PLANT = P_PLANT-LOW.

L_DATE = FST_OF_YEAR.


CALL FUNCTION 'ZMM_OPBAL_QTY_BASED_KEYDATE'
EXPORTING
I_MATNR = OPBAL-MATNR
I_PLANT = L_PLANT
I_KEYDT = L_DATE
IMPORTING
E_STK_OPBAL = OPEN_STOCK
* E_STK_VAL =
.

IF SY-SUBRC = 0.
OPBAL2-OP_BAL_YR = OPEN_STOCK. " Opening Stock for Yr
ENDIF.


TOTALL = OPTOT + RECTOT - ISSTOT.

*- Calculate Closing Bal for Year
TOTAL_YR = OPBAL2-OP_BAL_YR + RECTOT_YR - ISSTOT_YR.

IF OPTOT = '0' AND RECTOT = '0' AND ISSTOT = '0'.
FLG = 1.
ENDIF.

IF FLG = 1.
CLEAR FLG.
ELSE.
SERNO = SERNO + 1.

OPBAL2-SNO = SERNO.
OPBAL2-MATNR = OPBAL-MATNR.
OPBAL2-MAKTX = OPBAL-MAKTX.
OPBAL2-OP_BAL = OPTOT.
OPBAL2-REC = RECTOT.
OPBAL2-T_OPBAL_REC = OPTOT + RECTOT.
OPBAL2-ISS = ISSTOT.
OPBAL2-CL_BAL = TOTALL.
OPBAL2-BATCH = OPBAL-BATCH.
OPBAL2-SUBCON = MSLB-LBLAB.

*- Year Information
IF FLG_AFTER = 'X'.
OPBAL2-REC_YR = RECTOT_YR.
OPBAL2-ISS_YR = ISSTOT_YR.
OPBAL2-CL_BAL_YR = TOTAL_YR.
ENDIF.

APPEND OPBAL2.
ENDIF.

OPTOT = 0.
RECTOT = 0.
ISSTOT = 0.

RECTOT_YR = 0.
ISSTOT_YR = 0.
TOTAL_YR = 0.
ENDAT.

ENDLOOP.
ENDFORM. " GET_DATA

*&---------------------------------------------------------------------*
*& Form WRITE_REPORT
*&---------------------------------------------------------------------*
* Write Report
*----------------------------------------------------------------------*
FORM WRITE_REPORT .
IF NOT OPBAL2[] IS INITIAL.

*- Fill Layout
PERFORM FILL_LAYOUT USING GS_LAYOUT.

*- Perform Fill Events
PERFORM FILL_EVENTS USING IT_EVENTS.

*- Perform Fill ALV Header
PERFORM FILL_HEADER USING IT_LIST_HEADER.

*- Populate ALV Field catalog
PERFORM FILL_FLDCAT USING IT_FLDCAT.

*- Display Data
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = REPID
IT_FIELDCAT = IT_FLDCAT
IT_EVENTS = IT_EVENTS
IS_LAYOUT = GS_LAYOUT
TABLES
T_OUTTAB = OPBAL2.

ENDIF.
ENDFORM. " WRITE_REPORT

*&---------------------------------------------------------------------*
*& Form GET_OPENING_STOCK
*&---------------------------------------------------------------------*
* ******* USE THIS LOGIC FOR GETTING OPENING STOCK OF A BATCH ******
* ********** THE WAY ITS DONE (TO SOME EXTENT) IN Trxn MB5B **********
* **************** VERY USEFUL TO GET STOCK FASTER **************
*----------------------------------------------------------------------*
FORM GET_OPENING_STOCK .
DATA: TOTAL TYPE P DECIMALS 3.

CLEAR OPTOT.
REFRESH: IMARD, G_T_MSEG_LEAN.
CLEAR: IMARD, G_T_MSEG_LEAN.

*- Select data from IMARD
SELECT * FROM MARD INTO CORRESPONDING FIELDS OF TABLE IMARD
WHERE WERKS = P_PLANT-LOW
AND MATNR = OPBAL-MATNR.

IF SY-SUBRC = 0.
READ TABLE IMARD INDEX 1.
IF SY-SUBRC = 0.
TOTAL = IMARD-LABST.
ENDIF.
ENDIF.

*- Select data from MSEG and MKPF
SELECT
MKPF~MBLNR
MKPF~MJAHR
MKPF~BLART
MKPF~BUDAT
MKPF~CPUDT
MSEG~MATNR
MSEG~WERKS
MSEG~LGORT
MSEG~CHARG
MSEG~SHKZG
MSEG~MENGE

INTO CORRESPONDING FIELDS OF TABLE G_T_MSEG_LEAN
FROM MKPF AS MKPF JOIN MSEG AS MSEG
ON MKPF~MANDT = MSEG~MANDT AND
MKPF~MBLNR = MSEG~MBLNR AND
MKPF~MJAHR = MSEG~MJAHR
WHERE MSEG~MATNR = OPBAL-MATNR
AND MSEG~WERKS = P_PLANT-LOW
AND MSEG~CHARG = OPBAL-BATCH
AND MKPF~BUDAT GE P_DATE-LOW.

SORT G_T_MSEG_LEAN BY WERKS MATNR CHARG SHKZG DESCENDING.

IF NOT G_T_MSEG_LEAN[] IS INITIAL.
LOOP AT G_T_MSEG_LEAN.
IF G_T_MSEG_LEAN-SHKZG = 'H'.
TOTAL = TOTAL + G_T_MSEG_LEAN-MENGE.
ELSE.
TOTAL = TOTAL - G_T_MSEG_LEAN-MENGE.
ENDIF.
ENDLOOP.
*- Assign Stock
OPTOT = TOTAL.
ENDIF.

ENDFORM. " GET_OPENING_STOCK
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ММ All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.