SAP SD BAPI
From SapWiki
Contents
Crear Factura VF01
FUNCTION z_sd_crea_factura. *"---------------------------------------------------------------------- *"*"Interfase local *" IMPORTING *" REFERENCE(I_VBELN) TYPE LIKP-VBELN *" REFERENCE(I_FKART) TYPE RV60A-FKART *" REFERENCE(I_REF_DOC_CA) TYPE VBTYP_V *" REFERENCE(I_DOC_TYPE) TYPE AUART *" EXPORTING *" REFERENCE(E_VBELN) TYPE VBRK-VBELN *" REFERENCE(E_RETURN) TYPE BAPIRET2_T *"---------------------------------------------------------------------- data lt_return1 type bapiret1_tab. DATA ld_mode(1) VALUE 'A'. " 'N'. DATA lt_mtxt LIKE TABLE OF bdcmsgcoll WITH HEADER LINE. DATA ls_return TYPE bapiret2. DATA ls_return1 TYPE bapiret1. CLEAR: e_return[], bdcdata[], lt_mtxt[], e_vbeln. PERFORM crea_factura USING i_fkart i_vbeln i_ref_doc_ca i_doc_type CHANGING e_vbeln lt_return1[]. loop at lt_return1 into ls_return1. MOVE-CORRESPONDING ls_return1 TO ls_return. APPEND ls_return TO e_return. ENDLOOP. ENDFUNCTION. FORM crea_factura USING p_fkart p_vbeln p_ref_doc_ca p_doc_type CHANGING p_vbeln_factuta p_return TYPE bapiret1_tab. DATA lt_datain TYPE STANDARD TABLE OF bapivbrk WITH HEADER LINE. DATA lt_return TYPE STANDARD TABLE OF bapiret1 WITH HEADER LINE. DATA lt_success TYPE STANDARD TABLE OF bapivbrksuccess WITH HEADER LINE. CLEAR: lt_datain, lt_datain[], lt_return, lt_return[], lt_success, lt_success[]. lt_datain-ref_doc = p_vbeln. lt_datain-ref_doc_ca = p_ref_doc_ca. " ls_likp-vbtyp. lt_datain-doc_type = p_doc_type. "ls_likp-lfart. lt_datain-ordbilltyp = p_fkart. APPEND lt_datain. CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE' TABLES billingdatain = lt_datain return = lt_return success = lt_success. READ TABLE lt_success INDEX 1. IF sy-subrc = 0. p_vbeln_factuta = lt_success-bill_doc. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING WAIT = 'X' . endif. p_return[] = lt_return[]. ENDFORM.
Contabilizar SM - VL02N->Contabilizar SM
FUNCTION Z_SD_CONT_SAL_MERC. *"---------------------------------------------------------------------- *"*"Interfase local *" IMPORTING *" REFERENCE(I_VBELN) TYPE LIKP-VBELN *" EXPORTING *" REFERENCE(E_VBELN) TYPE LIKP-VBELN *" REFERENCE(E_RETURN) TYPE BAPIRET2_T *"---------------------------------------------------------------------- CLEAR: e_RETURN[], bdcdata[], lt_mtxt[], e_vbeln. perFORM cont_sm USING i_vbeln CHANGING e_return. ENDFUNCTION. FORM cont_sm USING p_vbeln CHANGING p_return type bapiret2_t. DATA: ls_likp TYPE likp, ls_vbkok TYPE vbkok, lv_fail, lt_prott TYPE STANDARD TABLE OF prott WITH HEADER LINE. data ls_return type bapiret2. SELECT SINGLE * INTO ls_likp FROM likp WHERE vbeln = p_vbeln. CHECK NOT ls_likp IS INITIAL. ls_vbkok-vbeln_vl = ls_likp-vbeln. ls_vbkok-wabuc = 'X'. SET UPDATE TASK LOCAL. CALL FUNCTION 'WS_DELIVERY_UPDATE' EXPORTING vbkok_wa = ls_vbkok synchron = 'X' no_messages_update = ' ' update_picking = 'X' COMMIT = ' ' delivery = ls_likp-vbeln nicht_sperren = 'X' if_error_messages_send_0 = 'X' TABLES prot = lt_prott EXCEPTIONS error_message = 1 OTHERS = 2. IF sy-subrc <> 0. ROLLBACK WORK. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO ls_return-message. append ls_return to p_return. EXIT. ELSE. LOOP AT lt_prott. CHECK lt_prott-msgty = 'E' OR lt_prott-msgty = 'A'. lv_fail = 'X'. ENDLOOP. IF lv_fail IS INITIAL. COMMIT WORK AND WAIT. ELSE. ROLLBACK WORK. MOVE lt_prott-msgid TO ls_return-ID. MOVE lt_prott-msgty TO ls_return-TYPE. MOVE lt_prott-msgno TO ls_return-NUMBER. MOVE lt_prott-msgv1 TO ls_return-MESSAGE_v1. MOVE lt_prott-msgv2 TO ls_return-MESSAGE_v2. MOVE lt_prott-msgv3 TO ls_return-MESSAGE_v3. MOVE lt_prott-msgv4 TO ls_return-MESSAGE_v4. MESSAGE ID lt_prott-MSGID TYPE lt_prott-MSGTY NUMBER lt_prott-MSGNo WITH lt_prott-MSGV1 lt_prott-MSGV2 lt_prott-MSGV3 lt_prott-MSGV4 INTO ls_return-MESSAGE. APPEND ls_return TO p_return. EXIT. ENDIF. ENDIF. ENDFORM.
Cambiar Lote de Entrega
FUNCTION z_sd_cambia_lote. *"---------------------------------------------------------------------- *"*"Interfase local *" IMPORTING *" REFERENCE(I_VBELN) TYPE LIKP-VBELN *" REFERENCE(I_VBELN_FACTURA) TYPE VBRK-VBELN *" EXPORTING *" REFERENCE(E_RETURN) TYPE BAPIRET2_T *"---------------------------------------------------------------------- PERFORM clear_entrega USING i_vbeln i_vbeln_factura CHANGING e_return[]. LOOP AT e_return ASSIGNING FIELD-SYMBOL(<return>) where type = 'E' OR type = 'A'. MESSAGE ID <return>-ID TYPE <return>-TYPE NUMBER <return>-NUMBER WITH <return>-message_v1 <return>-message_v2 <return>-message_v3 <return>-message_v4 INTO <return>-MESSAGE. ENDLOOP. IF sy-subrc = 0. ROLLBACK WORK. ELSE. IF NOT line_exists( e_return[ type = 'S' id = 'Z' number = '999' ] ). CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X' . ENDIF. ENDIF. ENDFUNCTION. types: BEGIN OF ty_lote, vbeln type vbfa-vbeln, posnn type vbfa-posnn, charg type lips-charg, UECHA type lips-UECHA, umvkz TYPE lips-umvkz, umvkn TYPE lips-umvkn, umrev TYPE lips-umrev, lfimg TYPE lips-lfimg, lgmng TYPE lips-lgmng, END OF ty_lote. types ty_lote_t type TABLE OF ty_lote. data gt_lote type ty_lote_t. FORM buscar_lote USING p_vbeln_factura p_lips TYPE lips CHANGING p_lote TYPE ty_lote_t . * buscar pedido CLEAR p_lote[]. SELECT SINGLE vbelv INTO @DATA(l_vbelv) FROM vbfa WHERE vbeln = @p_vbeln_factura AND vbtyp_v = 'C' AND vbtyp_n = 'M'. * buscar entregas SELECT * INTO CORRESPONDING FIELDS OF TABLE p_lote FROM vbfa WHERE vbelv = l_vbelv AND vbtyp_n = 'J'. LOOP AT p_lote ASSIGNING FIELD-SYMBOL(<ls_lote>). SELECT SINGLE charg uecha umvkz umvkn umrev lfimg lgmng INTO CORRESPONDING FIELDS OF <ls_lote> FROM lips WHERE vbeln = <ls_lote>-vbeln AND posnr = <ls_lote>-posnn. ENDLOOP. ENDFORM. FORM clear_entrega USING p_delivery p_vbeln_factura CHANGING p_return TYPE bapiret2_t. DATA delivery LIKE bapiobdlvhdrchg-deliv_numb. DATA header_data LIKE bapiobdlvhdrchg. DATA header_control LIKE bapiobdlvhdrctrlchg. DATA item_data TYPE bapiobdlvitemchg OCCURS 1 WITH HEADER LINE. DATA item_control TYPE bapiobdlvitemctrlchg OCCURS 1 WITH HEADER LINE. DATA return TYPE bapiret2 OCCURS 1 WITH HEADER LINE. DATA ti_lips LIKE lips OCCURS 1 WITH HEADER LINE. delivery = p_delivery. *header_control header_control-deliv_numb = p_delivery. header_data-deliv_numb = p_delivery. REFRESH ti_lips. PERFORM buscar_lote USING p_vbeln_factura ti_lips CHANGING gt_lote[]. LOOP AT gt_lote TRANSPORTING NO FIELDS WHERE charg IS NOT INITIAL. ENDLOOP. IF sy-subrc <> 0. * sin lotes, nada que hacer return-type = 'S'. return-id = 'Z'. return-number = '999'. APPEND return TO p_return. RETURN. ENDIF. SELECT * FROM lips INTO TABLE ti_lips WHERE vbeln EQ p_delivery. CHECK ti_lips[] IS NOT INITIAL. DATA f_lote(1). CLEAR f_lote. * verificar si entrega generada debería tener lotes en base a entrega de salida * original LOOP AT ti_lips WHERE charg IS INITIAL AND posnr < 900000. * verificar posicion tiene Part.lotes en entrega de salida original IF line_exists( gt_lote[ uecha = ti_lips-posnr ] ). f_lote = 'X'. ELSE. * verificar posicion tiene lotes en entrega de salida original LOOP AT gt_lote TRANSPORTING NO FIELDS WHERE posnn = ti_lips-posnr AND charg <> ''. ENDLOOP. IF sy-subrc = 0. f_lote = 'X'. ENDIF. ENDIF. ENDLOOP. IF f_lote <> 'X'. * entrega generada ya tiene lotes, nada que hacer return-type = 'S'. return-id = 'Z'. return-number = '999'. APPEND return TO p_return. RETURN. ENDIF. LOOP AT ti_lips WHERE charg IS INITIAL AND posnr < 900000. *item_data *** buscar si tiene particion de lotes LOOP AT gt_lote INTO DATA(ls_lote) WHERE uecha = ti_lips-posnr. item_data-deliv_numb = ti_lips-vbeln. item_data-deliv_item = ls_lote-posnn. item_data-fact_unit_nom = ls_lote-umvkz . item_data-fact_unit_denom = ls_lote-umvkn. item_data-conv_fact = ls_lote-umrev. item_data-dlv_qty = ls_lote-lfimg. item_data-dlv_qty_imunit = ls_lote-lgmng . item_data-batch = ls_lote-charg. item_data-hieraritem = ti_lips-posnr. "The batch split record below delivery item hierary item_data-usehieritm = '1'. APPEND item_data. CLEAR item_data. item_control-deliv_numb = ti_lips-vbeln. item_control-deliv_item = ls_lote-posnn. item_control-chg_delqty = 'X'. APPEND item_control. CLEAR item_control. ENDLOOP. IF sy-subrc <> 0. item_data-dlv_qty = ti_lips-lfimg. item_data-dlv_qty_imunit = ti_lips-lgmng . ENDIF. item_data-deliv_numb = ti_lips-vbeln. item_data-deliv_item = ti_lips-posnr. item_data-fact_unit_nom = ti_lips-umvkz . item_data-fact_unit_denom = ti_lips-umvkn. item_data-conv_fact = ti_lips-umrev. READ TABLE gt_lote INTO ls_lote WITH KEY posnn = ti_lips-posnr. item_data-batch = ls_lote-charg. * PERFORM buscar_lote USING p_vbeln_factura ti_lips CHANGING ITEM_DATA-batch. APPEND item_data. CLEAR item_data. *item_control item_control-deliv_numb = ti_lips-vbeln. item_control-deliv_item = ti_lips-posnr. item_control-chg_delqty = 'X'. APPEND item_control. CLEAR item_control. ENDLOOP. CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE' EXPORTING header_data = header_data header_control = header_control delivery = delivery TABLES item_data = item_data item_control = item_control return = p_return EXCEPTIONS OTHERS = 1. ENDFORM. " CLEAR_ENTREGA
Crear pedido con referencia VA01
FUNCTION z_sd_crea_pedido_con_ref_fact. *"---------------------------------------------------------------------- *"*"Interfase local *" IMPORTING *" REFERENCE(I_VBELN) TYPE VBRK-VBELN *" REFERENCE(I_AUART) TYPE VBAK-AUART *" REFERENCE(I_SAVE) TYPE XFELD DEFAULT 'X' *" REFERENCE(I_AUGRU) TYPE VBAK-AUGRU *" EXPORTING *" REFERENCE(E_VBELN) TYPE VBAK-VBELN *" REFERENCE(E_VBELN_ENTREGA) TYPE LIKP-VBELN *" REFERENCE(E_RETURN) TYPE BAPIRET2_T *"---------------------------------------------------------------------- DATA ld_mode(1) VALUE 'N'. " 'N'. DATA lt_mtxt LIKE TABLE OF bdcmsgcoll WITH HEADER LINE. DATA ls_return TYPE bapiret2. CLEAR: e_return[], bdcdata[], lt_mtxt[], e_vbeln. IF i_save <> 'X'. ld_mode = 'E'. ENDIF. SELECT SINGLE vkbur INTO @DATA(l_vkbur) FROM vbrp WHERE vbeln EQ @i_vbeln. PERFORM bdc_dynpro USING 'SAPMV45A' '0101'. PERFORM bdc_field USING 'BDC_OKCODE' '=COPY'. PERFORM bdc_field USING 'VBAK-AUART' i_auart. PERFORM bdc_field USING 'VBAK-VKORG' ''. PERFORM bdc_field USING 'VBAK-VTWEG' ''. PERFORM bdc_field USING 'VBAK-SPART' ''. PERFORM bdc_field USING 'VBAK-VKBUR' l_vkbur. PERFORM bdc_dynpro USING 'SAPLV45C' '0100'. PERFORM bdc_field USING 'BDC_OKCODE' '=UEBR'. PERFORM bdc_field USING 'VBRK-VBELN' i_vbeln. IF i_save = 'X'. * PERFORM bdc_dynpro USING 'SAPMV45A' '4001'. * PERFORM bdc_field USING 'BDC_OKCODE' * '/00'. PERFORM bdc_dynpro USING 'SAPMV45A' '4001'. PERFORM bdc_field USING 'BDC_OKCODE' '=HEAD'. if i_augru is NOT INITIAL. PERFORM bdc_field USING 'VBAK-AUGRU' i_augru. endif. PERFORM bdc_dynpro USING 'SAPMV45A' '4002'. PERFORM bdc_field USING 'BDC_OKCODE' '=T\10'. PERFORM bdc_dynpro USING 'SAPMV45A' '4002'. PERFORM bdc_field USING 'BDC_OKCODE' '=SICH'. PERFORM bdc_field USING 'VBKD-BSARK' '1'. ENDIF. CALL TRANSACTION 'VA01' USING bdcdata MODE ld_mode UPDATE 'S' MESSAGES INTO lt_mtxt. IF i_auart = 'ZD02'. * &1 &2 se ha grabado (se ha creado entrega &3) READ TABLE lt_mtxt WITH KEY msgnr = '260' msgid = 'V1'. ELSE. * &1 &2 se ha grabado READ TABLE lt_mtxt WITH KEY msgnr = '311' msgid = 'V1'. ENDIF. IF sy-subrc EQ 0. e_vbeln = lt_mtxt-msgv2. e_vbeln_entrega = lt_mtxt-msgv3. CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING input = e_vbeln IMPORTING output = e_vbeln. CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING input = e_vbeln_entrega IMPORTING output = e_vbeln_entrega. MOVE lt_mtxt-msgid TO ls_return-id. MOVE lt_mtxt-msgtyp TO ls_return-type. MOVE lt_mtxt-msgnr TO ls_return-number. MOVE lt_mtxt-msgv1 TO ls_return-message_v1. MOVE lt_mtxt-msgv2 TO ls_return-message_v2. MOVE lt_mtxt-msgv3 TO ls_return-message_v3. MOVE lt_mtxt-msgv4 TO ls_return-message_v4. MESSAGE ID lt_mtxt-msgid TYPE lt_mtxt-msgtyp NUMBER lt_mtxt-msgnr WITH lt_mtxt-msgv1 lt_mtxt-msgv2 lt_mtxt-msgv3 lt_mtxt-msgv4 INTO ls_return-message. APPEND ls_return TO e_return. ELSE. LOOP AT lt_mtxt. MOVE lt_mtxt-msgid TO ls_return-id. MOVE lt_mtxt-msgtyp TO ls_return-type. MOVE lt_mtxt-msgnr TO ls_return-number. MOVE lt_mtxt-msgv1 TO ls_return-message_v1. MOVE lt_mtxt-msgv2 TO ls_return-message_v2. MOVE lt_mtxt-msgv3 TO ls_return-message_v3. MOVE lt_mtxt-msgv4 TO ls_return-message_v4. MESSAGE ID lt_mtxt-msgid TYPE lt_mtxt-msgtyp NUMBER lt_mtxt-msgnr WITH lt_mtxt-msgv1 lt_mtxt-msgv2 lt_mtxt-msgv3 lt_mtxt-msgv4 INTO ls_return-message. APPEND ls_return TO e_return. ENDLOOP. ENDIF. ENDFUNCTION. * Batchinputdata of single transaction DATA: BDCDATA LIKE BDCDATA OCCURS 0 WITH HEADER LINE. * Nodata-Character DATA: NODATA_CHARACTER VALUE '/'. *----------------------------------------------------------------------* * Start new screen * *----------------------------------------------------------------------* FORM BDC_DYNPRO USING PROGRAM DYNPRO. CLEAR BDCDATA. BDCDATA-PROGRAM = PROGRAM. BDCDATA-DYNPRO = DYNPRO. BDCDATA-DYNBEGIN = 'X'. APPEND BDCDATA. ENDFORM. *----------------------------------------------------------------------* * Insert field * *----------------------------------------------------------------------* FORM BDC_FIELD USING FNAM FVAL. IF fval <> NODATA_CHARACTER. CLEAR BDCDATA. BDCDATA-FNAM = FNAM. BDCDATA-FVAL = FVAL. APPEND BDCDATA. ENDIF. ENDFORM.