*&---------------------------------------------------------------------*
*& Report ZVR_WM_MAIL_4_UNUSED_MATERIAL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zvr_wm_mail_4_unused_material.
TYPES : BEGIN OF ty_s032,
werks TYPE werks_d,
lgort TYPE lgort_d,
matnr TYPE matnr,
mtart TYPE mtart,
matkl TYPE matkl,
letztver TYPE letztver,
END OF ty_s032.
TYPES : BEGIN OF ty_makt,
matnr TYPE matnr,
spras TYPE spras,
maktx TYPE maktx,
END OF ty_makt.
TYPES : BEGIN OF ty_mard,
matnr TYPE matnr,
werks TYPE werks_d,
lgort TYPE lgort_d,
labst TYPE labst,
END OF ty_mard.
TYPES : BEGIN OF ty_mbew,
matnr TYPE matnr,
vprsv TYPE vprsv,
verpr TYPE verpr,
stprs TYPE stprs,
END OF ty_mbew.
TYPES : BEGIN OF ty_final,
matnr TYPE matnr,
maktx TYPE maktx,
mtart TYPE mtart,
matkl TYPE matkl,
werks TYPE werks_d,
lgort TYPE lgort_d,
labst TYPE labst,
value TYPE char13,"verpr,
END OF ty_final.
TYPES: BEGIN OF xml_line,
data(255) TYPE x,
END OF xml_line.
DATA : it_s032 TYPE STANDARD TABLE OF ty_s032,
wa_s032 TYPE ty_s032,
it_makt TYPE STANDARD TABLE OF ty_makt,
wa_makt TYPE ty_makt,
it_mard TYPE STANDARD TABLE OF ty_mard,
wa_mard TYPE ty_mard,
it_mbew TYPE STANDARD TABLE OF ty_mbew,
wa_mbew TYPE ty_mbew,
it_final TYPE STANDARD TABLE OF ty_final,
wa_final TYPE ty_final.
DATA : it_mail TYPE STANDARD TABLE OF zmail,
wa_mail TYPE zmail.
DATA : objpack LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE.
DATA : objhead LIKE solisti1 OCCURS 1 WITH HEADER LINE.
DATA : objbin LIKE solix OCCURS 10 WITH HEADER LINE.
DATA : objtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE.
DATA : reclist LIKE somlreci1 OCCURS 5 WITH HEADER LINE.
DATA : doc_chng LIKE sodocchgi1.
DATA : tab_lines LIKE sy-tabix.
DATA : l_num(3).
DATA : subj_date(10) TYPE c.
DATA : lv_date TYPE char10.
DATA : l_ixml TYPE REF TO if_ixml,
l_streamfactory TYPE REF TO if_ixml_stream_factory,
l_ostream TYPE REF TO if_ixml_ostream,
l_renderer TYPE REF TO if_ixml_renderer,
l_document TYPE REF TO if_ixml_document.
DATA : l_element_root TYPE REF TO if_ixml_element,
ns_attribute TYPE REF TO if_ixml_attribute,
r_element_properties TYPE REF TO if_ixml_element,
r_element TYPE REF TO if_ixml_element,
r_worksheet TYPE REF TO if_ixml_element,
r_table TYPE REF TO if_ixml_element,
r_column TYPE REF TO if_ixml_element,
r_row TYPE REF TO if_ixml_element,
r_cell TYPE REF TO if_ixml_element,
r_data TYPE REF TO if_ixml_element,
l_value TYPE string,
l_type TYPE string,
l_text(100) TYPE c,
r_styles TYPE REF TO if_ixml_element,
r_style TYPE REF TO if_ixml_element,
r_style1 TYPE REF TO if_ixml_element,
r_format TYPE REF TO if_ixml_element,
r_border TYPE REF TO if_ixml_element,
num_rows TYPE i.
DATA : g_sent_to_all TYPE sonv-flag,
g_tab_lines TYPE i,
lv_labst TYPE char17,
lv_value TYPE char13.
DATA : l_xml_table TYPE TABLE OF xml_line,
wa_xml TYPE xml_line,
l_xml_size TYPE i,
l_rc TYPE i.
PARAMETERS : date TYPE letztver DEFAULT sy-datum.
PERFORM get_data.
PERFORM process_xml_data.
PERFORM mail.
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_data .
CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
EXPORTING
date = date
days = '00'
months = '00'
signum = '-'
years = '01'
IMPORTING
calc_date = date.
SELECT werks
lgort
matnr
mtart
matkl
letztver
FROM s032
INTO TABLE it_s032
WHERE letztver LE date.
IF it_s032 IS NOT INITIAL.
SELECT matnr
spras
maktx
FROM makt
INTO TABLE it_makt
FOR ALL ENTRIES IN it_s032
WHERE spras = sy-langu AND matnr = it_s032-matnr.
SELECT matnr
werks
lgort
labst
FROM mard
INTO TABLE it_mard
FOR ALL ENTRIES IN it_s032
WHERE matnr = it_s032-matnr AND werks = it_s032-werks AND lgort = it_s032-lgort.
SELECT matnr
vprsv
verpr
stprs
FROM mbew
INTO TABLE it_mbew
FOR ALL ENTRIES IN it_s032
WHERE matnr = it_s032-matnr.
ENDIF.
SELECT *
FROM zmail
INTO TABLE it_mail.
LOOP AT it_s032 INTO wa_s032.
IF wa_s032-lgort IS NOT INITIAL.
wa_final-matnr = wa_s032-matnr.
READ TABLE it_makt INTO wa_makt WITH KEY matnr = wa_final-matnr.
IF sy-subrc = 0.
wa_final-maktx = wa_makt-maktx.
ENDIF.
wa_final-mtart = wa_s032-mtart.
wa_final-matkl = wa_s032-matkl.
wa_final-werks = wa_s032-werks.
wa_final-lgort = wa_s032-lgort.
READ TABLE it_mard INTO wa_mard WITH KEY matnr = wa_s032-matnr werks = wa_s032-werks lgort = wa_s032-lgort.
IF sy-subrc = 0.
wa_final-labst = wa_mard-labst.
IF wa_final-labst IS INITIAL.
CLEAR : wa_final, wa_makt, wa_mard, wa_s032.
CONTINUE.
ENDIF.
ENDIF.
READ TABLE it_mbew INTO wa_mbew WITH KEY matnr = wa_s032-matnr.
IF sy-subrc = 0.
IF wa_mbew-vprsv = 'S'.
wa_final-value = wa_final-labst * wa_mbew-stprs.
ELSEIF wa_mbew-vprsv = 'V'.
wa_final-value = wa_final-labst * wa_mbew-verpr.
ENDIF.
ENDIF.
APPEND wa_final TO it_final.
ENDIF.
CLEAR : wa_final, wa_makt, wa_s032.
ENDLOOP.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form PROCESS_XML_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM process_xml_data .
* Creating a ixml Factory
l_ixml = cl_ixml=>create( ).
* Creating the DOM Object Model
l_document = l_ixml->create_document( ).
* Create Root Node 'Workbook'
l_element_root = l_document->create_simple_element( name = 'Workbook' parent = l_document ).
l_element_root->set_attribute( name = 'xmlns' value = 'urn:schemas-microsoft-com:office:spreadsheet' ).
ns_attribute = l_document->create_namespace_decl( name = 'ss' prefix = 'xmlns' uri = 'urn:schemas-microsoft-com:office:spreadsheet' ).
l_element_root->set_attribute_node( ns_attribute ).
ns_attribute = l_document->create_namespace_decl( name = 'x' prefix = 'xmlns' uri = 'urn:schemas-microsoft-com:office:excel' ).
l_element_root->set_attribute_node( ns_attribute ).
* Create node for document properties.
r_element_properties = l_document->create_simple_element( name = 'MATERIAL_REPORT' parent = l_element_root ).
l_value = sy-uname.
l_document->create_simple_element( name = 'Author' value = l_value parent = r_element_properties ).
* Styles
r_styles = l_document->create_simple_element( name = 'Styles' parent = l_element_root ).
* Style for Header
r_style = l_document->create_simple_element( name = 'Style' parent = r_styles ).
r_style->set_attribute_ns( name = 'ID' prefix = 'ss' value = 'Header' ).
r_format = l_document->create_simple_element( name = 'Font' parent = r_style ).
r_format->set_attribute_ns( name = 'Bold' prefix = 'ss' value = '1' ).
r_format = l_document->create_simple_element( name = 'Interior' parent = r_style ).
r_format->set_attribute_ns( name = 'Color' prefix = 'ss' value = '#92D050' ).
r_format->set_attribute_ns( name = 'Pattern' prefix = 'ss' value = 'Solid' ).
r_format = l_document->create_simple_element( name = 'Alignment' parent = r_style ).
r_format->set_attribute_ns( name = 'Vertical' prefix = 'ss' value = 'Center' ).
r_format->set_attribute_ns( name = 'WrapText' prefix = 'ss' value = '1' ).
r_border = l_document->create_simple_element( name = 'Borders' parent = r_style ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Bottom' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Left' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Top' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Right' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
* Style for Data
r_style1 = l_document->create_simple_element( name = 'Style' parent = r_styles ).
r_style1->set_attribute_ns( name = 'ID' prefix = 'ss' value = 'Data' ).
r_border = l_document->create_simple_element( name = 'Borders' parent = r_style1 ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Bottom' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Left' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Top' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Right' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
* Worksheet
r_worksheet = l_document->create_simple_element( name = 'Worksheet' parent = l_element_root ).
r_worksheet->set_attribute_ns( name = 'Name' prefix = 'ss' value = 'Sheet1' ).
* Table
r_table = l_document->create_simple_element( name = 'Table' parent = r_worksheet ).
r_table->set_attribute_ns( name = 'FullColumns' prefix = 'x' value = '1' ).
r_table->set_attribute_ns( name = 'FullRows' prefix = 'x' value = '1' ).
* Column Formatting
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '40' ).
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '90' ).
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '140' ).
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '150' ).
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '90' ).
* Blank Row
r_row = l_document->create_simple_element( name = 'Row' parent = r_table ).
* Column Headers Row
r_row = l_document->create_simple_element( name = 'Row' parent = r_table ).
r_row->set_attribute_ns( name = 'AutoFitHeight' prefix = 'ss' value = '1' ).
* Sr. No.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Sr. No.' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Material No.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Material Number' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Material Description
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Material Description' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Material Type
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Material Type' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Material Group
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Material Group' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Plant
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Plant' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Storage Location
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Storage Location' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Unrestricted Quantity
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Unrestricted Quantity' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Unrestricted Quantity Value
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Unrestricted Quantity Value' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Blank Row after Column Headers
r_row = l_document->create_simple_element( name = 'Row' parent = r_table ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
* Data Table
LOOP AT it_final INTO wa_final.
r_row = l_document->create_simple_element( name = 'Row' parent = r_table ).
* Sr. No.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = sy-tabix.
CONDENSE l_value NO-GAPS.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'Number' ). " Cell format
* Material No.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-matnr.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Material Description
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-maktx.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Material Type
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-mtart.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Material Group
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-matkl.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Plant
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-werks.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Plant
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-lgort.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Unrest. Qty
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-labst.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Unrest. Qty Value
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-value.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
ENDLOOP.
* Creating a Stream Factory
l_streamfactory = l_ixml->create_stream_factory( ).
* Connect Internal XML Table to Stream Factory
l_ostream = l_streamfactory->create_ostream_itable( table = l_xml_table ).
* Rendering the Document
l_renderer = l_ixml->create_renderer( ostream = l_ostream document = l_document ).
l_rc = l_renderer->render( ).
* Saving the XML Document
l_xml_size = l_ostream->get_num_written_raw( ).
ENDFORM. " PROCESS_XML_DATA
*&---------------------------------------------------------------------*
*& Form MAIL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM mail .
lv_date = sy-datum.
* Mail Subject
doc_chng-obj_name = 'Unused_material_for_1_year'.
CONCATENATE lv_date+6(2) '-' lv_date+4(2) '-' lv_date+0(4) INTO subj_date.
CONCATENATE 'List of unused materials since 1 year,' subj_date INTO doc_chng-obj_descr SEPARATED BY space.
IF it_final IS NOT INITIAL..
"Body of the mail
PERFORM build_body_of_mail
USING:space,
'Dear User',
'',
'Kindly find the list of unused materials.',
'',
'Regards,',
'itsapaber'.
ELSE.
PERFORM build_body_of_mail
USING:space,
'Dear User',
'',
'There is no unused materials for past 1 year.',
'',
'Regards,',
'itsapaber'.
ENDIF.
DESCRIBE TABLE objtxt LINES tab_lines.
READ TABLE objtxt INDEX tab_lines.
doc_chng-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objtxt ).
* Packing List For the E-mail Body
objpack-head_start = 1.
objpack-head_num = 0.
objpack-body_start = 1.
objpack-body_num = tab_lines.
objpack-doc_type = 'RAW'.
APPEND objpack.
* Creation of the Document Attachment
LOOP AT l_xml_table INTO wa_xml.
CLEAR objbin.
objbin-line = wa_xml-data.
APPEND objbin.
ENDLOOP.
DESCRIBE TABLE objbin LINES tab_lines.
objhead = 'Unused Material List'.
APPEND objhead.
IF it_final IS NOT INITIAL.
* Packing List For the E-mail Attachment
objpack-transf_bin = 'X'.
objpack-head_start = 1.
objpack-head_num = 0.
objpack-body_start = 1.
objpack-body_num = tab_lines.
CONCATENATE 'Unused_material_' subj_date INTO objpack-obj_descr SEPARATED BY space.
objpack-doc_type = 'XLS'.
objpack-doc_size = tab_lines * 255.
APPEND objpack.
ENDIF.
* Target Recipent
"Receivers List.
LOOP AT it_mail INTO wa_mail.
IF wa_mail-to_mailid IS NOT INITIAL.
TRANSLATE wa_mail-to_mailid TO LOWER CASE.
reclist-rec_type = 'U'. "Internet address
reclist-receiver = wa_mail-to_mailid.
reclist-com_type = 'INT'.
reclist-notif_del = 'X'.
reclist-notif_ndel = 'X'.
APPEND reclist.
ENDIF.
IF wa_mail-cc_mailid IS NOT INITIAL.
TRANSLATE wa_mail-cc_mailid TO LOWER CASE.
reclist-rec_type = 'U'. "Internet address
reclist-receiver = wa_mail-cc_mailid.
reclist-com_type = 'INT'.
reclist-notif_del = 'X'.
reclist-notif_ndel = 'X'.
reclist-copy = 'X'. "CC Mail id
APPEND reclist.
ENDIF.
CLEAR : wa_mail.
ENDLOOP.
* Sending the document
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = doc_chng
put_in_outbox = 'X'
TABLES
packing_list = objpack
object_header = objhead
contents_txt = objtxt
contents_hex = objbin
receivers = reclist
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
operation_no_authorization = 4
OTHERS = 99.
IF sy-subrc = 0 .
* MESSAGE i303(me) WITH 'Mail has been Successfully Sent.'.
ELSE.
WAIT UP TO 2 SECONDS.
"This program starts the SAPconnect send process.
SUBMIT rsconn01 WITH mode = 'INT'
WITH output = 'X'
AND RETURN.
ENDIF.
ENDFORM. " MAIL
*&---------------------------------------------------------------------*
*& Form BUILD_BODY_OF_MAIL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_SPACE text
*----------------------------------------------------------------------*
FORM build_body_of_mail USING l_message.
objtxt = l_message.
APPEND objtxt.
CLEAR objtxt.
ENDFORM. " BUILD_BODY_OF_MAIL
Output.
*& Report ZVR_WM_MAIL_4_UNUSED_MATERIAL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zvr_wm_mail_4_unused_material.
TYPES : BEGIN OF ty_s032,
werks TYPE werks_d,
lgort TYPE lgort_d,
matnr TYPE matnr,
mtart TYPE mtart,
matkl TYPE matkl,
letztver TYPE letztver,
END OF ty_s032.
TYPES : BEGIN OF ty_makt,
matnr TYPE matnr,
spras TYPE spras,
maktx TYPE maktx,
END OF ty_makt.
TYPES : BEGIN OF ty_mard,
matnr TYPE matnr,
werks TYPE werks_d,
lgort TYPE lgort_d,
labst TYPE labst,
END OF ty_mard.
TYPES : BEGIN OF ty_mbew,
matnr TYPE matnr,
vprsv TYPE vprsv,
verpr TYPE verpr,
stprs TYPE stprs,
END OF ty_mbew.
TYPES : BEGIN OF ty_final,
matnr TYPE matnr,
maktx TYPE maktx,
mtart TYPE mtart,
matkl TYPE matkl,
werks TYPE werks_d,
lgort TYPE lgort_d,
labst TYPE labst,
value TYPE char13,"verpr,
END OF ty_final.
TYPES: BEGIN OF xml_line,
data(255) TYPE x,
END OF xml_line.
DATA : it_s032 TYPE STANDARD TABLE OF ty_s032,
wa_s032 TYPE ty_s032,
it_makt TYPE STANDARD TABLE OF ty_makt,
wa_makt TYPE ty_makt,
it_mard TYPE STANDARD TABLE OF ty_mard,
wa_mard TYPE ty_mard,
it_mbew TYPE STANDARD TABLE OF ty_mbew,
wa_mbew TYPE ty_mbew,
it_final TYPE STANDARD TABLE OF ty_final,
wa_final TYPE ty_final.
DATA : it_mail TYPE STANDARD TABLE OF zmail,
wa_mail TYPE zmail.
DATA : objpack LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE.
DATA : objhead LIKE solisti1 OCCURS 1 WITH HEADER LINE.
DATA : objbin LIKE solix OCCURS 10 WITH HEADER LINE.
DATA : objtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE.
DATA : reclist LIKE somlreci1 OCCURS 5 WITH HEADER LINE.
DATA : doc_chng LIKE sodocchgi1.
DATA : tab_lines LIKE sy-tabix.
DATA : l_num(3).
DATA : subj_date(10) TYPE c.
DATA : lv_date TYPE char10.
DATA : l_ixml TYPE REF TO if_ixml,
l_streamfactory TYPE REF TO if_ixml_stream_factory,
l_ostream TYPE REF TO if_ixml_ostream,
l_renderer TYPE REF TO if_ixml_renderer,
l_document TYPE REF TO if_ixml_document.
DATA : l_element_root TYPE REF TO if_ixml_element,
ns_attribute TYPE REF TO if_ixml_attribute,
r_element_properties TYPE REF TO if_ixml_element,
r_element TYPE REF TO if_ixml_element,
r_worksheet TYPE REF TO if_ixml_element,
r_table TYPE REF TO if_ixml_element,
r_column TYPE REF TO if_ixml_element,
r_row TYPE REF TO if_ixml_element,
r_cell TYPE REF TO if_ixml_element,
r_data TYPE REF TO if_ixml_element,
l_value TYPE string,
l_type TYPE string,
l_text(100) TYPE c,
r_styles TYPE REF TO if_ixml_element,
r_style TYPE REF TO if_ixml_element,
r_style1 TYPE REF TO if_ixml_element,
r_format TYPE REF TO if_ixml_element,
r_border TYPE REF TO if_ixml_element,
num_rows TYPE i.
DATA : g_sent_to_all TYPE sonv-flag,
g_tab_lines TYPE i,
lv_labst TYPE char17,
lv_value TYPE char13.
DATA : l_xml_table TYPE TABLE OF xml_line,
wa_xml TYPE xml_line,
l_xml_size TYPE i,
l_rc TYPE i.
PARAMETERS : date TYPE letztver DEFAULT sy-datum.
PERFORM get_data.
PERFORM process_xml_data.
PERFORM mail.
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_data .
CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
EXPORTING
date = date
days = '00'
months = '00'
signum = '-'
years = '01'
IMPORTING
calc_date = date.
SELECT werks
lgort
matnr
mtart
matkl
letztver
FROM s032
INTO TABLE it_s032
WHERE letztver LE date.
IF it_s032 IS NOT INITIAL.
SELECT matnr
spras
maktx
FROM makt
INTO TABLE it_makt
FOR ALL ENTRIES IN it_s032
WHERE spras = sy-langu AND matnr = it_s032-matnr.
SELECT matnr
werks
lgort
labst
FROM mard
INTO TABLE it_mard
FOR ALL ENTRIES IN it_s032
WHERE matnr = it_s032-matnr AND werks = it_s032-werks AND lgort = it_s032-lgort.
SELECT matnr
vprsv
verpr
stprs
FROM mbew
INTO TABLE it_mbew
FOR ALL ENTRIES IN it_s032
WHERE matnr = it_s032-matnr.
ENDIF.
SELECT *
FROM zmail
INTO TABLE it_mail.
LOOP AT it_s032 INTO wa_s032.
IF wa_s032-lgort IS NOT INITIAL.
wa_final-matnr = wa_s032-matnr.
READ TABLE it_makt INTO wa_makt WITH KEY matnr = wa_final-matnr.
IF sy-subrc = 0.
wa_final-maktx = wa_makt-maktx.
ENDIF.
wa_final-mtart = wa_s032-mtart.
wa_final-matkl = wa_s032-matkl.
wa_final-werks = wa_s032-werks.
wa_final-lgort = wa_s032-lgort.
READ TABLE it_mard INTO wa_mard WITH KEY matnr = wa_s032-matnr werks = wa_s032-werks lgort = wa_s032-lgort.
IF sy-subrc = 0.
wa_final-labst = wa_mard-labst.
IF wa_final-labst IS INITIAL.
CLEAR : wa_final, wa_makt, wa_mard, wa_s032.
CONTINUE.
ENDIF.
ENDIF.
READ TABLE it_mbew INTO wa_mbew WITH KEY matnr = wa_s032-matnr.
IF sy-subrc = 0.
IF wa_mbew-vprsv = 'S'.
wa_final-value = wa_final-labst * wa_mbew-stprs.
ELSEIF wa_mbew-vprsv = 'V'.
wa_final-value = wa_final-labst * wa_mbew-verpr.
ENDIF.
ENDIF.
APPEND wa_final TO it_final.
ENDIF.
CLEAR : wa_final, wa_makt, wa_s032.
ENDLOOP.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form PROCESS_XML_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM process_xml_data .
* Creating a ixml Factory
l_ixml = cl_ixml=>create( ).
* Creating the DOM Object Model
l_document = l_ixml->create_document( ).
* Create Root Node 'Workbook'
l_element_root = l_document->create_simple_element( name = 'Workbook' parent = l_document ).
l_element_root->set_attribute( name = 'xmlns' value = 'urn:schemas-microsoft-com:office:spreadsheet' ).
ns_attribute = l_document->create_namespace_decl( name = 'ss' prefix = 'xmlns' uri = 'urn:schemas-microsoft-com:office:spreadsheet' ).
l_element_root->set_attribute_node( ns_attribute ).
ns_attribute = l_document->create_namespace_decl( name = 'x' prefix = 'xmlns' uri = 'urn:schemas-microsoft-com:office:excel' ).
l_element_root->set_attribute_node( ns_attribute ).
* Create node for document properties.
r_element_properties = l_document->create_simple_element( name = 'MATERIAL_REPORT' parent = l_element_root ).
l_value = sy-uname.
l_document->create_simple_element( name = 'Author' value = l_value parent = r_element_properties ).
* Styles
r_styles = l_document->create_simple_element( name = 'Styles' parent = l_element_root ).
* Style for Header
r_style = l_document->create_simple_element( name = 'Style' parent = r_styles ).
r_style->set_attribute_ns( name = 'ID' prefix = 'ss' value = 'Header' ).
r_format = l_document->create_simple_element( name = 'Font' parent = r_style ).
r_format->set_attribute_ns( name = 'Bold' prefix = 'ss' value = '1' ).
r_format = l_document->create_simple_element( name = 'Interior' parent = r_style ).
r_format->set_attribute_ns( name = 'Color' prefix = 'ss' value = '#92D050' ).
r_format->set_attribute_ns( name = 'Pattern' prefix = 'ss' value = 'Solid' ).
r_format = l_document->create_simple_element( name = 'Alignment' parent = r_style ).
r_format->set_attribute_ns( name = 'Vertical' prefix = 'ss' value = 'Center' ).
r_format->set_attribute_ns( name = 'WrapText' prefix = 'ss' value = '1' ).
r_border = l_document->create_simple_element( name = 'Borders' parent = r_style ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Bottom' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Left' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Top' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Right' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
* Style for Data
r_style1 = l_document->create_simple_element( name = 'Style' parent = r_styles ).
r_style1->set_attribute_ns( name = 'ID' prefix = 'ss' value = 'Data' ).
r_border = l_document->create_simple_element( name = 'Borders' parent = r_style1 ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Bottom' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Left' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Top' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
r_format = l_document->create_simple_element( name = 'Border' parent = r_border ).
r_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Right' ).
r_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).
* Worksheet
r_worksheet = l_document->create_simple_element( name = 'Worksheet' parent = l_element_root ).
r_worksheet->set_attribute_ns( name = 'Name' prefix = 'ss' value = 'Sheet1' ).
* Table
r_table = l_document->create_simple_element( name = 'Table' parent = r_worksheet ).
r_table->set_attribute_ns( name = 'FullColumns' prefix = 'x' value = '1' ).
r_table->set_attribute_ns( name = 'FullRows' prefix = 'x' value = '1' ).
* Column Formatting
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '40' ).
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '90' ).
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '140' ).
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '150' ).
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '90' ).
* Blank Row
r_row = l_document->create_simple_element( name = 'Row' parent = r_table ).
* Column Headers Row
r_row = l_document->create_simple_element( name = 'Row' parent = r_table ).
r_row->set_attribute_ns( name = 'AutoFitHeight' prefix = 'ss' value = '1' ).
* Sr. No.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Sr. No.' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Material No.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Material Number' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Material Description
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Material Description' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Material Type
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Material Type' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Material Group
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Material Group' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Plant
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Plant' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Storage Location
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Storage Location' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Unrestricted Quantity
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Unrestricted Quantity' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Unrestricted Quantity Value
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data' value = 'Unrestricted Quantity Value' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Blank Row after Column Headers
r_row = l_document->create_simple_element( name = 'Row' parent = r_table ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
* Data Table
LOOP AT it_final INTO wa_final.
r_row = l_document->create_simple_element( name = 'Row' parent = r_table ).
* Sr. No.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = sy-tabix.
CONDENSE l_value NO-GAPS.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'Number' ). " Cell format
* Material No.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-matnr.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Material Description
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-maktx.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Material Type
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-mtart.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Material Group
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-matkl.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Plant
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-werks.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Plant
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-lgort.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Unrest. Qty
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-labst.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Unrest. Qty Value
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final-value.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
ENDLOOP.
* Creating a Stream Factory
l_streamfactory = l_ixml->create_stream_factory( ).
* Connect Internal XML Table to Stream Factory
l_ostream = l_streamfactory->create_ostream_itable( table = l_xml_table ).
* Rendering the Document
l_renderer = l_ixml->create_renderer( ostream = l_ostream document = l_document ).
l_rc = l_renderer->render( ).
* Saving the XML Document
l_xml_size = l_ostream->get_num_written_raw( ).
ENDFORM. " PROCESS_XML_DATA
*&---------------------------------------------------------------------*
*& Form MAIL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM mail .
lv_date = sy-datum.
* Mail Subject
doc_chng-obj_name = 'Unused_material_for_1_year'.
CONCATENATE lv_date+6(2) '-' lv_date+4(2) '-' lv_date+0(4) INTO subj_date.
CONCATENATE 'List of unused materials since 1 year,' subj_date INTO doc_chng-obj_descr SEPARATED BY space.
IF it_final IS NOT INITIAL..
"Body of the mail
PERFORM build_body_of_mail
USING:space,
'Dear User',
'',
'Kindly find the list of unused materials.',
'',
'Regards,',
'itsapaber'.
ELSE.
PERFORM build_body_of_mail
USING:space,
'Dear User',
'',
'There is no unused materials for past 1 year.',
'',
'Regards,',
'itsapaber'.
ENDIF.
DESCRIBE TABLE objtxt LINES tab_lines.
READ TABLE objtxt INDEX tab_lines.
doc_chng-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objtxt ).
* Packing List For the E-mail Body
objpack-head_start = 1.
objpack-head_num = 0.
objpack-body_start = 1.
objpack-body_num = tab_lines.
objpack-doc_type = 'RAW'.
APPEND objpack.
* Creation of the Document Attachment
LOOP AT l_xml_table INTO wa_xml.
CLEAR objbin.
objbin-line = wa_xml-data.
APPEND objbin.
ENDLOOP.
DESCRIBE TABLE objbin LINES tab_lines.
objhead = 'Unused Material List'.
APPEND objhead.
IF it_final IS NOT INITIAL.
* Packing List For the E-mail Attachment
objpack-transf_bin = 'X'.
objpack-head_start = 1.
objpack-head_num = 0.
objpack-body_start = 1.
objpack-body_num = tab_lines.
CONCATENATE 'Unused_material_' subj_date INTO objpack-obj_descr SEPARATED BY space.
objpack-doc_type = 'XLS'.
objpack-doc_size = tab_lines * 255.
APPEND objpack.
ENDIF.
* Target Recipent
"Receivers List.
LOOP AT it_mail INTO wa_mail.
IF wa_mail-to_mailid IS NOT INITIAL.
TRANSLATE wa_mail-to_mailid TO LOWER CASE.
reclist-rec_type = 'U'. "Internet address
reclist-receiver = wa_mail-to_mailid.
reclist-com_type = 'INT'.
reclist-notif_del = 'X'.
reclist-notif_ndel = 'X'.
APPEND reclist.
ENDIF.
IF wa_mail-cc_mailid IS NOT INITIAL.
TRANSLATE wa_mail-cc_mailid TO LOWER CASE.
reclist-rec_type = 'U'. "Internet address
reclist-receiver = wa_mail-cc_mailid.
reclist-com_type = 'INT'.
reclist-notif_del = 'X'.
reclist-notif_ndel = 'X'.
reclist-copy = 'X'. "CC Mail id
APPEND reclist.
ENDIF.
CLEAR : wa_mail.
ENDLOOP.
* Sending the document
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = doc_chng
put_in_outbox = 'X'
TABLES
packing_list = objpack
object_header = objhead
contents_txt = objtxt
contents_hex = objbin
receivers = reclist
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
operation_no_authorization = 4
OTHERS = 99.
IF sy-subrc = 0 .
* MESSAGE i303(me) WITH 'Mail has been Successfully Sent.'.
ELSE.
WAIT UP TO 2 SECONDS.
"This program starts the SAPconnect send process.
SUBMIT rsconn01 WITH mode = 'INT'
WITH output = 'X'
AND RETURN.
ENDIF.
ENDFORM. " MAIL
*&---------------------------------------------------------------------*
*& Form BUILD_BODY_OF_MAIL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_SPACE text
*----------------------------------------------------------------------*
FORM build_body_of_mail USING l_message.
objtxt = l_message.
APPEND objtxt.
CLEAR objtxt.
ENDFORM. " BUILD_BODY_OF_MAIL
Output.
2 comments
commentsDear Vimal,
ReplyVery good and helpful blog.
I tried your sample code and it did work except 2 thing; First one is to freeze first row of excel table and the second one is to separate amount by thousand separator in cells. Have you experienced before such things, if you offer some methods I would be greateful.
Thanks and regarda,
Hii I tried your code ,but I am getting some numbers in XML table instead of data
Reply