SAP MM BAPI

From SapWiki
Revision as of 15:20, 4 April 2020 by WikiSysop (talk | contribs) (Created page with "==BAPI_PO_CREATE1== <nowiki> form crear_pedido CHANGING p_oc. * Rutina que crea los pedidos de traslado (ME21N) DATA: ls_header TYPE bapimepoheader, ls_h...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

BAPI_PO_CREATE1

 form crear_pedido CHANGING p_oc.
 *  Rutina que crea los pedidos de traslado (ME21N)
 
   DATA: ls_header     TYPE bapimepoheader,
         ls_headerx    TYPE bapimepoheaderx,
         lt_items      TYPE STANDARD TABLE OF bapimepoitem,
         ls_items      TYPE bapimepoitem,
         lt_itemsx     TYPE STANDARD TABLE OF bapimepoitemx,
         ls_itemsx     TYPE bapimepoitemx,
         lt_schedules  TYPE STANDARD TABLE OF bapimeposchedule,
         ls_schedules  TYPE bapimeposchedule,
         lt_schedulesx TYPE STANDARD TABLE OF bapimeposchedulx,
         ls_schedulesx TYPE bapimeposchedulx,
         lt_return     TYPE TABLE OF bapiret2,
         ls_return     TYPE bapiret2,
         l_item_idx    TYPE I,
         l_numped      TYPE bapiekkoc-po_number.
 
   CLEAR: ls_header,
          ls_headerx.
 
   ls_header-doc_type   = 'ZXXX'.
 
   SELECT SINGLE EKORG EKGRP BUKRS lifnr INTO
   (ls_header-purch_org,ls_header-pur_group,ls_header-comp_code,ls_header-VENDOR)
   FROM ekko WHERE ebeln = wa_data-zzcontrato.
 
   ls_header-suppl_plnt = wa_data-werks_ori.
   ls_header-doc_date   = sy-datum.
   ls_header-REF_1      = wa_data-vbeln.
 
   ls_headerx-doc_type   = 'X'.
   ls_headerx-purch_org  = 'X'.
   ls_headerx-pur_group  = 'X'.
   ls_headerx-comp_code  = 'X'.
   ls_headerx-suppl_plnt = 'X'.
   ls_headerx-doc_date   = 'X'.
   ls_headerX-REF_1      = 'X'.
   ls_headerX-VENDOR     = 'X'.
 
 *Grabamos las tablas correspondientes a los materiales y repartos
   l_item_idx          = wa_data-posnr.
   ls_items-po_item    = l_item_idx.
   ls_items-material   = wa_data-matnr.
   ls_items-plant      = wa_data-werks_sumi.
   ls_items-stge_loc   = wa_data-lgort_sumi.
 *ls_items-item_cat    = '7'.
   ls_items-po_unit    = wa_data-VRKME.
   ls_items-TRACKINGNO = wa_data-posnr.  "wa_data-vbeln.   "N° necesidad
   ls_items-QUANTITY   = wa_data-KWMENG.
 
   ls_items-agreement  = wa_data-zzcontrato.
   select SINGLE * into @data(ls_ekpo) from ekpo
                                where ebeln = @wa_data-zzcontrato
                                  and LOEKZ <> 'X'.
   ls_items-agmt_item = ls_ekpo-ebelp.
 
   select SINGLE NETWR INTO ls_items-net_price from vbap
                                where vbeln = wa_data-vbeln and
                                      posnr = wa_data-posnr.
 *ls_items-no_rounding = 'X'.
   APPEND ls_items TO lt_items.
 
   ls_itemsx-po_item     = l_item_idx.
   ls_itemsx-po_itemx    = 'X'.
   ls_itemsx-material    = 'X'.
   ls_itemsx-plant       = 'X'.
   ls_itemsx-stge_loc    = 'X'.
 *ls_itemsx-item_cat    = 'X'.
   ls_itemsx-po_unit     = 'X'.
 *ls_itemsx-no_rounding = 'X'.
   ls_itemsx-TRACKINGNO  = 'X'.
   ls_itemsX-QUANTITY    = 'X'.
   ls_itemsX-agreement   = 'X'.
   ls_itemsX-agmt_item   = 'X'.
   ls_itemsX-net_price   = 'X'.
   APPEND ls_itemsx TO lt_itemsx.
 
   ls_schedules-po_item       = l_item_idx.
   ls_schedules-quantity      = wa_data-KWMENG.
   ls_schedules-delivery_date = wa_data-edatu.
   APPEND ls_schedules TO lt_schedules.
 
   ls_schedulesx-po_item       = l_item_idx.
   ls_schedulesx-po_itemx      = 'X'.
   ls_schedulesx-quantity      = 'X'.
   ls_schedulesx-delivery_date = 'X'.
   APPEND ls_schedulesx TO lt_schedulesx.
 
 * Llamamos a la BAPI para crear el pedido de traslados
   CALL FUNCTION 'BAPI_PO_CREATE1'
   EXPORTING
     poheader               = ls_header
     poheaderx              = ls_headerx
   IMPORTING
     exppurchaseorder       = l_numped
   TABLES
     RETURN                 = lt_return
     poitem                 = lt_items
     poitemx                = lt_itemsx
     poschedule             = lt_schedules
     poschedulex            = lt_schedulesx
     .
 
   if l_numped is INITIAL.
 * error
    LOOP AT lt_return INTO ls_return
     GROUP BY COND string( WHEN ls_return-type = 'E' THEN |EQ 1|
                           WHEN ls_return-type = 'A' THEN |EQ 2|
                           WHEN ls_return-type = 'W' THEN |EQ 3|
                           WHEN ls_return-type = 'I' THEN |EQ 4|
                           )
               ASCENDING
               ASSIGNING FIELD-SYMBOL(<group>).
      LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<ls_return>).
      ENDLOOP.
      EXIT.
    ENDLOOP.
    IF sy-subrc = 0.
       move <ls_return>-message to g_message.
       PERFORM add_news USING 'Crear Pedido:' icon_red_light.
       wa_data-stat =  icon_red_light.
    ENDIF.
   else.
 * éxito
       CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
       EXPORTING
         WAIT = 'X'.
 
       p_oc = l_numped.
 
 * Pedido grabado correctamente, lo informamos en el log
         MESSAGE i000(0K) WITH 'Pedido grabado correctamente' l_numped INTO g_message.
         PERFORM add_news USING 'Crear Pedido:' icon_green_light.
         wa_data-stat =  icon_green_light.
   endif.
 
   CLEAR: lt_items, lt_schedules, lt_itemsx, lt_schedulesx.
   REFRESH: lt_items, lt_schedules, lt_itemsx, lt_schedulesx.
 
 ENDFORM.