SAP SD BAPI VA01

From SapWiki
Revision as of 15:10, 6 April 2020 by WikiSysop (talk | contribs) (Created page with "==Ejemplo 1== <nowiki>FORM f_pedido_venta USING i_kunnr i_werks CHANGING p_documento TYPE vbak-vbeln o_da...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Ejemplo 1

FORM f_pedido_venta USING i_kunnr
                          i_werks
                 CHANGING p_documento TYPE vbak-vbeln
                          o_datos_pedido LIKE gt_datos_pedido[].
  DATA: ls_header TYPE bapisdhd1,
        lt_partners TYPE STANDARD TABLE OF bapiparnr,
        ls_partner TYPE bapiparnr,
        lt_items TYPE STANDARD TABLE OF bapisditm,
        ls_item TYPE bapisditm,
        lt_schedule TYPE STANDARD TABLE OF bapischdl,
        ls_schedule TYPE bapischdl,
        lt_condiciones TYPE STANDARD TABLE OF bapicond,
        ls_condicion TYPE bapicond,
        lt_return TYPE STANDARD TABLE OF bapiret2,
        ls_return TYPE bapiret2,
        posicion(6) TYPE n.
  DATA detalle LIKE LINE OF o_datos_pedido.
  CLEAR: ls_header,
         lt_items[],
         lt_condiciones[],
         lt_partners[],
         lt_schedule[],
         lt_return[].

  ls_header-doc_type = 'PSER'.
  ls_header-sales_org = 'PTG'.
  ls_header-distr_chan = 'PT'.
  ls_header-division =  'PT'.
  ls_header-req_date_h = sy-datlo.
 
  ls_partner-partn_role = 'AG'.
  ls_partner-partn_numb = i_kunnr.
  APPEND ls_partner TO lt_partners.
  posicion = '0'.
  
  LOOP AT o_datos_pedido INTO detalle.
    CLEAR: ls_item,
           ls_condicion,
           ls_schedule.
    posicion = posicion + 10.
    ls_item-itm_number = posicion.
    ls_item-material = detalle-matnr.
    ls_item-short_text = detalle-maktx.
    ls_item-plant = i_werks.
    
    APPEND ls_item TO lt_items.
    ls_condicion-itm_number = posicion.
    ls_condicion-cond_type = 'PCOND'.

    DATA ld_monto LIKE bapicurr-bapicurr.
    CALL FUNCTION 'BAPI_CURRENCY_CONV_TO_EXTERNAL'
      EXPORTING
        currency        = detalle-waerk
        amount_internal = detalle-kwert
      IMPORTING
        amount_external = ld_monto.
    ls_condicion-cond_value = ld_monto.
    ls_condicion-currency = detalle-waerk.
    APPEND ls_condicion TO lt_condiciones.
    ls_schedule-itm_number = posicion.
    ls_schedule-req_qty = detalle-menge.
    APPEND ls_schedule TO lt_schedule.
  ENDLOOP.

  CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
    EXPORTING
    order_header_in               = ls_header
     behave_when_error             = space
   IMPORTING
     salesdocument                 = p_documento
  TABLES
    return                        = lt_return
    order_items_in                = lt_items
    order_partners                = lt_partners
    order_schedules_in            = lt_schedule
    order_conditions_in           = lt_condiciones.

  IF p_documento IS NOT INITIAL.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait   = 'X'
      IMPORTING
        return = ls_return.

* Pedido de Venta grabado
    MESSAGE ID 'V1' TYPE 'S' NUMBER '311'
    WITH 'Pedido de Venta'(s01)
          p_documento.
  ELSE.
    LOOP AT lt_return INTO ls_return WHERE type = 'E'.
      MESSAGE ID ls_return-id TYPE ls_return-type NUMBER ls_return-number
      WITH ls_return-message_v1
           ls_return-message_v2
           ls_return-message_v3
           ls_return-message_v4.
      EXIT.
    ENDLOOP.
  ENDIF.

ENDFORM.                    "f_pedido_venta