Register Login

PR PURCHASE HISTORY ABAP Report

Updated May 18, 2018

*&---------------------------------------------------------------------&*
*&             Report  ZPO_PURCHASE_HISTORY                            &*
*&             Module : Material Management                            &*
*---------------------------------------------------------------------&*

report zpo_purchase_history no standard page heading message-id 00.

type-pools:slis.
types :begin of ty_po,
       banfn type eban-banfn,                 "Purchase Requisition Number
       bnfpo type eban-bnfpo,                 "Item Number of Purchase Requisition
       ekgrp type eban-ekgrp,                 "Purchasing Group
       badat type eban-badat,                 "Requisition (Request) Date
       menge type eban-menge,                 "Purchase Requisition Quantity
       meins type eban-meins,                 "Purchase Requisition Unit of Measure
       lifnr type ekko-lifnr,                 "Vendor Account Number
       bedat type ekko-bedat,                 "Purchasing Document Date
       ebeln type ekpo-ebeln,                 "Purchasing Document Number
       ebelp type ekpo-ebelp,                 "Item Number of Purchasing Document
       matkl type ekpo-matkl,                 "Material Group
       mtart type ekpo-mtart,                 "Material Type
       matnr type ekpo-matnr,                 "Material Number
       txz01 type ekpo-txz01,                 "Short Text
       menge1 type ekpo-menge,                "Purchase Order Quantity
       meins1 type ekpo-meins,                "Purchase Order Unit of Measure
       balqty type ekpo-menge,                "Balance Quantity
       netpr type ekpo-netpr,                 "Net Price in Purchasing Document
       peinh type ekpo-peinh,                 "Price Unit
       mblnr type mseg-mblnr,                 "Number of Material Document
       zeile type mseg-zeile,                 "Item in Material Document
       menge2 type mseg-menge,                "GR Quantity
       meins2 type mseg-meins,                "GR Unit of Measure
       werks type mseg-werks,                 "Plant
       charg type mseg-charg,                 "Batch
       belnr type rbkp-belnr,                 "Document Number of an Invoice Document
       bldat type rbkp-bldat,                 "Document Date in Document
       thick(10) type c,                      "Thickness
       width(10) type c,                      "Width
       length(10) type c,                     "Length
       grade(10) type c,                      "Grade
       end of ty_po.

types :begin of ty_ekko,
       ebeln type ekko-ebeln,
       lifnr type ekko-lifnr,
       bedat type ekko-bedat,
       end of ty_ekko.

types:begin of ty_ekpo,
       ebeln type ekpo-ebeln,
       ebelp type ekpo-ebelp,
       matnr type ekpo-matnr,
       txz01 type ekpo-txz01,
       menge type ekpo-menge,
       meins type ekpo-meins,
       netpr type ekpo-netpr,
       peinh type ekpo-peinh,
       banfn type ekpo-banfn,
       bnfpo type ekpo-bnfpo,
       mtart type ekpo-mtart,
       end of ty_ekpo.

types :begin of ty_eban,
       banfn type eban-banfn,
       bnfpo type eban-bnfpo,
       matnr type eban-matnr,
       menge type eban-menge,
       meins type eban-meins,
       end of ty_eban.

types : begin of ty_ekbe,
        ebeln type ekbe-ebeln,
        ebelp type ekbe-ebelp,
        belnr type ekbe-belnr,
        gjahr type ekbe-gjahr,
        buzei type ekbe-buzei,
        matnr type ekbe-matnr,
        end of ty_ekbe.

types : begin of ty_mseg,
        mblnr type mseg-mblnr,
        mjahr type mseg-mjahr,
        zeile type mseg-zeile,
        menge type mseg-menge,
        meins type mseg-meins,
        ebeln type mseg-ebeln,
        ebelp type mseg-ebelp,
        matnr type mseg-matnr,
        werks type mseg-werks,
        charg type mseg-charg,
        end of ty_mseg.

types : begin of ty_rbkp,
        belnr type rbkp-belnr,
        gjahr type rbkp-gjahr,
        bldat type rbkp-bldat,
        end of ty_rbkp.

types : begin of ty_rseg,
        belnr type rseg-belnr,
        gjahr type rseg-gjahr,
        ebeln type rseg-ebeln,
        ebelp type rseg-ebelp,
        end of ty_rseg.

data: it_po type standard table of ty_po,
      it_ekko type standard table of ty_ekko,
      it_ekpo type standard table of ty_ekpo,
      it_eban type standard table of ty_eban,
      it_ekbe type standard table of ty_ekbe,
      it_mseg type standard table of ty_mseg,
      it_rbkp type standard table of ty_rbkp,
      it_rseg type standard table of ty_rseg,
      wa_po type ty_po,
      wa_ekko type ty_ekko,
      wa_ekpo type ty_ekpo,
      wa_eban type ty_eban,
      wa_mseg type ty_mseg,
      wa_rbkp type ty_rbkp,
      wa_rseg type ty_rseg.

data: it_fcat type slis_t_fieldcat_alv,
      it_lshead type slis_t_listheader,
      it_sort type slis_t_sortinfo_alv,
      wa_fcat type slis_fieldcat_alv,
      wa_lshead type slis_listheader,
      wa_layout type slis_layout_alv,
      wa_sort type slis_sortinfo_alv.

data :it_cl_data like table of clobjdat,
      wa_cl_data like clobjdat.

data: c_matkl type ekpo-matkl,
      c_matnr type ekpo-matnr,
      c_ekgrp type eban-ekgrp,
      c_badat type eban-badat,
      c_index type sy-tabix,
      c_grmenge type mseg-erfmg,
      c_low(10) type c,
      c_high(10) type c,
      c_date type string.

*********Selection screen variables*********
selection-screen:begin of block b1 with frame title text-001.
select-options: s_matnr for c_matnr,
                s_matkl for c_matkl ,
                s_badat for c_badat obligatory,
                s_ekgrp for c_ekgrp.
parameters: ch_bal as checkbox.
selection-screen:end of block b1.

start-of-selection.
  perform getdata.
  perform setdata.
  perform fieldcat.
  perform display.
*&---------------------------------------------------------------------*
*&      Form  GETDATA
*&---------------------------------------------------------------------*
form getdata .

  select a~banfn
         a~bnfpo
         a~ekgrp
         a~badat
         b~ebeln
         b~ebelp
         b~matkl
         b~matnr
         into corresponding fields of table it_po
         from eban as a inner join ekpo as b
         on a~banfn = b~banfn and
            a~bnfpo = b~bnfpo
         where  a~badat in s_badat and
                a~ekgrp in s_ekgrp and
                b~matnr in s_matnr and
                b~matkl in s_matkl and
                b~loekz <> 'L' and
                a~loekz <> 'X'.

  if it_po[] is not initial.
    select ebeln
           ebelp
           matnr
           txz01
           menge
           meins
           netpr
           peinh
           banfn
           bnfpo
           mtart
           from ekpo into table it_ekpo
           for all entries in it_po
           where ebeln = it_po-ebeln and
                 ebelp = it_po-ebelp and
                 loekz <> 'L'.

    select banfn
           bnfpo
           matnr
           menge
           meins
           from eban into table it_eban
           for all entries in it_po
           where banfn = it_po-banfn and
                 bnfpo = it_po-bnfpo and
                 loekz <> 'X'.

    if it_ekpo[] is not initial.
      select ebeln
             lifnr
             bedat
             from ekko into table it_ekko
             for all entries in it_ekpo
             where ebeln = it_ekpo-ebeln.

      select ebeln
             ebelp
             belnr
             gjahr
             buzei
             matnr
             from ekbe into table it_ekbe
             for all entries in it_ekpo
             where ebeln = it_ekpo-ebeln and
                   ebelp = it_ekpo-ebelp .

      select belnr
             gjahr
             ebeln
             ebelp
             from rseg into table it_rseg
             for all entries in it_ekpo
             where ebeln = it_ekpo-ebeln and
                   ebelp = it_ekpo-ebelp.
    endif.

    if it_ekbe[] is not initial.
      select mblnr
             mjahr
             zeile
             menge
             meins
             ebeln
             ebelp
             matnr
             werks
             charg
             from mseg into table it_mseg
             for all entries in it_ekbe
             where mblnr = it_ekbe-belnr and
                   mjahr = it_ekbe-gjahr and
                   zeile = it_ekbe-buzei and
                   bwart = '101'.
    endif.

    if it_rseg[] is not initial.
      select belnr
             gjahr
             bldat
             from rbkp into table it_rbkp
             for all entries in it_rseg
             where belnr = it_rseg-belnr.
    endif.
  else.
    message s002.
    leave list-processing.
  endif.
endform.                    " GETDATA
*&---------------------------------------------------------------------*
*&      Form  SETDATA
*&---------------------------------------------------------------------*
form setdata .

  clear wa_po.
  loop at it_po into wa_po.
    c_index = sy-tabix.

********Calculate PR Quantity**********
    clear wa_eban.
    read table it_eban into wa_eban
               with key banfn = wa_po-banfn
                        bnfpo = wa_po-bnfpo.

    if sy-subrc eq 0.
      move:wa_eban-menge to wa_po-menge,
           wa_eban-meins to wa_po-meins.
    endif.

********Calculate PO Quantity**********
    clear wa_ekpo.
    read table it_ekpo into wa_ekpo
               with key banfn = wa_po-banfn
                        bnfpo = wa_po-bnfpo .

    if sy-subrc eq 0.
      move:wa_ekpo-txz01 to wa_po-txz01,
           wa_ekpo-netpr to wa_po-netpr,
           wa_ekpo-peinh to wa_po-peinh,
           wa_ekpo-mtart to wa_po-mtart,
           wa_ekpo-menge to wa_po-menge1,
           wa_ekpo-meins to wa_po-meins1.
    endif.

********Calculate Balance Quantity******
    clear wa_mseg.
    loop at it_mseg into wa_mseg
           where ebeln = wa_po-ebeln and
                 ebelp = wa_po-ebelp.
      c_grmenge = c_grmenge + wa_mseg-menge.
    endloop.

    move:wa_mseg-mblnr to wa_po-mblnr,
         c_grmenge to wa_po-menge2,
         wa_mseg-meins to wa_po-meins2,
         wa_mseg-werks to wa_po-werks,
         wa_mseg-charg to wa_po-charg.
    wa_po-balqty = wa_eban-menge - c_grmenge.

    clear : wa_rseg,wa_rbkp.
    read table it_rseg into wa_rseg with key
                   ebeln = wa_mseg-ebeln
                   ebelp = wa_mseg-ebelp.

    read table it_rbkp into wa_rbkp
               with key belnr = wa_rseg-belnr
                        gjahr = wa_rseg-gjahr.
    if sy-subrc eq 0.
      move : wa_rbkp-belnr to wa_po-belnr,
             wa_rbkp-bldat to wa_po-bldat.
    endif.

*********Assign Vendor,PO Date*********
    clear wa_ekko.
    read table it_ekko into wa_ekko
               with key ebeln = wa_po-ebeln.

    if sy-subrc eq 0.
      move:wa_ekko-lifnr to wa_po-lifnr,
           wa_ekko-bedat to wa_po-bedat.
    endif.

    call function 'ZSD_BATCH_CLASSIFICATION_DATA'
      exporting
        ch_charg                   = wa_po-charg
        ch_matnr                   = wa_po-matnr
        ch_werks                   = wa_po-werks
      tables
        cl_data                    = it_cl_data
*       I_SEL_CHARACTERISTIC       =
              .
    loop at it_cl_data into wa_cl_data.
      if wa_cl_data-ausp1 ne '?'.
        if wa_cl_data-atnam eq 'THICKNESS'.
          move wa_cl_data-ausp1 to wa_po-thick.

        elseif wa_cl_data-atnam eq 'LENGTH'.
          move wa_cl_data-ausp1 to wa_po-length.

        elseif wa_cl_data-atnam eq 'WIDTH'.
          move wa_cl_data-ausp1 to wa_po-width.

        elseif wa_cl_data-atnam eq 'GRADE'.
          move wa_cl_data-ausp1 to wa_po-grade.
        endif.
      endif.
    endloop.
    modify it_po from wa_po index c_index.
    clear :c_grmenge,wa_po,wa_ekpo,wa_mseg,c_index.
  endloop.
endform.                    " SETDATA
*&---------------------------------------------------------------------*
*&      Form  FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form fieldcat .
  perform buildfields using '1' 'BANFN'  'IT_PO' 'PR Number' '' .
  perform buildfields using '2' 'BNFPO'  'IT_PO' 'PR Item Number' '' .
  perform buildfields using '3' 'EKGRP'  'IT_PO' 'Purchase Group' '' .
  perform buildfields using '4' 'BADAT'  'IT_PO' 'Request Date' '' .
  perform buildfields using '5' 'MENGE'  'IT_PO' 'PR Quantity'  'X' .
  perform buildfields using '6' 'MEINS'  'IT_PO' 'PR Unit' '' .
  perform buildfields using '7' 'LIFNR'  'IT_PO' 'Vendor Number' '' .
  perform buildfields using '8' 'EBELN'  'IT_PO' 'Purchasing Doc No' '' .
  perform buildfields using '9' 'BEDAT'  'IT_PO' 'PO Date' '' .
  perform buildfields using '10' 'MTART' 'IT_PO' 'Material Type' '' .
  perform buildfields using '11' 'MATKL' 'IT_PO' 'Material Group' '' .
  perform buildfields using '12' 'MATNR' 'IT_PO' 'Material Number' '' .
  perform buildfields using '13' 'TXZ01' 'IT_PO' 'Material Desc' '' .
  perform buildfields using '14' 'THICK' 'IT_PO' 'Thickness' '' .
  perform buildfields using '15' 'WIDTH' 'IT_PO' 'width' '' .
  perform buildfields using '16' 'LENGTH' 'IT_PO' 'Length' '' .
  perform buildfields using '17' 'GRADE' 'IT_PO' 'Grade' '' .
  perform buildfields using '18' 'MENGE1' 'IT_PO' 'PO Quantity' 'X' .
  perform buildfields using '19' 'MEINS' 'IT_PO' 'PO Unit' '' .
  perform buildfields using '20' 'NETPR' 'IT_PO' 'Net Price' '' .
  perform buildfields using '21' 'PEINH' 'IT_PO' 'Price Unit' '' .
  perform buildfields using '22' 'MBLNR' 'IT_PO' 'GR Number' '' .
  perform buildfields using '23' 'MENGE2' 'IT_PO' 'GR Quantity' 'X' .
  perform buildfields using '24' 'MEINS2' 'IT_PO' 'GR Unit' '' .
  perform buildfields using '25' 'BELNR' 'IT_PO' 'GI number' '' .
  perform buildfields using '26' 'BLDAT' 'IT_PO' 'GI Date' '' .
  if ch_bal = 'X'.
    perform buildfields using '27' 'BALQTY' 'IT_PO' 'Balance Quantity' 'X' .
  endif.
endform.                    " FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  BUILDFIELDS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0449   text
*      -->P_0450   text
*      -->P_0451   text
*      -->P_0452   text
*----------------------------------------------------------------------*
form buildfields  using    value(p_col_pos) like sy-cucol
                           value(p_fldname) type slis_fieldname
                           value(p_tabname) type slis_tabname
                           value(p_reptext) like dd03p-reptext
                           value(p_do_sum) type char1.


  wa_fcat-col_pos = p_col_pos.
  wa_fcat-fieldname = p_fldname.
  wa_fcat-tabname = p_tabname.
  wa_fcat-reptext_ddic = p_reptext.
  wa_fcat-do_sum = p_do_sum.
  append wa_fcat to it_fcat.
  clear wa_fcat.

endform.                    " BUILDFIELDS
*&---------------------------------------------------------------------*
*&      Form  DISPLAY
*&---------------------------------------------------------------------*
form display .
  clear wa_layout.
  wa_layout-zebra = 'X'.
  wa_layout-colwidth_optimize = 'X'.

  perform build_sort using 'BANFN' '1' 'X'.

  call function 'REUSE_ALV_GRID_DISPLAY'
   exporting
*   I_INTERFACE_CHECK                 = ' '
*   I_BYPASSING_BUFFER                = ' '
*   I_BUFFER_ACTIVE                   = ' '
     i_callback_program               = sy-cprog
*   I_CALLBACK_PF_STATUS_SET          = ' '
*   I_CALLBACK_USER_COMMAND           = ' '
     i_callback_top_of_page           = 'TOP_OF_PAGE'
*   I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*   I_CALLBACK_HTML_END_OF_LIST       = ' '
*   I_STRUCTURE_NAME                  =
   i_background_id                   = 'ALV_BACKGROUND'
*   I_GRID_TITLE                      =
*   I_GRID_SETTINGS                   =
     is_layout                        = wa_layout
     it_fieldcat                      = it_fcat
*   IT_EXCLUDING                      =
*   IT_SPECIAL_GROUPS                 =
    it_sort                           = it_sort
*   IT_FILTER                         =
*   IS_SEL_HIDE                       =
*   I_DEFAULT                         = 'X'
*   I_SAVE                            = ' '
*   IS_VARIANT                        =
*   IT_EVENTS                         =
*   IT_EVENT_EXIT                     =
*   IS_PRINT                          =
*   IS_REPREP_ID                      =
*   I_SCREEN_START_COLUMN             = 0
*   I_SCREEN_START_LINE               = 0
*   I_SCREEN_END_COLUMN               = 0
*   I_SCREEN_END_LINE                 = 0
*   I_HTML_HEIGHT_TOP                 = 0
*   I_HTML_HEIGHT_END                 = 0
*   IT_ALV_GRAPHICS                   =
*   IT_HYPERLINK                      =
*   IT_ADD_FIELDCAT                   =
*   IT_EXCEPT_QINFO                   =
*   IR_SALV_FULLSCREEN_ADAPTER        =
* IMPORTING
*   E_EXIT_CAUSED_BY_CALLER           =
*   ES_EXIT_CAUSED_BY_USER            =
    tables
      t_outtab                          = it_po[]
 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.                    " DISPLAY

*&---------------------------------------------------------------------*
*&      Form  top_of_page
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form top_of_page.                                           "#EC *
**********Create report header*********
  refresh it_lshead.
  clear wa_lshead.
**********To display date in header*********
  write: s_badat-low to c_low dd/mm/yyyy,s_badat-high to c_high dd/mm/yyyy.
  if s_badat-high is initial.
    concatenate 'Dated on' c_low into c_date separated by space.
  else.
    concatenate 'Dated between' c_low 'and' c_high into c_date separated by space.
  endif.

  wa_lshead-typ = 'H'.
  wa_lshead-info = 'Pending Indents History - Material Type HR,ZINC and Others'.
  append wa_lshead to it_lshead.

  wa_lshead-typ = 'S'.
  wa_lshead-info = c_date.
  append wa_lshead to it_lshead.

  call function 'REUSE_ALV_COMMENTARY_WRITE'
    exporting
      it_list_commentary       = it_lshead
*      i_logo                   = 'SGL LOGO'
*     I_END_OF_LIST_GRID       =
*     I_ALV_FORM               =
            .
endform.                    "top_of_page
*&---------------------------------------------------------------------*
*&      Form  BUILD_SORT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_1072   text
*      -->P_1073   text
*      -->P_1074   text
*----------------------------------------------------------------------*
form build_sort  using    value(p_fldname) type slis_fieldname
                          value(p_spos) like alvdynp-sortpos
                          value(p_up) like alvdynp-sortup.
  wa_sort-fieldname = p_fldname.
  wa_sort-spos = p_spos.
  wa_sort-up = p_up.
  append wa_sort to it_sort.
  clear wa_sort.
endform.                    " BUILD_SORT


×