Thursday, April 7, 2011

Upload program to create Measuring Document

This is an upload program to create Measuring document in SAP PM module. This program uploads the data file in excel format from the presentation server.
Call transaction method is used create Measuring document.
Error log file is downloaded at the file location.
Entire program is given as below.


REPORT zpmcipr0003
NO STANDARD PAGE HEADING LINE-SIZE 1023.
TYPE-POOLS: truxs.
TABLES: t100.
************************************************************************
* Struture Declaration *
************************************************************************
TYPES: BEGIN OF ty_data,
point LIKE imrg-point, "Measuring Point
date_time(17) TYPE c, "Date and Time
readc LIKE rimr0-readc, "Measurement Reading
vlcod LIKE imrg-vlcod, "Valuation Code
mdtxt LIKE imrg-mdtxt, "Measurement Document Text
uname LIKE sy-uname,
prest LIKE imrg-prest,
TIDNR LIKE EQUZ-TIDNR, "Tech Identification numebr
PTTXT LIKE IMPTT-PTTXT, "Mea Point Description
END OF ty_data.

TYPES: BEGIN OF ty_log,
text TYPE char255,
END OF ty_log.

TYPES: BEGIN OF ty_imptt,
point LIKE imptt-point,
indct LIKE imptt-indct,
mptyp LIKE imptt-mptyp,
END OF ty_imptt.
************************************************************************
* Intrnal Table Declaration *
************************************************************************
DATA: it_data TYPE STANDARD TABLE OF ty_data,
bdcdata TYPE STANDARD TABLE OF bdcdata WITH HEADER LINE,
it_raw TYPE truxs_t_text_data,
wa_data TYPE ty_data,
l_ascii_list(200) TYPE c OCCURS 0 WITH HEADER LINE,
l_abap_list TYPE abaplist OCCURS 0 WITH HEADER LINE,
it_log TYPE STANDARD TABLE OF ty_log,
wa_log TYPE ty_log,
wa_t100 TYPE t100,
it_imptt TYPE STANDARD TABLE OF ty_imptt,
wa_imptt TYPE ty_imptt.

DATA: BEGIN OF t_error OCCURS 100,
row(10), "row number
point(13), "Measuring point
date_time(17), "Measuring date and time
text(80),
END OF t_error.
************************************************************************
* Data Declaration *
************************************************************************
DATA: ctumode LIKE ctu_params-dismode,
g_count_confirmed TYPE i,
g_count_in_error TYPE i,
error_text(200) TYPE c,
l_line TYPE i,
g_flag TYPE c,
l_equnr TYPE equi-equnr,
l_objnr TYPE equi-objnr,
l_point TYPE imptt-point.
************************************************************************
* Constants Declaration *
************************************************************************
CONSTANTS: c_bdcname LIKE apqi-groupid VALUE 'IK11_UPLOAD',
c_tcode LIKE tstc-tcode VALUE 'IK11'.

************************************************************************
* Selection Screen *
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS : p_file LIKE rlgrap-filename OBLIGATORY .
SELECTION-SCREEN END OF BLOCK b1.
************************************************************************
* At Selection Screen *
************************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name = ' '
IMPORTING
file_name = p_file.

************************************************************************
* Start of Selection *
************************************************************************
START-OF-SELECTION.
*Read the data from the file to internl table.
PERFORM read_data.
*Create Measuring Doc
PERFORM create_measuring_doc.
*Download Error log file
PERFORM download_error_log.
*&---------------------------------------------------------------------*
*& Form READ_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM read_data .
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
* I_FIELD_SEPERATOR =
i_line_header = 'X'
i_tab_raw_data = it_raw
i_filename = p_file
TABLES
i_tab_converted_data = it_data[]
* EXCEPTIONS
* CONVERSION_FAILED = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
RAISE upload_problem.
ENDIF.
ENDFORM. " READ_DATA
*&---------------------------------------------------------------------*
*& Form CREATE_MEASURING_DOC
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM create_measuring_doc .

DATA: l_indct LIKE imptt-indct,
l_mode TYPE c VALUE 'N',
l_smode TYPE c VALUE 'S',
l_mptyp TYPE imptt-mptyp,
l_index TYPE sy-tabix.

LOOP AT it_data INTO wa_data.
l_index = sy-tabix.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = wa_data-point
IMPORTING
output = wa_data-point.
IF wa_data-point = ''.

select SINGLE a~EQUNR b~OBJNR
INTO (l_equnr, l_objnr )
FROM EQUZ AS a
INNER JOIN EQUI AS b ON a~EQUNR = b~EQUNR
WHERE a~TIDNR = wa_data-TIDNR
AND a~DATBI = '99991231'
AND ( b~EQTYP = '5' or b~EQTYP = '6' or b~EQTYP = '7') .

* SELECT SINGLE OBJNR into l_objnr FROM EQUI WHERE EQUNR = l_equnr.
SELECT SINGLE POINT
INTO l_point
FROM IMPTT
WHERE MPOBJ = l_objnr
AND PTTXT = wa_data-pttxt
* AND ( mptyp = '1' OR
* mptyp = '2' OR
* mptyp = '3' )
.
wa_data-point = l_point.
ENDIF.
MODIFY it_data FROM wa_data INDEX l_index.
CLEAR: l_index,wa_data.
ENDLOOP.
*Get data from table imptt
SELECT point indct mptyp FROM imptt
INTO TABLE it_imptt
FOR ALL ENTRIES IN it_data
WHERE point = it_data-point
AND ( mptyp = '1' OR
mptyp = '2' OR
mptyp = '3' ).

LOOP AT it_data INTO wa_data.
l_index = sy-tabix.
CLEAR wa_imptt.
READ TABLE it_imptt INTO wa_imptt
WITH KEY point = wa_data-point.
IF sy-subrc NE 0.
*Write Heading
IF g_flag IS INITIAL.
WRITE:
/(10) 'Row',
(16) 'Measuring Point',
(26) 'Measuring Date & Time',
(80) 'Error Text'.
* (20) 'Equipment Number',
* (30) 'Tech. Identification ID',
* (40) 'Measurement Point Desc'.
SKIP.
*Write heading in log file.
t_error-row = 'Row number'.
t_error-point = 'Measuring Pnt'.
t_error-date_time = 'Measrng Date_Time'.
t_error-text = 'Error Text'.
APPEND t_error.
CLEAR t_error.
APPEND t_error.
g_flag = 'X'.
ENDIF.
ADD 1 TO g_count_in_error.
error_text = 'Measuring Point catagory is not 1,2 or 3'.
CONDENSE error_text.
WRITE:
/(10) l_index LEFT-JUSTIFIED,
(16) wa_data-point,
(26) wa_data-date_time,
(80) error_text,
(20) l_equnr,
(30) wa_data-TIDNR,
(40) wa_data-PTTXT.
SKIP.
MOVE-CORRESPONDING wa_data TO t_error.
MOVE error_text TO t_error-text.
MOVE l_index TO t_error-row.
APPEND t_error.
CLEAR t_error.
* APPEND t_error.
CONTINUE.
ENDIF.

PERFORM bdc_dynpro USING 'SAPLIMR0' '1210'.
PERFORM bdc_field USING 'BDC_CURSOR'
'IMRG-POINT'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
PERFORM bdc_field USING 'IMRG-POINT'
wa_data-point. "'141164'.
PERFORM bdc_field USING 'RIMR0-DFTIM'
wa_data-date_time+9(8)."'19:47:35'.
PERFORM bdc_field USING 'RIMR0-DFDAT'
wa_data-date_time+0(8). "'22.09.2010'.
PERFORM bdc_field USING 'RIMR0-DFRDR'
sy-uname. "'ASC6898'.
PERFORM bdc_dynpro USING 'SAPLIMR0' '5210'.
PERFORM bdc_field USING 'BDC_CURSOR'
'IMRG-MDTXT'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=BU'.
PERFORM bdc_field USING 'IMRG-ITIME'
wa_data-date_time+9(8)."'19:47:35'.
PERFORM bdc_field USING 'IMRG-IDATE'
wa_data-date_time+0(8). "'22.09.2010'.

l_indct = wa_imptt-indct.

IF l_indct IS INITIAL.
PERFORM bdc_field USING 'RIMR0-READC'
wa_data-readc. "'20'.
ELSE.
PERFORM bdc_field USING 'RIMR0-CNTRC'
wa_data-readc. "'40'.
* perform bdc_field using 'RIMR0-CDIFC'
* '+______________.__'.
ENDIF.
PERFORM bdc_field USING 'IMRG-VLCOD'
wa_data-vlcod. "'V001'.
PERFORM bdc_field USING 'IMRG-MDTXT'
wa_data-mdtxt. "'Test text'.
PERFORM bdc_field USING 'IMRG-READR'
wa_data-uname. "'ASC6898'.
PERFORM bdc_field USING 'IMRG-READR'
wa_data-uname. "'ASC6898'.
PERFORM bdc_field USING 'IMRG-PREST'
wa_data-prest.
CALL TRANSACTION 'IK11' USING bdcdata
MODE l_mode
UPDATE l_smode.
IF sy-subrc EQ 0.
ADD 1 TO g_count_confirmed.
ELSE.
*Write Heading
IF g_flag IS INITIAL.
WRITE:
/(16) 'Measuring Point',
(26) 'Measuring Date & Time',
(80) 'Error Text'.
SKIP.
*Write heading in log file
t_error-point = 'Measuring Pnt'.
t_error-date_time = 'Measrng Date_Time'.
t_error-text = 'Error Text'.
APPEND t_error.
CLEAR t_error.
APPEND t_error.
g_flag = 'X'.
ENDIF.

ADD 1 TO g_count_in_error.
SELECT SINGLE * FROM t100 INTO wa_t100
WHERE sprsl = sy-langu
AND arbgb = sy-msgid
AND msgnr = sy-msgno.
MOVE wa_t100-text TO error_text.
IF error_text CS '&1'.
REPLACE '&1' WITH sy-msgv1 INTO error_text.
ELSE.
REPLACE '&' WITH sy-msgv1 INTO error_text.
ENDIF.
IF error_text CS '&2'.
REPLACE '&2' WITH sy-msgv2 INTO error_text.
ELSE.
REPLACE '&' WITH sy-msgv2 INTO error_text.
ENDIF.
IF error_text CS '&3'.
REPLACE '&3' WITH sy-msgv3 INTO error_text.
ELSE.
REPLACE '&' WITH sy-msgv3 INTO error_text.
ENDIF.
IF error_text CS '&4'.
REPLACE '&4' WITH sy-msgv4 INTO error_text.
ELSE.
REPLACE '&' WITH sy-msgv4 INTO error_text.
ENDIF.
CONDENSE error_text.

WRITE:
/(10) sy-tabix,
(16) wa_data-point,
(26) wa_data-date_time,
(25) wa_data-TIDNR,
(40) wa_data-PTTXT,
(80) error_text.
SKIP.
MOVE-CORRESPONDING wa_data TO t_error.
MOVE error_text TO t_error-text.
APPEND t_error.
CLEAR t_error.
APPEND t_error.
ENDIF.
REFRESH: bdcdata.
CLEAR : wa_data,l_indct.
ENDLOOP.
ENDFORM. " CREATE_MEASURING_DOC
*----------------------------------------------------------------------*
* Start new screen *
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
CLEAR bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
APPEND bdcdata.
ENDFORM. "bdc_dynpro

*----------------------------------------------------------------------*
* Insert field *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
CLEAR bdcdata.
bdcdata-fnam = fnam.
IF fnam CS 'MENGE'.
bdcdata-fval = fval.
CONDENSE bdcdata-fval.
ELSE.
bdcdata-fval = fval.
ENDIF.
APPEND bdcdata.
ENDFORM. "bdc_field
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_ERROR_LOG
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM download_error_log .
DATA: l_dsn LIKE rlgrap-filename,
l_filelen TYPE i,
gui_filename TYPE string,
l_path TYPE string.
* Prepare the error log file location and file name
l_filelen = STRLEN( p_file ).
l_filelen = l_filelen - 3.
CONCATENATE p_file(l_filelen) 'ERR.txt' INTO l_dsn.

DESCRIBE TABLE t_error LINES l_line.
IF l_line = 0.
t_error-text = 'Error not found. Upload process completed!'.
APPEND t_error.
ENDIF.

gui_filename = l_dsn.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
* BIN_FILESIZE =
filename = gui_filename
filetype = 'ASC'
* APPEND = ' '
write_field_separator = 'X'
* HEADER = '00'
* TRUNC_TRAILING_BLANKS = ' '
* WRITE_LF = 'X'
* COL_SELECT = ' '
* COL_SELECT_MASK = ' '
* DAT_MODE = ' '
* CONFIRM_OVERWRITE = ' '
* NO_AUTH_CHECK = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* WRITE_BOM = ' '
* TRUNC_TRAILING_BLANKS_EOL = 'X'
* WK1_N_FORMAT = ' '
* WK1_N_SIZE = ' '
* WK1_T_FORMAT = ' '
* WK1_T_SIZE = ' '
* WRITE_LF_AFTER_LAST_LINE = ABAP_TRUE
* SHOW_TRANSFER_STATUS = ABAP_TRUE
* IMPORTING
* FILELENGTH =
TABLES
data_tab = t_error
* FIELDNAMES =
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
OTHERS = 22
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSE.
SKIP.

IF l_line = 0.
WRITE:/ 'Error not found. Upload process completed!'.
ELSE.
CONCATENATE 'Error file downloaded at' l_dsn INTO l_path
SEPARATED BY space.
WRITE:/ l_path.
ENDIF.
ENDIF.

ENDFORM. " DOWNLOAD_ERROR_LOG

No comments:

Post a Comment