07-07-2006 8:43 AM
hi all,
I am new in BAPI .Plz tell me How to create PO using BAPI in ABAP.Plz send me ABAP code or Mail me on my id hem_libra@rediffmail.com.I am using external range for Purchase order.I hav to enter old purchase order.
Thanx in advance
Regards,
Hemlata
07-07-2006 8:48 AM
07-07-2006 8:48 AM
Hello,
U can use the BAPI_PO_CREATE.
CHeck the where used list for this BAPI. And do the same thing in ur report also.
Don't points.
Rgerads,
Vasanth
07-07-2006 10:32 AM
Hi Hema,
here is some sample code to help you get started using the BAPI. Of course, you will have to substitute valid values for a couple variables.
constants : c_x value 'X'.
data: del_date type sy-datum.
data: pohead type bapimepoheader.
data: poheadx type bapimepoheaderx.
data: exp_head type bapimepoheader.
data: return type table of bapiret2 with header line.
data: poitem type table of bapimepoitem with header line.
data: poitemx type table of bapimepoitemx with header line.
data: posched type table of bapimeposchedule with header line.
data: poschedx type table of bapimeposchedulx with header line.
data: ex_po_number type bapimepoheader-po_number.
Header Level Data
pohead-comp_code = '0010'.
pohead-doc_type = 'NB' .
pohead-creat_date = sy-datum .
pohead-vendor = '0000012345'.
pohead-purch_org = '1000'.
pohead-pur_group = 'EMG'.
pohead-langu = sy-langu.
pohead-doc_date = sy-datum.
poheadx-comp_code = c_x.
poheadx-doc_type = c_x.
poheadx-creat_date = c_x.
poheadx-vendor = c_x.
poheadx-langu = c_x.
poheadx-purch_org = c_x.
poheadx-pur_group = c_x.
poheadx-doc_date = c_x.
Item Level Data
poitem-po_item = 1.
poitem-material = '000000000040001000'.
poitem-plant = '0006'.
poitem-stge_loc = '6000'.
poitem-quantity = 1.
append poitem.
poitemx-po_item = 1.
poitemx-po_itemx = c_x.
poitemx-material = c_x.
poitemx-plant = c_x .
poitemx-stge_loc = c_x .
poitemx-quantity = c_x .
poitemx-tax_code = c_x .
poitemx-item_cat = c_x .
poitemx-acctasscat = c_x .
append poitemx.
Schedule Line Level Data
posched-po_item = 1.
posched-sched_line = 1.
posched-del_datcat_ext = 'D'.
del_date = sy-datum + 1.
write del_date to posched-delivery_date.
posched-deliv_time = '000001'.
posched-quantity = 1.
append posched.
poschedx-po_item = 1.
poschedx-sched_line = 1.
poschedx-po_itemx = c_x.
poschedx-sched_linex = c_x.
poschedx-del_datcat_ext = c_x.
poschedx-delivery_date = c_x.
poschedx-quantity = c_x.
append poschedx.
call function 'BAPI_PO_CREATE1'
EXPORTING
poheader = pohead
poheaderx = poheadx
testrun = ' '
IMPORTING
exppurchaseorder = ex_po_number
expheader = exp_head
TABLES
return = return
poitem = poitem
poitemx = poitemx
poschedule = posched
poschedulex = poschedx.
call function 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
if not ex_po_number is initial.
call function 'DEQUEUE_ALL'.
else.
call function 'DEQUEUE_ALL'.
message i036.
endif.
Regards,
Shalini
07-07-2006 10:49 AM
u can try BAPI 'BAPI_PO_CREATE1' which is based on transaction ME21N.. refer the code below
REPORT ZMMR_VTLS_UPLOAD NO STANDARD PAGE HEADING
LINE-SIZE 470
MESSAGE-ID zisb.
tables : zvtls_sap.
*C-- Types Declarations
TYPES : BEGIN OF tp_flatfile_vtls,
lifnr type ekko-lifnr,
ebeln type ekko-ebeln,
ebelp type ekpo-ebelp,
text(134),
netpr(13),
menge(16),
matkl type ekpo-matkl,
matkl(15),
END OF tp_flatfile_vtls.
*=====================================================================
INTERNAL TABLES DECLARATION
*=====================================================================
DATA: t_flatfile_vtls TYPE tp_flatfile_vtls OCCURS 0 WITH HEADER LINE.
data : begin of t_po_header occurs 0,
po like ekko-ebeln,
vendor like ekko-lifnr,
end of t_po_header.
DATA : BEGIN OF T_PO_ITEM OCCURS 0,
PO LIKE EKKO-EBELN,
ITEM LIKE EKPO-EBELP,
TEXT(132),
PRICE TYPE P DECIMALS 2,
menge(16),
MATKL LIKE EKPO-MATKL,
KNTTP LIKE EKPO-KNTTP,
ASSET LIKE EKKN-ANLN1,
COST LIKE EKKN-KOSTL,
GL_ACC LIKE EKKN-SAKTO,
end of t_po_item.
DATA : BEGIN OF T_TABLE OCCURS 0,
MANDT LIKE ZVTLS_SAP-MANDT,
POVTLS LIKE ZVTLS_SAP-POVTLS,
ITEMVTLS LIKE ZVTLS_SAP-ITEMVTLS,
POSAP LIKE ZVTLS_SAP-POSAP,
ITEMSAP LIKE ZVTLS_SAP-ITEMSAP,
AEDAT LIKE ZVTLS_SAP-AEDAT,
PAEDT LIKE ZVTLS_SAP-PAEDT,
LOEKZ LIKE ZVTLS_SAP-LOEKZ,
END OF T_TABLE.
DATA : BEGIN OF T_MESSAGE OCCURS 0,
MSGTY,
MSGID(2),
MSGNO(3),
MSGTX(100),
PO LIKE ZVTLS_SAP-POVTLS,
END OF T_MESSAGE.
DATA : BEGIN OF t_bapi_poheader OCCURS 0.
INCLUDE STRUCTURE bapimepoheader.
DATA : END OF t_bapi_poheader.
DATA : BEGIN OF t_bapi_poheaderx OCCURS 0.
INCLUDE STRUCTURE bapimepoheaderx.
DATA : END OF t_bapi_poheaderx.
DATA : BEGIN OF t_bapi_poitem OCCURS 0.
INCLUDE STRUCTURE bapimepoitem.
DATA : END OF t_bapi_poitem.
DATA : BEGIN OF t_bapi_poitemx OCCURS 0.
INCLUDE STRUCTURE bapimepoitemx.
DATA : END OF t_bapi_poitemx.
DATA : BEGIN OF t_bapi_poaccount OCCURS 0.
INCLUDE STRUCTURE bapimepoaccount.
DATA : END OF t_bapi_poaccount.
DATA : BEGIN OF t_bapi_poaccountx OCCURS 0.
INCLUDE STRUCTURE bapimepoaccountx.
DATA : END OF t_bapi_poaccountx.
DATA : BEGIN OF t_bapireturn OCCURS 0.
INCLUDE STRUCTURE bapiret2.
DATA : END OF t_bapireturn.
DATA : BEGIN OF t_bapi_itemtext OCCURS 0.
INCLUDE STRUCTURE bapimepotext.
DATA : END OF t_bapi_itemtext.
*=====================================================================
V A R I A B L E S
*=====================================================================
DATA: w_success(6) TYPE n,
w_bklas like t023-bklas,
w_curryear(4),
w_begda like sy-datum,
w_endda like sy-datum,
w_begyr(4),
w_endyr(4),
w_currmon(2),
w_assetclass like ankt-anlkl,
w_price type p,
w_recordsap type i.
DATA: w_filepath TYPE rlgrap-filename,
w_rc TYPE sy-subrc,
w_sscrfields_ucomm1 TYPE sscrfields-ucomm,
w_file1 TYPE string,
w_file2 TYPE FILENAME-FILEINTERN.
DATA : INDEX TYPE SY-TABIX.
*=====================================================================
C O N S T A N T S
*=====================================================================
CONSTANTS: c_x TYPE c VALUE 'X',
c_hyp TYPE c VALUE '-',
c_err TYPE bdc_mart VALUE 'E',
c_sqno TYPE dzekkn VALUE '00',
c_comp(4) TYPE c VALUE '2000',
c_doctyp(3) TYPE c VALUE 'LRC',
c_purorg(4) TYPE c VALUE '1000',
c_purgrp(3) TYPE c VALUE '001',
c_plant(4) TYPE c VALUE '1000',
c_unit(2) TYPE c VALUE 'EA',
c_a(1) TYPE c VALUE 'A',
c_k(1) TYPE c VALUE 'K',
c_begda(2) TYPE c VALUE '01',
c_endda(2) TYPE c VALUE '31',
c_begmon(2) TYPE c VALUE '04',
c_endmon(2) TYPE c VALUE '03',
c_bapimepoheaderx TYPE x030l-tabname
VALUE 'bapimepoheaderx',
c_bapimepoitem TYPE x030l-tabname
VALUE 'bapimepoitem',
c_bapimepoaccount TYPE x030l-tabname
VALUE 'bapimepoaccount',
c_t_bapi_poheader(15) TYPE c
VALUE 't_bapi_poheader',
c_t_bapi_poitem(13) TYPE c
VALUE 't_bapi_poitem',
c_t_bapi_poitemx(14) TYPE c
VALUE 't_bapi_poitemx',
c_t_bapi_poaccount(16) TYPE c
VALUE 't_bapi_poaccount',
c_t_bapi_poaccountx(17) TYPE c
VALUE 't_bapi_poaccountx',
c_t_bapi_poheaderx(16) TYPE c
VALUE 't_bapi_poheaderx'.
CONSTANTS: c_slash(1) TYPE c VALUE '/',
c_hash(1) TYPE c VALUE '#',
c_pipe TYPE c VALUE '|',
c_1 TYPE i VALUE 1,
c_zero TYPE n VALUE '0',
c_rg1(3) TYPE c VALUE 'rg1',
c_gr3(3) TYPE c VALUE 'GR3',
c_gr2(3) TYPE c VALUE 'GR2',
c_e(1) TYPE c VALUE 'E',
c_filepath(8) TYPE c VALUE '/interf/',
c_filetype(10) TYPE c VALUE 'ASC',
c_id(3) TYPE C VALUE 'F01'.
CLASS cl_abap_char_utilities DEFINITION LOAD.
CONSTANTS:con_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.
*======================================================================
SELECTION SCREEN
*======================================================================
SELECTION-SCREEN BEGIN OF BLOCK inputpath WITH FRAME TITLE text-001.
SELECTION-SCREEN : BEGIN OF BLOCK blk2 WITH FRAME TITLE text-002.
PARAMETERS : p_fore RADIOBUTTON GROUP rg1
USER-COMMAND pc,
p_back RADIOBUTTON GROUP rg1 DEFAULT 'X'.
SELECTION-SCREEN : END OF BLOCK blk2.
SELECTION-SCREEN : BEGIN OF BLOCK blk1 WITH FRAME TITLE text-003.
PARAMETERS : p_file1 LIKE rlgrap-filename OBLIGATORY MODIF ID gr2.
PARAMETERS : p_afile1 LIKE rlgrap-filename OBLIGATORY MODIF ID gr3.
SELECTION-SCREEN : END OF BLOCK blk1.
SELECTION-SCREEN END OF BLOCK inputpath.
*C-- Initialization Event
INITIALIZATION.
CLEAR w_filepath.
CONCATENATE c_filepath sy-sysid c_slash sy-mandt c_slash INTO
w_filepath.
CONDENSE w_filepath NO-GAPS.
p_file1 = text-008.
p_afile1 = text-009.
*======================================================================
SELECTION SCREEN EVENTS
*======================================================================
*C-- Selection Screen Output
AT SELECTION-SCREEN OUTPUT.
IF p_fore = c_x.
w_sscrfields_ucomm1 = space.
ELSE.
w_sscrfields_ucomm1 = c_rg1.
ENDIF.
LOOP AT SCREEN.
*C--Modify selection screen if presentation
*C--or application server radio button is choose
IF w_sscrfields_ucomm1 = space.
IF screen-group1 = c_gr3.
screen-active = c_zero.
ENDIF.
ELSE.
IF screen-group1 = c_gr2.
screen-active = c_zero.
ENDIF.
ENDIF.
if screen-name = 'P_AFILE1'.
screen-input = 0.
ENDIF.
MODIFY SCREEN.
ENDLOOP.
*C-- Selection Screen VALUE-REQUEST FOR File path
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file1.
IF p_fore EQ c_x.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
IMPORTING
file_name = p_file1.
ENDIF.
*C-- At Start of the Selection Process
START-OF-SELECTION.
*C-- Load the contents of the input file into the internal table
IF p_fore EQ c_x.
w_file1 = p_file1.
ELSE.
w_file2 = p_afile1.
ENDIF.
IF p_fore EQ c_x. " Presentaion Server
*C--Validations for the input files
PERFORM validate_pre_file USING p_file1.
*C-- Load the contents of the input file into the internal table
PERFORM upload_file TABLES t_flatfile_vtls
USING w_file1
CHANGING w_rc.
IF w_rc <> 0.
MESSAGE s006 DISPLAY LIKE c_e.
ENDIF.
ELSE. " Application Server
*C--Validations for the input files
PERFORM validate_app_file USING w_file2.
*C-- Load the contents of the input file into the internal table
PERFORM upload_file_app TABLES t_flatfile_vtls
USING w_file2
CHANGING w_rc.
ENDIF.
loop at t_flatfile_vtls.
replace all occurrences of '"' in t_flatfile_vtls-text with ''.
replace all occurrences of '"' in t_flatfile_vtls-matkl with ''.
write 😕 t_flatfile_vtls-text,t_flatfile_vtls-matkl,
t_flatfile_vtls-ebeln.
endloop.
*======================================================================
GETTING DATA IN INTERNAL TABLES
*======================================================================
PERFORM INIT_TABLES.
PERFORM POPULATE_TABLES.
LOOP AT t_po_header.
PERFORM move_data_to_bapi_structures.
PERFORM call_bapi.
ENDLOOP. " AT t_po_header
WRITE: / 'No.of POs successfully created:', w_success.
PERFORM STORE_MESSAGES TABLES T_MESSAGE.
PERFORM free_internal_tables.
*======================================================================
PRINT DETAILS of Presentation Server
*======================================================================
*C-- PRINT OUTPUT
IF p_fore EQ c_x.
PERFORM write_summary_data.
ENDIF.
*======================================================================
PRINT DETAILS of Application Server
*======================================================================
*C-- Write to application server if needed
IF p_back = c_x.
*C-- Write Summary data to file
PERFORM write_summary_data_file.
*C-- Write Header discrepancy data to file
ENDIF.
*&----
*
*& Form validate_pre_file
*&----
*
Routine to validate presentation server file path.
*----
*
-->fp_name text
*----
*
FORM validate_pre_file USING fp_name TYPE rlgrap-filename.
DATA : l_result,
l_filename TYPE string.
l_filename = fp_name.
CLEAR l_result.
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = l_filename
RECEIVING
result = l_result
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE s007 DISPLAY LIKE c_e.
LEAVE LIST-PROCESSING.
ELSEIF l_result IS INITIAL.
MESSAGE s008 DISPLAY LIKE c_e.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " validate_pre_file_hdr
*&----
*
*& Form validate_app_file
*&----
*
text - Checks if the path entered and filename is correct
*----
*
FORM validate_app_file USING fp_file TYPE FILENAME-FILEINTERN.
data : l_fname(60).
CALL FUNCTION 'FILE_GET_NAME'
EXPORTING
LOGICAL_FILENAME = FP_FILE
OPERATING_SYSTEM = SY-OPSYS
IMPORTING
FILE_NAME = L_FNAME
EXCEPTIONS
FILE_NOT_FOUND = 1
OTHERS = 2.
IF SY-SUBRC = '0'.
OPEN DATASET L_FNAME FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc NE 0.
MESSAGE s007 DISPLAY LIKE c_e.
ELSE.
CLOSE DATASET l_fname.
ENDIF.
ENDIF.
ENDFORM. " validate_app_file
*&----
*
*& Form upload_file
*&----
*
Routine to upload data from file to tables.
*----
*
-->P_fp_flatfile
-->P_fp_file
<--P_fp_rc
*----
*
FORM upload_file TABLES fp_flatfile
USING fp_file TYPE string
CHANGING fp_rc TYPE sy-subrc.
IF fp_flatfile[] IS INITIAL.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = fp_file
filetype = c_filetype
has_field_separator = c_x
TABLES
data_tab = fp_flatfile
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17.
MOVE sy-subrc TO fp_rc.
ENDIF.
ENDFORM. " upload_file
&----
*& Form write_summary_data_hdr
&----
Write Summary of Data
----
FORM write_summary_data.
*C-- Local Declarations
DATA: l_num_vtls TYPE i,
l_num_sap_loaded TYPE i,
l_num_not_sap_loaded TYPE i.
l_num_sap_loaded = w_recordsap.
WRITE : /1(63) sy-uline .
FORMAT COLOR 3.
WRITE :/1(1) c_pipe,
7(40)'VTLS to SAP - PO Transfer'(004) ,
63(1) c_pipe.
FORMAT COLOR OFF.
DESCRIBE TABLE t_flatfile_vtls LINES l_num_vtls.
WRITE : /1(63) sy-uline .
WRITE :/1(1) c_pipe,
7(40)'No.of PO Records from VTLS:'(005) ,
50(1) c_pipe,
52(10) l_num_vtls,
63(1) c_pipe.
DESCRIBE TABLE t_sap LINES l_num_sap_loaded.
WRITE :/1(1) c_pipe,
7(40)'No.of records uploaded in SAP:'(006) ,
50(1) c_pipe,
52(10) l_num_sap_loaded ,
63(1) c_pipe.
l_num_not_sap_loaded = l_num_vtls - l_num_sap_loaded.
WRITE :/1(1) c_pipe,
7(40)'No.of records not loaded in SAP:'(007) ,
50(1) c_pipe,
52(10) l_num_not_sap_loaded ,
63(1) c_pipe.
WRITE : /1(63) sy-uline.
endform. "write_summary_data
&----
*& Form POPULATE_TABLES
&----
text
----
FORM POPULATE_TABLES.
W_CURRYEAR = SY-DATUM(4).
W_CURRMON = SY-DATUM+4(2).
IF W_CURRMON GE '04'.
W_BEGYR = W_CURRYEAR.
W_ENDYR = W_CURRYEAR + 1.
ELSE.
W_BEGYR = W_CURRYEAR - 1.
W_ENDYR = W_CURRYEAR.
ENDIF.
CONCATENATE W_BEGYR C_BEGMON C_BEGDA INTO W_BEGDA.
CONCATENATE W_ENDYR C_ENDMON C_ENDDA INTO W_ENDDA.
*WRITE 😕 W_BEGDA,W_ENDDA.
SORT T_FLATFILE_VTLS BY EBELN EBELP.
LOOP AT T_FLATFILE_VTLS.
*WRITE 😕 T_FLATFILE_VTLS-EBELN,T_FLATFILE_VTLS-EBELP,
*T_FLATFILE_VTLS-MATKL.
AT NEW EBELN.
T_PO_HEADER-PO = T_FLATFILE_VTLS-EBELN.
T_PO_HEADER-VENDOR = T_FLATFILE_VTLS-LIFNR.
APPEND T_PO_HEADER.
CLEAR T_PO_HEADER.
ENDAT.
T_PO_ITEM-PO = T_FLATFILE_VTLS-EBELN.
T_PO_ITEM-ITEM = T_FLATFILE_VTLS-EBELP.
T_PO_ITEM-TEXT = T_FLATFILE_VTLS-Text(132).
W_PRICE = T_FLATFILE_VTLS-NETPR.
T_PO_ITEM-PRICE = W_PRICE.
T_PO_ITEM-MENGE = T_FLATFILE_VTLS-MENGE.
SELECT SINGLE MATKL
into T_PO_ITEM-MATKL
FROM T023T
WHERE WGBEZ = T_FLATFILE_VTLS-MATKL.
CASE T_FLATFILE_VTLS-MATKL.
WHEN 'Monograph'.
T_PO_ITEM-KNTTP = C_A.
SELECT SINGLE ANLKL
INTO W_ASSETCLASS
FROM ANKT
WHERE TXK20 = 'Library Books'.
SELECT SINGLE ANLN1
INTO T_PO_ITEM-ASSET
FROM ANLA
WHERE ANLKL = W_ASSETCLASS
AND ERDAT BETWEEN W_BEGDA AND W_ENDDA.
WHEN 'Serials' or 'CD'.
T_PO_ITEM-KNTTP = C_K.
SELECT SINGLE BKLAS
INTO W_BKLAS
FROM T023
WHERE MATKL = T_PO_ITEM-MATKL.
SELECT SINGLE KONTS
INTO T_PO_ITEM-GL_ACC
FROM T030
WHERE BKLAS = W_BKLAS.
SELECT SINGLE KOSTL
INTO T_PO_ITEM-COST
FROM TKA3A
WHERE KSTAR = T_PO_ITEM-GL_ACC
AND BUKRS = C_COMP.
*WHEN OTHERS.
*MESSAGE
ENDCASE.
APPEND T_PO_ITEM.
CLEAR T_PO_ITEM.
ENDLOOP.
LOOP AT T_PO_HEADER.
WRITE:/ T_PO_HEADER-PO,T_PO_HEADER-VENDOR.
ENDLOOP.
LOOP AT T_PO_ITEM.
WRITE:/ T_PO_ITEM-PO,T_PO_ITEM-ITEM,T_PO_ITEM-TEXT,
T_PO_ITEM-PRICE,T_PO_ITEM-KNTTP.
ENDLOOP.
ENDFORM. "POPULATE_TABLES
*&----
*
*& Form init_tables
*&----
*
This form Routine is used ti initialize all the internal tables
*----
*
FORM INIT_TABLES .
CLEAR : t_bapi_poheader,
t_bapi_poheaderx,
t_bapi_poitem,
t_bapi_poitemx,
t_bapi_poaccount,
t_bapi_poaccountx,
w_success.
REFRESH: t_bapi_poheader,
t_bapi_poheaderx,
t_bapi_poitem,
t_bapi_poitemx,
t_bapi_poaccount,
t_bapi_poaccountx.
ENDFORM. " init_tables
*&----
*
*& Form move_data_to_bapi_structures
*&----
*
This form routine is used to move all the data records to Bapi
structures
*&----
*
FORM move_data_to_bapi_structures.
*C-- Header Data
PERFORM move_data_to_poheader.
*C-- Header Data check
CLEAR t_bapi_poheaderx.
PERFORM fill_check_structure USING c_bapimepoheaderx
c_t_bapi_poheader
c_t_bapi_poheaderx
c_x.
*C-- Force item renumbering Off
t_bapi_poheaderx-item_intvl = c_x.
t_bapi_poheaderx-item_intvl = ''.
PERFORM move_data_to_poitem.
ENDFORM. " move_data_to_bapi_structures
*&----
*
*& Form fill_check_structure
*&----
*
This form Routine will check whether the specified structure
exist/active
*----
*
FORM fill_check_structure USING fp_tabname TYPE any
fp_orgtabname TYPE any
fp_chktabname TYPE any
fp_check TYPE c.
FIELD-SYMBOLS : <fs_chk>, <fs_org>.
DATA: l_char1(61) TYPE c,
l_char2(61) TYPE c.
DATA: BEGIN OF tl_nametab OCCURS 60.
INCLUDE STRUCTURE x031l.
DATA: END OF tl_nametab.
REFRESH tl_nametab.
CALL FUNCTION 'RFC_GET_NAMETAB'
EXPORTING
tabname = fp_tabname
TABLES
nametab = tl_nametab
EXCEPTIONS
table_not_active = 1
OTHERS = 2.
IF sy-subrc <> 0.
CLEAR tl_nametab.
ENDIF.
LOOP AT tl_nametab.
CLEAR: l_char1, l_char2.
CONCATENATE fp_chktabname c_hyp tl_nametab-fieldname INTO l_char1.
ASSIGN (l_char1) TO <fs_chk>.
CONCATENATE fp_orgtabname c_hyp tl_nametab-fieldname INTO l_char2.
ASSIGN (l_char2) TO <fs_org>.
IF <fs_org> IS NOT INITIAL.
<fs_chk> = fp_check.
ENDIF.
ENDLOOP.
ENDFORM. " fill_check_structure
*&----
*
*& Form move_data_to_poheader
*----
*
This form Routine is used to populate all the header details
into bapi structure
*----
*
FORM move_data_to_poheader .
CLEAR t_bapi_poheader.
--- t_bapi_poheader-COMP_CODE
t_bapi_poheader-comp_code = C_COMP.
--- t_bapi_poheader-DOC_TYPE
t_bapi_poheader-doc_type = C_DOCTYP.
--- t_bapi_poheader-CREATED_BY
IF NOT t_po_header-created_by IS INITIAL.
t_bapi_poheader-created_by = t_po_header-created_by.
ENDIF.
--- t_bapi_poheader-VENDOR
IF NOT t_po_header-vendor IS INITIAL.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = t_po_header-vendor
IMPORTING
output = t_bapi_poheader-vendor.
ENDIF.
--- t_bapi_poheader-LANGU
t_bapi_poheader-langu = sy-langu.
--- t_bapi_poheader-PURCH_ORG
t_bapi_poheader-purch_org = C_PURORG.
--- t_bapi_poheader-PUR_GROUP
t_bapi_poheader-pur_group = C_PURGRP.
t_bapi_poheader-item_intvl = '10'.
ENDFORM. " move_data_to_poheader
*&----
*
*& Form move_data_to_poitem
*&----
*
This form Routine is used to populate all the item level data
into bapi structure
*----
*
FORM move_data_to_poitem .
REFRESH : t_bapi_poitem,
t_bapi_poitemx,
t_bapi_poaccount,
t_bapi_poaccountx,
t_bapi_itemtext.
LOOP AT t_po_item
WHERE po = t_po_header-po.
CLEAR t_bapi_poitem.
IF NOT T_PO_ITEM-KNTTP IS INITIAL.
T_BAPI_POITEM-ACCTASSCAT = T_PO_ITEM-KNTTP.
ENDIF.
--- t_bapi_poitem-PO_ITEM
IF NOT t_po_item-item IS INITIAL.
t_bapi_poitem-po_item = t_po_item-item.
ENDIF.
--- t_bapi_poitem-SHORT_TEXT
IF NOT t_po_item-text IS INITIAL.
t_bapi_poitem-short_text = t_po_item-text.
ENDIF.
--- t_bapi_poitem-PLANT
IF NOT t_po_item-plant IS INITIAL.
t_bapi_poitem-plant = c_plant.
ENDIF.
--- t_bapi_poitem-MATL_GROUP
IF NOT t_po_item-matkl IS INITIAL.
t_bapi_poitem-matl_group = t_po_item-matkl.
ENDIF.
--- t_bapi_poitem-QUANTITY
IF NOT t_po_item-menge IS INITIAL.
t_bapi_poitem-quantity = t_po_item-menge.
ENDIF.
Caution: Source field is longer than target field
--- t_bapi_poitem-PO_UNIT
IF NOT t_po_item-po_unit IS INITIAL.
t_bapi_poitem-po_unit = c_unit.
ENDIF.
--- t_bapi_poitem-NET_PRICE
IF NOT t_po_item-price IS INITIAL.
t_bapi_poitem-net_price = t_po_item-price.
ENDIF.
Caution: Source field is longer than target field
*C-- Item Data check
CLEAR t_bapi_poitemx.
PERFORM fill_check_structure USING c_bapimepoitem
c_t_bapi_poitem
c_t_bapi_poitemx
c_x.
*C-- Assign Item number to check structure
t_bapi_poitemx-po_item = t_po_item-item.
t_bapi_poitemx-po_itemx = c_x.
APPEND t_bapi_poitem.
APPEND t_bapi_poitemx.
*C-- Account Assisgnment.
CLEAR t_bapi_poaccount.
t_bapi_poaccount-creat_date = sy-datum.
--- t_bapi_poaccount-PO_ITEM
IF NOT t_po_item-item IS INITIAL.
t_bapi_poaccount-po_item = t_po_item-item.
ENDIF.
--- t_bapi_poaccount-QUANTITY
IF NOT t_po_item-menge IS INITIAL.
t_bapi_poaccount-quantity = t_po_item-menge.
ENDIF.
--- t_bapi_poaccount-GL_ACCOUNT
IF NOT t_po_item-gl_acc IS INITIAL.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = t_po_item-gl_acc
IMPORTING
output = t_bapi_poaccount-gl_account.
ENDIF.
--- t_bapi_poaccount-COSTCENTER
IF NOT t_po_item-cost IS INITIAL.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = t_po_item-cost
IMPORTING
output = t_bapi_poaccount-costcenter.
ENDIF.
--- t_bapi_poaccount-ASSET_NO
IF NOT t_po_item-asset IS INITIAL.
t_bapi_poaccount-asset_no = t_po_item-asset.
ENDIF.
*C-- Account Check
CLEAR t_bapi_poaccountx.
PERFORM fill_check_structure USING c_bapimepoaccount
c_t_bapi_poaccount
c_t_bapi_poaccountx
c_x.
*C-- Assign item number / acct. assgn. number to Account check
t_bapi_poaccountx-po_item = t_po_item-item.
t_bapi_poaccountx-serial_no = c_sqno.
t_bapi_poaccountx-po_itemx = c_x.
t_bapi_poaccountx-serial_nox = c_x.
APPEND t_bapi_poaccount.
APPEND t_bapi_poaccountx.
CLEAR t_bapi_poaccount.
*C--item long text...
clear t_bapi_itemtext.
t_bapi_itemtext-po_number = t_po_header-po.
t_bapi_itemtext-po_item = t_po_item-item.
t_bapi_itemtext-text_id = c_id.
t_bapi_itemtext-text_line = t_po_item-text.
append t_bapi_itemtext.
ENDLOOP. " AT t_poitem
ENDFORM. " move_data_to_poitem
*&----
*
*& Form call_bapi
*&----
*
This form Routine is used to commit the data records
*----
*
FORM call_bapi .
DATA : l_msgty TYPE c,
l_msgid(2) TYPE c,
l_msgno(3) TYPE c,
l_msgtx(100) TYPE c,
l_errflag TYPE c.
CLEAR: t_bapireturn.
REFRESH: t_bapireturn.
**C-- End of handling Type of SubC / Serial Nos
CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
poheader = t_bapi_poheader
poheaderx = t_bapi_poheaderx
IMPORTING
EXPPURCHASEORDER = t_bapi_poheader-po_number
TABLES
return = t_bapireturn
poitem = t_bapi_poitem
poitemx = t_bapi_poitemx
poaccount = t_bapi_poaccount
poaccountx = t_bapi_poaccountx
POTEXTITEM = t_bapi_itemtext.
READ TABLE t_bapireturn WITH KEY type = c_err TRANSPORTING NO FIELDS.
IF sy-subrc NE 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = c_x.
refresh t_table.
loop at t_bapi_poitem.
clear t_table.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = t_po_header-po
IMPORTING
output = t_table-povtls.
t_table-posap = t_bapi_poheader-po_number.
t_table-itemsap = t_bapi_poitem-po_item.
t_table-aedat = sy-datum.
t_table-paedt = sy-datum.
INDEX = INDEX + 1.
read table t_po_item index INDEX.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = t_po_item-item
IMPORTING
output = t_table-itemvtls.
append t_table.
w_recordsap = w_recordsap + 1.
endloop.
*C--populate ztable with PO details.
insert zvtls_sap FROM TABLE t_table.
ENDIF.
*C-- Write messages
WRITE: / 'PO Number'(001), t_po_header-po.
CLEAR l_errflag.
LOOP AT t_bapireturn.
CLEAR: l_msgty, l_msgid, l_msgno, l_msgtx.
l_msgty = t_bapireturn-type.
l_msgid = t_bapireturn-id.
l_msgno = t_bapireturn-number.
l_msgtx = t_bapireturn-message.
WRITE: / l_msgty, l_msgid, l_msgno, l_msgtx.
t_message-msgty = l_msgty.
t_message-msgid = l_msgid.
t_message-msgno = l_msgno.
t_message-msgtx = l_msgtx.
t_message-po = t_po_header-po.
append t_message.
clear t_message.
IF l_msgty EQ c_err.
l_errflag = c_x.
ENDIF. " l_msgty EQ 'E'
ENDLOOP.
ULINE.
IF l_errflag NE c_x.
w_success = w_success + 1.
ENDIF. " l_errflag NE C_X
ENDFORM. " call_bapi
*&----
*
*& Form free_internal_tables
*&----
*
This form routine is to free memory of all the internal tables
*----
*
FORM free_internal_tables .
FREE: t_po_header,
t_po_item,
t_bapi_poheader,
t_bapi_poheaderx,
t_bapi_poitem,
t_bapi_poitemx,
t_bapi_poaccount,
t_bapi_poaccountx.
ENDFORM. " free_internal_tables
&----
*& Form upload_file_app
&----
text
----
-->FP_FLATFILEtext
-->FP_FILE text
-->FP_RC text
----
FORM upload_file_app TABLES fp_flatfile
USING fp_file TYPE FILENAME-FILEINTERN
CHANGING fp_rc TYPE sy-subrc.
DATA: l_string TYPE tedata-data.
DATA: wa_data_file TYPE tp_flatfile_vtls,
l_wllength TYPE i,
FNAME(60).
CALL FUNCTION 'FILE_GET_NAME'
EXPORTING
LOGICAL_FILENAME = FP_FILE
OPERATING_SYSTEM = SY-OPSYS
IMPORTING
FILE_NAME = FNAME
EXCEPTIONS
FILE_NOT_FOUND = 1
OTHERS = 2.
IF SY-SUBRC = 0.
OPEN DATASET FNAME FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc NE 0.
*C-- commented by Bikash
MESSAGE s107(yaero_ps) DISPLAY LIKE c_e.
message e008.
ELSE.
DO.
CLEAR: l_string.
READ DATASET FNAME INTO l_string LENGTH l_wllength.
IF sy-subrc NE 0.
EXIT.
ELSE.
SPLIT l_string AT con_tab INTO wa_data_file-lifnr
wa_data_file-ebeln
wa_data_file-ebelp
wa_data_file-text
wa_data_file-netpr
wa_data_file-menge
wa_data_file-matkl.
APPEND wa_data_file TO fp_flatfile.
ENDIF.
ENDDO.
CLOSE DATASET FNAME.
ENDIF.
ENDIF.
ENDFORM. " upload_file_app
&----
*& Form STORE_MESSAGES
&----
text
----
-->FP_MESSAGEStext
----
FORM STORE_MESSAGES TABLES FP_MESSAGES STRUCTURE T_MESSAGE.
DATA: wl_output_data LIKE t_MESSAGE.
DATA: l_catstr TYPE string.
DATA: l_fieldvalue TYPE string.
DATA: l_index TYPE i VALUE 1.
DATA: L_FNAME(60).
FIELD-SYMBOLS <fs>.
CLEAR l_catstr.
CALL FUNCTION 'FILE_GET_NAME'
EXPORTING
LOGICAL_FILENAME = '/USR/SAP/VTLS/PO/LOG'
OPERATING_SYSTEM = SY-OPSYS
IMPORTING
FILE_NAME = L_FNAME
EXCEPTIONS
FILE_NOT_FOUND = 1
OTHERS = 2.
IF SY-SUBRC = '0'.
IF fp_messages[] IS NOT INITIAL.
OPEN DATASET L_FNAME FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc NE 0.
LEAVE LIST-PROCESSING.
ELSE.
LOOP AT fp_messages INTO wl_output_data.
DO.
ASSIGN COMPONENT l_index OF STRUCTURE wl_output_data TO <fs>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
MOVE <fs> TO l_fieldvalue.
IF l_catstr IS NOT INITIAL.
CONCATENATE l_catstr l_fieldvalue INTO l_catstr SEPARATED
BY con_tab.
ELSE.
MOVE l_fieldvalue TO l_catstr.
ENDIF.
l_index = l_index + c_1.
CLEAR l_fieldvalue.
CLEAR <fs>.
ENDDO.
l_index = c_1.
TRANSFER l_catstr TO L_FNAME .
CLEAR wl_output_data.
CLEAR l_catstr.
ENDLOOP.
CLOSE DATASET L_FNAME.
ENDIF.
ENDIF.
ENDIF.
ENDFORM. "STORE_MESSAGES