diff --git a/app/controllers/flight_logs_controller.rb b/app/controllers/flight_logs_controller.rb index 28322f7..55a89eb 100644 --- a/app/controllers/flight_logs_controller.rb +++ b/app/controllers/flight_logs_controller.rb @@ -1,7 +1,9 @@ class FlightLogsController < ApplicationController include AircraftScoped before_action :require_owner!, only: :destroy - before_action :set_flight_log, only: %i[show engine_data_series attach_engine_data remove_engine_data] + before_action :set_flight_log, only: %i[show edit update engine_data_series attach_engine_data remove_engine_data] + before_action :require_flight_editor!, only: %i[edit update] + helper_method :can_edit_flight? before_action :require_engine_data_access!, only: %i[attach_engine_data remove_engine_data] helper_method :manage_engine_data? @@ -99,6 +101,22 @@ class FlightLogsController < ApplicationController redirect_to aircraft_flight_log_path(@aircraft, @flight_log), notice: t("flash.flight_logs.engine_data_removed") end + def edit + @meters_editable = @flight_log.meters_editable? + end + + # A correction. The readings only change on the latest flight (see + # FlightLog#meters_editable?); on earlier ones they're left as they are. + def update + @meters_editable = @flight_log.meters_editable? + attributes = flight_log_params.except(:client_key) + attributes = attributes.except(*FlightLog::METERS) unless @meters_editable + HoursService.new(@aircraft).update_flight!(@flight_log, attributes) + redirect_to aircraft_flight_log_path(@aircraft, @flight_log), notice: t("flash.flight_logs.flight_corrected") + rescue ActiveRecord::RecordInvalid + render :edit, status: :unprocessable_entity + end + # Deleting a flight does not roll back hours; correct those with an hours update. def destroy @aircraft.flight_logs.find(params[:id]).destroy! @@ -108,14 +126,19 @@ class FlightLogsController < ApplicationController private + def can_edit_flight?(flight) = flight.editable_by?(current_user, role: current_role) + + def require_flight_editor! + return if can_edit_flight?(@flight_log) + redirect_to aircraft_flight_log_path(@aircraft, @flight_log), alert: t("flash.flight_logs.only_owners_or_the") + end + def set_flight_log @flight_log = @aircraft.flight_logs.find(params[:id]) end # Owners, or the pilot who logged the flight. - def manage_engine_data? - owner? || (@flight_log.user_id.present? && @flight_log.user_id == current_user.id) - end + def manage_engine_data? = can_edit_flight?(@flight_log) def require_engine_data_access! return if manage_engine_data? diff --git a/app/models/flight_log.rb b/app/models/flight_log.rb index 3594fb3..8162f76 100644 --- a/app/models/flight_log.rb +++ b/app/models/flight_log.rb @@ -26,6 +26,21 @@ class FlightLog < ApplicationRecord [ departure_location, destination_location ].compact_blank.join(" → ") end + # The meter readings that move the aircraft's and components' hours. + METERS = %w[tach_out tach_in tach_time hobbs_out hobbs_in hobbs_time].freeze + + # Its readings can be corrected only while it's the last flight logged and + # nothing has changed the hours since (an hours update, an import): only + # then is the aircraft's time just this flight's on top of the one before. + def meters_editable? + aircraft.flight_logs.maximum(:id) == id && (tach_in.nil? || tach_in == aircraft.tach_time) + end + + # Owners can correct any flight; a pilot, the flights they logged. + def editable_by?(user, role:) + role.in?(%w[admin owner]) || (user_id.present? && user_id == user.id) + end + private # Reject files the engine data parser can't read, before they're stored. diff --git a/app/services/hours_service.rb b/app/services/hours_service.rb index a6f3cb0..2b04cc0 100644 --- a/app/services/hours_service.rb +++ b/app/services/hours_service.rb @@ -67,8 +67,53 @@ class HoursService flight end + # Correct a saved flight. Its readings change only when it's the latest + # (FlightLog#meters_editable?); the aircraft and its components then move by + # the difference. Its oil and fuel records follow. With both readings given, + # the duration is worked out from them. + def update_flight!(flight, attributes) + Aircraft.transaction do + before = flight.attributes.slice(*FlightLog::METERS, "date", "departure_location", "destination_location", + "oil_added", "fuel_added", "route", "notes") + flight.assign_attributes(attributes) + flight.tach_time = flight.tach_in - flight.tach_out if flight.tach_in && flight.tach_out + flight.hobbs_time = flight.hobbs_in - flight.hobbs_out if flight.hobbs_in && flight.hobbs_out + flight.save! + + tach_delta = flight.tach_time - before["tach_time"].to_d + hobbs_delta = flight.hobbs_time.to_d - before["hobbs_time"].to_d + if tach_delta.nonzero? || hobbs_delta.nonzero? + @aircraft.tach_time += tach_delta + @aircraft.hobbs_time += hobbs_delta if @aircraft.hobbs_time + @aircraft.save! + cascade_to_components(@aircraft.hobbs? && flight.hobbs_time ? hobbs_delta : tach_delta, clamp: true) + end + sync_consumables(flight) + + changed = before.keys.reject { |key| before[key] == flight[key] } + @aircraft.log_event!("flight", I18n.t("#{H}.flight_corrected", date: I18n.l(flight.date, format: :app)), user: @user, + notes: changed.map { |key| FlightLog.human_attribute_name(key) }.join(", ").presence) + end + flight + end + private + # The flight's oil and fuel records match what it says was added. + def sync_consumables(flight) + { "oil" => [ flight.oil_added, flight.oil_level_after, flight.oil_added_type ], + "fuel" => [ flight.fuel_added, flight.fuel_level_after, flight.fuel_added_type ] }.each do |type, (qty, level, kind)| + record = flight.consumable_records.find_by(record_type: type) + if qty.to_f.positive? + record ||= @aircraft.consumable_records.new(record_type: type, flight_log: flight, flight_hours: @aircraft.tach_time, + notes: I18n.t("#{H}.auto_created_from_flight_log")) + record.update!(date: flight.date, quantity_added: qty, level_after: level, consumable_type: kind) + else + record&.destroy! + end + end + end + def cascade_to_components(delta, clamp: false) @aircraft.components.in_use.find_each do |c| his = c.hours_in_service + delta diff --git a/app/views/flight_logs/_fields.html.erb b/app/views/flight_logs/_fields.html.erb new file mode 100644 index 0000000..a705148 --- /dev/null +++ b/app/views/flight_logs/_fields.html.erb @@ -0,0 +1,38 @@ +<%# The flight's fields, for Log Flight and for correcting one. Its meter + readings are locked (meters_locked) on all but the latest flight. %> +<% meters_locked = local_assigns.fetch(:meters_locked, false) %> +
+ <%= f.field :date, as: :date, required: true %> + <%= render "shared/airport_field", f: f, attribute: :departure_location, label: t("flight_logs.new.from"), aircraft: @aircraft, placeholder: t("flight_logs.new.koqu") %> + <%= render "shared/airport_field", f: f, attribute: :destination_location, label: t("flight_logs.new.to"), aircraft: @aircraft, placeholder: t("flight_logs.new.code_or_name") %> +
+ <% if meters_locked %> +

<%= t("flight_logs.fields.meters_locked_html", link: link_to(t("flight_logs.fields.update_hours"), aircraft_path(@aircraft), class: "link")) %>

+ <% end %> + <%= tag.fieldset class: "grid gap-4 sm:grid-cols-3", disabled: meters_locked do %> + <%= t("flight_logs.new.tach_required") %> + <%= f.field :tach_out, as: :number, step: 0.1, label: t("flight_logs.new.tach_out") %> + <%= f.field :tach_in, as: :number, step: 0.1, label: t("flight_logs.new.tach_in") %> + <%= f.field :tach_time, as: :number, step: 0.1, label: t("flight_logs.new.or_duration"), hint: t("flight_logs.new.calculated_from_out_in_if") %> + <% end %> + <%= tag.fieldset class: "grid gap-4 sm:grid-cols-3", disabled: meters_locked do %> + <%= t("flight_logs.new.hobbs_optional") %> + <%= f.field :hobbs_out, as: :number, step: 0.1, label: t("flight_logs.new.hobbs_out") %> + <%= f.field :hobbs_in, as: :number, step: 0.1, label: t("flight_logs.new.hobbs_in") %> + <%= f.field :hobbs_time, as: :number, step: 0.1, label: t("flight_logs.new.or_duration") %> + <% end %> +
+ <%= t("flight_logs.new.oil_fuel_added") %> + <%= f.field :oil_added, as: :number, step: 0.25, label: t("flight_logs.new.oil_added_qt") %> + <%= f.field :oil_level_after, as: :number, step: 0.25, label: t("flight_logs.new.oil_level_after_qt") %> + <%= f.field :oil_added_type, label: t("flight_logs.new.oil_type"), placeholder: t("flight_logs.new.aeroshell_w100") %> + <%= f.field :fuel_added, as: :number, step: 0.1, label: t("flight_logs.new.fuel_added_gal") %> + <%= f.field :fuel_level_after, as: :number, step: 0.1, label: t("flight_logs.new.fuel_level_after_gal") %> + <%= f.field :fuel_added_type, label: t("flight_logs.new.fuel_type"), placeholder: t("flight_logs.new.n100ll") %> +
+ <%= f.field :route, as: :textarea, rows: 2 %> + <%= f.field :notes, as: :textarea, rows: 2 %> +
+ <%= f.field :track_log, as: :file, label: t("flight_logs.new.track_log_gpx_kml_csv") %> + <%= f.field :engine_data, as: :file, label: t("flight_logs.new.engine_monitor_data"), hint: t("flight_logs.new.cgr_30p_cgr_30c_flight"), accept: ".csv,text/csv" %> +
diff --git a/app/views/flight_logs/edit.html.erb b/app/views/flight_logs/edit.html.erb new file mode 100644 index 0000000..e5911b7 --- /dev/null +++ b/app/views/flight_logs/edit.html.erb @@ -0,0 +1,12 @@ +<% content_for :title, t(".correct_flight") %> +<%= render "shared/aircraft_header", aircraft: @aircraft %> +<%= form_with model: [@aircraft, @flight_log], class: "card card-body space-y-5 max-w-3xl" do |f| %> +

<%= t(".correct_the_flight_on", date: fmt_date(@flight_log.date_was)) %>

+ <%= f.errors_summary %> + <%= render "flight_logs/fields", f: f, meters_locked: !@meters_editable %> + <% if @meters_editable %>

<%= t(".changing_the_readings") %>

<% end %> +
+ <%= f.submit t(".save"), data: { turbo_submits_with: t("flight_logs.new.sending") } %> + <%= link_to t(".cancel"), aircraft_flight_log_path(@aircraft, @flight_log), class: "btn-secondary" %> +
+<% end %> diff --git a/app/views/flight_logs/new.html.erb b/app/views/flight_logs/new.html.erb index 54bcd79..92d7329 100644 --- a/app/views/flight_logs/new.html.erb +++ b/app/views/flight_logs/new.html.erb @@ -6,38 +6,7 @@ <%= f.hidden_field :client_key, data: { offline_form_target: "key" } %> <%= f.errors_summary %> -
- <%= f.field :date, as: :date, required: true %> - <%= render "shared/airport_field", f: f, attribute: :departure_location, label: t(".from"), aircraft: @aircraft, placeholder: t(".koqu") %> - <%= render "shared/airport_field", f: f, attribute: :destination_location, label: t(".to"), aircraft: @aircraft, placeholder: t(".code_or_name") %> -
-
- <%= t(".tach_required") %> - <%= f.field :tach_out, as: :number, step: 0.1, label: t(".tach_out") %> - <%= f.field :tach_in, as: :number, step: 0.1, label: t(".tach_in") %> - <%= f.field :tach_time, as: :number, step: 0.1, label: t(".or_duration"), hint: t(".calculated_from_out_in_if") %> -
-
- <%= t(".hobbs_optional") %> - <%= f.field :hobbs_out, as: :number, step: 0.1, label: t(".hobbs_out") %> - <%= f.field :hobbs_in, as: :number, step: 0.1, label: t(".hobbs_in") %> - <%= f.field :hobbs_time, as: :number, step: 0.1, label: t(".or_duration") %> -
-
- <%= t(".oil_fuel_added") %> - <%= f.field :oil_added, as: :number, step: 0.25, label: t(".oil_added_qt") %> - <%= f.field :oil_level_after, as: :number, step: 0.25, label: t(".oil_level_after_qt") %> - <%= f.field :oil_added_type, label: t(".oil_type"), placeholder: t(".aeroshell_w100") %> - <%= f.field :fuel_added, as: :number, step: 0.1, label: t(".fuel_added_gal") %> - <%= f.field :fuel_level_after, as: :number, step: 0.1, label: t(".fuel_level_after_gal") %> - <%= f.field :fuel_added_type, label: t(".fuel_type"), placeholder: t(".n100ll") %> -
- <%= f.field :route, as: :textarea, rows: 2 %> - <%= f.field :notes, as: :textarea, rows: 2 %> -
- <%= f.field :track_log, as: :file, label: t(".track_log_gpx_kml_csv") %> - <%= f.field :engine_data, as: :file, label: t(".engine_monitor_data"), hint: t(".cgr_30p_cgr_30c_flight"), accept: ".csv,text/csv" %> -
+ <%= render "flight_logs/fields", f: f %>

<%= t(".saving_adds_the_tach_time") %>

<%= f.submit t(".log_flight"), data: { turbo_submits_with: t(".sending") } %> diff --git a/app/views/flight_logs/show.html.erb b/app/views/flight_logs/show.html.erb index 8dd62a9..54bf4ed 100644 --- a/app/views/flight_logs/show.html.erb +++ b/app/views/flight_logs/show.html.erb @@ -2,7 +2,10 @@

<%= t(".flight_on_html", date: (fmt_date(@flight_log.date))) %>

- <% if owner? %><%= delete_button t(".delete"), aircraft_flight_log_path(@aircraft, @flight_log), confirm: t(".delete_this_flight_log_aircraft") %><% end %>
+
+ <% if can_edit_flight?(@flight_log) %><%= link_to t(".edit"), edit_aircraft_flight_log_path(@aircraft, @flight_log), class: "btn-secondary btn-sm" %><% end %> + <% if owner? %><%= delete_button t(".delete"), aircraft_flight_log_path(@aircraft, @flight_log), confirm: t(".delete_this_flight_log_aircraft") %><% end %> +
<%# One item per row on a phone (label, then value); two columns on wider screens. %>
<%= t(".tach") %>
<%= hours(@flight_log.tach_time) %> <%= "(#{@flight_log.tach_out} → #{@flight_log.tach_in})" if @flight_log.tach_out && @flight_log.tach_in %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index 80ae640..6119a84 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -821,6 +821,7 @@ en: flight_logs: flight_already_logged: Flight already logged. flight_logged_hrs: Flight logged (%{tach_time} hrs). + flight_corrected: Flight corrected. choose_an_engine_data: Choose an engine data file. engine_data_attached: Engine data attached. engine_data_removed: Engine data removed. @@ -2331,6 +2332,15 @@ en: skipped: Skipped warning: Warning flight_logs: + edit: + correct_flight: Correct Flight + correct_the_flight_on: Correct the flight on %{date} + changing_the_readings: Changing the readings moves the aircraft's and its components' hours by the difference. + save: Save + cancel: Cancel + fields: + meters_locked_html: The readings can be corrected only on the latest flight, before the hours change again. To correct the hours, use %{link} on the Overview. + update_hours: Update Hours days: gps_track_attached: GPS track attached engine_data_attached: Engine data attached @@ -2437,6 +2447,7 @@ en: flight_on_html: Flight on %{date} delete_this_flight_log_aircraft: Delete this flight log? Aircraft and component hours will NOT be rolled back. delete: Delete + edit: Edit tach: Tach hobbs: Hobbs route: Route @@ -3640,6 +3651,7 @@ en: hours_notes: 'Previous: %{old}, delta: %{delta}' consumable_added: "%{type} added: %{quantity} %{unit}" flight_logged: 'Flight logged: %{hours} hrs' + flight_corrected: 'Flight corrected: %{date}' flight_logged_route: 'Flight logged: %{hours} hrs (%{route})' aircraft_grounded_error: message: Aircraft is grounded and airworthiness enforcement is enabled diff --git a/config/routes.rb b/config/routes.rb index 83b894f..5f3e8bd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -57,7 +57,7 @@ Rails.application.routes.draw do resources :notes, except: :show do post :preview, on: :collection end - resources :flight_logs, only: %i[index new create show destroy] do + resources :flight_logs, only: %i[index new create show edit update destroy] do member do get :engine_data, action: :engine_data_series patch :engine_data, action: :attach_engine_data diff --git a/docs/guide/flights.md b/docs/guide/flights.md index f822159..0b1d4d2 100644 --- a/docs/guide/flights.md +++ b/docs/guide/flights.md @@ -17,6 +17,14 @@ On a phone or tablet with the app installed (see [Getting started](getting-start Track and engine data files attached offline are kept and sent too. +## Correct a flight + +Open the flight and choose **Edit**. Owners can correct any flight; a pilot, the flights they logged. The date, airports, route, notes, oil and fuel (its oil and fuel records follow), and the track and engine data files can always be changed. + +The meter readings can be changed only on the latest flight, and only until the hours change again (another flight, an hours update or an import). The aircraft and its installed components then move by the difference: fixing a tach in of 1001.4 to 1001.6 adds 0.2 hours. On earlier flights the readings are locked, since the flights after them started from those readings; correct the aircraft's hours with **Update Hours** instead. + +Deleting a flight (owners) doesn't take its hours back off; use **Update Hours** for that too. + ## Update hours When you don't log each flight, use **Update Hours** on the overview: enter the current meter readings and the difference is applied to every installed component. A reading lower than the current one is treated as a correction. diff --git a/test/integration/flight_edit_test.rb b/test/integration/flight_edit_test.rb new file mode 100644 index 0000000..5f069d3 --- /dev/null +++ b/test/integration/flight_edit_test.rb @@ -0,0 +1,111 @@ +require "test_helper" + +# Correcting a saved flight: everything can be fixed, but its meter readings +# only on the latest flight (the aircraft and components then move by the +# difference). Owners can correct any flight, a pilot the ones they logged. +class FlightEditTest < ActionDispatch::IntegrationTest + setup do + @owner = create_user("owner@example.com") + @pilot = create_user("pilot@example.com") + @other = create_user("other@example.com") + @a = create_aircraft(owner: @owner, pilot: @pilot, tach_time: 1000) + @a.roles.create!(user: @other, role: "pilot") + @engine = create_component(@a, hours_in_service: 500, hours_since_overhaul: 100) + end + + def log(user, tach_out:, tach_in:, **extra) + sign_in user + post aircraft_flight_logs_path(@a), params: { flight_log: { date: "2026-11-14", tach_out: tach_out, tach_in: tach_in, **extra } } + @a.flight_logs.order(:id).last + end + + test "correcting the latest flight's reading moves the aircraft and components by the difference" do + flight = log(@owner, tach_out: 1000.0, tach_in: 1001.4) + assert_equal 1001.4, @a.reload.tach_time + assert_equal 501.4, @engine.reload.hours_in_service + + get edit_aircraft_flight_log_path(@a, flight) + assert_select "fieldset[disabled]", 0 + patch aircraft_flight_log_path(@a, flight), params: { flight_log: { tach_in: 1001.6, tach_time: 1.4, notes: "Fixed typo" } } + assert_redirected_to aircraft_flight_log_path(@a, flight) + + flight.reload + assert_equal 1.6, flight.tach_time # worked out from the readings, not the stale duration + assert_equal "Fixed typo", flight.notes + assert_equal 1001.6, @a.reload.tach_time + assert_equal 501.6, @engine.reload.hours_in_service + assert_equal 101.6, @engine.hours_since_overhaul + assert_equal "Flight corrected: Nov 14, 2026", @a.events.where(category: "flight").first.event_name + assert_match "Tach in", @a.events.where(category: "flight").first.notes + end + + test "an earlier flight's readings are locked; everything else can be corrected" do + first = log(@owner, tach_out: 1000.0, tach_in: 1001.4) + log(@owner, tach_out: 1001.4, tach_in: 1003.0) + + get edit_aircraft_flight_log_path(@a, first) + assert_select "fieldset[disabled]", 2 + assert_select ".hint a[href=?]", aircraft_path(@a), text: "Update Hours" + + patch aircraft_flight_log_path(@a, first), params: { flight_log: { tach_in: 1009, tach_time: 9, destination_location: "kbos" } } + first.reload + assert_equal 1001.4, first.tach_in + assert_equal "KBOS", first.destination_location + assert_equal 1003.0, @a.reload.tach_time + assert_equal 503.0, @engine.reload.hours_in_service + end + + test "after an hours update the latest flight's readings lock too" do + flight = log(@owner, tach_out: 1000.0, tach_in: 1001.4) + patch hours_aircraft_path(@a), params: { tach_time: 1002.0 } + refute flight.reload.meters_editable? + + patch aircraft_flight_log_path(@a, flight), params: { flight_log: { tach_in: 1001.6 } } + assert_equal 1001.4, flight.reload.tach_in + assert_equal 1002.0, @a.reload.tach_time + end + + test "a pilot can correct the flights they logged, not someone else's" do + flight = log(@pilot, tach_out: 1000.0, tach_in: 1001.4) + get aircraft_flight_log_path(@a, flight) + assert_select "a[href=?]", edit_aircraft_flight_log_path(@a, flight) + patch aircraft_flight_log_path(@a, flight), params: { flight_log: { notes: "Mine" } } + assert_equal "Mine", flight.reload.notes + + delete session_path + sign_in @other + get aircraft_flight_log_path(@a, flight) + assert_select "a[href=?]", edit_aircraft_flight_log_path(@a, flight), 0 + get edit_aircraft_flight_log_path(@a, flight) + assert_redirected_to aircraft_flight_log_path(@a, flight) + patch aircraft_flight_log_path(@a, flight), params: { flight_log: { notes: "Not mine" } } + assert_equal "Mine", flight.reload.notes + + delete session_path + sign_in @owner + patch aircraft_flight_log_path(@a, flight), params: { flight_log: { notes: "Owner fixed" } } + assert_equal "Owner fixed", flight.reload.notes + end + + test "oil and fuel records follow the correction" do + flight = log(@owner, tach_out: 1000.0, tach_in: 1001.4, fuel_added: 10) + assert_equal [ 10 ], flight.consumable_records.where(record_type: "fuel").pluck(:quantity_added) + + patch aircraft_flight_log_path(@a, flight), params: { flight_log: { fuel_added: 12, oil_added: 1, oil_added_type: "W100" } } + assert_equal [ 12 ], flight.consumable_records.where(record_type: "fuel").pluck(:quantity_added) + assert_equal [ [ 1, "W100" ] ], flight.consumable_records.where(record_type: "oil").pluck(:quantity_added, :consumable_type) + + patch aircraft_flight_log_path(@a, flight), params: { flight_log: { fuel_added: "" } } + assert_empty flight.consumable_records.where(record_type: "fuel") + assert_equal 1, flight.consumable_records.where(record_type: "oil").count + end + + test "an invalid correction shows the form again and changes nothing" do + flight = log(@owner, tach_out: 1000.0, tach_in: 1001.4) + patch aircraft_flight_log_path(@a, flight), params: { flight_log: { date: "" } } + assert_response :unprocessable_entity + assert_select "fieldset[disabled]", 0 # still the latest flight + assert_equal Date.new(2026, 11, 14), flight.reload.date + assert_equal 1001.4, @a.reload.tach_time + end +end