Difference between revisions of "SAP MM BAPI"

From SapWiki
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
==BAPI_PO_CREATE1==  
+
==[[SAP_MM_BAPI_OC|Orden de Compra ME21N]]==
  <nowiki> form crear_pedido CHANGING p_oc.
+
==[[SAP_MM_BAPI_MATERIAL|Crear Material MM01]]==
  * Rutina que crea los pedidos de traslado (ME21N)
+
==[[SAP_MM_BAPI_MATERIAL_QMAT|Creación de vista calidad materiales]]==
   
+
==[[SAP_MM_BAPI_BOM|Crear Lista de Material CS01]]==
  DATA: ls_header    TYPE bapimepoheader,
+
== Crear una entrega de Pedido de traslado VL10B, contabilizarla y realizar picking==
        ls_headerx    TYPE bapimepoheaderx,
+
  <nowiki>*&---------------------------------------------------------------------*
        lt_items      TYPE STANDARD TABLE OF bapimepoitem,
+
*&      Form CREAR_ENTREGA
        ls_items      TYPE bapimepoitem,
+
*&---------------------------------------------------------------------*
        lt_itemsx     TYPE STANDARD TABLE OF bapimepoitemx,
+
*    Crear una entrega de Pedido de traslado VL10B
        ls_itemsx    TYPE bapimepoitemx,
+
*----------------------------------------------------------------------*
        lt_schedules  TYPE STANDARD TABLE OF bapimeposchedule,
+
*  -->  p1        text
        ls_schedules  TYPE bapimeposchedule,
+
* <--  p2        text
        lt_schedulesx TYPE STANDARD TABLE OF bapimeposchedulx,
+
*----------------------------------------------------------------------*
        ls_schedulesx TYPE bapimeposchedulx,
+
FORM CREAR_ENTREGA using p_ebeln
        lt_return     TYPE TABLE OF bapiret2,
+
                CHANGING p_entrega
        ls_return    TYPE bapiret2,
+
                        p_cont.
        l_item_idx    TYPE I,
+
* Crear una entrega de Pedido de traslado VL10B
        l_numped      TYPE bapiekkoc-po_number.
+
  DATA: begin of lt_ekpo occurs 1.
+
          include structure ekpo.
   CLEAR: ls_header,
+
  data n_doc type i.
          ls_headerx.
+
  data end of lt_ekpo.
+
 
  ls_header-doc_type   = 'ZXXX'.
+
  data : begin of lt_entregas occurs 1,
+
    vbeln type vbeln,
  SELECT SINGLE EKORG EKGRP BUKRS lifnr INTO
+
     num type i,
  (ls_header-purch_org,ls_header-pur_group,ls_header-comp_code,ls_header-VENDOR)
+
  end of lt_entregas.
  FROM ekko WHERE ebeln = wa_data-zzcontrato.
+
 
+
  data: lt_stock_trans_items like BAPIDLVREFTOSTO occurs 0 with header line,
   ls_header-suppl_plnt = wa_data-werks_ori.
+
        lt_return            like BAPIRET2 occurs 0 with header line,
  ls_header-doc_date   = sy-datum.
+
        l_ship_point        like BAPIDLVCREATEHEADER-SHIP_POINT,
   ls_header-REF_1      = wa_data-vbeln.
+
        l_due_date          like BAPIDLVCREATEHEADER-DUE_DATE,
+
        l_delivery          like BAPISHPDELIVNUMB-DELIV_NUMB.
  ls_headerx-doc_type  = 'X'.
+
 
  ls_headerx-purch_org  = 'X'.
+
  data: lt_DELIVERIES        like BAPISHPDELIVNUMB occurs 0 with header line,
  ls_headerx-pur_group = 'X'.
+
        lt_CREATED_ITEMS     like BAPIDLVITEMCREATED occurs 0 with header line.
  ls_headerx-comp_code  = 'X'.
+
  data: ls_vbkok            like vbkok,
  ls_headerx-suppl_plnt = 'X'.
+
        lt_vbpok            like vbpok occurs 0 with header line,
  ls_headerx-doc_date   = 'X'.
+
        lt_prot              like prott occurs 0 with header line,
  ls_headerX-REF_1      = 'X'.
+
 
  ls_headerX-VENDOR     = 'X'.
+
        l_error_any_0              type c length 1,
   
+
        l_error_in_item_deletion_0 type c length 1,
  *Grabamos las tablas correspondientes a los materiales y repartos
+
        l_error_in_pod_update_0   type c length 1,
  l_item_idx          = wa_data-posnr.
+
        l_error_in_interface_0    type c length 1,
  ls_items-po_item    = l_item_idx.
+
        l_error_in_goods_issue_0  type c length 1,
  ls_items-material  = wa_data-matnr.
+
        l_error_in_final_check_0   type c length 1,
  ls_items-plant     = wa_data-werks_sumi.
+
        l_error_partner_update    type c length 1,
  ls_items-stge_loc  = wa_data-lgort_sumi.
+
        l_error_sernr_update      type c length 1.
  *ls_items-item_cat    = '7'.
+
 
  ls_items-po_unit    = wa_data-VRKME.
+
  SELECT SINGLE vstel
  ls_items-TRACKINGNO = wa_data-posnr. "wa_data-vbeln.   "N° necesidad
+
    FROM  ekpv
  ls_items-QUANTITY  = wa_data-KWMENG.
+
    INTO   l_ship_point
   
+
    WHERE  ebeln EQ p_ebeln.
  ls_items-agreement = wa_data-zzcontrato.
+
 
  select SINGLE * into @data(ls_ekpo) from ekpo
+
  SELECT *
                                where ebeln = @wa_data-zzcontrato
+
    FROM ekpo UP TO 1 ROWS
                                  and LOEKZ <> 'X'.
+
    INTO table lt_ekpo
  ls_items-agmt_item = ls_ekpo-ebelp.
+
    WHERE ebeln EQ p_ebeln
+
    ORDER BY ebelp.
  select SINGLE NETWR INTO ls_items-net_price from vbap
+
 
                                where vbeln = wa_data-vbeln and
+
**********************************************************************
                                      posnr = wa_data-posnr.
+
* crear entrega
*ls_items-no_rounding = 'X'.
+
**********************************************************************
  APPEND ls_items TO lt_items.
+
  IF p_entrega IS INITIAL.
+
 
  ls_itemsx-po_item     = l_item_idx.
+
    REFRESH lt_stock_trans_items.
  ls_itemsx-po_itemx   = 'X'.
+
 
  ls_itemsx-material    = 'X'.
+
    LOOP AT lt_ekpo.
  ls_itemsx-plant       = 'X'.
+
      CLEAR lt_stock_trans_items.
  ls_itemsx-stge_loc    = 'X'.
+
      lt_stock_trans_items-ref_doc   = lt_ekpo-ebeln.
*ls_itemsx-item_cat   = 'X'.
+
      lt_stock_trans_items-ref_item   = lt_ekpo-ebelp.
   ls_itemsx-po_unit     = 'X'.
+
      lt_stock_trans_items-dlv_qty   = lt_ekpo-menge.
*ls_itemsx-no_rounding = 'X'.
+
      lt_stock_trans_items-sales_unit = lt_ekpo-meins.
  ls_itemsx-TRACKINGNO = 'X'.
+
      APPEND lt_stock_trans_items.
  ls_itemsX-QUANTITY    = 'X'.
+
    ENDLOOP.
  ls_itemsX-agreement  = 'X'.
+
 
  ls_itemsX-agmt_item  = 'X'.
+
    CALL FUNCTION 'BAPI_OUTB_DELIVERY_CREATE_STO'
  ls_itemsX-net_price   = 'X'.
+
    EXPORTING
  APPEND ls_itemsx TO lt_itemsx.
+
      ship_point        = l_ship_point
   
+
      due_date          = sy-datum
  ls_schedules-po_item      = l_item_idx.
+
    IMPORTING
  ls_schedules-quantity      = wa_data-KWMENG.
+
      delivery          = l_delivery
  ls_schedules-delivery_date = wa_data-edatu.
+
    TABLES
  APPEND ls_schedules TO lt_schedules.
+
      stock_trans_items = lt_stock_trans_items
   
+
      DELIVERIES        = lt_DELIVERIES
  ls_schedulesx-po_item       = l_item_idx.
+
      CREATED_ITEMS    = lt_CREATED_ITEMS
  ls_schedulesx-po_itemx      = 'X'.
+
      RETURN            = lt_return.
   ls_schedulesx-quantity      = 'X'.
+
 
  ls_schedulesx-delivery_date = 'X'.
+
    IF l_delivery IS INITIAL.
  APPEND ls_schedulesx TO lt_schedulesx.
+
      LOOP AT lt_return WHERE TYPE EQ 'E'.
+
        MESSAGE ID lt_return-ID TYPE 'I' NUMBER lt_return-NUMBER WITH
* Llamamos a la BAPI para crear el pedido de traslados
+
        lt_return-MESSAGE_V1
  CALL FUNCTION 'BAPI_PO_CREATE1'
+
        lt_return-MESSAGE_V2
  EXPORTING
+
        lt_return-MESSAGE_V3
    poheader              = ls_header
+
        lt_return-MESSAGE_V4 INTO g_message.
    poheaderx              = ls_headerx
+
 
  IMPORTING
+
        PERFORM add_news USING 'Creación de entrega:' icon_red_light.
    exppurchaseorder       = l_numped
+
        wa_data-stat = icon_red_light.
   TABLES
+
 
    RETURN                 = lt_return
+
      ENDLOOP.
    poitem                = lt_items
+
 
    poitemx                = lt_itemsx
+
    ELSE.
    poschedule            = lt_schedules
+
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    poschedulex            = lt_schedulesx
+
      EXPORTING
    .
+
        WAIT = 'X'.
   
+
      .
   if l_numped is INITIAL.
+
      p_entrega = l_delivery.
* error
+
      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</nowiki>
 +
 
 +
==BAPI_GOODSMVT_CREATE entrada de mercancía para pedido de traslado MIGO con referencia a entrega ==
 +
<nowiki>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</nowiki>
 +
 
 +
==Crear entrega de salida según pedido(VL01N), contabilizarla y realizar picking==
 +
  <nowiki>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
 
     LOOP AT lt_return INTO ls_return
    GROUP BY COND string( WHEN ls_return-type = 'E' THEN |EQ 1|
+
      GROUP BY COND string( WHEN ls_return-type = 'E' THEN |EQ 1|
                          WHEN ls_return-type = 'A' THEN |EQ 2|
+
                          WHEN ls_return-type = 'A' THEN |EQ 2|
                          WHEN ls_return-type = 'W' THEN |EQ 3|
+
                          WHEN ls_return-type = 'W' THEN |EQ 3|
                          WHEN ls_return-type = 'I' THEN |EQ 4|
+
                          WHEN ls_return-type = 'I' THEN |EQ 4|
                          )
+
                          )
              ASCENDING
+
              ASCENDING
              ASSIGNING FIELD-SYMBOL(<group>).
+
              ASSIGNING FIELD-SYMBOL(<group>).
 
       LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<ls_return>).
 
       LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<ls_return>).
 +
        exit.
 
       ENDLOOP.
 
       ENDLOOP.
 
       EXIT.
 
       EXIT.
     ENDLOOP.
+
  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.
 
     IF sy-subrc = 0.
       move <ls_return>-message to g_message.
+
      LOOP AT lt_lips.
      PERFORM add_news USING 'Crear Pedido:' icon_red_light.
+
        select COUNT(*) from marc where matnr = lt_lips-matnr and
      wa_data-stat =  icon_red_light.
+
                                        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.
 
     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.</nowiki>
 
  
== Crear una entrega de Pedido de traslado VL10B, contabilizarla y realizar picking==
+
**********************************************************************
<nowiki> *&---------------------------------------------------------------------*
+
***picking
*&      Form  CREAR_ENTREGA
+
**********************************************************************
*&---------------------------------------------------------------------*
+
   data: lt_vbpok type TABLE OF vbpok WITH HEADER LINE,
*     Crear una entrega de Pedido de traslado VL10B
+
         lt_prot type TABLE OF prott WITH HEADER LINE,
*----------------------------------------------------------------------*
+
         ls_vbkok like vbkok,
* -->  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_any_0              type c length 1,
 
         l_error_in_item_deletion_0 type c length 1,
 
         l_error_in_item_deletion_0 type c length 1,
Line 185: Line 532:
 
         l_error_partner_update    type c length 1,
 
         l_error_partner_update    type c length 1,
 
         l_error_sernr_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</nowiki>
 
  
==BAPI_GOODSMVT_CREATE entrada de mercancía para pedido de traslado MIGO con referencia a entrega ==
+
    REFRESH lt_return.
<nowiki> form recepcion USING p_oc p_entrega CHANGING p_materialdocument
+
 
                                              p_MATDOCUMENTYEAR.
+
    select * into TABLE lt_ekpo from ekpo where ebeln = p_oc.
* entrada de mercancía para pedido de traslado MIGO con referencia
+
 
* a entrega
+
    ls_vbkok-vbeln_vl = l_delivery.
+
    ls_vbkok-wabuc    = 'X'. "contabilizar Salida merc.
  DATA: lt_item TYPE TABLE OF  bapi2017_gm_item_create,
+
 
        lt_ekpo TYPE TABLE OF ekpo,
+
    SELECT vbeln AS vbeln_vl
        lt_return TYPE TABLE OF bapiret2.
+
          posnr AS posnr_vl
+
          matnr
  data: ls_ekpo TYPE ekpo,
+
          charg
        ls_header LIKE bapi2017_gm_head_01,
+
          lfimg
        ls_code LIKE  bapi2017_gm_code,
+
          lgmng
        ls_item LIKE LINE OF lt_item,
+
          umvkz
        ls_return LIKE LINE OF lt_return.
+
          umvkn
+
          vrkme
  DATA: l_materialdocument TYPE  bapi2017_gm_head_ret-mat_doc,
+
          meins
        l_MATDOCUMENTYEAR TYPE  BAPI2017_GM_HEAD_RET-DOC_YEAR,
+
      FROM lips
        l_testrun TYPE testrun VALUE space.
+
      INTO CORRESPONDING FIELDS OF TABLE lt_vbpok
+
                                  WHERE vbeln EQ l_delivery.
  CLEAR: lt_item[],
+
 
        ls_header,
+
    LOOP AT lt_vbpok.
        ls_code,
+
      lt_vbpok-vbeln      = lt_vbpok-vbeln_vl.
        l_materialdocument.
+
      lt_vbpok-posnn      = lt_vbpok-posnr_vl.
+
      lt_vbpok-taqui = 'X'.   "confirmar picking
  SELECT * FROM ekpo INTO TABLE lt_ekpo
+
      MODIFY lt_vbpok.
                          WHERE ebeln = p_oc.
+
    ENDLOOP.
  IF sy-subrc = 0.
+
 
    ls_header-pstng_date = sy-datum.
+
    CALL FUNCTION 'WS_DELIVERY_UPDATE_2'
    ls_header-doc_date  = sy-datum.
+
      EXPORTING
    ls_header-ref_doc_no = p_entrega.
+
        vbkok_wa                  = ls_vbkok
    ls_header-header_txt = wa_data-vbeln.
+
        commit                    = 'X'
    ls_code-gm_code      = '01'. "MB1C ver tabla T158G
+
        delivery                  = l_delivery
+
        update_picking            = 'X'
    LOOP AT lt_ekpo INTO ls_ekpo.
+
        if_error_messages_send    = ' '
      CLEAR ls_item.
+
      IMPORTING
      ls_item-move_type  = '101'.
+
        ef_error_any              = l_error_any_0
      ls_item-material  = ls_ekpo-matnr.
+
        ef_error_in_item_deletion = l_error_in_item_deletion_0
      ls_item-plant      = ls_ekpo-werks.
+
        ef_error_in_pod_update   = l_error_in_pod_update_0
      ls_item-stge_loc  = ls_ekpo-lgort.
+
        ef_error_in_interface    = l_error_in_interface_0
+
        ef_error_in_goods_issue  = l_error_in_goods_issue_0
* si material esta sujeto a lote => lote = pedido de traslado
+
        ef_error_in_final_check  = l_error_in_final_check_0
      SELECT COUNT(*) FROM MARc WHERE MATNR = ls_ekpo-matnr
+
        ef_error_partner_update  = l_error_partner_update
      AND WERKS = ls_ekpo-werks
+
        ef_error_sernr_update    = l_error_sernr_update
      AND XCHAR = 'X'.
+
      TABLES
      IF sy-subrc = 0.
+
        vbpok_tab                = lt_vbpok
        ls_item-batch = p_oc.
+
        prot                      = lt_prot.
      ENDIF.
+
 
      ls_item-entry_qnt  = ls_ekpo-menge.
+
    IF l_error_any_0 EQ 'X'.
      ls_item-po_number  = ls_ekpo-ebeln.
+
      LOOP AT lt_prot where msgty eq 'E'.
      ls_item-po_item   = ls_ekpo-ebelp.
+
        MESSAGE ID    lt_prot-msgid
      ls_item-val_type  = ls_ekpo-bwtar.
+
                TYPE  'I'
      ls_item-mvt_ind    = 'B'. "indicador de movimiento
+
                NUMBER lt_prot-msgno
      ls_item-DELIV_NUMB = p_entrega.
+
                WITH   lt_prot-msgv1
      APPEND ls_item TO lt_item.
+
                      lt_prot-msgv2
    ENDLOOP.
+
                      lt_prot-msgv3
+
                      lt_prot-msgv4 into g_message.
    CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
+
        PERFORM add_news USING 'Entrega Cliente Final:' icon_red_light.
    EXPORTING
+
      ENDLOOP.
      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.
 
     ELSE.
  * error
+
      MESSAGE S000(0k) WITH 'Se contabilizó la entrega' l_delivery into g_message.
      LOOP AT lt_return INTO ls_return
+
      PERFORM add_news USING 'Entrega Cliente Final:' icon_green_light.
      GROUP BY COND string( WHEN ls_return-TYPE = 'E' THEN |EQ 1|
+
      p_contabilizado_cli = 'X'.
      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</nowiki>
 
  
==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.
 
     ENDIF.
+
endform.</nowiki>
  else.
+
 
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
+
==Leer Datos de Clasificación de Material==
      EXPORTING
+
  <nowiki>FORM obtiene_embalaje USING    p_matnr
        wait = 'X'.
+
                      CHANGING p_emba1.
+
 
      p_entrega = l_delivery.
+
  DATA: vl_classtype   LIKE klah-klart VALUE '001',
      MESSAGE s000(0k) WITH  l_delivery 'creada con éxito' into g_message.
+
        vl_objecttable LIKE tcla-obtab VALUE 'MARA',
      PERFORM add_news USING 'Entrega Cliente Final:' icon_green_light.
+
        vl_clint       LIKE klah-clint VALUE '0000000000'.
  endif.
+
 
  else.
+
   DATA: tmp_material(18).
    l_delivery = p_entrega.
+
 
  endif.
+
   DATA: tl_objectdata LIKE clobjdat OCCURS 0 WITH HEADER LINE.
  *--------------------------------------------------------------------*
+
   DATA tl_class      LIKE sclass OCCURS 0 WITH HEADER LINE.
  * contabiliza SM & picking entregas.
+
  DATA: vl_objek LIKE ausp-objek.
*--------------------------------------------------------------------*
+
 
  DATA lt_return2 TYPE bapiret2_t.
+
   CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  DATA lt_lips TYPE TABLE OF lips WITH HEADER LINE.
+
    EXPORTING
  DATA: ls_data          TYPE bapiobdlvhdrchg,
+
      input = p_matnr
        ls_control        TYPE bapiobdlvhdrctrlchg,
+
    IMPORTING
        lt_item_control   TYPE TABLE OF  bapiobdlvitemctrlchg WITH HEADER LINE,
+
      output = tmp_material.
        lt_item_data      TYPE TABLE OF bapiobdlvitemchg WITH HEADER LINE,
+
 
        lt_item_spl       TYPE TABLE OF /spe/bapiobdlvitemchg WITH HEADER LINE,
+
  vl_objek = tmp_material.
        ls_techn_control  TYPE  bapidlvcontrol.
+
 
+
   CALL FUNCTION 'CLAF_CLASSIFICATION_OF_OBJECTS'
    check l_delivery is NOT INITIAL.
+
    EXPORTING
+
      classtext          = 'X'
* modificar entrega
+
      classtype         = vl_classtype
    REFRESH lt_lips.
+
      clint             = vl_clint
* si no tiene lote => actualizar
+
      features           = 'X'
    select * into TABLE lt_lips from lips where vbeln = l_delivery and charg = space.
+
       language          = sy-langu
    IF sy-subrc = 0.
+
       object            = vl_objek
      LOOP AT lt_lips.
+
      objecttable        = vl_objecttable
        select COUNT(*) from marc where matnr = lt_lips-matnr and
+
      key_date          = sy-datum
                                        werks = lt_lips-werks and
+
      initial_charact    = 'X'
                                        xchar = 'X'. "sujeto a lote
+
      change_service_clf = 'X'
        check sy-subrc = 0.
+
     TABLES
        lt_item_data-deliv_numb      = lt_lips-vbeln.
+
      t_class            = tl_class
        lt_item_data-deliv_item      = lt_lips-posnr.
+
      t_objectdata      = tl_objectdata
        lt_item_data-dlv_qty        = lt_lips-lfimg.
+
     EXCEPTIONS
        lt_item_data-batch          = p_oc.   "lote => pedido de traslado
+
      no_classification = 1
        lt_item_data-material        = lt_lips-matnr.
+
      no_classtypes     = 2
        lt_item_data-fact_unit_nom  = lt_lips-umvkz.
+
      invalid_class_type = 3
        lt_item_data-fact_unit_denom = lt_lips-umvkn.
+
      OTHERS            = 4.
        lt_item_data-base_uom        = lt_lips-meins.
+
endform.</nowiki>
        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.
 

Latest revision as of 22:13, 4 April 2020

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.

Leer Datos de Clasificación de Material

FORM obtiene_embalaje  USING    p_matnr
                       CHANGING p_emba1.

  DATA: vl_classtype   LIKE klah-klart VALUE '001',
        vl_objecttable LIKE tcla-obtab VALUE 'MARA',
        vl_clint       LIKE klah-clint VALUE '0000000000'.

  DATA: tmp_material(18).

  DATA: tl_objectdata LIKE clobjdat OCCURS 0 WITH HEADER LINE.
  DATA  tl_class      LIKE  sclass  OCCURS 0 WITH HEADER LINE.
  DATA: vl_objek LIKE ausp-objek.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = p_matnr
    IMPORTING
      output = tmp_material.

  vl_objek = tmp_material.

  CALL FUNCTION 'CLAF_CLASSIFICATION_OF_OBJECTS'
    EXPORTING
      classtext          = 'X'
      classtype          = vl_classtype
      clint              = vl_clint
      features           = 'X'
      language           = sy-langu
      object             = vl_objek
      objecttable        = vl_objecttable
      key_date           = sy-datum
      initial_charact    = 'X'
      change_service_clf = 'X'
    TABLES
      t_class            = tl_class
      t_objectdata       = tl_objectdata
    EXCEPTIONS
      no_classification  = 1
      no_classtypes      = 2
      invalid_class_type = 3
      OTHERS             = 4.
endform.