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:
rickyandClaude Opus 5.5 committed 2026-11-05 07:24:28 -05:00
1 parent 4d3dc7150a
commit f94ccee99b
13 files changed
+237 -15

No files matched your search

@@ -0,0 +1,65 @@
import { Controller } from "@hotwired/stimulus"
import { nextDue, describeDue } from "next_due"
// Live next-due preview (and prefill of an AD's next-due tach reading) from
// the selected AD/inspection type's recurrence rule.
//
// data-controller="next-due" data-next-due-rules-value='{"12": {"hours": 100, "months": 12, "days": 0}}'
// targets: rule (select), date, hours, dueHours (optional), permanent (optional), preview
export default class extends Controller {
static targets = ["rule", "date", "hours", "dueHours", "permanent", "preview"]
static values = { rules: Object }
connect() {
// Don't overwrite a reading that's already filled in (e.g. editing a record).
this.manual = this.hasDueHoursTarget && parseFloat(this.dueHoursTarget.value) > 0
this.recalc()
}
// The user typed their own next-due reading: stop overwriting it.
markManual() {
this.manual = this.dueHoursTarget.value.trim() !== "" && parseFloat(this.dueHoursTarget.value) > 0
this.renderPreview()
}
// Inputs that the rule depends on changed: recompute, unless overridden.
changed() {
this.manual = false
this.recalc()
}
recalc() {
const due = this.computeDue()
if (this.hasDueHoursTarget && !this.manual && !this.isPermanent) {
this.dueHoursTarget.value = due.hours != null ? due.hours.toFixed(1) : "0"
}
this.renderPreview(due)
}
renderPreview(due = this.computeDue()) {
if (!this.hasPreviewTarget) return
const rule = this.currentRule
let text
if (this.isPermanent) text = "Permanent compliance: never due again."
else if (!this.ruleTarget.value) text = ""
else if (!rule) text = "One-time: no recurring due date."
else {
if (this.hasDueHoursTarget && this.manual) due = { ...due, hours: parseFloat(this.dueHoursTarget.value) }
text = describeDue(due) ? `Next due: ${describeDue(due)}` : "Enter the date and hours to calculate the next due."
}
this.previewTarget.textContent = text
this.previewTarget.hidden = text === ""
}
computeDue() {
return nextDue(this.currentRule, this.dateTarget.value, this.hoursTarget.value)
}
get currentRule() {
return this.rulesValue[this.ruleTarget.value] || null
}
get isPermanent() {
return this.hasPermanentTarget && this.permanentTarget.checked
}
}
+34
View File
@@ -0,0 +1,34 @@
// Next-due math, mirroring Compliance.next_due in app/services/compliance.rb:
// months land on the END of the month, then days are added; hours are added
// to the hours at completion. Dates are "YYYY-MM-DD" strings (no timezones).
export function nextDue(rule, dateStr, hours) {
const due = {}
if (!rule) return due
const months = Number(rule.months) || 0
const days = Number(rule.days) || 0
const interval = Number(rule.hours) || 0
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(dateStr || "")
if (m && (months > 0 || days > 0)) {
let d = new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3]))
if (months > 0) d = new Date(d.getFullYear(), d.getMonth() + months + 1, 0) // last day of month
if (days > 0) d.setDate(d.getDate() + days)
due.date = [d.getFullYear(), String(d.getMonth() + 1).padStart(2, "0"), String(d.getDate()).padStart(2, "0")].join("-")
}
const base = parseFloat(hours)
if (interval > 0 && !Number.isNaN(base)) due.hours = Math.round((base + interval) * 10) / 10
return due
}
export function describeDue(due) {
const parts = []
if (due.hours != null) parts.push(`${due.hours.toLocaleString("en-US", { minimumFractionDigits: 1, maximumFractionDigits: 1 })} tach`)
if (due.date) {
const [y, mo, d] = due.date.split("-").map(Number)
parts.push(new Date(y, mo - 1, d).toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" }))
}
if (parts.length === 0) return ""
return parts.length > 1 ? `${parts.join(" or ")}, whichever comes first` : parts[0]
}