SAP SD BAPI

From SapWiki
Revision as of 17:07, 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.

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.

Crear entrega con referencia

 FORM crear_entrega_2 USING i_vbeln.  
  DATA: lt_request like table of bapideliciousrequest with header line,
        lt_items like table of bapideliciouscreateditems with header line,
        lt_return like table of bapiret2 with header line,
        lt_vbap LIKE TABLE OF vbap WITH HEADER LINE,
        ls_vbak LIKE vbak.
 
  SELECT SINGLE * INTO ls_vbak FROM vbak
                             WHERE vbeln = i_vbeln.
  CHECK sy-subrc EQ 0.

  SELECT * INTO TABLE lt_vbap FROM vbap
                             WHERE vbeln = i_vbeln.

  LOOP AT lt_vbap.
    CLEAR lt_request.
    lt_request-document_numb = lt_vbap-vbeln.
    lt_request-document_item = lt_vbap-posnr.
    lt_request-ship_to = ls_vbak-kunnr.
    lt_request-sold_to = ls_vbak-kunnr.
    lt_request-sales_organisation = ls_vbak-vkorg.
    lt_request-distribution_channel = ls_vbak-vtweg.
    lt_request-division = ls_vbak-spart.
    lt_request-plant = lt_vbap-werks.
    lt_request-quantity_sales_uom = lt_vbap-KWMENG.
    lt_request-quantity_base__uom = lt_vbap-KWMENG.
    lt_request-sales_unit = lt_vbap-vrkme.
    lt_request-base_uom = lt_vbap-meins.
    lt_request-material = lt_vbap-matnr.
    lt_request-delivery_date = ls_vbak-bstdk.
    lt_request-transp_plan_date = ls_vbak-bstdk.
    lt_request-loading_date = ls_vbak-bstdk.
    lt_request-goods_issue_date = ls_vbak-bstdk.
    lt_request-document_type = 'A'. "Delivery
    lt_request-document_type_predecessor = 'A'. "Sales Ord
    lt_request-document_type_delivery = 'LF'.

    APPEND lt_request.
  ENDLOOP.

  CALL FUNCTION 'BAPI_DELIVERYPROCESSING_EXEC'
    TABLES
      request      = lt_request
      createditems = lt_items
      return       = lt_return.

  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
   EXPORTING
     wait          = 'X'
 * IMPORTING
 *   RETURN        =
            .
 endform.

Crear Entrega Sin Referencia

* BAPI for VL01NO
 form DELIVERY_CREATENOREF using i_vbeln
                                i_werks
                                i_vstel
                                i_commit
                                i_dlv_items type any TABLE
                       changing o_vbeln
                                o_created_items type ANY TABLE
                                o_return type BAPIRET2_T
                                of_rc type sysubrc.
  data: ls_likp type likp,
        lt_lips type table of lips,
        ls_lips like line of lt_lips,
        ls_t001w like t001w.

  data: lt_dates type table of BAPIDLVDEADLN,
        ls_dates like line of lt_dates,
        lt_DLV_ITEMS type table of BAPIDLVNOREFITEM,
        ls_dlv_items like line of lt_DLV_ITEMS.

  data: ls_return type bapiret2,
        lt_return type table of bapiret2,
        ld_par1 type SY-MSGV1 ,
        ld_par2 type SY-MSGV1 .

  data: lt_CREATED_ITEMS like TABLE OF  BAPIDLVITEMCREATED,
        ls_created_items LIKE LINE OF lt_CREATED_ITEMS.
  data ld_vbtyv like lips-vbtyv.

  data: ld_prctr type prctr,
        ld_kostl like zsdceco_centro-kostl,
        ld_werks like lips-werks,
        ld_werks_o like lips-werks.

  clear of_rc.
  clear o_vbeln.

  select single * into ls_likp from likp
                              where vbeln = i_vbeln.
  if sy-subrc ne 0.
 * error entrega no existe
    move i_vbeln to ld_par1.

    CALL FUNCTION 'BALW_BAPIRETURN_GET2'
      EXPORTING
        TYPE   = 'E'
        CL     = 'VL'
        NUMBER = 559
        PAR1   = ld_par1
      IMPORTING
        RETURN = ls_return.

    append ls_return to o_return.
    of_rc = 1.
    return.
  endif.

  select * into table lt_lips from lips
                             where vbeln = i_vbeln.
 ***
  select single * into ls_t001w from T001W where WERKS = I_WERKS.

 ***
  ls_dates-timetype = 'WSHDRWADAT'. "WS GOODS ISSUE

  data ls_time(14).
  concatenate sy-datlo sy-timlo into ls_time.

  move ls_time to ls_dates-timestamp_utc.
  ls_dates-timezone = 'CHILE'.

  append ls_dates to lt_dates.

  if i_dlv_items[] is not initial.
 * si las posiciones son entregadas=>tomar, si no,
 * tomar todas las posiciones de la entrega
    FIELD-SYMBOLS <wa> type any.

    loop at i_dlv_items ASSIGNING <wa>.
      MOVE-CORRESPONDING <wa> to ls_dlv_items.
      append ls_dlv_items to lt_DLV_ITEMS.
    ENDLOOP.
  else.
    loop at lt_lips into ls_lips.
 *DATES -TIMESTAMP_UTC = SY-UNAME
      ls_DLV_ITEMS-REF_ITem = ls_LIPS-POSNR.
      ls_DLV_ITEMS-MATERIAL  = ls_LIPS-MATNR.
      ls_DLV_ITEMS-ITEM_categ = 'ZREF'.
      ls_DLV_ITEMS-DLV_QTY = ls_LIPS-LFIMG.
      ls_DLV_ITEMS-sales_unit = ls_lips-meins.
 *    ls_DLV_ITEMS-plant = i_werks.

      append ls_DLV_ITEMS to lt_DLV_ITEMS.
    endloop.
  endif.

 *  select single * into ls_t001w from T001W where WERKS = i_WERKS.
 *  data ld_ship_to type KUNWE.
 *  ld_ship_to = i_werks.

  CALL FUNCTION 'BAPI_OUTB_DELIVERY_CREATENOREF'
    EXPORTING
      SHIP_POINT           = i_vstel "ls_LIKP-VSTEL
      DLV_TYPE             = 'ZREF'
      SALESORG             = ls_t001w-vkorg
      DISTR_CHAN           = ls_t001w-vtweg
      DIVISION             = ls_t001w-spart
      SHIP_TO              = ls_t001w-kunnr
     DATE_USAGE           = '0'
 *     DEBUG_FLG            =
   IMPORTING
     DELIVERY             = o_vbeln
 *     NUM_DELIVERIES       =
    TABLES
      DATES                = lt_dates
      DLV_ITEMS            = lt_dlv_items
 *     SERIAL_NUMBERS       =
 *     EXTENSION_IN         =
 *     DELIVERIES           =
      CREATED_ITEMS        = lt_created_items
 *     EXTENSION_OUT        =
      RETURN               = o_return
            .
  o_created_items[] = lt_created_items[].

  if o_vbeln is not initial
    and i_commit is not initial.

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        WAIT   = 'X'
      IMPORTING
        RETURN = ls_return.

    if ls_return is INITIAL.
 * actualizar entrega creada(LIPS) con documento y pos de referencia y KOSTL
      loop at lt_CREATED_ITEMS into ls_CREATED_ITEMS .
        select single vbtyp into ld_vbtyv from likp
                             where vbeln = i_vbeln. "entrega entrada
 *                              and posnr  = ls_created_items-REF_ITem.

 * obtiene centro de entrega salida creada
        select single werks into ld_werks from lips
                                   where vbeln = o_vbeln "entrega  salida creada
                                     and posnr = ls_created_items-DELIV_ITEM.

        select single prctr into ld_prctr
                      from marc where matnr = ls_CREATED_ITEMS-material
                                 and werks  = ld_werks.
        if sy-subrc = 0.
 * obtiene centro de entrega entrada
          select single werks into ld_werks_o from lips
                           where vbeln = i_vbeln "entrega entrada
                             and posnr = ls_created_items-REF_ITEM.

          select single kostl into ld_kostl
                         from zsdceco_centro
                              where werks = ld_werks_o
                                and prctr = ld_prctr.
          if sy-subrc ne 0.
            clear ld_kostl.
          endif.
        endif.

        UPDATE lips
               set kostl   = ld_kostl
                   zzvbeln = i_vbeln
                   zzposnr = ls_created_items-REF_ITem
                   zzvbtyv = ld_vbtyv
         where vbeln = o_vbeln
            and posnr = ls_created_items-deliv_item.

      endloop.
      commit WORK AND WAIT.
    endif.

  endif.
 endform.                    "DELIVERY_CREATENOREF