Generic Extraction via Function Module

How does the extraction API work in case you are extracting data using a self defined function module with generic delta?

Sometimes, if your application or requirement makes it necessary, you have to create your own function module to extract the data from whatever sap system to bw.
Therefore you normally make a copy of the standard fm 'RSAX_BIW_GET_DATA_SIMPLE'. To make this new function work for you, you have to realize the following.

1) The FM gets called by the extraction API for at least 2 times. First time is for initialization and from the 2nd time on it is for data extraction.
2) Right after the last data package (somehow you need to make sure that it is the last one) you have to raise the exception no_more_data.

Example with lots of comments


FUNCTION RSAX_BIW_GET_DATA_SIMPLE.
*"--------------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" VALUE(I_REQUNR) TYPE SRSC_S_IF_SIMPLE-REQUNR
*" VALUE(I_DSOURCE) TYPE SRSC_S_IF_SIMPLE-DSOURCE OPTIONAL
*" VALUE(I_MAXSIZE) TYPE SRSC_S_IF_SIMPLE-MAXSIZE OPTIONAL
*" VALUE(I_INITFLAG) TYPE SRSC_S_IF_SIMPLE-INITFLAG OPTIONAL
*" VALUE(I_READ_ONLY) TYPE SRSC_S_IF_SIMPLE-READONLY OPTIONAL
*" TABLES
*" I_T_SELECT TYPE SRSC_S_IF_SIMPLE-T_SELECT OPTIONAL
*" I_T_FIELDS TYPE SRSC_S_IF_SIMPLE-T_FIELDS OPTIONAL
*" E_T_DATA STRUCTURE SFLIGHT OPTIONAL
*" EXCEPTIONS
*" NO_MORE_DATA
*" ERROR_PASSED_TO_MESS_HANDLER
*"--------------------------------------------------------------------------

* Auxiliary Selection criteria structure
DATA: L_S_SELECT TYPE SRSC_S_SELECT.

* Maximum number of lines for DB table
STATICS: S_S_IF TYPE SRSC_S_IF_SIMPLE,

* counter
S_COUNTER_DATAPAKID LIKE SY-TABIX,

* cursor
S_CURSOR TYPE CURSOR.

* Initialization mode (first call by SAPI) or data transfer mode
IF I_INITFLAG = SBIWA_C_FLAG_ON.

* the coding here will be processed the first time the fm gets called.
* it is used to populate static or global variables and to check whether
* it's called by the right datasource or not.
ELSE. "Initialization mode or data extraction ?
* this part will be executed from the 2nd call on
* First data package -> OPEN CURSOR
IF S_COUNTER_DATAPAKID = 0.

* in case it is for the first data package, range tabs gets filled, a cursor will
* be opened or a initial dataset will be read from database into a global internal table
* this has to be done here, with the first data package, to avoid getting the same data with
* each call of the fm.
* additionally you can check whether you get select-options for your 'generic delta'-field or not,
* and, if it is provided, you are able to set a flag in order to do different selections
* based on this flag for delta or full load.
ENDIF. "First data package ?
* from now on records gets fetched from the database or gets read from the internal table
* and the return table e_t_data gets populated with the number of rows given by parameter s_s_if-maxsize.
* if the last record is populated to table e_t_data you need to raise the exception no_more_data
* and you need to close the cursor, if you opened one.
* with each data package you also have to increase s_counter_datapakid.
ENDIF. "Initialization mode or data extraction ?

ENDFUNCTION.



1. When do we go for Generic Extraction?
When the standard extractors are not supporting the extraction what you need.If you create a custom object say by combining certain base tables in R3 say custom tables ZTAB1 and ZTAB2. These two tables are not SAP provided tables and there will not be any standard extractors. So cases like this you will have to go for generic extractors.
2. I am trying to Delta enable the extractor but am not able to set it. or How to set Delta in a Generic Extractor?
You would choose a field which captures the time when the record was changed/inserted. That is what is referred to as time-stamp field.
3. What are the different types of Generic Extractor?
Generic extractors are of 3 types:
1. Based on table/view
2. Based on Infoset Query
3. Based on Function module
The first 2 are straight forward extraction where you would either use a standard SAP or custom table/view or Infoset query to extract data.
4. Why do you go for function module in generic extraction?
If you have to get data from more than 1 table and also incorporate some logic then you would use a fn module extractor. It is like a
program where you would code using ABAP and have all your logic incorporated.
5. How to create a Generic Extractor?
a. Go to T.code RSO2,
b. Select whether you want to create Master data Attributes / Texts/Transaction data.
c. Give Datasource name ex: 'ZGEN_DS'
d. Then, choose whether you want to extract data directly from a Table/View/Function Module.
For Table, it is enough to mention directly the table name in the respected place.
For View, You have to create the view (database view, projection view...) using t.code se11. Then use that view name in the respected place of rso2.
For Function Module you have to define the function module.
e. Then choose Generate. It will give all the fields of the datasource created. there you can set further settings like ( inversion, hide...).
6. I have created a view for a set of fields for Generic Transactional data Extraction,For the generated extract structure, I have appended another set of fields now how to populate data for these fields.
7. how u will do setup in generic extraction?
you need not require to do a set up in generic extraction.It's only required in case of LIS extractors.you can directly do a full load if the data source is not delta enabled or an init with data transfer if it a delta enabled generic extractor.There is no "set up process" for any extractor other than SAP delivered LO extractors.

 Sample Code snippet:

FUNCTION ZRSAX_BIW_GET_DATA_PA.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(I_REQUNR) TYPE  SRSC_S_IF_SIMPLE-REQUNR
*"     VALUE(I_DSOURCE) TYPE  SRSC_S_IF_SIMPLE-DSOURCE OPTIONAL
*"     VALUE(I_MAXSIZE) TYPE  SRSC_S_IF_SIMPLE-MAXSIZE OPTIONAL
*"     VALUE(I_INITFLAG) TYPE  SRSC_S_IF_SIMPLE-INITFLAG OPTIONAL
*"     VALUE(I_READ_ONLY) TYPE  SRSC_S_IF_SIMPLE-READONLY OPTIONAL
*"  TABLES
*"      I_T_SELECT TYPE  SRSC_S_IF_SIMPLE-T_SELECT OPTIONAL
*"      I_T_FIELDS TYPE  SRSC_S_IF_SIMPLE-T_FIELDS OPTIONAL
*"      E_T_DATA STRUCTURE  ZFICOPA_RECO OPTIONAL
*"----------------------------------------------------------------------
*Tables
*  TABLES: zficopa_reco.

*Types
  TYPES:      BEGIN OF ty_likp,
                wadat LIKE likp-wadat,
                vbeln LIKE likp-vbeln,
                vstel like likp-vstel,
                  END OF ty_likp,
              BEGIN OF ty_vbup,
                 vbeln LIKE vbup-vbeln,
                 posnr LIKE vbup-posnr,
                 wbsta LIKE vbup-wbsta,

              END OF ty_vbup,
              BEGIN OF ty_lips,
                vbeln LIKE bkpf-xblnr,          "lips-vbeln,
                vbeln1 LIKE lips-vbeln,
                posnr LIKE lips-posnr,
                matnr LIKE lips-matnr,
                charg LIKE lips-charg,
                lfimg LIKE lips-lfimg,
                vgbel LIKE lips-vgbel,
                vgpos LIKE lips-vgpos,
                werks LIKE lips-werks,
             END OF ty_lips,
              BEGIN OF ty_bkpf,
                xblnr LIKE bkpf-xblnr,
                belnr LIKE bkpf-belnr,
                GJAHR LIKE BKPF-GJAHR,
                BLART LIKE BKPF-BLART,
              END OF ty_bkpf,
              BEGIN OF ty_bkpf1,
                awkey LIKE bkpf-awkey,
                belnr LIKE bkpf-belnr,
              END OF ty_bkpf1,

              BEGIN OF ty_bseg,
                vbeln LIKE bseg-vbeln,
                belnr LIKE bseg-belnr,
                dmbtr LIKE bseg-dmbtr,
                vbel2 LIKE bseg-vbel2,
                posn2 LIKE bseg-posn2,
                hkont LIKE bseg-hkont,
                matnr LIKE bseg-matnr,
                erfmg LIKE bseg-erfmg,
                erfme LIKE bseg-erfme,
                werks LIKE bseg-werks,
                prctr LIKE bseg-prctr,
                shkzg LIKE bseg-shkzg,

                  paobjnr LIKE bseg-paobjnr,
              END OF ty_bseg,

              BEGIN OF ty_bseg1,
                vbeln LIKE bseg-vbeln,
                belnr LIKE bseg-belnr,
                dmbtr LIKE bseg-dmbtr,
                vbel2 LIKE bseg-vbel2,
                posn2 LIKE bseg-posn2,
                hkont LIKE bseg-hkont,
                matnr LIKE bseg-matnr,
                erfmg LIKE bseg-erfmg,
                erfme LIKE bseg-erfme,
                werks LIKE bseg-werks,
                prctr LIKE bseg-prctr,
                shkzg LIKE bseg-shkzg,

                paobjnr LIKE bseg-paobjnr,
              END OF ty_bseg1,
               BEGIN OF ty_bseg2,
                vbeln LIKE bseg-vbeln,
                belnr LIKE bseg-belnr,
                dmbtr LIKE bseg-dmbtr,
                vbel2 LIKE bseg-vbel2,
                posn2 LIKE bseg-posn2,
                hkont LIKE bseg-hkont,
                matnr LIKE bseg-matnr,
                erfmg LIKE bseg-erfmg,
                erfme LIKE bseg-erfme,
                werks LIKE bseg-werks,
                prctr LIKE bseg-prctr,
                 shkzg LIKE bseg-shkzg,

                paobjnr LIKE bseg-paobjnr,
              END OF ty_bseg2,
              BEGIN OF ty_vbrp,
                vbeln LIKE vbrp-vbeln,
                vbeln4 LIKE bkpf-awkey,
                posnr LIKE vbrp-posnr,
                vgbel LIKE vbrp-vgbel,
                vgpos LIKE vbrp-vgpos,
                kzwi1 LIKE vbrp-kzwi1,
                fkimg LIKE vbrp-fkimg,
                paobjnr LIKE vbrp-paobjnr,
              END OF ty_vbrp,
              BEGIN OF ty_vbrk,
                vbeln LIKE vbrk-vbeln,
                awkey LIKE bkpf-awkey,
                fkdat LIKE vbrk-fkdat,
                fkart LIKE vbrk-fkart,
              END OF ty_vbrk,
              BEGIN OF ty_ce1l000,
                kdpos LIKE ce1l000-kdpos,
                kaufn LIKE ce1l000-kaufn,
                rbeln LIKE ce1l000-rbeln,
                rposn  LIKE ce1l000-rposn,
                vv103 LIKE ce1l000-vv103,
                vv104 LIKE ce1l000-vv104,
                vv161 LIKE ce1l000-vv161,
                vv162 LIKE ce1l000-vv162,
                vv163 LIKE ce1l000-vv163,
                vv164 LIKE ce1l000-vv164,
                vv165 LIKE ce1l000-vv165,
                vv166 LIKE ce1l000-vv166,
                vv167 LIKE ce1l000-vv167,
                vv168 LIKE ce1l000-vv168,
                vv169 LIKE ce1l000-vv169,
                vv170 LIKE ce1l000-vv170,
                vv172 LIKE ce1l000-vv172,
              END OF ty_ce1l000,

              BEGIN OF ty_sum,
                cogs LIKE ce1l000-vv170 ,
                sales LIKE ce1l000-vv103,
              END OF ty_sum,

               BEGIN OF ty_result,
                wadat LIKE likp-wadat,
                fkdat LIKE vbrk-fkdat,
                vbeln LIKE likp-vbeln,
                fkart LIKE vbrk-fkart,
                vbeln_so LIKE bseg-vbel2,
                posnr2 LIKE bseg-posn2,
                matnr LIKE bseg-matnr,
                werks LIKE bseg-werks,
                prctr LIKE bseg-prctr,
                hkont_cogs LIKE bseg-dmbtr,"bseg-hkont,
                hkont_fisales LIKE bseg-dmbtr,
                cogs_value LIKE bseg-dmbtr,
                copa_sale LIKE bseg-dmbtr,
                 vbeln3 LIKE vbrk-vbeln,
                posnr3 LIKE vbrp-posnr,
                waers LIKE reguh-waers,
                diff_sale LIKE bseg-dmbtr,
                lfimg LIKE lips-lfimg,
*                vbeln3 like vbrk-vbeln,
*                posnr3 like vbrp-posnr,
                fi_sales LIKE vbrp-kzwi1,
                paobjnr LIKE bseg-paobjnr,
                cogs_dif LIKE bseg-dmbtr,
                posnr LIKE lips-posnr,
                kaufn LIKE ce1l000-kaufn,
                kdpos LIKE ce1l000-kdpos,
                belnr LIKE bkpf-belnr,
               END OF ty_result,

              BEGIN OF ty_hkont,
                hkont LIKE bseg-hkont,
              END OF ty_hkont.

RANGES : HKONT FOR BSEG-HKONT.
DATA : WA_HKONT LIKE LINE OF HKONT.


WA_HKONT-SIGN = 'I'.
WA_HKONT-OPTION = 'EQ'.
WA_HKONT-LOW = '0000400402'.
WA_HKONT-HIGH = ' '.
APPEND WA_HKONT TO HKONT.
CLEAR WA_HKONT.
WA_HKONT-SIGN = 'I'.
WA_HKONT-OPTION = 'EQ'.
WA_HKONT-LOW = '0000400902'.
WA_HKONT-HIGH = ' '.
APPEND WA_HKONT TO HKONT.

*Internal Tables
  DATA:     it_likp TYPE ty_likp OCCURS 0 WITH HEADER LINE,
            wa_likp TYPE ty_likp,
            it_vbup TYPE ty_vbup OCCURS 0 WITH HEADER LINE,
            wa_vbup TYPE ty_vbup,
            it_lips TYPE ty_lips OCCURS 0 WITH HEADER LINE,
            wa_lips TYPE ty_lips,
            it_bkpf TYPE ty_bkpf OCCURS 0 WITH HEADER LINE,
            wa_bkpf TYPE ty_bkpf,
            it_bkpf1 TYPE ty_bkpf1 OCCURS 0 WITH HEADER LINE,
            wa_bkpf1 TYPE ty_bkpf1,
            it_bseg TYPE ty_bseg OCCURS 0 WITH HEADER LINE,
            wa_bseg TYPE ty_bseg,
            it_bseg1 TYPE ty_bseg1 OCCURS 0 WITH HEADER LINE,
            wa_bseg1 TYPE ty_bseg1,
            it_bseg2 TYPE ty_bseg2 OCCURS 0 WITH HEADER LINE,
            wa_bseg2 TYPE ty_bseg2,
            it_vbrp TYPE ty_vbrp OCCURS 0 WITH HEADER LINE,
            wa_vbrp TYPE ty_vbrp,
            it_vbrk TYPE ty_vbrk OCCURS 0 WITH HEADER LINE,
            wa_vbrk TYPE ty_vbrk,
            it_ce1l000 TYPE ty_ce1l000 OCCURS 0 WITH HEADER LINE,
            wa_ce1l000 TYPE ty_ce1l000,

            it_sum TYPE ty_sum OCCURS 0 WITH HEADER LINE,
            wa_sum TYPE ty_sum,


            it_result TYPE ty_result OCCURS 0 WITH HEADER LINE,
            wa_result TYPE ty_result,

            it_hkont TYPE ty_hkont OCCURS 0 WITH HEADER LINE.

  DATA: flag TYPE c,
        gv_periv TYPE periv,
        gv_gjahr TYPE gjahr,
        gv_mnt_cnt TYPE i,
        gv_plifz LIKE t5a4a-dlydy,
        gv_flg TYPE c,
        mt_count TYPE i,
        w_lines TYPE i,
        m_lines TYPE i.
* Auxiliary Selection criteria structure
  DATA: l_s_select TYPE srsc_s_select.
  DATA :  vbrp_flg  TYPE sy-tabix,
          vbup_flg  TYPE sy-tabix.
* Maximum number of lines for DB table
  STATICS: s_s_if TYPE srsc_s_if_simple,

* counter
          s_counter_datapakid LIKE sy-tabix,

* cursor
          s_cursor TYPE cursor,
          s1_cursor TYPE cursor,
          s2_cursor TYPE cursor,
          s3_cursor TYPE cursor,
          s4_cursor TYPE cursor,
          s5_cursor TYPE cursor,
          s6_cursor TYPE cursor,
          s7_cursor TYPE cursor,
          s8_cursor TYPE cursor,
          s9_cursor TYPE cursor.

* Select ranges
  RANGES: l_r_wadat   FOR likp-wadat,
                 l_r_werks   FOR pbim-werks.

* Initialization mode (first call by SAPI) or data transfer mode
* (following calls) ?
  IF i_initflag = sbiwa_c_flag_on.

************************************************************************
* Initialization: check input parameters
*                 buffer input parameters
*                 prepare data selection
************************************************************************

* Check DataSource validity
    CASE i_dsource.
      WHEN 'ZMFA_GET_DATA_FICOPA'.
      WHEN OTHERS.
        IF 1 = 2. MESSAGE e009(r3). ENDIF.
* this is a typical log call. Please write every error message like this
        log_write 'E'                  "message type
                  'R3'                 "message class
                  '009'                "message number
                  i_dsource   "message variable 1
                  ' '.                 "message variable 2
        RAISE error_passed_to_mess_handler.
    ENDCASE.

    APPEND LINES OF i_t_select TO s_s_if-t_select.

* Fill parameter buffer for data extraction calls
    s_s_if-requnr    = i_requnr.
    s_s_if-dsource = i_dsource.
    s_s_if-maxsize   = i_maxsize.

* Fill field list table for an optimized select statement
* (in case that there is no 1:1 relation between InfoSource fields
* and database table fields this may be far from beeing trivial)
    APPEND LINES OF i_t_fields TO s_s_if-t_fields.

  ELSE.                 "Initialization mode or data extraction ?

************************************************************************
* Data transfer: First Call      OPEN CURSOR + FETCH
*                Following Calls FETCH only
************************************************************************

* First data package -> OPEN CURSOR
    IF s_counter_datapakid = 0.

* Fill range tables BW will only pass down simple selection criteria
* of the type SIGN = 'I' and OPTION = 'EQ' or OPTION = 'BT'.
      LOOP AT s_s_if-t_select INTO l_s_select WHERE fieldnm = 'WADAT'.
        MOVE-CORRESPONDING l_s_select TO l_r_wadat.
        APPEND l_r_wadat.
      ENDLOOP.

      LOOP AT s_s_if-t_select INTO l_s_select WHERE fieldnm = 'WERKS'.
        MOVE-CORRESPONDING l_s_select TO l_r_werks.
        APPEND l_r_werks.
      ENDLOOP.
* Determine number of database records to be read per FETCH statement
* from input parameter I_MAXSIZE. If there is a one to one relation
* between DataSource table lines and database entries, this is trivial.
* In other cases, it may be impossible and some estimated value has to
* be determined.
      OPEN CURSOR WITH HOLD s_cursor FOR
      SELECT vbeln wadat vstel FROM likp
      BYPASSING BUFFER
                               WHERE
*                               VBTYP EQ 'J' and
*                               VKORG IN ('l001', 'l002') and
                               wadat   IN l_r_wadat AND
                               lfart IN ('ZLFD', 'ZLFF', 'ZLR', 'ZNL5').

* Fetch records into interface table.
*   named E_T_'Name of extract structure'.
      WHILE NOT s_cursor IS INITIAL .
        FETCH NEXT CURSOR s_cursor
                   APPENDING CORRESPONDING FIELDS
                   OF TABLE it_likp
                   PACKAGE SIZE s_s_if-maxsize.
        IF sy-subrc <> 0.
          CLOSE CURSOR s_cursor.
        ENDIF.
      ENDWHILE.
    ENDIF.                             "First data package ?


    DELETE it_likp WHERE vstel EQ 'LP01'.

    IF it_likp[] IS NOT INITIAL.
      OPEN CURSOR WITH HOLD s1_cursor FOR
        SELECT vbeln posnr wbsta FROM vbup
        BYPASSING BUFFER
        FOR ALL ENTRIES IN it_likp
                                 WHERE vbeln = it_likp-vbeln AND
                                       wbsta EQ 'C'.


      WHILE NOT s1_cursor IS INITIAL .
        FETCH NEXT CURSOR s1_cursor
                   APPENDING CORRESPONDING FIELDS
                   OF TABLE it_vbup
                   PACKAGE SIZE s_s_if-maxsize.
        IF sy-subrc <> 0.
          CLOSE CURSOR s1_cursor.

        ENDIF.
      ENDWHILE.
    ENDIF.

    SORT it_vbup BY vbeln posnr.

    IF it_vbup[] IS NOT INITIAL.
      OPEN CURSOR WITH HOLD s2_cursor FOR
        SELECT vbeln posnr matnr charg lfimg vgbel vgpos werks FROM lips
        BYPASSING BUFFER
        FOR ALL ENTRIES IN it_vbup
                                 WHERE vbeln = it_vbup-vbeln AND
                                       posnr = it_vbup-posnr.

      WHILE NOT s2_cursor IS INITIAL .
        FETCH NEXT CURSOR s2_cursor
                   APPENDING CORRESPONDING FIELDS
                   OF TABLE it_lips
                   PACKAGE SIZE s_s_if-maxsize.
        IF sy-subrc <> 0.
          CLOSE CURSOR s2_cursor.

        ENDIF.
      ENDWHILE.


      DELETE it_lips WHERE lfimg = '0.000'.

    ENDIF.

    LOOP AT it_lips INTO wa_lips.
      wa_lips-vbeln1 = wa_lips-vbeln.
      MODIFY it_lips FROM wa_lips.
    ENDLOOP.

    IF it_lips[] IS NOT INITIAL.
      DELETE it_lips WHERE lfimg IS INITIAL.
      CLEAR gv_periv.
      SELECT SINGLE periv INTO gv_periv FROM t001 WHERE bukrs = 'L001'.

      CALL FUNCTION 'DATE_TO_PERIOD_CONVERT'
        EXPORTING
          i_date  = l_r_wadat-low " sy-datum
          i_periv = gv_periv
        IMPORTING
          e_gjahr = gv_gjahr
        EXCEPTIONS
          OTHERS  = 4.

      OPEN CURSOR WITH HOLD s3_cursor FOR
        SELECT belnr xblnr BLART GJAHR FROM bkpf
        BYPASSING BUFFER
        FOR ALL ENTRIES IN it_lips
                                 WHERE bukrs = 'L001' AND
                                       bstat = '' and
                                       xblnr = it_lips-vbeln. " and

      WHILE NOT s3_cursor IS INITIAL .
        FETCH NEXT CURSOR s3_cursor
                   APPENDING CORRESPONDING FIELDS
                   OF TABLE it_bkpf
                   PACKAGE SIZE s_s_if-maxsize.
        IF sy-subrc <> 0.
          CLOSE CURSOR s3_cursor.

        ENDIF.
      ENDWHILE.
    ENDIF.

    loop at it_bKPF INTO wa_bkpf.
      if wa_bkpf-blart = 'WL'.
      ELSE.
        DELETE IT_BKPF.
     ENDIF.
      if wa_bkpf-gjahr = gv_gjahr .
      ELSE.
        DELETE IT_BKPF.
        ENDIF.

       endloop.

    IF it_bkpf[] IS NOT INITIAL.
      SORT it_bkpf BY xblnr belnr DESCENDING.
      DELETE ADJACENT DUPLICATES FROM it_bkpf COMPARING xblnr.

      OPEN CURSOR WITH HOLD s4_cursor FOR
        SELECT vbeln belnr dmbtr vbel2 posn2
               hkont matnr erfmg erfme
               werks prctr shkzg
               FROM bseg
        BYPASSING BUFFER
        FOR ALL ENTRIES IN it_bkpf
                                 WHERE bukrs = 'L001' AND
                                       belnr = it_bkpf-belnr AND
                                       gjahr = gv_gjahr." AND
                                     

      WHILE NOT s4_cursor IS INITIAL .
        FETCH NEXT CURSOR s4_cursor
                   APPENDING CORRESPONDING FIELDS
                   OF TABLE it_bseg
                   PACKAGE SIZE s_s_if-maxsize.
        IF sy-subrc <> 0.
          CLOSE CURSOR s4_cursor.

        ENDIF.
      ENDWHILE.

      loop at it_bseg into wa_bseg.

        IF WA_BSEG-HKONT IN HKONT.
          ELSE.
            DELETE IT_BSEG.
            ENDIF.

     ENDLOOP.

   loop at it_bseg into wa_bseg.
     if wa_bseg-shkzg = 'H'.
           wa_bseg-dmbtr = wa_bseg-dmbtr * -1.
           MODIFY it_bseg FROM wa_bseg.
       endif.
ENDLOOP.
    ENDIF.

    IF it_lips[] IS NOT INITIAL.


      OPEN CURSOR WITH HOLD s5_cursor FOR
        SELECT vbeln posnr vgbel  vgpos fkimg paobjnr       "KZWI1
               FROM vbrp
        BYPASSING BUFFER
        FOR ALL ENTRIES IN it_lips
                                 WHERE vgbel = it_lips-vbeln1 AND
                                       vgpos = it_lips-posnr.



      WHILE NOT s5_cursor IS INITIAL .
        FETCH NEXT CURSOR s5_cursor
                   APPENDING CORRESPONDING FIELDS
                   OF TABLE it_vbrp
                   PACKAGE SIZE s_s_if-maxsize.
        IF sy-subrc <> 0.
          CLOSE CURSOR s5_cursor.

        ENDIF.
      ENDWHILE.
    ENDIF.
    LOOP AT it_vbrp INTO wa_vbrp.
      wa_vbrp-vbeln4 = wa_vbrp-vbeln.

      MODIFY it_vbrp FROM wa_vbrp.

    ENDLOOP.

    IF it_vbrp[] IS NOT INITIAL.

      OPEN CURSOR WITH HOLD s6_cursor FOR
        SELECT vbeln fkdat fkart
               FROM vbrk
        BYPASSING BUFFER
        FOR ALL ENTRIES IN it_vbrp
                                 WHERE vbeln = it_vbrp-vbeln AND
                                         fkart NE 'ZOX1' AND
                                         fksto = ''.


      WHILE NOT s6_cursor IS INITIAL .
        FETCH NEXT CURSOR s6_cursor
                   APPENDING CORRESPONDING FIELDS
                   OF TABLE it_vbrk
                   PACKAGE SIZE s_s_if-maxsize.
        IF sy-subrc <> 0.
          CLOSE CURSOR s6_cursor.

        ENDIF.
      ENDWHILE.
      DELETE it_vbrk WHERE fkart EQ 'S1'.
      DELETE it_vbrk WHERE fkart EQ 'ZS1'.
    ENDIF.

    SORT it_vbrk BY vbeln.
    LOOP AT it_vbrp INTO wa_vbrp.
      vbrp_flg = sy-tabix.
      READ TABLE it_vbrk WITH KEY vbeln = wa_vbrp-vbeln.
      IF sy-subrc <> 0.
        DELETE it_vbrp INDEX vbrp_flg.
      ENDIF.
    ENDLOOP.

    IF it_vbrk[] IS NOT INITIAL.

      OPEN CURSOR WITH HOLD s7_cursor FOR
        SELECT kdpos kaufn rbeln rposn vv103 vv104 vv161 vv162 vv163
                vv164 vv165 vv166 vv167 vv168
                vv169 vv170 vv172
                FROM ce1l000
        BYPASSING BUFFER
        FOR ALL ENTRIES IN it_vbrk
                                 WHERE
                                         vrgar EQ 'F'AND
                                         rbeln = it_vbrk-vbeln.
*                                         RPOSN = it_vbrp-posnr.

      WHILE NOT s7_cursor IS INITIAL .
        FETCH NEXT CURSOR s7_cursor
                   APPENDING CORRESPONDING FIELDS
                   OF TABLE it_ce1l000
                   PACKAGE SIZE s_s_if-maxsize.
        IF sy-subrc <> 0.
          CLOSE CURSOR s7_cursor.

        ENDIF.
      ENDWHILE.
    ENDIF.

    LOOP AT it_vbrk INTO wa_vbrk.
      wa_vbrk-awkey = wa_vbrk-vbeln.

      MODIFY it_vbrk FROM wa_vbrk.

    ENDLOOP.

    IF it_vbrk[] IS NOT INITIAL.
*      DELETE it_vbrp WHERE fkimg IS INITIAL.
      CLEAR gv_periv.
      SELECT SINGLE periv INTO gv_periv FROM t001 WHERE bukrs = 'L001'.

      CALL FUNCTION 'DATE_TO_PERIOD_CONVERT'
        EXPORTING
          i_date  = l_r_wadat-low "sy-datum
          i_periv = gv_periv
        IMPORTING
          e_gjahr = gv_gjahr
        EXCEPTIONS
          OTHERS  = 4.

      OPEN CURSOR WITH HOLD s8_cursor FOR
        SELECT belnr awkey  FROM bkpf
        BYPASSING BUFFER
        FOR ALL ENTRIES IN it_vbrk
                                 WHERE bukrs = 'L001' AND
                                       gjahr = gv_gjahr AND
                                       blart = 'RV' AND
                                       awtyp = 'VBRK' AND
                                       awkey = it_vbrk-awkey.

      WHILE NOT s8_cursor IS INITIAL .
        FETCH NEXT CURSOR s8_cursor
                   APPENDING CORRESPONDING FIELDS
                   OF TABLE it_bkpf1
                   PACKAGE SIZE s_s_if-maxsize.
        IF sy-subrc <> 0.
          CLOSE CURSOR s8_cursor.

        ENDIF.
      ENDWHILE.
    ENDIF.
*
    IF it_bkpf1[] IS NOT INITIAL.
      SORT it_bkpf1 BY awkey belnr DESCENDING.
      DELETE ADJACENT DUPLICATES FROM it_bkpf1 COMPARING awkey.

      OPEN CURSOR WITH HOLD s9_cursor FOR
              SELECT dmbtr hkont shkzg paobjnr
               FROM bseg
        BYPASSING BUFFER
        FOR ALL ENTRIES IN it_bkpf1
                                 WHERE bukrs = 'L001' AND
                                       belnr = it_bkpf1-belnr AND
                                       gjahr = gv_gjahr AND
                                       hkont IN ('0000300000','0000300003','0000301001','0000302001',
                                                 '0000304001','0000305001','0000305002','0000306001',
                                                 '0000307001','0000308001','0000308003','0000308011',
                                                 '0000308012','0000317004','0000317005','0000303001').

      WHILE NOT s9_cursor IS INITIAL .
        FETCH NEXT CURSOR s9_cursor
                   APPENDING CORRESPONDING FIELDS
                   OF TABLE it_bseg1
                   PACKAGE SIZE s_s_if-maxsize.
        IF sy-subrc <> 0.
          CLOSE CURSOR s9_cursor.
*           RAISE NO_MORE_DATA.
        ENDIF.
      ENDWHILE.
    ENDIF.

    SORT it_vbup BY vbeln.
    LOOP AT it_vbup INTO wa_vbup.
      vbup_flg = sy-tabix.
      READ TABLE it_lips WITH KEY vbeln = wa_vbup-vbeln
                                   posnr = wa_vbup-posnr .
      IF sy-subrc <> 0.
        DELETE it_vbup INDEX vbup_flg.
      ENDIF.
    ENDLOOP.

    LOOP AT it_vbup INTO wa_vbup.
      wa_result-vbeln = wa_vbup-vbeln.
      wa_result-posnr = wa_vbup-posnr.
      APPEND wa_result TO it_result.
    ENDLOOP.

    SORT it_lips BY vbeln posnr.
    SORT it_bseg  BY belnr matnr erfmg.
    SORT it_vbrp BY vgbel vgpos.
    SORT it_ce1l000 BY rbeln rposn.
    SORT it_vbrk BY vbeln.
    SORT it_bkpf BY xblnr.

    LOOP AT it_result INTO wa_result.

      READ TABLE it_lips WITH KEY vbeln = wa_result-vbeln
                                   posnr = wa_result-posnr.

      wa_result-matnr = it_lips-matnr.
      wa_result-lfimg = it_lips-lfimg.
      wa_result-vbeln_so = it_lips-vgbel.
      wa_result-posnr2 = it_lips-vgpos.
      wa_result-werks = it_lips-werks.
      CLEAR : it_lips.

      READ TABLE it_likp WITH KEY vbeln = wa_result-vbeln.

      wa_result-wadat = it_likp-wadat.

      CLEAR : it_likp.

      READ TABLE it_bkpf WITH KEY xblnr = wa_result-vbeln.

      wa_result-belnr = it_bkpf-belnr.

      CLEAR  it_bkpf.
      READ TABLE it_bseg WITH KEY belnr =  wa_result-belnr "wa_result-vbeln
                                  matnr = wa_result-matnr
                                  erfmg = wa_result-lfimg.

      wa_result-prctr = it_bseg-prctr.
      wa_result-hkont_cogs = it_bseg-dmbtr.

      CLEAR: it_bseg.

      READ TABLE it_vbrp  WITH KEY vgbel = wa_result-vbeln
                                   vgpos = wa_result-posnr.

      wa_result-vbeln3 = it_vbrp-vbeln.
      wa_result-posnr3 = it_vbrp-posnr.
      wa_result-paobjnr = it_vbrp-paobjnr.
      CLEAR: it_vbrp.
      DATA: bseg1_dmbtr LIKE bseg-dmbtr.

      CLEAR bseg1_dmbtr.

      READ TABLE it_bseg1 WITH KEY paobjnr = wa_result-paobjnr.

      IF sy-subrc EQ 0.
        LOOP AT it_bseg1 INTO wa_bseg1 WHERE paobjnr = wa_result-paobjnr.
          IF wa_bseg1-shkzg = 'S'.
            wa_bseg1-dmbtr =  wa_bseg1-dmbtr * -1 .
          ENDIF.
          bseg1_dmbtr = bseg1_dmbtr + wa_bseg1-dmbtr.

          CLEAR wa_bseg1.

        ENDLOOP.

      ELSE.
        CLEAR bseg1_dmbtr.
      ENDIF.

* wa_result-FI_SALES = bseg1_dmbtr.

      wa_result-hkont_fisales = bseg1_dmbtr.

      CLEAR : it_bseg1.

    SORT it_vbrk BY vbeln.

      READ TABLE it_vbrk WITH KEY vbeln =  wa_result-vbeln3.
if sy-subrc = 0.
      wa_result-fkdat =  it_vbrk-fkdat.
      wa_result-fkart = it_vbrk-fkart.
endif.
      CLEAR: it_vbrk.

      READ TABLE it_ce1l000  WITH KEY rbeln =  wa_result-vbeln3
                                      rposn =  wa_result-posnr3.

      wa_result-cogs_value = it_ce1l000-vv161 + it_ce1l000-vv162 + it_ce1l000-vv163
                    + it_ce1l000-vv164 + it_ce1l000-vv165 + it_ce1l000-vv166
                    + it_ce1l000-vv167 + it_ce1l000-vv168 + it_ce1l000-vv169
                    + it_ce1l000-vv170 + it_ce1l000-vv172.

      wa_result-copa_sale = it_ce1l000-vv103 + it_ce1l000-vv104.

      CLEAR : it_ce1l000.

      MODIFY it_result FROM wa_result.

      CLEAR wa_result.
    ENDLOOP.

    LOOP AT it_result INTO wa_result.
      MOVE-CORRESPONDING  wa_result TO e_t_data.
      APPEND e_t_data.
    ENDLOOP.

IF e_t_data[] IS INITIAL.
      RAISE no_more_data.
    ENDIF.

S_COUNTER_DATAPAKID = S_COUNTER_DATAPAKID + 1.
  ENDIF.

ENDFUNCTION.


Comments

Popular posts from this blog

One stage stop to know all about BW Extractors-Part1

COPA Extraction Step By Step in SAP BW

How does a datasource communicates "DELTA" with BW?