*&---------------------------------------------------------------------*
*& Report ZPP_SEND_EMAIL
*&变更配方自动发送EMAIL
*&---------------------------------------------------------------------*
*&2013.6.28
*&
*&---------------------------------------------------------------------*
REPORT ZPP_SEND_EMAIL.
data: begin of tab_g_mail occurs 0.
include structure cdpos.
data: matnr like mara-matnr.
data: IDNRK like stpo-IDNRK.
DATA: WERKS LIKE MAST-WERKS.
DATA: STLAL LIKE MAST-STLAL.
data: DDTEXT like dd04t-DDTEXT.
*DATA:
data:end of tab_g_mail.
DATA: LT_CDHDR LIKE TABLE OF CDHDR WITH HEADER LINE.
DATA: LT_CDHDR_TEMP LIKE TABLE OF CDHDR WITH HEADER LINE.
DATA: LT_CDPOS LIKE TABLE OF TAB_G_MAIL WITH HEADER LINE.
DATA: BEGIN OF tab_werks OCCURS 0.
DATA: TABNAME LIKE CDPOS-TABNAME.
DATA: TABKEY LIKE CDPOS-TABKEY.
DATA: WERKS LIKE MAST-WERKS.
DATA: STLBE LIKE STZU-STLBE.
data: IDNRK like stpo-IDNRK.
data: END OF tab_werks.
data: lt_werks like TABLE OF tab_werks WITH HEADER LINE.
data: ld_start like CDHDR-UDATE,ld_end like CDHDR-UDATE.
DATA: li_count type i.
data: ldt_now type cdhdr-UDATE,ldt_time type cdhdr-UTIME,ldt_starttime type cdhdr-UTIME.
DATA: LS_TIME(14) TYPE C.
ldt_now = SY-DATUM.
ldt_time = SY-UZEIT.
*LDT_NOW = '20130628'.
*ldt_time = '160000'.
*LS_TIME = LDT_NOW + LDT_TIME.
IF ldt_time < '040000'. "四小时
ld_start = ldt_now - 1.
else.
ld_start = ldt_now.
endif.
ldt_starttime = ldt_time - 14400. "四小时
PERFORM get_data.
perform send_mail tables tab_g_mail.
*&---------------------------------------------------------------------*
*& Form SEND_MAIL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form send_mail tables tab_l_mail like tab_g_mail[].
DATA: i_objpack LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
i_objtxt LIKE solisti1 OCCURS 0 WITH HEADER LINE,
i_objbin LIKE solisti1 OCCURS 0 WITH HEADER LINE,
i_reclist LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
i_record LIKE solisti1 OCCURS 0 WITH HEADER LINE,
rec_l_contents type solisti1,
v_objhead TYPE soli_tab ,
v_lines_txt TYPE i,
v_lines_bin TYPE i,
v_docchgi TYPE sodocchgi1,
v_lines_bin_all TYPE i ,
filelen TYPE i.
data: begin of tab_l_mailadd occurs 0,
pmail(30) type c,
end of tab_l_mailadd.
data: begin of tab_mail occurs 0,
pmail(30) type c,
end of tab_mail.
DATA: BEGIN OF itab_text OCCURS 0 , "内表作为邮件附件
MATNR LIKE MAST-MATNR,
STLAL LIKE MAST-STLAL,
tabname like cdpos-tabname,
tabkey like cdpos-tabkey,
FNAME like cdpos-FNAME,
DDTEXT like dd04t-DDTEXT,
IDNRK LIKE stpo-IDNRK,
CHNGIND like cdpos-FNAME,
VALUE_OLD like cdpos-VALUE_OLD,
VALUE_NEW LIKE CDPOS-VALUE_OLD,
END OF itab_text.
DATA:ls_name(100) type c.
DATA: DDTEXT LIKE DD03T-DDTEXT,EDDTEXT LIKE DD04T-DDTEXT,DDDTEXT LIKE DD01T-DDTEXT.
LOOP AT TAB_WERKS.
***附近内容
itab_text-matnr = '物料代码'.
itab_text-stlal = '可选BOM'.
itab_text-tabname = '表名'.
itab_text-tabkey = '表关键字'.
itab_text-FNAME = '字段名'.
itab_text-DDTEXT = '字段描述'.
itab_text-idnrk = '组件'.
itab_text-CHNGIND = '操作'.
ITAB_TEXT-VALUE_OLD = '旧值'.
ITAB_TEXT-VALUE_NEW = '新值'.
APPEND itab_text.
v_docchgi-obj_name = 'SAPSEND'.
v_docchgi-expiry_dat = sy-datum + 2 . "过期日期
v_docchgi-sensitivty = 'F'. "Functional object
v_docchgi-doc_size = v_lines_txt * 255.
v_docchgi-obj_descr = '研究院配方变更'.
*邮件内容
concatenate '以下是变更信息:' rec_l_contents into rec_l_contents separated by space.
append rec_l_contents to i_objtxt.
loop at tab_g_mail where tabname = tab_werks-tabname and TABKEY = tab_werks-TABKEY.
*** 取字段名称
SELECT SINGLE dd04t~DDTEXT dd03t~ddtext dd01t~ddtext
into (ddtext,eddtext,dddtext)
FROM DD03L LEFT JOIN DD03T
ON DD03L~TABNAME = DD03T~TABNAME
AND DD03L~FIELDNAME = DD03T~FIELDNAME
AND DD03T~DDLANGUAGE = SY-LANGU
LEFT JOIN DD04T
ON DD03L~ROLLNAME = DD04T~ROLLNAME
AND DD04T~DDLANGUAGE = SY-LANGU
LEFT JOIN DD01T
ON DD03L~DOMNAME = DD01T~DOMNAME
AND DD01T~DDLANGUAGE = SY-LANGU
WHERE DD03L~TABNAME = tab_werks-tabname and
DD03L~FIELDNAME = tab_g_mail-FNAME.
IF DDTEXT = SPACE.
IF EDDTEXT = SPACE.
tab_g_mail-ddtext = DDDTEXT.
ELSE.
tab_g_mail-ddtext = EDDTEXT.
ENDIF.
else.
tab_g_mail-ddtext = ddtext.
ENDIF.
clear rec_l_contents.
concatenate '物料代码' tab_g_mail-matnr
'可选BOM' tab_g_mail-stlal "'编号:' tab_werks-TABKEY
'组件:' tab_werks-IDNRK
"'表名:' tab_g_mail-tabname
"'字段:' tab_g_mail-FNAME
'字段名称' tab_g_mail-ddtext
'操作:' itab_text-CHNGIND
'旧值:' tab_g_mail-VALUE_OLD '新值:' tab_g_mail-VALUE_NEW
into rec_l_contents separated by space.
append rec_l_contents to i_objtxt.
*** 附件内容
CLEAR:itab_text.
itab_text-matnr = tab_g_mail-matnr.
itab_text-stlal = tab_g_mail-stlal.
itab_text-tabname = tab_werks-tabname.
itab_text-tabkey = tab_werks-TABKEY.
itab_text-FNAME = tab_g_mail-FNAME.
itab_text-ddtext = tab_g_mail-ddtext.
itab_text-idnrk = tab_werks-IDNRK.
itab_text-CHNGIND = tab_g_mail-CHNGIND.
itab_text-VALUE_OLD = tab_g_mail-VALUE_OLD.
itab_text-VALUE_NEW = tab_g_mail-VALUE_NEW.
APPEND itab_text.
endloop.
clear rec_l_contents.
concatenate rec_l_contents '详细信息请查阅附件,谢谢!' into rec_l_contents separated by space.
append rec_l_contents to i_objtxt.
DESCRIBE TABLE i_objtxt LINES v_lines_txt.
i_objpack-transf_bin = ''.
i_objpack-head_start = 1.
i_objpack-head_num = 0.
i_objpack-body_start = 1.
i_objpack-body_num = v_lines_txt.
i_objpack-doc_type = 'RAW'.
APPEND i_objpack.
**内表作为邮件附件
DATA: tmpstr TYPE string .
CLEAR: tmpstr,i_record,i_record[].
PERFORM itabtostr TABLES itab_text USING tmpstr.
PERFORM strtorecord TABLES i_record USING tmpstr filelen.
APPEND LINES OF i_record TO i_objbin.
DESCRIBE TABLE i_record LINES v_lines_bin.
DESCRIBE TABLE i_objbin LINES v_lines_bin_all.
i_objpack-transf_bin = 'X'.
i_objpack-body_start = v_lines_bin_all - v_lines_bin + 1 .
i_objpack-body_num = v_lines_bin.
i_objpack-doc_type = 'XLS'.
i_objpack-obj_name = 'text'.
i_objpack-doc_size = v_lines_bin * 255.
CONCATENATE tab_werks-TABKEY '.xls' '' INTO i_objpack-obj_descr."附件名
APPEND i_objpack.
**接收人
* CLEAR i_reclist.
* i_reclist-receiver = ''.
* i_reclist-express = 'X'.
* i_reclist-rec_type = 'U'.
* APPEND i_reclist.
****收件人
SELECT EMAIL as pmail
INTO CORRESPONDING FIELDS OF TABLE tab_mail
FROM ZPP_0810
WHERE WERKS = TAB_WERKS-WERKS AND
( STLBE = TAB_WERKS-STLBE OR STLBE = '*').
LOOP AT TAB_MAIL.
CLEAR i_reclist.
i_reclist-receiver = tab_mail-pmail.
i_reclist-express = 'X'.
i_reclist-rec_type = 'U'.
APPEND i_reclist.
ENDLOOP.
REFRESH tab_mail.
CLEAR:tab_mail.
SELECT EMAIL as pmail
INTO CORRESPONDING FIELDS OF TABLE tab_mail
FROM ZPP_0810
WHERE ( WERKS = TAB_WERKS-WERKS OR WERKS = '*' ) AND
( STLBE = TAB_WERKS-STLBE ).
LOOP AT TAB_MAIL.
CLEAR i_reclist.
i_reclist-receiver = tab_mail-pmail.
i_reclist-express = 'X'.
i_reclist-rec_type = 'U'.
APPEND i_reclist.
ENDLOOP.
REFRESH tab_mail.
CLEAR:tab_mail.
SELECT EMAIL as pmail
INTO CORRESPONDING FIELDS OF TABLE tab_mail
FROM ZPP_0810
WHERE ( WERKS = '*' ) AND
( STLBE = '*').
LOOP AT TAB_MAIL.
CLEAR i_reclist.
i_reclist-receiver = tab_mail-pmail.
i_reclist-express = 'X'.
i_reclist-rec_type = 'U'.
APPEND i_reclist.
ENDLOOP.
REFRESH tab_mail.
CLEAR:tab_mail.
***删除重复
sort i_reclist.
DELETE ADJACENT DUPLICATES FROM i_reclist COMPARING ALL FIELDS.
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = v_docchgi
put_in_outbox = 'X'
commit_work = 'X'
TABLES
packing_list = i_objpack
object_header = v_objhead
contents_bin = i_objbin
contents_txt = i_objtxt
receivers = i_reclist
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.
REFRESH: i_objpack,v_objhead,i_objbin,i_objtxt,i_reclist,itab_text.
CLEAR:i_objpack,v_objhead,i_objbin,i_objtxt,i_reclist,v_docchgi,itab_text.
ENDLOOP.
wait up to 2 seconds.
submit rsconn01 with mode = 'INT'
with output = 'X' and RETURN.
endform. "SEND_MAIL
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GET_DATA .
IF ld_start = ldt_now. "同一天
select * from CDHDR
* join CDHDR ON
* CDHDR~MANDANT = CDPOS~MANDANT AND
* CDHDR~OBJECTCLAS = CDPOS~OBJECTCLAS AND
* CDHDR~OBJECTID = CDPOS~OBJECTID AND
* CDHDR~CHANGENR = CDPOS~CHANGENR
into CORRESPONDING FIELDS OF TABLE LT_CDHDR_TEMP
WHERE CDHDR~TCODE IN ('CS02','CV02N') AND
CDHDR~UDATE = LD_START AND
CDHDR~UTIME >= ldt_starttime AND
CDHDR~UTIME < ldt_time.
APPEND LINES OF LT_CDHDR_TEMP TO LT_CDHDR.
else."跨日
select * from CDHDR
* join CDHDR ON
* CDHDR~MANDANT = CDPOS~MANDANT AND
* CDHDR~OBJECTCLAS = CDPOS~OBJECTCLAS AND
* CDHDR~OBJECTID = CDPOS~OBJECTID AND
* CDHDR~CHANGENR = CDPOS~CHANGENR
into CORRESPONDING FIELDS OF TABLE LT_CDHDR_TEMP
WHERE CDHDR~TCODE IN ('CS02','CV02N') AND
CDHDR~UDATE = LD_START AND
CDHDR~UTIME >= ldt_starttime AND
CDHDR~UTIME <= '235959'.
APPEND LINES OF LT_CDHDR_TEMP TO LT_CDHDR.
select * from CDHDR
* join CDHDR ON
* CDHDR~MANDANT = CDPOS~MANDANT AND
* CDHDR~OBJECTCLAS = CDPOS~OBJECTCLAS AND
* CDHDR~OBJECTID = CDPOS~OBJECTID AND
* CDHDR~CHANGENR = CDPOS~CHANGENR
into CORRESPONDING FIELDS OF TABLE LT_CDHDR_TEMP
WHERE CDHDR~TCODE IN ('CS02','CV02N') AND
CDHDR~UDATE = LDT_NOW AND
CDHDR~UTIME < LDT_TIME.
APPEND LINES OF LT_CDHDR_TEMP TO LT_CDHDR.
endif.
LOOP AT LT_CDHDR.
SELECT * FROM CDPOS
INTO CORRESPONDING FIELDS OF TABLE LT_CDPOS
WHERE CDPOS~MANDANT = LT_CDHDR-MANDANT AND
CDPOS~OBJECTCLAS = LT_CDHDR-OBJECTCLAS AND
CDPOS~OBJECTID = LT_CDHDR-OBJECTID AND
CDPOS~CHANGENR = LT_CDHDR-CHANGENR.
* AND
* CDPOS~TABKEY IN ('300M000002190000001800000037','300M000002190000000500000010').
* AND
* CDPOS~OBJECTID = '300M00000219' AND
* CDPOS~TABNAME = 'STPO'.
LOOP AT LT_CDPOS.
SELECT count(*) into li_count
FROM ZMM_SEND_MAIL
WHERE ZMM_SEND_MAIL~TABNAME = LT_CDPOS-TABNAME AND
ZMM_SEND_MAIL~FNAME = LT_CDPOS-FNAME AND
ZMM_SEND_MAIL~CHNGIND = LT_CDPOS-CHNGIND.
IF sy-subrc = 0.
APPEND LT_CDPOS TO TAB_G_MAIL.
CLEAR:LT_CDPOS.
ENDIF.
ENDLOOP.
ENDLOOP.
DATA: S_STLTY LIKE STPO-STLTY.
DATA: M_STLNR LIKE MAST-STLNR.
DATA: M_STLAL LIKE MAST-STLAL.
DATA: S_STLNR LIKE STPO-STLNR.
DATA: S_STLKN LIKE STPO-STLKN.
DATA: S_STPOZ LIKE STPO-STPOZ.
DATA: S_IDNRK LIKE STPO-IDNRK.
data: C_TABNAME LIKE CDPOS-TABNAME.
LOOP AT TAB_G_MAIL.
****取工厂
C_TABNAME = TAB_G_MAIL-TABNAME.
IF C_TABNAME = 'STPO' OR C_TABNAME = 'STPU'.
S_STLTY = TAB_G_MAIL-TABKEY+3(1).
S_STLNR = TAB_G_MAIL-TABKEY+4(8).
S_STLKN = TAB_G_MAIL-TABKEY+12(8).
S_STPOZ = TAB_G_MAIL-TABKEY+20(8).
SELECT SINGLE STLAL INTO M_STLAL
FROM STKO
WHERE STLTY = S_STLTY AND
STLNR = S_STLNR.
SELECT SINGLE STLAL INTO M_STLAL
FROM STAS
WHERE STLTY = S_STLTY AND
STLNR = S_STLNR AND
STLKN = S_STLKN .
***取主键信息
SELECT SINGLE IDNRK INTO S_IDNRK
FROM STPO
WHERE STLTY = S_STLTY AND
STLNR = S_STLNR AND
STLKN = S_STLKN AND
STPOZ = S_STPOZ.
ENDIF.
IF C_TABNAME = 'STAS'.
S_STLTY = TAB_G_MAIL-TABKEY+3(1).
S_STLNR = TAB_G_MAIL-TABKEY+4(8).
M_STLAL = TAB_G_MAIL-TABKEY+12(2).
S_STLKN = TAB_G_MAIL-TABKEY+14(8).
S_STPOZ = TAB_G_MAIL-TABKEY+22(8).
***取主键信息
SELECT SINGLE IDNRK INTO S_IDNRK
FROM STPO
WHERE STLTY = S_STLTY AND
STLNR = S_STLNR AND
STLKN = S_STLKN AND
STPOZ = S_STPOZ.
ENDIF.
IF C_TABNAME = 'STKO'.
S_STLTY = TAB_G_MAIL-TABKEY+3(1).
S_STLNR = TAB_G_MAIL-TABKEY+4(8).
M_STLAL = TAB_G_MAIL-TABKEY+12(2).
S_STLKN = TAB_G_MAIL-TABKEY+14(8).
ENDIF.
* AND
* STKOZ = S_STKOZ.
SELECT MAST~WERKS
INTO CORRESPONDING FIELDS OF TABLE lt_werks
FROM MAST
WHERE STLNR = S_STLNR AND
STLAL = M_STLAL.
LOOP AT lt_werks.
TAB_WERKS-TABNAME = TAB_G_MAIL-TABNAME.
TAB_WERKS-TABKEY = TAB_G_MAIL-TABKEY.
TAB_WERKS-WERKS = LT_WERKS-WERKS.
TAB_WERKS-IDNRK = S_IDNRK.
SELECT SINGLE STLBE INTO TAB_WERKS-STLBE
FROM STZU
WHERE STLTY = S_STLTY AND
STLNR = S_STLNR.
APPEND TAB_WERKS.
CLEAR:LT_WERKS.
ENDLOOP.
SELECT SINGLE MAST~MATNR MAST~WERKS
INTO (TAB_G_MAIL-MATNR,TAB_G_MAIL-WERKS)
FROM MAST
WHERE STLNR = S_STLNR AND
STLAL = M_STLAL.
TAB_G_MAIL-STLAL = M_STLAL.
* S_STLTY = TAB_G_MAIL-TABKEY+3(1).
* M_STLNR = TAB_G_MAIL-TABKEY+4(8).
* M_STLAL = TAB_G_MAIL-TABKEY+12(2).
* SELECT MAST~WERKS
* INTO CORRESPONDING FIELDS OF TABLE lt_werks
* FROM MAST
* WHERE STLNR = M_STLNR AND
* STLAL = M_STLAL.
* LOOP AT lt_werks.
* TAB_WERKS-TABKEY = TAB_G_MAIL-TABKEY.
* TAB_WERKS-WERKS = LT_WERKS-WERKS.
* APPEND TAB_WERKS.
* CLEAR:LT_WERKS.
* ENDLOOP.
MODIFY TAB_G_MAIL.
ENDLOOP.
sort TAB_WERKS by TABNAME TABKEY WERKS IDNRK.
DELETE ADJACENT DUPLICATES FROM TAB_WERKS COMPARING ALL FIELDS.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form itabtostr
*&---------------------------------------------------------------------*
FORM itabtostr TABLES intab
USING outstr TYPE string.
DATA: tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
enter(2) TYPE c VALUE cl_abap_char_utilities=>cr_lf,
n TYPE i.
DATA: BEGIN OF headtab OCCURS 0 ,
length TYPE i ,
decimals TYPE i,
type_kind TYPE c,
name(30) TYPE c,
END OF headtab.
DATA descr_ref TYPE REF TO cl_abap_structdescr.
FIELD-SYMBOLS: <comp_wa> TYPE abap_compdescr ,
<f_field> ,
<f_intab> TYPE ANY .
DATA:str TYPE string,
str2 TYPE string ,
text1 TYPE c.
descr_ref ?= cl_abap_typedescr=>describe_by_data( intab ).
LOOP AT descr_ref->components ASSIGNING <comp_wa>.
MOVE-CORRESPONDING <comp_wa> TO headtab.
APPEND headtab.
ENDLOOP.
DESCRIBE TABLE headtab LINES n.
LOOP AT intab ASSIGNING <f_intab>.
DO n TIMES.
ASSIGN COMPONENT sy-index OF STRUCTURE <f_intab> TO <f_field>.
str = <f_field>.
READ TABLE headtab INDEX sy-index.
IF headtab-type_kind = 'I' OR headtab-type_kind = 'P'
OR headtab-type_kind = 'F'.
SEARCH str FOR '-'.
IF sy-subrc = 0 AND sy-fdpos <> 0.
SPLIT str AT '-' INTO str text1.
CONDENSE str.
CONCATENATE '-' str INTO str.
ELSE.
CONDENSE str.
ENDIF.
ELSE.
* SHIFT str LEFT DELETING LEADING '0' .
ENDIF.
CONCATENATE str2 tab str INTO str2.
ENDDO.
SHIFT str2.
CONCATENATE outstr str2 enter INTO outstr.
CLEAR str2.
ENDLOOP.
ENDFORM. "itabtostr
*&---------------------------------------------------------------------*
*& Form strtorecord
*&---------------------------------------------------------------------*
FORM strtorecord TABLES record USING str len.
DATA:tmpbuffer TYPE xstring.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = str
mimetype = '"text/html; charset=gb2312"'
* encoding = '8400'
IMPORTING
buffer = tmpbuffer
EXCEPTIONS
failed = 1
OTHERS = 2.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = tmpbuffer
append_to_table = ''
IMPORTING
output_length = len
TABLES
binary_tab = record.
ENDFORM. "strtorecord
FORM GET_DATA .
通过后台作业配置4小时定期取配方变更信息
通过ZMM_SEND_EMAIL自建表控制哪些表修改才发送邮件
perform send_mail tables tab_g_mail.
对符合的数据进行定向发送EMAIL,通过自建表zpp_0810配置工厂对应的收件地址,取得接受人地址;
内容分两种方式发送:1、邮件内容i_objtxt;2、附近已EXCEL发送 itab_text
i_objtxt
-->