mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Calculate next-due for AD compliance and inspections from their rules
- AD compliance "next due at tach reading" is prefilled from the AD's hour recurrence (hours at compliance + interval), recalculates live as the AD/date/hours change, keeps a typed-in override, and is filled server-side when left blank. - Live "Next due" preview on AD compliance and inspection record forms (hours and/or end-of-month date, whichever comes first), mirroring Compliance.next_due so preview and status agree. - Reject an interval entered as the next-due reading (e.g. "100" for "every 100 hours"), which made a complied AD show as overdue. - ADs table shows hours remaining next to the due tach reading. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4d3dc7150a
commit
f94ccee99b
13 files changed
+237
-15
No files matched your search
@@ -7,10 +7,34 @@ class AdCompliance < ApplicationRecord
|
||||
|
||||
validates :date_complied, :compliance_notes, presence: true
|
||||
validate { errors.add(:base, "Must belong to an aircraft or component") unless aircraft_id || component_id }
|
||||
validate :next_due_is_a_future_reading
|
||||
before_validation :fill_next_due, if: -> { next_due_at_time.to_d.zero? }
|
||||
|
||||
scope :recent_first, -> { order(date_complied: :desc, created_at: :desc) }
|
||||
|
||||
# Prefill next_due_at_time from the AD's hour recurrence (hours at compliance
|
||||
# + interval). Only fills a blank/zero value, so a typed-in reading wins.
|
||||
def fill_next_due
|
||||
return if permanent? || airworthiness_directive.nil?
|
||||
base = aircraft_hours_at_compliance || aircraft&.tach_time
|
||||
due = Compliance.next_due(airworthiness_directive.due_rule, date: date_complied, hours: base)
|
||||
self.next_due_at_time = due[:hours] if due[:hours]
|
||||
end
|
||||
|
||||
def self.for_aircraft(aircraft)
|
||||
where(aircraft_id: aircraft.id).or(where(component_id: aircraft.components.select(:id)))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# next_due_at_time is an absolute tach reading, not an interval. Catch the
|
||||
# easy mistake of entering "100" for "every 100 hours".
|
||||
def next_due_is_a_future_reading
|
||||
due = next_due_at_time.to_d
|
||||
return if permanent? || due <= 0
|
||||
base = aircraft_hours_at_compliance || aircraft&.tach_time
|
||||
return unless base && due <= base
|
||||
errors.add(:next_due_at_time, "must be a tach reading after the compliance hours (#{base}). " \
|
||||
"For “every 100 hours” enter #{base + 100}, not 100")
|
||||
end
|
||||
end
|
||||
@@ -27,6 +27,11 @@ class AirworthinessDirective < ApplicationRecord
|
||||
def conditional? = compliance_type == "conditional"
|
||||
def bulletin_label = BULLETIN_LABELS[bulletin_type]
|
||||
|
||||
# Recurrence rule for next-due math (AD status uses hours and months).
|
||||
def due_rule
|
||||
{ hours: recurring_hours.to_f, months: recurring_months, days: 0 } if recurring?
|
||||
end
|
||||
|
||||
def latest_compliance_for(aircraft)
|
||||
ad_compliances.for_aircraft(aircraft).order(date_complied: :desc, created_at: :desc).first
|
||||
end
|
||||
|
||||
@@ -16,6 +16,10 @@ class InspectionType < ApplicationRecord
|
||||
|
||||
def to_s = name
|
||||
|
||||
def due_rule
|
||||
{ hours: recurring_hours.to_f, months: recurring_months, days: recurring_days } if recurring?
|
||||
end
|
||||
|
||||
def interval_description
|
||||
return "One-time" unless recurring?
|
||||
parts = []
|
||||
|
||||
Reference in new issue
Block a user