SAP SD BAPI

From SapWiki
Revision as of 16:01, 4 April 2020 by WikiSysop (talk | contribs)

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.