Friday, 2 December 2016

Remove billing block from sales order



REPORT  /XXXP10/SSD006 NO STANDARD PAGE HEADING.
INCLUDE /xxxp10/ssD006_top.            " Data Declarations
INCLUDE /xxxp10/ssD006_scr.            " Selection Screen Definition
INCLUDE /xxxp10/ssD006_forms.          " Form Routines
***********************************************************************
* INITIALIZATION
***********************************************************************
INITIALIZATION.
* Include to implement LCL_EMAIL class
  
INCLUDE /xxxp10/ssD006_lcl_email_imp.
*  PERFORM initialize.


***********************************************************************
*  Validations                                                        *
***********************************************************************
AT SELECTION-SCREEN ON s_vkorg.
  
PERFORM validate_sales_org.
*AT SELECTION-SCREEN ON s_erdat.
*  PERFORM validate_erdat.
AT SELECTION-SCREEN ON s_auart.
  
PERFORM validate_auart.
AT SELECTION-SCREEN ON s_faksk.
  
PERFORM validate_faksk.
***********************************************************************
* START-OF-SELECTION
***********************************************************************
START-OF-SELECTION.

  
PERFORM get_data.
***********************************************************************
* END-OF-SELECTION
***********************************************************************
END-OF-SELECTION.

  
PERFORM process_data.
  
if g_it_vbak is not INITIAL.

  
CALL SCREEN 9000.

  
else.
    
MESSAGE text-T13 type 'I'.
    
LEAVE LIST-PROCESSING.
  
endif.
*****************************************************************
Include Top
*****************************************************************
***********************************************************************
* TYPES DECLARATION
***********************************************************************
CONSTANTS c_wbstk type c VALUE 'C'.
TYPES:
* Strcture type to declare selection screen
  
BEGIN OF t_sel,
    vkorg 
TYPE vbak-vkorg,              " Sales organization
    erdat 
TYPE vbap-erdat,              " SO date
    auart 
TYPE vbak-auart,              " Order types
    faksk 
TYPE vbak-faksk,              " Billing block
*    pstyv type vbak-pstyv,
    mail  
TYPE ad_smtpadr,              " Email Address
  
END OF t_sel,

  
BEGIN OF t_vbuk,
    vbeln 
TYPE vbeln,
    posnr 
TYPE posnr,
    vgbel 
TYPE vgbel,
    wbstk 
TYPE wbstk,
  
END OF t_vbuk.
* Structure type to declare sales order data
TYPES BEGIN OF t_vbak,
        vbeln 
TYPE vbak-vbeln,
        erdat 
TYPE vbak-erdat,
        auart 
TYPE vbak-auart,
        faksk 
TYPE vbak-faksk,
        flag  
TYPE c,
        
message TYPE char50,
        
END OF t_vbak,
* Structure type to declare SCHEDULE item data
        
BEGIN OF t_vbep,
        vbeln 
TYPE vbep-vbeln,
        posnr 
TYPE vbep-posnr,
        ettyp 
TYPE vbep-ettyp,
        lfrel 
TYPE vbep-lfrel,
        wmeng 
TYPE vbep-wmeng,
        banfn 
TYPE vbep-banfn,
        
END OF t_vbep,
*Sturcture type to declare material
        
BEGIN OF t_mseg,
        kdauf 
TYPE kdauf,
        kdpos 
TYPE kdpos,
        menge 
TYPE mseg-menge,
        
END OF t_mseg,

        
BEGIN OF t_zzvar,
          z_return 
TYPE zzvar-z_return,
        
END OF t_zzvar,
* Structure type to declare final
        
BEGIN OF t_final,
        vbeln 
TYPE vbak-vbeln,
        posnr 
TYPE vbep-posnr,
        erdat 
TYPE vbak-erdat,
        auart 
TYPE vbak-auart,
        faksk 
TYPE vbak-faksk,
        ettyp 
TYPE vbep-ettyp,
        lfrel 
TYPE vbep-lfrel,
        wmeng 
TYPE vbep-wmeng,
        menge 
TYPE mseg-menge,
        banfn 
TYPE vbep-banfn,
        flag  
TYPE c,
        
END OF t_final.

***********************************************************************
* Work areas
***********************************************************************
DATA:
* Work area to declare selection screen
  g_wa_sel        
TYPE t_sel.
DATA:
* work area for vbak
  g_wa_vbak       
TYPE t_vbak,
* work are for mseg
  g_wa_mseg       
TYPE t_mseg,
* work area for vbep
  g_wa_vbep       
TYPE t_vbep,
* work area for final
  g_wa_final       
TYPE t_final,
* internal table for auart
  g_wa_auart 
TYPE t_zzvar,
  g_wa_pstyv 
TYPE t_zzvar,
  g_wa_faksk 
TYPE t_zzvar,
  g_wa_vbuk  
type t_vbuk.
***********************************************************************
* Internal tables
***********************************************************************
DATA:
* internal table for vbak
  g_it_vbak 
TYPE STANDARD TABLE OF t_vbak,
* internal table for mseg
  g_it_mseg 
TYPE STANDARD TABLE OF t_mseg,
* internal table for vbep
  g_it_vbep 
TYPE STANDARD TABLE OF t_vbep,
* internal table for auart
  g_it_auart 
TYPE ztt_zxreturn,
  g_it_pstyv 
TYPE STANDARD TABLE OF t_zzvar,
  g_it_faksk 
TYPE ztt_zxreturn,
* internal table for final
  g_it_final 
TYPE STANDARD TABLE OF t_final,
  g_it_vbuk  
type STANDARD TABLE OF t_vbuk.


***********************************************************************
* Define local class
***********************************************************************
CLASS lcl_email DEFINITION FINAL.
  
PUBLIC SECTION.
    
CLASS-METHODSsend_email IMPORTING
                               im_name            
TYPE any
                               im_typ             
TYPE char1
                               im_title           
TYPE any
                               im_langu           
TYPE sylangu
                               im_format          
TYPE char1
                               im_attachment_type 
TYPE soodk-objtp
                               im_doc_type        
TYPE so_obj_tp.
ENDCLASS.                    "lcl_email DEFINITION
***********************************************************************
* Variables
***********************************************************************
DATA:
     go_dock     
TYPE REF TO cl_gui_docking_container,
     go_table      
TYPE REF TO cl_gui_alv_grid,
     go_salv1      
TYPE REF TO cl_salv_table,
     go_email      
TYPE REF TO lcl_email,
     go_func1      
TYPE REF TO cl_salv_functions,
     go_display1   
TYPE REF TO cl_salv_display_settings,
     go_column     
TYPE REF TO cl_salv_column_table,
     go_columns    
TYPE REF TO cl_salv_columns_table,
     go_layout     
TYPE REF TO cl_salv_layout.


**********************************************************
Selection screen
**********************************************************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-t01.
SELECT-OPTIONS:
  s_vkorg 
FOR g_wa_sel-vkorg,            " Sales Organization
  s_erdat 
FOR g_wa_sel-erdat,            " SO date
  s_auart 
FOR g_wa_sel-auart,            " Order type
*  s_pstyv FOR g_wa_sel-pstyv,            " Order type
  s_faksk 
FOR g_wa_sel-faksk.
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-t02.
SELECT-OPTIONS
  s_mail 
FOR g_wa_sel-mail               " Recipient Address
             
NO INTERVALS.
SELECTION-SCREEN END OF BLOCK b2.

*********************************************************
Forms
*********************************************************
*----------------------------------------------------------------------*
* This subroutine is for validating purchasing group                   *
*----------------------------------------------------------------------*
FORM validate_sales_org .
  
IF NOT s_vkorg  IS INITIAL.
    
SELECT SINGLE vkorg
      
FROM tvko
      
INTO g_wa_sel-vkorg
      
WHERE vkorg IN s_vkorg[].
    
IF sy-subrc <> 0.
      
MESSAGE text-t04 TYPE 'E'.
    
ENDIF.
  
ENDIF.
ENDFORM.                    " VALIDATE_SALES_ORG
*&---------------------------------------------------------------------*
*&      Form  VALIDATE_AUART
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  This subroutine is for validating purchasing group
*----------------------------------------------------------------------*
FORM validate_auart .
  
IF NOT s_auart  IS INITIAL.
    
SELECT SINGLE auart
      
FROM tvak
      
INTO g_wa_sel-auart
      
WHERE auart IN s_auart[].
    
IF sy-subrc <> 0.
      
MESSAGE text-t05 TYPE 'E'.
    
ENDIF.
  
ENDIF.
ENDFORM.                    " VALIDATE_AUART
*&---------------------------------------------------------------------*
*&      Form  VALIDATE_FAKSK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  This subroutine is for validating purchasing group
*----------------------------------------------------------------------*
FORM validate_faksk .
  
IF NOT s_faksk  IS INITIAL.
    
SELECT SINGLE faksp
      
FROM tvfs
      
INTO g_wa_sel-faksk
      
WHERE faksp IN s_faksk[].
    
IF sy-subrc <> 0.
      
MESSAGE text-t06 TYPE 'E'.
    
ENDIF.
  
ENDIF.
ENDFORM.                    " VALIDATE_FAKSK
*&---------------------------------------------------------------------*
*&      Module  STATUS_9000  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_9000 OUTPUT.
  
DATA l_key TYPE salv_s_layout_key.
  
SET PF-STATUS 'ST9000'.
  
SET TITLEBAR 'TI9000'.

  
IF cl_gui_alv_grid=>offline( ) IS INITIAL.
    
IF go_dock IS INITIAL.
      
CREATE OBJECT go_dock
        
EXPORTING
          repid     
sy-repid
          ratio     
50
          dynnr     
'9000'
          side      
cl_gui_docking_container=>dock_at_top
          extension 
100.

    
ENDIF.
  
ENDIF.

  
CALL METHOD cl_salv_table=>factory
    
EXPORTING
*     list_display = IF_SALV_C_BOOL_SAP=>FALSE
      r_container  
go_dock
    
IMPORTING
      r_salv_table 
go_salv1
    
CHANGING
      t_table      
g_it_vbak.
* Get Object Reference to the Tool Bar Functions
  
CALL METHOD go_salv1->get_functions
    RECEIVING
      
value go_func1.
* Activate all ALV Tool Bar Functions
  
CALL METHOD go_func1->set_all
    
EXPORTING
      
value if_salv_c_bool_sap=>true.
* Get the Object Reference for the ALV Layout
  
CALL METHOD go_salv1->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 ).

  
CALL METHOD go_salv1->get_columns
    RECEIVING
      
value go_columns.
* Set the Key Fixation

  go_column ?= go_columns
->get_columncolumnname 'MESSAGE' ).
  go_column
->set_long_texttext-t10 ).
  go_column
->set_medium_texttext-t10 ).
  go_column
->set_short_texttext-t10 ).
  go_column
->set_output_length15 ).
  go_columns
->set_key_fixationabap_true ).
  go_column ?= go_columns
->get_columncolumnname 'FLAG' ).
  go_column
->set_long_texttext-t11 ).
  go_column
->set_medium_texttext-t11 ).
  go_column
->set_short_texttext-t11 ).
  go_column
->set_output_length12 ).



  go_display1 
go_salv1->get_display_settings( ).
  go_display1
->set_list_headertext-t16 ).
  
PERFORM send_mail.
  
CALL METHOD go_salv1->display.
ENDMODULE.                 " STATUS_9000  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_9000 INPUT.
  
CASE sy-ucomm.
    
WHEN 'BACK' OR 'EXIT'.
      
LEAVE TO SCREEN 0.
  
ENDCASE.
ENDMODULE.                 " USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
*&      Form  SEND_MAIL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  This subroutine is for validating purchasing group
*----------------------------------------------------------------------*
FORM send_mail .
  
CREATE OBJECT go_email.
  
IF go_email IS BOUND.
    
CALL METHOD go_email->send_email
      
EXPORTING
        im_name            
s_mail
        im_typ             
'U'
        im_title           
text-t03
        im_langu           
sy-langu
        im_format          
'E'
        im_attachment_type 
'XLS'
        im_doc_type        
'RAW'.
  
ENDIF.
ENDFORM.                    " SEND_MAIL
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  This subroutine is for get data
*----------------------------------------------------------------------*
FORM get_data .

  
SELECT vbeln
         erdat
         auart
         faksk
  
FROM vbak
  
INTO TABLE g_it_vbak
  
WHERE vkorg IN s_vkorg
  
AND   erdat IN s_erdat
  
AND   auart IN s_auart
  
AND   faksk IN s_faksk.
  
IF sy-subrc EQ 0.
    
SELECT vbeln posnr ettyp lfrel wmeng banfn FROM vbep
      
INTO TABLE g_it_vbep
      
FOR ALL ENTRIES IN g_it_vbak
      
WHERE vbeln g_it_vbak-vbeln.
    
SELECT kdauf kdpos menge FROM mseg INTO TABLE g_it_mseg
      
FOR ALL ENTRIES IN g_it_vbak
      
WHERE kdauf g_it_vbak-vbeln.
    
SELECT lips~vbeln lips~posnr lips~vgbel vbuk~wbstk
      
INTO TABLE g_it_vbuk
      
FROM lips INNER JOIN vbuk
      
ON lips~vbeln vbuk~vbeln )
      
FOR ALL ENTRIES IN g_it_vbak
      
WHERE vgbel g_it_vbak-vbeln.
    
SORT g_it_vbuk BY vgbel.
  
ENDIF.
ENDFORM.                    " GET_DATA
*&---------------------------------------------------------------------*
*&      Form  PROCESS_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  This subroutine is for process data
*----------------------------------------------------------------------*
FORM process_data .

  
DATAl_header_inx TYPE bapisdh1x,
        l_header_in  
TYPE bapisdh1,
        l_po_history 
LIKE TABLE OF bapiekbes,
        w_po_history 
LIKE bapiekbes,
        l_lips_vbeln 
TYPE vbeln,
        l_lips_posnr 
TYPE posnr.

  
DATAlt_return TYPE STANDARD TABLE OF bapiret2.
  
DATA g_it_ekpo TYPE STANDARD TABLE OF ekpo,
         g_wa_ekpo 
TYPE ekpo,
         l_ekkn_ebeln 
TYPE ebeln,
         l_ekkn_ebelp 
TYPE ebelp,
         lv_ebeln 
TYPE ebeln.

  
DATA l_lines LIKE sy-tabix.                             "D10K989190
  
DATA l_po_banfn TYPE TABLE OF ekpo  WITH HEADER LINE.   "D10K989190

  
SORT g_it_mseg BY kdauf kdpos.
  
LOOP AT g_it_vbak INTO g_wa_vbak.
    
CLEARg_wa_vbepg_wa_mseg.
    g_wa_vbak
-flag 'Y'.
    
MODIFY g_it_vbak FROM g_wa_vbak TRANSPORTING flag.

    
READ TABLE g_it_vbep INTO g_wa_vbep
      
WITH KEY vbeln g_wa_vbak-vbeln BINARY SEARCH.
    
IF sy-subrc EQ 0.
      
LOOP AT g_it_vbep INTO g_wa_vbep FROM sy-tabix WHERE wmeng > 0.
        
IF g_wa_vbep-vbeln g_wa_vbak-vbeln.
          
IF g_wa_vbep-ettyp 'CX'."No Inventory management
            
CLEAR g_wa_vbep.
            
CONTINUE.
          
ENDIF.
*          IF g_wa_vbep-lfrel = 'X'." Item is relevant for delivery" D- D10K988843 20-Oct-2016
*Check if PR is done for this SO item
          
IF g_wa_vbep-banfn IS NOT INITIAL.
*Check if PO is created
* Change for - D10K989190
* There can be more than one PO created for a PR. Need to check all the
* POs to see if any of them has been GR complete. If even one is not,
* the SO cannot have the billing block removed.
*            CLEAR lv_ebeln.                                   "D10K989190
*            SELECT SINGLE ebeln FROM ekpo INTO lv_ebeln WHERE "D10K989190
*              banfn = g_wa_vbep-banfn.                     "D10K989190
            
CLEAR l_lines.                                  "D10K989190
            
REFRESH l_po_banfn.                             "D10K989190
            
SELECT FROM ekpo INTO TABLE l_po_banfn        "D10K989190
                 
WHERE banfn g_wa_vbep-banfn.             "D10K989190
            
DESCRIBE TABLE l_po_banfn LINES l_lines.        "D10K989190
*            IF sy-subrc EQ 0.                              "D10K989190
            
IF l_lines > 0.                                 "D10K989190
              
LOOP AT l_po_banfn.                           "D10K989190
                
REFRESH l_po_history.
                
CALL FUNCTION 'BAPI_PO_GETDETAIL'
                  
EXPORTING
                    purchaseorder          
l_po_banfn-ebeln
                    history                
'X'
                  
TABLES
                    po_item_history_totals 
l_po_history.
                
IF l_po_history[] IS INITIAL.               "D10K989190
                  g_wa_vbak
-flag 'N'.                     "D10K989190
                  
MODIFY g_it_vbak FROM g_wa_vbak TRANSPORTING flag"D10K989190
                  
CLEAR g_wa_vbak.                          "D10K989190
                  
EXIT.                                     "D10K989190
                
ELSE.                                       "D10K989190
                  
CLEAR l_ekkn_ebelnl_ekkn_ebelp.
                  
SELECT SINGLE ebeln ebelp FROM ekkn INTO (l_ekkn_ebelnl_ekkn_ebelpWHERE vbeln g_wa_vbep-vbeln
                                                                                         
AND vbelp g_wa_vbep-posnr.
*                  IF sy-subrc EQ 0.
                  
CLEAR w_po_history.
                  
READ TABLE l_po_history INTO w_po_history WITH KEY l_ekkn_ebelp.
                  
IF w_po_history-deliv_qty NE g_wa_vbep-wmeng.
                    g_wa_vbak
-flag 'N'.
                    
MODIFY g_it_vbak FROM g_wa_vbak TRANSPORTING flag.
                    
CLEAR g_wa_vbak.
                    
EXIT.
                  
ELSE.
                    
CLEAR g_wa_vbep.
                    
CONTINUE.
                  
ENDIF.
*                  ENDIF.
                
ENDIF.                                      "D10K989190
              
ENDLOOP.
            
ELSE.
              g_wa_vbak
-flag 'N'.
              
MODIFY g_it_vbak FROM g_wa_vbak TRANSPORTING flag.
              
CLEAR g_wa_vbak.
              
EXIT.
            
ENDIF.
          
ELSE.
            
CLEAR g_wa_vbuk.
            
LOOP AT g_it_vbuk INTO g_wa_vbuk WHERE vgbel g_wa_vbep-vbeln.
*                                                   AND wbstk NE c_wbstk.
              
IF g_wa_vbuk-wbstk NE c_wbstk.
                g_wa_vbak
-flag 'N'.
                
MODIFY g_it_vbak FROM g_wa_vbak TRANSPORTING flag.
                
CLEAR g_wa_vbak.
                
EXIT.
              
ENDIF.
            
ENDLOOP.
            
IF sy-subrc <> 0.
              g_wa_vbak
-flag 'N'.
              
MODIFY g_it_vbak FROM g_wa_vbak TRANSPORTING flag.
              
CLEAR g_wa_vbak.
              
EXIT.
            
ENDIF.
*compare order qty with pgi qty
            
CLEAR g_wa_mseg.
            
READ TABLE g_it_mseg INTO g_wa_mseg
            
WITH KEY kdauf g_wa_vbep-vbeln
                     kdpos 
g_wa_vbep-posnr
                     
BINARY SEARCH.
            
IF sy-subrc 0.
*              IF g_wa_vbep-wmeng NE g_wa_mseg-menge.
              
IF g_wa_vbep-wmeng NE g_wa_mseg-menge.
*PGI is partial, billing block cannot be removed for this SO
*Go to next SO
                g_wa_vbak
-flag 'N'.
                
MODIFY g_it_vbak FROM g_wa_vbak TRANSPORTING flag.
                
CLEAR g_wa_vbak.
                
EXIT.
              
ENDIF.
            
ELSE.
              
CLEARl_lips_vbelnl_lips_posnr.
              
SELECT SINGLE vbeln posnr FROM lips INTO (l_lips_vbelnl_lips_posnr)
                
WHERE vgbel g_wa_vbep-vbeln
                
AND   vgpos g_wa_vbep-posnr.
              
IF sy-subrc NE 0.
                g_wa_vbak
-flag 'N'.
                
MODIFY g_it_vbak FROM g_wa_vbak TRANSPORTING flag.
                
CLEAR g_wa_vbak.
                
EXIT.
              
ENDIF.
            
ENDIF.
          
ENDIF.
*        ENDIF." D- D10K988843 20-Oct-2016
        
ELSE.
          
CLEAR g_wa_vbak.
          
EXIT.
        
ENDIF.
        
CLEAR g_wa_vbep.
      
ENDLOOP.

    
ENDIF.
    
CLEAR g_wa_vbak.
  
ENDLOOP.
  
LOOP AT g_it_vbak INTO g_wa_vbak WHERE flag NE 'N'.
*update the flag.
    l_header_inx
-updateflag 'U'.
    l_header_inx
-bill_block 'X'.  " Billing
    l_header_in
-bill_block  ''.   " Remove Billing block.
*Call the bapi.
    
CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
      
EXPORTING
        salesdocument    
g_wa_vbak-vbeln
        order_header_in  
l_header_in
        order_header_inx 
l_header_inx
      
TABLES
        
return           lt_return.
*check for errors.
    
LOOP AT lt_return TRANSPORTING NO FIELDS WHERE type 'A' OR type 'E'.
      
EXIT.
    
ENDLOOP.

    
IF sy-subrc 0.
      g_wa_vbak
-message 'Error in updating'.
      
MODIFY g_it_vbak FROM g_wa_vbak TRANSPORTING message.
    
ELSE.
      g_wa_vbak
-message 'Billing block removed'.
      
MODIFY g_it_vbak FROM g_wa_vbak TRANSPORTING message.
      
COMMIT WORK AND WAIT.
    
ENDIF.
  
ENDLOOP.
ENDFORM.                    " PROCESS_DATA

***************************************************************
Email implementation
***************************************************************
*- Initial Development Delete billing block report                    *
***********************************************************************

  s_erdat
-sign 'I'.
  s_erdat
-option 'EQ'.
  s_erdat
-low = ( sy-datum 180 ).
  s_erdat
-high sy-datum.
  
APPEND s_erdat TO s_erdat[].
  /sbdp10/cl_s_sales_order
=>get_order(
    
IMPORTING
      et_return 
g_it_auart    " Return value
  
).
*  SELECT z_return FROM zzvar INTO TABLE g_it_auart
*    WHERE z_name = '/SBDP10/SSD006'
*    AND   z_key  = 'VBAK-AUART'.
*  IF sy-subrc EQ 0.
*    SELECT z_return FROM zzvar INTO TABLE g_it_pstyv
*     WHERE z_name = '/SBDP10/SSO001'
*     AND   z_key  = 'VBAK-PSTYV'.
  /sbdp10/cl_s_sales_order
=>get_billingblock(
   
IMPORTING
     et_return 
g_it_faksk    " Return value
 
).
*    SELECT z_return FROM zzvar INTO TABLE g_it_faksk
*     WHERE z_name = '/SBDP10/SSD006'
*     AND   z_key  = 'VBAK-FAKSK'.
*  ENDIF.
  
LOOP AT g_it_auart INTO g_wa_auart.
    
CLEAR s_auart.
    s_auart
-sign 'I'.
    s_auart
-option 'EQ'.
    s_auart
-low g_wa_auart-z_return.
    
APPEND s_auart TO s_auart[].
  
ENDLOOP.
*  LOOP AT g_it_pstyv INTO g_wa_pstyv.
*    CLEAR s_pstyv.
*    s_pstyv-sign = 'I'.
*    s_pstyv-option = 'EQ'.
*    s_pstyv-low = g_wa_auart-z_return.
*    APPEND s_pstyv TO s_pstyv[].
*  ENDLOOP.
  
LOOP AT g_it_faksk INTO g_wa_faksk.
    
CLEAR s_faksk.
    s_faksk
-sign 'I'.
    s_faksk
-option 'EQ'.
    s_faksk
-low g_wa_faksk-z_return.
    
APPEND s_faksk TO s_faksk[].
  
ENDLOOP.
*----------------------------------------------------------------------*
*       CLASS lcl_email IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
  
CLASS lcl_email IMPLEMENTATION.

    
METHOD send_email.
      
DATA:
        lt_messages        
TYPE STANDARD TABLE OF solisti1,
        lt_receivers       
TYPE STANDARD TABLE OF somlrec90,
        lt_field_cat       
TYPE lvc_t_fcat,

        la_message         
LIKE LINE OF lt_messages,
        la_title           
TYPE sodocchgi1,

        lt_choice          
TYPE if_salv_bs_xml=>t_type_xml_choice,
        ls_choice          
TYPE if_salv_bs_xml=>s_type_xml_choice,
        r_result_data      
TYPE REF TO cl_salv_ex_result_data_table,
        lr_data            
TYPE REF TO data,
        l_xml              
TYPE xstring,
        l_version          
TYPE string,
        l_just             
TYPE text6,
        l_value            
TYPE text200,

        r_columns          
TYPE REF TO cl_salv_columns_table,
        r_aggregations     
TYPE REF TO cl_salv_aggregations,
        r_table            
TYPE REF TO data,
        r_filters          
TYPE REF TO cl_salv_filters,

        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_sender          
TYPE REF TO cl_sapuser_bcs,
        lv_currency        
TYPE waers,
        lv_unit            
TYPE meins,
        lv_receiver        
TYPE so_name,
        lv_email_subject   
TYPE so_obj_des,
        lv_gentext         
TYPE itex132,
        lv_binary_content  
TYPE solix_tab,
        lv_binary_content2 
TYPE solix_tab,
        lv_size            
TYPE so_obj_len,
        lv_size2           
TYPE so_obj_len,
        lv_length          
TYPE i,
        lv_sent_to_all     
TYPE os_boolean,
        lv_distlst         
TYPE so_obj_nam,
        lv_mailto          
TYPE ad_smtpadr,
        lv_email_sent      
TYPE os_boolean,
        lv_user            
TYPE sy-uname,
        lv_attach_name     
TYPE so_obj_des.

      
FIELD-SYMBOLS :
        <field_cat>       
LIKE LINE OF lt_field_cat.
*Attachment for no Ack
* Convert ALV in current layout to Excel and send as attachement

      
IF go_salv1 IS BOUND.
        
CALL METHOD go_salv1->get_columns
          RECEIVING
            
value r_columns.
      
ENDIF.

      lt_field_cat 
cl_salv_controller_metadata=>get_lvc_fieldcatalog(
        r_columns      
r_columns
        r_aggregations 
r_aggregations ).

      
SORT lt_field_cat BY no_out col_pos.

      
LOOP AT lt_field_cat ASSIGNING <field_cat>
                                  
WHERE no_out  ' '
                                  
AND   hotspot 'X'.
        
CLEAR <field_cat>-hotspot.
        
MODIFY lt_field_cat FROM <field_cat> TRANSPORTING hotspot.
      
ENDLOOP.
* Use Excel .XLSX format.
      lt_choice 
cl_salv_export_xml_dialog=>get_gui_spreadsheet_formats(  ).
      
READ TABLE lt_choice INTO ls_choice
           
WITH KEY xml_type '10'.

      
GET REFERENCE OF g_it_vbak INTO lr_data.


      r_result_data 
cl_salv_ex_util=>factory_result_data_table(
       r_data                      
lr_data
       t_fieldcatalog              
lt_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      
ls_choice-xml_type
          xml_version   
l_version
          r_result_data 
r_result_data
          xml_flavour   
if_salv_bs_c_tt=>c_tt_xml_flavour_export
        
IMPORTING
          xml           
l_xml.
* convert the text string into UTF-16LE binary data including
* byte-order-mark. Mircosoft Excel prefers these settings
* all this is done by new class cl_bcs_convert (see note 1151257)

      
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
        
EXPORTING
          
buffer        l_xml
        
IMPORTING
          output_length 
lv_length
        
TABLES
          binary_tab    
lv_binary_content.
      lv_size 
lv_length.
*Attachment for with Ack
* Convert ALV in current layout to Excel and send as attachement
*        CALL private mehtod TO START email transmission USING bcs_class
      lv_size2 
lv_length.
*        lv_receiver = im_name.
      lv_email_subject 
text-t15.
*  Email body

      la_message
-line text-t14 .
      
APPEND la_message TO lt_messages.
*  Send email

      
TRY .
*     -------- create persistent send request ------------------------
          lo_send_request 
cl_bcs=>create_persistent( ).
*     -------- create and set document with attachment ---------------
          lo_document 
cl_document_bcs=>create_document(
            i_type    
im_doc_type
            i_text    
lt_messages
            i_subject 
lv_email_subject ).                 "#EC NOTEXT
*            IF im_attachment_name IS INITIAL.
          lv_attach_name 
text-t15.
*            ENDIF.
*            IF im_content_hex IS NOT INITIAL.
**     Add the document as and attachment to email document object
          lo_document
->add_attachment(
            i_attachment_type    
im_attachment_type
            i_attachment_subject 
lv_attach_name
            i_attachment_size    
lv_size
            i_att_content_hex    
lv_binary_content
            
).
*            ENDIF.
*     add document object to send request
          lo_send_request
->set_documentlo_document ).
* --------- Set the sender to the User Running the Report ---------
          lo_sender 
cl_sapuser_bcs=>createsy-uname ).
          lo_send_request
->set_senderlo_sender ).

          
LOOP AT s_mail[] INTO s_mail.
            lv_mailto 
s_mail-low.

            
CHECK lv_mailto IS NOT INITIAL.
            lo_recipient 
cl_cam_address_bcs=>create_internet_addresslv_mailto ).
          
CLEAR lv_mailto.
*     add recipient object to send request
            
CALL METHOD lo_send_request->add_recipient
              
EXPORTING
                i_recipient 
lo_recipient
                i_express   
'X'.
            
FREElo_recipient.
          
ENDLOOP.
*     ---------- send document ---------------------------------------
          lv_email_sent 
lo_send_request->sendi_with_error_screen 'X' ).

          
COMMIT WORK.

          
IF lv_email_sent IS INITIAL.
            
MESSAGE i500(sbcomsWITH s_mail-low.
          
ELSE.
            
MESSAGE s022(so).
          
ENDIF.
*   ------------ exception handling ----------------------------------
*   replace this rudimentary exception handling with your own one !!!
        
CATCH cx_bcs INTO lo_bcs_exception.
          
MESSAGE i865(soWITH lo_bcs_exception->error_type.

      
ENDTRY.
    
ENDMETHOD.                           "send_email

  
ENDCLASS.                              "lcl_email IMPLEMENTATION