SAP MM BAPI

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

Orden de Compra ME21N

Crear Material MM01

Creación de vista calidad materiales

Crear Lista de Material CS01

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

Crear entrega de salida según pedido(VL01N), contabilizarla y realizar picking

form entrega_cliente_final USING p_pedido p_oc CHANGING p_entrega
                                                        p_contabilizado_cli.
* VL01N
  DATA: lt_sales_order    TYPE TABLE OF bapidlvreftosalesorder WITH HEADER LINE,
        lt_return type TABLE OF bapiret2,
        lt_ekpo type TABLE OF ekpo WITH HEADER LINE,
        ls_return like LINE OF lt_return,
        l_delivery       TYPE  bapishpdelivnumb-deliv_numb,
        l_num_deliveries TYPE  bapidlvcreateheader-num_deliveries,
        l_VSTEL type VSTEL.

  if p_entrega is INITIAL.
    lt_sales_order-ref_doc = p_pedido.
    lt_sales_order-REF_ITEM = wa_data-posnr.
    APPEND lt_sales_order.

    SELECT SINGLE vstel into l_VSTEL from vbap where vbeln = wa_data-vbeln and
                                                   posnr = wa_data-posnr.

* crear la entrega
    CALL FUNCTION 'BAPI_OUTB_DELIVERY_CREATE_SLS'
      EXPORTING
        SHIP_POINT              = l_vstel
      IMPORTING
        delivery                = l_delivery
        num_deliveries          = l_num_deliveries
      TABLES
        sales_order_items       = lt_sales_order
        return                  = lt_return
        .

  IF l_delivery IS INITIAL.
    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 'Entrega Cliente Final:' icon_red_light.
     return.
   ENDIF.

  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 'Entrega Cliente Final:' icon_green_light.
  endif.
  else.
    l_delivery = p_entrega.
  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.

   check l_delivery is NOT INITIAL.

* modificar entrega
    REFRESH lt_lips.
* si no tiene lote => actualizar
    select * into TABLE lt_lips from lips where vbeln = l_delivery and charg = space.
    IF sy-subrc = 0.
      LOOP AT lt_lips.
        select COUNT(*) from marc where matnr = lt_lips-matnr and
                                        werks = lt_lips-werks and
                                        xchar = 'X'.  "sujeto a lote
        check sy-subrc = 0.
        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_data-batch           = p_oc.   "lote => pedido de traslado
        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.

        lt_item_control-deliv_numb   = lt_lips-vbeln.
        lt_item_control-deliv_item   = lt_lips-posnr.
        lt_item_control-chg_delqty   = 'X'.

        APPEND lt_item_spl.
        APPEND lt_item_control.
        APPEND lt_item_data.
      ENDLOOP.

*—–> modificar la entrega
      if lt_item_data[] is NOT INITIAL.
        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 INTO ls_return WHERE TYPE = 'E'.
          MOVE ls_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.
          RETURN.   "------------------------->
        ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          WAIT = 'X'.
        ENDIF.
      ENDIF.
    ENDIF.

**********************************************************************
***picking
**********************************************************************
   data: lt_vbpok type TABLE OF vbpok WITH HEADER LINE,
         lt_prot  type TABLE OF prott WITH HEADER LINE,
         ls_vbkok like vbkok,
         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.

    REFRESH lt_return.

    select * into TABLE lt_ekpo from ekpo where ebeln = p_oc.

    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
      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-taqui = 'X'.   "confirmar picking
      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 'Entrega Cliente Final:' icon_red_light.
      ENDLOOP.

    ELSE.
      MESSAGE S000(0k) WITH  'Se contabilizó la entrega' l_delivery into g_message.
      PERFORM add_news USING 'Entrega Cliente Final:' icon_green_light.
      p_contabilizado_cli = 'X'.

    ENDIF.
endform.