Code20090401 - ooxxvv/MySAPnotes GitHub Wiki

Code20090401

  • 將 Interactive Form 附檔在 eMail 中寄出或 Download

Code

************************************************************************
**Program name: SEND_PDF_FORM
**Description : Offline Interactive Form - Send
**Date/Author : 2009.12.16 / Joey Lee
************************************************************************
** M O D I F I C A T I O N  L O G
************************************************************************
** ChangeDate  Programmer  Request     Description
** ==========  ==========  ==========  ==================
** 2009.12.16  Joey Lee                New Program
************************************************************************
REPORT ztestpdf_r001 NO STANDARD PAGE HEADING
                        LINE-SIZE 135
                        LINE-COUNT 58(2).
*=======================================================================
*=================================================Global Date declear===
DATA:
*wa_vndbnk     LIKE zvk_vndbnk,
      wa_formoutput TYPE fpformoutput,
      wa_zekko LIKE ekko,
      it_zekpo TYPE ztestpdf_ty001.

*=======================================================================
*=====================================================Selection Sceen===
PARAMETERS: p_outdir LIKE rlgrap-filename OBLIGATORY.

*=======================================================================
*========================================================Initial Data===
*LOAD-OF-PROGRAM
*INITIALIZATION.

*=======================================================================
*=============================================Selection Screen Events===
*AT SELECTION-SCREEN OUTPUT.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_outdir.

  PERFORM get_directory_name USING    'P_OUTDIR'
                             CHANGING p_outdir.

*AT SELECTION-SCREEN.
*AT SELECTION-SCREEN ON <f>.
*AT SELECTION-SCREEN ON BLOCK <>.

*=======================================================================
*=======================================================Report Events===
START-OF-SELECTION.
*-Get data
  PERFORM get_data.
*-Generate Interactive Form (PDF)
  PERFORM generate_pdf_form.
*-Down Load
  PERFORM download_file.
*  PERFORM send_mail.

END-OF-SELECTION.

*=======================================================================
*=========================================================List Events===
*TOP-OF-PAGE.
*END-OF-PAGE.
*AT LINE-SELECTION.
*TOP-OF-PAGE DURING LINE-SELECTION.
*AT USER-COMMAND.

*=======================================================================
*=========================================================Subroutines===
*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM get_data .

  DATA: lt_zekpo LIKE LINE OF it_zekpo OCCURS 0 WITH HEADER LINE.

  SELECT SINGLE *
    INTO CORRESPONDING FIELDS OF wa_zekko
    FROM ekko
    WHERE ebeln EQ '3000000007'.

  SELECT *
    INTO CORRESPONDING FIELDS OF TABLE lt_zekpo
    FROM ekpo
    WHERE ebeln EQ '3000000007'.


  it_zekpo = lt_zekpo[].
*
*  BREAK-POINT.

ENDFORM.                    " get_data
*&---------------------------------------------------------------------*
*&      Form  generate_pdf_form
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM generate_pdf_form .
  DATA: l_funcname      TYPE funcname,
        lw_outputparams TYPE sfpoutputparams,
        lw_docparams    TYPE sfpdocparams.

*-First get name of the generated function module
*  CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
*    EXPORTING
*      i_name     = 'ZTESTPDF001'
*    IMPORTING
*      e_funcname = l_funcname.

*-Set output parameters and open spool job
*---------------------------suppress printer dialog popup
  lw_outputparams-nodialog = 'X'.
*---------------------------launch print preview
  lw_outputparams-getpdf   = 'X'.


*-Open spool job
  CALL FUNCTION 'FP_JOB_OPEN'
    CHANGING
      ie_outputparams = lw_outputparams
    EXCEPTIONS
      cancel          = 1
      usage_error     = 2
      system_error    = 3
      internal_error  = 4
      OTHERS          = 5.


*-Set form language and country (->form locale)
*  lw_docparams-langu    = 'E'.
*  lw_docparams-country  = 'US'.
  lw_docparams-fillable = 'X'.
  lw_docparams-dynamic  = 'X'.

CALL FUNCTION '/1BCDWB/SM00000126'
 EXPORTING
   /1BCDWB/DOCPARAMS        = lw_docparams
*   EBELN                    = '3000000007'
 IMPORTING
   /1BCDWB/FORMOUTPUT       = wa_formoutput
 EXCEPTIONS
   USAGE_ERROR              = 1
   SYSTEM_ERROR             = 2
   INTERNAL_ERROR           = 3
   OTHERS                   = 4.

  IF sy-subrc NE 0.
    MESSAGE i000(398) WITH 'Generated Form Error, SY-SUBRC=' sy-subrc.
    STOP.
  ENDIF.

*-Close spool job
  CALL FUNCTION 'FP_JOB_CLOSE'
    EXCEPTIONS
      usage_error    = 1
      system_error   = 2
      internal_error = 3
      OTHERS         = 4.

ENDFORM.                    " generate_pdf_form
*&---------------------------------------------------------------------*
*&      Form  send_mail
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM send_mail .
  DATA: lt_att_content_hex TYPE solix_tab,
        lo_send_request    TYPE REF TO cl_bcs VALUE IS INITIAL,
        lt_message_body    TYPE bcsy_text VALUE IS INITIAL,
        lo_document        TYPE REF TO cl_document_bcs VALUE IS INITIAL,
        lx_document_bcs    TYPE REF TO cx_document_bcs VALUE IS INITIAL,
        lo_sender          TYPE REF TO if_sender_bcs VALUE IS INITIAL,
        l_send             TYPE adr6-smtp_addr VALUE '[email protected]',
        lo_recipient       TYPE REF TO if_recipient_bcs VALUE IS INITIAL,
        lv_sent_to_all(1)  TYPE c VALUE IS INITIAL.

*-Conver PDF to Binary
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer     = wa_formoutput-pdf
    TABLES
      binary_tab = lt_att_content_hex.

  CLASS cl_bcs DEFINITION LOAD.
  lo_send_request = cl_bcs=>create_persistent( ).

*-Message body and subject
  APPEND 'Dear Vendor,' TO lt_message_body.
  APPEND ' '            TO lt_message_body.
  APPEND 'Please fill the attached form and send it back to us.'
                        TO lt_message_body.
  APPEND ' '            TO lt_message_body.
  APPEND 'Thank You,'   TO lt_message_body.

  lo_document = cl_document_bcs=>create_document(
                                   i_type    = 'RAW'
                                   i_text    = lt_message_body
                                   i_subject = 'Vendor Payment Form' ).
*-Add attachment
  TRY.
      lo_document->add_attachment(
                     EXPORTING
                       i_attachment_type    = 'PDF'
                       i_attachment_subject = 'Vendor Payment Form'
                       i_att_content_hex    = lt_att_content_hex ).
    CATCH cx_document_bcs INTO lx_document_bcs.
  ENDTRY.

*-Pass the document to send request
  lo_send_request->set_document( lo_document ).

*-Create sender
  lo_sender = cl_cam_address_bcs=>create_internet_address( l_send ).

*-Set sender
  lo_send_request->set_sender(
                     EXPORTING
                       i_sender = lo_sender ).

*-Create recipient
  lo_recipient = cl_sapuser_bcs=>create( sy-uname ).

*-Set recipient
  lo_send_request->add_recipient(
                     EXPORTING
                       i_recipient = lo_recipient
                       i_express   = 'X' ).

  lo_send_request->add_recipient(
                     EXPORTING
                       i_recipient = lo_recipient
                       i_express   = 'X' ).

* Send email
  lo_send_request->send(
                     EXPORTING
                       i_with_error_screen = 'X'
                     RECEIVING
                       result = lv_sent_to_all ).
  COMMIT WORK.

  MESSAGE 'The payment form has been emailed to the Vendor' TYPE 'I'.

ENDFORM.                    " send_mail
*&---------------------------------------------------------------------*
*&      Form  GET_DIRECTORY_NAME
*----------------------------------------------------------------------*
FORM get_directory_name USING    x_file_field_name
                     CHANGING    y_directory_name.

  CLASS cl_gui_frontend_services DEFINITION LOAD.
  DATA : lv_file_path TYPE rlgrap-filename,
         lv_file_name TYPE rlgrap-filename,
         lv_outdir    TYPE string,
         lv_directory TYPE string.

  CONSTANTS : lc_window_title TYPE string
                        VALUE 'Select HTML download folder'.

  lv_directory = y_directory_name.

  CALL METHOD cl_gui_frontend_services=>directory_browse
    EXPORTING
      window_title         = lc_window_title
      initial_folder       = lv_directory
    CHANGING
      selected_folder      = lv_outdir
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3.



  IF sy-subrc EQ 0.
    p_outdir = lv_outdir.
  ENDIF.

ENDFORM.                    " GET_DIRECTORY_NAME
*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD_FILE
*&---------------------------------------------------------------------*
FORM download_file .

  DATA: lt_att_content_hex TYPE solix_tab,
        l_filename TYPE string,
        l_bin_filesize TYPE i.

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer        = wa_formoutput-pdf
    IMPORTING
      output_length = l_bin_filesize
    TABLES
      binary_tab    = lt_att_content_hex.

  l_filename = p_outdir.

  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      bin_filesize            = l_bin_filesize
      filename                = l_filename
      filetype                = 'BIN'
      CONFIRM_OVERWRITE       = 'X'
    TABLES
      data_tab                = lt_att_content_hex
    EXCEPTIONS
      file_write_error        = 1
      no_batch                = 2
      gui_refuse_filetransfer = 3
      invalid_type            = 4
      no_authority            = 5
      unknown_error           = 6
      header_not_allowed      = 7
      separator_not_allowed   = 8
      filesize_not_allowed    = 9
      header_too_long         = 10
      dp_error_create         = 11
      dp_error_send           = 12
      dp_error_write          = 13
      unknown_dp_error        = 14
      access_denied           = 15
      dp_out_of_memory        = 16
      disk_full               = 17
      dp_timeout              = 18
      file_not_found          = 19
      dataprovider_exception  = 20
      control_flush_error     = 21
      OTHERS                  = 22.

  IF sy-subrc <> 0.
    EXIT.
  ENDIF.

ENDFORM.                    " DOWNLOAD_FILE