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