SAP:ZMMR033 Send eMail by Excel

44 篇文章 0 订阅
*&---------------------------------------------------------------------*
*& Progarm      :  ZMMR033                        Author : Jimmy Wong
*& Created      :  28 Mar 2013                    App    : MM
*& Title        :  Send eMail by PO Excel
*& Description  :  Send eMail by PO Excel
*&---------------------------------------------------------------------*
*&  Version       Author      Date        description
*&                Jimmy       28 Mar 2013 the first version
*&  the last update time  2013.03.28 16:00
*&---------------------------------------------------------------------*

report  zmmr033 no standard page heading.

tables: zpoemail.
data: it_zpoemail like table of zpoemail with header line.
data: xls_content type solix_tab,
      xls_size    type so_obj_len.
constants: gc_crlf type value cl_bcs_convert=>gc_crlf. " ┐ New Line In XLS
data: begin of i_reclist occurs 0,
      receiver like somlreci1-receiver  ,
      end of i_reclist .
data: lv_subrc like sy-subrc.

type-pools: slis.
data: fc_hier type slis_t_fieldcat_alv,
      wa_hier type slis_fieldcat_alv .
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-do_sum &6.
  wa_hier-ref_tabname &7 .
  wa_hier-ref_fieldname &8 .
  append wa_hier to fc_hier.
end-of-definition.


selection-screen begin of block with frame title text-001.
select-options : s_aedat for zpoemail-aedat  default sy-datum.
select-options : s_ebeln for zpoemail-ebeln.
selection-screen end of block 1.
selection-screen begin of block b3 with frame title text-002  .
parameters: p_alv radiobutton group r2 default 'X'.
parameters p_err as checkbox .
parameters p_succ as checkbox.
selection-screen skip 1.
parameters: p_email radiobutton group r2.
selection-screen end of block b3.

*&---------------------------------------------------------------------*
* start of process
*&---------------------------------------------------------------------*
start-of-selection.
  if s_aedat[] is initial.
    s_aedat-sign 'I'.
    s_aedat-option 'EQ'.
    s_aedat-low sy-datum.
    append s_aedat.
  endif.
  perform get_data.
  if p_alv 'X'.
    if p_err is initial.
      delete it_zpoemail where isfail 'X'.
    endif.
    if p_succ is initial.
      delete it_zpoemail where zemta 'X'.
    endif.
    if it_zpoemail[] is initial.
      message i003(zmm).
      exit.
    endif.
    perform display_data.
  endif.
  if p_email 'X'.
    delete it_zpoemail where isfail 'X' or zemta 'X'.
    if it_zpoemail[] is initial.
      message i003(zmm).
      exit.
    endif.
    perform get_email changing lv_subrc.
    if lv_subrc ne 0.
      write: 'eMail address No Maintain.'.
      exit.
    endif.
    perform foramt_xls.
    if  xls_content is  initial.
      write: 'eMail Transfer format error.'.
      exit.
    endif.
    perform send_mail_via_bcs changing  lv_subrc.
    if lv_subrc eq 0.
      loop at it_zpoemail.
        update zpoemail set zemta 'X'
                       where ebeln   it_zpoemail-ebeln
                        and aedat it_zpoemail-aedat
                        and aezet it_zpoemail-aezet.
      endloop.
      write:'Send eMail success.'.
    else.
      write:'Send eMail Failing.'.
    endif.
  endif.

*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_data .
  select into table it_zpoemail
    from zpoemail
    where ebeln in s_ebeln
      and aedat in s_aedat.
endform.                    " GET_DATA
*&---------------------------------------------------------------------*
*&      Form  DISPALY_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form display_data .
  refresh: fc_hier.

  alv_field  'EBELN' '' '' '16' 'L' '' 'EKKO' 'EBELN'.
  alv_field  'AEDAT' 'Create date' '' '12' 'L' '' '' ''.
  alv_field  'AEZET' 'Create time' '' '10' 'L' '' '' ''.
  alv_field  'AENAM' 'User Name' '' '12' 'L' '' '' ''.
  alv_field  'ZEMTA' 'Send PO List Status' '' '18' 'L' '' '' ''.
  alv_field  'ISFAIL' 'Send Single PO Status' '' '18' 'L' '' '' ''.
  alv_field  'MSGDS' '' 'Send Single PO Message' '50' 'L' '' '' ''.


  perform display_alv tables  fc_hier it_zpoemail using 'A'.

endform.                    " DISPALY_DATA
*&---------------------------------------------------------------------*
*&      Form  display_alv
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->FC_HIER    text
*      -->ITAB       text
*      -->P_SAVE     text
*----------------------------------------------------------------------*
form display_alv tables fc_hier itab
                  using p_save .
  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      it_fieldcat        fc_hier[]
      i_save             p_save
      i_callback_program sy-repid
    tables
      t_outtab           itab[]
    exceptions
      program_error      1
      others             2.
endform.                    "display_alv
*&---------------------------------------------------------------------*
*&      Form  get_email
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_SUBRC    text
*----------------------------------------------------------------------*
form get_email changing p_subrc .
  select  distinct zemal as  receiver
    into table i_reclist
    from zbuyer
    where zrgco 'G2'.
  p_subrc sy-subrc.
endform.                    " GET_EMAIL

*&---------------------------------------------------------------------*
*&      Form  FORAMT_XLS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      Format XLS file as attachment
*----------------------------------------------------------------------*
form foramt_xls    .
  data : begin of lt_xls occurs 0,
         ebeln type ekpo-ebeln, " PO#
         ebelp type ekpo-ebelp, " Line#
         matnr type ekpo-matnr, "Vtech Part No.
         mfrpn type mara-mfrpn, "Vendor Ordering No.
         mfrnr type ekpo-mfrnr, " Manufacturer
         kdmat type knmt-kdmat, "Customer P/N
         menge type ekpo-menge, "Order Qty.
         netpr type ekpo-netpr, "Unit Price
         waers type ekko-waers, "Currency
         eindt type eket-eindt, "Delivery Date(DD/MM/YYYY)
         aedat type ekko-aedat, "PO Creation Date
         netwr type ekpo-netwr,
         price(20),             "Unit Price
         qty(20),               "Order Qty.
         dldat(10),             "Delivery Date(DD/MM/YYYY)
         crdat(10),             "PO Creation Date(DD/MM/YYYY)
         meins type ekpo-meins,
         loekz type ekpo-loekz,
         peinh type ekpo-peinh,
         kunnr type lfa1-kunnr,
         amount(20),             "Unit Price,
         end of lt_xls.

  data : ls_xls like lt_xls,
         lv_len type i,
         lv_lines type i,
         lv_index type i,
         lv_total like ekpo-netwr,
         lv_amont like ekpo-netwr,
         lv_price like ekpo-netpr,
         lv_total_c(20) .

  data : begin of it_text occurs 0,
         text(255),
         end of it_text.
* get data
  select  *
    from ekko as join ekpo as on a~ebeln b~ebeln
    join eket as on c~ebeln b~ebeln and c~ebelp b~ebelp
    left join mara as on d~matnr b~matnr
    left join lfa1 as on a~lifnr e~lifnr
    into corresponding fields of table lt_xls
    for all entries in it_zpoemail
    where a~ebeln it_zpoemail-ebeln.

* TXT Header
  concatenate  'Vtech Part No.' ',' "GC_TAB
               'PO#' ',' "GC_TAB
               'Line#' ',' "GC_TAB
               'Vendor Ordering No.' ',' "GC_TAB
               'Manufacturer' ',' "GC_TAB
               'Customer P/N' ',' "GC_TAB
               'Order Qty.' ',' "GC_TAB
               'Unit Price' ',' "GC_TAB
               'Amount' ',' "GC_TAB
               'Currency' ',' "GC_TAB
               'Delivery Date(DD/MM/YYYY)' ',' "GC_TAB
               'PO Creation Date(DD/MM/YYYY)' "CL_ABAP_CHAR_UTILITIES=>NEWLINE "GC_CRLF "
               into it_text-text.
  append it_text.
  clear  it_text.

  describe table lt_xls lines lv_lines.
  clear lv_index.
  sort lt_xls by  ebeln ebelp.
* XLS Lines
  loop at lt_xls into ls_xls.
    lv_index lv_index + 1.
    perform convert_price using ls_xls-netpr ls_xls-peinh ls_xls-waers changing ls_xls-price.
    if ls_xls-loekz <> 'L'.
      move ls_xls-menge to ls_xls-qty .
      condense ls_xls-qty.
    else.
      ls_xls-qty '0'.
    endif.
    lv_price ls_xls-price .
    lv_amont ls_xls-menge * lv_price.
    ls_xls-amount lv_amont.
    condense ls_xls-amount.
    lv_total lv_total +  lv_amont .

*    WRITE LS_XLS-AEDAT TO LS_XLS-CRDAT DD/MM/YYYY.
    perform convert_date_format using ls_xls-eindt changing ls_xls-dldat.
    perform convert_date_format using ls_xls-aedat changing ls_xls-crdat.

    if ls_xls-kunnr is not initial."Vendor also is customer
      select single kdmat into ls_xls-kdmat from knmt where matnr ls_xls-matnr and kunnr ls_xls-kunnr.
    endif.

    perform convert_matnr_foramt changing ls_xls-matnr.
    concatenate
                  ls_xls-matnr ',' "GC_TAB
                  ls_xls-ebeln ',' "GC_TAB
                  ls_xls-ebelp ',' "GC_TAB
                  ls_xls-mfrpn ',' "GC_TAB
                  ls_xls-mfrnr ',' "GC_TAB
                  ls_xls-kdmat ',' "GC_TAB
                  ls_xls-qty   ',' "GC_TAB
                  ls_xls-price ',' "GC_TAB
                  ls_xls-amount ','
                  ls_xls-waers ',' "GC_TAB
                  ls_xls-dldat ',' "GC_TAB
                  ls_xls-crdat "CL_ABAP_CHAR_UTILITIES=>NEWLINE "GC_CRLF "
                  into it_text-text.

    append it_text.
    clear it_text.
    at end of ebeln.
      lv_total_c lv_total  .
      condense lv_total_c .
      concatenate
                    ''   ',' "GC_TAB
                    '' ',' "GC_TAB
                    '' ',' "GC_TAB
                    '' ',' "GC_TAB
                    '' ',' "GC_TAB
                    '' ',' "GC_TAB
                    ''  ',' "GC_TAB
                    'Total' ',' "GC_TAB
                    lv_total_c ','
                    '' ',' "GC_TAB
                    ''  ',' "GC_TAB
                    ''  "CL_ABAP_CHAR_UTILITIES=>NEWLINE "GC_CRLF "
                    into it_text-text.
      append it_text.
      clear: it_text,lv_total,lv_total_c.
    endat.
  endloop.

  it_text-text gc_crlf.
  append it_text.
  clear it_text.

  call function 'SCMS_TEXT_TO_BINARY'
    exporting
      mimetype      'TXT'
    importing
      output_length lv_len
    tables
      text_tab      it_text
      binary_tab    xls_content
    exceptions
      failed        1
      others        2.
  if sy-subrc <> 0.
  endif.
  xls_size lv_len 1."Remove the last blank line
endform.                    " FORAMT_XLS
*&---------------------------------------------------------------------*
*&      Form  CONVERT_DATE_FORMAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form convert_date_format  using    pi_date type sy-datum
                          changing pe_date.
  check pi_date is not initial.
  concatenate pi_date+6(2pi_date+4(2pi_date+0(4into pe_date separated by '/'.

endform.                    " CONVERT_DATE_FORMAT
*&---------------------------------------------------------------------*
*&      Form  CONVERT_MATNR_FORAMT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_LS_XLS_MATNR  text
*----------------------------------------------------------------------*
form convert_matnr_foramt  changing p_matnr.
  call function 'CONVERSION_EXIT_MATN1_OUTPUT'
    exporting
      input  p_matnr
    importing
      output p_matnr.

endform.                    " CONVERT_MATNR_FORAMT
*&---------------------------------------------------------------------*
*&      Form  CONVERT_PRICE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form convert_price  using    pi_netpr
                             pi_peinh
                             pi_waers
                    changing pe_price.
  data : lv_amount type bapicurr-bapicurr,
         lv_price(20).

  if pi_waers <> 'HKD' or pi_waers <> 'USD' or
     pi_waers <> 'CNY' or pi_waers <> 'EUR'.
    call function 'BAPI_CURRENCY_CONV_TO_EXTERNAL'
      exporting
        currency        pi_waers
        amount_internal pi_netpr
      importing
        amount_external lv_amount.
  else.
    lv_amount =  pi_netpr.
  endif.

  if pi_peinh <> 0.
    lv_amount lv_amount / pi_peinh.
  endif.

*  WRITE LV_AMOUNT TO LV_PRICE.
  move lv_amount to lv_price.
  condense lv_price.

  pe_price lv_price.
  condense pe_price.
endform.                    " CONVERT_PRICE

*&---------------------------------------------------------------------*
*&      Form  SEND_MAIL_VIA_BCS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_SUBRC    text
*----------------------------------------------------------------------*
form send_mail_via_bcs   changing p_subrc.
* BCS data
  data  send_request       type ref to cl_bcs.
  data  text               type bcsy_text.
  data  document           type ref to cl_document_bcs.
  data  recipient          type ref to if_recipient_bcs.
  data  bcs_exception      type ref to cx_bcs.
  data  sent_to_all        type os_boolean.
  data  mailto             type adr6-smtp_addr.
  data  subject            type sood-objdes.
  data  subject_title      type sood-objdes.
  data  lv_send         type ad_smtpadr value 'cms-fax@vtech.com '.
  data  lo_sender         type ref to if_sender_bcs.

  p_subrc 4.

  subject_title 'Purchase order approve list'.
  subject 'Purchase order approve list'.
*  concatenate 'Purchase order'  ''  into subject separated by space.

  append 'Dear Sirs/Madams' to text.
  append initial line to text.
  append '  Please find the Purchase order waiting approve summary list for today in attachment.' to text.
  append initial line to text.
  append 'Thanks & regards' to text.
* end
  try.
*   ---------- create persistent send request ----------------------
      send_request cl_bcs=>create_persistent).

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

      if xls_content is not initial.
        document->add_attachment(
                   i_attachment_type 'CSV'
                   i_attachment_subject subject
                   i_attachment_size    xls_size
                   i_att_content_hex xls_content ).
      endif.

*   add document to send request
      send_request->set_documentdocument ).
*   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_addresslv_send ).
      "Set sender
      send_request->set_senderlo_sender ).
*   ---------- add recipient (e-mail address) ----------------------
      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_recipienti_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.
      write:/ bcs_exception->error_type.
      exit.
  endtry.
endform.                    " SEND_MAIL_VIA_BCS
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值