Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

not able to get all GL related documents number in output..!!! thanks advance

former_member622718
Participant
0 Kudos
*&---------------------------------------------------------------------*
*& Report Z_FI_GLACC
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT z_fi_glacc NO STANDARD PAGE HEADING.


TABLES : bseg,bkpf,skat,lfa1.


TYPES: BEGIN OF ty_final,
         sel,                 " for extra coloun in alv
         prctr  TYPE prctr,   " profit centre
         hkont  TYPE hkont,   " General Ledger Account
         belnr  TYPE belnr_d, " Accounting Document Number
         budat  TYPE budat,   " Posting Date in the Document
         bldat  TYPE bldat,   " Document Date in Document
         augcp  TYPE augcp,   " Clearing Entry Date
         pswsl  TYPE pswsl,   " Amount for Updating in General Ledger
         sgtxt  TYPE sgtxt,   " Item Text
         dzuonr TYPE dzuonr,  " Assignment number
         wrbtr  TYPE wrbtr,   " Amount in document currency
         lifnr  TYPE lifnr,   " Account Number of Vendor or Creditor
         gsber  TYPE gsber,   " Business area
         bupla  TYPE bupla,   " business place
         xblnr  TYPE xblnr,   " Reference Document Number
         bktxt  TYPE bktxt,   " Document Header Text
         cpudt  TYPE cpudt,   " Day On Which Accounting Document Was Entered
         saknr  TYPE saknr,   " gl account
         txt50  TYPE txt50_skat,   " G/L Account Long Text
         name1  TYPE name1,   " Vendor Namae
       END OF  ty_final.


DATA: gt          TYPE TABLE OF ty_final,
      gwa         TYPE ty_final,
      gt_fcat     TYPE slis_t_fieldcat_alv,
      wa_fcat     TYPE slis_fieldcat_alv,
      gs_layout   TYPE slis_layout_alv,
      gs_varient  TYPE disvariant,
      v_prog      TYPE sy-repid,
      v_name      TYPE sy-uname,
      rs_selfield TYPE slis_selfield,
      v_selfield  TYPE slis_selfield-value,
      r_ucomm     TYPE sy-ucomm.


INITIALIZATION.
  v_prog = sy-repid.
  v_name = sy-uname.


  SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
  SELECT-OPTIONS : s_hkont FOR bseg-hkont,         " gl account num
                   s_budat FOR bseg-h_budat,       " posting data
                   s_prctr FOR bseg-prctr.         " profit centre
  SELECTION-SCREEN END OF BLOCK b1.


START-OF-SELECTION.
  PERFORM get_data.


END-OF-SELECTION.
  PERFORM fcat.
  PERFORM alvcall.
  PERFORM usercmd USING r_ucomm CHANGING rs_selfield.


FORM get_data.


  SELECT
          prctr,
          hkont,
          belnr,
          h_budat,
          h_bldat,
          augcp,
          pswsl,
          sgtxt,
          zuonr,
          wrbtr,
          lifnr,
          gsber,
          bupla
          FROM bseg INTO TABLE @DATA(l_bseg)
          WHERE hkont IN @s_hkont
          AND h_budat IN @s_budat
          AND prctr IN @s_prctr.


  CHECK sy-subrc EQ 0.




  SELECT  xblnr,
          bktxt,
          cpudt,
          belnr
          FROM bkpf INTO TABLE @DATA(l_bkpf) FOR ALL ENTRIES IN  @l_bseg
          WHERE belnr EQ @l_bseg-belnr
          AND budat IN @s_budat.




    SELECT saknr,
           txt50
           FROM skat INTO TABLE @DATA(l_skat) FOR ALL ENTRIES IN @l_bseg
           WHERE saknr EQ @l_bseg-hkont.


  ENDIF.


  SELECT lifnr,
         name1 FROM lfa1 INTO TABLE @DATA(l_lfa1) FOR ALL ENTRIES IN @l_bseg
         WHERE lifnr EQ @l_bseg-lifnr.


  SORT: l_bseg BY belnr ,
        l_bkpf BY belnr.


  LOOP AT l_bseg INTO DATA(w_bseg).
    gwa-prctr = w_bseg-prctr.
    gwa-hkont = w_bseg-hkont.
    gwa-belnr = w_bseg-belnr.
    gwa-budat = w_bseg-h_budat.
    gwa-bldat = w_bseg-h_bldat.
    gwa-augcp = w_bseg-augcp.
    gwa-pswsl = w_bseg-pswsl.
    gwa-sgtxt = w_bseg-sgtxt.
    gwa-dzuonr = w_bseg-zuonr.
    gwa-wrbtr = w_bseg-wrbtr.
    gwa-lifnr = w_bseg-lifnr.
    gwa-gsber = w_bseg-gsber.
    gwa-bupla = w_bseg-bupla.


    READ TABLE l_bkpf INTO DATA(w_bkpf) WITH KEY belnr = w_bseg-belnr BINARY SEARCH.


    IF sy-subrc EQ 0.
      gwa-xblnr = w_bkpf-xblnr.
      gwa-bktxt = w_bkpf-bktxt.
      gwa-cpudt = w_bkpf-cpudt.
    ENDIF.


    READ TABLE l_skat INTO DATA(w_skat) WITH KEY saknr = w_bseg-hkont BINARY SEARCH.


    IF sy-subrc EQ 0.
      gwa-saknr = w_skat-saknr.
      gwa-txt50 = w_skat-txt50.
    ENDIF.


    READ TABLE l_lfa1 INTO DATA(w_lfa1) WITH KEY lifnr = w_bseg-lifnr .


    IF sy-subrc EQ 0.
      gwa-name1 = w_lfa1-name1.
    ENDIF.


    APPEND gwa TO gt.
    CLEAR gwa.
  ENDLOOP.


ENDFORM.


FORM fcat.


  gs_layout-zebra = 'X'.
  gs_layout-colwidth_optimize = 'X'.
  gs_layout-box_fieldname  = 'SEL'.
  gs_varient-report = sy-repid.


  wa_fcat-fieldname = 'BELNR'.
  wa_fcat-ref_tabname = 'BSEG'.
  wa_fcat-seltext_m = 'Document Number'.
  wa_fcat-hotspot = 'X'.
  wa_fcat-outputlen = '15'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'SGTXT'.
  wa_fcat-ref_tabname = 'BSEG'.
  wa_fcat-seltext_m = 'Text'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'PRCTR'.
  wa_fcat-ref_tabname = 'BSEG'.
  wa_fcat-seltext_m = 'Profit Centre'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'BUDAT'.
  wa_fcat-ref_tabname = 'BSEG'.
  wa_fcat-seltext_m = 'Posting Date'.
  wa_fcat-datatype    = 'DATS'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'AUGCP'.
  wa_fcat-ref_tabname = 'BSEG'.
  wa_fcat-seltext_m = 'Clearing Entry Date'.
  wa_fcat-datatype    = 'DATS'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'BLDAT'.
  wa_fcat-ref_tabname = 'BSEG'.
  wa_fcat-seltext_m = 'Document Date'.
  wa_fcat-datatype    = 'DATS'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'CPUDT'.
  wa_fcat-ref_tabname = 'BKPF'.
  wa_fcat-seltext_m = 'Entry Date'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'WRBTR'.
  wa_fcat-ref_tabname = 'BSEG'.
  wa_fcat-seltext_m = 'Amount in Document Currency'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'PSWSL'.
  wa_fcat-ref_tabname = 'BSEG'.
  wa_fcat-seltext_m = 'Currency'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'LIFNR'.
  wa_fcat-ref_tabname = 'LFA1'.
  wa_fcat-seltext_m = 'Vendor Number'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'NAME1'.
  wa_fcat-ref_tabname = 'LFA1'.
  wa_fcat-seltext_m = 'Vendor Name'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'XBLNR'.
  wa_fcat-ref_tabname = 'BKPF'.
  wa_fcat-seltext_m = 'Reference Document Number'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'DZUONR'.
  wa_fcat-ref_tabname = 'BSEG'.
  wa_fcat-seltext_m = 'Assignmeant'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'BKTXT'.
  wa_fcat-ref_tabname = 'BKPF'.
  wa_fcat-seltext_m = 'Header Text'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'SAKNR'.
  wa_fcat-ref_tabname = 'SKA1'.
  wa_fcat-seltext_m = 'G/L Account'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'TXT50'.
  wa_fcat-ref_tabname = 'SKAT'.
  wa_fcat-seltext_m = 'G/L Discription'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.


  wa_fcat-fieldname = 'GSBER'.
  wa_fcat-ref_tabname = 'BSEG'.
  wa_fcat-seltext_m = 'Business Area'.
  wa_fcat-outputlen = '15'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.




  wa_fcat-fieldname = 'BUPLA'.
  wa_fcat-ref_tabname = 'BSEG'.
  wa_fcat-seltext_m = 'Business Place'.
  wa_fcat-outputlen = '15'.
  APPEND wa_fcat TO gt_fcat.
  CLEAR wa_fcat.




ENDFORM.




FORM alvcall.


  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = v_prog
      i_callback_user_command = 'USERCMD'
      i_grid_title            = 'G/L ACCOUNTS DETAILS'
      is_layout               = gs_layout
      it_fieldcat             = gt_fcat
      is_variant              = gs_varient
    TABLES
      t_outtab                = gt.


ENDFORM.




FORM  usercmd  USING r_ucoma TYPE sy-ucomm CHANGING rs_selfield TYPE slis_selfield.
  CASE r_ucoma.
    WHEN '&IC1'.
      CASE rs_selfield-fieldname.
        WHEN 'BELNR'.
          SET PARAMETER ID 'BLN'
          FIELD rs_selfield-value.
          CALL TRANSACTION 'FB03'.
          " AND SKIP FIRST SCREEN.
      ENDCASE.
  ENDCASE.
ENDFORM.
1 ACCEPTED SOLUTION

stekoester
Explorer

Hi manoj16393,

there are already good tips in the previous comments, but there is a lot to be fixed.

I made some changes in your and commented important points. I haven't fixed functional logic of getting the data, because this should be taken from FI Consultant.

Your requirement is a good sample, why it is needed, that a consultant describes the requirement. The consultant should provide the all needed tables for this. The developer has to take care of using key fields that connect the tables the right way.

REPORT zmanoj1.
TABLES : bseg.
TYPES:
  " STKOES: Output structures should be created in DDIC to prevent usage of unnecessary code like your fcat creation
  " STKOES: And second and much more important reason is: reusability
  BEGIN OF ty_final,
    sel,                    " for extra coloun in alv
    prctr   TYPE prctr,      " profit centre
    hkont   TYPE hkont,      " General Ledger Account
    belnr   TYPE belnr_d,    " Accounting Document Number
    h_budat TYPE budat,      " Posting Date in the Document           STKOES: Use the same variable name like in bseg
    h_bldat TYPE bldat,      " Document Date in Document              STKOES: Use the same variable name like in bseg
    augcp   TYPE augcp,      " Clearing Entry Date
    pswsl   TYPE pswsl,      " Amount for Updating in General Ledger
    sgtxt   TYPE sgtxt,      " Item Text
    zuonr   TYPE dzuonr,     " Assignment number                      STKOES: Use the same variable name like in bseg
    wrbtr   TYPE wrbtr,      " Amount in document currency
    lifnr   TYPE lifnr,      " Account Number of Vendor or Creditor
    gsber   TYPE gsber,      " Business area
    bupla   TYPE bupla,      " business place
    xblnr   TYPE xblnr,      " Reference Document Number
    bktxt   TYPE bktxt,      " Document Header Text
    cpudt   TYPE cpudt,      " Day On Which Accounting Document Was Entered
    txt50   TYPE txt50_skat, " G/L Account Long Text
    name1   TYPE name1,      " Vendor Namae
    gjahr   TYPE gjahr,      " Fiscal Year
    bukrs   TYPE bukrs,      " Company Code
  END OF  ty_final.

DATA:
  gt         TYPE TABLE OF ty_final,
*  gwa         TYPE ty_final,             " STKOES: There is no need of global structures " Not needed when using inline declaration
  gt_fcat    TYPE slis_t_fieldcat_alv,
*  wa_fcat     TYPE slis_fieldcat_alv,    " STKOES: There is no need of global structures " Not needed when using inline declaration
  gs_layout  TYPE slis_layout_alv,
  gs_variant TYPE disvariant,
  v_prog     TYPE sy-repid.
*  v_name      TYPE sy-uname,             " STKOES: If you don't use, then don't declare it
*  rs_selfield TYPE slis_selfield,        " STKOES: If you don't use, then don't declare it
*  v_selfield  TYPE slis_selfield-value,  " STKOES: If you don't use, then don't declare it
*  r_ucomm     TYPE sy-ucomm.             " STKOES: If you don't use, then don't declare it

INITIALIZATION.
  v_prog = sy-repid.
*  v_name = sy-uname.

  SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
    SELECT-OPTIONS:
      " STKOES: Better to use key fields of database table, this is faster. So I have added three more select-options
      so_belnr  FOR bseg-belnr,         " Accounting Document Number
      so_bukrs  FOR bseg-bukrs,         " Company Code
      so_gjahr  FOR bseg-gjahr,         " Fiscal Year

      s_hkont   FOR bseg-hkont,         " gl account num
      s_budat   FOR bseg-h_budat,       " posting data
      s_prctr   FOR bseg-prctr.         " profit centre
  SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.
  PERFORM get_data.

END-OF-SELECTION.
  PERFORM fcat.
  PERFORM alvcall.
*  PERFORM usercmd USING r_ucomm CHANGING rs_selfield.    " STKOES: This is called from ALV screen only

FORM get_data.
  IF s_hkont IS NOT INITIAL OR s_prctr IS NOT INITIAL OR s_budat IS NOT INITIAL.
    " STKOES: Because of the fields on the selction screen it is needed to get all matching document numbers
    SELECT bukrs, belnr, gjahr  FROM bseg INTO TABLE @DATA(l_bseg_tmp)
                                WHERE bukrs   IN @so_bukrs
                                  AND belnr   IN @so_belnr
                                  AND gjahr   IN @so_gjahr
                                  AND hkont   IN @s_hkont
                                  AND prctr   IN @s_prctr
                                  AND h_budat IN @s_budat
                                ORDER BY PRIMARY KEY.
    IF sy-subrc EQ 0.
      DELETE ADJACENT DUPLICATES FROM l_bseg_tmp.   " STKOES: Keep every combination of BUKRS, BELNR and GJAHR only once in temp table
      SELECT  bukrs, belnr, gjahr, buzei, augcp, gsber, wrbtr, pswsl, zuonr, sgtxt, hkont, lifnr, prctr, bupla,
              h_budat, h_bldat  FROM bseg INTO TABLE @DATA(l_bseg)
                                FOR ALL ENTRIES IN @l_bseg_tmp
                                WHERE bukrs   EQ @l_bseg_tmp-bukrs
                                  AND belnr   EQ @l_bseg_tmp-belnr
                                  AND gjahr   EQ @l_bseg_tmp-gjahr
                                ORDER BY PRIMARY KEY.
      CLEAR: l_bseg_tmp. " STKOES: Data not needed anymore
    ENDIF.
  ELSE.
    SELECT  bukrs, belnr, gjahr, buzei, augcp, gsber, wrbtr, pswsl, zuonr, sgtxt, hkont, lifnr, prctr, bupla,
            h_budat AS budat, h_bldat AS bldat  FROM bseg INTO TABLE @l_bseg
                                                WHERE bukrs   IN @so_bukrs
                                                  AND belnr   IN @so_belnr
                                                  AND gjahr   IN @so_gjahr
                                                ORDER BY PRIMARY KEY.
  ENDIF.
  IF sy-subrc EQ 0.
    " STKOES: Here the usage of all key fields is required. We selected them from bseg
    SELECT bukrs, belnr, gjahr, cpudt, xblnr, bktxt FROM bkpf INTO TABLE @DATA(l_bkpf)
                                                    FOR ALL ENTRIES IN  @l_bseg
                                                    WHERE bukrs EQ @l_bseg-bukrs
                                                      AND belnr EQ @l_bseg-belnr
                                                      AND gjahr EQ @l_bseg-gjahr
*                                                      AND budat EQ @l_bseg-h_budat
                                                    ORDER BY PRIMARY KEY.

    " STKOES: Database table contains language key, so we have to use it
    " STKOES: Chart of Accounts is key field too, so we have to use. Chart of Accounts is assigned to company code
    SELECT t~bukrs, s~saknr, s~txt50  FROM skat AS s
                                      INNER JOIN t001 AS t
                                        ON t~ktopl EQ s~ktopl
                                      INTO TABLE @DATA(l_skat)
                                      FOR ALL ENTRIES IN @l_bseg
                                      WHERE s~spras EQ @sy-langu
                                        AND s~saknr EQ @l_bseg-hkont
                                        AND t~bukrs EQ @l_bseg-bukrs.

    SELECT lifnr, name1 FROM lfa1 INTO TABLE @DATA(l_lfa1)
                        FOR ALL ENTRIES IN @l_bseg
                        WHERE lifnr EQ @l_bseg-lifnr.

    LOOP  AT l_bkpf REFERENCE INTO DATA(lr_bkpf).  " STKOES: Usage of reference reduces memory usage
      LOOP AT l_bseg INTO DATA(w_bseg)
          WHERE bukrs EQ lr_bkpf->bukrs
            AND belnr EQ lr_bkpf->belnr
            AND gjahr EQ lr_bkpf->gjahr.

        APPEND VALUE #( BASE CORRESPONDING #( w_bseg ) xblnr = lr_bkpf->xblnr
                                                       bktxt = lr_bkpf->bktxt
                                                       cpudt = lr_bkpf->cpudt
                                                       txt50 = VALUE #( l_skat[ bukrs = lr_bkpf->bukrs saknr = w_bseg-hkont ]-txt50 OPTIONAL )
                                                       name1 = VALUE #( l_lfa1[ lifnr = w_bseg-lifnr ]-name1 OPTIONAL ) ) TO gt.
      ENDLOOP.
    ENDLOOP.
    CLEAR: l_bseg, l_bkpf, l_skat, l_lfa1. " STKOES: Data can be cleared
  ELSE.
    " STKOES: To prevent displaying empty ALV list-processing should be left
    MESSAGE 'No data selected' TYPE 'S' DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.
ENDFORM.

FORM fcat.
  gs_layout = VALUE #( zebra = 'X' colwidth_optimize = 'X' box_fieldname  = 'SEL' ).
  gs_variant-report = sy-repid.

  " STKOES: Instead of creating fieldcatalog like this, it's better to create a structure in DDIC and use this.
  APPEND LINES OF VALUE slis_t_fieldcat_alv(<br>    ( fieldname = 'BELNR'    ref_tabname = 'BSEG'  seltext_m = 'Document Number'     outputlen = '15' hotspot = 'X' )
    ( fieldname = 'GJAHR'    ref_tabname = 'BSEG'  seltext_m = 'Fiscal Year'         outputlen = '4' )
    ( fieldname = 'BUKRS'    ref_tabname = 'BSEG'  seltext_m = 'Company Code'        outputlen = '4' )
    ( fieldname = 'SGTXT'    ref_tabname = 'BSEG'  seltext_m = 'Text' )
    ( fieldname = 'PRCTR'    ref_tabname = 'BSEG'  seltext_m = 'Profit Centre' )
    ( fieldname = 'H_BUDAT'  ref_tabname = 'BSEG'  seltext_m = 'Posting Date'        datatype = 'DATS' )
    ( fieldname = 'AUGCP'    ref_tabname = 'BSEG'  seltext_m = 'Clearing Entry Date' datatype = 'DATS' )
    ( fieldname = 'H_BLDAT'  ref_tabname = 'BSEG'  seltext_m = 'Document Date'       datatype = 'DATS' )
    ( fieldname = 'CPUDT'    ref_tabname = 'BKPF'  seltext_m = 'Entry Date' )
    ( fieldname = 'WRBTR'    ref_tabname = 'BSEG'  seltext_m = 'Amount in Document Currency' )
    ( fieldname = 'PSWSL'    ref_tabname = 'BSEG'  seltext_m = 'Currency' )
    ( fieldname = 'LIFNR'    ref_tabname = 'LFA1'  seltext_m = 'Vendor Number' )
    ( fieldname = 'NAME1'    ref_tabname = 'LFA1'  seltext_m = 'Vendor Name' )
    ( fieldname = 'WRBTR'    ref_tabname = 'BSEG'  seltext_m = 'Amount in Document Currency' cfieldname = 'PSWSL' ctabname = 'GT' )
    ( fieldname = 'ZUONR'    ref_tabname = 'BSEG'  seltext_m = 'Assignmeant' )
    ( fieldname = 'BKTXT'    ref_tabname = 'BKPF'  seltext_m = 'Header Text' )
    ( fieldname = 'HKONT'    ref_tabname = 'BSEG'  seltext_m = 'G/L Account' )
    ( fieldname = 'TXT50'    ref_tabname = 'SKAT'  seltext_m = 'G/L Discription' )
    ( fieldname = 'GSBER'    ref_tabname = 'BSEG'  seltext_m = 'Business Area'       outputlen = '15' )
    ( fieldname = 'BUPLA'    ref_tabname = 'BSEG'  seltext_m = 'Business Place'      outputlen = '15' ) ) TO gt_fcat.
ENDFORM.

FORM alvcall.
  " STKOES: You should stop using this function module. Use cl_gui_custom_container and cl_gui_alv_grid instead
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = v_prog
      i_callback_user_command = 'USERCMD'
      i_grid_title            = 'G/L ACCOUNTS DETAILS'
      is_layout               = gs_layout
      it_fieldcat             = gt_fcat
      is_variant              = gs_variant
    TABLES
      t_outtab                = gt.
ENDFORM.

FORM usercmd  USING r_ucoma TYPE sy-ucomm CHANGING rs_selfield TYPE slis_selfield.
  CASE r_ucoma.
    WHEN '&IC1'.
      CASE rs_selfield-fieldname.
        WHEN 'BELNR'.
          " STKOES: Get the selected line of output data
          DATA(ls_output_line) = VALUE #( gt[ rs_selfield-tabindex ] OPTIONAL ).
          " STKOES: We have to set every paramter id for the required fields on the entry screen of FB03
          SET PARAMETER ID 'BLN' FIELD ls_output_line-belnr.
          SET PARAMETER ID 'BUK' FIELD ls_output_line-bukrs.
          SET PARAMETER ID 'GJR' FIELD ls_output_line-gjahr.
          CALL TRANSACTION 'FB03' AND SKIP FIRST SCREEN.    " STKOES: Because every paramter id is set we can skip first screen
      ENDCASE.
  ENDCASE.
ENDFORM.

Greetings
Stephan

11 REPLIES 11

DominikTylczyn
Active Contributor

Hello manoj16393

Your l_bkpf and l_skat tables are not sorted and you don't sortem explicitly. Still you read them with BINARY SEARCH. That doesn't look right.

BR

Dominik Tylczynski

0 Kudos

Hi sir,

Out manager said there logic missing itseems.. I should get all GL account related to document account numbers... But I getting only one document number..

Please tell us logic I am stil learning

I would help you if I knew what you are trying to do

hi sir,

what the problem is, i am getting data of gl account ( that is 71200550) only one record but i want ( 71200550, 21400130, 25100050) plz guide me in getting these number also..

thank you

Hello Manoj Kumar

What are the selection criteria that you used to get these results?

0 Kudos

GL account , posting date, profit center..

Please help me

DominikTylczyn
Active Contributor
0 Kudos

Hello Manoj Kumar

I really don't know what your business requirements are. However assuming that you've fed 71200550 account number to your report than it has to display only one entry of document 10000042. Look, your SELECT statement from BSEG table gets only one record from the table for document 10000042, as there is only one item of document 10000042 posted to account 71200550. That's why you see only one line in ALV. You would have to perform one more select from BSEG to get all items of document 10000042.

Another approach could be to select FI documents headers from BKPF based on posting date and then select all items of those documents from BSEG and then filter out the documents without any item posted against an account 71200550

BR

Dominik Tylczynski

VenkatRamesh_V
Active Contributor
0 Kudos

Hi,

Add all key fields in the internal table l_bseg.

Regards,

Ramesh

stekoester
Explorer

Hi manoj16393,

there are already good tips in the previous comments, but there is a lot to be fixed.

I made some changes in your and commented important points. I haven't fixed functional logic of getting the data, because this should be taken from FI Consultant.

Your requirement is a good sample, why it is needed, that a consultant describes the requirement. The consultant should provide the all needed tables for this. The developer has to take care of using key fields that connect the tables the right way.

REPORT zmanoj1.
TABLES : bseg.
TYPES:
  " STKOES: Output structures should be created in DDIC to prevent usage of unnecessary code like your fcat creation
  " STKOES: And second and much more important reason is: reusability
  BEGIN OF ty_final,
    sel,                    " for extra coloun in alv
    prctr   TYPE prctr,      " profit centre
    hkont   TYPE hkont,      " General Ledger Account
    belnr   TYPE belnr_d,    " Accounting Document Number
    h_budat TYPE budat,      " Posting Date in the Document           STKOES: Use the same variable name like in bseg
    h_bldat TYPE bldat,      " Document Date in Document              STKOES: Use the same variable name like in bseg
    augcp   TYPE augcp,      " Clearing Entry Date
    pswsl   TYPE pswsl,      " Amount for Updating in General Ledger
    sgtxt   TYPE sgtxt,      " Item Text
    zuonr   TYPE dzuonr,     " Assignment number                      STKOES: Use the same variable name like in bseg
    wrbtr   TYPE wrbtr,      " Amount in document currency
    lifnr   TYPE lifnr,      " Account Number of Vendor or Creditor
    gsber   TYPE gsber,      " Business area
    bupla   TYPE bupla,      " business place
    xblnr   TYPE xblnr,      " Reference Document Number
    bktxt   TYPE bktxt,      " Document Header Text
    cpudt   TYPE cpudt,      " Day On Which Accounting Document Was Entered
    txt50   TYPE txt50_skat, " G/L Account Long Text
    name1   TYPE name1,      " Vendor Namae
    gjahr   TYPE gjahr,      " Fiscal Year
    bukrs   TYPE bukrs,      " Company Code
  END OF  ty_final.

DATA:
  gt         TYPE TABLE OF ty_final,
*  gwa         TYPE ty_final,             " STKOES: There is no need of global structures " Not needed when using inline declaration
  gt_fcat    TYPE slis_t_fieldcat_alv,
*  wa_fcat     TYPE slis_fieldcat_alv,    " STKOES: There is no need of global structures " Not needed when using inline declaration
  gs_layout  TYPE slis_layout_alv,
  gs_variant TYPE disvariant,
  v_prog     TYPE sy-repid.
*  v_name      TYPE sy-uname,             " STKOES: If you don't use, then don't declare it
*  rs_selfield TYPE slis_selfield,        " STKOES: If you don't use, then don't declare it
*  v_selfield  TYPE slis_selfield-value,  " STKOES: If you don't use, then don't declare it
*  r_ucomm     TYPE sy-ucomm.             " STKOES: If you don't use, then don't declare it

INITIALIZATION.
  v_prog = sy-repid.
*  v_name = sy-uname.

  SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
    SELECT-OPTIONS:
      " STKOES: Better to use key fields of database table, this is faster. So I have added three more select-options
      so_belnr  FOR bseg-belnr,         " Accounting Document Number
      so_bukrs  FOR bseg-bukrs,         " Company Code
      so_gjahr  FOR bseg-gjahr,         " Fiscal Year

      s_hkont   FOR bseg-hkont,         " gl account num
      s_budat   FOR bseg-h_budat,       " posting data
      s_prctr   FOR bseg-prctr.         " profit centre
  SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.
  PERFORM get_data.

END-OF-SELECTION.
  PERFORM fcat.
  PERFORM alvcall.
*  PERFORM usercmd USING r_ucomm CHANGING rs_selfield.    " STKOES: This is called from ALV screen only

FORM get_data.
  IF s_hkont IS NOT INITIAL OR s_prctr IS NOT INITIAL OR s_budat IS NOT INITIAL.
    " STKOES: Because of the fields on the selction screen it is needed to get all matching document numbers
    SELECT bukrs, belnr, gjahr  FROM bseg INTO TABLE @DATA(l_bseg_tmp)
                                WHERE bukrs   IN @so_bukrs
                                  AND belnr   IN @so_belnr
                                  AND gjahr   IN @so_gjahr
                                  AND hkont   IN @s_hkont
                                  AND prctr   IN @s_prctr
                                  AND h_budat IN @s_budat
                                ORDER BY PRIMARY KEY.
    IF sy-subrc EQ 0.
      DELETE ADJACENT DUPLICATES FROM l_bseg_tmp.   " STKOES: Keep every combination of BUKRS, BELNR and GJAHR only once in temp table
      SELECT  bukrs, belnr, gjahr, buzei, augcp, gsber, wrbtr, pswsl, zuonr, sgtxt, hkont, lifnr, prctr, bupla,
              h_budat, h_bldat  FROM bseg INTO TABLE @DATA(l_bseg)
                                FOR ALL ENTRIES IN @l_bseg_tmp
                                WHERE bukrs   EQ @l_bseg_tmp-bukrs
                                  AND belnr   EQ @l_bseg_tmp-belnr
                                  AND gjahr   EQ @l_bseg_tmp-gjahr
                                ORDER BY PRIMARY KEY.
      CLEAR: l_bseg_tmp. " STKOES: Data not needed anymore
    ENDIF.
  ELSE.
    SELECT  bukrs, belnr, gjahr, buzei, augcp, gsber, wrbtr, pswsl, zuonr, sgtxt, hkont, lifnr, prctr, bupla,
            h_budat AS budat, h_bldat AS bldat  FROM bseg INTO TABLE @l_bseg
                                                WHERE bukrs   IN @so_bukrs
                                                  AND belnr   IN @so_belnr
                                                  AND gjahr   IN @so_gjahr
                                                ORDER BY PRIMARY KEY.
  ENDIF.
  IF sy-subrc EQ 0.
    " STKOES: Here the usage of all key fields is required. We selected them from bseg
    SELECT bukrs, belnr, gjahr, cpudt, xblnr, bktxt FROM bkpf INTO TABLE @DATA(l_bkpf)
                                                    FOR ALL ENTRIES IN  @l_bseg
                                                    WHERE bukrs EQ @l_bseg-bukrs
                                                      AND belnr EQ @l_bseg-belnr
                                                      AND gjahr EQ @l_bseg-gjahr
*                                                      AND budat EQ @l_bseg-h_budat
                                                    ORDER BY PRIMARY KEY.

    " STKOES: Database table contains language key, so we have to use it
    " STKOES: Chart of Accounts is key field too, so we have to use. Chart of Accounts is assigned to company code
    SELECT t~bukrs, s~saknr, s~txt50  FROM skat AS s
                                      INNER JOIN t001 AS t
                                        ON t~ktopl EQ s~ktopl
                                      INTO TABLE @DATA(l_skat)
                                      FOR ALL ENTRIES IN @l_bseg
                                      WHERE s~spras EQ @sy-langu
                                        AND s~saknr EQ @l_bseg-hkont
                                        AND t~bukrs EQ @l_bseg-bukrs.

    SELECT lifnr, name1 FROM lfa1 INTO TABLE @DATA(l_lfa1)
                        FOR ALL ENTRIES IN @l_bseg
                        WHERE lifnr EQ @l_bseg-lifnr.

    LOOP  AT l_bkpf REFERENCE INTO DATA(lr_bkpf).  " STKOES: Usage of reference reduces memory usage
      LOOP AT l_bseg INTO DATA(w_bseg)
          WHERE bukrs EQ lr_bkpf->bukrs
            AND belnr EQ lr_bkpf->belnr
            AND gjahr EQ lr_bkpf->gjahr.

        APPEND VALUE #( BASE CORRESPONDING #( w_bseg ) xblnr = lr_bkpf->xblnr
                                                       bktxt = lr_bkpf->bktxt
                                                       cpudt = lr_bkpf->cpudt
                                                       txt50 = VALUE #( l_skat[ bukrs = lr_bkpf->bukrs saknr = w_bseg-hkont ]-txt50 OPTIONAL )
                                                       name1 = VALUE #( l_lfa1[ lifnr = w_bseg-lifnr ]-name1 OPTIONAL ) ) TO gt.
      ENDLOOP.
    ENDLOOP.
    CLEAR: l_bseg, l_bkpf, l_skat, l_lfa1. " STKOES: Data can be cleared
  ELSE.
    " STKOES: To prevent displaying empty ALV list-processing should be left
    MESSAGE 'No data selected' TYPE 'S' DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.
ENDFORM.

FORM fcat.
  gs_layout = VALUE #( zebra = 'X' colwidth_optimize = 'X' box_fieldname  = 'SEL' ).
  gs_variant-report = sy-repid.

  " STKOES: Instead of creating fieldcatalog like this, it's better to create a structure in DDIC and use this.
  APPEND LINES OF VALUE slis_t_fieldcat_alv(<br>    ( fieldname = 'BELNR'    ref_tabname = 'BSEG'  seltext_m = 'Document Number'     outputlen = '15' hotspot = 'X' )
    ( fieldname = 'GJAHR'    ref_tabname = 'BSEG'  seltext_m = 'Fiscal Year'         outputlen = '4' )
    ( fieldname = 'BUKRS'    ref_tabname = 'BSEG'  seltext_m = 'Company Code'        outputlen = '4' )
    ( fieldname = 'SGTXT'    ref_tabname = 'BSEG'  seltext_m = 'Text' )
    ( fieldname = 'PRCTR'    ref_tabname = 'BSEG'  seltext_m = 'Profit Centre' )
    ( fieldname = 'H_BUDAT'  ref_tabname = 'BSEG'  seltext_m = 'Posting Date'        datatype = 'DATS' )
    ( fieldname = 'AUGCP'    ref_tabname = 'BSEG'  seltext_m = 'Clearing Entry Date' datatype = 'DATS' )
    ( fieldname = 'H_BLDAT'  ref_tabname = 'BSEG'  seltext_m = 'Document Date'       datatype = 'DATS' )
    ( fieldname = 'CPUDT'    ref_tabname = 'BKPF'  seltext_m = 'Entry Date' )
    ( fieldname = 'WRBTR'    ref_tabname = 'BSEG'  seltext_m = 'Amount in Document Currency' )
    ( fieldname = 'PSWSL'    ref_tabname = 'BSEG'  seltext_m = 'Currency' )
    ( fieldname = 'LIFNR'    ref_tabname = 'LFA1'  seltext_m = 'Vendor Number' )
    ( fieldname = 'NAME1'    ref_tabname = 'LFA1'  seltext_m = 'Vendor Name' )
    ( fieldname = 'WRBTR'    ref_tabname = 'BSEG'  seltext_m = 'Amount in Document Currency' cfieldname = 'PSWSL' ctabname = 'GT' )
    ( fieldname = 'ZUONR'    ref_tabname = 'BSEG'  seltext_m = 'Assignmeant' )
    ( fieldname = 'BKTXT'    ref_tabname = 'BKPF'  seltext_m = 'Header Text' )
    ( fieldname = 'HKONT'    ref_tabname = 'BSEG'  seltext_m = 'G/L Account' )
    ( fieldname = 'TXT50'    ref_tabname = 'SKAT'  seltext_m = 'G/L Discription' )
    ( fieldname = 'GSBER'    ref_tabname = 'BSEG'  seltext_m = 'Business Area'       outputlen = '15' )
    ( fieldname = 'BUPLA'    ref_tabname = 'BSEG'  seltext_m = 'Business Place'      outputlen = '15' ) ) TO gt_fcat.
ENDFORM.

FORM alvcall.
  " STKOES: You should stop using this function module. Use cl_gui_custom_container and cl_gui_alv_grid instead
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = v_prog
      i_callback_user_command = 'USERCMD'
      i_grid_title            = 'G/L ACCOUNTS DETAILS'
      is_layout               = gs_layout
      it_fieldcat             = gt_fcat
      is_variant              = gs_variant
    TABLES
      t_outtab                = gt.
ENDFORM.

FORM usercmd  USING r_ucoma TYPE sy-ucomm CHANGING rs_selfield TYPE slis_selfield.
  CASE r_ucoma.
    WHEN '&IC1'.
      CASE rs_selfield-fieldname.
        WHEN 'BELNR'.
          " STKOES: Get the selected line of output data
          DATA(ls_output_line) = VALUE #( gt[ rs_selfield-tabindex ] OPTIONAL ).
          " STKOES: We have to set every paramter id for the required fields on the entry screen of FB03
          SET PARAMETER ID 'BLN' FIELD ls_output_line-belnr.
          SET PARAMETER ID 'BUK' FIELD ls_output_line-bukrs.
          SET PARAMETER ID 'GJR' FIELD ls_output_line-gjahr.
          CALL TRANSACTION 'FB03' AND SKIP FIRST SCREEN.    " STKOES: Because every paramter id is set we can skip first screen
      ENDCASE.
  ENDCASE.
ENDFORM.

Greetings
Stephan

0 Kudos

Thank you sir...!!!

0 Kudos

Great thanks so much