SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Daily sales report



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> SD
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Sat Sep 20, 2008 9:40 pm    Post subject: Daily sales report Reply with quote

Code:
report  zcra0002.
----------------------------------------------------------------------
tables : vbak,vbap.

----------------------------------------------------------------------
*TYPE-POOLS                                                           *
----------------------------------------------------------------------
type-pools slis .

*Sales Document: Header Data
data : begin of wt_vbak occurs 0,
           vbeln like vbak-vbeln,
           erdat like vbak-erdat,
           kunnr like vbak-kunnr,
       end of wt_vbak.

*Sales Document: Item Data
data : begin of wt_vbap occurs 0,
           vbeln like vbap-vbeln,
           posnr like vbap-posnr,
           matnr like vbap-matnr,
           arktx like vbap-arktx,
           kwmeng like vbap-kwmeng,
           werks like vbap-werks,
           erdat like vbak-erdat,
           kunnr like vbak-kunnr,
       end of wt_vbap.

*Sales Document Flow
data : begin of wt_vbfa occurs 0,
           vbelv like vbfa-vbelv,
           posnv like vbfa-posnv,
           vbeln like vbfa-vbeln,
           posnn like vbfa-posnn,
           vbtyp_n like vbfa-vbtyp_n,
       end of wt_vbfa.

*Sales Document Flow
data : begin of wt_vbfa1 occurs 0,
           vbelv like vbfa-vbelv,
           posnv like vbfa-posnv,
           vbeln like vbfa-vbeln,
           posnn like vbfa-posnn,
           vbtyp_n like vbfa-vbtyp_n,
       end of wt_vbfa1.

*SD Document: Delivery Header Data
data : begin of wt_likp occurs 0,
           vbeln like likp-vbeln,
           lfdat like likp-lfdat,
       end of wt_likp.

*SD document: Delivery: Item data
data : begin of wt_lips occurs 0,
           vbeln like lips-vbeln,
           posnr like lips-posnr,
           lfimg like lips-lfimg,
           meins like lips-meins,
       end of wt_lips.

*Delivery Note Additional Quantity (L20)
data : begin of wt_lipso2 occurs 0,
           vbeln like lipso2-vbeln,
           posnr like lipso2-posnr,
           msehi like lipso2-msehi,
           adqntp like lipso2-adqntp,
       end of wt_lipso2.

*Final Table for ALV Display
data : begin of wt_final occurs 0,
           erdat like vbak-erdat,
           vbeln like vbap-vbeln,
           posnr like vbap-posnr,
           kunnr like vbak-kunnr,
           name1 like kna1-name1,
           matnr like vbap-matnr ,
           arktx like vbap-arktx,
           kwmeng like vbap-kwmeng,
           werks like vbap-werks,
           delno like likp-vbeln,
           delitem like lips-posnr,
           deldate like likp-lfdat,
           delquan like lips-lfimg,
           uom     like lips-meins,
           altuom  like lipso2-msehi,
           quanl20 like lips-lfimg,
           invoice like vbrk-vbeln,
           invitem like vbrp-posnr,
       end of wt_final.

*Billing Document: Header Data
data : begin of wt_vbrk occurs 0,
           vbeln like vbrk-vbeln,
           fksto like vbrk-fksto,
       end of wt_vbrk.

*Customer Details
data : begin of wt_kna1 occurs 0,
           kunnr like kna1-kunnr,
           name1 like kna1-name1,
       end of wt_kna1.

----------------------------------------------------------------------
*ALV DATA                                                             *
----------------------------------------------------------------------
data :  ws_alvfield type slis_fieldcat_alv ,
        wt_fieldcat type slis_t_fieldcat_alv.

----------------------------------------------------------------------
*VARIABLES                                                            *
----------------------------------------------------------------------
*DATA : WV_

----------------------------------------------------------------------
*CONSTANTS                                                            *
----------------------------------------------------------------------
*CONSTANTS : WC_

----------------------------------------------------------------------
*PARAMETERS / SELECTION SCREEN                                        *
----------------------------------------------------------------------
selection-screen begin of block b1 with frame
                                   title text-001.
select-options  s_werks for vbap-werks.
parameters p_vkorg like vbak-vkorg obligatory.
select-options : s_vtweg for vbak-vtweg,
                 s_spart for vbak-spart,
                 s_erdat for vbak-erdat obligatory.
selection-screen end of block b1.

************************************************************************
*AT SELECTION-SCREEN                                                  *
************************************************************************
at selection-screen.

************************************************************************
*INITIALIZATION                                                       *
************************************************************************
initialization.

************************************************************************
*START-OF-SELECTION                                                   *
************************************************************************
start-of-selection.
  perform select_data.
  perform prepare_data.

************************************************************************
*END-OF-SELECTION                                                     *
************************************************************************
end-of-selection.
  perform display_data.

************************************************************************
*SUB ROUTINES                                                         *
************************************************************************

*---------------------------------------------------------------------
**      Form  select_data
*---------------------------------------------------------------------
form select_data .
  data wlt_vbak  like wt_vbak occurs 0 with header line.
  data wlt_vbfa1 like wt_vbfa1 occurs 0 with header line.
  select vbeln erdat kunnr
                     from vbak
                     into table wt_vbak
                    where erdat in s_erdat
                      and vkorg = p_vkorg
                      and vtweg in s_vtweg
                      and spart in s_spart.
  if sy-subrc = 0.
    wlt_vbak = wt_vbak.
    sort wlt_vbak by kunnr.
    delete adjacent duplicates from wlt_vbak
                    comparing kunnr.
    select kunnr name1 from kna1
                       into table wt_kna1
                        for all entries in wlt_vbak
                      where kunnr = wlt_vbak-kunnr.
    if sy-subrc = 0.
      sort wt_kna1 by kunnr.
    endif.
    sort wt_vbak by vbeln.
    select vbeln posnr matnr arktx kwmeng werks
                 from vbap
                 into table wt_vbap
                  for all entries in wt_vbak
                where vbeln = wt_vbak-vbeln
                  and werks in s_werks.
    if sy-subrc = 0.
      sort wt_vbap by vbeln posnr.
      select vbelv posnv vbeln posnn vbtyp_n
              into table wt_vbfa
              from vbfa
               for all entries in wt_vbap
             where vbelv = wt_vbap-vbeln
               and posnv = wt_vbap-posnr
               and vbtyp_n = 'J'.
      if sy-subrc = 0.
        sort wt_vbfa by vbeln posnn.
        delete adjacent duplicates from wt_vbfa
                        comparing vbeln posnn.
        select vbelv posnv vbeln posnn vbtyp_n
                into table wt_vbfa1
                from vbfa
                 for all entries in wt_vbfa
               where vbelv = wt_vbfa-vbeln
                 and posnv = wt_vbfa-posnn
                 and vbtyp_n = 'M'.
        if sy-subrc = 0.
          sort wt_vbfa1 by vbeln posnn.
          wlt_vbfa1] = wt_vbfa1[.
          sort wlt_vbfa1 by vbeln.
          delete adjacent duplicates from wlt_vbfa1
                          comparing vbeln.
          select vbeln fksto from vbrk
                             into table wt_vbrk
                              for all entries in wlt_vbfa1
                            where vbeln = wlt_vbfa1-vbeln.
        endif.
        select vbeln lfdat
                from likp
                into table wt_likp
                 for all entries in wt_vbfa
               where vbeln = wt_vbfa-vbeln.
        if sy-subrc = 0.
          sort wt_likp by vbeln.
        endif.
        select vbeln posnr lfimg meins
                from lips
                into table wt_lips
                 for all entries in wt_vbfa
               where vbeln = wt_vbfa-vbeln
                 and posnr = wt_vbfa-posnn.
        select vbeln posnr msehi adqntp
                   from lipso2
                   into table wt_lipso2
                    for all entries in wt_vbfa
                  where vbeln = wt_vbfa-vbeln
                    and posnr = wt_vbfa-posnn
                    and msehi in ('L','L20').

      endif.
    endif.
  endif.
endform.                    " select_data
*---------------------------------------------------------------------
**      Form  prepare_data
*---------------------------------------------------------------------
form prepare_data .
*Local Data
  data wlt_vbfa like wt_vbfa occurs 0 with header line.
  data : begin of wls_struct1,
             vbeln like vbap-vbeln,
             posnr like vbap-posnr,
         end of wls_struct1.

  data : begin of wls_struct2,
             vbeln like vbfa-vbelv,
             posnr like vbfa-posnv,
         end of wls_struct2.

  data : begin of wlt_delivery occurs 0,
               vbeln like lips-vbeln,
               posnr like lips-posnr,
               order like vbap-vbeln,
               oitem like vbap-posnr,
               lfimg like lips-lfimg,
               meins like lips-meins,
               altuom like lips-meins,
               quanl20 like lips-lfimg,
               lfdat  like likp-lfdat,
               invoice like vbrk-vbeln,
               invitem like vbrp-posnr,
         end of wlt_delivery.

  data wlv_index like sy-tabix.
  data wlv_index1 like sy-tabix.

  data : wlv_adqntpl   like lipso2-adqntp,
         wlv_adqntpl20 like lipso2-adqntp,
         wlv_factor    like lipso2-adqntp.
*Local Data ///

  sort wt_vbrk by vbeln.
  sort wt_vbfa1 by vbeln posnn.

*Delete the Cancelled Invoices
  wlv_index = 1.
  loop at wt_vbrk.
    loop at wt_vbfa1 from wlv_index.
      if wt_vbfa1-vbeln > wt_vbrk-vbeln.
        wlv_index = sy-tabix.
        exit.
      elseif wt_vbfa1-vbeln = wt_vbrk-vbeln.
        if wt_vbrk-fksto = 'X'.
          wt_vbfa1-vbeln = 'X'.
          modify wt_vbfa1 transporting vbeln.
          clear wt_vbfa1.
        endif.
      endif.
    endloop.
  endloop.
  delete wt_vbfa1 where vbeln = 'X'.

*Merge Invoices and Deliveries
  sort wt_vbfa1 by vbelv posnv.
  loop at wt_vbfa1.
    wlt_delivery-invoice = wt_vbfa1-vbeln.
    wlt_delivery-invitem = wt_vbfa1-posnn.
    wlt_delivery-vbeln = wt_vbfa1-vbelv.
    wlt_delivery-posnr = wt_vbfa1-posnv.
    append wlt_delivery.
    clear wlt_delivery.
  endloop.

*Merge Delivery Item details
  sort wlt_delivery by vbeln posnr.
  wlv_index = 1.
  loop at wt_lips.
    wls_struct1-vbeln = wt_lips-vbeln.
    wls_struct1-posnr = wt_lips-posnr.
    loop at wlt_delivery from wlv_index.
      wls_struct2-vbeln = wlt_delivery-vbeln.
      wls_struct2-posnr = wlt_delivery-posnr.
      if wls_struct2 > wls_struct1.
        wlv_index = sy-tabix.
        exit.
      elseif wls_struct2 = wls_struct1.
        wlt_delivery-lfimg = wt_lips-lfimg.
        wlt_delivery-meins = wt_lips-meins.
        modify wlt_delivery transporting lfimg meins.
        clear wlt_delivery.
      endif.
    endloop.
  endloop.

*Merge Sales Order with Delivery and Invoice
  wlv_index = 1.
  loop at wt_vbfa.
    wls_struct1-vbeln = wt_vbfa-vbeln.
    wls_struct1-posnr = wt_vbfa-posnn.
    loop at wlt_delivery from wlv_index.
      wls_struct2-vbeln = wlt_delivery-vbeln.
      wls_struct2-posnr = wlt_delivery-posnr.
      if wls_struct2 > wls_struct1.
        wlv_index = sy-tabix.
        exit.
      elseif wls_struct2 = wls_struct1.
        wlt_delivery-order = wt_vbfa-vbelv.
        wlt_delivery-oitem = wt_vbfa-posnv.
        modify wlt_delivery transporting order oitem
                                         lfimg meins.
        clear wlt_delivery.
      endif.
    endloop.
  endloop.

*Merge the Alternate UOM and Quantity in L20 with Delivery
  sort wlt_delivery by vbeln posnr.
  sort wt_lipso2 by vbeln.
  wlv_index = 1.
  loop at wlt_delivery.
    wlv_index1 = sy-tabix.
    wls_struct1-vbeln = wlt_delivery-vbeln.
    wls_struct1-posnr = wlt_delivery-posnr.
    loop at wt_lipso2 from wlv_index.
      wls_struct2-vbeln = wt_lipso2-vbeln.
      wls_struct2-posnr = wt_lipso2-posnr.
      if wls_struct2 > wls_struct1.
        wlv_index = sy-tabix.
        exit.
      elseif wls_struct2 = wls_struct1.
        if wt_lipso2-msehi = 'L'.
          wlv_adqntpl = wt_lipso2-adqntp.
        elseif wt_lipso2-msehi = 'L20'.
          wlv_adqntpl20 = wt_lipso2-adqntp.
        endif.
      endif.
    endloop.
    if not wlv_adqntpl20 is initial.
      wlt_delivery-altuom = 'L20'.
      wlt_delivery-quanl20 = ( wlv_adqntpl20 / wlv_adqntpl )
                               * wlt_delivery-lfimg.
      modify wlt_delivery index wlv_index1
                          transporting altuom quanl20.
    endif.
  endloop.

  sort wt_vbfa1 by vbelv posnv.
  sort wlt_delivery by vbeln posnr.

*Merge the Delivery Date from Header into Items
  wlv_index = 1.
  loop at wt_likp.
    loop at wlt_delivery from wlv_index.
      if wlt_delivery-vbeln > wt_likp-vbeln.
        wlv_index = sy-tabix.
        exit.
      elseif wlt_delivery-vbeln = wt_likp-vbeln.
        wlt_delivery-lfdat = wt_likp-lfdat.
        modify wlt_delivery transporting lfdat.
      endif.
    endloop.
  endloop.

*Final Table for Display
  sort wlt_delivery by order oitem.
  wlv_index = 1.
  loop at wlt_delivery.
    wt_final-vbeln = wlt_delivery-order.
    wt_final-posnr = wlt_delivery-oitem.
    wt_final-delno = wlt_delivery-vbeln.
    wt_final-delitem = wlt_delivery-posnr.
    wt_final-delquan = wlt_delivery-lfimg.
    wt_final-uom = wlt_delivery-meins.
    wt_final-altuom = wlt_delivery-altuom.
    wt_final-deldate = wlt_delivery-lfdat.
    wt_final-quanl20 = wlt_delivery-quanl20.
    wt_final-invoice = wlt_delivery-invoice.
    wt_final-invitem = wlt_delivery-invitem.
    append wt_final.
    clear wt_final.
  endloop.

*Merge Sales Header and Sales Items
  sort wt_final by vbeln posnr.
  wlv_index = 1.
  loop at wt_vbak.
    loop at wt_vbap from wlv_index.
      if wt_vbap-vbeln > wt_vbak-vbeln.
        wlv_index = sy-tabix.
        exit.
      elseif wt_vbap-vbeln = wt_vbak-vbeln.
        wt_vbap-erdat = wt_vbak-erdat.
        wt_vbap-kunnr = wt_vbak-kunnr.
        modify wt_vbap transporting erdat kunnr.
      endif.
    endloop.
  endloop.

*Merge Sales Order Details into Final Table
  wlv_index = 1.
  loop at wt_vbap.
    wls_struct1-vbeln = wt_vbap-vbeln.
    wls_struct1-posnr = wt_vbap-posnr.
    loop at wt_final from wlv_index.
      wls_struct2-vbeln = wt_final-vbeln.
      wls_struct2-posnr = wt_final-posnr.
      if wls_struct2 > wls_struct1.
        wlv_index = sy-tabix.
        exit.
      elseif wls_struct2 = wls_struct1.
        read table wt_kna1 with  key kunnr = wt_vbap-kunnr
                           binary search.
        if sy-subrc = 0.
          wt_final-name1 = wt_kna1-name1.
        endif.
          wt_final-erdat = wt_vbap-erdat.
          wt_final-matnr = wt_vbap-matnr.
          wt_final-arktx = wt_vbap-arktx.
          wt_final-werks = wt_vbap-werks.
          wt_final-kwmeng = wt_vbap-kwmeng.
          wt_final-kunnr = wt_vbap-kunnr.
          modify wt_final transporting erdat matnr
                          arktx kwmeng werks kunnr name1.
          clear wt_final.
          clear : wt_vbap-matnr,wt_vbap-posnr,wt_vbap-arktx,
                  wt_vbap-kwmeng,wt_vbap-werks.
        endif.
      endloop.
    endloop.
  endform.                    " prepare_data
*---------------------------------------------------------------------
**      Form  display_data
*---------------------------------------------------------------------
form display_data .
  data wlv_column like sy-cucol.
  data wls_layout    type   slis_layout_alv.
  data wlv_repid     like   sy-repid.
  data wlt_sort type slis_t_sortinfo_alv.
  data wls_sort type slis_sortinfo_alv.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'ERDAT'.
  ws_alvfield-seltext_l = text-011.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'VBELN'.
  ws_alvfield-seltext_l = text-012.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'POSNR'.
  ws_alvfield-seltext_l = text-013.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'KUNNR'.
  ws_alvfield-seltext_l = text-014.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'NAME1'.
  ws_alvfield-seltext_l = text-028.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'MATNR' .
  ws_alvfield-seltext_l = text-015.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'ARKTX' .
  ws_alvfield-seltext_l = text-016.
  ws_alvfield-do_sum = 'X'.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'KWMENG' .
  ws_alvfield-seltext_l = text-017.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'WERKS' .
  ws_alvfield-seltext_l = text-018.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'DELNO' .
  ws_alvfield-seltext_l = text-019.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'DELITEM' .
  ws_alvfield-seltext_l = text-020.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'DELDATE' .
  ws_alvfield-seltext_l = text-021.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'DELQUAN' .
  ws_alvfield-seltext_l = text-022.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'UOM' .
  ws_alvfield-seltext_l = text-023.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname =  'QUANL20'.
  ws_alvfield-seltext_l = text-024.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'ALTUOM' .
  ws_alvfield-seltext_l = text-025.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'INVOICE' .
  ws_alvfield-seltext_l = text-026.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wlv_column = wlv_column + 1.
  ws_alvfield-col_pos = wlv_column.
  ws_alvfield-fieldname = 'INVITEM' .
  ws_alvfield-seltext_l = text-027.
  append ws_alvfield to wt_fieldcat.
  clear ws_alvfield.

  wls_sort-fieldname =  'VBELN'.
  wls_sort-tabname   =  'WT_FINAL'.
  wls_sort-subtot    = 'X'.
  wls_sort-up   = 'X'.
  append wls_sort to wlt_sort.

************FILL THE LAYOUT****************************
  wlv_repid = sy-repid.
  wls_layout-colwidth_optimize    = 'X'.
  wls_layout-zebra                = 'X'.
  wls_layout-detail_initial_lines = 'X'.
  wls_layout-detail_popup         = 'X'.
  wls_layout-get_selinfos         = 'X'.
  wls_layout-group_change_edit    = 'X'.

  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      i_callback_program = wlv_repid
      is_layout          = wls_layout
      it_fieldcat        = wt_fieldcat
      it_sort            = wlt_sort
      i_default            = 'X'
      i_save             = 'X'
    tables
      t_outtab           = wt_final
    exceptions
      program_error      = 1
      others             = 2.
  if sy-subrc  0.
    message e005(zc).
  endif.
endform.                    " display_data
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> SD All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.