Friday, August 24, 2012

SD - Billing: automatic email when create invoice: VF01

setting to be done:

1.settings with tcode:
vv32.
nace --> v3-billing--> put your form name, driver program and routine.

2. write routine in driver program to email.

vv32 settings:

for email, set medium to 5-external send and date/time to 4 - immediately.




nace settings:
select you application. IN this case, its V3 - billing, then click output types.

highlight outputtype (ZD00) and click on processing routines


Fill in required details.


Create routines in dirver program:
example:

REPORT zsd_rlb_invoice_zd00.

* declaration of data
INCLUDE rlb_invoice_data_declare.
* definition of forms
INCLUDE rlb_invoice_form01.
INCLUDE rlb_print_forms.
INCLUDE zrlb_top.                                           " crq6257
DATA: gw_title TYPE string,
      gw_date  TYPE string.

*---------------------------------------------------------------------*
*       FORM ENTRY
*---------------------------------------------------------------------*
FORM entry USING return_code us_screen.

  DATA: lf_retcode TYPE sy-subrc.
  CLEAR retcode.
  xscreen = us_screen.
  PERFORM processing USING us_screen
                     CHANGING lf_retcode.
  IF lf_retcode NE 0.
    return_code = 1.
  ELSE.
    return_code = 0.
  ENDIF.

ENDFORM.                    "ENTRY
*---------------------------------------------------------------------*
*       FORM PROCESSING                                               *
*---------------------------------------------------------------------*
FORM processing USING proc_screen
                CHANGING cf_retcode.

  DATA: ls_print_data_to_read TYPE lbbil_print_data_to_read.
  DATA: ls_bil_invoice        TYPE lbbil_invoice.
  DATA: lf_fm_name            TYPE rs38l_fnam.
  DATA: ls_control_param      TYPE ssfctrlop.
  DATA: ls_composer_param     TYPE ssfcompop.
  DATA: ls_recipient          TYPE swotobjid.
  DATA: ls_sender             TYPE swotobjid.
  DATA: lf_formname           TYPE tdsfname.
  DATA: ls_addr_key           LIKE addr_key.
  DATA: ls_dlv-land           LIKE vbrk-land1.
  DATA: ls_job_info           TYPE ssfcrescl.
  DATA: ls_totqty             TYPE i.                       "SR304987
  DATA: l_fkart2              TYPE c.                       "SR304987

* SmartForm from customizing table TNAPR
  lf_formname = tnapr-sform.

* determine print data
  PERFORM set_print_data_to_read USING    lf_formname
                                 CHANGING ls_print_data_to_read
                                 cf_retcode.

  IF cf_retcode = 0.
* select print data
    PERFORM get_data USING    ls_print_data_to_read
                     CHANGING ls_addr_key
                              ls_dlv-land
                              ls_bil_invoice
                              cf_retcode.
  ENDIF.

  IF cf_retcode = 0.
    PERFORM set_print_param USING    ls_addr_key
                                     ls_dlv-land
                            CHANGING ls_control_param
                                     ls_composer_param
                                     ls_recipient
                                     ls_sender
                                     cf_retcode.
  ENDIF.

  IF cf_retcode = 0.
* determine smartform function module for invoice
    CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
         EXPORTING  formname           = lf_formname
*                 variant            = ' '
*                 direct_call        = ' '
         IMPORTING  fm_name            = lf_fm_name
         EXCEPTIONS no_form            = 1
                    no_function_module = 2
                    OTHERS             = 3.
    IF sy-subrc <> 0.
*   error handling
      cf_retcode = sy-subrc.
      PERFORM protocol_update.
    ENDIF.
  ENDIF.

  PERFORM reverse_by_bill_type CHANGING ls_bil_invoice.     "26.06.09

  PERFORM f_checkqty USING    ls_bil_invoice                "SR304987
                     CHANGING ls_totqty                     "SR304987
                              l_fkart2.                     "SR304987

  IF ls_totqty > 0.                                         "SR304987
    IF cf_retcode = 0.
      PERFORM check_repeat.                                 "SR304987
*      PERFORM zcheck_repeat.                                "SR304987
      IF ls_composer_param-tdcopies EQ 0.
        nast_anzal = 1.
      ELSE.
        nast_anzal = ls_composer_param-tdcopies.
      ENDIF.
      ls_composer_param-tdcopies = 1.
*      DO nast_anzal TIMES.
* In case of repetition only one time archiving
      IF sy-index > 1 AND nast-tdarmod = 3.
        nast_tdarmod = nast-tdarmod.
        nast-tdarmod = 1.
        ls_composer_param-tdarmod = 1.
      ENDIF.
      IF sy-index NE 1 AND repeat IS INITIAL.
        repeat = 'X'.
      ENDIF.

      ls_composer_param-tdnewid     = 'X'.
      ls_composer_param-tdtelenum   = ls_bil_invoice-hd_gen-bil_number.
      ls_composer_param-tdtelenume  = ls_bil_invoice-hd_gen-sold_to_party.
      ls_composer_param-tddivision  = 'X'.

* crq6257
* The following validation is for bil type ZEUR, etc, etc.
      IF nast-nacha EQ '5' AND
          ( ls_bil_invoice-hd_gen-bil_type = 'ZEUR' OR
           ls_bil_invoice-hd_gen-bil_type = 'ZCWR' OR
           ls_bil_invoice-hd_gen-bil_type = 'ZEWR' OR
           ls_bil_invoice-hd_gen-bil_type = 'ZCN1' OR
           ls_bil_invoice-hd_gen-bil_type = 'ZCN2').


*Calling the SMARTFORM using the function module retrieved above
*GET_OTF parameter in the CONTROL_PARAMETERS is set to get the OTF
*format of the output
        CLEAR: ls_control_param-device.
        ls_control_param-no_dialog = 'X'.
        ls_control_param-preview = space. "Suppressing the dialog box for print preview
        ls_control_param-getotf = 'X'.

* call smartform invoice
        CALL FUNCTION lf_fm_name
          EXPORTING
            archive_index        = toa_dara
            archive_parameters   = arc_params
            control_parameters   = ls_control_param
            output_options       = ls_composer_param
            user_settings        = space
            is_bil_invoice       = ls_bil_invoice
            is_nast              = nast
            is_repeat            = repeat
            lt_title             = gw_title
            lt_date_type         = gw_date
          IMPORTING
            job_output_info    = ls_job_info
          EXCEPTIONS
            formatting_error     = 1
            internal_error       = 2
            send_error           = 3
            user_canceled        = 4
            OTHERS               = 5.

        IF sy-subrc <> 0.
*   error handling
          cf_retcode = sy-subrc.
          PERFORM protocol_update.
* get SmartForm protocoll and store it in the NAST protocoll
          PERFORM add_smfrm_prot.
        ENDIF.

        lt_job_info       = ls_job_info.
        lt_bil_invoice    = ls_bil_invoice .
        lt_composer_param = ls_composer_param.

        PERFORM f_get_recipients.
        PERFORM f_convert_pdf.
        PERFORM f_email_body.
        PERFORM f_packing_list.
        PERFORM f_send_email.

      ELSE.

* call smartform invoice
        CALL FUNCTION lf_fm_name
             EXPORTING
                        archive_index        = toa_dara
                        archive_parameters   = arc_params
                        control_parameters   = ls_control_param
*                 mail_appl_obj        =
                        mail_recipient       = ls_recipient
                        mail_sender          = ls_sender
                        output_options       = ls_composer_param
                        user_settings        = space
                        is_bil_invoice       = ls_bil_invoice
                        is_nast              = nast
                        is_repeat            = repeat
                        lt_title             = gw_title
                        lt_date_type         = gw_date
             IMPORTING  job_output_info      = ls_job_info
*                     document_output_info =
*                     job_output_options   =
             EXCEPTIONS formatting_error     = 1
                        internal_error       = 2
                        send_error           = 3
                        user_canceled        = 4
                        OTHERS               = 5.
        IF sy-subrc <> 0.
*   error handling
          cf_retcode = sy-subrc.
          PERFORM protocol_update.
* get SmartForm protocoll and store it in the NAST protocoll
          PERFORM add_smfrm_prot.
        ENDIF.
*      ENDDO.
      ENDIF.


* get SmartForm spoolid and store it in the NAST protocoll
      DATA ls_spoolid LIKE LINE OF ls_job_info-spoolids.
      LOOP AT ls_job_info-spoolids INTO ls_spoolid.
        IF ls_spoolid NE space.
          PERFORM protocol_update_spool USING '342' ls_spoolid
                                              space space space.
        ENDIF.
      ENDLOOP.
      ls_composer_param-tdcopies = nast_anzal.
      IF NOT nast_tdarmod IS INITIAL.
        nast-tdarmod = nast_tdarmod.
        CLEAR nast_tdarmod.
      ENDIF.

    ENDIF.
  ELSE.                                                     "SR304987
    IF sy-batch NE 'X'.                                     "SR304987
      MESSAGE s899(bd) WITH 'Invoice has a 0 quantity & cannot be printed'"SR304987
      EXIT.
    ELSE.
      WRITE 'batch'.
    ENDIF.                                                  "SR304987
  ENDIF.                                                    "SR304987
* get SmartForm protocoll and store it in the NAST protocoll
* PERFORM ADD_SMFRM_PROT.

ENDFORM.                    "PROCESSING

*start of insertion SR304987
*&---------------------------------------------------------------------*
*&      Form  F_CHECKQTY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_checkqty USING ls_bil_invoice TYPE lbbil_invoice
                CHANGING ls_totqty TYPE i
                         l_fkart2  TYPE c.

  TYPESBEGIN OF gty_it_gen.
          INCLUDE STRUCTURE lbbil_it_gen.
  TYPESEND OF gty_it_gen.

  RANGES: r_fkart FOR vbrk-fkart.

  DATA: gs_it_gen    TYPE gty_it_gen,
        l_fkart      LIKE vbrk-fkart
        .

*Assign range with document type
  r_fkart-low = 'F1'.
  r_fkart-option = 'EQ'.
  r_fkart-sign = 'I'.
  APPEND r_fkart.

  r_fkart-low = 'F2'.
  r_fkart-option = 'EQ'.
  r_fkart-sign = 'I'.
  APPEND r_fkart.

  r_fkart-low = 'ZDIS'.
  r_fkart-option = 'EQ'.
  r_fkart-sign = 'I'.
  APPEND r_fkart.

  r_fkart-low = 'ZEUR'.
  r_fkart-option = 'EQ'.
  r_fkart-sign = 'I'.
  APPEND r_fkart.

  r_fkart-low = 'ZTRS'.
  r_fkart-option = 'EQ'.
  r_fkart-sign = 'I'.
  APPEND r_fkart.

  CLEAR l_fkart2.
  CLEAR ls_totqty.

  READ TABLE ls_bil_invoice-it_gen INTO gs_it_gen INDEX 1.
  IF sy-subrc = 0.

    SELECT SINGLE fkart INTO l_fkart FROM vbrk
    WHERE vbeln = gs_it_gen-bil_number AND
          fkart IN r_fkart.
    IF sy-subrc = 0.
      l_fkart2 = 'X'.
    ENDIF.

    IF l_fkart2 EQ 'X'.
      SELECT SUM( fkimg ) FROM vbrp INTO ls_totqty
      WHERE vbeln = gs_it_gen-bil_number.
    ELSE.
      ls_totqty = '1'.
    ENDIF.

  ENDIF.

ENDFORM.                    " F_CHECKQTY
*End of insertion SR304987

*&---------------------------------------------------------------------*
*&      Form  ZCHECK_REPEAT
*&---------------------------------------------------------------------*
FORM zcheck_repeat .

  TYPESBEGIN OF ty_nast.
          INCLUDE STRUCTURE nast.
  TYPESEND OF ty_nast.

  DATA: t_nast TYPE TABLE OF ty_nast WITH HEADER LINE.

  CLEAR repeat.
  CLEAR t_nast.
  REFRESH t_nast.

*  SELECT * INTO *NAST FROM NAST WHERE KAPPL = NAST-KAPPL
*  WHERE KAPPL = NAST-KAPPL
*  AND   OBJKY = NAST-OBJKY
*  AND   KSCHL = NAST-KSCHL
*  AND   SPRAS = NAST-SPRAS
*  AND   PARNR = NAST-PARNR
*  AND   PARVW = NAST-PARVW
*  AND   NACHA BETWEEN '1' AND '4'.

  SELECT objky kschl spras parnr parvw vstat FROM nast
  INTO CORRESPONDING FIELDS OF TABLE t_nast
  WHERE kappl = nast-kappl
  AND   objky = nast-objky
  AND   kschl = nast-kschl
  AND   spras = nast-spras
  AND   parnr = nast-parnr
  AND   parvw = nast-parvw
  AND   nacha BETWEEN '1' AND '4'.

  DELETE ADJACENT DUPLICATES FROM t_nast.

  LOOP AT t_nast WHERE vstat = '1'.
    repeat = 'X'.
  ENDLOOP.

*    IF *NAST-VSTAT = '1'.
*      REPEAT = 'X'.
*    ENDIF.
*  ENDSELECT.


ENDFORM.                    " CHECK_REPEAT

*&---------------------------------------------------------------------*
*&      Form  REVERSE_BY_BILL_TYPE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM reverse_by_bill_type  CHANGING ps_bil_invoice TYPE lbbil_invoice.

  FIELD-SYMBOLS:
     TYPE lbbil_it_price,
      TYPE lbbil_hd_kond.

  CASE ps_bil_invoice-hd_gen-bil_type.
    WHEN 'ZLEP' OR  'ZLEF' OR 'ZFRA' OR 'ZFRH'.
      IF ps_bil_invoice-hd_gen-bil_netwr < 0.
        ps_bil_invoice-hd_gen-bil_netwr = ps_bil_invoice-hd_gen-bil_netwr * -1.
        ps_bil_invoice-hd_gen-bil_disc = ps_bil_invoice-hd_gen-bil_disc * -1.
        ps_bil_invoice-hd_gen-bil_tax = ps_bil_invoice-hd_gen-bil_tax * -1.
        ps_bil_invoice-hd_gen-dpnet = ps_bil_invoice-hd_gen-dpnet * -1.
        ps_bil_invoice-hd_gen-dpend = ps_bil_invoice-hd_gen-dpend * -1.
        ps_bil_invoice-hd_gen-dpmws_end = ps_bil_invoice-hd_gen-dpmws_end * -1.
        ps_bil_invoice-hd_gen-dpmws = ps_bil_invoice-hd_gen-dpmws * -1.
        LOOP AT ps_bil_invoice-it_price ASSIGNING .
          -netwr = -netwr * -1.
          -mwsbp = -mwsbp * -1.
          -netpr = -netpr * -1.
        ENDLOOP.

        LOOP AT ps_bil_invoice-hd_kond ASSIGNING .
          -kwert = -kwert * -1.
          -kawrt = -kawrt * -1.
        ENDLOOP.

        gw_date = 'Invoice Date (Tax Point)'.

        IF ps_bil_invoice-hd_gen-bil_type = 'ZLEP' OR ps_bil_invoice-hd_gen-bil_type = 'ZLEF'.
          SELECT  SINGLE vtext
              FROM  tvfkt
              INTO  gw_title
              WHERE spras EQ sy-langu
              AND   fkart EQ ps_bil_invoice-hd_gen-bil_type.
        ELSE.
          gw_title = 'Framework Invoice'.
        ENDIF.

      ELSE.
        IF ps_bil_invoice-hd_gen-bil_type = 'ZLEP' OR ps_bil_invoice-hd_gen-bil_type = 'ZLEF'.
          gw_title = 'Levy Credit'.
        ELSE.
          gw_title = 'Framework Credit'.
        ENDIF.

        gw_date = 'Credit Note Date (Tax Point)'.
      ENDIF.
    WHEN OTHERS.
  ENDCASE.
ENDFORM.                    " REVERSE_BY_BILL_TYPE
*&---------------------------------------------------------------------*
*&      Form  F_SEND_EMAIL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_send_email .

  CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
    EXPORTING
      document_data                    = wa_document_data
     put_in_outbox                    = 'X'
     sender_address                   = gv_sender
     sender_address_type              = 'SMTP'
*     commit_work                      = '' "commit work will be done by ME29N
   IMPORTING
     sent_to_all                      = wa_sent_to_all
    TABLES
      packing_list                     = it_packing_list
     contents_bin                     = it_pdf
     contents_txt                     = it_body_msg
      receivers                        = it_receivers
   EXCEPTIONS
     too_many_receivers               = 1
     document_not_sent                = 2
     document_type_not_exist          = 3
     operation_no_authorization       = 4
     parameter_error                  = 5
     x_error                          = 6
     enqueue_error                    = 7
     OTHERS                           = 8
            .
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

** Equivalent for the Scot transaction, that is not automatic
*SUBMIT rsconn01 AND RETURN.


*  IF sy-sysid = 'ECD'.
*    WAIT UP TO 5 SECONDS.
*    SUBMIT rsconn01
*      WITH mode = 'INT'
*      WITH output = ''
*      AND RETURN.
*  ENDIF.

  CLEAR: gv_bin_filesize,
*          gv_attachment_name,
         wa_document_data,
         wa_receivers,
         wa_packing_list,
         wa_body_msg,
         wa_otf_data,
         wa_pdf.

  REFRESH: it_packing_list,
           it_pdf,
           it_body_msg,
           it_receivers,
           it_pdfdata,
           it_otf_final.


ENDFORM.                    " F_SEND_EMAIL
*&---------------------------------------------------------------------*
*&      Form  F_GET_RECIPIENTS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_get_recipients.

  DATA: lv_adrnr TYPE kna1-adrnr,
        lv_stp   TYPE lbbil_invoice-hd_gen-sold_to_party.


  lv_stp = lt_bil_invoice-hd_gen-sold_to_party.

  SELECT SINGLE adrnr INTO lv_adrnr
     FROM kna1
    WHERE kunnr = lv_stp.

  IF sy-subrc EQ 0.

    SELECT SINGLE smtp_addr INTO gv_recepient
      FROM adr6
      WHERE addrnumber = lv_adrnr.

  ENDIF.

  wa_receivers-receiver = gv_recepient.
  wa_receivers-express = 'X'.
  wa_receivers-com_type = 'INT'.
  wa_receivers-rec_type = 'U'"Internet address
  wa_receivers-notif_del = 'X'" request delivery notification
  wa_receivers-notif_ndel = 'X'" request not delivered notification
  APPEND wa_receivers TO it_receivers.
  CLEAR: wa_receivers.



ENDFORM.                    " F_GET_RECIPIENTS
*&---------------------------------------------------------------------*
*&      Form  F_CONVERT_PDF
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_convert_pdf .


* APPENDING THE OTF DATA INTO THE FINAL TABLE
  it_otf_final[] = lt_job_info-otfdata[].

* CONVERTING OTF DATA INTO PDF DATA
  CALL FUNCTION 'CONVERT_OTF'
    EXPORTING
      format                = 'PDF'
    IMPORTING
      bin_filesize          = gv_bin_filesize
    TABLES
      otf                   = it_otf_final
      lines                 = it_pdfdata[]
    EXCEPTIONS
      err_max_linewidth     = 1
      err_format            = 2
      err_conv_not_possible = 3
      err_bad_otf           = 4
      OTHERS                = 5.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDFORM.                    " F_CONVERT_PDF
*&---------------------------------------------------------------------*
*&      Form  F_EMAIL_BODY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_email_body .

  DATA: lv_title TYPE ssfcompop-tdtitle.

* To send data as email attachment, we need to have a table of SOLISTI1.
* This table contains line size of 255 characters. Below function module
* does the trick of changing the table from X character sized lines into
* any given Y character sized lines.

  CALL FUNCTION 'SX_TABLE_LINE_WIDTH_CHANGE'
    EXPORTING
      line_width_dst              = '255'
    TABLES
      content_in                  = it_pdfdata[]
      content_out                 = it_pdf[]
    EXCEPTIONS
      err_line_width_src_too_long = 1
      err_line_width_dst_too_long = 2
      err_conv_failed             = 3
      OTHERS                      = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

* SUBJECT OF THE MAIL.
  CONCATENATE lt_composer_param-tdtitle' : ' lt_bil_invoice-hd_gen-bil_number
         INTO lv_title SEPARATED BY space.

  wa_document_data-obj_name = 'MAIL_TO_HEAD'.
  wa_document_data-obj_descr = lv_title.

* BODY OF THE MAIL
  wa_body_msg = 'Hi,'.
  APPEND wa_body_msg TO it_body_msg.
  CLEAR wa_body_msg.

  wa_body_msg = 'This is test body email for Billing Document creation.'.
  APPEND wa_body_msg TO it_body_msg.
  CLEAR wa_body_msg.
ENDFORM.                    " F_EMAIL_BODY
*&---------------------------------------------------------------------*
*&      Form  F_PACKING_LIST
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_packing_list .

* WRITE PACKING LIST FOR BODY
  DESCRIBE TABLE it_body_msg LINES gv_tab_lines.
  wa_packing_list-head_start = 1.
  wa_packing_list-head_num = 0.
  wa_packing_list-body_start = 1.
  wa_packing_list-body_num = gv_tab_lines.
  wa_packing_list-doc_type = 'RAW'.
  APPEND wa_packing_list TO it_packing_list.
  CLEAR wa_packing_list.

* WRITE PACKING LIST FOR ATTACHMENT
  wa_packing_list-transf_bin = 'X'.
  wa_packing_list-head_start = 1.
  wa_packing_list-head_num = 1.
  wa_packing_list-body_start = 1.

  DESCRIBE TABLE it_pdf LINES wa_packing_list-body_num.
  wa_packing_list-doc_type = 'PDF'.
*    w_packing_list-obj_descr = 'PDF Attachment'.
  wa_packing_list-obj_descr = lt_composer_param-tdtitle.
  wa_packing_list-obj_name = 'PDF_ATTACHMENT'.
  wa_packing_list-doc_size = wa_packing_list-body_num * 255.
  APPEND wa_packing_list TO it_packing_list.
  CLEAR wa_packing_list.

* FILL THE DOCUMENT DATA &GET SIZE OF ATTACHMENT
  wa_document_data-obj_langu = sy-langu.
  wa_document_data-doc_size = ( gv_tab_lines - 1 ) * 255 + STRLEN( wa_attachment ).

ENDFORM.                    " F_PACKING_LIST



1 comment:

  1. Very informative and impressive post you have written, this is quite interesting and i have went through it completely, an upgraded information is shared, keep sharing such valuable information. Send Invoices by Email

    ReplyDelete