REPORT  ZCLEDGER LINE-SIZE 145..
"* ABAP Z REPORT

TYPE-POOLS: SLIS.

TABLES: KNA1,
BKPF,
BSEG.

*----------------------------------------------------------------------*
*                      DECLARATION FOR ALV GRID.                       *
*----------------------------------------------------------------------*
DATA: REPORT_ID  LIKE SY-REPID.
DATA: I_LAYOUT   TYPE SLIS_LAYOUT_ALV.
DATA: I_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.


TYPES:BEGIN OF TY_BKPF,
      BUKRS TYPE BKPF-BUKRS,
      BELNR TYPE BKPF-BELNR,
      GJAHR TYPE BKPF-GJAHR,
      BUDAT TYPE BKPF-BUDAT,
      BLART TYPE BKPF-BLART,
      END OF TY_BKPF.

TYPES:BEGIN OF TY_BSEG,
      BUKRS TYPE BSEG-BUKRS,
      BELNR TYPE BSEG-BELNR,
      GJAHR TYPE BSEG-GJAHR,
      KUNNR TYPE BSEG-KUNNR,
      BUZID TYPE BSEG-BUZID,
      AUGBL TYPE BSEG-AUGBL,
      SHKZG TYPE BSEG-SHKZG,
      WRBTR TYPE BSEG-WRBTR,
      END OF TY_BSEG.

TYPES:BEGIN OF TY_FINAL,
      ZUONR   LIKE BSEG-ZUONR,
      BELNR   LIKE BSEG-BELNR,
      BLART   LIKE BKPF-BLART,
      UMSKZ   LIKE BSEG-UMSKZ,
      SGTXT   LIKE BSEG-SGTXT,
      BLDAT   LIKE BSEG-BZDAT,
      DEBIT   LIKE BSEG-WRBTR,
      CREDIT  LIKE BSEG-WRBTR,
      XBLNR   LIKE BSID-XBLNR,
      AUGBL   LIKE BSID-AUGBL,
       AUGBL2   LIKE BSID-AUGBL,
       XBLNR2 LIKE BSID-XBLNR,


      END OF TY_FINAL.
TYPES:BEGIN OF TY_BSEG2,
      BUKRS TYPE BSEG-BUKRS,
      BELNR TYPE BSEG-BELNR,
      GJAHR TYPE BSEG-GJAHR,
      KUNNR TYPE BSEG-KUNNR,
      BLDAT TYPE BSAD-BLDAT,
      AUGBL TYPE BSEG-AUGBL,
      SHKZG TYPE BSEG-SHKZG,
      WRBTR TYPE BSEG-WRBTR,
      ZUONR TYPE BSEG-ZUONR,
      SGTXT TYPE BSEG-SGTXT,
      WRBTR1 TYPE BSEG-WRBTR,
      WRBTR2 TYPE BSEG-WRBTR,
      END OF TY_BSEG2.
TYPES:BEGIN OF TY_SUM,
      KUNNR LIKE KNA1-KUNNR,
      WRBTR1 LIKE BSID-WRBTR,
      WRBTR2 LIKE BSID-WRBTR,
      END OF TY_SUM.

DATA:IT_BKPF TYPE STANDARD TABLE OF TY_BKPF,
     IT_BSEG TYPE STANDARD TABLE OF TY_BSEG,
     IT_BSEG1 TYPE STANDARD TABLE OF TY_BSEG,
     IT_BSEG2 TYPE STANDARD TABLE OF TY_BSEG,
     IT_FINAL TYPE STANDARD TABLE OF TY_FINAL.

DATA:WA_BKPF TYPE TY_BKPF,
     WA_BSEG TYPE TY_BSEG,
     WA_BSEG1 TYPE TY_BSEG,
     WA_BSEG3 TYPE TY_BSEG,
     WA_BSEG2 TYPE TY_BSEG2,
*     WA_BSAD TYPE TY_BSAD,
     WA_FINAL TYPE TY_FINAL,
     WA_SUM TYPE TY_SUM.
DATA : BEGIN OF ITAB OCCURS 0,
       BUDAT LIKE BSID-BUDAT, "posting date
KUNNR LIKE BSID-KUNNR, "customer no
WRBTR LIKE BSID-WRBTR, "amount in document
SHKZG LIKE BSID-SHKZG, "debit/credit indicator
SHKZG1 LIKE BSID-SHKZG, "debit/credit indicator

BUKRS LIKE BSID-BUKRS,
BELNR(30),
BLART LIKE BSID-BLART,
BLDAT LIKE BSID-BLDAT,
XBLNR LIKE BSID-XBLNR,
XBLNR2 LIKE BSID-XBLNR,

ZUONR LIKE BSID-ZUONR,
SGTXT LIKE BSID-SGTXT,
WRBTR_S LIKE BSID-WRBTR, "amount in document
WRBTR_H LIKE BSID-WRBTR, "amount in document
AMOUNT LIKE  BSID-WRBTR,
AUGBL LIKE BSID-AUGBL,
BUDAT1 LIKE BSID-BUDAT,
AUGDT LIKE BSID-AUGDT,
*SGTXT LIKE BSID-SGTXT,
GJAHR LIKE BSID-GJAHR,
BSCHL LIKE BSID-BSCHL,
BUZEI LIKE BSID-BUZEI,
CHE_NO LIKE BSID-XBLNR,
VBELN LIKE BSID-VBELN,
NAME1 LIKE KNA1-NAME1,
XRAGL LIKE BSID-XRAGL,
AUGBL2 LIKE BSID-AUGBL,
XNEGP LIKE BSID-XNEGP ,
HKONT LIKE BSAD-HKONT,
XBLNR1 LIKE BSAD-XBLNR,
       END OF ITAB.
DATA : ITAB1 LIKE ITAB OCCURS 0 WITH HEADER LINE.
DATA : ITAB2 LIKE ITAB OCCURS 0 WITH HEADER LINE.
DATA : ITAB3 LIKE ITAB OCCURS 0 WITH HEADER LINE.
DATA : ITAB4 LIKE ITAB OCCURS 0 WITH HEADER LINE.
DATA : ITAB5 LIKE ITAB OCCURS 0 WITH HEADER LINE.
DATA : ITAB6 LIKE ITAB OCCURS 0 WITH HEADER LINE.
DATA : BEGIN OF FINAL OCCURS 0,
        BUDAT LIKE BSID-BUDAT,
        ZUONR LIKE BSID-ZUONR,
        WRBTR_S LIKE BSID-WRBTR ,
        XBLNR1  LIKE BSID-XBLNR,
        AUGDT   LIKE BSID-AUGDT,
        WRBTR_H LIKE BSID-WRBTR,
        VBELN LIKE BSID-VBELN,
        BELNR LIKE BSID-BELNR,
        NAME1 LIKE KNA1-NAME1,
        KUNNR LIKE KNA1-KUNNR,
        BUDAT1 LIKE BSID-BUDAT,
        BLART LIKE BSID-BLART,
        BSCHL LIKE BSID-BSCHL,
        AUGBL LIKE BSID-AUGBL,
       END OF FINAL.
DATA:DATE1 TYPE SY-DATUM,
CLO LIKE BSID-WRBTR,
SUM_S LIKE BSID-WRBTR,
SUM_H LIKE BSID-WRBTR.
.
DATA : KEYBALANCE TYPE TABLE OF  BAPI3007_3 WITH HEADER LINE.
DATA : AMT TYPE P DECIMALS 2.
DATA : DEBIT_TOT TYPE BSAD-WRBTR.
DATA : CREDIT_TOT TYPE BSAD-WRBTR.
DATA : CLOSE_AMT TYPE P DECIMALS 2.

************************************************************************
*Parameters
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK MYSEL WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS: P_BUKRS FOR BSEG-BUKRS NO INTERVALS OBLIGATORY.
SELECT-OPTIONS: S_KUNNR FOR BSEG-KUNNR NO-EXTENSION NO INTERVALS
OBLIGATORY.
SELECT-OPTIONS: S_BUDAT FOR BKPF-BUDAT OBLIGATORY.
*  PARAMETERS    : P_GJAHR TYPE BKPF-GJAHR OBLIGATORY.
SELECTION-SCREEN END OF BLOCK MYSEL.
DATA: V_BK(4).
DATA: T_KUNNR LIKE S_KUNNR OCCURS 0 WITH HEADER LINE.

************************************************************************
* START-OF-SELECTION
************************************************************************
START-OF-SELECTION.
  REPORT_ID = SY-REPID.


******OPENING BALANCE CALCULATION
  DATE1 = S_BUDAT-LOW - 1.

  LOOP AT P_BUKRS.

    CALL FUNCTION 'BAPI_AR_ACC_GETKEYDATEBALANCE'
      EXPORTING
        COMPANYCODE  = P_BUKRS-LOW
        CUSTOMER     = S_KUNNR-LOW
        KEYDATE      = DATE1
        BALANCESPGLI = 'X'
      TABLES
        KEYBALANCE   = KEYBALANCE[].

    READ TABLE KEYBALANCE WITH KEY SP_GL_IND = ''.
    IF SY-SUBRC = 0.
    ELSE.
      CLEAR KEYBALANCE-LC_BAL.
    ENDIF.

    AMT = AMT + KEYBALANCE-LC_BAL.
    CLEAR:P_BUKRS-LOW.
  ENDLOOP.

*  ****************************opening balance okkk...
***************OPEN ITEM FROM BSID.
loop at p_bukrs.
  SELECT KUNNR WRBTR SHKZG BUDAT BLDAT BLART AUGDT  ZUONR AUGBL
  SGTXT BELNR BSCHL GJAHR XNEGP HKONT
*BSCHL ,ITAB2-BSCHL

    INTO (ITAB2-KUNNR ,ITAB2-WRBTR ,ITAB2-SHKZG ,
  ITAB2-BUDAT ,ITAB2-BLDAT ,
  ITAB2-BLART,
  ITAB2-AUGDT,ITAB2-ZUONR,
  ITAB2-AUGBL,ITAB2-SGTXT,ITAB2-BELNR,ITAB2-BSCHL,ITAB2-GJAHR,
ITAB2-XNEGP,ITAB2-HKONT) FROM BSID
    WHERE KUNNR = S_KUNNR-LOW
    AND BUKRS = P_BUKRS-LOW
    AND BUDAT IN S_BUDAT.
    APPEND ITAB2.
  ENDSELECT.
LOOP AT ITAB2.
IF ITAB2-SHKZG = 'H'.
ITAB2-WRBTR_H = ITAB2-WRBTR * -1.
MODIFY ITAB2.
ELSE.
IF ITAB2-BLART = 'J1' AND ITAB2-SHKZG = 'S' AND ITAB2-BSCHL = '04' .
ITAB2-WRBTR_H = ITAB2-WRBTR * 1.
MODIFY ITAB2.
ELSE.
ITAB2-WRBTR_S = ITAB2-WRBTR.
MODIFY ITAB2.
ENDIF.
ENDIF.
ENDLOOP.
*************COLLECTION FROM BSAD
  SELECT KUNNR WRBTR SHKZG BUDAT BLDAT BLART AUGDT  ZUONR AUGBL
  SGTXT BELNR XRAGL BSCHL XBLNR GJAHR XNEGP HKONT

    INTO (ITAB-KUNNR ,ITAB-WRBTR ,ITAB-SHKZG ,
  ITAB-BUDAT ,ITAB-BLDAT ,
  ITAB-BLART,
  ITAB-AUGDT,ITAB-ZUONR,
  ITAB-AUGBL,ITAB-SGTXT,ITAB-BELNR,ITAB-XRAGL,ITAB-BSCHL,ITAB-XBLNR,
ITAB-GJAHR,ITAB-XNEGP,ITAB-HKONT) FROM BSAD
    WHERE KUNNR = S_KUNNR-LOW
    AND BUKRS  = P_BUKRS-LOW

    AND BUDAT IN S_BUDAT.
    APPEND ITAB.
  ENDSELECT.
*ENDLOOP.
DATA : A1 LIKE BSID-AUGBL.
LOOP AT ITAB.

A1 = ITAB-BELNR.
IF ITAB-AUGBL = A1 AND ITAB-SHKZG = 'H'.
ITAB-WRBTR_H = ITAB-WRBTR * -1.
MODIFY ITAB.
ELSEIF ITAB-BLART = 'DG'.
ITAB-WRBTR_H = ITAB-WRBTR * -1.
MODIFY ITAB.
ELSEIF ITAB-BLART = 'J1' AND ITAB-BSCHL = '07' AND ITAB-SHKZG = 'S'.  .
ITAB-WRBTR_S = ITAB-WRBTR * 1.
MODIFY ITAB.
ELSEIF ITAB-BLART = 'J1' AND ITAB-SHKZG = 'H' AND ITAB-BSCHL <> '07'   .
ITAB-WRBTR_H = ITAB-WRBTR * -1.
MODIFY ITAB.

ELSEIF ITAB-BLART = 'J1' AND ITAB-SHKZG = 'S' AND ITAB-BSCHL <> '07' .
ITAB-WRBTR_H = ITAB-WRBTR * 1.
MODIFY ITAB.

ELSEIF ITAB-BLART = 'AB' AND ITAB-SHKZG = 'S'  .
ITAB-WRBTR_S = ITAB-WRBTR * 1.
MODIFY ITAB.
ELSEIF ITAB-BLART = 'AB' AND ITAB-SHKZG = 'H'  .
ITAB-WRBTR_H = ITAB-WRBTR * -1.
MODIFY ITAB.

ELSE.
IF ITAB-BLART = 'LU' AND ITAB-BSCHL = '16' AND ITAB-SHKZG = 'S'.
ITAB-WRBTR_S = ITAB-WRBTR * -1.
MODIFY ITAB.
ELSEIF ITAB-BLART = 'LU' AND ITAB-BSCHL = '16' AND ITAB-SHKZG = 'H'.
ITAB-WRBTR_H = ITAB-WRBTR * -1.
MODIFY ITAB.
ELSEIF ITAB-BLART = 'RV' AND ITAB-BSCHL <> '01'.
ITAB-WRBTR_H = ITAB-WRBTR * -1.
MODIFY ITAB.
ELSEIF ITAB-BLART = 'LU' AND  ITAB-BSCHL = '06'
AND ITAB-SHKZG = 'S'.
ITAB-WRBTR_H = ITAB-WRBTR * 1.
MODIFY ITAB.
ELSEIF ITAB-BLART = 'LU' AND  ITAB-BSCHL = '05'
AND ITAB-SHKZG = 'S'.
ITAB-WRBTR_H = ITAB-WRBTR * -1.
MODIFY ITAB.

ELSE.
ITAB-WRBTR_S = ITAB-WRBTR.
MODIFY ITAB.

ENDIF.
ENDIF.
ENDLOOP.
LOOP AT ITAB.
IF ITAB-WRBTR_H <> 0 AND ITAB-SHKZG = 'H' AND ITAB-BSCHL = '15'.

SELECT KUNNR WRBTR SHKZG BUDAT BLDAT BLART AUGDT  ZUONR AUGBL
  SGTXT BELNR XRAGL BSCHL XBLNR GJAHR XNEGP HKONT

    INTO (ITAB3-KUNNR ,ITAB3-WRBTR_H ,ITAB3-SHKZG ,
  ITAB3-BUDAT ,ITAB3-BLDAT ,
  ITAB3-BLART,
  ITAB3-AUGDT,ITAB3-ZUONR,
  ITAB3-AUGBL,ITAB3-SGTXT,ITAB3-BELNR,ITAB3-XRAGL,ITAB3-BSCHL,
ITAB3-XBLNR,
ITAB3-GJAHR,ITAB3-XNEGP,ITAB3-HKONT) FROM BSAD
    WHERE KUNNR = ITAB-KUNNR
    AND BUKRS = P_BUKRS-LOW
    AND AUGBL = ITAB-AUGBL
    AND BSCHL <> '15'.
    APPEND ITAB3.
  ENDSELECT.
  ENDIF.
ENDLOOP.

DATA : NAME1 LIKE KNA1-NAME1,
       ORT01 LIKE KNA1-ORT01.
SELECT SINGLE NAME1 ORT01 INTO (NAME1,ORT01) FROM KNA1
WHERE KUNNR = S_KUNNR-LOW
AND SPRAS = 'EN'.

ENDLOOP.
LOOP AT ITAB.
IF ITAB-WRBTR_H <> 0 AND ITAB-SHKZG = 'H' AND ITAB-BSCHL = '15'.
DELETE ITAB.
ENDIF.
ENDLOOP.
LOOP AT ITAB3.
IF ITAB3-SHKZG = 'H'.
ITAB3-WRBTR_H = ITAB3-WRBTR_H * 1.
MODIFY ITAB3.
ELSE.
ITAB3-WRBTR_H = ITAB3-WRBTR_H * -1.
MODIFY ITAB3.
ENDIF.
ENDLOOP.
APPEND LINES OF ITAB3 TO ITAB2.
APPEND LINES OF ITAB TO ITAB2.
*LOOP AT ITAB2.
*WRITE :/  ITAB2-KUNNR,ITAB2-WRBTR_S,ITAB2-WRBTR_H,ITAB2-SHKZG,
*ITAB2-BLART,ITAB2-BSCHL,
*ITAB2-AUGBL,
*ITAB2-BELNR.
*AT LAST.
*SUM.
*WRITE : / ITAB2-WRBTR_S,ITAB2-WRBTR_H.
*ENDAT.
*ENDLOOP.
SORT ITAB2 BY AUGBL.
SORT ITAB2 BY BUDAT AUGBL .
 SKIP.
  WRITE : / ITAB2-KUNNR.
  ULINE.
  SKIP.
  SORT FINAL BY  AUGDT .
  SKIP.
  SKIP.
  WRITE : /2 ' XYZ LIMITED.'.
  SKIP.
  WRITE : /2 ' Party Code :' , 16 S_KUNNR-LOW ,
          42 'Party Name: ',   NAME1.
  WRITE :/42 'City :       ', ort01.
  skip.
  write :/2 '  Run Period : ', s_budat-low , ' to  ', s_budat-high.
  SKIP.
  ULINE.
  WRITE : /1 SY-VLINE ,2 'Opening Balance as on Date ',s_BUDAT-LOW , ' '
 , 50  AMT,         145 SY-VLINE.
  ULINE.
  WRITE : /1 SY-VLINE ,2 'Post. Date',
           13 SY-VLINE, 16 'Comm.Inv No' ,
           31 SY-VLINE ,38 'Debit' ,
           49 SY-VLINE ,50 '     Cheque No' CENTERED,
           70 SY-VLINE, 71 'Cheque Date',
           83 SY-VLINE, 84 '    Credit',
           100 SY-VLINE, 101 'Comm.Inv No',
           115 SY-VLINE , 116 'Rec.DocNo',
           127 sy-vline, 128 'Doc No',
           140 sy-vline, 141 'D.T.',
           145 sy-vline..
  ULINE.
  loop at itab2.
  if itab2-wrbtr_h <> 0.
     itab2-budat = itab2-augdt.
     itab2-vbeln = itab2-zuonr.
     modify itab2.
     ELSE.
     ITAB2-AUGBL = SPACE.
     MODIFY ITAB2.
endif.
if itab2-wrbtr_s <> 0.
   itab2-augdt = space.
   itab2-vbeln = space.
   modify itab2.
else.
    itab2-zuonr = space.
    itab2-budat = itab2-augdt.
   modify itab2.
endif.


 SELECT SINGLE ZUONR INTO ITAB2-che_no FROM BSEG
   WHERE BELNR = ITAB2-augbl
   AND GJAHR = ITAB2-GJAHR
   AND BUKRS = P_BUKRS-LOW.
   modify itab2.
  endloop.
  SORT ITAB2 BY AUGBL.
  SORT ITAB2 BY BUDAT AUGBL.
  DELETE ITAB2 WHERE BUDAT < S_BUDAT-LOW AND BUDAT <> '00000000'.
  LOOP AT ITAB2 .
*WHERE BLART <> 'LU' AND BSCHL <> '15'.
    WRITE : /1 SY-VLINE ,2  ITAB2-BUDAT,
    13 SY-VLINE ,14 ITAB2-ZUONR,
     31 SY-VLINE ,32 ITAB2-WRBTR_S ,
     49 SY-VLINE ,50 ITAB2-che_no,
     70 SY-VLINE ,71 ITAB2-AUGDT,
    83 SY-VLINE ,84  ITAB2-WRBTR_H,
    100 SY-VLINE ,101 ITAB2-VBELN,
     115 SY-VLINE,116 ITAB2-AUGBL,
    127 SY-VLINE ,128 itab2-BELNR,140 SY-VLINE ,141 itab2-BLART ,145
SY-VLINE..
 AT LAST.
      ULINE.
      SUM.
      WRITE : /1 SY-VLINE ,2 'TOTAL :',
             31 SY-VLINE ,32 ITAB2-WRBTR_S ,49 SY-VLINE ,
             83 SY-VLINE ,84 ITAB2-WRBTR_H,100 sy-vline,
             145 SY-VLINE .
      ULINE.
    ENDAT.
  ENDLOOP.
  LOOP AT ITAB2 .
    SUM_S = SUM_S + ITAB2-WRBTR_S.
    SUM_H = SUM_H + ITAB2-WRBTR_H.
  ENDLOOP.
  CLO = AMT + SUM_S + SUM_H.

  WRITE : /1 SY-VLINE ,2 'Closing Balance as on Date ',
  s_BUDAT-HIGH , ' :  ' , 84 clo,145 SY-VLINE .
  ULINE.




Serkan AKKAVAK
Bilgisayar Mühendisi
serkurumsal@yandex.com