ABAP

Wednesday 24 August 2016

Report with email functionality

Report with email functionality

REPORT  /XXXXXX/mmm003 NO STANDARD PAGE HEADING.

INCLUDE /XXXXXX/mmm003_top.            " Data Declarations
INCLUDE /XXXXXX/mmm003_scr.            " Selection Screen Definition
INCLUDE /XXXXXX/mmm003_forms.          " Form Routines
INCLUDE /XXXXXX/mmm003_lcl_email_imp.  " Class Implementations

*----------------------------------------------------------------------*
*  AT SELECTION-SCREEN OUTPUT
*----------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
  PERFORM modify_screen.

*----------------------------------------------------------------------*
*  Validations
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
* Check if at least one value is entered on the selection screen
  PERFORM check_mat_plnt_vend_org.

AT SELECTION-SCREEN ON s_matnr.
* Validate Material
  PERFORM validate_material.

AT SELECTION-SCREEN ON s_werks.
* Validate Plant
  PERFORM validate_plant.

AT SELECTION-SCREEN ON s_lifnr.
* Validate vendor
  PERFORM validate_vendor.

AT SELECTION-SCREEN ON s_ekorg.
* Validate purchase org
  PERFORM validate_purch_org.

*----------------------------------------------------------------------*
*START-OF-SELECTION.
*----------------------------------------------------------------------*
START-OF-SELECTION.
* Fetch data
  PERFORM get_data.
* Prepare final data
  PERFORM prepare_final_data.

*----------------------------------------------------------------------*
* END-OF-SELECTION
*----------------------------------------------------------------------*
END-OF-SELECTION.
  IF g_it_pir_cond    IS INITIAL AND
     g_it_pir_coo     IS INITIAL AND
     g_it_pir_srclist IS INITIAL AND
     g_it_pir_uom     IS INITIAL AND
     g_it_pir         IS INITIAL.
    MESSAGE i113(ei).                  " No data found
  ELSE.
    PERFORM display_data.
  ENDIF.
**********************************************************************
include /XXXXXX/MMM003_TOP
**********************************************************************
*----------------------------------------------------------------------*
* Type Declarations
*----------------------------------------------------------------------*
TYPES:
* Strcture type to declare selection screen
  BEGIN OF t_sel,
    matnr TYPE matnr,
    lifnr TYPE lifnr,
    ekorg TYPE t024e-ekorg,
    werks TYPE werks_d,
    mail  TYPE ad_smtpadr,
  END OF t_sel,
* Strcture type to declare purchasing info record data
  BEGIN OF t_pir,
    infnr TYPE infnr,
    matnr TYPE matnr,
    lifnr TYPE lifnr,
    ekorg TYPE ekorg,
    werks TYPE werks_d,
  END OF t_pir,
* Strcture type to declare material info record data
  BEGIN OF t_a017,
    lifnr TYPE lifnr,
    matnr TYPE matnr,
    ekorg TYPE ekorg,
    werks TYPE werks_d,
    knumh TYPE knumh,
  END OF t_a017,
* Strcture type to declare condition records data to
* get condition price and unit
  BEGIN OF t_konp,
    knumh TYPE knumh,
    kbetr TYPE kbetr_kond,
    kpein TYPE kpein,
  END OF t_konp,
* Strcture type to declare condition records data to
* get condition unit
  BEGIN OF t_konpt,
    knumh TYPE knumh,
    kmein TYPE kmein,
  END OF t_konpt,
* Strcture type to declare final data
  BEGIN OF t_pir_cond,
    matnr TYPE matnr,
    werks TYPE werks_d,
    lifnr TYPE lifnr,
    ekorg TYPE ekorg,
    infnr TYPE infnr,
    kbetr TYPE kbetr_kond,
    kpein TYPE kpein,
  END OF t_pir_cond,
* Strcture type to declare purchasing info record data
* to get country of origin
  BEGIN OF t_pir_origin,
    infnr TYPE infnr,
    matnr TYPE matnr,
    lifnr TYPE lifnr,
    urzla TYPE uland,
    ekorg TYPE ekorg,
    werks TYPE werks_d,
  END OF t_pir_origin,
* Strcture type to declare final data
  BEGIN OF t_pir_coo,
    matnr TYPE matnr,
    werks TYPE werks_d,
    lifnr TYPE lifnr,
    ekorg TYPE ekorg,
    infnr TYPE infnr,
    urzla TYPE uland,
    herkl TYPE herkl,
  END OF t_pir_coo,
* Strcture type to declare plant data for material to get
* country of origin
  BEGIN OF t_marc,
    matnr TYPE matnr,
    werks TYPE werks_d,
    herkl TYPE herkl,
  END OF t_marc,
* Strcture type to declare plant data for material to get
* special procurement type for costing
  BEGIN OF t_marc1,
    matnr TYPE matnr,
    werks TYPE werks_d,
    sobsk TYPE ck_sobsl,
  END OF t_marc1,
* Strcture type to declare material master data to get UOM
  BEGIN OF t_mara,
    matnr TYPE matnr,
    meins TYPE meins,
  END OF t_mara,
* Strcture type to declare final data
  BEGIN OF t_pir_srclist,
    matnr TYPE matnr,
    werks TYPE werks_d,
    lifnr TYPE lifnr,
    ekorg TYPE ekorg,
    reswk TYPE bewrk,
    lifnd TYPE elifn,
    flifn TYPE flifn,
    sobsk TYPE ck_sobsl,
  END OF t_pir_srclist,
* Strcture type to declare final data
   BEGIN OF t_pir_uom,
    matnr TYPE matnr,
    werks TYPE werks_d,
    lifnr TYPE lifnr,
    ekorg TYPE ekorg,
    infnr TYPE infnr,
    kmein TYPE kmein,
    meins TYPE meins,
  END OF t_pir_uom,
* Strcture type to declare source list data
  BEGIN OF t_eord,
    matnr TYPE matnr,
    werks TYPE werks_d,
    lifnr TYPE elifn,
    flifn TYPE flifn,
    reswk TYPE bewrk,
  END OF t_eord,
* Strcture type to declare special procurement key data
  BEGIN OF t_t460a,
    werks TYPE werks_d,
    sobsl TYPE sobsl,
    wrk02 TYPE werks_d,
  END OF t_t460a.

*----------------------------------------------------------------------*
* Work areas
*----------------------------------------------------------------------*
DATA:
* Work area to declare selection screen
  g_wa_sel        TYPE t_sel,
* Workarea to declare purchasing info record data
  g_wa_pir        TYPE t_pir,
* Workarea to declare material info record data
  g_wa_a017       TYPE t_a017,
* Workarea to declare purchasing info record data
* to get country of origin
  g_wa_pir_origin TYPE t_pir_origin,
* Workarea to declare source list data
  g_wa_eord       TYPE t_eord,
* Workarea to declare condition records data to get pricing unit
  g_wa_konp       TYPE t_konp,
* Workarea to declare condition records data to get condition unit
  g_wa_konpt      TYPE t_konpt,
* Workarea to declare plant data for material to get
* country of origin
  g_wa_marc       TYPE t_marc,
* Workarea to declare plant data for material to get
* special procurement type for costing
  g_wa_marc1      TYPE t_marc1,
* Workarea to declare final data related to conditon price and unit
  g_wa_pir_cond    TYPE t_pir_cond,
* Workarea to declare final data related to PIR and material master UOM
  g_wa_pir_uom    TYPE t_pir_uom,
* Workarea to declare final data related to PIR and source list data
  g_wa_pir_srclist    TYPE t_pir_srclist.

*----------------------------------------------------------------------*
* INTERNAL TABLES
*----------------------------------------------------------------------*
DATA:
* Internal table to hold special procurement key data
  g_it_t460a        TYPE SORTED TABLE
                      OF t_t460a WITH UNIQUE KEY werks sobsl,
* Internal table to hold source list data
  g_it_eord1        TYPE SORTED TABLE
                      OF t_eord WITH NON-UNIQUE KEY matnr werks,
* Internal table to hold plant data for material to get
* country of origin
  g_it_marc         TYPE SORTED TABLE
                      OF t_marc WITH UNIQUE KEY matnr werks,
* Internal table to hold plant data for material to get
* special procurement type for costing
  g_it_marc1        TYPE SORTED TABLE
                      OF t_marc1 WITH UNIQUE KEY matnr werks,
* Internal table to hold
  g_it_a017         TYPE SORTED TABLE
                      OF t_a017
                    WITH NON-UNIQUE KEY lifnr matnr ekorg werks,
* Internal table to hold
  g_it_konp         TYPE SORTED TABLE
                      OF t_konp WITH NON-UNIQUE KEY knumh,
* Internal table to hold
  g_it_konpt        TYPE SORTED TABLE
                      OF t_konpt WITH NON-UNIQUE KEY knumh,
* Internal table to hold material master data to get UOM
  g_it_mara         TYPE SORTED TABLE
                      OF t_mara WITH UNIQUE KEY matnr,
* Internal table to hold purchasing info record data to get
* country of origin
  g_it_pir_origin   TYPE STANDARD TABLE
                      OF t_pir_origin,
* Internal table to hold purchasing info record data to get
* country of origin
  g_it_pir_coo_temp TYPE STANDARD TABLE
                      OF t_pir_origin,
* Internal table to hold purchasing info record data
  g_it_pir          TYPE STANDARD TABLE
                      OF t_pir,
* Internal table to hold purchasing info record data
  g_it_pir_temp     TYPE STANDARD TABLE
                      OF t_pir,
* Internal table to hold final data related to conditon price and unit
  g_it_pir_cond      TYPE STANDARD TABLE
                      OF t_pir_cond,
* Internal table to hold PIR and material master country of origin data
  g_it_pir_coo      TYPE STANDARD TABLE
                      OF t_pir_coo,
* Internal table to hold final data related to PIR and source list data
  g_it_pir_srclist      TYPE STANDARD TABLE
                      OF t_pir_srclist,
* Internal table to hold final data related to PIR and material
* master UOM
  g_it_pir_uom      TYPE STANDARD TABLE
                      OF t_pir_uom.

*----------------------------------------------------------------------*
* CONSTANTS
*----------------------------------------------------------------------*
CONSTANTS:
  c_kpein  TYPE kpein VALUE '100',
  c_kbetr1 TYPE kbetr_kond VALUE '0.00',
  c_kbetr2 TYPE kbetr_kond VALUE '0.01'.

*----------------------------------------------------------------------*
* CLASS lcx_excp_prc_disp DEFINITION
*----------------------------------------------------------------------*
* Exception Class Data Dislpay
*----------------------------------------------------------------------*
CLASS cl_excp_prc_disp DEFINITION
      INHERITING FROM cx_static_check FINAL.
ENDCLASS.                              " cl_excp_prc_disp DEFINITION

*----------------------------------------------------------------------*
*  CLASS cl_email_grid DEFINITION
*----------------------------------------------------------------------*
*  This class is used to send mail
*----------------------------------------------------------------------*
CLASS cl_email_grid DEFINITION.

  PUBLIC SECTION.
    METHODS email_grid IMPORTING im_name   TYPE any
                                  im_typ    TYPE char1
                                  im_title  TYPE any
                                  im_langu  TYPE sylangu
                                  im_format TYPE char1.
  PRIVATE SECTION.
    DATA:
      r_columns      TYPE REF TO cl_salv_columns_table,
      r_aggregations TYPE REF TO cl_salv_aggregations.

ENDCLASS.                              " cl_email_grid DEFINITION

*----------------------------------------------------------------------*
* OBJECTS
*----------------------------------------------------------------------*
DATA:
  go_alv        TYPE REF TO cl_salv_table,
  go_functions  TYPE REF TO cl_salv_functions,
  go_layout     TYPE REF TO cl_salv_layout,
  go_columns    TYPE REF TO cl_salv_columns_table,
  go_column     TYPE REF TO cl_salv_column_table,
  go_display    TYPE REF TO cl_salv_display_settings,
  go_aggr       TYPE REF TO cl_salv_aggregations.

**********************************************************************
include /XXXXXX/MMM003_SCR

**********************************************************************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-t01.
SELECT-OPTIONS:
  s_matnr FOR g_wa_sel-matnr,          " Material
  s_werks FOR g_wa_sel-werks           " Plant
              NO INTERVALS NO-EXTENSION,
  s_lifnr FOR g_wa_sel-lifnr,          " Vendor
  s_ekorg FOR g_wa_sel-ekorg.          " Purchasing Organization
SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-t02.
SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS
  p_rb1 TYPE RADIOBUTTON GROUP rbg.
SELECTION-SCREENCOMMENT 5(45text-t04.
PARAMETERS
  p_rb2 TYPE RADIOBUTTON GROUP rbg.
SELECTION-SCREENCOMMENT 55(45text-t05,
                  END OF LINE.

SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS
  p_rb3 TYPE RADIOBUTTON GROUP rbg.
SELECTION-SCREENCOMMENT 5(45text-t06.
PARAMETERS
  p_rb4 TYPE RADIOBUTTON GROUP rbg.
SELECTION-SCREENCOMMENT 55(45text-t07,
                  END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS
  p_rb5 TYPE RADIOBUTTON GROUP rbg.
SELECTION-SCREENCOMMENT 5(45text-t08.
PARAMETERS
  p_rb6 TYPE RADIOBUTTON GROUP rbg.
SELECTION-SCREENCOMMENT 55(67text-t09,
                  END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS
  p_rb7 TYPE RADIOBUTTON GROUP rbg.
SELECTION-SCREENCOMMENT 5(40text-t10,
                  END OF LINE.
SELECTION-SCREEN END OF BLOCK b2.

SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE text-t03.
SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS:
  p_rb8 TYPE RADIOBUTTON GROUP rb1
               DEFAULT 'X' USER-COMMAND usr.
SELECTION-SCREENCOMMENT 5(30text-t17.
PARAMETERS:
  p_dlist TYPE soos1-recnam            " Distribution List
               MODIF ID m1.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS:
  p_rb9 TYPE RADIOBUTTON GROUP rb1.
SELECTION-SCREENCOMMENT 5(30text-t16.
PARAMETERS:
  p_mail TYPE ad_smtpadr               " Recipient Address
              MODIF ID m2.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK b3.
**********************************************************************
include /XXXXXX/MMM003_FORMS

**********************************************************************
*&--------------------------------------------------------------------*
*& FORM get_no_plant_data.
*&--------------------------------------------------------------------*
*  This subroutine indentifies PIRs with no plant assignment          *
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM get_no_plant_data.
* Get PIR data from tables EINA and EINE based on the
* selection screen entries and check no plant assignment
  SELECT eina~infnr
         eina~matnr
         eina~lifnr
         eine~ekorg
         eine~werks
    INTO TABLE g_it_pir
    FROM eina INNER JOIN eine
      ON eina~infnr EQ eine~infnr
   WHERE eina~lifnr IN s_lifnr
     AND eina~matnr IN s_matnr
     AND eina~loekz EQ space
     AND eine~ekorg IN s_ekorg
     AND eine~esokz EQ '0'
     AND eine~werks EQ space
     AND eine~loekz EQ space.

ENDFORM.                               " FORM get_no_plant_data.
*&--------------------------------------------------------------------*
*& FORM populate_pir_origin.
*&--------------------------------------------------------------------*
*  This subroutine populates final data where the coutry of origin in *
*  the PIR is not filled in                                           *
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM populate_pir_origin.
  DATA
* Workarea to declare PIR and material master country of origin data
    l_wa_pir_coo TYPE t_pir_coo.

  LOOP AT g_it_pir_origin INTO g_wa_pir_origin.
    l_wa_pir_coo-matnr g_wa_pir_origin-matnr.
    l_wa_pir_coo-werks g_wa_pir_origin-werks.
    l_wa_pir_coo-lifnr g_wa_pir_origin-lifnr.
    l_wa_pir_coo-ekorg g_wa_pir_origin-ekorg.
    l_wa_pir_coo-infnr g_wa_pir_origin-infnr.
    l_wa_pir_coo-urzla g_wa_pir_origin-urzla.
    CLEAR g_wa_marc.
    READ TABLE g_it_marc INTO g_wa_marc
                         WITH KEY matnr g_wa_pir_origin-matnr
                                  werks g_wa_pir_origin-werks
                         BINARY SEARCH.
    IF sy-subrc EQ 0.
      l_wa_pir_coo-herkl g_wa_marc-herkl.
    ENDIF.
    APPEND l_wa_pir_coo TO g_it_pir_coo.
    CLEAR l_wa_pir_coo.
  ENDLOOP.

ENDFORM.                               " FORM populate_pir_origin.

*&--------------------------------------------------------------------*
*& FORM get_pir_origin.
*&--------------------------------------------------------------------*
*  This subroutine retrieves country of origin data from PIR tables   *
*  and material master table                                          *
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM get_pir_origin.
  IF p_rb4 EQ 'X'.
* Fetch PIR data where country of origin equal to space
    SELECT eina~infnr
           eina~matnr
           eina~lifnr
           eina~urzla
           eine~ekorg
           eine~werks
      INTO TABLE g_it_pir_origin
      FROM eina INNER JOIN eine
        ON eina~infnr EQ eine~infnr
     WHERE eina~lifnr IN s_lifnr
       AND eina~matnr IN s_matnr
       AND eina~loekz EQ space
       AND eina~urzla EQ space
       AND eine~ekorg IN s_ekorg
       AND eine~esokz EQ '0'
       AND eine~werks IN s_werks
       AND eine~loekz EQ space.
  ELSE.
* Fetch purchasing info records to get country of origin
    SELECT eina~infnr
           eina~matnr
           eina~lifnr
           eina~urzla
           eine~ekorg
           eine~werks
      INTO TABLE g_it_pir_origin
      FROM eina INNER JOIN eine
        ON eina~infnr EQ eine~infnr
     WHERE eina~lifnr IN s_lifnr
       AND eina~matnr IN s_matnr
       AND eina~loekz EQ space
       AND eine~ekorg IN s_ekorg
       AND eine~esokz EQ '0'
       AND eine~werks IN s_werks
       AND eine~loekz EQ space.
  ENDIF.
  IF g_it_pir_origin IS NOT INITIAL.
    g_it_pir_coo_temp[] g_it_pir_origin[].
    SORT g_it_pir_coo_temp BY matnr werks.
    DELETE ADJACENT DUPLICATES FROM g_it_pir_coo_temp
                          COMPARING matnr
                                    werks.
* Fetch material master data to get country of origin
    SELECT matnr
           werks
           herkl
      INTO TABLE g_it_marc
      FROM marc
       FOR ALL ENTRIES IN g_it_pir_coo_temp
     WHERE matnr EQ g_it_pir_coo_temp-matnr
       AND werks EQ g_it_pir_coo_temp-werks.
    IF sy-subrc EQ 0.
      FREE g_it_pir_coo_temp.
      SORT g_it_pir_origin.
    ENDIF.
  ENDIF.
ENDFORM.                               " FORM get_pir_origin.
*&--------------------------------------------------------------------*
*& FORM get_pir_source_list.
*&--------------------------------------------------------------------*
*  This subroutine brings source list
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM get_pir_source_list.
* Get PIR data from tables EINA and EINE based on the
* selection screen entries
  SELECT eina~infnr
         eina~matnr
         eina~lifnr
         eine~ekorg
         eine~werks
    INTO TABLE g_it_pir
    FROM eina INNER JOIN eine
      ON eina~infnr EQ eine~infnr
   WHERE eina~lifnr IN s_lifnr
     AND eina~matnr IN s_matnr
     AND eina~loekz EQ space
     AND eine~ekorg IN s_ekorg
     AND eine~esokz EQ '0'
     AND eine~werks IN s_werks
     AND eine~loekz EQ space.

  IF g_it_pir IS NOT INITIAL.
* Get source list data from EORD table based on the itab G_IT_PIR
    SELECT matnr
           werks
           lifnr
           flifn
           reswk
      INTO TABLE g_it_eord1
      FROM eord
       FOR ALL ENTRIES IN g_it_pir
     WHERE matnr EQ g_it_pir-matnr
       AND werks EQ g_it_pir-werks
       AND vdatu LE sy-datum
       AND bdatu GE sy-datum.

    IF g_it_eord1 IS NOT INITIAL.
      SELECT matnr
             werks
             sobsk
        INTO TABLE g_it_marc1
        FROM marc
        FOR ALL ENTRIES IN g_it_eord1
       WHERE matnr EQ g_it_eord1-matnr
         AND werks EQ g_it_eord1-werks.

* Get data from table T460A based on the procurement plant(RESWK)
      SELECT werks
             sobsl
             wrk02
        INTO TABLE g_it_t460a
        FROM t460a
        FOR ALL ENTRIES IN g_it_eord1
       WHERE werks EQ g_it_eord1-reswk.
    ENDIF.
  ENDIF.

ENDFORM.                               " FORM get_pir_source_list.

*&--------------------------------------------------------------------*
*& FORM get_pir_uom.
*&--------------------------------------------------------------------*
*  This subroutines retrieves all the materials where the UOM in the  *
*  basic view of the material master is not the same as in the PIR    *
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM get_pir_uom.
* Get PIR data from tables EINA and EINE based on the
* selection screen entries
  SELECT eina~infnr
         eina~matnr
         eina~lifnr
         eine~ekorg
         eine~werks
    INTO TABLE g_it_pir
    FROM eina INNER JOIN eine
      ON eina~infnr EQ eine~infnr
   WHERE eina~lifnr IN s_lifnr
     AND eina~matnr IN s_matnr
     AND eina~loekz EQ space
     AND eine~ekorg IN s_ekorg
     AND eine~esokz '0'
     AND eine~werks IN s_werks
     AND eine~loekz EQ space.
  IF sy-subrc EQ 0.
    SORT g_it_pir BY matnr.
    g_it_pir_temp[] g_it_pir[].
    DELETE ADJACENT DUPLICATES FROM g_it_pir_temp
                          COMPARING matnr.
    SORT g_it_pir.
  ENDIF.

  IF g_it_pir IS NOT INITIAL.
* Get data from the table A017 based on the PIR data G_IT_PIR
    SELECT lifnr
           matnr
           ekorg
           werks
           knumh
      INTO TABLE g_it_a017
      FROM a017
      FOR ALL ENTRIES IN g_it_pir
     WHERE kappl EQ 'M'
       AND kschl EQ 'PB00'
       AND lifnr EQ g_it_pir-lifnr
       AND matnr EQ g_it_pir-matnr
       AND ekorg EQ g_it_pir-ekorg
       AND werks EQ g_it_pir-werks
       AND esokz EQ '0'
       AND datbi GE sy-datum
       AND datab LE sy-datum.
    IF g_it_a017 IS NOT INITIAL.
* Fetch condition records data to get unit of measurement(KMEIN)
      SELECT knumh
             kmein
        INTO TABLE g_it_konpt
        FROM konp
         FOR ALL ENTRIES IN g_it_a017
       WHERE knumh EQ g_it_a017-knumh.
      IF sy-subrc EQ 0.
        DELETE ADJACENT DUPLICATES FROM g_it_konpt
                              COMPARING knumh.
      ENDIF.
    ENDIF.
  ENDIF.

  IF g_it_pir_temp IS NOT INITIAL.
* Get unit from material master table based on the itab G_IT_PIR_TEMP
    SELECT matnr
           meins
      INTO TABLE g_it_mara
      FROM mara
      FOR ALL ENTRIES IN g_it_pir_temp
     WHERE matnr EQ g_it_pir_temp-matnr.
    IF sy-subrc EQ 0.
      FREE g_it_pir_temp.
    ENDIF.
  ENDIF.

ENDFORM.                               " FORM get_pir_uom.

*--------------------------------------------------------------------*
*  Form  PROCESS_PIR_UOM                                             *
*--------------------------------------------------------------------*
*  This subroutine populates materials whose UOM in PIR is not same  *
*  as in the material master
*---------------------------------------------------------------------*
FORM process_pir_uom .
  DATA
* Workarea to declare material master data to get UOM
    l_wa_mara       TYPE t_mara.

  LOOP AT g_it_pir INTO g_wa_pir.
    g_wa_pir_uom-matnr g_wa_pir-matnr.
    g_wa_pir_uom-werks g_wa_pir-werks.
    g_wa_pir_uom-lifnr g_wa_pir-lifnr.
    g_wa_pir_uom-ekorg g_wa_pir-ekorg.
    g_wa_pir_uom-infnr g_wa_pir-infnr.
    CLEAR g_wa_a017.
    READ TABLE g_it_a017 INTO g_wa_a017
                      WITH KEY lifnr g_wa_pir-lifnr
                               matnr g_wa_pir-matnr
                               ekorg g_wa_pir-ekorg
                               werks g_wa_pir-werks
                      BINARY SEARCH.
    IF sy-subrc EQ 0.
      CLEAR g_wa_konpt.
      READ TABLE g_it_konpt INTO g_wa_konpt
                            WITH KEY knumh g_wa_a017-knumh
                            BINARY SEARCH.
      IF sy-subrc EQ 0.
        g_wa_pir_uom-kmein g_wa_konpt-kmein.
      ENDIF.

      CLEAR l_wa_mara.
      READ TABLE g_it_mara INTO l_wa_mara
                           WITH KEY matnr g_wa_pir-matnr
                           BINARY SEARCH.
      IF sy-subrc EQ 0.
        g_wa_pir_uom-meins l_wa_mara-meins.
      ENDIF.

      IF g_wa_pir_uom-kmein NE g_wa_pir_uom-meins.
        APPEND g_wa_pir_uom TO g_it_pir_uom.
      ENDIF.
    ENDIF.

    CLEAR g_wa_pir_uom,
            g_wa_pir.
  ENDLOOP.

ENDFORM.                               " PROCESS_PIR_UOM

*&--------------------------------------------------------------------*
*& FORM get_price_unit_data.
*&--------------------------------------------------------------------*
*  This subroutine retrieves PIRs data and conditon records data      *
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM get_price_unit_data.
* Fetch PIR data from tables EINA and EINE based on the
* selectionscreen entries
  SELECT eina~infnr
         eina~matnr
         eina~lifnr
         eine~ekorg
         eine~werks
    INTO TABLE g_it_pir
    FROM eina INNER JOIN eine
      ON eina~infnr EQ eine~infnr
   WHERE eina~lifnr IN s_lifnr
     AND eina~matnr IN s_matnr
     AND eina~loekz EQ space
     AND eine~ekorg IN s_ekorg
     AND eine~esokz EQ '0'
     AND eine~werks IN s_werks
     AND eine~loekz EQ space.

  IF g_it_pir IS NOT INITIAL.
* Fetch material info record data from table A017 based on the
* internal table G_IT_PIR
    SELECT lifnr
           matnr
           ekorg
           werks
           knumh
      INTO TABLE g_it_a017
      FROM a017
      FOR ALL ENTRIES IN g_it_pir
     WHERE kappl EQ 'M'
       AND kschl EQ 'PB00'
       AND lifnr EQ g_it_pir-lifnr
       AND matnr EQ g_it_pir-matnr
       AND ekorg EQ g_it_pir-ekorg
       AND werks EQ g_it_pir-werks
       AND esokz EQ '0'
       AND datbi GE sy-datum
       AND datab LE sy-datum.
  ENDIF.
  IF g_it_a017 IS NOT INITIAL.
    IF p_rb1 EQ 'X'.
* Fetch conditions data where price unit not equal to 100
      SELECT knumh
             kbetr
             kpein
        INTO TABLE g_it_konp
        FROM konp
        FOR ALL ENTRIES IN g_it_a017
       WHERE knumh EQ g_it_a017-knumh
         AND kpein <> c_kpein .
    ELSEIF p_rb2 EQ 'X'.
* Fetch conditions data where condition price equal to 0.00 or 0.01
      SELECT knumh
             kbetr
             kpein
        INTO TABLE g_it_konp
        FROM konp
        FOR ALL ENTRIES IN g_it_a017
       WHERE knumh EQ g_it_a017-knumh
         AND kbetr IN (c_kbetr1,c_kbetr2).
    ENDIF.
  ENDIF.
ENDFORM.                               " FORM get_price_unit_data.

*&--------------------------------------------------------------------*
*& FORM populate_price_unit_data.
*&--------------------------------------------------------------------*
*  This subroutine populates PIRs and conditon records data           *
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM populate_price_unit_data.
  SORT:
    g_it_pir.

  LOOP AT g_it_pir INTO g_wa_pir.

    g_wa_pir_cond-infnr g_wa_pir-infnr.
    g_wa_pir_cond-matnr g_wa_pir-matnr.
    g_wa_pir_cond-werks g_wa_pir-werks.
    g_wa_pir_cond-lifnr g_wa_pir-lifnr.
    g_wa_pir_cond-ekorg g_wa_pir-ekorg.
    CLEAR g_wa_a017.
    READ TABLE g_it_a017 INTO g_wa_a017
                         WITH KEY lifnr g_wa_pir-lifnr
                                  matnr g_wa_pir-matnr
                                  ekorg g_wa_pir-ekorg
                                  werks g_wa_pir-werks
                         BINARY SEARCH.
    IF sy-subrc EQ 0.
      CLEAR g_wa_konp.
      READ TABLE g_it_konp INTO g_wa_konp
                           WITH KEY knumh g_wa_a017-knumh
                           BINARY SEARCH.
      IF sy-subrc EQ 0.
        g_wa_pir_cond-kbetr g_wa_konp-kbetr.
        g_wa_pir_cond-kpein g_wa_konp-kpein.
        APPEND g_wa_pir_cond TO g_it_pir_cond.
        CLEAR g_wa_pir_cond.
      ENDIF.
    ENDIF.
  ENDLOOP.                             " LOOP AT g_it_pir

ENDFORM.                               " FORM populate_price_unit_data.

*&--------------------------------------------------------------------*
*& FORM populate_pir_coo.
*&--------------------------------------------------------------------*
*  This subroutine populates PIRs where the country of origin does not*
*  match with that on the material master                             *
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM populate_pir_coo.
  DATA
* Workarea to declare PIR and material master country of origin data
   l_wa_pir_coo    TYPE t_pir_coo.

  LOOP AT g_it_pir_origin INTO g_wa_pir_origin.
    CLEAR g_wa_marc.
    READ TABLE g_it_marc INTO g_wa_marc
    WITH KEY matnr g_wa_pir_origin-matnr
             werks g_wa_pir_origin-werks
             BINARY SEARCH.
    IF sy-subrc EQ 0.
      IF g_wa_marc-herkl <> g_wa_pir_origin-urzla.
        l_wa_pir_coo-matnr g_wa_pir_origin-matnr.
        l_wa_pir_coo-werks g_wa_pir_origin-werks.
        l_wa_pir_coo-lifnr g_wa_pir_origin-lifnr.
        l_wa_pir_coo-ekorg g_wa_pir_origin-ekorg.
        l_wa_pir_coo-infnr g_wa_pir_origin-infnr.
        l_wa_pir_coo-urzla g_wa_pir_origin-urzla.
        l_wa_pir_coo-herkl g_wa_marc-herkl.
        APPEND l_wa_pir_coo TO g_it_pir_coo.
        CLEARl_wa_pir_coo,
               g_wa_pir_origin.
      ENDIF.
    ENDIF.
  ENDLOOP.

ENDFORM.                               " FORM populate_pir_coo.
*&--------------------------------------------------------------------*
*& FORM populate_pir_src_list.
*&--------------------------------------------------------------------*
*  This subroutine is used to get PIR and Source list info
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM populate_pir_src_list.
  DATA :
    l_display       TYPE c,
* Workarea for special procurement key data
    l_wa_t460a      TYPE t_t460a.

  LOOP AT g_it_pir INTO g_wa_pir.
    g_wa_pir_srclist-matnr g_wa_pir-matnr.
    g_wa_pir_srclist-werks g_wa_pir-werks.
    g_wa_pir_srclist-lifnr g_wa_pir-lifnr.
    g_wa_pir_srclist-ekorg g_wa_pir-ekorg.

    CLEAR g_wa_eord.
    READ TABLE g_it_eord1 INTO g_wa_eord
                          WITH KEY matnr g_wa_pir-matnr
                                   werks g_wa_pir-werks
                          BINARY SEARCH.
    IF sy-subrc EQ 0.
      CLEAR g_wa_marc1.
      READ TABLE g_it_marc1 INTO g_wa_marc1
                            WITH KEY matnr g_wa_eord-matnr
                                     werks g_wa_eord-werks
                          BINARY SEARCH.
*     If SPKC has a value, get plant value from T460A table
*     translate SPKC value to plant

      IF sy-subrc EQ AND g_wa_marc1-sobsk IS NOT INITIAL.
        CLEAR l_wa_t460a.
        READ TABLE g_it_t460a INTO l_wa_t460a
                             WITH KEY werks g_wa_eord-werks
                                      sobsl g_wa_marc1-sobsk.

        g_wa_pir_srclist-sobsk l_wa_t460a-wrk02.

        IF g_wa_eord-reswk IS NOT INITIAL AND
           l_wa_t460a-wrk02 NE g_wa_eord-reswk.
          l_display 'Y'.
        ENDIF.                         " IF g_wa_eord-reswk IS NOT INITIAL...
*       If the SL FIXED SOURCE IS a Vendor
        IF g_wa_eord-lifnr IS NOT INITIAL
           AND g_wa_eord-flifn EQ 'X'.
          l_display 'Y'.
        ENDIF.                         " IF g_wa_eord-lifnr IS NOT INITIAL
      ELSE.                            " SPKC value is blank
*       If the SL fixed source is a procurement plant
        IF g_wa_eord-reswk IS NOT INITIAL
          AND g_wa_eord-flifn 'X'.
          l_display 'Y'.
        ENDIF.                         " IF sy-subrc EQ 0 AND g_wa_marc1-sobsk.
      ENDIF.                           " IF sy-subrc EQ 0 AND ...
    ENDIF.                             "  IF sy-subrc EQ 0
    IF l_display 'Y'.
      g_wa_pir_srclist-lifnd g_wa_eord-lifnr.
      g_wa_pir_srclist-flifn g_wa_eord-flifn.
      g_wa_pir_srclist-reswk g_wa_eord-reswk.
*      g_wa_pir_srclist-sobsk = g_wa_marc1-sobsk.
      APPEND g_wa_pir_srclist TO g_it_pir_srclist.
    ENDIF.                             " IF l_display = 'Y'
    CLEAR:
      g_wa_pir_srclist,
      g_wa_pir,
      l_display.
  ENDLOOP.                             " LOOP AT g_it_pir

ENDFORM.                               " FORM populate_pir_src_list.

*&--------------------------------------------------------------------*
*& FORM display_data.
*&--------------------------------------------------------------------*
*  This subroutine is used to display data
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM display_data.
  DATA :
    l_key TYPE salv_s_layout_key.
  TRY .
* 1. Instantiate the ALV Global Class
* -----------------------------------------
      IF p_rb1 EQ 'X' OR
         p_rb2 EQ 'X'.
        CALL METHOD cl_salv_table=>factory
          IMPORTING
            r_salv_table go_alv
          CHANGING
            t_table      g_it_pir_cond.
      ELSEIF p_rb3 EQ 'X'.
        CALL METHOD cl_salv_table=>factory
          IMPORTING
            r_salv_table go_alv
          CHANGING
            t_table      g_it_pir.
      ELSEIF p_rb4 EQ 'X' OR
             p_rb5 EQ 'X'.
        CALL METHOD cl_salv_table=>factory
          IMPORTING
            r_salv_table go_alv
          CHANGING
            t_table      g_it_pir_coo.
      ELSEIF p_rb6 EQ 'X'.
        CALL METHOD cl_salv_table=>factory
          IMPORTING
            r_salv_table go_alv
          CHANGING
            t_table      g_it_pir_srclist.
      ELSEIF p_rb7 EQ 'X'.
        CALL METHOD cl_salv_table=>factory
          IMPORTING
            r_salv_table go_alv
          CHANGING
            t_table      g_it_pir_uom.
      ENDIF.

* 2. This Function is for the Tool Bar
* -----------------------------------------
* Get Object Reference to the Tool Bar Functions
      CALL METHOD go_alv->get_functions
        RECEIVING
          value go_functions.

* Activate all ALV Tool Bar Functions
      CALL METHOD go_functions->set_all
        EXPORTING
          value if_salv_c_bool_sap=>true.

* 3. This is for the ALV Layout Functions
* -----------------------------------------
* Get the Object Reference for the ALV Layout
      CALL METHOD go_alv->get_layout
        RECEIVING
          value go_layout.
* Set the Key
      l_key-report sy-repid.
      go_layout->set_keyl_key ).
* Set the Save Layout Option
      go_layout->set_save_restrictionif_salv_c_layout=>restrict_none ).
* Activate Option of Saving Default Setting
      go_layout->set_defaultabap_true ).

* 4. Update Column Related Data
* -----------------------------------------
      CALL METHOD go_alv->get_columns
        RECEIVING
          value go_columns.
* Set the Key Fixation
      go_columns->set_key_fixationabap_true ).
      IF p_rb3 EQ 'X'.
        go_columns->set_column_positioncolumnname 'MATNR'
                                            position ).
        go_columns->set_column_positioncolumnname 'WERKS'
                                            position ).
        go_columns->set_column_positioncolumnname 'LIFNR'
                                            position ).
        go_columns->set_column_positioncolumnname 'EKORG'
                                            position ).
        go_columns->set_column_positioncolumnname 'INFNR'
                                            position ).
      ENDIF.
      TRY.
          IF p_rb4 EQ 'X' OR
             p_rb5 EQ 'X'.
* Country of Origin - PIR - Set Column Text
            go_column ?= go_columns->get_columncolumnname 'URZLA' ).
            go_column->set_long_texttext-t20 ).
            go_column->set_medium_texttext-t21 ).
            go_column->set_short_texttext-t22 ).
            go_column->set_output_length22 ).
* Country of Origin – Material Master - Set Column Text
            go_column ?= go_columns->get_columncolumnname 'HERKL' ).
            go_column->set_long_texttext-t23 ).
            go_column->set_medium_texttext-t24 ).
            go_column->set_short_texttext-t25 ).
            go_column->set_output_length27 ).
          ELSEIF p_rb6 EQ 'X'.
* PIR Vendor - Set Column Text
            go_column ?= go_columns->get_columncolumnname 'LIFNR' ).
            go_column->set_long_texttext-t30 ).
            go_column->set_medium_texttext-t30 ).
            go_column->set_short_texttext-t30 ).
            go_column->set_output_length12 ).
* Fixed Vendor - Set Column Text
            go_column ?= go_columns->get_columncolumnname 'LIFND' ).
            go_column->set_long_texttext-t32 ).
            go_column->set_medium_texttext-t32 ).
            go_column->set_short_texttext-t31 ).
            go_column->set_output_length15 ).
          ELSEIF p_rb7 EQ 'X'.
* Unit Of Measure – PIR  - Set Column Text
            go_column ?= go_columns->get_columncolumnname 'KMEIN' ).
            go_column->set_long_texttext-t26 ).
            go_column->set_medium_texttext-t27 ).
            go_column->set_short_texttext-t27 ).
            go_column->set_output_length11 ).
* Base Unit of Measure – Material Master  - Set Column Text
            go_column ?= go_columns->get_columncolumnname 'MEINS' ).
            go_column->set_long_texttext-t28 ).
            go_column->set_medium_texttext-t28  ).
            go_column->set_short_texttext-t29 ).
            go_column->set_output_length15 ).
          ENDIF.
        CATCH cx_salv_not_found.                        "#EC NO_HANDLER
* Do Nothing
      ENDTRY.
* Modify Display Settings
* -----------------------------------------
      CALL METHOD go_alv->get_display_settings
        RECEIVING
          value go_display.
      go_display->set_striped_patternabap_true ).
      IF p_dlist IS NOT INITIAL OR
         p_mail  IS NOT INITIAL.
        PERFORM send_mail.
      ENDIF.
* 6. Display the ALV Data
* -----------------------------------------
      go_alv->display).
    CATCH cx_salv_msg .
      MESSAGE i113(ei).                  " No data found
  ENDTRY.

ENDFORM.                               " FORM display_data.

*---------------------------------------------------------------------*
*  FORM send_mail.
*---------------------------------------------------------------------*
*  This subroutine is used to send mail either through distribution
*  list or recipient address
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM send_mail.
  CONSTANTS
    c_format TYPE char1 VALUE 'E'.
  DATA:
    lo_email TYPE REF TO cl_email_grid,
    l_title  TYPE string .

  l_title text-t11.
  CREATE OBJECT lo_email.
  IF p_dlist IS NOT INITIAL.
* This method is used to send mail through distribution list
    CALL METHOD lo_email->email_grid
      EXPORTING
        im_name   p_dlist
        im_typ    'D'
        im_title  l_title
        im_langu  sy-langu
        im_format c_format.
  ELSEIF p_mail IS NOT INITIAL.
* This method is used to send mail to recipient address
    CALL METHOD lo_email->email_grid
      EXPORTING
        im_name   p_mail
        im_typ    'U'
        im_title  l_title
        im_langu  sy-langu
        im_format c_format.
  ENDIF.

ENDFORM.                               " SEND_MAIL

*&--------------------------------------------------------------------*
*& FORM validate_material.
*&--------------------------------------------------------------------*
*  This suborutine is used to validate material
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM validate_material.
  IF NOT s_matnr[]  IS INITIAL.
    SELECT matnr
      INTO g_wa_sel-matnr
        UP TO ROWS
      FROM mara
     WHERE matnr IN s_matnr.
    ENDSELECT.
    IF sy-subrc NE 0.
      MESSAGE text-t12 TYPE 'E'.
    ENDIF.
  ENDIF.                               " IF NOT s_matnr
ENDFORM.                               " FORM validate_material.

*&--------------------------------------------------------------------*
*& FORM validate_plant.
*&--------------------------------------------------------------------*
*  This suborutine is used to validate plant
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM validate_plant.
  IF NOT s_werks[] IS INITIAL.
    SELECT werks
      INTO g_wa_sel-werks
        UP TO ROWS
      FROM t001w
     WHERE werks IN s_werks.
    ENDSELECT.
    IF sy-subrc NE 0.
      MESSAGE text-t13 TYPE 'E'.
    ENDIF.
  ENDIF.
ENDFORM.                               " FORM validate_plant.

*&--------------------------------------------------------------------*
*& FORM validate_vendor.
*&--------------------------------------------------------------------*
*  This suborutine is used to validate vendor
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM validate_vendor.
  IF NOT s_lifnr[] IS INITIAL.
    SELECT lifnr
      INTO g_wa_sel-lifnr
        UP TO ROWS
      FROM lfa1
     WHERE lifnr IN s_lifnr.
    ENDSELECT.
    IF sy-subrc NE 0.
      MESSAGE text-t14 TYPE 'E'.
    ENDIF.
  ENDIF.
ENDFORM.                               " FORM validate_vendor.

*&--------------------------------------------------------------------*
*& FORM validate_purch_org.
*&--------------------------------------------------------------------*
*  This suborutine is used to validate purchase org
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM validate_purch_org.
  IF NOT s_ekorg[]  IS INITIAL.
    SELECT ekorg
      INTO g_wa_sel-ekorg
        UP TO ROWS
      FROM t024e
     WHERE ekorg IN s_ekorg.
    ENDSELECT.
    IF sy-subrc NE 0.
      MESSAGE text-t15 TYPE 'E'.
    ENDIF.
  ENDIF.
ENDFORM.                               " FORM validate_purch_org.
*&--------------------------------------------------------------------*
*& FORM modify_screen.
*&--------------------------------------------------------------------*
*  This suborutine is used to modify screen
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM modify_screen.
  LOOP AT SCREEN.
* Distribution List
    IF p_rb8 IS INITIAL.
      IF screen-group1 'M1'.
        screen-active  0.
        MODIFY SCREEN.
      ENDIF.
    ENDIF.
* Recipient List
    IF p_rb9 IS INITIAL.
      IF screen-group1 'M2'.
        screen-active  0.
        MODIFY SCREEN.
      ENDIF.
    ENDIF.
  ENDLOOP.
ENDFORM.                               " FORM modify_screen.
*&--------------------------------------------------------------------*
*& FORM get_data.
*&--------------------------------------------------------------------*
*  This suborutine is used to get data
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM get_data.
  IF p_rb1 EQ 'X' OR
     p_rb2 EQ 'X'.
    PERFORM get_price_unit_data.
  ELSEIF p_rb3 EQ 'X'.
    PERFORM get_no_plant_data.
  ELSEIF p_rb4 EQ 'X' OR
         p_rb5 EQ 'X'.
    PERFORM get_pir_origin.
  ELSEIF p_rb6 EQ 'X'.
    PERFORM get_pir_source_list.
  ELSE.
    PERFORM get_pir_uom.
  ENDIF.
ENDFORM.                               " FORM get_data.
*&--------------------------------------------------------------------*
*& FORM prepare_final_data .
*&--------------------------------------------------------------------*
*  This suborutine is used to prepare final data
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM prepare_final_data.
  IF p_rb1 EQ 'X' OR
     p_rb2 EQ 'X'.
    PERFORM populate_price_unit_data.
  ELSEIF p_rb4 EQ 'X'.
    PERFORM populate_pir_origin.
  ELSEIF p_rb5 EQ 'X'.
    PERFORM populate_pir_coo.
  ELSEIF p_rb6 EQ 'X'.
    PERFORM populate_pir_src_list.
  ELSEIF p_rb7 EQ 'X'.
    PERFORM process_pir_uom.
  ENDIF.
ENDFORM.                               " _FORM prepare_final_data.
*&--------------------------------------------------------------------*
*& FORM check_mat_plnt_vend.
*&--------------------------------------------------------------------*
*  This suborutine is used to validate material, Plant ,Vendor and org
*---------------------------------------------------------------------*
*  There are no interface parameters to be passed to this subroutine
*---------------------------------------------------------------------*
FORM check_mat_plnt_vend_org.

  IF s_matnr IS INITIAL AND
     s_werks IS INITIAL AND
     s_lifnr IS INITIAL AND
     s_ekorg IS INITIAL.
    MESSAGE text-t18 TYPE 'E'.
  ENDIF.

ENDFORM.                               " FORM check_mat_plnt_vend....
**********************************************************************
include /XXXXXX/MMM003_LCL_EMAIL_IMP

**********************************************************************
*----------------------------------------------------------------------*
*  CLASS cl_email_grid IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_email_grid IMPLEMENTATION.

  METHOD email_grid.

* Convert ALV in current layout to Excel and send as attachement
    DATA:
      l_gentext        TYPE itex132,
      l_binary_content TYPE solix_tab,
      l_size           TYPE so_obj_len,
      l_length         TYPE i,
      l_distlst        TYPE so_obj_nam,
      l_mailto         TYPE ad_smtpadr,
      l_desc           TYPE so_obj_des,
      l_sent_to_all    TYPE os_boolean,
      l_xml            TYPE xstring,
      l_version        TYPE string,

      l_wa_choice      TYPE if_salv_bs_xml=>s_type_xml_choice,
      l_it_main_text   TYPE bcsy_text,
      l_it_choice      TYPE if_salv_bs_xml=>t_type_xml_choice,
      l_it_field_cat   TYPE lvc_t_fcat,

      lo_send_request  TYPE REF TO cl_bcs,
      lo_document      TYPE REF TO cl_document_bcs,
      lo_recipient     TYPE REF TO if_recipient_bcs,
      lo_bcs_exception TYPE REF TO cx_bcs,
      lo_data          TYPE REF TO data,
      lo_sender        TYPE REF TO cl_sapuser_bcs,          "#EC NEEDED
      lo_result_data   TYPE REF TO cl_salv_ex_result_data_table.

    l_it_field_cat cl_salv_controller_metadata=>get_lvc_fieldcatalog(
        r_columns      go_columns
        r_aggregations go_aggr ).

* Use Excel .XLSX format.
    l_it_choice cl_salv_export_xml_dialog=>get_gui_spreadsheet_formats(  ).
    READ TABLE l_it_choice INTO l_wa_choice
         WITH KEY xml_type '10'.

    IF p_rb1 EQ 'X' OR
       p_rb2 EQ 'X'.
      GET REFERENCE OF g_it_pir_cond INTO lo_data.
    ELSEIF p_rb3 EQ 'X'.
      GET REFERENCE OF g_it_pir INTO lo_data.
    ELSEIF p_rb4 EQ 'X' OR
           p_rb5 EQ 'X'.
      GET REFERENCE OF g_it_pir_coo INTO lo_data.
    ELSEIF p_rb6 EQ 'X'.
      GET REFERENCE OF g_it_pir_srclist INTO lo_data.
    ELSEIF p_rb7 EQ 'X'.
      GET REFERENCE OF g_it_pir_uom INTO lo_data.
    ENDIF.

    lo_result_data cl_salv_ex_util=>factory_result_data_table(
        r_data                      lo_data
        t_fieldcatalog              l_it_field_cat ).

    CASE cl_salv_bs_a_xml_base=>get_version).
      WHEN if_salv_bs_xml=>version_25.
        l_version                if_salv_bs_xml=>version_25.
      WHEN if_salv_bs_xml=>version_26.
        l_version                if_salv_bs_xml=>version_26.
    ENDCASE.
*
    CALL METHOD cl_salv_bs_tt_util=>if_salv_bs_tt_util~transform
      EXPORTING
        xml_type      l_wa_choice-xml_type
        xml_version   l_version
        r_result_data lo_result_data
        xml_flavour   if_salv_bs_c_tt=>c_tt_xml_flavour_export
      IMPORTING
        xml           l_xml.

    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
      EXPORTING
        buffer        l_xml
      IMPORTING
        output_length l_length
      TABLES
        binary_tab    l_binary_content.

    l_size l_length.
    TRY.
* create persistent send request
        lo_send_request cl_bcs=>create_persistent).

* Build file name and Email title
        l_desc l_gentext im_title.
        CONCATENATE l_desc sy-datum sy-uzeit
               INTO l_desc SEPARATED BY '_'.

* Build text body of email text i.e. the actual text in the email
        CONCATENATE l_gentext sy-datum sy-uzeit
               INTO l_gentext SEPARATED BY space.

        APPEND l_gentext  TO l_it_main_text.

        lo_document cl_document_bcs=>create_document(
          i_type    'RAW'
          i_text    l_it_main_text
          i_subject l_desc ).

* add the spread sheet as attachment to document object
        lo_document->add_attachment(
          i_attachment_type    'XLS'
          i_attachment_subject l_desc
          i_attachment_size    l_size
          i_att_content_hex    l_binary_content ).

* add document object to send request
        lo_send_request->set_documentlo_document ).
* add recipient (e-mail address)
        CASE im_typ.
          WHEN 'U'.
* Check if email address exists for user.
            CLEAR l_mailto.
            l_mailto im_name.
            lo_recipient cl_cam_address_bcs=>create_internet_addressl_mailto ).
          WHEN 'D'.
* Create recipient object for Distribution List
            l_distlst   im_name.
            lo_recipient cl_distributionlist_bcs=>getu_persistent(
                                i_dliname l_distlst
                                i_private space ).
          WHEN OTHERS.
        ENDCASE.
* add recipient object to send request
        lo_send_request->add_recipientlo_recipient ).
* send document
        l_sent_to_all lo_send_request->sendi_with_error_screen 'X' ).

        COMMIT WORK.

        IF l_sent_to_all IS INITIAL.
          MESSAGE i500(sbcomsWITH im_name.
        ELSE.
          MESSAGE s022(so).
        ENDIF.
*exception handling
      CATCH cx_bcs INTO lo_bcs_exception.
        MESSAGE i865(soWITH lo_bcs_exception->error_type.
    ENDTRY.

  ENDMETHOD.                           " email_grid

ENDCLASS.                              " cl_email_grid IMPLEMENTATION