Wednesday, December 15, 2010

Approve PO in Page of Approving PR, do Error in My Bapi

Posted by Aries Wandari

I create bapi to track the date PO reach full release, since PO release date is only tracked on header change and its takes long time to pull it from SAP directly. To minimize data extracted from CDHDR, only history data of 'ME21N', 'ME22N', 'ME23N', 'ME29N', 'ME28' are pulled. User reporting some release dates of PO is invalid. Debugging shows that some user is approving PO via PR approval page ME54N and ME55.

SELECT objectid changenr udate tcode objectid
into (i_cdhdr-ebeln, i_cdhdr-changenr, i_cdhdr-frgdt, i_cdhdr-tcode, i_cdhdr-objectid)
FROM CDHDR
WHERE udate in p_erdat AND OBJECTCLAS = 'EINKBELEG'
AND TCODE in ('ME21N','ME22N','ME23N','ME29N','ME28','ME54N','ME55').
  append i_cdhdr.
endselect.

 

image

---------------------

FUNCTION ZBAPI_PO .
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     VALUE(DATELO) TYPE  ZBAPIPOSTR-BEDAT OPTIONAL
*"     VALUE(DATEHI) TYPE  ZBAPIPOSTR-BEDAT OPTIONAL
*"     VALUE(DOCLO) TYPE  ZBAPIPOSTR-EBELN OPTIONAL
*"     VALUE(DOCHI) TYPE  ZBAPIPOSTR-EBELN OPTIONAL
*"     VALUE(DATETY) TYPE  ZBAPIPRSTR-LOEKZ OPTIONAL
*"  EXPORTING
*"     VALUE(RETURN) TYPE  BAPIRET2
*"  TABLES
*"      TBLPO STRUCTURE  ZBAPIPOSTR OPTIONAL
*"----------------------------------------------------------------------
*
*  CHANGES :
*  20-04-2010  Creation Imam F
*  15-07-2010  Farid
*  ADD FRGDT  > RELEASE DATE
*      NETWR2 > TOTAL VALUE IN LOCAL PRICE
*      EINDT  > TARGET DELIV DATE
*      BLDAT  > GR DOCUMENT DATE
*      BLDAT2 > IR DOCUMENT DATE
*      BUDAT  > GR POSTING DATE
*      BUDAT2 > IR POSTING DATE
*  Parameter based on docno or amend date (from table history)

*DATA DEFINITION
data: begin of p_badat occurs 0,
        sign   type char1,
        option type char2,
        low    like ekko-bedat,
        high   like ekko-bedat,
end of p_badat.
data: begin of p_erdat occurs 0,
        sign   type char1,
        option type char2,
        low    like ekko-aedat,
        high   like ekko-aedat,
end of p_erdat.
data: begin of p_ebeln occurs 0,
        sign   type char1,
        option type char2,
        low    like ekko-ebeln,
        high   like ekko-ebeln,
end of p_ebeln.

data: begin of i_po occurs 0.
        include structure ZBAPIPOSTR.
data: OBJECTID9 like cdhdr-objectid.
data: frggr like ekko-frggr.
data: frgsx like ekko-frgsx.
data: end of i_po.

data : begin of i_t001k occurs 0,
  bwkey like t001k-bwkey,
  bukrs like t001k-bukrs,
end of i_t001k.

data : begin of i_t024 occurs 0,
  ekgrp like t024-ekgrp,
  eknam like t024-eknam,
end of i_t024.

DATA : BEGIN OF IT16FS OCCURS 0,
          FRGGR LIKE T16FS-FRGGR,      "Release group
          FRGSX LIKE T16FS-FRGSX,      "Release code
          FRGC1 LIKE T16FS-FRGC1,      "Release code
          FRGC2 LIKE T16FS-FRGC2,      "Release code
          FRGC3 LIKE T16FS-FRGC3,      "Release code
          FRGC4 LIKE T16FS-FRGC4,      "Release code
          FRGC5 LIKE T16FS-FRGC5,      "Release code
       END OF IT16FS.
DATA: frg like T16FS-FRGC2.

DATA :  tp_name like THEAD-TDNAME,
        lines type TLINE occurs 0 with header line.

DATA : BEGIN OF i_ekkn OCCURS 0,
  EBELN like ekkn-ebeln,
  EBELP like ekkn-EBELP,
  KOSTL like ekkn-kostl,
  AUFNR like ekkn-aufnr,
  SAKTO like ekkn-sakto,
  GSBER like ekkn-gsber,
end of i_ekkn.

DATA : BEGIN OF i_ekbe OCCURS 0,
  EBELN like ekbe-ebeln,
  EBELP like ekbe-EBELP,
  BEWTP like ekbe-bewtp,
  BELNR like ekbe-belnr,
  ELIKZ like ekbe-elikz,
  BUDAT like ekbe-budat,
  BLDAT like ekbe-bldat,
end of i_ekbe.

DATA : BEGIN OF i_eket OCCURS 0,
  EBELN like eket-ebeln,
  EBELP like eket-EBELP,
  EINDT like eket-eindt,
end of i_eket.

DATA : objnr type COBRB-OBJNR.
DATA : xbewtp like i_ekbe-bewtp.

DATA : BEGIN OF i_cdhdr occurs 0,
  ebeln like ekko-ebeln,
  changenr like cdhdr-changenr,
  FRGDT like cdhdr-udate,
  tcode like cdhdr-tcode,
  objectid like cdhdr-objectid,
  tabname like cdpos-tabname,
  fname like cdpos-fname,
  value_new like cdpos-value_new,
end of i_cdhdr.

FIELD-SYMBOLS: <fs> like i_cdhdr.

DATA: i_hdrrel like table of i_cdhdr with header line.

*DATA: i_cdhdr type sorted table of t_cdhdr with non-unique key changenr objectid with header line.

data : begin of i_t16fw occurs 0,
  frggr like t16fw-frggr,
  frgco like t16fw-frgco,
  werks like t16fw-werks,
  bname like usr21-bname,
  smtp_addr like adr6-smtp_addr,
end of i_t16fw.

data : begin of i_lfa1 occurs 0,
  lifnr like lfa1-lifnr,
  name1 like lfa1-name1,
  name2 like lfa1-name2,
  anred like lfa1-anred,
end of i_lfa1.

DATA : loc_amount TYPE EKPO-NETWR, opt(3) type c.

*only DATE or DOC, not both
if not DATELO is initial.
  if DATEHI is initial.
    DATEHI = DATELO.
  endif.

  p_erdat-sign = 'I'.
  p_erdat-option = 'BT'.
  p_erdat-low = datelo.
  p_erdat-high = datehi.
  append p_erdat.

  opt = 'DAT'.

elseif ( not DOCLO is initial ).
  if DOCHI is initial.
    DOCHI = DOCLO.
  endif.

  p_ebeln-sign = 'I'.
  p_ebeln-option = 'BT'.
  p_ebeln-low = doclo.
  p_ebeln-high = dochi.
  append p_ebeln.

  opt = 'DOC'.
endif.

* PO selection based on PO history
  if ( opt = 'DAT' ).

    SELECT objectid changenr udate tcode objectid
    into (i_cdhdr-ebeln, i_cdhdr-changenr, i_cdhdr-frgdt, i_cdhdr-tcode, i_cdhdr-objectid)
    FROM CDHDR
    WHERE udate in p_erdat AND OBJECTCLAS = 'EINKBELEG'
    AND TCODE in ('ME21N','ME22N','ME23N','ME29N','ME28','ME54N','ME55').
      append i_cdhdr.
    endselect.

    i_hdrrel[] = i_cdhdr[].

    sort i_cdhdr by ebeln.
    delete adjacent duplicates from i_cdhdr comparing ebeln.

    if not i_cdhdr is initial.
      SELECT a~ebeln b~ebelp b~loekz b~aedat txz01 matnr werks lgort matkl
        infnr menge meins netpr peinh netwr wepos weunb repos webre mtart
        a~bstyp bsart a~statu lponr zterm zbd1t ekorg ekgrp waers wkurs bedat
        knumv unsez procstat bednr frgke frgzu lifnr a~ebeln as objectid9
        frggr frgsx banfn bnfpo a~mandt a~ernam elikz
      INTO CORRESPONDING FIELDS OF TABLE i_po
      FROM EKKO as a inner join EKPO as b on a~EBELN = b~EBELN
      for all entries in i_cdhdr
      WHERE
      a~ebeln = i_cdhdr-ebeln.
    endif.

* PO selection based on input PO doc
  elseif ( opt = 'DOC' ) and ( not p_ebeln is initial ).
     SELECT a~ebeln b~ebelp b~loekz b~aedat txz01 matnr werks lgort matkl
        infnr menge meins netpr peinh netwr wepos weunb repos webre mtart
        a~bstyp bsart a~statu lponr zterm zbd1t ekorg ekgrp waers wkurs bedat
        knumv unsez procstat bednr frgke frgzu lifnr a~ebeln as objectid9
        frggr frgsx banfn bnfpo a~mandt ernam elikz
     INTO CORRESPONDING FIELDS OF TABLE i_po
     FROM EKKO as a inner join EKPO as b on a~EBELN = b~EBELN
     WHERE
     a~ebeln in p_ebeln.

     if not i_po[] is initial.
       SELECT objectid changenr udate tcode objectid
       into (i_hdrrel-ebeln, i_hdrrel-changenr, i_hdrrel-frgdt, i_hdrrel-tcode, i_hdrrel-objectid)
       FROM CDHDR
       for all entries in i_po
       WHERE objectid = i_po-objectid9 AND OBJECTCLAS = 'EINKBELEG'
       AND TCODE in ('ME21N','ME22N','ME23N','ME29N','ME28','ME54N','ME55').
         append i_hdrrel.
       endselect.
     endif.

  endif.

*if not i_po is initial. this fail
IF not i_po[] is initial.

* populate detail change (cdpos) only from ME29N and ME28 (approval)
  delete i_hdrrel where tcode = 'ME21N' or tcode = 'ME22N' or tcode = 'ME23N'.
  loop at i_hdrrel assigning <fs>.
    select fname value_new into (<fs>-fname, <fs>-value_new)
    from cdpos
    where changenr = <fs>-changenr and fname = 'PROCSTAT' and value_new = '5'.
    endselect.
  endloop.

*get last full release approval history
  sort i_hdrrel by ebeln changenr descending.
  delete adjacent duplicates from i_hdrrel comparing ebeln.

* DATA SELECTON
  select bwkey bukrs into table i_t001k
  from T001K order by BWKEY BUKRS.

  select ekgrp eknam into table i_t024
  from T024 order by ekgrp eknam.

  select FRGGR FRGSX FRGC1 FRGC2 FRGC3 FRGC4 FRGC5
  into table IT16FS
  from T16FS.
  sort IT16FS by FRGGR FRGSX.

  if sy-subrc = 0.
        SELECT EBELN EBELP KOSTL AUFNR SAKTO into table i_ekkn
        from EKKN for all entries in i_po
        where EBELN = i_po-EBELN
                and  EBELP = i_po-EBELP.
        sort i_ekkn by ebeln ebelp.

*farid: get target delivery status and actual deliv date
        SELECT EBELN EBELP BEWTP BELNR ELIKZ BUDAT BLDAT into table i_ekbe
        from EKBE for all entries in i_po
        where EBELN = i_po-EBELN
                and  EBELP = i_po-EBELP.
*E > GR, Q > MIRO, sort belnr asc so catch first gr
        sort i_ekbe by ebeln ebelp bewtp belnr.

*farid: get target delivery date
        SELECT EBELN EBELP EINDT into table i_eket
        from EKET for all entries in i_po
        where EBELN = i_po-EBELN
                and  EBELP = i_po-EBELP.
        sort i_eket by ebeln ebelp.

        SELECT frggr frgco werks bname smtp_addr into table i_t16fw
        from T16FW as a inner join USR21 as b on a~objid = b~bname
        left outer join ADR6 as c on c~addrnumber = b~addrnumber
        and c~persnumber = b~persnumber
        order by werks frggr frgco.

        SELECT lifnr name1 name2 anred into table i_lfa1
        from LFA1 for all entries in i_po
        where lifnr = i_po-lifnr.
        sort i_lfa1 by lifnr.
  endif.
endif.

*DATA PROCESSING
loop at i_po.
  read table i_t001k with key BWKEY = i_po-werks
  binary search.
  if sy-subrc = 0.
    i_po-BUKRS = i_t001k-bukrs.
  endif.

  read table i_t024 with key EKGRP = i_po-ekgrp
  binary search.
  if sy-subrc = 0.
    i_po-eknam = i_t024-eknam.
  endif.

*->  Change PO status
  clear xbewtp.

*search GR, bewtp = E, get the first because sort by belnr asc
  read table i_ekbe with key EBELN = i_po-ebeln
      EBELP = i_po-ebelp BEWTP = 'E' binary search.
  if sy-subrc = 0.
    i_po-bldat = i_ekbe-bldat.
    i_po-budat = i_ekbe-budat.
    xbewtp = i_ekbe-bewtp.
    CASE i_ekbe-bewtp.
      WHEN 'E'.
        if i_ekbe-elikz = 'X'.
          i_po-STATUS = 'GR Total'.
        else.
          i_po-STATUS = 'GR Partial'.
        endif.
      WHEN 'Q'.
        i_po-STATUS = 'Invoiced'.
      WHEN OTHERS.
        i_po-STATUS = i_ekbe-bewtp.
    ENDCASE.
  endif.

*search IR, bewtp = Q, get the first because sort by belnr asc
  read table i_ekbe with key EBELN = i_po-ebeln
      EBELP = i_po-ebelp BEWTP = 'Q' binary search.
  if sy-subrc = 0.
    i_po-bldat2 = i_ekbe-bldat.
    i_po-budat2 = i_ekbe-budat.
  endif.

  IF i_po-LOEKZ = 'X' or i_po-LOEKZ = 'L'.
    i_po-STATUS = 'Deleted'.
  ENDIF.

*get target deliv date
  read table i_eket with key EBELN = i_po-ebeln
      EBELP = i_po-ebelp  binary search.
  if sy-subrc = 0.
    i_po-eindt = i_eket-eindt.
  endif.

* Release Status
  IF i_po-FRGKE = '2'.
    i_po-rlssts = 'Released'.
  ELSE.
    read table IT16FS with key
      FRGGR = i_po-FRGGR
      FRGSX = i_po-FRGSX
      binary search.
    if sy-subrc = 0.
      if i_po-FRGZU = 'X'.
        frg = IT16FS-FRGC2.
      elseif i_po-FRGZU = 'XX'.
        frg = IT16FS-FRGC3.
      elseif i_po-FRGZU = 'XXX'.
        frg = IT16FS-FRGC4.
      elseif i_po-FRGZU = 'XXXX'.
        frg = IT16FS-FRGC5.
      else.
        frg = IT16FS-FRGC1.
      endif.
      if frg = 'H1'.
        i_po-rlssts = 'WAppr HOD'.
      elseif frg = 'M1'.
        i_po-rlssts = 'WAppr GM'.
      elseif frg = 'D1'.
        i_po-rlssts = 'WAppr Director'.
      elseif frg = 'P1'.
        i_po-rlssts = 'WAppr President'.
      else.
        if xbewtp = 'E' or xbewtp = 'Q'.
          i_po-rlssts = 'Released'.
        else.
          i_po-rlssts = 'WAppr'.
        endif.
      endif.

*     reading next approval id & email
      read table i_t16fw with key frggr = i_po-frggr
        frgco = frg binary search.
      if sy-subrc = 0.
        i_po-bname = i_t16fw-bname.
        i_po-smtp_addr = i_t16fw-smtp_addr.
      endif.

    else.
      i_po-rlssts = 'WAppr'.
    endif.
  ENDIF.

*-> get PR item text
    refresh lines.
    concatenate i_po-banfn i_po-bnfpo into tp_name.

    call function 'READ_TEXT'
      EXPORTING
        id                      = 'B01'
        language                = sy-langu
        name                    = tp_name
        object                  = 'EBAN'
      TABLES
        lines                   = lines
      EXCEPTIONS
        id                      = 1
        language                = 2
        name                    = 3
        not_found               = 4
        object                  = 5
        reference_check         = 6
        wrong_access_to_archive = 7.

    read table lines index 1.
    move lines-tdline to i_po-itemtxt.
    clear lines.

*get cost center & IO
  read table i_ekkn with key ebeln = i_po-ebeln
  ebelp = i_po-ebelp
  binary search.
  if sy-subrc = 0.
    i_po-sakto = i_ekkn-sakto.
    i_po-aufnr = i_ekkn-aufnr.
    i_po-kostl = i_ekkn-kostl.
    i_po-gsber = i_ekkn-gsber.
  endif.
    if not i_po-AUFNR is initial.
      if i_po-BSART = 'ZWOR'.
        concatenate  'OR' i_po-AUFNR into objnr.
        select single KOSTL into i_po-KOSTL from COBRB
        where OBJNR = objnr.
      else.
        select single KOSTV into i_po-KOSTL from COAS
        where AUFNR = i_po-AUFNR.
      endif.
    endif.

* calculate amount
*   if i_pr-peinh = 0 or i_pr is initial.
*     i_pr-peinh = 1.
*   endif.
*   i_pr-AMOUNT = i_pr-menge * i_pr-preis / i_pr-peinh.

*-> get PO release date from changes table
*farid: if full release, get date from i_hdrrel
  clear i_po-frgdt.
  read table i_hdrrel with key ebeln = i_po-ebeln.
  if ( sy-subrc = 0 ) and ( i_po-frgke = '2' ).
    i_po-frgdt = i_hdrrel-frgdt.
  endif.

*-> get PO Status
  read table i_lfa1 with key lifnr = i_po-lifnr
  binary search.
  if sy-subrc = 0.
    i_po-name1 = i_lfa1-name1.
    i_po-name2 = i_lfa1-name2.
    i_po-anred = i_lfa1-anred.
  endif.

*farid: Get PO Value at Local Currency
              CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
                 EXPORTING
                   CLIENT = i_po-MANDT
                   DATE = i_po-BEDAT
                   FOREIGN_AMOUNT = i_po-NETWR
                   FOREIGN_CURRENCY = i_po-WAERS
                   LOCAL_CURRENCY  = 'USD'
                   RATE = 0
                   TYPE_OF_RATE = 'M'
                   READ_TCURR = 'X'
                IMPORTING
                   LOCAL_AMOUNT = loc_amount
                   EXCEPTIONS
                   NO_RATE_FOUND = 1
                   OVERFLOW = 2
                   NO_FACTORS_FOUND = 3
                   NO_SPREAD_FOUND = 4
                   DERIVED_2_TIMES = 5.

              IF SY-SUBRC = 0.
                i_po-NETWR2 = loc_amount.
              ENDIF.

*  MODIFY i_po.
  if i_po-ebeln ne space.
    MOVE-CORRESPONDING i_po to TBLPO.
    APPEND TBLPO.
  endif.

  CLEAR i_po.
  CLEAR TBLPO.

endloop.   "i_po

ENDFUNCTION.

Wednesday, December 8, 2010

abap error Unable to interpret "/" as a number

Posted by Aries Wandari

I have batch bdc program to close PR, but always error on assigning PR item on ME52 screen “Unable to interpret / as a number”.

Below is screen where error happen, when PR item number inserted.

image 

My old itab already shows that bnfpo is like eban-bnfpo. Change it to character type did solve the problem.

data: begin of itab occurs 0,
             banfn like eban-banfn,
             'bnfpo like eban-bnfpo, 'error here
             bnfpo(5) type C,
        end of itab.

LOOP AT ITAB into wa_itab.

perform open_group.

perform bdc_dynpro      using 'SAPMM06B' '0105'.
perform bdc_field       using 'BDC_CURSOR'
                              'EBAN-BANFN'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'EBAN-BANFN'
                              wa_itab-BANFN.
perform bdc_dynpro      using 'SAPMM06B' '0106'.
perform bdc_field       using 'BDC_CURSOR'
                              'RM06B-BNFPO'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'RM06B-BNFPO'
                              wa_itab-BNFPO.
perform bdc_dynpro      using 'SAPMM06B' '0106'.
perform bdc_field       using 'BDC_CURSOR'
                              'EBAN-BNFPO(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              '=DETA'.
perform bdc_field       using 'RM06B-BNFPO'
                              wa_itab-BNFPO.

perform bdc_field       using 'RM06B-TCSELFLAG(01)'
                              'X'.
perform bdc_dynpro      using 'SAPMM06B' '0102'.
perform bdc_field       using 'BDC_CURSOR'
                              'EBAN-EBAKZ'.
perform bdc_field       using 'BDC_OKCODE'
                              '=BU'.
perform bdc_transaction using 'ME52'.

endloop.

Wednesday, November 24, 2010

SAP User Exit

Posted by Aries Wandari

On module SAP PM, user has scenario that Work Order can’t technically complete if Purchase Requisition generate from that WO not processed to PO yet, with condition the PR in valid (not deleted).

Googling, I find Report  Z_FIND_USEREXIT. Put your tcode and execute, you’ll find the exit, the component, the function module, and the exit include. Then we can edit this code.

Z_FIND_USEREXIT

image

Get the Exit name.

image

image

image

Get the Function Module.

image

Get the include.

image

ZXWO1U03

*&---------------------------------------------------------------------*
*&  Include           ZXWO1U03                                         *
*&---------------------------------------------------------------------*
DATA : STATUS like EBAN-STATU,
        BANFN like EBAN-BANFN,
        LOEKZ like EBAN-LOEKZ.

select STATU BANFN LOEKZ into (STATUS, BANFN, LOEKZ)
    from EBAN
    where ARSNR = CAUFVD_IMP-RSNUM.

    IF STATUS NE 'B' AND LOEKZ NE 'X'.

      CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
           EXPORTING
              titel     = 'Check TECO'
              textline1 = 'PR has not converted to PO :'
              textline2 = BANFN
              start_column = 25
              start_row    = 6.

      RAISE COMPLETION_REJECTED.

*      MESSAGE E899(00) WITH 'PR Number '
*                             BANFN
*                           ' has not converted to PO.'
*                           RAISING COMPLETION_REJECTED.
    ENDIF.
ENDSELECT.

--------------------

*&---------------------------------------------------------------------*
*& Report  Z_FIND_USEREXIT                                             *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT z_find_userexit NO STANDARD PAGE HEADING.

*&---------------------------------------------------------------------*

*&  Enter the transaction code that you want to search through in order

*&  to find which Standard SAP User Exits exists.

*&

*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------*

*& Tables

*&---------------------------------------------------------------------*

TABLES : tstc,     "SAP Transaction Codes

         tadir,    "Directory of Repository Objects

         modsapt,  "SAP Enhancements - Short Texts

         modact,   "Modifications

         trdir,    "System table TRDIR

         tfdir,    "Function Module

         enlfdir,  "Additional Attributes for Function Modules

         tstct.    "Transaction Code Texts

*&---------------------------------------------------------------------*

*& Variables

*&---------------------------------------------------------------------*

DATA : jtab LIKE tadir OCCURS 0 WITH HEADER LINE.

DATA : field1(30).

DATA : v_devclass LIKE tadir-devclass.

*&---------------------------------------------------------------------*

*& Selection Screen Parameters

*&---------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK a01 WITH FRAME TITLE text-001.

SELECTION-SCREEN SKIP.

PARAMETERS : p_tcode LIKE tstc-tcode OBLIGATORY.

SELECTION-SCREEN SKIP.

SELECTION-SCREEN END OF BLOCK a01.

*&---------------------------------------------------------------------*

*& Start of main program

*&---------------------------------------------------------------------*

START-OF-SELECTION.

* Validate Transaction Code

  SELECT SINGLE * FROM tstc

    WHERE tcode EQ p_tcode.

* Find Repository Objects for transaction code

  IF sy-subrc EQ 0.

    SELECT SINGLE * FROM tadir

       WHERE pgmid    = 'R3TR'

         AND object   = 'PROG'

         AND obj_name = tstc-pgmna.

    MOVE : tadir-devclass TO v_devclass.

    IF sy-subrc NE 0.

      SELECT SINGLE * FROM trdir

         WHERE name = tstc-pgmna.

      IF trdir-subc EQ 'F'.

        SELECT SINGLE * FROM tfdir

          WHERE pname = tstc-pgmna.

        SELECT SINGLE * FROM enlfdir

          WHERE funcname = tfdir-funcname.

        SELECT SINGLE * FROM tadir

          WHERE pgmid    = 'R3TR'

            AND object   = 'FUGR'

            AND obj_name = enlfdir-area.

        MOVE : tadir-devclass TO v_devclass.

      ENDIF.

    ENDIF.

* Find SAP Modifactions

    SELECT * FROM tadir

      INTO TABLE jtab

      WHERE pgmid    = 'R3TR'

        AND object   = 'SMOD'

        AND devclass = v_devclass.

    SELECT SINGLE * FROM tstct

      WHERE sprsl EQ sy-langu

        AND tcode EQ p_tcode.

    FORMAT COLOR COL_POSITIVE INTENSIFIED OFF.

    WRITE:/(19) 'Transaction Code - ',

    20(20) p_tcode,

    45(50) tstct-ttext.

    SKIP.

    IF NOT jtab[] IS INITIAL.

      WRITE:/(95) sy-uline.

      FORMAT COLOR COL_HEADING INTENSIFIED ON.

      WRITE:/1 sy-vline,

      2 'Exit Name',

      21 sy-vline ,

      22 'Description',

      95 sy-vline.

      WRITE:/(95) sy-uline.

      LOOP AT jtab.

        SELECT SINGLE * FROM modsapt

        WHERE sprsl = sy-langu AND

        name = jtab-obj_name.

        FORMAT COLOR COL_NORMAL INTENSIFIED OFF.

        WRITE:/1 sy-vline,

        2 jtab-obj_name HOTSPOT ON,

        21 sy-vline ,

        22 modsapt-modtext,

        95 sy-vline.

      ENDLOOP.

      WRITE:/(95) sy-uline.

      DESCRIBE TABLE jtab.

      SKIP.

      FORMAT COLOR COL_TOTAL INTENSIFIED ON.

      WRITE:/ 'No of Exits:' , sy-tfill.

    ELSE.

      FORMAT COLOR COL_NEGATIVE INTENSIFIED ON.

      WRITE:/(95) 'No User Exit exists'.

    ENDIF.

  ELSE.

    FORMAT COLOR COL_NEGATIVE INTENSIFIED ON.

    WRITE:/(95) 'Transaction Code Does Not Exist'.

  ENDIF.

* Take the user to SMOD for the Exit that was selected.

AT LINE-SELECTION.

  GET CURSOR FIELD field1.

  CHECK field1(4) EQ 'JTAB'.

  SET PARAMETER ID 'MON' FIELD sy-lisel+1(10).

  CALL TRANSACTION 'SMOD' AND SKIP FIRST SCREEN.

Tuesday, November 9, 2010

PHP and IIS Preparation

Posted by Aries Wandari

PHP (ISAPI) and IIS Preparation
1. Copy php5ts.dll to C:\windows\system32
2. Tell php how to execute php files

On IIS, right click on web site > properties > click tab home directory > click configuration button.

image

image

Add application extension mapping, at executable field input c:\php\php5isapi.dll, at extension field input.php

image 
3. Add web service extension as picture below.

image
Insert extension name and required files.

image

Check “set extension status to allowed check box”.

image

Allowing index.php files to run as a directory default.

image
Add windows path c:\php\ext (right click on my computer > advanced > environment variables > system variables)

image
Check by running phpInfo function (tes.php) and see whether configuration and library is loaded normally.

 

TROUBLESHOOT

1. Error open folder for session.

clip_image002

adodb* is using tmp folder as session and cache store. We need to create the folder

Create tmp folder on E:\ (same drive of web application folder)

2. Error connect mssql.

clip_image004

MSSQL refuse connection of the application.

Right click on SQL Server > properties > tab Security > Authentification, SQL Server and Windows.

clip_image006

3. Configuration file php.ini located on c:\windows instead of its real location on c:\php

Its mean that system cant find config file and load default configuration. Need to add phprc system variables.

System variables:
Variable: PHPRC
Value: C:\php

Monday, October 25, 2010

Read Text Object

Posted by Aries Wandari

Today user request me to download Notification long text. Function READ_TEXT need text id, text name, text object as parameters. Below are steps to find those parameters.

call function 'READ_TEXT'
  EXPORTING
    id                      = ?
    language          = sy-langu
    name                = ?
    object               = ?
  TABLES
    lines                  = lines
  EXCEPTIONS
    id                       = 1
    language           = 2
    name                 = 3
    not_found          = 4
    object                 = 5
    reference_check = 6
    wrong_access_to_archive = 7.
In any long text editor, go to menu Go To > Header.
image
There is Text Name, Language, Text ID, Text Object.
image

Monday, October 18, 2010

Datetime to Character Conversion

Posted by Aries Wandari

Datetime or smalldatetime conversion to character data. Add 100 to a style value to get a four-place year that includes the century (yyyy).

Without century (yy)With century (yyyy)
Standard

Input/Output**
-0 or 100 (*) Defaultmon dd yyyy hh:miAM (or PM)
1101USAmm/dd/yy
2102ANSIyy.mm.dd
3103British/Frenchdd/mm/yy
4104Germandd.mm.yy
5105Italiandd-mm-yy
6106-dd mon yy
7107-Mon dd, yy
8108-hh:mm:ss
-9 or 109 (*) Default + millisecondsmon dd yyyy hh:mi:ss:mmmAM (or PM)
10110USAmm-dd-yy
11111JAPANyy/mm/dd
12112ISOyymmdd
-13 or 113 (*) Europe default + millisecondsdd mon yyyy hh:mm:ss:mmm(24h)
14114-hh:mi:ss:mmm(24h)
-20 or 120 (*) ODBC canonicalyyyy-mm-dd hh:mi:ss(24h)
-21 or 121 (*) ODBC canonical (with milliseconds)yyyy-mm-dd hh:mi:ss.mmm(24h)
-126(***)ISO8601yyyy-mm-dd Thh:mm:ss.mmm(no spaces)
-130*Hijri****dd mon yyyy hh:mi:ss:mmmAM
-131*Hijri****dd/mm/yy hh:mi:ss:mmmAM
Please check http://msdn.microsoft.com/en-us/library/aa226054(SQL.80).aspx for detail.

Tuesday, October 5, 2010

Programmer Team, Current Project

Posted by Aries Wandari

Programmer team current project.

Project
Description
Status
MSSRMaterial Selling System (non finish goods, waste, etc).Application done, waiting integration with SAP.
TLIMSWeb application replacing old LIMS.User trial.
Export TradingMaintain export trading data related to sales order, such as arrange
shipment, forwarder, forwarder commission, etc.
User trial.
Import non Trading (Procurement)Maintain import trading data (procurement import purchase order).Define user requirements.
Account Payable (AP) WorkflowTrack invoice status that accounting received from vendor.Workflow and application design done, go to coding.
SAP Server RelocationSAP server relocation currently abroad to local site.
SAP TradingSet new company code that will handle all corporate trading transaction. Since the company is registered as manufacture company, we have to set trading transaction separately from current system.Design organizational structure.

Current major system maintained by IT (besides SAP). Most are inhouse applications developed by our software programmer division team.

System
Development
Description
CAFinhouseContract System
- Manage company contract related to vendor.
CRDSinhouseCustomer Relation Database System
- Provide information about PT TITAN customer (integrated with SAP already), specific customer requirement for the product, competitor purchasing information, etc.
- Record technical and sales visit of our technical service staff.
- Process sample request, customer trial request from customer.
- Manage customer complaint or record customer satisfaction from customer.
EDSinhouseElectronic Document System
- Manage company document electronically such as ISO Document, etc.
ERMinhouseEnterprise Risk Management
- Manage risk of a job/project to ensure its safety aspects (identify risk, search for outcomes, etc).
HR SysteminhouseHR System for Employee
- Web base application to create/display/approve employee annual leave, business travel, attendance report.
- Display employee personal data, medical record history.
- Display company HR related form and publication (Internal Memorandum, Organization Notice, etc).
IT Support/IT HelpdeskinhouseIT Support System.
- Record application and database backup routine.
- Record any event/problem on server system, application, network, pabx, etc.
- Managing IT helpdesk request from user.
LIMSexternalLaboratory Information Management System
- Maintain experiment and laboratory data about characteristic of a product)
LDSinhouseLaboratory Data System
- Maintain laboratorium data related to products development and quality
control. The system consume LIMS data for a judge that a product is sold as Prime, Near Prime etc.
Online ApplicationinhouseWeb based application for Admin Service Department request for service, such as transportation request, meal, office stationaries, business card, uniform request.
PersonelinhouseHR System for HR Department
- Manage employee personal data,
- Maintain employee personal information data, family data, working experience, education,
annual leave, medical, training and position history.
- Recognition achieved and warning accepted from the company.
- Create employee work schedule, process employee absens.
PTServiceinhouseSAP to Local Database Data Transporter (SQL)
- A system to download SAP mature data to TPN local database server.
Corporate IntranetinhousePT TITAN Intranet web, provide corporate matters, department links and publication.
Also provide links to another application (Phone Book, Web Mail, HR System for Employee, etc)
ServicesinhouseAdmin Service System
- Process logistic operation such as vehicle kilometers and petrol entry, telephone usage,
laundry, employee meals.
TrackinginhouseHSE Related Software
- Software for tracking Safety related issue of the company. Such as record any incident report,
near miss, sub standard, safety talk performed, inspection walkthrough, ideas/suggestion, emergency
drill, housekeeping, HSE Comitte and HSE Audit, Non Conformity Report and Safety Assesment.
Weightbridge SystemexternalWeightbridge System
- A system that validate between real truck load with product weight shows on Delivery Order, in order to deliver finish product to customer.

Wednesday, August 4, 2010

Fingerprint Project

Posted by Aries Wandari

Pemasangan fingerprint untuk di beberapa site di plant untuk menggantikan reader absensi dengan proximity card, Jumat dan Sabtu 30-31 Juni 2010 (2 hari).

Point safety:
1. Safety induction untuk si pekerja.
2. Pemakaian perlengkapan safety.

a. Glasses
b. Helmet
c. Jaket lengan panjang
d. Safety Shoes
e. Body harness (khusus untuk pemasangan di gate 2)
3. Pembuatan permit berdasarkan tipe pekerjaan.

Deskripsi pekerjaan:
1. Pemasangan fingerprint yang melekat di tembok. Mungkin perlu melakukan pekerjaan bor dinding. Mungkin juga ada pekerjaan pengelasan untuk pemasangan di store.
2. Pemasangan (penarikan) kabel jaringan dari lokasi fingerprint ke hub terdekat.
3. Khusus untuk pemasangan di gate 2, ada pekerjaan di ketinggian +- 4 m di atas permukaan tanah untuk pemasangan koneksi relay.

Lokasi pemasangan:
1. Gate 2
2. Kantin
3. Store
4. Control Building

Note:
- Melihat waktu pekerjaan yang singkat (hanya 2 hari), item safety bisa dipinjam melalui admin secretary (untuk visitor). Untuk pekerjaan lebih lama dapat meminta ke HSE.
- Pekerjaan mengebor dinding memerlukan secondary hot permit. Sedangkan pekerjaan pengelasan/gerinda memerlukan primary hot permit (mungkin ada pekerjaan ini untuk pemasangan fingerprint di store, mengingat konstruksi pintunya yang terbuat dari besi).
- Pekerjaan mengebor dinding perlu konfirmasi pula ke bagian yang mengurus layout bangunan (Maint. Dept.) bila ada kemungkinan di dalam dinding tersebut terdapat instalasi kabel dsb.
- Permit pekerjaan dikeluarkan per area.
- Area gate 2,kantin dan store, permit dikeluarkan oleh Admin Service Dept.
- Area control building, permit dikeluarkan oleh shift supervisor.

Wednesday, July 21, 2010

(Few) SQL Function

Posted by Aries Wandari

Get first day of current year:
SELECT     *
FROM         tbl
WHERE     (tbl.datefield = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0))

Get last day of current year:
SELECT     *
FROM         tbl
WHERE     DATEADD(dd,-1,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0)))

Get first day of current month:
SELECT     *
FROM         tbl
WHERE     select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)

Get first day of last month:
SELECT     *
FROM         tbl
WHERE     DATEADD(mm, DATEDIFF(mm,0,DATEADD(mm,-1,getdate())), 0)

Get month part of a datetime field.
DATEPART(MONTH, A.AEDAT)

Get year part of a datetime field.
DATEPART(YEAR, A.AEDAT)

Cast field to char.
CAST(aField AS char(4))

Set 2 digit literal with trailing 0.
RIGHT('0' + RTRIM(MONTH(A.AEDAT)), 2)

Get Date Part Only of Datetime Field
CONVERT(varchar(8), GETDATE(), 112)

Get Time Part Only of Datetime Field
CONVERT(varchar(8), GETDATE(), 108)

Get Yesterday Date

DATEADD(DAY,DATEDIFF(DAY,'20000102',GETDATE()),'20000101')


And many else (from shanecooper.net) 
Monday of the Current Week
select DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)
Sunday of the Current Week
set DATEFIRST 1
select DATEADD(dd, 1 - DATEPART(dw, getdate()), getdate())
First Day of the Year
select DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
First Day of the Quarter
select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)
Midnight for the Current Day
select DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)
Last Day of Prior Month
select dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()  ), 0))
Last Day of Prior Year
select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()  ), 0))
Last Day of Current Month
select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate()  )+1, 0))
Last Day of Current Year
select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()  )+1, 0))
First Monday of the Month
select DATEADD(wk, DATEDIFF(wk,0, dateadd(dd,6-datepart(day,getdate()),getdate()) ), 0)    

Tuesday, July 20, 2010

Instal/Uninstal Windows Service

Posted by Aries Wandari

Many customized SAP reports run slowly in SAP environment. So we make a bapi to download a mature data and store it on our sql server. The program run as service on the server and run in daily basis. We also profide small application so user can update the data on request.

To install service manually
  1. Go to directory in which installutill located (for me, in WINDOWS\Microsoft.NET\Framework\v2.0.50727).
  2. Run InstallUtil.exe with your exe as parameter. Enter code below:
    installutil abc.exe


To uninstall service manually



  • Run InstallUtil.exe with your exe as a parameter. Enter code below:


    installutil /u abc.exe

Monday, July 19, 2010

SAP Error in spool C call: spool overflow

Posted by Aries Wandari

Error in spool C call: spool overflow
t.code SPAD
menu path: Administration > Clean Spool

Add Full Trust Location on VS2008

Posted by Aries Wandari

Im developing .net application in my local computer. Because we’ll develop it in team, I have to save it in share forlder so any else developer can access. Then come this error when I run the application.

Message=”Request for the permission of type ‘System.Data.OleDb.OleDbPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ failed.”
Its because .net dont trust developer location. Run caspol.exe (in directory C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727) to add developer trust site.
D:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>caspol.exe -pp off -m -ag 1.2 –url file:w:\* FullTrust
image

Monday, June 28, 2010

BAPI_SALESORDER_CREATEFROMDAT2

Posted by Aries Wandari

     Im on the way creating waste selling on intranet. User will input material that will be sold, do some level approval and create sales order in SAP. Im using BAPI_SALESORDER_CREATEFROMDAT2 to create sales order. Test using the function is ok (sales document number diplayed). But when I try to display the SO via VA03, error ‘SD document xxx is not in the database or has been archived’ displayed.
Function test displaying sales order successfully created.
image

To handle this, we  need to commit the transaction by running BAPI_TRANSACTION_COMMIT.

Sequence test on creating sales order and commit transaction:
On function module SE37, click menu Function Module > Test > Test sequence, input BAPI_SALESORDER_CREATEFROMDAT2 and BAPI_TRANSACTION_COMMIT, then run.

Example of abap program testing the function:

*&---------------------------------------------------------------------*
*& Report  ZCREATESALESDOC                                             *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*
REPORT  ZCREATESALESDOC                         .
DATA: order_header_in LIKE standard table of bapisdhd1 with header line.
DATA: order_items_in TYPE STANDARD TABLE OF bapisditm WITH HEADER LINE.
DATA: order_schedules_in
TYPE STANDARD TABLE OF bapischdl WITH HEADER LINE.
DATA: order_partners TYPE STANDARD TABLE OF bapiparnr WITH HEADER LINE.
DATA: return TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE.
data: itemx like bapisditmx occurs 10 with header line.
order_header_in-doc_type = 'ZORL'.
order_header_in-sales_org = 'ID10'.
order_header_in-distr_chan = 'DC'.
order_header_in-division = '00'.
order_header_in-purch_date = sy-datum.
order_header_in-purch_no_c = 'BAPITEST'.
APPEND order_header_in.
order_partners-partn_role = 'AG'.
order_partners-partn_numb = '0000100080'.
APPEND order_partners.
order_partners-partn_role = 'WE'.
order_partners-partn_numb = '0000100082'.
APPEND order_partners.
*req deliv date, pricing date
*payment terms, incoterms
*add next line with more than one item
order_items_in-itm_number = '000010'.
order_items_in-material = 'LL0209AA'.
order_items_in-target_qty = '1000'.
order_items_in-target_qu = 'KG'.
order_items_in-sales_unit = 'KG'.
APPEND order_items_in.
order_schedules_in-itm_number = '000010'.
order_schedules_in-req_qty = '1000'.
APPEND order_schedules_in.
*Add following lines with more than one line
itemx-itm_number = '000010'.
itemx-updateflag = 'I'.
itemx-material = 'X'.
itemx-target_qty = 'X'.
append itemx.
*stop add more lines
REFRESH return.
*BREAK-POINT.
CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'
EXPORTING
  sales_header_in = order_header_in
TABLES
  return = return
  sales_items_in = order_items_in
  sales_items_inx = itemx
  sales_partners = order_partners
  sales_schedules_in = order_schedules_in.
COMMIT WORK.
LOOP AT return.
WRITE: / return-message.
ENDLOOP.

Wednesday, June 9, 2010

Non Valuated Material Stock

Posted by Aries Wandari

     Something interesting found in this non valuated material. I’ve GR non val material 1700000015 for 15 EA. But in my customized report ZMMF001, the total stock is not updated. But in SAP standard report MB52, the quantity is updated. In report ZMMF001 I use account MBEW-LBKUM as a reference. While on MB52 it use MARD-LABST as reference. This show that non valuated material is not using this field.
clip_image002
clip_image004
MM03 > Accounting1, field Total Stock (MBEW-LBKUM)
clip_image006
MM03 > Plant stock, field Unsres.use stock (MARD-LABST)
clip_image008

Freeze PO on Full Release

Posted by Aries Wandari

One of procurement audit finding is, PO release has to be reset whether PO value increase or decrease. Old fashion way, PO release only reset if total value is increase only. Testing and googling shows that there is no way PO release reset if total value is decrease. So we choose freeze PO on its fully release state.
SPRO > IMG > Materials Mangement > Purchasing > Purchase Order > Release Procedure for Purchase Orders > Define Release Procedure for Purchase Orders > Release Indicator
clip_image002
But then problem occur. If user want to edit the PO, it has to be un-released first. And still PO cant be un-released if PO has been issued (error is ‘Document has already been outputted… ’). We still waiting the solution from SAP. Or we need to create new different procurement procedure for this. Or, another solution is using user-exit.
clip_image004

SAP PM Miscellaneous

Posted by Aries Wandari

ALV Report Template

Posted by Aries Wandari

*&---------------------------------------------------------------------*
*& Report  ZMMF008                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*
REPORT  ZMMF008                                 .
*========================== Global definitions  ====================*
**
* Data Types
**

type-pools: slis.
types: begin of tp_data,
b_mandt like eban-mandt,
b_banfn like eban-banfn,     "Purchase requisition
b_bnfpo like eban-bnfpo,     "Item of requisition
b_bsart like eban-bsart,     "Document type
b_loekz like eban-loekz,     "Deletion indicator
*b_statu like eban-statu,     "Processing status
*b_estkz like eban-estkz,     "Creation indicator
b_frgkz like eban-frgkz,     "Release indicator
b_frgzu like eban-frgzu,     "Release status
b_frgst like eban-frgst,     "Release strategy
b_ekgrp like eban-ekgrp,     "Purchasing Group
b_ernam like eban-ernam,     "Created by
*b_erdat like eban-erdat,     "Changed on
b_afnam like eban-afnam,     "Requisitioner
b_txz01 like eban-txz01,     "Short text
b_matnr like eban-matnr,     "Material
b_werks like eban-werks,     "Plant
*b_lgort like eban-lgort,     "Storage Location
b_bednr like eban-bednr,     "Req. Tracking Number
*b_matkl like eban-matkl,     "Material Group
b_menge like eban-menge,     "Quantity requested
b_meins like eban-meins,     "Unit of measure
b_badat like eban-badat,     "Requisition date
*b_lfdat like eban-lfdat,     "Delivery date
*b_frgdt like eban-frgdt,     "Release date
*b_webaz like eban-webaz,     "GR processing time
b_preis like eban-preis,     "Valuation price
b_peinh like eban-peinh,     "Price unit
*b_pstyp like eban-pstyp,     "Item category
*b_knttp like eban-knttp,     "Acct assignment cat.
*b_wepos like eban-wepos,     "Goods receipt
*b_weunb like eban-weunb,     "GR non-valuated
*b_repos like eban-repos,     "Invoice receipt
*b_lifnr like eban-lifnr,     "Desired vendor
*b_flief like eban-flief,     "Fixed vendor
b_ekorg like eban-ekorg,     "Purch. Organization
*b_vrtyp like eban-vrtyp,     "Purch. doc. category
b_ebeln like eban-ebeln,     "Purchase order
b_ebelp like eban-ebelp,     "Purchase order item
*b_bedat like eban-bedat,     "Purchase order date
*b_bsmng like eban-bsmng,     "Quantity ordered
b_ebakz like eban-ebakz,     "Closed
b_frggr like eban-frggr,     "Release group
*b_frgrl like eban-frgrl,     "Subject to release
b_waers like eban-waers,     "Currency
b_arsnr like eban-arsnr,     "Settlement res. no.
b_arsps like eban-arsps,     "Item settlem.reser.
b_rlwrt like eban-rlwrt,     "Total val. upon release
p_ebeln like ekpo-ebeln,     "Purchasing Document
p_ebelp like ekpo-ebelp,     "Item
p_loekz like ekpo-loekz,     "Deletion indicator
*p_statu like ekpo-statu,     "RFQ status
*p_aedat like ekpo-aedat,     "Last changed on
*p_txz01 like ekpo-txz01,     "Short text
*p_matnr like ekpo-matnr,     "Material
p_bukrs like ekpo-bukrs,     "Company Code
*p_werks like ekpo-werks,     "Plant
*p_lgort like ekpo-lgort,     "Storage Location
*p_bednr like ekpo-bednr,     "Req. Tracking Number
*p_matkl like ekpo-matkl,     "Material Group
p_menge like ekpo-menge,     "Order quantity
p_meins like ekpo-meins,     "Order unit
*p_bprme like ekpo-bprme,     "Order Price Unit
*p_bpumz like ekpo-bpumz,     "Quantity conversion
p_bpumn like ekpo-bpumn,     "Quantity conversion
*p_umrez like ekpo-umrez,     "Equal To
*p_umren like ekpo-umren,     "Denominator
p_netpr like ekpo-netpr,     "Net order price
p_peinh like ekpo-peinh,     "Price unit
p_netwr like ekpo-netwr,     "Net order value
p_brtwr like ekpo-brtwr,     "Gross order value
*p_uebto like ekpo-uebto,     "Overdeliv. tolerance
*p_uebtk like ekpo-uebtk,     "Unltd overdelivery
*p_untto like ekpo-untto,     "Underdel. tolerance
*p_elikz like ekpo-elikz,     "Delivery Completed
*p_knttp like ekpo-knttp,     "Acct assignment cat.
*p_wepos like ekpo-wepos,     "Goods receipt
*p_weunb like ekpo-weunb,     "GR non-valuated
*p_repos like ekpo-repos,     "Invoice receipt
*p_webre like ekpo-webre,     "GR-based inv. verif.
*p_bstyp like ekpo-bstyp,     "Purch. doc. category
*p_plifz like ekpo-plifz,     "Planned deliv. time
*p_banfn like ekpo-banfn,     "Purchase requisition
*p_bnfpo like ekpo-bnfpo,     "Item of requisition
*p_mtart like ekpo-mtart,     "Material Type
*p_afnam like ekpo-afnam,     "Requisitioner
p_anfnr like ekpo-anfnr,
k_frgke like ekko-frgke,
k_frggr like ekko-frggr,
k_frgsx like ekko-frgsx,
prx1date like cdhdr-udate,
prx1time like cdhdr-utime,
prx2date like cdhdr-udate,
prx2time like cdhdr-utime,
prx3date like cdhdr-udate,
prx3time like cdhdr-utime,
prx4date like cdhdr-udate,
prx4time like cdhdr-utime,
prx5date like cdhdr-udate,
prx5time like cdhdr-utime,
prfnldate like cdhdr-udate,
prfnltime like cdhdr-utime,
pox1date like cdhdr-udate,
pox1time like cdhdr-utime,
pox2date like cdhdr-udate,
pox2time like cdhdr-utime,
pox3date like cdhdr-udate,
pox3time like cdhdr-utime,
pox4date like cdhdr-udate,
pox4time like cdhdr-utime,
pofnldate like cdhdr-udate,
pofnltime like cdhdr-utime,
prrel_cd_tx1 like BAPIRLCORQ-REL_CD_TX1,
prrel_cd_tx2 like BAPIRLCORQ-REL_CD_TX2,
prrel_cd_tx3 like BAPIRLCORQ-REL_CD_TX3,
prrel_cd_tx4 like BAPIRLCORQ-REL_CD_TX4,
prrel_cd_tx5 like BAPIRLCORQ-REL_CD_TX5,
porel_cd_tx1 like BAPIRLCORQ-REL_CD_TX1,
porel_cd_tx2 like BAPIRLCORQ-REL_CD_TX2,
porel_cd_tx3 like BAPIRLCORQ-REL_CD_TX3,
porel_cd_tx4 like BAPIRLCORQ-REL_CD_TX4,
porel_cd_tx5 like BAPIRLCORQ-REL_CD_TX5,
days01 type i, "days calculate PO final release - PR final release
weeks type i, "days01 as weeks
days01str(5) type c, "days01 in str
weekstr(5) type c, "weeks in str
itemtx(50) type C,           "item text
mark,
       end of tp_data,
       tp_tbl_data type standard table of tp_data.
types: begin of chgtype,
changenr like cdhdr-changenr,
mandant like cdhdr-mandant,
objectid like cdhdr-objectid,
udate like cdhdr-udate,
utime like cdhdr-utime,
tcode like cdhdr-tcode,
objectclas like cdpos-objectclas,
tabname  like cdpos-tabname,
tabkey  like cdpos-tabkey,
fname  like cdpos-fname,
value_old  like cdpos-value_old,
value_new like cdpos-value_new,
end of chgtype.

**
* Constants
**

**
* Data objects (variable declarations and definitions)
**

data: irel_final like BAPIRLCORQ occurs 5 with header line.
data: ihdr type sorted table of chgtype with unique key changenr,
      ipos type sorted table of chgtype with non-unique key changenr.
*      ipos type sorted table of chgtype with non-unique key value_new with header line. "error sort value_new

* Report data to be shown. data:
    it_data type standard table of tp_data,
    it_data_pr type standard table of tp_data,
    it_data_po type standard table of tp_data,
    it_final type standard table of tp_data
*    it_data_gr type standard table of tp_data,
*    it_data_inv type standard table of tp_data
*    it_data2 type sorted table of tp_data with non-unique key b_banfn b_bnfpo p_ebeln p_ebelp k_bewtp
.
data: wa type tp_data.
data: wahdr type chgtype, wapos type chgtype, wahdrpos type chgtype.
data: flag type i, l_from type i, prtabkey like CDPOS-tabkey, potabkey like CDPOS-tabkey.
* Heading of the report.
data: t_heading type slis_t_listheader.
FIELD-SYMBOLS: <fs> type tp_data.
*======================= Selection Screen ==========================*
selection-screen:
begin of block b1 with frame title text-t01.
DATA: w_aux_werks like eban-werks
,w_aux_badat like eban-badat
,w_aux_banfn like eban-banfn
,w_aux_ebeln like ekpo-ebeln
,w_aux_matnr like eban-matnr
,w_aux_matkl like eban-matkl
,w_aux_ernam like eban-ernam
,w_aux_bednr like eban-bednr
.
SELECT-OPTIONS s_werks for w_aux_werks obligatory no intervals default '5201'.
SELECT-OPTIONS s_badat for w_aux_badat.
SELECT-OPTIONS s_banfn for w_aux_banfn.
SELECT-OPTIONS s_ebeln for w_aux_ebeln.
SELECT-OPTIONS s_matnr for w_aux_matnr.
SELECT-OPTIONS s_matkl for w_aux_matkl.
SELECT-OPTIONS s_ernam for w_aux_ernam.
SELECT-OPTIONS s_bednr for w_aux_bednr.
selection-screen: end of block b1.
*======================== Event Blocks =============================*
at selection-screen.
start-of-selection.
  perform get_data_PR using it_data_pr.
  perform get_data_PO using it_data_pr it_data_po.
  perform get_reldt_PRPO using it_data_po it_final.

  it_data = it_final.
*  perform get_data_INV using it_data_gr it_data_inv.
*LOOP AT it_data_pr ASSIGNING <fs>.
*ENDLOOP.
end-of-selection.
  perform build_alv using it_data t_heading.
*======================== Subroutines ==============================*
*&------------------------------------------------------------------*
*&      Form  get_data
*&------------------------------------------------------------------*
*       Gets the information to be shown in the report.
*-------------------------------------------------------------------*

form get_data_PR using data_pr type tp_tbl_data.
  clear wa.
  unassign <fs>.
  SELECT
  mandt banfn bnfpo bsart loekz frgkz frgzu frgst ekgrp ernam afnam
  txz01 matnr werks bednr menge meins badat preis peinh ekorg
  ebeln ebelp ebakz frggr waers arsnr arsps rlwrt
  from eban
  into
  (wa-b_mandt,
   wa-b_banfn, wa-b_bnfpo, wa-b_bsart, wa-b_loekz, wa-b_frgkz,
   wa-b_frgzu, wa-b_frgst, wa-b_ekgrp, wa-b_ernam, wa-b_afnam,
   wa-b_txz01, wa-b_matnr, wa-b_werks, wa-b_bednr, wa-b_menge,
   wa-b_meins, wa-b_badat, wa-b_preis, wa-b_peinh, wa-b_ekorg,
   wa-b_ebeln, wa-b_ebelp, wa-b_ebakz, wa-b_frggr, wa-b_waers,
   wa-b_arsnr, wa-b_arsps, wa-b_rlwrt)
  where
  werks in s_werks
  AND badat in s_badat
  AND banfn in s_banfn
  AND matnr in s_matnr
  AND matkl in s_matkl
  AND ernam in s_ernam
  AND bednr in s_bednr
  .
       append wa to data_pr.
  endselect.
endform.                    " get_data
form get_data_PO using data_pr type tp_tbl_data
                 data_po type tp_tbl_data.
  clear wa. clear flag.
  if NOT data_pr is initial.
    loop at data_pr into wa.
      clear flag.
      select
      p~ebeln p~ebelp p~loekz p~bukrs p~menge
      p~meins p~bpumn p~netpr p~peinh p~netwr
      p~brtwr p~anfnr
      k~frgke k~frggr k~frgsx
      into
      (wa-p_ebeln, wa-p_ebelp, wa-p_loekz, wa-p_bukrs, wa-p_menge,
       wa-p_meins, wa-p_bpumn, wa-p_netpr, wa-p_peinh, wa-p_netwr,
       wa-p_brtwr, wa-p_anfnr,
       wa-k_frgke, wa-k_frggr, wa-k_frgsx)
      from ekpo as p inner join ekko as k on p~ebeln = k~ebeln
      where banfn = wa-b_banfn and bnfpo = wa-b_bnfpo .
        flag = 1.
        append wa to data_po.
      endselect.
      if flag is initial.
        append wa to data_po..
      endif.
    endloop.
*    it_data = data_po.
  endif.
endform.
form get_reldt_PRPO using data type tp_tbl_data
                          final type tp_tbl_data.
.
  if NOT data is initial.
    loop at data into wa.
*get PR release step
      CALL FUNCTION 'ME_REL_INFO'
        EXPORTING
          i_frgkz = wa-b_frgkz
          i_frggr = wa-b_frggr
          i_frgst = wa-b_frgst
          i_frgot = '1'
          i_no_dialog = 'X'
        TABLES
          rel_final = irel_final
        .
      if not irel_final is initial.
        wa-prrel_cd_tx1 = irel_final-rel_cd_tx1.
        wa-prrel_cd_tx2 = irel_final-rel_cd_tx2.
        wa-prrel_cd_tx3 = irel_final-rel_cd_tx3.
        wa-prrel_cd_tx4 = irel_final-rel_cd_tx4.
        wa-prrel_cd_tx5 = irel_final-rel_cd_tx5.
      endif.
      clear irel_final. clear irel_final[].
*get PO release step
      CALL FUNCTION 'ME_REL_INFO'
        EXPORTING
          i_frgkz = wa-k_frgke
          i_frggr = wa-k_frggr
          i_frgst = wa-k_frgsx
          i_frgot = '1'
          i_no_dialog = 'X'
        TABLES
          rel_final = irel_final
      .
      if not irel_final is initial.
        wa-porel_cd_tx1 = irel_final-rel_cd_tx1.
        wa-porel_cd_tx2 = irel_final-rel_cd_tx2.
        wa-porel_cd_tx3 = irel_final-rel_cd_tx3.
        wa-porel_cd_tx4 = irel_final-rel_cd_tx4.
        wa-porel_cd_tx5 = irel_final-rel_cd_tx5.
      endif.
      clear irel_final. clear irel_final[].
*populate header to ihdr
      select changenr mandant objectid udate utime tcode
      into table ihdr
      from cdhdr CLIENT SPECIFIED
      where ( mandant = wa-b_mandt
      and objectclas = 'BANF'
      and objectid = wa-b_banfn
      AND TCODE in ('ME54N','ME55') )
      OR
      ( mandant = wa-b_mandt
      and objectclas = 'EINKBELEG'
      and objectid = wa-b_ebeln
      AND TCODE in ('ME29N','ME28') ).
      CONCATENATE wa-b_mandt wa-b_banfn wa-b_bnfpo INTO prtabkey.
      CONCATENATE wa-b_mandt wa-b_ebeln INTO potabkey.
      if not ihdr is initial.
*populate detail + header search to ipos, sorted by changenr
        select changenr objectid objectclas tabname tabkey fname value_old value_new
        into (wapos-changenr, wapos-objectid, wapos-objectclas, wapos-tabname,
        wapos-tabkey, wapos-fname, wapos-value_old, wapos-value_new)
        from cdpos
        for all entries in ihdr
        where ( objectid = ihdr-objectid
        and changenr = ihdr-changenr
        and objectclas = 'BANF'
        and tabname = 'EBAN'
        and tabkey = prtabkey
        and ( fname = 'FRGZU' or fname = 'FRGKZ' ) )
        or
        ( objectid = ihdr-objectid
        and changenr = ihdr-changenr
        and objectclas = 'EINKBELEG'
        and tabname = 'EKKO'
        and tabkey = potabkey
        and ( fname = 'FRGZU' OR fname = 'FRGKE' ) )
        .
          read table ihdr transporting no fields with key changenr = wapos-changenr binary search.
          if sy-subrc = 0.
            l_from = sy-tabix.
            loop at ihdr into wahdr from l_from where changenr = wapos-changenr.
              wapos-mandant = wahdr-mandant.
              wapos-objectid = wahdr-objectid.
              wapos-changenr = wahdr-changenr.
              wapos-udate = wahdr-udate.
              wapos-utime = wahdr-utime.
              wapos-tcode = wahdr-tcode.
            endloop.
          endif.
          append wapos to ipos.
        endselect.
*get PR PO release date
        loop at ipos into wapos.
          CASE wapos-objectclas.
            WHEN 'BANF'.
              CASE wapos-value_new.
                WHEN 'X'.
                  wa-prx1date = wapos-udate.
                  wa-prx1time = wapos-utime.
                  clear wa-prx2date. clear wa-prx3date. clear wa-prx4date. clear wa-prx5date.
                  clear wa-prx2time. clear wa-prx3time. clear wa-prx4time. clear wa-prx5time.
                WHEN 'XX'.
                  wa-prx2date = wapos-udate.
                  wa-prx2time = wapos-utime.
                  clear wa-prx3date. clear wa-prx4date. clear wa-prx5date.
                  clear wa-prx3time. clear wa-prx4time. clear wa-prx5time.
                WHEN 'XXX'.
                  wa-prx3date = wapos-udate.
                  wa-prx3time = wapos-utime.
                  clear wa-prx4date. clear wa-prx5date.
                  clear wa-prx4time. clear wa-prx5time.
                WHEN 'XXXX'.
                  wa-prx4date = wapos-udate.
                  wa-prx4time = wapos-utime.
                  clear wa-prx5date.
                  clear wa-prx5time.
                WHEN 'XXXXX'.
                  wa-prx5date = wapos-udate.
                  wa-prx5time = wapos-utime.
                WHEN '2'.
                  if wa-b_frgkz eq 2.
                    wa-prfnldate = wapos-udate.
                    wa-prfnltime = wapos-utime.
                  endif.
              ENDCASE.
            WHEN 'EINKBELEG'.
              CASE wapos-value_new.
                WHEN 'X'.
                  wa-pox1date = wapos-udate.
                  wa-pox1time = wapos-utime.
                  clear wa-pox2date. clear wa-pox3date. clear wa-pox4date.
                  clear wa-pox2time. clear wa-pox3time. clear wa-pox4time.
                WHEN 'XX'.
                  wa-pox2date = wapos-udate.
                  wa-pox2time = wapos-utime.
                  clear wa-pox3date. clear wa-pox4date.
                  clear wa-pox3time. clear wa-pox4time.
                WHEN 'XXX'.
                  wa-pox3date = wapos-udate.
                  wa-pox3time = wapos-utime.
                  clear wa-pox4date.
                  clear wa-pox4time.
                WHEN 'XXXX'.
                  wa-pox4date = wapos-udate.
                  wa-pox4time = wapos-utime.
                WHEN '2'.
                  if wa-k_frgke eq 2.
                    wa-pofnldate = wapos-udate.
                    wa-pofnltime = wapos-utime.
                  endif.
              ENDCASE.
          ENDCASE.
        endloop.
      endif. "end of if ihdr is initial
*days calculate PO final release - PR final release
      if ( not wa-pofnldate is initial ) and ( not wa-prfnldate is initial ).
*        wa-days01 = wa-pofnldate - wa-prfnldate.
*        wa-weeks = ( wa-days01 + 7 ) / 7.
        wa-days01str = wa-pofnldate - wa-prfnldate.
        wa-weekstr = ( wa-days01 + 7 ) / 7.
      endif.
      append wa to final.
      clear wa. clear wahdr. clear wapos. clear ihdr. clear ipos.
    endloop.
  endif.
endform.

*&------------------------------------------------------------------*
*&      Form  build_alv
*&------------------------------------------------------------------*
*       Builds and display the ALV Grid.
*-------------------------------------------------------------------*


form build_alv using t_data type tp_tbl_data
                     t_heading  type slis_t_listheader.
* ALV required data objects.
data: w_title   type lvc_title,
      w_repid   type syrepid,
      w_comm    type slis_formname,
      w_status  type slis_formname,
      x_layout  type slis_layout_alv,
      t_event    type slis_t_event,
      t_fieldcat type slis_t_fieldcat_alv,
      t_sort     type slis_t_sortinfo_alv.
refresh t_fieldcat.
refresh t_event.
refresh t_sort.
clear x_layout.
clear w_title.
* Field Catalog
  perform set_fieldcat2 using:
1  'B_BANFN' 'BANFN' 'EBAN' space space  space  space  space  space space 'X' space space space space t_fieldcat ,
3  'B_BNFPO' 'BNFPO' 'EBAN' space space  space  space  space  space space space space space space space t_fieldcat ,
4  'B_BADAT' 'BADAT' 'EBAN' space space  space  space  space  space space space space space space space t_fieldcat ,
5  'B_LOEKZ' 'LOEKZ' 'EBAN' 5 space  'Del' space  space  space space space space space space space t_fieldcat ,
6  'PRREL_CD_TX1'  ''      '' 15 space  'PRRel1By'  space  space  space space space space space space space t_fieldcat ,
9  'PRX1DATE'  ''      '' space space  'PRRel1Dt'  space  space  space space space space space space space t_fieldcat ,
12 'PRREL_CD_TX2'  ''      '' 15 space  'PRRel2By'  space  space  space space space space space space space t_fieldcat ,
15 'PRX2DATE'  ''      '' space space  'PRRel2Dt'  space  space  space space space space space space space t_fieldcat ,
18 'PRREL_CD_TX3'  ''      '' 15 space  'PRRel3By'  space  space  space space space space space space space t_fieldcat ,
21 'PRX3DATE'  ''      '' space space  'PRRel3Dt'  space  space  space space space space space space space t_fieldcat ,
24 'PRREL_CD_TX4'  ''      '' 15 space  'PRRel4By'  space  space  space space space space space space space t_fieldcat ,
27 'PRX4DATE'  ''      '' space space  'PRRel4Dt'  space  space  space space space space space space space t_fieldcat ,
30 'PRREL_CD_TX5'  ''      '' 15 space  'PRRel5By'  space  space  space space space space space space space t_fieldcat ,
33 'PRX5DATE'  ''      '' space space  'PRRel5Dt'  space  space  space space space space space space space t_fieldcat ,
34 'PRFNLDATE'  ''      '' space space  'PRFinalRelDt'  space  space  space space space space space space space t_fieldcat ,
36 'P_EBELN' 'EBELN' 'EKPO' space space  space  space  space  space space space space space space space t_fieldcat ,
39 'P_EBELP' 'EBELP' 'EKPO' space space  space  space  space  space space space space space space space t_fieldcat ,
40 'P_LOEKZ' 'LOEKZ' 'EKPO' 5 space  'Del' space  space  space space space space space space space t_fieldcat ,
42 'POREL_CD_TX1'  ''      '' 22 space  'PORel1By'  space  space  space space space space space space space t_fieldcat ,
45 'POX1DATE'  ''      '' space space  'PORel1Dt'  space  space  space space space space space space space t_fieldcat ,
48 'POREL_CD_TX2'  ''      '' 22 space  'PORel2By'  space  space  space space space space space space space t_fieldcat ,
51 'POX2DATE'  ''      '' space space  'PORel2Dt'  space  space  space space space space space space space t_fieldcat ,
54 'POREL_CD_TX3'  ''      '' 22 space  'PORel3By'  space  space  space space space space space space space t_fieldcat ,
57 'POX3DATE'  ''      '' space space  'PORel3Dt'  space  space  space space space space space space space t_fieldcat ,
60 'POREL_CD_TX4'  ''      '' 22 space  'PORel4By'  space  space  space space space space space space space t_fieldcat ,
63 'POX4DATE'  ''      '' space space  'PORel4Dt'  space  space  space space space space space space space t_fieldcat ,
64 'POFNLDATE'  ''      '' space space  'POFinalRelDt'  space  space  space space space space space space space t_fieldcat ,
67 'DAYS01STR'  ''      '' space space  'DaysProcessing'  space  space  space space space space space space space t_fieldcat ,
69 'WEEKSTR'  ''      '' space space  'WeeksProcessing'  space  space  space space space space space space space t_fieldcat
.

* Layout
x_layout-zebra = 'X'.x_layout-box_fieldname = 'MARK'.x_layout-box_tabname = 'IT_DATA'.
* Top of page heading
  perform set_top_page_heading using t_heading t_event.
* Events
  perform set_events using t_event.
* GUI Status
  w_status = ''.
  w_repid = sy-repid.
* Title
* w_title = <<If you want to set a title for
*             the ALV, please, uncomment and edit this line>>.
* User commands
  w_comm   = 'USER_COMMAND'.

* Data Sort
* Example
*  PERFORM set_order USING '<field>' 'IT_DATA' 'X' space space t_sort.
PERFORM set_order USING 'B_BANFN' 'IT_DATA' 'X' space space t_sort.
* Displays the ALV grid
  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      i_callback_program       = w_repid
      it_fieldcat              = t_fieldcat
      is_layout                = x_layout
      it_sort                  = t_sort
      i_callback_pf_status_set = w_status
      i_callback_user_command  = w_comm
      i_save                   = 'X'
      it_events                = t_event
      i_grid_title             = w_title
    tables
      t_outtab                 = t_data
    exceptions
      program_error            = 1
      others                   = 2.
  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
            with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.
endform.                    " build_alv.
*&------------------------------------------------------------------*
*&      Form  set_top_page_heading
*&------------------------------------------------------------------*
*       Creates the report headings.
*-------------------------------------------------------------------*
form set_top_page_heading using t_heading type slis_t_listheader
                                t_events  type slis_t_event.
data: x_heading type slis_listheader,
      x_event   type line of slis_t_event.
* Report title
  clear t_heading[].
  clear x_heading.
  x_heading-typ = 'H'.
  x_heading-info = ''(001).
  append x_heading to t_heading.
* Program name
  clear x_heading.
  x_heading-typ = 'S'.
  x_heading-key = 'Program: '.
  x_heading-info = sy-repid.
  append x_heading to t_heading.
* User who is running the report
  clear x_heading.
  x_heading-typ = 'S'.
  x_heading-key = 'User: '.
  x_heading-info = sy-uname.
  append x_heading to t_heading.
* Date of execution
  clear x_heading.
  x_heading-typ = 'S'.
  x_heading-key = 'Date: '.
  write sy-datum to x_heading-info.
  append x_heading to t_heading.
* Time of execution
  clear x_heading.
  x_heading-typ = 'S'.
  x_heading-key = 'Time: '.
  write sy-uzeit to x_heading-info.
  append x_heading to t_heading.
* Top of page event
  x_event-name = slis_ev_top_of_page.
  x_event-form = 'TOP_OF_PAGE'.
  append x_event to t_events.
endform.
*&------------------------------------------------------------------*
*&      Form  set_events
*&------------------------------------------------------------------*
*       Sets the events for ALV.
*       The TOP_OF_PAGE event is alredy being registered in
*       the set_top_page_heading subroutine.
*-------------------------------------------------------------------*
form set_events using t_events type slis_t_event.
data: x_event   type line of slis_t_event.
**
* Example
* -------
*  clear x_event.
*  x_event-name = .
*  x_event-form = .
*  append x_event to t_event.
**
endform.
*&------------------------------------------------------------------*
*&      Form  set_order
*&------------------------------------------------------------------*
*       Adds an entry to the order table.
*-------------------------------------------------------------------*
FORM set_order USING p_fieldname p_tabname p_up p_down p_subtot
                     t_sort TYPE slis_t_sortinfo_alv.
  DATA: x_sort TYPE slis_sortinfo_alv.
  CLEAR x_sort.
  x_sort-fieldname = p_fieldname.
  x_sort-tabname   = p_tabname.
  x_sort-up = p_up.
  x_sort-down = p_down.
  x_sort-subtot = p_subtot.
  APPEND x_sort TO t_sort.
ENDFORM.                    "set_order
*&------------------------------------------------------------------*
*&      Form  set_fieldcat2
*&------------------------------------------------------------------*
*       Adds an entry to the field catalog.
*    p_colpos: Column position.
*    p_fieldname: Field of internal table which is being described by
*          this record of the field catalog.
*    p_ref_fieldname: (Optional) Table field / data element which
*              describes the properties of the field.
*              If this field is not given, it is copied from
*              the fieldname.
*    p_ref_tabname: (Optional) Table which holds the field referenced
*            by <<p_ref_fieldname>>.
*                   If this is not given, the parameter
*                   <<p_ref_fieldname>> references a data element.
*    p_outputlen: (Optional) Column width.
*    p_noout: (Optional) If set to 'X', states that the field is not
*          showed initially. If so, the field has to be
*             included in the report at runtime using the display
*             options.
*    p_seltext_m: (Optional) Medium label to be used as column header.
*    p_seltext_l: (Optional) Long label to be used as column header.
*    p_seltext_s: (Optional) Small label to be used as column header.
*    p_reptext_ddic: (Optional) Extra small (heading) label to be
*              used as column header.
*    p_ddictxt: (Optional) Set to 'L', 'M', 'S' or 'R' to select
*               whether to use SELTEXT_L, SELTEXT_M, SELTEXT_S,
*               or REPTEXT_DDIC as text for column header.
*    p_hotspot: (Optional) If set to 'X', this field will be used
*            as a hotspot area for cursor, alolowing the user
*        to click on the field.
*    p_showasicon: (Optional) If set to 'X', this field will be shown
*                  as an icon and the contents of the field will set
*           which icon to show.
*    p_checkbox: (Optional) If set to 'X', this field will be shown
*                as a checkbox.
*    p_edit: (Optional) If set to 'X', this field will be editable.
*    p_dosum: (Optional) If set to 'X', this field will be summed
*             (aggregation function) according to the grouping set
*             by the order functions.
*    t_fieldcat: Table which contains the whole fieldcat.
*-------------------------------------------------------------------*
FORM set_fieldcat2 USING
      p_colpos p_fieldname p_ref_fieldname p_ref_tabname
      p_outputlen p_noout
      p_seltext_m p_seltext_l p_seltext_s p_reptext_ddic p_ddictxt
      p_hotspot p_showasicon p_checkbox p_edit
      p_dosum
      t_fieldcat TYPE slis_t_fieldcat_alv.
  DATA: wa_fieldcat TYPE slis_fieldcat_alv.
  CLEAR wa_fieldcat.
* General settings
  wa_fieldcat-fieldname = p_fieldname.
  wa_fieldcat-col_pos = p_colpos.
  wa_fieldcat-no_out = p_noout.
  wa_fieldcat-hotspot = p_hotspot.
  wa_fieldcat-checkbox = p_checkbox.
  wa_fieldcat-icon = p_showasicon.
  wa_fieldcat-do_sum = p_dosum.
* Set reference fieldname, tablenam and rollname.
* If p_ref_tabname is not given, the ref_fieldname given
*    is a data element.
* If p_ref_tabname is given, the ref_fieldname given is a
*    field of a table.
* In case ref_fieldname is not given,
*    it is copied from the fieldname.
  IF p_ref_tabname IS INITIAL.
    wa_fieldcat-rollname =   p_ref_fieldname.
  ELSE.
    wa_fieldcat-ref_tabname = p_ref_tabname.
    IF p_ref_fieldname EQ space.
      wa_fieldcat-ref_fieldname =   wa_fieldcat-fieldname.
    ELSE.
      wa_fieldcat-ref_fieldname =   p_ref_fieldname.
    ENDIF.
  ENDIF.
* Set output length.
  IF NOT p_outputlen IS INITIAL.
    wa_fieldcat-outputlen = p_outputlen.
  ENDIF.
* Set text headers.
  IF NOT p_seltext_m IS INITIAL.
    wa_fieldcat-seltext_m = p_seltext_m.
  ENDIF.
  IF NOT p_seltext_l IS INITIAL.
    wa_fieldcat-seltext_l = p_seltext_l.
  ENDIF.
  IF NOT p_seltext_s IS INITIAL.
    wa_fieldcat-seltext_s = p_seltext_s.
  ENDIF.
  IF NOT p_reptext_ddic IS INITIAL.
    wa_fieldcat-reptext_ddic = p_reptext_ddic.
  ENDIF.
  IF NOT p_ddictxt IS INITIAL.
    wa_fieldcat-ddictxt = p_ddictxt.
  ENDIF.
* Set as editable or not.
  IF NOT p_edit IS INITIAL.
    wa_fieldcat-input     = 'X'.
    wa_fieldcat-edit     = 'X'.
  ENDIF.
  APPEND wa_fieldcat TO t_fieldcat.
ENDFORM.                   "set_fieldcat2
*======================== Subroutines called by ALV ================*
*&------------------------------------------------------------------*
*&      Form  top_of_page
*&------------------------------------------------------------------*
*       Called on top_of_page ALV event.
*       Prints the heading.
*-------------------------------------------------------------------*
form top_of_page.
  call function 'REUSE_ALV_COMMENTARY_WRITE'
    exporting
*     i_logo             = <<If you want to set a logo, please,
*                          uncomment and edit this line>>
      it_list_commentary = t_heading.
endform.                    " alv_top_of_page

*&------------------------------------------------------------------*
*&      Form  user_command
*&------------------------------------------------------------------*
*       Called on user_command ALV event.
*       Executes custom commands.
*-------------------------------------------------------------------*


form user_command using r_ucomm     like sy-ucomm
                        rs_selfield type slis_selfield.

IF r_ucomm = '&IC1' AND
     rs_selfield-fieldname = 'EBELN' AND
     rs_selfield-value <> space.

    REFRESH bdcdata.
    CLEAR bdcdata.

    PERFORM bdc_dynpro      USING 'SAPLMEGUI' '0014'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=MECHOB'.

    PERFORM bdc_dynpro      USING 'SAPLMEGUI' '0002'.
    PERFORM bdc_field       USING 'BDC_CURSOR'
                                  'MEPO_SELECT-EBELN'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=MEOK'.
    PERFORM bdc_field       USING 'MEPO_SELECT-EBELN'
                                  rs_selfield-value.
    PERFORM bdc_field       USING 'MEPO_SELECT-BSTYP_F'
                                   'X'.
    CALL TRANSACTION 'ME23N' USING bdcdata MODE 'E'.
    CLEAR: rs_selfield.
endif.

endform.                    "user_command
*----------------------------------------------------------------------*
*        Start new screen                                              *
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
  CLEAR bdcdata.
  bdcdata-program  = program.
  bdcdata-dynpro   = dynpro.
  bdcdata-dynbegin = 'X'.
  APPEND bdcdata.
ENDFORM.                    "bdc_dynpro

*----------------------------------------------------------------------*
*        Insert field                                                  *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
  IF fval <> nodata_character.
    CLEAR bdcdata.
    bdcdata-fnam = fnam.
    bdcdata-fval = fval.
    APPEND bdcdata.
  ENDIF.
ENDFORM.                    "bdc_field

*----------------------------------------------------------------------*
*        Insert field                                                  *
*----------------------------------------------------------------------*
FORM bdc_nodata USING p_nodata.
  nodata_character = p_nodata.
ENDFORM.                    "bdc_nodata