Posted: Thu Sep 20, 2007 10:50 am Post subject: Post Goods Movement using BAPI_GOODSMVT_CREATE
BAPI_GOODSMVT_CREATE - Post goods movements with MB_CREATE_GOODS_MOVEMENT
Functionality
You can use this method to create a material document for a goods movement.
You can only create one material document each time you call up the method.
You can use a simulation function before posting the goods movement to check how the data is updated in the database. The standard system is configured so that the goods movement is posted immediately without simulating beforehand.
Code:
*&---------------------------------------------------------------------*
*& Report ZFIB_CARGA_DATA_IMPORTA_PROV
*&
*&---------------------------------------------------------------------*
*& Descrption: Carga de data de campo de importación maestro de material
*& Date : Enero 2006
*&---------------------------------------------------------------------*
REPORT ZMMB_CREAR_ENTRADA_AUTO_SINIMP.
************************************************************************
* T A B L E S *
************************************************************************
TABLES: bsis, "Contabilidad: Índice secundario para ctas.mayor
bseg. "Segmento de documento de Contabilidad
************************************************************************
* I N T E R N A L T A B L E S *
************************************************************************
DATA: BEGIN OF it_entrada OCCURS 0,
pstng_date LIKE bapi2017_gm_head_01-pstng_date, "Fecha de cantab.
doc_date LIKE bapi2017_gm_head_01-doc_date,
header_txt LIKE bapi2017_gm_head_01-header_txt,
REF_DOC_NO LIKE bapi2017_gm_head_01-REF_DOC_NO,
material LIKE bapi2017_gm_item_create-material,
plant LIKE bapi2017_gm_item_create-plant,
stge_loc LIKE bapi2017_gm_item_create-stge_loc,
VENDOR LIKE bapi2017_gm_item_create-VENDOR,
move_type LIKE bapi2017_gm_item_create-move_type,
MVT_IND LIKE bapi2017_gm_item_create-MVT_IND,
PO_NUMBER LIKE bapi2017_gm_item_create-PO_NUMBER,
po_item LIKE bapi2017_gm_item_create-po_item,
ENTRY_QNT LIKE bapi2017_gm_item_create-ENTRY_QNT,
ENTRY_UOM LIKE bapi2017_gm_item_create-ENTRY_UOM,
end of it_entrada.
****** Data de importacion
DATA: BEGIN OF BDC_TAB OCCURS 0.
INCLUDE STRUCTURE BDCDATA.
DATA: END OF BDC_TAB.
data: BEGIN OF it_data OCCURS 0,
PRONU like EIPO-PRONU, "Factura: número
PRODA(8) type c, "Factura: FECHA
PO_NUMBER type BSTNR, "Número de pedido
po_item type EBELP, "Número de posición
ENTRY_QNT type ERFMG, "Cantidad
pstng_date(8) type c, "Fecha de cantab.
doc_date(8) type c, "Fecha de documento
header_txt type BKTXT, "Texto de cabecera
*data importacion
VORPA like eipo-vorpa, "Clase comprobante
VORNU like eipo-vornu, "No Comprobante
VORDA like eipo-vorda, "Fecha comprobante
NACAR like EIPO-NACAR, "Clase comprobante
NACNU like EIPO-NACNU, "No Comprobante
NACDA(8) type c, "Fecha comprobante
NAUEL(8) type c, "Validez comprobante
NAHOE like EIPO-NAHOE, "Entidad emisora
NACA2 like EIPO-NACA2, "Clase comprobante
NACN2 like EIPO-NACN2, "No Comprobante
NACD2(8) type c, "Fecha comprobante
NAUE2(8) type c, "Validez comprobante
NAHO2 like EIPO-NAHO2, "Entidad emisora
NACA3 like EIPO-NACA3, "Clase comprobante
NACN3 like EIPO-NACN3, "No Comprobante
NACD3(8) type c, "Fecha comprobante
NAUE3(8) type c, "Validez comprobante
NAHO3 like EIPO-NAHO3, "Entidad emisora
NACA4 like EIPO-NACA4, "Clase comprobante
NACN4 like EIPO-NACN4, "No Comprobante
NACD4(8) type c, "Fecha comprobante
NAUE4(8) type c, "Validez comprobante
NAHO4 like EIPO-NAHO4, "Entidad emisora
NACA5 like EIPO-NACA5, "Clase comprobante
NACN5 like EIPO-NACN5, "No Comprobante
NACD5(8) type c, "Fecha comprobante
NAUE5(8) type c, "Validez comprobante
NAHO5 like EIPO-NAHO5, "Entidad emisora
end of it_data.
data: BEGIN OF it_data2 OCCURS 0,
PRONU like EIPO-PRONU, "Factura: número
PRODA(8) type c, "Factura: FECHA
PO_NUMBER type BSTNR, "Número de pedido
po_item type EBELP, "Número de posición
ENTRY_QNT type ERFMG, "Cantidad
pstng_date(8) type c, "Fecha de cantab.
doc_date(8) type c, "Fecha de documento
header_txt type BKTXT, "Texto de cabecera
EXPOS like EIPO-EXPOS, "Posicion
end of it_data2.
DATA BEGIN OF messtab OCCURS 10.
INCLUDE STRUCTURE bdcmsgcoll.
DATA END OF messtab.
DATA: BEGIN OF it_messtab OCCURS 0,
TYPE LIKE bapiret2-TYPE,
NUMBER LIKE bapiret2-NUMBER,
MESSAGE LIKE bapiret2-MESSAGE,
MESSAGE_V2 LIKE bapiret2-MESSAGE_V2,
MESSAGE_V3 LIKE bapiret2-MESSAGE_V3,
MESSAGE_V4 LIKE bapiret2-MESSAGE_V4,
PRONU like EIPO-PRONU,
END OF it_messtab.
************************************************************************
* D A T A *
************************************************************************
DATA: l_goodsmvt_header LIKE bapi2017_gm_head_01,
l_goodsmvt_code LIKE bapi2017_gm_code,
l_testrun LIKE bapi2017_gm_gen-testrun,
l_goodsmvt_headret LIKE bapi2017_gm_head_ret,
l_materialdocument LIKE bapi2017_gm_head_ret-mat_doc,
l_matdocumentyear LIKE bapi2017_gm_head_ret-doc_year.
DATA: i_goodsmvt_item LIKE bapi2017_gm_item_create OCCURS 10
WITH HEADER LINE,
i_return LIKE bapiret2 OCCURS 10 WITH HEADER LINE.
DATA: w_data TYPE string.
data: nlinea type i.
TYPE-POOLS: slis.
INCLUDE <icon>.
INCLUDE <symbol>.
CONSTANTS : gc_formname_top_of_page TYPE slis_formname
VALUE 'TOP_OF_PAGE',
gc_formname_end_of_list TYPE slis_formname
VALUE 'END_OF_LIST'.
DATA : gt_fieldcat TYPE slis_t_fieldcat_alv, "fieldattributes
gs_layout TYPE slis_layout_alv , "list layout
gt_sort TYPE slis_t_sortinfo_alv, "sort orders
gt_events TYPE slis_t_event, "list events
gt_sp_group TYPE slis_t_sp_group_alv, "list groups
g_repid LIKE sy-repid, "reportname
gs_keyinfo TYPE slis_keyinfo_alv, "relation
"between header + item table
gt_list_top_of_page TYPE slis_t_listheader, "top-of-page
g_tabname_header TYPE slis_tabname, "name header
g_tabname_item TYPE slis_tabname, "name item
g_save(1) TYPE c,
gx_variant LIKE disvariant,
g_status TYPE slis_formname VALUE 'STANDARD01', "user-status
g_variant LIKE disvariant, "user-variant
g_exit(1) TYPE c.
************************************************************************
* S E L E C T I O N S *
************************************************************************
************************************************************************
* P A R A M E T E R S *
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK pblock WITH FRAME title text-001.
PARAMETERS: p_entry LIKE rlgrap-filename DEFAULT 'C:\DATENT.txt'.
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN END OF BLOCK pblock.
************************************************************************
* A T S E L E C T I O N - S C R E E N *
************************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_entry.
CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
EXPORTING
field_name = ' '
mask = ',Todos los archivos,'
CHANGING
file_name = p_entry
EXCEPTIONS
mask_too_long = 1
OTHERS = 2.
IF sy-subrc NE 0.
ENDIF.
************************************************************************
* I N I T I A L I Z A T I O N *
************************************************************************
INITIALIZATION.
* initialization list viewer
g_repid = sy-repid.
* build table with events to be triggered
PERFORM eventtab_build USING gt_events[].
g_save = 'A'.
CLEAR g_variant.
g_variant-report = g_repid.
************************************************************************
* S T A R T - O F - S E L E C T I O N *
************************************************************************
start-of-selection.
perform leer_archivo.
perform procesar_data.
perform imprimir_error.
ENDFORM. " crear_entrada
*&---------------------------------------------------------------------*
*& Form procesar_data
*&---------------------------------------------------------------------*
* text: Proceso para grupar por factura
*----------------------------------------------------------------------*
FORM procesar_data .
it_data2[] = it_data[].
sort it_data by PRONU .
sort it_data2 by PRONU .
loop at it_data.
move-corresponding it_data to it_data2.
at new PRONU .
"DATOS CABECERA
nlinea = 0.
perform data_cab.
endat.
"Posiciones
perform data_pos.
at end of pronu.
"crear entreda
perform crear_entrada.
endat.
endloop.
ENDFORM. " procesar_data
*&--------------------------------------------------------------------*
*& Form bdc_newdynpro
*&--------------------------------------------------------------------*
FORM bdc_newdynpro USING program dynpro.
*&--------------------------------------------------------------------*
*& Form bdc_field
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
ENDFORM. "bdc_field
*&---------------------------------------------------------------------*
*& Form data_cab
*&---------------------------------------------------------------------*
* text: Datos de Cabezera de la entrada
*----------------------------------------------------------------------*
FORM data_cab .
data xfec type d.
refresh i_goodsmvt_item.
CLEAR l_goodsmvt_header.
concatenate it_data2-pstng_date+4(4)
it_data2-pstng_date+2(2)
it_data2-pstng_date(2) into xfec.
l_goodsmvt_header-pstng_date = xfec.
concatenate it_data2-doc_date+4(4)
it_data2-doc_date+2(2)
it_data2-doc_date(2) into xfec.
l_goodsmvt_header-pr_uname = sy-uname.
l_goodsmvt_header-header_txt = it_data2-header_txt.
L_GOODSMVT_HEADER-REF_DOC_NO = 'R01'.
* Tipo de movimiento
l_goodsmvt_code = '01'. "Goods receipt for purchase order
ENDFORM. " data_cab
*&---------------------------------------------------------------------*
*& Form data_pos
*&---------------------------------------------------------------------*
* text: Datos de posicion de la entrada
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM data_pos .
CLEAR i_goodsmvt_item.
* i_goodsmvt_item-material = '000000000070002332'.
* i_goodsmvt_item-plant = 'TRAN'.
* i_goodsmvt_item-stge_loc = '0100'.
* i_goodsmvt_item-VENDOR = '0000000016'.
i_goodsmvt_item-move_type = '101'.
i_goodsmvt_item-MVT_IND = 'B'. "Indicador de movimiento
i_goodsmvt_item-PO_NUMBER = it_data-PO_NUMBER.
i_goodsmvt_item-po_item = it_data-PO_ITEM.
i_goodsmvt_item-ENTRY_QNT = it_data-ENTRY_QNT.
* I_GOODSMVT_ITEM-ENTRY_UOM = 'T'.
* I_GOODSMVT_ITEM-NO_MORE_GR = '1'.
append i_goodsmvt_item.
ENDFORM. " data_pos
*&---------------------------------------------------------------------*
*& Form imprimir_error
*&---------------------------------------------------------------------*
* text: imprecion de log de errores
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM imprimir_error .
describe table it_messtab lines sy-tfill.
if sy-tfill ne 0.
PERFORM cosntruye_lista USING gt_fieldcat[].
PERFORM titulo_lista USING gt_list_top_of_page[].
PERFORM ordenamiento USING gt_sort[].
PERFORM layout_build USING gs_layout.
PERFORM muestra_lista.
endif.
ENDFORM. " imprimir_error
*&---------------------------------------------------------------------*
*& Form eventtab_build
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_GT_EVENTS[] text
*----------------------------------------------------------------------*
FORM eventtab_build USING
lt_events TYPE slis_t_event.
DATA: ls_event TYPE slis_alv_event.
*
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
i_list_type = 0
IMPORTING
et_events = lt_events.
READ TABLE lt_events WITH KEY name = slis_ev_top_of_page
INTO ls_event.
IF sy-subrc = 0.
MOVE gc_formname_top_of_page TO ls_event-form.
APPEND ls_event TO lt_events.
ENDIF.
ENDFORM. " eventtab_build
*&---------------------------------------------------------------------*
*& Form cosntruye_lista
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_GT_FIELDCAT[] text
*----------------------------------------------------------------------*
FORM cosntruye_lista USING lt_fieldcat TYPE slis_t_fieldcat_alv.
ENDFORM. " cosntruye_lista
*&---------------------------------------------------------------------*
*& Form titulo_lista
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_GT_LIST_TOP_OF_PAGE[] text
*----------------------------------------------------------------------*
FORM titulo_lista USING lt_top_of_page TYPE slis_t_listheader.
DATA: ls_line TYPE slis_listheader.
DATA: lin TYPE i.
DATA: hlpstr(100) TYPE c,
date_str_l(15) TYPE c,
date_str_h(15) TYPE c,
p_titge(50) type c.
CLEAR: date_str_l, date_str_h.
p_titge = 'Lista de entradas automaticas'.
* Headline : type H
CLEAR ls_line.
ls_line-typ = 'H'.
WRITE p_titge TO ls_line-info CENTERED.
APPEND ls_line TO lt_top_of_page.
** Subheadline: type S
CLEAR ls_line.
ls_line-typ = 'S'.
CONCATENATE sy-datum+6(2) '.' sy-datum+4(2) '.' sy-datum(4)
INTO hlpstr.
* CONCATENATE text-009 hlpstr INTO hlpstr separateD by space.
WRITE hlpstr TO ls_line-info LEFT-JUSTIFIED.
APPEND ls_line TO lt_top_of_page.
ENDFORM. " titulo_lista
*&---------------------------------------------------------------------*
*& Form ordenamiento
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_GT_SORT[] text
*----------------------------------------------------------------------*
FORM ordenamiento USING e99_sort TYPE slis_t_sortinfo_alv.
DATA: ls_sort TYPE slis_sortinfo_alv.
CLEAR ls_sort.
* xdefine standard sort order:
ls_sort-fieldname = 'PRONU'.
ls_sort-up = 'X'.
ls_sort-down = ' '.
*--- Subtotals by group change ----------------------------------------*
APPEND ls_sort TO e99_sort.
* xdefine standard sort order:
ls_sort-fieldname = 'TYPE'.
ls_sort-up = 'X'.
ls_sort-down = ' '.
*--- Subtotals by group change ----------------------------------------*
APPEND ls_sort TO e99_sort.
ls_sort-fieldname = 'NUMBER'.
ls_sort-up = 'X'.
ls_sort-down = ' '.
*--- Subtotals by group change ----------------------------------------*
APPEND ls_sort TO e99_sort.
ENDFORM. " ordenamiento
*&---------------------------------------------------------------------*
*& Form layout_build
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_GS_LAYOUT text
*----------------------------------------------------------------------*
FORM layout_build USING ls_layout TYPE slis_layout_alv.
ENDFORM. " layout_build
*&---------------------------------------------------------------------*
*& Form muestra_lista
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM muestra_lista .
* this form diplays the selected withholding tax list above with the
* ABAP List viewer
*&--------------------------------------------------------------------*
*& Form comment_change
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->LT_TOP_OF_Ptext
*---------------------------------------------------------------------*
FORM comment_change USING lt_top_of_page TYPE slis_t_listheader.
DATA: wa_lt TYPE slis_listheader.
LOOP AT lt_top_of_page INTO wa_lt.
IF wa_lt-typ = 'S' AND wa_lt-key = text-103.
wa_lt-info = wa_lt-info + 1.
MODIFY lt_top_of_page FROM wa_lt.
ENDIF.
ENDLOOP.
ENDFORM. "comment_change
*&--------------------------------------------------------------------*
*& Form top_of_page
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM top_of_page.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = gt_list_top_of_page.
ENDFORM. "top_of_page
*&---------------------------------------------------------------------*
*& Form user_command
*&---------------------------------------------------------------------*
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
DATA: l_materialdocument LIKE bapi2017_gm_head_ret-mat_doc,
l_matdocumentyear LIKE bapi2017_gm_head_ret-doc_year.
CLEAR: r_ucomm.
READ TABLE IT_MESSTAB index rs_selfield-tabindex.
CASE rs_selfield-sel_tab_field.
WHEN '1-MESSAGE_V2'.
SEARCH IT_MESSTAB-MESSAGE_V2 for '/'.
if sy-subrc eq 0.
SPLIT IT_MESSTAB-MESSAGE_V2 AT '/'
INTO l_materialdocument l_matdocumentyear.
SET PARAMETER ID 'MBN' FIELD l_materialdocument.
SET PARAMETER ID 'MJA' FIELD l_matdocumentyear.
CALL TRANSACTION 'MB03' AND SKIP FIRST SCREEN.
else.
MESSAGE w022(icc_cl).
endif.
WHEN OTHERS.
MESSAGE w022(icc_cl).
ENDCASE.
ENDFORM. "user_command
The following is an abap program making used of the BAPI function BAPI_GOODSMVT_CREATE to do Goods Receipts for Purchase Order after importing the data from an external system.
Code:
*
* BAPI TO Upload Inventory Data
*
* GMCODE Table T158G - 01 - MB01 - Goods Receipts for Purchase Order
* 02 - MB31 - Goods Receipts for Prod Order
* 03 - MB1A - Goods Issue
* 04 - MB1B - Transfer Posting
* 05 - MB1C - Enter Other Goods Receipt
* 06 - MB11
*
* Domain: KZBEW - Movement Indicator
* Goods movement w/o reference
* B - Goods movement for purchase order
* F - Goods movement for production order
* L - Goods movement for delivery note
* K - Goods movement for kanban requirement (WM - internal only)
* O - Subsequent adjustment of "material-provided" consumption
* W - Subsequent adjustment of proportion/product unit material
*
report zbapi_goodsmovement.
parameters: p-file like rlgrap-filename default
'c:\sapdata\TEST.txt'.
parameters: e-file like rlgrap-filename default
'c:\sapdata\gdsmvterror.txt'.
parameters: xpost like sy-datum default sy-datum.
data: begin of gmhead.
include structure bapi2017_gm_head_01.
data: end of gmhead.
data: begin of gmcode.
include structure bapi2017_gm_code.
data: end of gmcode.
data: begin of mthead.
include structure bapi2017_gm_head_ret.
data: end of mthead.
data: begin of itab occurs 100.
include structure bapi2017_gm_item_create.
data: end of itab.
data: begin of errmsg occurs 10.
include structure bapiret2.
data: end of errmsg.
data: wmenge like iseg-menge,
errflag.
data: begin of pcitab occurs 100,
ext_doc(10), "External Document Number
mvt_type(3), "Movement Type
doc_date(8), "Document Date
post_date(8), "Posting Date
plant(4), "Plant
material(18), "Material Number
qty(13), "Quantity
recv_loc(4), "Receiving Location
issue_loc(4), "Issuing Location
pur_doc(10), "Purchase Document No
po_item(3), "Purchase Document Item No
del_no(10), "Delivery Purchase Order Number
del_item(3), "Delivery Item
prod_doc(10), "Production Document No
scrap_reason(10), "Scrap Reason
upd_sta(1), "Update Status
end of pcitab.
if errflag is initial.
commit work and wait.
if sy-subrc ne 0.
write:/ 'Error in updating'.
exit.
else.
write:/ mthead-mat_doc, mthead-doc_year.
perform upd_sta.
endif.
endif.
*---------------------------------------------------------------------*
* FORM UPD_STA *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
form upd_sta.
loop at pcitab.
pcitab-upd_sta = 'X'.
modify pcitab.
endloop.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.