SAP SD BAPI TRANSPORTE

From SapWiki

Ejemplo 1

REPORT ztest_bapi_shipment_create2 .
***************************************************************
* This report is provided to help understanding the interface *
* of function module 'BAPI_SHIPMENT_CREATE' with regard to    *
* its ability to fill the different shipment-deadlines        *
* With running this report, new shipments are created         *
* see note 531207
***************************************************************

* fill the interface for bapi
DATA: headerdata LIKE bapishipmentheader,
        headerdeadline LIKE bapishipmentheaderdeadline
                   OCCURS 0 WITH HEADER LINE,
        itemdata LIKE bapishipmentitem  OCCURS 0 WITH HEADER LINE,
        stagedata LIKE bapishipmentstage OCCURS 0 WITH HEADER LINE,
        stagedeadline LIKE bapishipmentstagedeadline
                          OCCURS 0 WITH HEADER LINE,
        itemonstage LIKE bapishipmentitemonstage
                         OCCURS 0 WITH HEADER LINE,
        address LIKE bapishipmentaddress
                         OCCURS 0 WITH HEADER LINE,
        hdunheader LIKE bapishipmenthdunheader
                         OCCURS 0 WITH HEADER LINE,
        hdunitem LIKE bapishipmenthdunitem
                         OCCURS 0 WITH HEADER LINE,
        return LIKE bapiret2  OCCURS 0 WITH HEADER LINE.


SELECTION-SCREEN COMMENT /1(83) com1.
SELECTION-SCREEN COMMENT /1(83) com2.
SELECTION-SCREEN COMMENT /1(83) com3.
SELECTION-SCREEN COMMENT /1(83) com4.
SELECTION-SCREEN COMMENT /1(83) com5.
SELECTION-SCREEN COMMENT /1(83) com6.
SELECTION-SCREEN COMMENT /1(83) com7.
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN ULINE.


SELECTION-SCREEN BEGIN OF BLOCK header WITH FRAME TITLE text-hrd.
* Headerdate - obligatory
SELECTION-SCREEN COMMENT /1(79) com8 FOR FIELD tplst.
PARAMETER: tplst LIKE vttk-tplst.
SELECTION-SCREEN COMMENT /1(79) com9 FOR FIELD tplst.
PARAMETER: shtyp LIKE vttk-shtyp.
SELECTION-SCREEN END OF BLOCK header.

SELECTION-SCREEN ULINE.

SELECTION-SCREEN BEGIN OF BLOCK status WITH FRAME TITLE com10.
* Plan
PARAMETER:  plan LIKE  bapishipmentheader-status_plan.
*checkin
PARAMETER:  check LIKE  bapishipmentheader-status_checkin.
*load start
PARAMETER:  load_s LIKE  bapishipmentheader-status_load_start.
*load end
PARAMETER:  load_e LIKE  bapishipmentheader-status_load_end.
*Completion
PARAMETER:  compl LIKE  bapishipmentheader-status_compl.
* tra begin
PARAMETER:  ship_s LIKE  bapishipmentheader-status_shpmnt_start.
* tra end
PARAMETER:  ship_e LIKE  bapishipmentheader-status_shpmnt_end.
SELECTION-SCREEN END OF BLOCK status.

* Headerdeadline
SELECTION-SCREEN BEGIN OF BLOCK h_deadline WITH FRAME TITLE com11.
PARAMETERS: ttype(10)
           DEFAULT 'HDRSTPLDT', "= End of planning
           utc LIKE bapishipmentheaderdeadline-time_stamp_utc,
           zone LIKE bapishipmentheaderdeadline-time_zone.

PARAMETERS: ttype2(10)
           DEFAULT 'HDRSTCIPDT',"= planned date of check-in
           utc2 LIKE bapishipmentheaderdeadline-time_stamp_utc,
           zone2 LIKE bapishipmentheaderdeadline-time_zone.

PARAMETERS: ttype3(10)
           DEFAULT 'HDRSTCIADT',"= actual date of check-in
           utc3 LIKE bapishipmentheaderdeadline-time_stamp_utc,
           zone3 LIKE bapishipmentheaderdeadline-time_zone.

PARAMETERS: ttype4(10)
           DEFAULT 'HDRSTLSPDT',"= planned date f. start loading
           utc4 LIKE bapishipmentheaderdeadline-time_stamp_utc,
           zone4 LIKE bapishipmentheaderdeadline-time_zone.

PARAMETERS: ttype5(10)
           DEFAULT 'HDRSTLSADT',"= current date f. start loading
           utc5 LIKE bapishipmentheaderdeadline-time_stamp_utc,
           zone5 LIKE bapishipmentheaderdeadline-time_zone.

PARAMETERS: ttype6(10)
           DEFAULT 'HDRSTLEPDT',"= planned date f. end loading
           utc6 LIKE bapishipmentheaderdeadline-time_stamp_utc,
           zone6 LIKE bapishipmentheaderdeadline-time_zone.

PARAMETERS: ttype7(10)
           DEFAULT 'HDRSTLEADT',"= actual date f. end loading
           utc7 LIKE bapishipmentheaderdeadline-time_stamp_utc,
           zone7 LIKE bapishipmentheaderdeadline-time_zone.

PARAMETERS: ttype8(10)
           DEFAULT 'HDRSTCPDT',"= planned date f. shpmnt completion
           utc8 LIKE bapishipmentheaderdeadline-time_stamp_utc,
           zone8 LIKE bapishipmentheaderdeadline-time_zone.

PARAMETERS: ttype9(10)
           DEFAULT 'HDRSTCADT',"= actual date f. shpmnt completion
           utc9 LIKE bapishipmentheaderdeadline-time_stamp_utc,
           zone9 LIKE bapishipmentheaderdeadline-time_zone.

PARAMETERS: ttype10(10)
           DEFAULT 'HDRSTSSPDT',"= planned date f. start shpmnt
           utc10 LIKE bapishipmentheaderdeadline-time_stamp_utc,
           zone10 LIKE bapishipmentheaderdeadline-time_zone.

PARAMETERS: ttype11(10)
           DEFAULT 'HDRSTSSADT',"= actual date f. start shpmnt
           utc11 LIKE bapishipmentheaderdeadline-time_stamp_utc,
           zone11 LIKE bapishipmentheaderdeadline-time_zone.

PARAMETERS: ttype12(10)
           DEFAULT 'HDRSTSEPDT',"= planned date f. end shpmnt
           utc12 LIKE bapishipmentheaderdeadline-time_stamp_utc,
           zone12 LIKE bapishipmentheaderdeadline-time_zone.

PARAMETERS: ttype13(10)
           DEFAULT 'HDRSTSEADT',"= actual date f. end shpmnt
           utc13 LIKE bapishipmentheaderdeadline-time_stamp_utc,
           zone13 LIKE bapishipmentheaderdeadline-time_zone.

SELECTION-SCREEN END OF BLOCK h_deadline.

***********************************************************************
* Format the select-options                                           *
***********************************************************************
INITIALIZATION.
 com1 = 'This report was provided to help understanding the behaviour'.
 com2 = 'of function module BAPI_SHIPMENT_CREATE with regard to its '.
 com3 = 'ability to fill the new shipments deadlines: '.
 com4 = 'Planning, Check-In, Loading Start, Loading end, '.
 com5 = 'Shpmnt completion, Shipment start and shipment end.'.
 com6 = 'This report creates new shipments which can be '.
 com7 = 'viewed e.g. with transaction VT03n'.
 com8 = 'Transportation Planning Point (obligatory)'.
 com9 = 'Shipment type (obligatory)'.
 com10 = 'BAPISHIPMENTHEADER-Flags --> see note 531207 '.
 com11 = 'HEADERDEADLINE-Structure --> see note 531207 '.
***********************************************************************


START-OF-SELECTION.
******************head begin***********************************
 headerdata-trans_plan_pt = tplst.
 headerdata-shipment_type = shtyp.

* change Plan status
*  HEADERDATA-SHIPMENT_NUM = tknum.
 IF plan <> space.
   headerdata-status_plan = plan.
 ENDIF.
* Change Check_in status
 IF check <> space.
   headerdata-status_checkin = check.
 ENDIF.
*load beginn
 IF load_s <> space .
   headerdata-status_load_start = load_s.
 ENDIF.
* load end
 IF load_e <> space .
   headerdata-status_load_end = load_e.
 ENDIF.
* completion
 IF compl <> space.
   headerdata-status_compl = compl.
 ENDIF.
*tra beginn
 IF ship_s <> space.
   headerdata-status_shpmnt_start = ship_s.
 ENDIF.
*tra beginn
 IF ship_e <> space.
   headerdata-status_shpmnt_end = ship_e.
 ENDIF.

***** HEADERDEADLINE END OF PLANNING
 IF ttype <> space.
   headerdeadline-time_type = ttype.
   headerdeadline-time_stamp_utc = utc.
   headerdeadline-time_zone = zone.
   APPEND headerdeadline.
 ENDIF.
***** HEADERDEADLINE PLANNED DATE OF CHECK-IN
 IF ttype2 <> space.
   headerdeadline-time_type = ttype2.
   headerdeadline-time_stamp_utc = utc2.
   headerdeadline-time_zone = zone2.
   APPEND headerdeadline.
 ENDIF.
***** HEADERDEADLINE ACTUAL DATE OF CHECK-IN
 IF ttype3 <> space.
   headerdeadline-time_type = ttype3.
   headerdeadline-time_stamp_utc = utc3.
   headerdeadline-time_zone = zone3.
   APPEND headerdeadline.
 ENDIF.
***** HEADERDEADLINE PLANNED D START OF LOADING
 IF ttype4 <> space.
   headerdeadline-time_type = ttype4.
   headerdeadline-time_stamp_utc = utc4.
   headerdeadline-time_zone = zone4.
   APPEND headerdeadline.
 ENDIF.
***** HEADERDEADLINE CURRENT D START OF LOADING
 IF ttype5 <> space.
   headerdeadline-time_type = ttype5.
   headerdeadline-time_stamp_utc = utc5.
   headerdeadline-time_zone = zone5.
   APPEND headerdeadline.
 ENDIF.
***** HEADERDEADLINE PLANNED D END OF LOADING
 IF ttype6 <> space.
   headerdeadline-time_type = ttype6.
   headerdeadline-time_stamp_utc = utc6.
   headerdeadline-time_zone = zone6.
   APPEND headerdeadline.
 ENDIF.
***** HEADERDEADLINE ACTUAL D END OF LOADING
 IF ttype7 <> space.
   headerdeadline-time_type = ttype7.
   headerdeadline-time_stamp_utc = utc7.
   headerdeadline-time_zone = zone7.
   APPEND headerdeadline.
 ENDIF.
***** HEADERDEADLINE PLANNED D SHIPMENT COMPLETION
 IF ttype8 <> space.
   headerdeadline-time_type = ttype8.
   headerdeadline-time_stamp_utc = utc8.
   headerdeadline-time_zone = zone8.
   APPEND headerdeadline.
 ENDIF.
***** HEADERDEADLINE ACTUAL D SHIPMENT COMPLETION
 IF ttype9 <> space.
   headerdeadline-time_type = ttype9.
   headerdeadline-time_stamp_utc = utc9.
   headerdeadline-time_zone = zone9.
   APPEND headerdeadline.
 ENDIF.
***** HEADERDEADLINE PLANNED D START OF SHIPMENT
 IF ttype10 <> space.
   headerdeadline-time_type = ttype10.
   headerdeadline-time_stamp_utc = utc10.
   headerdeadline-time_zone = zone10.
   APPEND headerdeadline.
 ENDIF.
***** HEADERDEADLINE ACTUAL D START OF SHIPMENT
 IF ttype11 <> space.
   headerdeadline-time_type = ttype11.
   headerdeadline-time_stamp_utc = utc11.
   headerdeadline-time_zone = zone11.
   APPEND headerdeadline.
 ENDIF.
***** HEADERDEADLINE PLANNED D END OF SHIPMENT
 IF ttype12 <> space.
   headerdeadline-time_type = ttype12.
   headerdeadline-time_stamp_utc = utc12.
   headerdeadline-time_zone = zone12.
   APPEND headerdeadline.
 ENDIF.
***** HEADERDEADLINE CURRENT D END OF SHIPMENT
 IF ttype13 <> space.
   headerdeadline-time_type = ttype13.
   headerdeadline-time_stamp_utc = utc13.
   headerdeadline-time_zone = zone13.
   APPEND headerdeadline.
 ENDIF.

************************************************************
************************************************************
 CALL FUNCTION 'BAPI_SHIPMENT_CREATE'
      EXPORTING
           headerdata     = headerdata
      TABLES
           headerdeadline = headerdeadline
           itemdata       = itemdata
           stagedata      = stagedata
           stagedeadline  = stagedeadline
           itemonstage    = itemonstage
           address        = address
           hdunheader     = hdunheader
           hdunitem       = hdunitem
           return         = return.

 LOOP AT return.

   WRITE: return.

 ENDLOOP.

*Colocar commit work para grabar

Ejemplo 2

 FORM CREAR_TRANSPORTE  CHANGING o_TKNUM type TKNUM
                                o_SUBRC.
  data: lt_return type TABLE OF bapiret2,
      ls_return like LINE OF lt_return.

  data: ls_headerdata LIKE  BAPISHIPMENTHEADER,
        ls_itemdata type BAPISHIPMENTITEM,
        lt_itemdata type TABLE OF BAPISHIPMENTITEM,
        ls_HEADERDEADLINE  type BAPISHIPMENTHEADERDEADLINE,
        lt_HEADERDEADLINE  type table of BAPISHIPMENTHEADERDEADLINE.

  ls_headerdata-SHIPMENT_TYPE = 'ZDT'.
  ls_headerdata-TRANS_PLAN_PT = vttk-tplst.
  ls_headerdata-SHIPPING_COND = vttk-vsbed.
  ls_headerdata-SHIPMENT_ROUTE = vttk-route.
  ls_headerdata-SERVICE_AGENT_ID = vttk-tdlnr.
  ls_headerdata-CONTAINER_ID = vttk-signi.
  ls_headerdata-EXTERNAL_ID_1 = vttk-exti1.
  ls_headerdata-status_plan = 'X'.

  loop at gt_likp into gs_likp where vbeln is NOT INITIAL.
    ls_itemdata-DELIVERY = gs_likp-vbeln.

    append ls_itemdata to lt_itemdata.
  endloop.

  data ld_time(14) type c.

  CONCATENATE sy-datum sy-timlo into ld_time.

  ls_HEADERDEADLINE-TIME_TYPE = 'HDRSTPLDT'. "WSHDRTDDAT
  move ld_time to ls_HEADERDEADLINE-TIME_STAMP_UTC.
  ls_HEADERDEADLINE-time_zone = 'CHILE'.

  append ls_HEADERDEADLINE to lt_HEADERDEADLINE.
 *
  CALL FUNCTION 'BAPI_SHIPMENT_CREATE'
    EXPORTING
      HEADERDATA           = ls_headerdata
   IMPORTING
     TRANSPORT            = o_TKNUM
 *     SHIPMENTGUID         =
    TABLES
     HEADERDEADLINE       = lt_HEADERDEADLINE
     ITEMDATA             = lt_itemdata
      RETURN               = lt_return
            .
  if o_TKNUM is INITIAL.
 * transporte no creado
    o_subrc = 1.
    loop at lt_return into ls_return WHERE type = 'E'.
      exit.
    endloop.
    if sy-subrc = 0.
      MESSAGE ID ls_return-id TYPE 'E' NUMBER ls_return-number
                     WITH ls_return-MESSAGE_V1
                          ls_return-MESSAGE_V2
                          ls_return-MESSAGE_V3
                          ls_return-MESSAGE_V4.
    endif.
  else.
    o_subrc = 0.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        WAIT   = 'X'
      IMPORTING
        RETURN = ls_return.

 * GRABA TEXTOS
    if ls_return is INITIAL.
      PERform get_texts_from_screen_and_save using o_TKNUM .
    else.
      MESSAGE ID ls_return-id TYPE 'E' NUMBER ls_return-number
                     WITH ls_return-MESSAGE_V1
                          ls_return-MESSAGE_V2
                          ls_return-MESSAGE_V3
                          ls_return-MESSAGE_V4.
    endif.

    message s006(VW) with o_TKNUM.
  ENDIF.

 ENDFORM.                    " CREAR_TRANSPORTE

Ejemplo 3 con embalaje

 FORM CREAR_TRANSPORTE using i_unidad
                       CHANGING o_TKNUM type TKNUM
                                o_SUBRC.

  data: lt_return type TABLE OF bapiret2,
      ls_return like LINE OF lt_return.

  data: ls_headerdata LIKE  BAPISHIPMENTHEADER,
        ls_itemdata type BAPISHIPMENTITEM,
        lt_itemdata type TABLE OF BAPISHIPMENTITEM,
        ls_HEADERDEADLINE  type BAPISHIPMENTHEADERDEADLINE,
        lt_HEADERDEADLINE  type table of BAPISHIPMENTHEADERDEADLINE.

  data: ls_HDUNHEADER	LIKE BAPISHIPMENTHDUNHEADER,
        lt_HDUNHEADER	like TABLE OF ls_HDUNHEADER,
        ls_HDUNITEM LIKE BAPISHIPMENTHDUNITEM,
        lt_HDUNITEM like TABLE OF ls_HDUNITEM.

  ls_headerdata-SHIPMENT_TYPE = gd_SHTYP.
  ls_headerdata-TRANS_PLAN_PT = vttk-tplst.
  ls_headerdata-SHIPPING_COND = vttk-vsbed.
  ls_headerdata-SHIPMENT_ROUTE = vttk-route.
  ls_headerdata-SERVICE_AGENT_ID = vttk-tdlnr.
  ls_headerdata-CONTAINER_ID = vttk-signi.
  ls_headerdata-EXTERNAL_ID_1 = vttk-exti1.
  move LIKP-ANZPK to ls_headerdata-TEXT_1.
  ls_headerdata-status_plan = 'X'.

    loop at gt_likp into gs_likp where vbeln is NOT INITIAL
                              .
      ls_itemdata-DELIVERY = gs_likp-vbeln.

      append ls_itemdata to lt_itemdata.
    endloop.

  data ld_time(14) type c.

  CONCATENATE vttk-dtdis vttk-uzdis into ld_time.

  ls_HEADERDEADLINE-TIME_TYPE = 'HDRSTPLDT'. "WSHDRTDDAT
  move ld_time to ls_HEADERDEADLINE-TIME_STAMP_UTC.
  ls_HEADERDEADLINE-time_zone = 'CHILE'.

  append ls_HEADERDEADLINE to lt_HEADERDEADLINE.

   if i_unidad ne space.
     loop at gt_likp into gs_likp where vbeln is NOT INITIAL
                                      and sel = 'X'.
       clear ls_HDUNITEM.
       select  vbeln posnr matnr lfimg
        into

         (ls_HDUNITEM-DELIV_NUMB,ls_HDUNITEM-DELIV_ITEM,ls_HDUNITEM-material,ls_HDUNITEM-PACK_QTY)
                     from lips
                      where vbeln = gs_likp-vbeln.
         ls_HDUNITEM-HDL_UNIT_EXID_INTO = '$0001'.
         append  ls_HDUNITEM to lt_HDUNITEM.
       endselect.
     endloop.
     if sy-subrc = 0.
       ls_HDUNHEADER-SHIP_MAT = i_unidad.
       ls_HDUNHEADER-HDL_UNIT_EXID = '$0001'.
       append ls_HDUNHEADER to lt_HDUNHEADER.
     else.
       message E490(VW) WITH 'No se ha seleccionado ninguna Entrega'.
     endif.

  else.
    clear:  lt_HDUNHEADER[],
           lt_HDUNITEM[].
  endif.
 *
  CALL FUNCTION 'BAPI_SHIPMENT_CREATE'
    EXPORTING
      HEADERDATA           = ls_headerdata
   IMPORTING
     TRANSPORT            = o_TKNUM
    TABLES
     HEADERDEADLINE       = lt_HEADERDEADLINE
     ITEMDATA             = lt_itemdata
     HDUNHEADER           = lt_HDUNHEADER
     HDUNITEM             = lt_HDUNITEM
      RETURN               = lt_return
            .
  if o_TKNUM is INITIAL.
 * transporte no creado
    o_subrc = 1.
    loop at lt_return into ls_return WHERE type = 'E'.
      exit.
    endloop.
    if sy-subrc = 0.
      MESSAGE ID ls_return-id TYPE 'E' NUMBER ls_return-number
                     WITH ls_return-MESSAGE_V1
                          ls_return-MESSAGE_V2
                          ls_return-MESSAGE_V3
                          ls_return-MESSAGE_V4.


    endif.
  else.
    o_subrc = 0.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        WAIT   = 'X'
      IMPORTING
        RETURN = ls_return.

 * GRABA TEXTOS
    if ls_return is INITIAL.
      PERform get_texts_from_screen_and_save using o_TKNUM .
    else.
      MESSAGE ID ls_return-id TYPE 'E' NUMBER ls_return-number
                     WITH ls_return-MESSAGE_V1
                          ls_return-MESSAGE_V2
                          ls_return-MESSAGE_V3
                          ls_return-MESSAGE_V4.
    endif.

    message s006(VW) with o_TKNUM.
  ENDIF.

 ENDFORM.                    " CREAR_TRANSPORTE

Modificar Transporte

  • Modificar estado transporte
 DATA: l_it_header        TYPE bapishipmentheader,
        l_it_headeraction  TYPE  bapishipmentheaderaction,
        l_it_return        TYPE TABLE OF bapiret2.
  DATA: l_wa_return TYPE bapiret2.

  l_it_header-shipment_num  = shipment.
  l_it_headeraction-shipment_num  = 'X'.

  l_it_header-status_shpmnt_start       = 'X'.
  l_it_headeraction-status_shpmnt_start = 'C'.

  CALL FUNCTION 'BAPI_SHIPMENT_CHANGE'
    EXPORTING
      headerdata       = l_it_header
      headerdataaction = l_it_headeraction
    TABLES
      return           = l_it_return.

bloquear patente ( VTTK-SIGNI )

 FORM BLOQUEAR_PATENTE .

  data : lt_SYSTEM_STATUS type TABLE OF BAPI_ITOB_STATUS WITH HEADER LINE,
         lt_USER_STATUS type TABLE OF BAPI_ITOB_STATUS WITH HEADER LINE.
  data wa_return type bapiret2.
  data: l_equi type EQUNR,
        l_objnr type JSTO-OBJNR,
        l_stat_e LIKE jest-stat.

  move wa_vttk-signi to l_equi.

  CALL FUNCTION 'BAPI_EQUI_GETSTATUS'
    EXPORTING
      EQUIPMENT           = l_equi
   IMPORTING
     RETURN              = wa_return
    TABLES
      SYSTEM_STATUS       = lt_system_status
      USER_STATUS         = lt_user_status
            .
  if wa_return is NOT INITIAL.
    write:/ 'Transporte:',
             wa_vttk-tknum,
            'Error leer estado Patente:',
             wa_vttk-signi,
             wa_return-MESSAGE(80).
    gd_bloqueado = co_no_aplica.
    return.
  endif.

  loop at lt_USER_STATUS TRANSPORTING NO FIELDS WHERE status = 'E0001'.
  ENDLOOP.
  if sy-subrc = 0.

    CONCATENATE 'IE' wa_vttk-signi into l_objnr.

    move 'E0002' to l_stat_e.
    CALL FUNCTION 'STATUS_CHANGE_EXTERN'
      EXPORTING
        OBJNR                     = l_objnr
        USER_STATUS               = l_stat_e
     EXCEPTIONS
       OBJECT_NOT_FOUND          = 1
       STATUS_INCONSISTENT       = 2
       STATUS_NOT_ALLOWED        = 3
       OTHERS                    = 4
              .
    IF SY-SUBRC <> 0.
      write:/ 'Transporte:',
             wa_vttk-tknum,
             'Error bloqueo patente:',
             wa_vttk-signi.
    else.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
       EXPORTING
         WAIT          = 'X'
                .
      write:/ 'Transporte:',
             wa_vttk-tknum,
             'Patente bloqueada con exito:',
              wa_vttk-signi.
      gd_bloqueado = co_yes.
      add 1 to gd_num_bloqueados.
    ENDIF.
  else.
    gd_bloqueado = co_no_aplica.
  endif.

 ENDFORM.                    " BLOQUEAR_PATENTE