Make your own free website on Tripod.com

Table of Contents:


REPORT zvsales_by_shipto
       LINE-SIZE 83
       NO STANDARD PAGE HEADING.
*@----------------------------------------------------------------------
*@ Program id    :
*@
*@ Program Desc  : This Report lists Sales by ship-to customer.
*@
*@ Transaction Code - zvsalesbyshipto
*@ Input files   : ID - none
*@ Output files  : ID - none
*@
*@ Tables Updated: Table - none
*@
*@
*@ Author      : IJADALOW         LABK934444
*@ Date        : February 26, 2003
*@ Requested by: Joyce Homeniuk, Ophelia Tik
*@----------------------------------------------------------------------
*@----------------------------------------------------------------------

*-----------------------------------------------------------------------
* Includes
*-----------------------------------------------------------------------
INCLUDE zsmaster.

*-----------------------------------------------------------------------
* Tables
*-----------------------------------------------------------------------
TABLES: ce1anoc,
        adrc,
        vbpa,
        t001,    "Co code
        t009b.   "Fiscal year variant periods

** Structure
TABLES: zsalesbyshipto.

*-----------------------------------------------------------------------
* Internal Tables
*-----------------------------------------------------------------------
DATA: i_output LIKE zsalesbyshipto OCCURS 0 WITH HEADER LINE.

DATA: BEGIN OF itab OCCURS 0,
        kndnr LIKE ce1anoc-kndnr,               "sold-to
        name1 LIKE kna1-name1,
        perio LIKE ce1anoc-perio,
        zmonth LIKE zsalesbyshipto-zmonth,
        kaufn LIKE ce1anoc-kaufn,
        kdpos LIKE ce1anoc-kdpos,
        kunnr LIKE vbpa-kunnr,                  "Ship-to
        adrnr LIKE vbpa-adrnr,                  "address no.
        city1 LIKE adrc-city1,
        artnr LIKE ce1anoc-artnr,               "product
        arktx LIKE vbap-arktx,
        revenue LIKE zsalesbyshipto-revenue,
        erlpr LIKE ce1anoc-erlpr,
        erleq LIKE ce1anoc-erleq,
        rabat LIKE ce1anoc-rabat,
        kursf LIKE ce1anoc-kursf,
        drcos LIKE ce1anoc-drcos,
        totalpm LIKE zsalesbyshipto-totalpm,
        totalpmper LIKE zsalesbyshipto-totalpmper,
        absmg LIKE ce1anoc-absmg,
        belnr LIKE ce1anoc-belnr,
        vkorg LIKE ce1anoc-vkorg,
      END OF itab.

*----------------------------------------------------------------------
* Data for alv display
*----------------------------------------------------------------------
* Type pools
*----------------------------------------------------------------------
TYPE-POOLS: slis.

DATA: gs_layout TYPE slis_layout_alv.
DATA: fcat TYPE slis_fieldcat_alv OCCURS 0 WITH HEADER LINE.
DATA: gt_list_top_of_page TYPE slis_t_listheader.
DATA: gt_events TYPE slis_t_event.
DATA: alv_repid LIKE sy-repid.
DATA: alv_ddic LIKE dd02l-tabname.

CONSTANTS:
gc_formname_top_of_page TYPE slis_formname VALUE 'TOP_OF_PAGE'.

alv_ddic = 'ZSALESBYSHIPTO'.

*/-- Selection Criteria --------------------------------------------*\
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001.
SELECT-OPTIONS: s_comp  FOR ce1anoc-vkorg OBLIGATORY NO INTERVALS
                                                  NO-EXTENSION,
                s_cust  FOR vbpa-kunnr,                       "customer
                s_perio FOR ce1anoc-perio OBLIGATORY.         "Period

SELECTION-SCREEN END OF BLOCK blk1.

SELECTION-SCREEN BEGIN OF BLOCK blk2 WITH FRAME TITLE text-002.
SELECTION-SCREEN: SKIP.
PARAMETERS: rb_cad RADIOBUTTON GROUP rad1 DEFAULT 'X'.
PARAMETERS: rb_usd RADIOBUTTON GROUP rad1.
SELECTION-SCREEN END OF BLOCK blk2.
*/-------------------------------------------------------------------\*

START-OF-SELECTION.

  PERFORM get_data.

END-OF-SELECTION.

  mac_set_status_bar 'Preparing output......'.

* Layout
  PERFORM init_layout.
* Fieldcatalog
  PERFORM get_fieldcat.
  PERFORM init_fieldcat.
* Top of page comment
  REFRESH: gt_events, gt_list_top_of_page.
  PERFORM eventtab_build USING gt_events[].

  PERFORM output_list.

*/-------------------------------------------------------------------\*
FORM get_data.
  DATA: l_exrate LIKE itab-kursf,
        l_index like sy-tabix.
  SELECT *
    INTO CORRESPONDING FIELDS OF TABLE itab
    FROM ce1anoc
    WHERE perio IN s_perio
      AND vkorg IN s_comp
      AND kndnr IN s_cust.

  SORT itab BY kaufn kdpos artnr perio.

  LOOP AT itab.
    SELECT SINGLE name1 FROM kna1
       INTO itab-name1
      WHERE kunnr = itab-kndnr.

*   Get the ship-to customer and address num.
    SELECT SINGLE kunnr adrnr FROM vbpa
      INTO (itab-kunnr, itab-adrnr)
     WHERE vbeln = itab-kaufn
       AND parvw = 'WE'.

    SELECT SINGLE city1 FROM adrc
      INTO itab-city1
     WHERE addrnumber = itab-adrnr.
*   when the sales order number is '54xxxxxx', get the corresponding
*   order number from vbap.
    IF itab-kaufn(4) = '0054'.
      SELECT SINGLE vgbel vgpos arktx FROM vbap
        INTO (itab-kaufn, itab-kdpos, itab-arktx)
       WHERE vbeln = itab-kaufn
         AND posnr = itab-kdpos.
    ELSE.
      SELECT SINGLE arktx FROM vbap
        INTO itab-arktx
       WHERE vbeln = itab-kaufn
         AND posnr = itab-kdpos.
    ENDIF.

    CLEAR l_exrate.
    IF itab-kursf >= '1.0'.
      PERFORM get_exchange_rate USING itab-perio l_exrate.
    ELSE.
      l_exrate = itab-kursf.
    ENDIF.

    IF rb_cad = 'X'.
      itab-revenue = ( itab-erlpr + itab-erleq + itab-rabat ) /
                       l_exrate.
      itab-drcos = itab-drcos / l_exrate.
    ELSE.
      itab-revenue = ( itab-erlpr + itab-erleq + itab-rabat ).
    ENDIF.

    itab-totalpm = itab-revenue - itab-drcos.

*   Get Month name
    mac_get_month_str_for_period itab-vkorg itab-perio+4(3) itab-zmonth.

    CLEAR i_output.
    READ TABLE i_output WITH KEY kaufn = itab-kaufn
                          kdpos = itab-kdpos
                                 artnr = itab-artnr
                                 perio = itab-perio.
    l_index = sy-tabix.
    IF itab-perio = i_output-perio AND
       itab-kaufn = i_output-kaufn AND
       itab-kdpos = i_output-kdpos AND
       itab-artnr = i_output-artnr.

      ADD: i_output-revenue    TO itab-revenue,
           i_output-drcos      TO itab-drcos,
           i_output-totalpm    TO itab-totalpm,
           i_output-absmg      TO itab-absmg.
*     Get the Total PM percentage
      IF itab-revenue <> 0.
        itab-totalpmper = itab-totalpm / itab-revenue * 100.
      ELSE.
        itab-totalpmper = 0.
      ENDIF.

      MOVE-CORRESPONDING itab TO i_output.
      MODIFY i_output INDEX l_index.

    ELSE.

*     Get the Total PM percentage
      IF itab-revenue <> 0.
        itab-totalpmper = itab-totalpm / itab-revenue * 100.
      ELSE.
        itab-totalpmper = 0.
      ENDIF.

      MOVE-CORRESPONDING itab TO i_output.
      APPEND i_output.
    ENDIF.

    MODIFY itab.

  ENDLOOP.

  SORT i_output BY kndnr kunnr perio kaufn.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  INIT_LAYOUT
*&---------------------------------------------------------------------*
FORM init_layout.

  gs_layout-colwidth_optimize = 'X'.

ENDFORM.                    " INIT_LAYOUT

*&---------------------------------------------------------------------*
*&      Form  GET_FIELDCAT
*&---------------------------------------------------------------------*
FORM get_fieldcat.

  CLEAR fcat[].
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
       EXPORTING
            i_structure_name       = alv_ddic
       CHANGING
            ct_fieldcat            = fcat[]
       EXCEPTIONS
            inconsistent_interface = 1
            program_error          = 2
            OTHERS                 = 3.

ENDFORM.                    " GET_FIELDCAT

*&---------------------------------------------------------------------*
*&      Form  INIT_FIELDCAT
*&---------------------------------------------------------------------*
FORM init_fieldcat.

  LOOP AT fcat.
    fcat-ddictxt = 'L'.
    MODIFY fcat.
  ENDLOOP.

ENDFORM.                    " INIT_FIELDCAT

*&---------------------------------------------------------------------*
*&      Form  EVENTTAB_BUILD
*&---------------------------------------------------------------------*
FORM eventtab_build USING p_events TYPE slis_t_event.

  DATA: ls_event TYPE slis_alv_event.

  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
       EXPORTING
            i_list_type = 0
       IMPORTING
            et_events   = p_events.
  READ TABLE p_events WITH KEY name =  slis_ev_top_of_page
                           INTO ls_event.
  IF sy-subrc = 0.
    MOVE gc_formname_top_of_page TO ls_event-form.
    APPEND ls_event TO p_events.
  ENDIF.

ENDFORM.                    " EVENTTAB_BUILD

*---------------------------------------------------------------------*
*       FORM TOP_OF_PAGE                                              *
*---------------------------------------------------------------------*
FORM top_of_page.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
       EXPORTING
            it_list_commentary = gt_list_top_of_page.
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  OUTPUT_LIST
*&---------------------------------------------------------------------*
FORM output_list.

* sort i_output by vkorg kunnr pernr.

  alv_repid = sy-repid.

  CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
      EXPORTING
           i_callback_program       = alv_repid
*           i_callback_user_command  = 'USER_COMMAND'
           it_fieldcat              = fcat[]
           it_events                = gt_events[]
           is_layout                = gs_layout
       TABLES
            t_outtab                = i_output.

ENDFORM.                    " OUTPUT_LIST

*&---------------------------------------------------------------------*
*&      Form  GET_EXCHANGE_RATE
*&---------------------------------------------------------------------*
FORM get_exchange_rate USING pc_period CHANGING pc_rate.

  DATA:   l_temp_year   TYPE mac_t_year,
          l_temp_period TYPE mac_t_period,
          l_temp_end    LIKE sy-datum,
          l_temp_strt    LIKE sy-datum.

  CLEAR: l_temp_year, l_temp_period, l_temp_end.
  l_temp_year   = pc_period(4).
  l_temp_period = pc_period+4(3).
  mac_get_dates_for_period '1000' l_temp_year l_temp_period
                                  l_temp_strt l_temp_end.


  CALL FUNCTION 'READ_EXCHANGE_RATE'
       EXPORTING
            date             = l_temp_strt
            foreign_currency = 'CAD'
            local_currency   = 'USD'
            type_of_rate     = 'M'
       IMPORTING
            exchange_rate    = pc_rate.

  IF sy-subrc <> 0.
    pc_rate = 1.
  ENDIF.

ENDFORM.                    " GET_EXCHANGE_RATE