To extract purchase price variance (PPV) from SAP Instructed to go for detail in BSEG then validate posting period from BKPF

REPORT ZAWBPVPM LINE-COUNT 58 LINE-SIZE 170 .
*-----------------------------------------------------------------------
*
* Purpose: To extract purchase price variance (PPV) from SAP
* Instructed to go for detail in BSEG then validate posting
* period from BKPF
*
**** This program will extract previous months transactions ****
*
* Reading MSEG only for Crosby (0007) to retrieve location.
* Need to retrieve location because 5th pos. of raw mtl code
* for Crosby can be a 7,0, or 9.
*
* Tables : BSEG - Accounting Document detail
* BKPF - Accounting Document header
* MSEG - Material Document detail (need for item storage loc)
* MAKT - Material Description
* Files : zitmxref - Cross reference SAP to Legacy Item Ids (input)
* : zfiu007 - Extract to Legacy for Buffalo (output)
* : zfiu008 - for Crosby (output)
* : zfiu009 - for Geneseo (output)
*-----------------------------------------------------------------------
* Modifications
*
* 9/18/95 AWB replaced unix file with table ZXR06 (cross reference)
*
*-----------------------------------------------------------------------
TABLES : BKPF, BSEG, MSEG, MAKT, ZXR06.

DATA: DSN1(30) VALUE '/atomnt/ /abap/zfiu007.txt',
DSN2(30) VALUE '/atomnt/ /abap/zfiu008.txt',
DSN3(30) VALUE '/atomnt/ /abap/zfiu009.txt'.

DSN1+8(3) = SY-SYSID.
DSN2+8(3) = SY-SYSID.
DSN3+8(3) = SY-SYSID.

DATA: BEGIN OF *BSEG OCCURS 1000.
INCLUDE STRUCTURE BSEG.
DATA: END OF *BSEG.

DATA: BEGIN OF *MSEG OCCURS 1000.
INCLUDE STRUCTURE MSEG.
DATA: END OF *MSEG.

DATA: COUNT(4) TYPE N,
BCOUNT(4) TYPE N,
CCOUNT(4) TYPE N,
GCOUNT(4) TYPE N,
PAMT(9) TYPE P DECIMALS 2,
TOTAL(9) TYPE P DECIMALS 2,
NUMAMT(13) TYPE C,
PLANT(4) TYPE C,
POSTPER(6) TYPE N,
INDX(4) TYPE P,
XREFLKY(26) TYPE C,
XREFPKY(22) TYPE C,
FIRST(1) TYPE C,
SAPID(6) TYPE C,
MATDSC(25) TYPE C,
PSTYYMM(6) TYPE C,
PSTMM(2) TYPE N,
YEARCHK(4) TYPE N.
*
DATA: BEGIN OF EXTREC,
RAWCDE(5) TYPE C,
VARAMT(13) TYPE C,
PERIOD(2) TYPE C,
DIVSN(1) TYPE C,
SIGN(1) TYPE C,
FILL(58) TYPE C.
DATA: END OF EXTREC.
*
*TOP-OF-PAGE.
*WRITE: SY-DATUM DD/MM/YY, 59 SY-TITLE, 100 TEXT-006, 165 SY-PAGNO.
* ULINE.
*******************************************************
OPEN DATASET DSN1 FOR OUTPUT IN TEXT MODE.
OPEN DATASET DSN2 FOR OUTPUT IN TEXT MODE.
OPEN DATASET DSN3 FOR OUTPUT IN TEXT MODE.
*******************************************************
IF SY-SUBRC NE 0.
WRITE:/ TEXT-099.
WRITE:/ TEXT-001, SY-SUBRC.
WRITE:/ TEXT-099.
EXIT.
ENDIF.
PERFORM HEADER.
PERFORM MAIN.
PERFORM TRAILER.
*---------------------------------------------------------------------*
* FORM MAIN *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM MAIN.
IF SY-DATUM+4(2) = 01.
YEARCHK = SY-DATUM+0(4) - 1.
ELSE.
YEARCHK = SY-DATUM+0(4).
ENDIF.
* select records from BSEG
SELECT * FROM BSEG INTO TABLE *BSEG
WHERE BUKRS = 'EAUS'
AND HKONT = '0004816000'
AND GJAHR = YEARCHK
AND GSBER = '0002'
AND KTOSL = 'PRD'
*
OR BUKRS = 'EAUS'
AND HKONT = '0005614000'
AND GJAHR = YEARCHK
AND GSBER = '0002'
AND KTOSL = 'PRD'
*
OR BUKRS = 'EAUS'
AND HKONT = '0004816000'
AND GJAHR = YEARCHK
AND GSBER = '0015'
AND KTOSL = 'PRD'
*
OR BUKRS = 'EAUS'
AND HKONT = '0005614000'
AND GJAHR = YEARCHK
AND GSBER = '0015'
AND KTOSL = 'PRD'.

IF SY-SUBRC = 0.

SORT *BSEG BY
WERKS
MATNR.

LOOP AT *BSEG.
IF FIRST = 'X'.
ON CHANGE OF *BSEG-WERKS.
SKIP.
WRITE: 51 'Total', TOTAL.
SKIP.
WRITE: / 'TOTAL RECS PROCESSED: ' , COUNT.
CLEAR COUNT.
CLEAR TOTAL.
NEW-PAGE.
ENDON.
ENDIF.
MOVE 'X' TO FIRST.


SELECT SINGLE * FROM BKPF
WHERE BUKRS = *BSEG-BUKRS
AND BELNR = *BSEG-BELNR
AND GJAHR = *BSEG-GJAHR.

IF SY-SUBRC = 0.

PERFORM CLEAR_FLD.

IF *BSEG-WERKS = 0007.
PERFORM GET_RAW7.
ELSE.
PERFORM GET_RAW68.
ENDIF.


IF SY-DATUM+4(2) NE 01.
PSTMM = SY-DATUM+4(2) - 1.
MOVE SY-DATUM+0(4) TO PSTYYMM+0(4).
MOVE PSTMM TO PSTYYMM+4(2).
ELSE.
PSTYYMM+0(4) = SY-DATUM+0(4) - 1.
PSTMM = 12.
MOVE PSTMM TO PSTYYMM+4(2).
ENDIF.

IF BKPF-BUDAT+0(6) = PSTYYMM.
MOVE BKPF-BUDAT+0(6) TO POSTPER.
MOVE BKPF-BUDAT+4(2) TO EXTREC-PERIOD.

IF *BSEG-GSBER = '0002'.
MOVE 'L' TO EXTREC-DIVSN.
ELSEIF *BSEG-GSBER = '0015'.
MOVE 'F' TO EXTREC-DIVSN.
ENDIF.

MOVE SPACE TO EXTREC-SIGN.
IF *BSEG-BSCHL = '93'.
MOVE '-' TO EXTREC-SIGN.
ENDIF.

MOVE *BSEG-PSWBT TO NUMAMT.
MOVE ' ' TO EXTREC-VARAMT+0(2).
WRITE NUMAMT+0(9) TO EXTREC-VARAMT+2(9).
WRITE NUMAMT+10(2) TO EXTREC-VARAMT+11(2).
MOVE *BSEG-PSWBT TO PAMT.
MOVE *BSEG-WERKS+2(2) TO PLANT.

IF EXTREC-SIGN = '-'.
TOTAL = TOTAL - PAMT.
ELSE.
TOTAL = TOTAL + PAMT.
ENDIF.
COUNT = COUNT + 1.

IF *BSEG-WERKS = 0006.
PERFORM BUFFEXT.

ELSEIF *BSEG-WERKS = 0007.
PERFORM CRSBEXT.

ELSEIF *BSEG-WERKS = 0008.
PERFORM GENSEXT.
ELSE.

ENDIF.

PERFORM WRITE.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.
ENDFORM.
*---------------------------------------------------------------------*
* FORM CLEAR_FLD *
*---------------------------------------------------------------------*
* Clear extract fields *
*---------------------------------------------------------------------*
FORM CLEAR_FLD.
CLEAR EXTREC-RAWCDE.
CLEAR EXTREC-VARAMT.
CLEAR EXTREC-PERIOD.
CLEAR EXTREC-DIVSN.
CLEAR EXTREC-SIGN.
ENDFORM.
*---------------------------------------------------------------------*
* FORM GET_RAW7 *
*---------------------------------------------------------------------*
* Get location for Crosby *
*---------------------------------------------------------------------*
FORM GET_RAW7.

CLEAR EXTREC-RAWCDE.

SELECT * FROM MSEG INTO TABLE *MSEG
WHERE MBLNR = BKPF-AWKEY+0(10)
AND MJAHR = *BSEG-GJAHR
AND MATNR = *BSEG-MATNR
AND WERKS = *BSEG-WERKS.

IF SY-SUBRC = 0.

LOOP AT *MSEG.

SELECT SINGLE * FROM ZXR06
WHERE ZMATNR = *MSEG-MATNR
AND ZPLT = *MSEG-WERKS
AND ZLOCA = *MSEG-LGORT.

IF SY-SUBRC = 0.
MOVE ZXR06-ZMFGID+0(4) TO EXTREC-RAWCDE+0(4).
MOVE ZXR06-ZRPTL+3(1) TO EXTREC-RAWCDE+4(1).
ELSE.
MOVE '*' TO EXTREC-RAWCDE+0(1).
MOVE *MSEG-MATNR+14(4) TO EXTREC-RAWCDE+1(4).
ENDIF.
ENDLOOP.

* if no record on MSEG default to location 0001 - per D.Lotierzo 7/17/95
ELSE.
SELECT SINGLE * FROM ZXR06
WHERE ZMATNR = *MSEG-MATNR
AND ZPLT = *MSEG-WERKS
AND ZLOCA = '0001'.
IF SY-SUBRC = 0.
MOVE ZXR06-ZMFGID+0(4) TO EXTREC-RAWCDE+0(4).
MOVE ZXR06-ZRPTL+3(1) TO EXTREC-RAWCDE+4(1).
ELSE.
MOVE '*' TO EXTREC-RAWCDE+0(1).
MOVE *BSEG-MATNR+14(4) TO EXTREC-RAWCDE+1(4).
ENDIF.
ENDIF.
ENDFORM.
*---------------------------------------------------------------------*
* FORM GET_RAW68 *
*---------------------------------------------------------------------*
* Get raw material code Buffalo/Geneseo *
*---------------------------------------------------------------------*
FORM GET_RAW68.

CLEAR EXTREC-RAWCDE.

SELECT * FROM ZXR06
WHERE ZMATNR = *BSEG-MATNR
AND ZPLT = *BSEG-WERKS.
ENDSELECT.
IF SY-SUBRC = 0.
MOVE ZXR06-ZMFGID+0(4) TO EXTREC-RAWCDE+0(4).
IF EXTREC-RAWCDE > SPACE.
MOVE *BSEG-WERKS+3(1) TO EXTREC-RAWCDE+4(1).
ENDIF.
ELSE.
MOVE '*' TO EXTREC-RAWCDE+0(1).
MOVE *BSEG-MATNR+14(4) TO EXTREC-RAWCDE+1(4).
ENDIF.

ENDFORM.
*---------------------------------------------------------------------*
* FORM WRITE *
*---------------------------------------------------------------------*
* Write edit report *
*---------------------------------------------------------------------*
FORM WRITE.
*
*
SELECT SINGLE * FROM MAKT
WHERE MATNR = *BSEG-MATNR
AND SPRAS = 'E'.
IF SY-SUBRC = 0.
MOVE MAKT-MAKTG+0(25) TO MATDSC.
ELSE.
CLEAR MATDSC.
ENDIF.

WRITE:/ *BSEG-MATNR+12(6),
9 *BSEG-WERKS,
16 *BSEG-GSBER,
24 BKPF-BUDAT,
37 EXTREC-RAWCDE,
46 *BSEG-BELNR,
57 PAMT,
75 EXTREC-SIGN,
80 MATDSC.

IF EXTREC-RAWCDE = SPACE.
WRITE: 109 TEXT-004.
ENDIF.

IF *BSEG-WERKS = SPACE AND EXTREC-RAWCDE+0(1) = '*'.
WRITE 109 TEXT-002.
WRITE:/109 TEXT-003.
ELSE.
IF *BSEG-WERKS = SPACE.
WRITE: 109 TEXT-002.
ENDIF.
ENDIF.

IF EXTREC-RAWCDE+0(1) = '*'.
WRITE: 109 TEXT-003.
ENDIF.
ENDFORM.
*---------------------------------------------------------------------*
* FORM BUFFEXT *
*---------------------------------------------------------------------*
* Extract for Buffalo Plant *
*---------------------------------------------------------------------*
FORM BUFFEXT.
BCOUNT = BCOUNT + 1.
TRANSFER EXTREC TO DSN1.
ENDFORM.
*---------------------------------------------------------------------*
* FORM CRSBEXT *
*---------------------------------------------------------------------*
* Extract for Crosby Plant *
*---------------------------------------------------------------------*
FORM CRSBEXT.
CCOUNT = CCOUNT + 1.
TRANSFER EXTREC TO DSN2.
ENDFORM.
*---------------------------------------------------------------------*
* FORM GENSEXT *
*---------------------------------------------------------------------*
* Extract for Crosby Plant *
*---------------------------------------------------------------------*
FORM GENSEXT.
GCOUNT = GCOUNT + 1.
TRANSFER EXTREC TO DSN3.
ENDFORM.
*---------------------------------------------------------------------*
* FORM HEADER *
*---------------------------------------------------------------------*
* Generate header records for extract files. *
*---------------------------------------------------------------------*
FORM HEADER.
MOVE '99' TO EXTREC-RAWCDE+0(2).
MOVE SY-REPID TO EXTREC-VARAMT.
MOVE SPACE TO EXTREC-PERIOD.
MOVE SPACE TO EXTREC-DIVSN.
*
TRANSFER EXTREC TO DSN1.
*
TRANSFER EXTREC TO DSN2.
*
TRANSFER EXTREC TO DSN3.
*
ENDFORM.
*---------------------------------------------------------------------*
* FORM TRAILER *
*---------------------------------------------------------------------*
* Generate trailer records for extract files. *
* Print total record for control report. *
*---------------------------------------------------------------------*
FORM TRAILER.
*
MOVE SPACE TO EXTREC-RAWCDE.
MOVE '99' TO EXTREC-RAWCDE+0(2).
MOVE COUNT TO EXTREC-VARAMT.
MOVE SPACE TO EXTREC-PERIOD.
MOVE SPACE TO EXTREC-DIVSN.
MOVE BCOUNT TO EXTREC-VARAMT.
TRANSFER EXTREC TO DSN1.
CLOSE DATASET DSN1.
*
MOVE CCOUNT TO EXTREC-VARAMT.
TRANSFER EXTREC TO DSN2.
CLOSE DATASET DSN2.
*
MOVE GCOUNT TO EXTREC-VARAMT.
TRANSFER EXTREC TO DSN3.
CLOSE DATASET DSN3.
*
SKIP.
WRITE: 51 'Total', TOTAL.
SKIP.
WRITE: / 'TOTAL RECS PROCESSED: ' , COUNT.
*
ENDFORM.