SAP MM BAPI

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

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.

Crear una entrega de Pedido de traslado VL10B, contabilizarla y realizar picking

 *&---------------------------------------------------------------------*
 *&      Form  CREAR_ENTREGA
 *&---------------------------------------------------------------------*
 *     Crear una entrega de Pedido de traslado VL10B
 *----------------------------------------------------------------------*
 *  -->  p1        text
 *  <--  p2        text
 *----------------------------------------------------------------------*
 FORM CREAR_ENTREGA using p_ebeln
                 CHANGING p_entrega
                          p_cont.
 * Crear una entrega de Pedido de traslado VL10B
   DATA: begin of lt_ekpo occurs 1.
           include structure ekpo.
   data n_doc type i.
   data end of lt_ekpo.
 
   data : begin of lt_entregas occurs 1,
     vbeln type vbeln,
     num type i,
   end of lt_entregas.
 
   data: lt_stock_trans_items like BAPIDLVREFTOSTO occurs 0 with header line,
         lt_return            like BAPIRET2 occurs 0 with header line,
         l_ship_point         like BAPIDLVCREATEHEADER-SHIP_POINT,
         l_due_date           like BAPIDLVCREATEHEADER-DUE_DATE,
         l_delivery           like BAPISHPDELIVNUMB-DELIV_NUMB.
 
   data: lt_DELIVERIES        like BAPISHPDELIVNUMB occurs 0 with header line,
         lt_CREATED_ITEMS     like BAPIDLVITEMCREATED occurs 0 with header line.
   data: ls_vbkok             like vbkok,
         lt_vbpok             like vbpok occurs 0 with header line,
         lt_prot              like prott occurs 0 with header line,
 
         l_error_any_0              type c length 1,
         l_error_in_item_deletion_0 type c length 1,
         l_error_in_pod_update_0    type c length 1,
         l_error_in_interface_0     type c length 1,
         l_error_in_goods_issue_0   type c length 1,
         l_error_in_final_check_0   type c length 1,
         l_error_partner_update     type c length 1,
         l_error_sernr_update       type c length 1.
 
   SELECT SINGLE vstel
     FROM   ekpv
     INTO   l_ship_point
     WHERE  ebeln EQ p_ebeln.
 
   SELECT *
     FROM ekpo UP TO 1 ROWS
     INTO table lt_ekpo
     WHERE ebeln EQ p_ebeln
     ORDER BY ebelp.
 
 **********************************************************************
 * crear entrega
 **********************************************************************
   IF p_entrega IS INITIAL.
 
     REFRESH lt_stock_trans_items.
 
     LOOP AT lt_ekpo.
       CLEAR lt_stock_trans_items.
       lt_stock_trans_items-ref_doc    = lt_ekpo-ebeln.
       lt_stock_trans_items-ref_item   = lt_ekpo-ebelp.
       lt_stock_trans_items-dlv_qty    = lt_ekpo-menge.
       lt_stock_trans_items-sales_unit = lt_ekpo-meins.
       APPEND lt_stock_trans_items.
     ENDLOOP.
 
     CALL FUNCTION 'BAPI_OUTB_DELIVERY_CREATE_STO'
     EXPORTING
       ship_point        = l_ship_point
       due_date          = sy-datum
     IMPORTING
       delivery          = l_delivery
     TABLES
       stock_trans_items = lt_stock_trans_items
       DELIVERIES        = lt_DELIVERIES
       CREATED_ITEMS     = lt_CREATED_ITEMS
       RETURN            = lt_return.
 
     IF l_delivery IS INITIAL.
       LOOP AT lt_return WHERE TYPE EQ 'E'.
         MESSAGE ID lt_return-ID TYPE 'I' NUMBER lt_return-NUMBER WITH
         lt_return-MESSAGE_V1
         lt_return-MESSAGE_V2
         lt_return-MESSAGE_V3
         lt_return-MESSAGE_V4 INTO g_message.
 
         PERFORM add_news USING 'Creación de entrega:' icon_red_light.
         wa_data-stat =  icon_red_light.
 
       ENDLOOP.
 
     ELSE.
       CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
       EXPORTING
         WAIT = 'X'.
       .
       p_entrega = l_delivery.
       MESSAGE s000(0k) WITH   l_delivery 'creada con éxito' INTO g_message.
       PERFORM add_news USING 'Creación de entrega:' icon_green_light.
       wa_data-stat =  icon_green_light.
     ENDIF.
 
 *   Entrega creada asigna en ITAB.
     LOOP AT lt_CREATED_ITEMS.
       lt_entregas-vbeln = lt_CREATED_ITEMS-DELIV_NUMB.
       lt_entregas-num = 1.
       COLLECT lt_entregas.
     ENDLOOP.
 
   ELSE.
     l_delivery = p_entrega.
     lt_entregas-vbeln = l_delivery.
     lt_entregas-num = 1.
     COLLECT lt_entregas.
   ENDIF.
 
 *--------------------------------------------------------------------*
 * contabiliza SM & picking entregas.
 *--------------------------------------------------------------------*
   DATA lt_return2 TYPE bapiret2_t.
   DATA lt_lips TYPE TABLE OF lips WITH HEADER LINE.
   DATA: ls_data           TYPE bapiobdlvhdrchg,
         ls_control        TYPE bapiobdlvhdrctrlchg,
         lt_item_control   TYPE TABLE OF  bapiobdlvitemctrlchg WITH HEADER LINE,
         lt_item_data      TYPE TABLE OF bapiobdlvitemchg WITH HEADER LINE,
         lt_item_spl       TYPE TABLE OF /spe/bapiobdlvitemchg WITH HEADER LINE,
         ls_techn_control  TYPE  bapidlvcontrol.
 
   loop at lt_entregas.
     REFRESH lt_return.
 
 * modificar entrega
     REFRESH lt_lips.
 * si no tiene almacen => actualizar con almacén de origen
     select * into TABLE lt_lips from lips where vbeln = l_delivery and lgort = space.
     IF sy-subrc = 0.
       LOOP AT lt_lips.
         lt_item_spl-deliv_numb      = lt_lips-vbeln.
         lt_item_spl-deliv_item      = lt_lips-posnr.
         lt_item_control-deliv_numb  = lt_lips-vbeln.
         lt_item_control-deliv_item  = lt_lips-posnr.
         lt_item_data-deliv_numb     = lt_lips-vbeln.
         lt_item_data-deliv_item     = lt_lips-posnr.
         lt_item_data-dlv_qty        = lt_lips-lfimg.
         lt_item_spl-stge_loc        = wa_data-lgort_ori. "almacén de origen
         lt_item_data-material       = lt_lips-matnr.
         lt_item_data-fact_unit_nom  = lt_lips-umvkz.
         lt_item_data-fact_unit_denom  = lt_lips-umvkn.
         lt_item_data-base_uom         = lt_lips-meins.
         lt_item_data-sales_unit       = lt_lips-vrkme.
 
         APPEND lt_item_spl.
         APPEND lt_item_control.
         APPEND lt_item_data.
       ENDLOOP.
 
 *—–> modificar la entrega
       ls_data-deliv_numb         = l_delivery.
       ls_control-deliv_numb      = l_delivery.
       ls_techn_control-upd_ind   = 'U'.
 
       CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE'
       EXPORTING
         header_data                   = ls_data
         header_control                = ls_control
         delivery                      = l_delivery
         techn_control                 = ls_techn_control
       TABLES
         item_data                     = lt_item_data[]
         item_control                  = lt_item_control[]
         RETURN                        = lt_return
         item_data_spl                 = lt_item_spl[]
         .
 
       LOOP AT lt_return WHERE TYPE = 'E'.
         MOVE lt_return-MESSAGE TO g_message.
         PERFORM add_news USING 'Modif. Almacén de Entrega:' icon_red_light.
         wa_data-stat =  icon_red_light.
         EXIT.
       ENDLOOP.
       IF sy-subrc = 0.
         CONTINUE.
       ELSE.
         CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
         EXPORTING
           WAIT = 'X'.
       ENDIF.
     ENDIF.
 
     l_delivery = lt_entregas-vbeln.
     ls_vbkok-vbeln_vl = l_delivery.
     ls_vbkok-wabuc    = 'X'.   "contabilizar Salida merc.
 
     SELECT vbeln AS vbeln_vl
            posnr AS posnr_vl
            matnr
            charg
            lfimg
            lgmng
            umvkz
            umvkn
            vrkme
            meins
            werks FROM lips
                  INTO CORRESPONDING FIELDS OF TABLE lt_vbpok
                  WHERE vbeln EQ l_delivery.
 
     LOOP AT lt_vbpok.
       lt_vbpok-vbeln       = lt_vbpok-vbeln_vl.
       lt_vbpok-posnn       = lt_vbpok-posnr_vl.
       lt_vbpok-lgort       = wa_data-lgort_ori. "lt_ekpo-lgort. "p_lgort.
       lt_vbpok-mdiff_lgort = wa_data-lgort_ori.  "lt_ekpo-lgort. "p_lgort.
 
       MODIFY lt_vbpok.
     ENDLOOP.
 
     CALL FUNCTION 'WS_DELIVERY_UPDATE_2'
       EXPORTING
         vbkok_wa                  = ls_vbkok
 *        commit                    = 'X'
         delivery                  = l_delivery
         update_picking            = 'X'
         if_error_messages_send    = ' '
       IMPORTING
         ef_error_any              = l_error_any_0
         ef_error_in_item_deletion = l_error_in_item_deletion_0
         ef_error_in_pod_update    = l_error_in_pod_update_0
         ef_error_in_interface     = l_error_in_interface_0
         ef_error_in_goods_issue   = l_error_in_goods_issue_0
         ef_error_in_final_check   = l_error_in_final_check_0
         ef_error_partner_update   = l_error_partner_update
         ef_error_sernr_update     = l_error_sernr_update
       TABLES
         vbpok_tab                 = lt_vbpok
         prot                      = lt_prot.
 
     IF l_error_any_0 EQ 'X'.
       LOOP AT lt_prot where msgty eq 'E'.
         MESSAGE ID     lt_prot-msgid
                 TYPE   'I'
                 NUMBER lt_prot-msgno
                 WITH   lt_prot-msgv1
                        lt_prot-msgv2
                        lt_prot-msgv3
                        lt_prot-msgv4 into g_message.
         PERFORM add_news USING 'Contabilización de entrega:' icon_red_light.
         wa_data-stat =  icon_red_light.
 *        error = 'X'.
       ENDLOOP.
 
     ELSE.
       COMMIT WORK AND WAIT.
       MESSAGE S000(0k) WITH  'Se contabilizó la entrega' l_delivery into g_message.
       PERFORM add_news USING 'Contabilización de entrega:' icon_green_light.
       wa_data-stat =  icon_green_light.
       p_cont = 'X'.
     ENDIF.
 
   endloop.
 ENDFORM.                    " CREAR_ENTREGA

BAPI_GOODSMVT_CREATE entrada de mercancía para pedido de traslado MIGO con referencia a entrega

 form recepcion USING p_oc p_entrega CHANGING p_materialdocument
                                              p_MATDOCUMENTYEAR.
 * entrada de mercancía para pedido de traslado MIGO con referencia
 * a entrega
 
  DATA: lt_item TYPE TABLE OF  bapi2017_gm_item_create,
        lt_ekpo TYPE TABLE OF ekpo,
        lt_return TYPE TABLE OF bapiret2.
 
  data: ls_ekpo TYPE ekpo,
        ls_header LIKE  bapi2017_gm_head_01,
        ls_code LIKE  bapi2017_gm_code,
        ls_item LIKE LINE OF lt_item,
        ls_return LIKE LINE OF lt_return.
 
  DATA: l_materialdocument TYPE  bapi2017_gm_head_ret-mat_doc,
        l_MATDOCUMENTYEAR TYPE  BAPI2017_GM_HEAD_RET-DOC_YEAR,
        l_testrun TYPE testrun VALUE space.
 
  CLEAR: lt_item[],
         ls_header,
         ls_code,
         l_materialdocument.
 
  SELECT * FROM ekpo INTO TABLE lt_ekpo
                          WHERE ebeln = p_oc.
  IF sy-subrc = 0.
     ls_header-pstng_date = sy-datum.
     ls_header-doc_date   = sy-datum.
     ls_header-ref_doc_no = p_entrega.
     ls_header-header_txt = wa_data-vbeln.
     ls_code-gm_code      = '01'. "MB1C ver tabla T158G
 
     LOOP AT lt_ekpo INTO ls_ekpo.
       CLEAR ls_item.
       ls_item-move_type  = '101'.
       ls_item-material   = ls_ekpo-matnr.
       ls_item-plant      = ls_ekpo-werks.
       ls_item-stge_loc   = ls_ekpo-lgort.
 
 * si material esta sujeto a lote => lote = pedido de traslado
       SELECT COUNT(*) FROM MARc WHERE MATNR = ls_ekpo-matnr
       AND WERKS = ls_ekpo-werks
       AND XCHAR = 'X'.
       IF sy-subrc = 0.
         ls_item-batch = p_oc.
       ENDIF.
       ls_item-entry_qnt  = ls_ekpo-menge.
       ls_item-po_number  = ls_ekpo-ebeln.
       ls_item-po_item    = ls_ekpo-ebelp.
       ls_item-val_type   = ls_ekpo-bwtar.
       ls_item-mvt_ind    = 'B'. "indicador de movimiento
       ls_item-DELIV_NUMB = p_entrega.
       APPEND ls_item TO lt_item.
     ENDLOOP.
 
     CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
     EXPORTING
       goodsmvt_header  = ls_header
       goodsmvt_code    = ls_code
       testrun          = l_testrun
     IMPORTING
       materialdocument = l_materialdocument
       MATDOCUMENTYEAR  = l_MATDOCUMENTYEAR
     TABLES
       goodsmvt_item    = lt_item
       RETURN           = lt_return
       .
     IF lt_return[] IS INITIAL.
 * proceso OK
       p_materialdocument = l_materialdocument.
       p_MATDOCUMENTYEAR  = l_MATDOCUMENTYEAR.
 
       MESSAGE S000(0k) WITH 'Se creo documento de material'
       l_materialdocument l_MATDOCUMENTYEAR INTO g_message.
       PERFORM add_news USING 'Recepción:' icon_green_light.
 * commit work
       CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
       EXPORTING
         WAIT = 'X'.
    ELSE.
 * 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>).
           EXIT.
         ENDLOOP.
         EXIT.
       ENDLOOP.
       IF sy-subrc = 0.
         MOVE <ls_return>-MESSAGE TO g_message.
         PERFORM add_news USING 'Recepción:' icon_red_light.
          wa_data-stat =  icon_red_light.
       ENDIF.
 
     ENDIF.
  ELSE.
  ENDIF.
 ENDFORM. " f_entrada_merc_pedido