FUNCTION Z_BAPI_FLIGHT_GETDETAIL.
*"--------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"       IMPORTING
*"             VALUE(AIRLINEID) LIKE  BAPISFLKEY-AIRLINEID
*"             VALUE(CONNECTIONID) LIKE  BAPISFLKEY-CONNECTID
*"             VALUE(FLIGHTDATE) LIKE  BAPISFLKEY-FLIGHTDATE
*"       EXPORTING
*"             VALUE(FLIGHT_DATA) LIKE  BAPISFLDAT
*"  STRUCTURE  BAPISFLDAT
*"             VALUE(ADDITIONAL_INFO) LIKE  BAPISFLADD
*"  STRUCTURE  BAPISFLADD
*"             VALUE(AVAILIBILITY) LIKE  BAPISFLAVA
*"  STRUCTURE  BAPISFLAVA
*"       TABLES
*"              EXTENSION_IN STRUCTURE  BAPIPAREX OPTIONAL
*"              EXTENSION_OUT STRUCTURE  BAPIPAREX OPTIONAL
*"              RETURN STRUCTURE  BAPIRET2 OPTIONAL
*"--------------------------------------------------------------------


  clear: msg1_hlp, msg2_hlp, flight_data, additional_info, availibility.
  refresh: return, extension_out.

  data: lv_sflights2 like sflights2.


  translate airlineid to upper case.            "#EC SYNTCHAR

*********** 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 ************************************


*********** BAdI 1 for Customer specific coding ***********************
* At this point there should be a BAdI that the customer can use to   *
* check the data in the parameter ExtensionIn.                        *
* Unfortunately there was no time to define and implement the BAdI in *
* this release. This will be done at a later time.                    *
* We are aware that this is inconsistent with the existence of the    *
* ExtensionIn. However, this BAPI is for learning purposes only and   *
* not productive. Therefore we think it better to show how to define  *
* the ExtensionIn (even though it can not be used here) instead of    *
* leaving it out. ("Showing some is better than nothing")             *
*********** End of BAdI 1 *********************************************


*********** Selection of requested data********************************
  select single * from sflights2 into lv_sflights2
             where carrid = AIRLINEID
               and connid = CONNECTIONID
               and fldate = FLIGHTDATE.

* write return message and return if flight was not found
  if sy-subrc <> 0.
    concatenate AIRLINEID CONNECTIONID FLIGHTDATE into
               msg1_hlp separated by space.
    call function 'BALW_BAPIRETURN_GET2'       "Flight not found
         EXPORTING
              TYPE   = 'E'
              CL     = 'BC_IBF'
              NUMBER = 55
              PAR1   = MSG1_HLP
         IMPORTING
              RETURN = return.
    append return.
    perform final_return_message               "BAPI was not successful
        USING     'E'
        CHANGING  return[].
    return.
  endif.
*********** End of selection data*************************************


******* Convertion to external data representation (BAPI format)*******
  call function 'MAP2E_SFLIGHTS2_TO_BAPISFLDAT'
       EXPORTING sflights2 = lv_sflights2
       CHANGING BAPISFLDAT = flight_data
       EXCEPTIONS ERROR_CONVERTING_CURR_AMOUNT.
  if sy-subrc <> 0.
    call function 'BALW_BAPIRETURN_GET2'     "Currency conversion error
         EXPORTING
              TYPE   = 'W'
              CL     = 'BC_IBF'
              NUMBER = 15
         IMPORTING
              RETURN = return.
    append return.
  endif.

  call function 'MAP2E_SFLIGHTS2_TO_BAPISFLADD'
       EXPORTING
            sflights2  = lv_sflights2
       CHANGING
            BAPISFLADD = additional_info.

  flight_data-arrdate = flight_data-flightdate
                       + lv_sflights2-period.      "Compute Arrival date

* Determine availibility
  availibility-economax  = lv_sflights2-seatsmax.
  availibility-econofree = lv_sflights2-seatsmax -
                                lv_sflights2-seatsocc.
  availibility-businmax  = lv_sflights2-seatsmax_b.
  availibility-businfree = lv_sflights2-seatsmax_b -
                                lv_sflights2-seatsocc_b.
  availibility-firstmax  = lv_sflights2-seatsmax_f.
  availibility-firstfree = lv_sflights2-seatsmax_f -
                                lv_sflights2-seatsocc_f.
******* End of convertion (BAPI format)********************************


*********** BAdI 2 for Customer specific coding ***********************
* At this point there should be a BAdI that the customer can use to   *
* proceed his data and fill the parameter ExtensionOut.               *
* Unfortunately there was no time to define and implement the BAdI in *
* this release. This will be done at a later time.                    *
* We are aware that this is inconsistent with the existence of the    *
* ExtensionOut. However, this BAPI is for learning purposes only and  *
* not productive. Therefore we think it better to show how to define  *
* the ExtensionOut (even though it can not be used here) instead of   *
* leaving it out. ("Showing some is better than nothing")             *
*********** End of BAdI 2 *********************************************


* create success return message
  perform final_return_message               "BAPI was successful
      USING     'S'
      CHANGING  return[].


endfunction.
********* End of function *********************************************




********* Form for writing the final return message *******************
*                                                                     *
* Write success message if return contains no error message,          *
*   otherwise write final error message                               *
***********************************************************************

form final_return_message
    USING
       VALUE(mess_type)  type bapi_mtype
    CHANGING
       return type return_type.

data: return_item type bapiret2.

if mess_type = 'S'.
  call function 'BALW_BAPIRETURN_GET2'        "BAPI was successful
      EXPORTING
          TYPE   = 'S'
          CL     = 'BC_IBF'
          NUMBER = 0
      IMPORTING
          RETURN = return_item.
  append return_item to return.
endif.
if mess_type = 'E'.
  call function 'BALW_BAPIRETURN_GET2'        "BAPI was not successful
      EXPORTING
          TYPE   = 'E'
          CL     = 'BC_IBF'
          NUMBER = 1
      IMPORTING
          RETURN = return_item.
  append return_item to return.
endif.
endform.
********* End of form for final return message ************************