Paperless Project(自动EMAIL PO)
Paperless Project(Auto-eamail)
ME9F Output 后,CALL 出自定义屏,可以输入自定义用户邮件内容,更改发件人,收件,CC,BCC等信息。
然后将PO转成PDF附件格式和邮件内容一起发给指定邮箱
FUNCTION convert_otf_and_mail.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(MAIL_SENDER) LIKE SWOTOBJID STRUCTURE SWOTOBJID
*" VALUE(MAIL_RECIPIENT) LIKE SWOTOBJID STRUCTURE SWOTOBJID
*" VALUE(MAILOPTIONS) LIKE ITCPP STRUCTURE ITCPP
*" EXPORTING
*" VALUE(MAIL_OK) TYPE C
*" VALUE(OFFICE_OBJID) LIKE SOODK STRUCTURE SOODK
*" REFERENCE(MSGID) TYPE SYMSGID
*" REFERENCE(MSGNO) TYPE SYMSGNO
*" REFERENCE(MSGV1) TYPE ANY
*" REFERENCE(MSGV2) TYPE ANY
*" REFERENCE(MSGV3) TYPE ANY
*" REFERENCE(MSGV4) TYPE ANY
*" TABLES
*" MAIL_APPLICATION_OBJECT_IDS STRUCTURE SWOTOBJID
*" OTF STRUCTURE ITCOO
*"----------------------------------------------------------------------
STATICS BEGIN OF off_tab OCCURS 100.
INCLUDE STRUCTURE soli.
STATICS END OF off_tab.
STATICS BEGIN OF rec_tab OCCURS 1.
INCLUDE STRUCTURE soos1.
STATICS END OF rec_tab.
STATICS BEGIN OF head_tab OCCURS 1.
INCLUDE STRUCTURE soli.
STATICS END OF head_tab.
"Marked By M.C 2009.12.17
* STATICS BEGIN OF obj_hd_change.
* INCLUDE STRUCTURE sood1.
* STATICS END OF obj_hd_change.
"M.C 2009.12.17
data: BEGIN OF obj_hd_change.
INCLUDE STRUCTURE sood1.
data: END OF obj_hd_change.
"end of changing
STATICS: obj_type LIKE sood-objtp,
obj_lang LIKE thead-tdspras,
form TYPE tdobjname,
originator LIKE soos1-recextnam.
DATA: sent_to_all LIKE sonv-flag,
l_subrc LIKE sy-subrc.
DATA: l_entry TYPE soli,
l_wa TYPE soli.
DATA: it_message LIKE soli OCCURS 10 WITH HEADER LINE. " Email Content M.C 2008.12.16
DATA: l_ebeln LIKE thead-tdname, " Email Content M.C 2008.12.16
l_objdes LIKE sood1-objdes.
DATA: lt_text LIKE tline OCCURS 0 WITH HEADER LINE, " Email Content M.C 2008.12.16
l_objnam LIKE sood1-objnam.
"M.C 2010.01.13
DATA: L_subject(120) type c,
L_REVNO LIKE EKKO-REVNO,
L_SORTL LIKE LFA1-SORTL.
DATA: L_PATH TYPE STRING.
REFRESH: off_tab, rec_tab, head_tab.
* copy OTF into SAPconnect tab and get language, layout set
CLEAR: obj_lang, form.
LOOP AT otf.
off_tab-line = otf. APPEND off_tab.
IF obj_lang = space AND otf-tdprintcom = pc_id_codepage.
obj_lang = otf-tdprintpar+8(1). "get 1st language in doc
ENDIF.
IF form = space AND otf-tdprintcom = pc_id_info.
IF otf-tdprintpar(2) = '01'.
form = otf-tdprintpar+3(16). "get layout set name
ENDIF.
ENDIF.
ENDLOOP.
* send data to SAPoffice
CLEAR obj_hd_change.
obj_hd_change-objla = obj_lang.
obj_hd_change-objnam = mailoptions-tddataset.
obj_hd_change-objnam+6 = mailoptions-tdsuffix1.
IF obj_hd_change-objnam = space.
obj_hd_change-objnam = 'SAPScriptMail'.
ENDIF.
obj_hd_change-objsns = 'F'. "functional object
obj_hd_change-objdes = mailoptions-tdtitle.
IF obj_hd_change-objdes = space.
obj_hd_change-objdes = '$/$/$'.
IF mailoptions-tdprogram = space.
REPLACE '$' WITH sy-cprog INTO obj_hd_change-objdes.
ELSE.
REPLACE '$' WITH mailoptions-tdprogram INTO obj_hd_change-objdes.
ENDIF.
CONDENSE obj_hd_change-objdes.
REPLACE '$' WITH sy-datum INTO obj_hd_change-objdes.
CONDENSE obj_hd_change-objdes.
REPLACE '$' WITH sy-uzeit INTO obj_hd_change-objdes.
CONDENSE obj_hd_change-objdes.
ENDIF.
CLEAR rec_tab.
rec_tab-recextnam = mail_recipient.
rec_tab-recesc = 'J'. "BOR address object
rec_tab-mailstatus = 'E'. "inform if error occurs
rec_tab-deliver = 'X'. "request delivery notification
rec_tab-not_deli = 'X'. "request non-delivery notification
APPEND rec_tab.
CLEAR head_tab[].
APPEND c_sapconnect_sapscript TO head_tab.
itcpp_to_head_tab 'TDDEVICE' mailoptions-tddevice.
itcpp_to_head_tab 'TDNOPRINT' mailoptions-tdnoprint.
*itcpp_to_head_tab 'TDNOARCH' mailoptions-tdnoarch.
*itcpp_to_head_tab 'TDARMOD' mailoptions-tdarmod.
itcpp_to_head_tab 'TDDEST' mailoptions-tddest.
itcpp_to_head_tab 'TDPAGES' mailoptions-tdpages.
itcpp_to_head_tab 'TDIEXIT' mailoptions-tdiexit.
itcpp_to_head_tab 'USEREXIT' mailoptions-userexit.
itcpp_to_head_tab 'TDSCABAP' mailoptions-tdscabap.
itcpp_to_head_tab 'TDSCDRIVER' mailoptions-tdscdriver.
itcpp_to_head_tab 'TDSCREEN' mailoptions-tdscreen.
*itcpp_to_head_tab 'FAXFORMAT' mailoptions-faxformat.
itcpp_to_head_tab 'TDAPPL' mailoptions-tdappl.
itcpp_to_head_tab 'TDDATASET' mailoptions-tddataset.
itcpp_to_head_tab 'TDSUFFIX1' mailoptions-tdsuffix1.
itcpp_to_head_tab 'TDTITLE' mailoptions-tdtitle.
itcpp_to_head_tab 'TDPROGRAM' mailoptions-tdprogram.
CLEAR head_tab.
head_tab-line = form.
APPEND head_tab.
obj_type = 'OTF'.
originator = mail_sender.
"Below codes added by M.C 2008.12.16
"GET PO Number
l_objdes = obj_hd_change-objdes.
TABLES: ZPONO_EXTRAC.
SELECT SINGLE *
FROM ZPONO_EXTRAC WHERE ZTYPE = 'PO'.
IF SY-SUBRC EQ 0.
l_ebeln = l_objdes+ZPONO_EXTRAC-STARTP(ZPONO_EXTRAC-LEN).
ELSE.
l_ebeln = l_objdes+7(10). "PO NO.
endif.
l_objnam = obj_hd_change-objnam. "==MAIL'
CONDENSE L_EBELN.
DATA: L TYPE I.
L = STRLEN( L_EBELN ).
IF L <> 10.
MESSAGE E398(00) WITH TEXT-E11.
ENDIF.
"Popup Write email window
"Get PO EMAIL CONTENT FROM PO TEXT.
* CALL FUNCTION 'ZGET_LONG_TEXT'
* EXPORTING
* textname = l_ebeln
* ztdobject = 'EKKO'
* ztdid = 'F06'
* ztdspras = sy-langu
* TABLES
* ou_itab = lt_text.
*
* LOOP AT lt_text.
* it_message = lt_text-tdline.
* APPEND it_message.
* ENDLOOP.
"End Of adding
"M.C 2009.12.15
TABLES :ZPOARH_PATH,EKKO,EKPO. "Download path table.
DATA: L_FILE TYPE string VALUE 'C:\tmp\'. "Default
DATA: L_PONO LIKE EKKO-EBELN.
DATA: LT_ARC_PATH LIKE ZPOARH_PATH OCCURS 0 WITH HEADER LINE.
MOVE L_EBELN TO L_PONO.
CLEAR G_LIFNR.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = L_PONO
IMPORTING
OUTPUT = L_PONO.
CLEAR :L_REVNO,
L_SORTL,
L_SUBJECT.
SELECT SINGLE * from EKKO WHERE EBELN = L_PONO.
IF SY-SUBRC EQ 0.
move EKKO-LIFNR TO G_LIFNR.
MOVE EKKO-REVNO TO L_REVNO.
"M.C 2010.01.13
SELECT SINGLE * FROM LFA1 WHERE LIFNR = EKKO-LIFNR.
IF SY-SUBRC EQ 0.
IF LFA1-SORTL NE ''. MOVE LFA1-SORTL TO L_SORTL.
ELSE. MOVE LFA1-LIFNR TO L_SORTL.ENDIF.
IF L_REVNO EQ ''. L_REVNO = '1.0'. ENDIF.
CONDENSE L_REVNO.
CONDENSE L_SORTL.
CONCATENATE L_PONO L_REVNO L_SORTL INTO L_SUBJECT SEPARATED BY '-'.
ELSE.
L_SUBJECT = L_PONO.
ENDIF.
"END OF ADDING
SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_ARC_PATH
FROM ZPOARH_PATH WHERE WERKS = EKKO-EKORG.
IF SY-SUBRC EQ 0.
READ TABLE LT_ARC_PATH INDEX 1.
IF SY-SUBRC EQ 0.
CLEAR L_PATH.
MOVE LT_ARC_PATH-zpath TO L_PATH.
PERFORM SYS_CHECKING USING L_PATH. "Check path.
CONCATENATE LT_ARC_PATH-zpath L_SUBJECT '.PDF' into L_file.
if NOT L_FILE CS '\'.
MESSAGE E398(00) WITH 'Invalid path or file name!'.
endif.
endif.
else.
CONCATENATE L_file L_SUBJECT '.PDF' into L_file .
endif.
ELSE.
MESSAGE E398(00) WITH TEXT-E12.
endif.
data: it_adr6 like adr6 OCCURS 0 WITH HEADER LINE.
DATA lt_email TYPE TABLE OF string.
DATA ls_email TYPE string.
"initial objects.
data: l_sender like ADR6-SMTP_ADDR.
data: lt_ret like sy-subrc.
data: l_from like adr6-smtp_addr.
data: lt_sender like zsender_list OCCURS 0 WITH HEADER LINE.
Refresh TXT_ITAB.
CLEAR EDT_FROM. "manson
CLEAR EDT_TO.
CLEAR EDT_CC.
CLEAR EDT_BCC.
CLEAR EDT_SUBJECT.
CLEAR EMAIL_CHANGE_FLAG.
"M.C 2010.01.14
CALL FUNCTION 'ZGET_SENDER_EMAIL'
EXPORTING
IM_LOGIN = sy-uname
TABLES
ex_sender = lt_sender
.
if not lt_sender[] is INITIAL.
READ TABLE lt_sender index 1.
if sy-subrc eq 0.
move lt_sender-smtp_addr to l_from.
endif.
endif.
"end of insert
PERFORM Write_Email TABLES it_adr6
USING l_subject
l_from.
"M.C 2010.01.14 Update sender email address
L_SENDER = EDT_FROM.
IF L_SENDER IS INITIAL
OR not L_SENDER CS '@'.
MESSAGE e398(00) with 'Invalid sender email address,cannot continue...!'.
else.
CALL FUNCTION 'ZUPDATE_SENDER_EMAIL'
EXPORTING
im_email = l_sender
IMPORTING
EX_RETURN = lt_Ret.
.
if lt_Ret NE 0.
MESSAGE e398(00) WITH 'Update sender email address was faild!'.
endif.
endif.
"M.C 2009.12.16
"e+ add email content from popup windor editor
IF NOT TXT_ITAB[] IS INITIAL AND EMAIL_CHANGE_FLAG EQ 'X'.
FIELD-SYMBOLS TYPE TEXT_TABLE_LINE.
Loop at TXT_ITAB ASSIGNING .
IT_MESSAGE = -LINE.
APPEND IT_MESSAGE.
ENDLOOP.
* ELSE.
* MESSAGE E398(00) WITH TEXT-E14.
** leave to SCREEN 0.
ENDIF.
"Reset reciver
DATA: lc_net(1) TYPE c VALUE 'U',
lc_mail(4) TYPE c VALUE 'MAIL'.
if not it_adr6[] is INITIAL.
*"Multiple contact
* Loop at it_adr6.
* rec_tab-RECEXTNAM = it_adr6-smtp_addr.
* rec_tab-recesc = lc_net.
* rec_tab-sndart = lc_mail.
* rec_tab-sndex = 'X'.
* rec_tab-sndpri = 1.
* rec_tab-mailstatus = 'E'.
* APPEND rec_tab.
* ENDLOOP.
* to
REFRESH lt_EMAIL.
clear ls_email.
IF EDT_TO IS NOT INITIAL.
SPLIT EDT_TO AT ';' INTO TABLE lt_email.
ENDIF.
LOOP AT lt_email INTO ls_email.
rec_tab-recextnam = ls_email.
rec_tab-recesc = lc_net.
rec_tab-sndart = lc_mail.
rec_tab-sndex = 'X'.
rec_tab-sndpri = 1.
rec_tab-mailstatus = 'E'.
APPEND REC_TAB.
ENDLOOP.
endif.
"Copy to
REFRESH lt_EMAIL.
clear ls_email.
IF EDT_CC IS NOT INITIAL.
SPLIT EDT_CC AT ';' INTO TABLE lt_email.
ENDIF.
LOOP AT lt_email INTO ls_email.
rec_tab-recextnam = ls_email.
rec_tab-recesc = lc_net.
rec_tab-sndart = lc_mail.
rec_tab-sndex = 'X'.
rec_tab-sndcp = 1.
rec_tab-mailstatus = 'E'.
APPEND REC_TAB.
ENDLOOP.
"BCC to
REFRESH lt_EMAIL.
clear ls_email.
IF EDT_BCC IS NOT INITIAL.
SPLIT EDT_BCC AT ';' INTO TABLE lt_email.
ENDIF.
LOOP AT lt_email INTO ls_email.
rec_tab-recextnam = ls_email.
rec_tab-recesc = lc_net.
rec_tab-sndart = lc_mail.
rec_tab-sndex = 'X'.
rec_tab-sndbc = 1.
rec_tab-mailstatus = 'E'.
APPEND REC_TAB.
ENDLOOP.
"Change the email Subject.
if obj_hd_change-OBJDES <> edt_subject
and not edt_subject is INITIAL AND EMAIL_CHANGE_FLAG EQ 'X'.
obj_hd_change-OBJDES = edt_subject.
endif.
"SAVE CURRENT EMAIL CONTENT TO SAPOFFICE(SO01)
*
* CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'
* EXPORTING
* owner = sy-uname
* region = ou_fol
* IMPORTING
* folder_id = l_folder_id
* EXCEPTIONS
* OTHERS = 5.
*
*
*DATA: lt_attachments LIKE STANDARD TABLE OF sood5 WITH HEADER LINE.
* g_document-foltp = l_folder_id-foltp.
* g_document-folyr = l_folder_id-folyr.
* g_document-folno = l_folder_id-folno.
* g_document-objtp = c_objtp.
* g_document-objdes = obj_hd_change-OBJDES.
* g_document-file_ext = c_file_ext.
*
* g_header_data-objdes = obj_hd_change-OBJDES.
*
* CALL FUNCTION 'SO_DOCUMENT_REPOSITORY_MANAGER'
* EXPORTING
* method = 'SAVE'
* office_user = sy-uname
* IMPORTING
* authority = g_authority
* TABLES
* objcont = it_message
* attachments = lt_attachments
* CHANGING
* document = g_document
* header_data = g_header_data
* EXCEPTIONS
* OTHERS = 1.
*
*
*"Insert attchment to sapoffice
* folder_id-objtp = l_folder_id-foltp.
* folder_id-objyr = l_folder_id-folyr.
* folder_id-objno = l_folder_id-folno.
*
* object_id-objtp = c_objtp.
* object_id-objyr = g_document-objyr.
* object_id-objno = g_document-objno.
*
* link_folder_id-objtp = l_folder_id-foltp.
* link_folder_id-objyr = l_folder_id-folyr.
* link_folder_id-objno = l_folder_id-folno.
*DATA: attach_type LIKE sood-objtp VALUE 'OTF' .
*
* g_header_data-objdes = obj_hd_change-OBJDES.
* CALL FUNCTION 'SO_ATTACHMENT_INSERT'
* EXPORTING
* object_id = object_id
* attach_type = attach_type
* object_hd_change = obj_hd_change
* owner = sy-uname
* TABLES
* objcont = off_tab
* objhead = head_tab
* EXCEPTIONS
* active_user_not_exist = 35
* communication_failure = 71
* object_type_not_exist = 17
* operation_no_authorization = 21
* owner_not_exist = 22
* parameter_error = 23
* substitute_not_active = 31
* substitute_not_defined = 32
* system_failure = 72
* x_error = 1000.
*
* IF sy-subrc > 0.
* ENDIF.
"End of addintional function M.C 2009.12.17
DATA it_otf like itcoo occurs 100 with header line.
DATA CANCEL.
DATA PDF LIKE TLINE OCCURS 100 WITH HEADER LINE.
DATA GT_PDF_DATA LIKE TLINE OCCURS 100 WITH HEADER LINE.
DATA DOCTAB LIKE DOCS OCCURS 1 WITH HEADER LINE.
DATA: NUMBYTES TYPE I,
g_numbytes TYPE I.
it_otf[] = otf[].
"Convert to PDF
PERFORM CONVERT_PDF TABLES IT_otf
gt_pdf_data
CHANGING g_numbytes.
"Download PDF data to Local
perform download_w_ext tables gt_pdf_data
using l_file
'.pdf'
'BIN'
g_numbytes
cancel.
"End of adding M.C 2009.12.15
* call SO_OBJECT_SEND
CALL FUNCTION 'SO_OBJECT_SEND'
EXPORTING
* folder_id = folder_id
* object_id = object_id
** outbox_flag = 'X'
* link_folder_id = link_folder_id
object_hd_change = obj_hd_change
object_type = obj_type
owner = SY-UNAME
STORE_FLAG = 'X'
* DELETE_FLAG = ' '
* SENDER = ' '
* CHECK_ALREADY_SENT = ' '
* GIVE_OBJECT_BACK =
originator = originator
originator_type = 'J'
* LINK_FOLDER_ID = ' '
IMPORTING
object_id_new = office_objid
sent_to_all = sent_to_all
* OFFICE_OBJECT_KEY =
TABLES
objcont = off_tab
objhead = head_tab
* OBJPARA =
* OBJPARB =
receivers = rec_tab
* PACKING_LIST =
* ATT_CONT = off_tab
* ATT_HEAD = head_tab
* NOTE_TEXT =
note_text = it_message "Email Content M.C 2008.12.16
* LINK_LIST =
application_object = mail_application_object_ids
EXCEPTIONS
active_user_not_exist = 1
communication_failure = 2
component_not_available = 3
folder_not_exist = 4
folder_no_authorization = 5
forwarder_not_exist = 6
note_not_exist = 7
object_not_exist = 8
object_not_sent = 9
object_no_authorization = 10
object_type_not_exist = 11
operation_no_authorization = 12
owner_not_exist = 13
parameter_error = 14
substitute_not_active = 15
substitute_not_defined = 16
system_failure = 17
too_much_receivers = 18
user_not_exist = 19
x_error = 20
OTHERS = 21.
l_subrc = sy-subrc.
CALL FUNCTION 'SSF_SO_ERROR'
EXPORTING
sent_to_all = sent_to_all
subrc = l_subrc
IMPORTING
ok = mail_ok
msgid = msgid
msgno = msgno
msgv1 = msgv1
msgv2 = msgv2
msgv3 = msgv3
msgv4 = msgv4
TABLES
receivers = rec_tab.
ENDFUNCTION.
"M.C 2009.12.15 Download to Local
form download_w_ext tables datatab
using filename type string
value(extension) type c
mode type c
bin_filesize type i
cancel type c.
TYPES:
abap_encoding TYPE abap_encod.
DATA: cncode type CPCODEPAGE ,
CNCODEN TYPE ABAP_ENCODING.
data: name type string,
path type string,
fullpath type string,
ext type string,
filter type string,
size type i,
guiobj type ref to cl_gui_frontend_services,
uact type i.
IF NOT DATATAB[] IS INITIAL.
if mode <> 'ASC' and mode <> 'BIN'.
sy-subrc = 1. exit.
endif.
clear cancel.
ext = extension.
if ext(1) = '.'.
shift ext.
endif.
name = filename.
if name is initial.
name = 'test.*'.
replace '$' with ext into name.
elseif name na '.'. "name has no extension, add ext
concatenate name '.' ext into name.
endif.
filter = '(*.$)|*.$|'.
replace '$' with ext into filter.
replace '$' with ext into filter.
CALL FUNCTION 'NLS_GET_FRONTEND_CP'
EXPORTING
langu = '1'
IMPORTING
frontend_codepage = cncode
EXCEPTIONS
illegal_syst_codepage = 21
no_frontend_cp_found = 23
internal_or_db_error = 25.
MOVE CNCODE TO CNCODEN .
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
BIN_FILESIZE = bin_filesize
FILENAME = filename
FILETYPE = 'BIN'
* DAT_MODE = 'X'
* WRITE_FIELD_SEPARATOR = 'X'
codepage = cncoden "Added by michael 2008.05.06
* IMPORTING
* FILELENGTH = NUMBYTES
TABLES
DATA_TAB = datatab
.
ENDIF.
ENDFORM.
"end of adding
"M.C 2009.12.15
FORM CONVERT_PDF TABLES HOTFDATA "Convert to PDF
LT_PDF_DTA
CHANGING l_DOC_SIZE.
DATA: HFORMAT(10) TYPE C.
IF NOT HOTFDATA[] IS INITIAL.
HFORMAT = 'PDF'.
CALL FUNCTION 'CONVERT_OTF'
EXPORTING
FORMAT = HFORMAT
* MAX_LINEWIDTH = 132
IMPORTING
BIN_FILESIZE = l_DOC_SIZE
TABLES
OTF = HOTFDATA
LINES = LT_PDF_DTA
EXCEPTIONS
ERR_MAX_LINEWIDTH = 1
ERR_FORMAT = 2
ERR_CONV_NOT_POSSIBLE = 3
OTHERS = 4.
ENDIF.
ENDFORM.
"M.C 2009.12.16
FORM Write_Email TABLES im_adr6
USING L_TITLE
L_FROM.
DATA: LT_ADR6 LIKE ADR6 OCCURS 0 WITH HEADER LINE.
DATA: L_Receiver type string.
CALL FUNCTION 'ZGET_VENDOR_EMAIL'
EXPORTING
im_lifnr = G_LIFNR
IMPORTING
EX_STMP_ADDR = L_Receiver
TABLES
EX_ADR6 = LT_ADR6.
READ TABLE LT_ADR6 index 1.
EDT_FROM = L_FROM.
EDT_TO = L_Receiver.
EDT_SUBJECT = L_TITLE.
IM_ADR6[] = LT_ADR6[].
REFRESH TXT_ITAB.
CALL SCREEN 888 STARTING AT 200 300
ENDING AT 400 600.
ENDFORM.
FORM SYS_CHECKING USING L_PATH.
DATA: obj type REF TO CL_GUI_FRONTEND_SERVICES.
DATA: L_RETURN TYPE ABAP_BOOL.
DATA: RTV TYPE I.
DATA: tmp_file type string.
create OBJECT obj.
CALL METHOD OBJ->DIRECTORY_EXIST
EXPORTING
DIRECTORY = L_path
RECEIVING
result = L_RETURN.
if l_return NE 'X'.
MESSAGE E398(00) with TEXT-E04.
ENDIF.
FREE obj.
ENDFORM.
阅读(2235) | 评论(0) | 转发(0) |