Class: TripLeg

Inherits:
Object show all
Defined in:
lib/dbr_models/trip_leg.rb,
lib/models/trip_leg.rb

Overview

This TripLeg is overriding the TripLeg model in fossil that usually would go to fos. It is pulling data from the mysql in memory database which is a copy of the fos trip leg table but with an added id column, and single ids for all foreign keys, so the associations are all rewritten to use the single primary key. The other methods are special methods for datamart trip_leg summary table

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fuel_data_by_fbo_keys(trip_legs, fbo_keys) ⇒ Object



958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# File 'lib/models/trip_leg.rb', line 958

def fuel_data_by_fbo_keys trip_legs, fbo_keys
  data =  Hash.new { |hash, key| hash[key] = Array.new }
  trip_legs.each do |leg|
    leg.fuel_expenses.each do |expense|

      fbo_name = leg.send( "#{expense.type_string}_fbo__name" )
      match = false
      fbo_keys.each do |key|
        if fbo_name.index(key) == 0 # fbo name can have suffix like A, B, SOUTH, EAST, etc.
          match = true
          break
        end
      end
      
      data[fbo_name].push( expense.fuel_passdown_hash ) if match
    end
  end
  data
end

.fuel_passdown_data(start_date, end_date, fbo_keys) ⇒ Object



943
944
945
946
# File 'lib/models/trip_leg.rb', line 943

def fuel_passdown_data start_date, end_date, fbo_keys
  trip_legs = TripLeg.get_fuel_passdown_trip_legs( start_date, end_date )
  fuel_data_by_fbo_keys( trip_legs, fbo_keys )
end

.get_fuel_passdown_trip_legs(start_date, end_date) ⇒ Object



948
949
950
951
952
953
954
955
956
# File 'lib/models/trip_leg.rb', line 948

def get_fuel_passdown_trip_legs start_date, end_date
  start_date = start_date.to_fos_days
  end_date = end_date.to_fos_days
  TripLeg.filter(:cancelled => 0, :status => [0,1,2]).
          filter(~{:'leg state' => 5}).
          filter({:'dept date act local' => start_date..end_date,
                  :'depart date - local' => start_date..end_date}.sql_or).
          order(:depart_date_local).all
end

Instance Method Details

#actual_arrival_date_time_baseObject



728
729
730
# File 'lib/models/trip_leg.rb', line 728

def actual_arrival_date_time_base
  (actual_arrival_date_time_gmt.to_time.utc + (home_tz_gmt_offset/10).hours).to_datetime
end

#actual_arrival_date_time_gmtObject

Fixed to check if actual fields are populated, on older records these fields are not populated so we can’t use them. Updated to calculated actual times based on dept datetime and block time actual. Jade Tucker 2/11/2011



676
677
678
679
680
681
682
683
# File 'lib/models/trip_leg.rb', line 676

def actual_arrival_date_time_gmt
  if arr_date_act_gmt != 0 then
    DateTime.from_fos_date_time(arr_date_act_gmt, arriv_time_act_gmt)
  else
    block_time_minutes = block_time_actual ? block_time_actual.minutes : 0
    (actual_departure_date_time_gmt.to_time.utc + block_time_minutes).to_datetime 
  end
end

#actual_departure_date_time_baseObject



721
722
723
# File 'lib/models/trip_leg.rb', line 721

def actual_departure_date_time_base
  (actual_departure_date_time_gmt.to_time.utc + (home_tz_gmt_offset/10).hours).to_datetime
end

#actual_land_date_time_gmtObject



685
686
687
688
689
690
# File 'lib/models/trip_leg.rb', line 685

def actual_land_date_time_gmt
  time = land_time_act_gmt
  time -= 60*24 if land_time_act_gmt > arriv_time_act_gmt
  date = (arr_date_act_gmt > 0 ? arr_date_act_gmt : arrival_date_gmt)
  DateTime.from_fos_date_time(date, time)
end

#actual_takeoff_date_time_gmtObject



665
666
667
668
669
# File 'lib/models/trip_leg.rb', line 665

def actual_takeoff_date_time_gmt
  time = t_o_time_act_gmt
  time += 60*24 if t_o_time_act_gmt < dept_time_act_gmt
  DateTime.from_fos_date_time(dept_date_act_gmt, time)
end

#actual_time_zone_changeObject



842
843
844
# File 'lib/models/trip_leg.rb', line 842

def actual_time_zone_change
  time_zone_change.to_f/10.0
end

#arrival_airport_zipcodeObject



875
876
877
878
879
# File 'lib/models/trip_leg.rb', line 875

def arrival_airport_zipcode
  arrival_airport__city_state_zip and
    arrival_airport__city_state_zip.match(/(\d{5}-\d{4}$|\d{5})$/) and
      $1
end

#arrival_ap_idObject

Datamart Methods



853
854
855
# File 'lib/models/trip_leg.rb', line 853

def arrival_ap_id
  arrival_airport_id ? "#{arrival_ap_prefix}#{arrival_airport_id}" : ""
end

#arrival_diff_in_minutesObject



885
886
887
# File 'lib/models/trip_leg.rb', line 885

def arrival_diff_in_minutes
   (leg_status == "VERI" or leg_status == "FLOG") ? ((actual_arrival_date_time_local - planned_arrival_date_time_local)*1440).to_i : nil
end

#arrival_fbo_is_primaryObject



861
862
863
# File 'lib/models/trip_leg.rb', line 861

def arrival_fbo_is_primary
  arrival_fbo__primary == 1 ? 'Yes' : 'No'
end

#arrival_fuel_costObject



920
921
922
# File 'lib/models/trip_leg.rb', line 920

def arrival_fuel_cost
  arrival_fuel_expenses.inject(0) { |sum, e| sum + e.fuel_cost }
end

#arrival_fuel_expensesObject



912
913
914
# File 'lib/models/trip_leg.rb', line 912

def arrival_fuel_expenses
  fuel_expenses.select { |expense| expense.arrival_airport == 1 }
end

#arrival_fuel_purchased_quantityObject



932
933
934
# File 'lib/models/trip_leg.rb', line 932

def arrival_fuel_purchased_quantity
  arrival_fuel_expenses.inject(0) { |sum, e| sum + e.quantity.to_i }
end

#arrival_icao_expandedObject



834
835
836
# File 'lib/models/trip_leg.rb', line 834

def arrival_icao_expanded
  "#{arrival_icao_val} #{arrival_airport__name} #{arrival_airport__city} ,#{arrival_airport__state_abbreviation}"
end

#arrival_icao_valObject



826
827
828
# File 'lib/models/trip_leg.rb', line 826

def arrival_icao_val
  arrival_icao == nil || arrival_icao.blank? ? arrival_ap_prefix + arrival_airport_id : arrival_icao
end

#custom_statute_milesObject

converts nautical miles to statute miles with fractional bit silly because its so similar in value to statute_miles



847
848
849
850
# File 'lib/models/trip_leg.rb', line 847

def custom_statute_miles
  return 0 unless nautical_miles
  ((nautical_miles*1.150777*10).to_i)/10.0
end

#delayed?Boolean

Returns:

  • (Boolean)


788
789
790
# File 'lib/models/trip_leg.rb', line 788

def delayed?
  Time.now.utc > planned_departure_date_time_gmt and !departed?
end

#depart_ap_idObject



857
858
859
# File 'lib/models/trip_leg.rb', line 857

def depart_ap_id
  depart_airport_id ? "#{depart_ap_prefix}#{depart_airport_id}" : ""
end

#departed?Boolean

Returns:

  • (Boolean)


784
785
786
# File 'lib/models/trip_leg.rb', line 784

def departed?
  dept_date_act_gmt!=0
end

#departure_airport_zipcodeObject



869
870
871
872
873
# File 'lib/models/trip_leg.rb', line 869

def departure_airport_zipcode
  departure_airport__city_state_zip and
    departure_airport__city_state_zip.match(/(\d{5}-\d{4}$|\d{5})$/) and
      $1
end

#departure_diff_in_minutesObject



881
882
883
# File 'lib/models/trip_leg.rb', line 881

def departure_diff_in_minutes
   (leg_status == "VERI" or leg_status == "FLOG") ? ((actual_departure_date_time_local - planned_departure_date_time_local)*1440).to_i : nil
end

#departure_fbo_is_primaryObject



865
866
867
# File 'lib/models/trip_leg.rb', line 865

def departure_fbo_is_primary
  departure_fbo__primary == 1 ? 'Yes' : 'No'
end

#departure_fuel_costObject



916
917
918
# File 'lib/models/trip_leg.rb', line 916

def departure_fuel_cost
  departure_fuel_expenses.inject(0) { |sum, e| sum + e.fuel_cost }
end

#departure_fuel_expensesObject



908
909
910
# File 'lib/models/trip_leg.rb', line 908

def departure_fuel_expenses
  fuel_expenses.select { |expense| expense.arrival_airport == 0 }
end

#departure_fuel_purchased_quantityObject



928
929
930
# File 'lib/models/trip_leg.rb', line 928

def departure_fuel_purchased_quantity
  departure_fuel_expenses.inject(0) { |sum, e| sum + e.quantity.to_i }
end

#departure_icao_expandedObject



830
831
832
# File 'lib/models/trip_leg.rb', line 830

def departure_icao_expanded
  "#{departure_icao_val} #{departure_airport__name} #{departure_airport__city} ,#{departure_airport__state_abbreviation}"
end

#departure_icao_valObject

This method guarantees an icao for a view that needs one. If there is an icao, use it, otherwise make it from the prefix and airport_id .. which works as an icao, but beware, because these put together icao’s that you make are valid icao codes, but you can’t search the airport based it, since these airport rows don’t have an icao code at all, they only have the prefix and airport_id. whacky.



822
823
824
# File 'lib/models/trip_leg.rb', line 822

def departure_icao_val
  departure_icao == nil || departure_icao.blank? ? depart_ap_prefix + depart_airport_id : departure_icao
end

#ebt_timeObject



757
758
759
760
761
762
# File 'lib/models/trip_leg.rb', line 757

def ebt_time
  # subtracting the two datetimes yields a fraction of a day, so multiplying by minutes
  # in day ( 1440 ) to get total minutes difference. assumes flights not more than 24 hours.
  minutes_between = (planned_arrival_date_time_gmt - planned_departure_date_time_gmt) * 1440;
  DateTime.from_minutes(minutes_between)
end

#fuel_expensesObject

Fuel Expense Stuff



904
905
906
# File 'lib/models/trip_leg.rb', line 904

def fuel_expenses
  flight_log_expenses.select{ |expense| expense.type == 1 }
end

#is_charter?Boolean

Returns:

  • (Boolean)


792
793
794
# File 'lib/models/trip_leg.rb', line 792

def is_charter?
  [100, 11].include? leg_type_code
end

#is_completed_leg?Boolean

Flight explorer is continually updating the land time for a flight, so its really not possible to tell if a flight is completed by checking that the arrival date or time is no longer 0/1440.

Returns:

  • (Boolean)


780
781
782
# File 'lib/models/trip_leg.rb', line 780

def is_completed_leg?
  (verified? or Time.now.utc > actual_arrival_date_time_gmt) and cancel_code == 0
end

#is_deadhead?Boolean

Returns:

  • (Boolean)


764
765
766
767
# File 'lib/models/trip_leg.rb', line 764

def is_deadhead?
  deadhead==1 or pax_count==0
#    (status==1 or verify_date > 0) and (deadhead==1 or pax_count==0) and [107, 115, 108, 104].include? leg_type_code
end

#is_flown?Boolean

Returns:

  • (Boolean)


773
774
775
# File 'lib/models/trip_leg.rb', line 773

def is_flown?
  ((verified? or flight_time_actual > 0) and cancel_code == 0)
end

#leg_classObject



889
890
891
892
893
894
895
896
897
898
899
900
901
# File 'lib/models/trip_leg.rb', line 889

def leg_class
  my_class = case aircraft_type_id
    when 'C750' then 'C750'
    when 'CL30' then 'CL30'
    # when 'Managed' then 'Managed'  #TODO There is no way to know if the plane is managed other than being given specific tail numbers
    when 'PRM1' then 'Premier'
    when 'BE35' then 'King Air'
    else 'NA'
  end

  my_class = 'NA' if (aircraft__owner_name =~ /xojet/i) == nil
  my_class
end

#leg_statusObject



806
807
808
809
810
811
812
813
814
815
816
# File 'lib/models/trip_leg.rb', line 806

def leg_status
  return "VERI" if verify_date > 0 or status == 1
  return "FLOG" if flight_time_actual > 0
  return "CANC" if cancel_code == 1 or [10,23].include? status
  case status.to_i
    when 21 then "MAINT"
    when 0 then "SCHED"
    when 20 then "SCHED" #really is "RESVD"
    else status
  end
end

#passenger_listObject

sort on lead_pax value to lead pax = 1 is first and the lead_pax values of 0 are all after



797
798
799
800
801
802
803
804
# File 'lib/models/trip_leg.rb', line 797

def passenger_list
  return '' if trip__passengers.blank? or is_deadhead?
  trip__passengers.sort_by{|tp| -tp.lead_pax}.collect do |tp|
    if (leg_number >= tp.departure_leg_number and leg_number <= tp.arrival_leg_number) or tp.all_legs == 1
      tp.name + (tp.lead_pax==1 ? " (lead pax)" : '' )
    end
  end.compact.uniq.join(' : ')
end

#picObject



747
# File 'lib/models/trip_leg.rb', line 747

def pic; pilot_code(:pic); end

#pic_crewObject



754
# File 'lib/models/trip_leg.rb', line 754

def pic_crew; pilot_crew(:pic); end

#pilot_code(type) ⇒ Object

get pic or sic or purser for this trip leg. type should be :pic or :sic or :pur



743
744
745
746
# File 'lib/models/trip_leg.rb', line 743

def pilot_code(type)
  crew_leg = pilot_crew(type)
  crew_leg ? crew_leg.crew : ''
end

#pilot_crew(type) ⇒ Object



750
751
752
753
# File 'lib/models/trip_leg.rb', line 750

def pilot_crew(type)
  return nil unless type
  crew_legs.find{|cl| cl.position_code_code == type.to_s.upcase }
end

#sicObject



748
# File 'lib/models/trip_leg.rb', line 748

def sic; pilot_code(:sic); end

#sic_crewObject



755
# File 'lib/models/trip_leg.rb', line 755

def sic_crew; pilot_crew(:sic); end

#total_fuel_costObject



924
925
926
# File 'lib/models/trip_leg.rb', line 924

def total_fuel_cost
  arrival_fuel_cost + departure_fuel_cost
end

#total_fuel_purchased_quantityObject



936
937
938
# File 'lib/models/trip_leg.rb', line 936

def total_fuel_purchased_quantity
  departure_fuel_purchased_quantity + arrival_fuel_purchased_quantity
end

#total_trip_statute_milesObject



838
839
840
# File 'lib/models/trip_leg.rb', line 838

def total_trip_statute_miles
  trip__total_statute_miles.to_f/10.0 
end

#verified?Boolean

Returns:

  • (Boolean)


769
770
771
# File 'lib/models/trip_leg.rb', line 769

def verified?
  (status == 1 or verify_date > 0)
end