FUNCTION z_bapi_get_booked_flight.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     VALUE(AIRLINEID) LIKE  BAPISBODAT-AIRLINEID
*"     VALUE(CONNECTID) LIKE  BAPISBODAT-CONNECTID
*"     VALUE(FLIGHTDATE) LIKE  BAPISBODAT-FLIGHTDATE
*"     VALUE(BOOKINGID) LIKE  BAPISBODAT-BOOKINGID
*"  EXPORTING
*"     VALUE(FLUG_INPUT) TYPE  ZFLUG_INPUT
*"     VALUE(FLIGHT) TYPE  ZFLUG
*"     VALUE(BOOKING) TYPE  SBOOK
*"     VALUE(CUSTOM) TYPE  SCUSTOM
*"     VALUE(URL) TYPE  SCARR-URL
*"  TABLES
*"      RETURN STRUCTURE  BAPIRET2 OPTIONAL
*"----------------------------------------------------------------------
  DATA:
    ispfli TYPE spfli,
    isflight TYPE sflight,
    zbapisbodat TYPE bapisbodat.

  CLEAR: msg1_hlp, msg2_hlp.
  REFRESH: return.

*---------- Authority check -------------------------------------------*
  AUTHORITY-CHECK OBJECT 'S_FLBOOK'
           ID 'ACTVT'  FIELD '03'.
  IF sy-subrc <> 0.
    CALL FUNCTION 'BALW_BAPIRETURN_GET2'
      EXPORTING
        type   = 'E'
        cl     = 'BC_IBF'
        number = '004'
      IMPORTING
        return = return.
    APPEND return.
    PERFORM final_return_message                "BAPI was not successful
      USING     'E'
      CHANGING  return[].
    RETURN.
  ENDIF.
*------ End of Authority check ---------------------------------------*

*------ Check and interprete AIRLINEID -------------------------------*
  IF airlineid IS NOT INITIAL.
    TRANSLATE airlineid TO UPPER CASE.
    DATA: l_url TYPE scarr-url.
    SELECT SINGLE mandt url FROM scarr INTO (lv_mandt, l_url)
      WHERE carrid = airlineid.
    IF sy-subrc <> 0.
      WRITE airlineid TO msg1_hlp.
      CALL FUNCTION 'BALW_BAPIRETURN_GET2'          "airline not found
          EXPORTING
              type      = 'E'
              cl        = 'BC_IBF'
              number    = 50
              par1      = msg1_hlp
              parameter = 'AIRLINEID'
          IMPORTING
              return    = return.
      APPEND return.
    ELSE.
      url = l_url.
    ENDIF.
  ENDIF.
*------ End of AIRLINE -----------------------------------------------*

*---- Return in case of errors in input data -------------------------*
  READ TABLE return WITH KEY type = 'E'.
  IF sy-subrc = 0.
    PERFORM final_return_message                "BAPI was not successful
      USING     'E'
      CHANGING  return[].
    RETURN.
  ENDIF.
*---------- End of Checking selection data ----------------------------*

*---------- Read relevant booking and return data ---------------------*
  SELECT SINGLE * FROM sbook INTO booking
    WHERE carrid  = airlineid
    AND   connid  = connectid
    AND   fldate  = flightdate
    AND   bookid  = bookingid.
* create warning message if selection is empty
  IF sy-subrc <> 0.
    CALL FUNCTION 'BALW_BAPIRETURN_GET2'       "no booking found
       EXPORTING
           type   = 'W'
           cl     = 'BC_IBF'
           number = 100
       IMPORTING
           return = return.
    APPEND return.
* convertion of results to external data represenatation (BAPI format)
  ELSE.
    CALL FUNCTION 'MAP2E_SBOOK_TO_BAPISBODAT'
      EXPORTING
        sbook      = booking
      CHANGING
        bapisbodat = zbapisbodat.
  ENDIF.
*----------- End of read relevant booking data ------------------------*

*---------- Read relevant flight and return data ----------------------*
  SELECT SINGLE * FROM spfli INTO ispfli
    WHERE carrid  = airlineid
    AND   connid  = connectid.
  flug_input-cityfrom  = ispfli-cityfrom.
  flug_input-cityto    = ispfli-cityto.
  flight-fltime        = ispfli-fltime.

  SELECT SINGLE * FROM sflight INTO isflight
    WHERE carrid  = airlineid
    AND   connid  = connectid
    AND   fldate  = flightdate.
  flight-carrid    = isflight-carrid.
  flight-connid    = isflight-connid.
  flight-fldate    = isflight-fldate.
  flight-price     = isflight-price.
  flight-currency  = isflight-currency.
  flight-seatsfree = isflight-seatsmax - isflight-seatsocc.

* create warning message if selection is empty
  IF sy-subrc <> 0.
    CALL FUNCTION 'BALW_BAPIRETURN_GET2'       "no flight found
       EXPORTING
           type   = 'W'
           cl     = 'BC_IBF'
           number = 56
       IMPORTING
           return = return.
    APPEND return.
  ENDIF.
*----------- End of read relevant flight data -------------------------*

*---------- Read relevant customer and return data --------------------*
  SELECT SINGLE * FROM scustom INTO custom
    WHERE id  = booking-customid.
* create warning message if selection is empty
  IF sy-subrc <> 0.
    CALL FUNCTION 'BALW_BAPIRETURN_GET2'       "no customer found
       EXPORTING
           type   = 'W'
           cl     = 'BC_IBF'
           number = 150
       IMPORTING
           return = return.
    APPEND return.
  ENDIF.
*----------- End of read relevant customer data ----------------------*

* Create a success return message
  PERFORM final_return_message                  "BAPI was successful
    USING     'S'
    CHANGING  return[].
ENDFUNCTION.