Program:ZSDR012

T-code: ZSD012

可以后台手动运行,每天发送billing document To customer

1、  界面上输入什么日期,发送前一天的 billing document

2、  ZSDBill 表中必须有数据

*&---------------------------------------------------------------------*
*& Progarm      :  ZSDR012                        Author : Jimmy Wong
*& Created      :  28 Nov 2012                    App    : SD
*& Title        :  Send Billing Document to Customer with PDF Attachment
*& Description  :  Send Billing Document to Customer with PDF Attachment
*&---------------------------------------------------------------------*
*&  Version       Author      Date        description
*&                Jimmy       28 Nov 2012  the first version
*&M1              Jimmy       25 Jan 2013  Manual invoice maintenance Item
*&  the last update time  2013.01.29 09:00
*&---------------------------------------------------------------------*

report  zsdr012 no standard page heading.
*&---------------------------------------------------------------------*
* database table
*&---------------------------------------------------------------------*
tables:vbrk,adr6.
constants:gc_x    type char1 value 'X',
          gc_u    type char1 value 'U',
          gc_nosend type char20 value 'NO SEND', "NO SEND a
          gc_resend type char20 value 'RE SEND', "RE SEND b
          gc_sended type char20 value 'SENDT'. "SENDT c

data: begin of gt_vbrk occurs 0,
      vbeln like vbrk-vbeln,  "billing No.
      fkart like vbrk-fkart,  "billing type
      vkorg like vbrk-vkorg,  "sales org
      fkdat like vbrk-fkdat,  "billing date
      kunnr like vbrk-kunrg,  "customer
      zemta type char1,
      aubel like vbrp-aubel,
      aupos like vbrp-aupos,
      message type c length 255,
      end of gt_vbrk.
data: gt_vbrk1 like gt_vbrk occurs 0 with header line.
data: begin of gt_vbeln occurs 0,
     vbeln like vbrk-vbeln,  "billing No.
     end of gt_vbeln.
data: begin of gt_errorlog occurs 0,
      vbeln type vbrk-vbeln,                        "billing No.
      kunnr type vbrk-kunrg,                        "customer
      msgds type string,                            "message
      end of gt_errorlog.
data:begin of it_kna1 occurs 0,
      kunnr like kna1-kunnr,
      adrnr like kna1-adrnr,
    end of it_kna1.
data:begin of it_kunnr occurs 0,
      kunnr like kna1-kunnr,
      cust type c ,
    end of it_kunnr.
data:lt_errorlog like standard table of zmailerror_bill with header line ,
     i_pdf type tline occurs 10 with header line.
data: begin of i_reclist occurs 100,
      receiver type  adr6-smtp_addr,
      end of i_reclist.
data: begin of it_pdf occurs 0,
      vbeln type vbrk-vbeln,
      pdf_xstring type xstring,
      end of it_pdf.
data: begin of it_vbkd occurs 0,                            "M1 add
      vbeln like vbkd-vbeln,
      posnr like vbkd-posnr,
      mrnkz like vbkd-mrnkz,
      end of it_vbkd.

type-pools: slis.
data: fc_hier type slis_t_fieldcat_alv,
      wa_hier type slis_fieldcat_alv .
data: gv_lag        type char1,
      pdf_size type i,
      pdf_xstring type xstring.
*----------------------------------------------------------------------*
*  Parameter & Select-Options                                          *
*----------------------------------------------------------------------*
selection-screen begin of block block1 with frame title text-001.
select-options: s_fkdat for vbrk-fkdat default sy-datum.
select-options: s_vbeln for vbrk-vbeln,                    "Billing NO.
                s_kunrg for vbrk-kunrg,                    "Customer
*                s_fkdat for vbrk-fkdat,                    "Billing Date
                s_fkart for vbrk-fkart,                    "Billing Type
                v_mail for adr6-smtp_addr no intervals.
selection-screen skip.
parameters: p_kschl like nast-kschl default 'ZRD0'.
selection-screen end of block block1 .
*selection-screen begin of block b2 with frame title text-002 no intervals.
*parameters: p_no radiobutton group rad default 'X',
*            p_sent radiobutton group rad,
*            p_all radiobutton group rad.
*selection-screen end of block b2.

selection-screen begin of block b3 with frame title text-003 no intervals.
parameters: p_alv radiobutton group r2 default 'X',
            p_email radiobutton group r2.
selection-screen end of block b3.

initialization.
  clear gv_lag.

start-of-selection.
  perform get_period.
  perform get_data.
  if p_alv = 'X'.
    if gt_vbrk[] is initial.
      message i003(zmm).
      exit.
    endif.
    perform check_data.
    perform build_fieldcat.
    perform print_out.
  else.
    if gt_vbrk[] is initial.
      write:'No Billing Document was found!'.
      exit.
    endif.
    perform email_process.
  endif.
  perform insert_message.
*&---------------------------------------------------------------------*
*&      Form  INSERT_MESSAGE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form insert_message .
  if  gt_errorlog[] is not initial.
    loop at gt_errorlog.
      move-corresponding gt_errorlog to lt_errorlog.
      lt_errorlog-bldat = sy-datum.
      lt_errorlog-aezet = sy-uzeit.
      append lt_errorlog.
    endloop.
    sort lt_errorlog by vbeln bldat aezet.
    delete adjacent duplicates from lt_errorlog[] comparing vbeln bldat aezet.

modify zmailerror_bill from table lt_errorlog[].
    free: lt_errorlog,i_pdf,it_kunnr,it_kna1,gt_errorlog,gt_vbrk.
  endif.
endform.                    " INSERT_MESSAGE
*&---------------------------------------------------------------------*
*&      Form  GET_PERIOD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_period .
  if s_fkdat[] is initial.
    s_fkdat-sign = 'I'.
    s_fkdat-option = 'EQ'.
    s_fkdat-low = sy-datum - 1.
    append s_fkdat.
  else.
    loop at s_fkdat.
      if s_fkdat-low is not initial.
        s_fkdat-low = s_fkdat-low - 1.
      endif.
      if s_fkdat-high is not initial.
        s_fkdat-high = s_fkdat-high - 1.
      endif.
      modify s_fkdat .
    endloop.
  endif.

endform.                    " GET_PERIOD

*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_data .

select distinct a~vbeln a~fkart a~vkorg a~fkdat a~kunrg as kunnr b~zemta
        c~aubel c~aupos
    into corresponding fields of table gt_vbrk
    from vbrk as a inner join zsdbill as b on a~vbeln = b~vbeln
    inner join vbrp as c on a~vbeln = c~vbeln and b~posnr = c~posnr
*    inner join vbkd as d  on c~aubel = d~vbeln and d~posnr = '000000' "M1
    where a~vbeln in s_vbeln
      and a~kunrg in s_kunrg
      and a~fkdat in s_fkdat
      and a~fkart in s_fkart
      and a~fksto ne 'X'
      and b~zemta = ''
      and a~fkart not in ('RE','S1').
*      and d~mrnkz ne 'X'.

if gt_vbrk[] is not initial.
*M1 add
    " check SO item mrnkz 只要存在mrnkz = ‘X’ 则Invoice 不发送
    select vbeln posnr mrnkz
      into corresponding fields of table it_vbkd
      from vbkd
      for all entries in  gt_vbrk
      where vbeln = gt_vbrk-aubel
        and posnr =  gt_vbrk-aupos .

select vbeln posnr mrnkz
      appending corresponding fields of table it_vbkd
      from vbkd
      for all entries in  gt_vbrk
      where vbeln = gt_vbrk-aubel
        and posnr = '000000' .

sort  it_vbkd by vbeln posnr.
    loop at  gt_vbrk.
      read table it_vbkd with key vbeln = gt_vbrk-aubel posnr = gt_vbrk-aupos binary search.
      if sy-subrc eq 0.
        read table it_vbkd with key vbeln = gt_vbrk-aubel posnr = gt_vbrk-aupos mrnkz = 'X'.
        if sy-subrc eq 0.
          gt_vbeln-vbeln = gt_vbrk-vbeln.
          collect gt_vbeln.
          clear: gt_vbeln.
        endif.
      else.
        read table it_vbkd with key vbeln = gt_vbrk-aubel posnr = '000000' mrnkz = 'X' .
        if sy-subrc eq 0.
          gt_vbeln-vbeln = gt_vbrk-vbeln.
          collect gt_vbeln.
          clear: gt_vbeln.
        endif.
      endif.
    endloop.

gt_vbrk1[] = gt_vbrk[].
    clear:gt_vbrk,gt_vbrk[].
    sort gt_vbeln by vbeln.
    loop at gt_vbrk1.
      move-corresponding gt_vbrk1 to gt_vbrk.
      read table gt_vbeln with  key vbeln = gt_vbrk-vbeln binary search.
      if sy-subrc ne 0.
        clear: gt_vbrk-aubel,gt_vbrk-aupos.
        collect gt_vbrk.
        clear: gt_vbrk.
      endif.
    endloop.
*M1 End
    if gt_vbrk[] is not initial.
      select kunnr adrnr
        into corresponding fields of table it_kna1
        from kna1
        for all entries in gt_vbrk
        where kunnr = gt_vbrk-kunnr.

loop at gt_vbrk.
        move-corresponding gt_vbrk to it_kunnr.
        it_kunnr-cust = 'X' .
        collect it_kunnr.
        clear: it_kunnr.
      endloop.
      sort it_kunnr by kunnr.
      sort it_kna1 by kunnr.
      sort gt_vbrk by kunnr vbeln.
    endif.
  endif.
  free:  gt_vbrk1,it_vbkd,gt_vbeln.
endform.                    " GET_DATA
*&---------------------------------------------------------------------*
*&      Form  EMAIL_PROCESS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form email_process .
  clear gv_lag.
  refresh gt_errorlog.
  if p_email = 'X'.
    loop at it_kunnr .
      clear gv_lag.
*     get customer address
      perform get_email_address.
      if gv_lag = gc_x.
        continue.
      endif.

if it_kunnr-cust = 'X'.
        loop at gt_vbrk where kunnr = it_kunnr-kunnr .
          clear gv_lag.

*     Conversion PDF
          perform convert_pdf using gt_vbrk-vbeln.
          if gv_lag = gc_x.
            continue.
          endif.
        endloop.
*     Send email
        if it_pdf[] is not initial.
          perform send_email using it_kunnr-cust.
        else.
          clear:i_reclist[],i_reclist.
        endif.
      else.
        loop at gt_vbrk where kunnr = it_kunnr-kunnr .
          clear gv_lag.

*     Conversion PDF
          perform convert_pdf using gt_vbrk-vbeln.
          if gv_lag = gc_x.
            continue.
          endif.

*     Send email
          perform send_email using it_kunnr-cust.

clear:it_pdf[],it_pdf.
        endloop.
      endif.
      clear:it_pdf[],it_pdf,i_reclist[],i_reclist.
    endloop.
  endif.
endform.                    " EMAIL_PROCESS
*&---------------------------------------------------------------------*
*&      Form  GET_EMAIL_ADDRESS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_email_address .
  data lv_adrnr type adrnr.
  data: begin of lt_adr6 occurs 0,
          smtp_addr type adr6-smtp_addr,
        end of lt_adr6.

* Get Vendor Email Address
  clear lv_adrnr.
  if it_kunnr-kunnr is not initial.
    read table it_kna1 with key kunnr =  it_kunnr-kunnr binary search.
    if sy-subrc = 0.
      select smtp_addr into  table lt_adr6
        from adr6
        where persnumber = space
          and addrnumber = it_kna1-adrnr.
      if sy-subrc = 0.
        loop at lt_adr6.
          if lt_adr6-smtp_addr is not initial.
            clear i_reclist.
            i_reclist-receiver = lt_adr6-smtp_addr.
            translate i_reclist-receiver to upper case.
            append i_reclist.
          endif.
        endloop.
        if i_reclist[] is initial.
          gv_lag            = gc_x.
          gt_errorlog-vbeln = '' .
          gt_errorlog-kunnr = gt_vbrk-kunnr.
          gt_errorlog-msgds = 'Billing-mail address of the Customer is empty, the message was not sent.'.
          append gt_errorlog.
          exit.
        endif.
      else.
        gv_lag            = gc_x.
        gt_errorlog-vbeln = '' .
        gt_errorlog-kunnr = gt_vbrk-kunnr.
        gt_errorlog-msgds = 'No maintenance the Billing Customer-mail address, the message is not sent.'.
        append gt_errorlog.
        " EXIT.
      endif.
    else.
      gv_lag            = gc_x.
      gt_errorlog-vbeln = '' .
      gt_errorlog-kunnr = gt_vbrk-kunnr.
      gt_errorlog-msgds = 'No maintenance the Billing Customer-mail address, the message is not sent.'.
      append gt_errorlog.
    endif.
  else.
    gv_lag            = gc_x.
    gt_errorlog-vbeln = '' .
    gt_errorlog-kunnr = gt_vbrk-kunnr.
    gt_errorlog-msgds = 'Billing Customer, the message was not sent.'.
    append gt_errorlog.
  endif.

endform.                    " GET_EMAIL_ADDRESS
*&---------------------------------------------------------------------*
*&      Form  CONVERT_PDF
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0211   text
*----------------------------------------------------------------------*
form convert_pdf  using  p_vbeln.
  data: w_nast like nast.
  data: it_otf like table of itcoo with header line.

data: control_parameters type ssfctrlop,
        output_options     type ssfcompop.

select single * from nast into w_nast
    where objky = p_vbeln
      and kappl = 'V3'
      and kschl = p_kschl
      and spras = sy-langu.
  if sy-subrc ne 0.
    gv_lag            = gc_x.
    gt_errorlog-vbeln = gt_vbrk-vbeln.
    gt_errorlog-kunnr = gt_vbrk-kunnr.
    gt_errorlog-msgds = 'No message maintained for Billing.'.
    append gt_errorlog.
  else.
    perform entry_neu_email(zyrlb_invoice3)
            tables it_otf using w_nast.
    if it_otf[] is initial.
      gv_lag            = gc_x.
      gt_errorlog-vbeln = gt_vbrk-vbeln.
      gt_errorlog-kunnr = gt_vbrk-kunnr.
      gt_errorlog-msgds = 'Smartform not called for Billing.'.
      append gt_errorlog.
    else.
      call function 'CONVERT_OTF'
        exporting
          format                = 'PDF'
        importing
          bin_filesize          = pdf_size
          bin_file              = pdf_xstring
        tables
          otf                   = it_otf
          lines                 = i_pdf
        exceptions
          err_max_linewidth     = 1
          err_format            = 2
          err_conv_not_possible = 3
          err_bad_otf           = 4
          others                = 5.
      if sy-subrc <> 0.
        gv_lag            = gc_x.
        gt_errorlog-vbeln = gt_vbrk-vbeln.
        gt_errorlog-kunnr = gt_vbrk-kunnr.
        gt_errorlog-msgds = 'Convert OTF data to PDF failed for Billing.'.
        append gt_errorlog.
      else.
        it_pdf-vbeln = gt_vbrk-vbeln.
        it_pdf-pdf_xstring  = pdf_xstring.
        append it_pdf.
      endif.
    endif.
  endif.

endform.                    " CONVERT_PDF
*&---------------------------------------------------------------------*
*&      Form  SEND_EMAIL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form send_email using p_cust .
  data: t_subrc like sy-subrc.

perform send_mail_via_bcs using  p_cust changing t_subrc.

if t_subrc = 0.
    loop at it_pdf.
*     Update email status
      update zsdbill  set zemta = gc_x
                           aenam   = sy-uname
                           laeda   = sy-datum
                           aezet   = sy-uzeit
                     where vbeln   = it_pdf-vbeln.
      if sy-subrc <> 0.
        rollback work.
      else.
*    Update gt_vbrk email status
        gt_vbrk-zemta = gc_x.
        modify  gt_vbrk transporting zemta where vbeln = it_pdf-vbeln   .
      endif.
    endloop.
  else.
    loop at it_pdf.
      gt_errorlog-vbeln = it_pdf-vbeln.
      gt_errorlog-kunnr = it_kunnr-kunnr.
      gt_errorlog-msgds = 'Email has not been sent to Customer.'.
      append gt_errorlog.
    endloop.
  endif.
endform.                    " SEND_EMAIL
*&---------------------------------------------------------------------*
*&      Form  SEND_MAIL_VIA_BCS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_T_SUBRC  text
*----------------------------------------------------------------------*
form send_mail_via_bcs using  p_cust  changing p_subrc.
  data  send_request       type ref to cl_bcs.
  data  text               type bcsy_text.
  data  subject            type sood-objdes.
  data  subject_title      type sood-objdes.
  data  pdf_content        type solix_tab.
  data  lp_pdf_size        type so_obj_len.
  data  document           type ref to cl_document_bcs.
  data  lo_sender         type ref to if_sender_bcs.
  data  lv_send           type ad_smtpadr value 'cms-fax@vtech.com '.
  data  mailto             type adr6-smtp_addr.
  data  recipient          type ref to if_recipient_bcs.
  data  bcs_exception      type ref to cx_bcs.
  data  sent_to_all        type os_boolean.

p_subrc = 4.
  if p_cust = 'X'.
    subject_title = 'Billing documents FROM VTECH' .
  else.
    concatenate 'Billing document ' gt_vbrk-vbeln 'FROM VTECH' into subject_title separated by space.
  endif.
  append 'Attention: Account Department' to text.
  "append initial line to text.
  append '  Enclosed invoices for your attention and information.' to text.
  "append initial line to text.
  append '  Should there be any problem with regard to invoices, please feel free to contact us immediately and will reponse to your request promptly.' to text.
  append initial line to text.
  append 'Thanks and regards' to text.
  append 'CMS/LOGISTIC' to text.

try.
*   ---------- create persistent send request ----------------------
      send_request = cl_bcs=>create_persistent( ).

*   ---------- add document ----------------------------------------
*   get PDF xstring and convert it to BCS format

document = cl_document_bcs=>create_document(
            i_type    = 'RAW'
            i_text    = text
            i_subject = subject_title ).

loop at it_pdf.

concatenate 'Billing document' it_pdf-vbeln into subject separated by space.

lp_pdf_size = xstrlen( it_pdf-pdf_xstring ).

pdf_content = cl_document_bcs=>xstring_to_solix(
            ip_xstring = it_pdf-pdf_xstring ).

document->add_attachment(
              i_attachment_type = 'PDF'
              i_attachment_subject = subject
              i_att_content_hex = pdf_content ).

endloop.
*   add document to send request
      send_request->set_document( document ).
*   add seng (e-mail address)
*-Sender(v_address = You can have static EMAIL Address for  sender or different EMAIL addresses )
      "gv_send = 'cms-fax@vtech.com '.
      lo_sender = cl_cam_address_bcs=>create_internet_address( lv_send ).
      "Set sender
      send_request->set_sender( lo_sender ).
*   ---------- add recipient (e-mail address) ----------------------
      if v_mail[] is not initial.
        loop at v_mail.
          i_reclist-receiver = v_mail-low.
          translate i_reclist-receiver to upper case.
          collect i_reclist.
          clear:i_reclist.
        endloop.
      endif.
      loop at i_reclist.
        mailto = i_reclist-receiver.
        recipient = cl_cam_address_bcs=>create_internet_address(
            i_address_string = mailto ).
*       ADD recipient TO send REQUEST
        send_request->add_recipient( i_recipient = recipient ).
      endloop.

*   ---------- send document ---------------------------------------
      sent_to_all = send_request->send(
          i_with_error_screen = 'X' ).

if sent_to_all = 'X'.
        p_subrc = 0.
      endif.

*   ---------- explicit 'commit work' is mandatory! ----------------
      commit work.

* ------------------------------------------------------------------
* *            exception handling
* ------------------------------------------------------------------
* * replace this very rudimentary exception handling
* * with your own one !!!
* ------------------------------------------------------------------
    catch cx_bcs into bcs_exception.
      if p_email = 'X'.
        loop at it_pdf.
          gt_errorlog-vbeln = it_pdf-vbeln.
          gt_errorlog-kunnr = it_kunnr-kunnr.
          gt_errorlog-msgds = bcs_exception->error_type.
          append gt_errorlog.
        endloop.
      else.
        message s000(oo) with bcs_exception->error_type.
      endif.
      exit.
  endtry.
endform.                    " SEND_MAIL_VIA_BCS
*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form build_fieldcat .
  define alv_field.
    clear:wa_hier.
    wa_hier-fieldname = &1.
    wa_hier-seltext_m = &2.
    wa_hier-seltext_l = &3.
    wa_hier-outputlen = &4.
    wa_hier-just = &5.
    wa_hier-ref_tabname = &6 .
    wa_hier-ref_fieldname = &7 .
    append wa_hier to fc_hier.
  end-of-definition.

refresh fc_hier.
  alv_field  'VBELN' '' '' '15' ''  'VBRK'  'VBELN' .
  alv_field  'FKART' '' '' '10'  '' 'VBRK'  'FKART' .
  alv_field  'VKORG' '' '' '6'  '' 'VBRK'   'VKORG' .
  alv_field  'FKDAT' '' '' '10'  '' 'VBRK'   'FKDAT' .
  alv_field  'KUNNR' '' '' '10'  ''  'KNA1' 'KUNNR' .
  alv_field  'ZEMTA' '' 'Mail Mark' ''  '' '' ''   .
  alv_field  'MESSAGE' '' 'Message' '50'  '' '' ''   .
endform.                    " BUILD_FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  PRINT_OUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form print_out .
  sort gt_vbrk by fkdat kunnr vbeln.
  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      i_callback_program      = sy-repid
      it_fieldcat             = fc_hier[]
      i_save                  = 'A'
      i_callback_user_command = 'ALV_USER_COMMAND'
    tables
      t_outtab                = gt_vbrk[]
    exceptions
      program_error           = 1
      others                  = 2.
  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.                    " PRINT_OUT
*&---------------------------------------------------------------------*
*&      Form  ALV_USER_COMMAND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->R_UCOMM      text
*      -->RS_SELFIELD  text
*----------------------------------------------------------------------*
form alv_user_command using r_ucomm like sy-ucomm
                  rs_selfield type slis_selfield. "响应ALV点击自定义按钮之后的事件
  data:lv_matnr like mara-matnr.
  case r_ucomm.
    when '&IC1'.
      if rs_selfield-fieldname ='VBELN' and rs_selfield-value <>''.
        set parameter id :'VF' field rs_selfield-value .
        call transaction 'VF03'  and skip first screen .
      endif.
  endcase.
endform.                    "alv_user_command
*&---------------------------------------------------------------------*
*&      Form  CHECK_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form check_data .
  data:lv_tabix like sy-tabix.
  data: begin of lt_adr occurs 0,
        smtp_addr type adr6-smtp_addr,
      end of lt_adr.
  data: w_nast like nast.
  data: it_otf like table of itcoo with header line.

data: control_parameters type ssfctrlop,
        output_options     type ssfcompop.

loop at gt_vbrk.
    lv_tabix = sy-tabix.

"Email Address.
    if gt_vbrk-kunnr is not initial.
      read table it_kna1 with key kunnr =  gt_vbrk-kunnr binary search.
      if sy-subrc = 0.
        select smtp_addr into  table lt_adr
          from adr6
          where persnumber = space
            and addrnumber = it_kna1-adrnr.
        if sy-subrc ne 0.
          concatenate gt_vbrk-message 'No maintenance the Billing Customer-mail address, the message is not sent.'
              into gt_vbrk-message separated by space.
        endif.
      else.
        concatenate gt_vbrk-message 'No maintenance the Billing Customer-mail address, the message is not sent.'
            into gt_vbrk-message separated by space.
      endif.
    else.
      concatenate gt_vbrk-message 'Billing Customer, the message was not sent.'
            into gt_vbrk-message separated by space.
    endif.

"Convent PDF
    select single * from nast into w_nast
      where objky = gt_vbrk-vbeln
        and kappl = 'V3'
        and kschl = p_kschl
        and spras = sy-langu.
    if sy-subrc ne 0.
      concatenate gt_vbrk-message 'No message maintained for Billing.'
          into gt_vbrk-message separated by space.
*    else.
*      perform entry_neu_email(zyrlb_invoice3)
*              tables it_otf using w_nast.
*      if it_otf[] is initial.
*        concatenate gt_vbrk-message 'Smartform not called for Billing.'
*            into gt_vbrk-message separated by space.
*      else.
*        call function 'CONVERT_OTF'
*          exporting
*            format                = 'PDF'
*          importing
*            bin_filesize          = pdf_size
*            bin_file              = pdf_xstring
*          tables
*            otf                   = it_otf
*            lines                 = i_pdf
*          exceptions
*            err_max_linewidth     = 1
*            err_format            = 2
*            err_conv_not_possible = 3
*            err_bad_otf           = 4
*            others                = 5.
*        if sy-subrc <> 0.
*          concatenate gt_vbrk-message 'Convert OTF data to PDF failed for Billing.'
*              into gt_vbrk-message separated by space.
*        endif.
*      endif.
    endif.

modify gt_vbrk index lv_tabix  transporting message.
  endloop.
endform.                    " CHECK_DATA

*----------------------------------------------------------------------*
*      Print of a invoice by SAPscript SMART FORMS               *
*----------------------------------------------------------------------*

REPORT zyrlb_invoice3.

* declaration of data
INCLUDE ZRLB_INVOICE_DATA_DECLARE.
*INCLUDE rlb_invoice_data_declare.
* definition of forms
INCLUDE ZRLB_INVOICE_FORM01.
*INCLUDE rlb_invoice_form01.
INCLUDE ZRLB_PRINT_FORMS.
*INCLUDE rlb_print_forms.

*---------------------------------------------------------------------*
*       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: IT_ERRORLIST          type TSFERROR.

* 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.

IF cf_retcode = 0.
    PERFORM check_repeat.
    IF ls_composer_param-tdcopies EQ 0.
      nast_anzal = 1.
    ELSE.
      nast_anzal = ls_composer_param-tdcopies.
    ENDIF.
    ls_composer_param-tdcopies = 1.
* BEGIN: Country specific extension for Hungary
  DATA: lv_ccnum TYPE IDHUCCNUM.

* If a valid entry exists for the form in customizing view
* IDHUBILLINGOUT then the localized output shall be used.
  SELECT SINGLE ccnum INTO lv_ccnum FROM IDHUBILLINGOUT WHERE
    kschl = nast-kschl.

IF sy-subrc EQ 0.
    IF lv_ccnum IS INITIAL.
      lv_ccnum = 1.
    ENDIF.
  ELSE.
    CLEAR lv_ccnum.
  ENDIF.
* END: Country specific extension for Hungary
    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.
* BEGIN: Country specific extension for Hungary
    IF lv_ccnum IS NOT INITIAL.
      IF nast-repid IS INITIAL.
        nast-repid = 1.
      ELSE.
        nast-repid = nast-repid + 1.
      ENDIF.
      nast-pfld1 = lv_ccnum.
    ENDIF.
* END: Country specific extension for Hungary
* 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
           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.
        CALL FUNCTION 'SSF_READ_ERRORS'
            IMPORTING
                 ERRORTAB = IT_ERRORLIST.

ENDIF.
    ENDDO.
* 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.

* get SmartForm protocoll and store it in the NAST protocoll
* PERFORM ADD_SMFRM_PROT.

ENDFORM.                    "PROCESSING
*&---------------------------------------------------------------------*
*&      Form  entry_neu_email
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_OTF      text
*      -->P_NAST     text
*----------------------------------------------------------------------*
form entry_neu_email tables p_otf structure itcoo
                     using p_nast structure nast.

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: 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: cf_retcode type sy-subrc.
  data: l_op type ssfcompop.
  data: l_c type ssfctrlop.
  data: l_user_settings.

* SmartForm from customizing table TNAPR
  lf_formname = 'ZWLB_INVOICE3'.
  nast = p_nast.
* 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.
* 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.
      cf_retcode = sy-subrc.
    endif.
  endif.

if cf_retcode = 0.
    l_c-no_dialog = 'X'.
    l_c-getotf = 'X'.
    if l_c-device is initial .
      l_c-device = 'PRINTER'.
    endif.
*
    l_op-tdnoprev = 'X'.
    if l_op-tddest is initial.
      l_op-tddest = 'LP01'.
    endif.
    l_op-tdimmed = 'X'.
    l_op-tdnewid = 'X'.  "new request

call function lf_fm_name
      exporting
        control_parameters = l_c
        output_options     = l_op
        user_settings      = l_user_settings
        is_bil_invoice     = ls_bil_invoice
        is_nast            = nast
        is_repeat          = repeat
      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.
    else.
      p_otf[] = ls_job_info-otfdata[].
    endif.
  endif.
  clear:nast.
endform.                    "entry_neu_email

SAP: ZSD012 Send Billing Document to Customer with PDF Attachment相关推荐

  1. SAP S/4HANA里如何创建Customer主数据以及执行后续处理

    来自Jerry的同事Zhang Sean. 1, Launch tcode: BP and select the Organization 2, Maintain the information fo ...

  2. SAP MRKO Error - For document type RE, an entry is required in field Reference -

    SAP MRKO Error - For document type RE, an entry is required in field Reference - MRKO,对于供应商100364,执行 ...

  3. SAP Link Between Material Document and Accounting Document Table

    SAP Link Between Material Document and Accounting Document Table 如下物料凭证号4904373511/2018,对应的财务凭证号3200 ...

  4. SAP云平台的Document Service

    SAP云平台以微服务的方式提供了Document的CRUD(增删改查)操作.该微服务基于标准的CMIS协议(Content Management Interoperability Service). ...

  5. SAP UI5 加载本地并不存在的 PDF 文件的错误处理

    这个 _onLoadListener 函数什么时候注册的呢? iframe 完成加载之后,就触发这个 load 事件注册的处理函数: PDFViewer.prototype.onAfterRender ...

  6. PDF to Word Document Converter for Mac(PDF文档转换器)

    如何将pdf文件转换为word.PPT.html.jpg等文件?PDF to Word&Document Converter是一个PDF文档转换器,可让您快速将PDF文档转换为其他格式的文件. ...

  7. 使用SAP CRM中间件从ERP下载Customer的错误消息:Customer classification does not exist

    After I create a customer in ERP via tcode VD01 and try to download it into CRM via request download ...

  8. SAP Analytics Cloud和Cloud for Customer之间的Single Sign on配置

    详细方法在这篇SAP博客上能够找到: https://blogs.sap.com/2019/01/17/setup-sso-between-cloud-for-customer-and-sap-ana ...

  9. SAP STO With Billing流程与配置

    STO With Billing 跨公司库存转储(公司间交易)流程如下: 注意:PO类型根据实际需要来,比如我配置的PO类型是ZNB1. 业务模拟 配置没问题,那么完成业务后,相关信息大致如下: 1. ...

最新文章

  1. Kaggle经典数据分析项目:泰坦尼克号生存预测!
  2. 【c语言】统计字符次数
  3. [USACO]奶牛博览会(DP)
  4. python编程15讲答案,[Python编程:从入门到实践] 第十五章:生成数据 习题答案
  5. LINQ技术、EF技术都出来蛮久了,软件开发者、软件公司是否还有必要有自己的代码生成器?...
  6. 皮一皮:这是直男的鬼才逻辑?
  7. 用于J2EE开发的Cloud IDE
  8. 深入理解CRITICAL_SECTION
  9. c++代码健壮性_复活Navex-使用图查询进行代码分析(上)
  10. Java中Math3 各种随机数生成器的使用(Random Generator)
  11. 重新认识fprint,sprint
  12. sap固定资产号码范围_SAP--FI详细解---固定资产
  13. 计算机学院毕业语录,毕业的句子简单到哭的_毕业语录
  14. PDF Expert教程|七个提高效率的小技巧
  15. 树莓派 小型服务器的搭建
  16. 谷歌浏览器翻译插件的安装与使用
  17. Zabbix设置主动模式与被动模式
  18. SpringBoot 之 Web开发
  19. C语言超出类型数值范围的表示方法
  20. word启动时出现运行时错误 未注册类弹窗问题

热门文章

  1. 「工具」三分钟了解一款在线流程绘制工具:Whimsical
  2. DHCP 中继Snooping解释以及配置
  3. 自媒体一般都是去哪里搬运
  4. 程序员的节日1024
  5. Linux Zram的概念
  6. WARNING: channel “pecl.php.net“ has updated its protocols
  7. 控制iphone音乐播放器的相关函数
  8. MT4电脑版如何下载
  9. 将数据字典转为Excel表格
  10. ionic升华过程8-cordova插件+mui小案例