Appointment statuses in the plugin settings; call-outs as their own entry

Statuses (name, colour, counts toward hours, order) are set up on the plugin's settings page
instead of being hard-coded. Appointments keep a fixed key, so renaming is safe; removing a status
in use asks which status to move its appointments to (done on save by a SettingsController hook).
Colours come from the settings everywhere statuses show, which also fixes Cancelled never being
coloured (cancelled/canceled mismatch).

Call-outs (an employee off work) are their own kind of entry (event_type 'call_out') with an
employee, date, days and notes, added from a button on each calendar day. They sit at the top of
the day in red stripes and never count toward hours. Migration 004 converts Call_Out appointments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-02 20:22:52 -05:00
1 parent f2a16b1821
commit 31356ac3fb
23 files changed
+536 -252

No files matched your search

@@ -51,7 +51,12 @@ class CustomerAppointmentsController < ApplicationController
.includes(:customer, :vehicle, :user)
.where('date < ?', Time.zone.now.beginning_of_day)
scope = scope.where(customer_id: @customer.id) if @customer
scope = scope.where(status: @status) if @status
# The status filter also offers call-outs, which have no status
if @status == CustomerAppointment::CALL_OUT
scope = scope.where(event_type: CustomerAppointment::CALL_OUT)
elsif @status
scope = scope.where(status: @status).where(event_type: [nil, ''])
end
scope = scope.where('date >= ?', @from.in_time_zone.beginning_of_day) if @from
scope = scope.where('date <= ?', @to.in_time_zone.end_of_day) if @to
@@ -72,6 +77,9 @@ class CustomerAppointmentsController < ApplicationController
def new
@appointment = CustomerAppointment.new
@appointment.project = @project
# The calendar's "call-out" link makes an employee's call-out instead of a customer appointment
@appointment.event_type = CustomerAppointment::CALL_OUT if params[:event_type] == CustomerAppointment::CALL_OUT
@appointment.status = RedmineQboCalendar::AppointmentStatus.default.key unless @appointment.call_out?
@appointment.date = (params[:start_date].to_date + 9.hours) if params[:start_date].present?
@appointment.customer_id = params[:customer_id] if params[:customer_id].present?
prefill_estimate_and_vehicle
@@ -137,6 +145,7 @@ class CustomerAppointmentsController < ApplicationController
:vehicle_id,
:description,
:status,
:event_type,
:user_id,
:date,
:estimated_hours,
+43 -16
View File
@@ -22,6 +22,11 @@ class CustomerAppointment < ActiveRecord::Base
validates :subject, presence: true
validates :date, presence: true
# A call-out is about the employee who's off (checked when set, so older call-outs without one still save)
validates :user, presence: true, if: -> { call_out? && (new_record? || will_save_change_to_user_id?) }
# Call-outs have no customer side; appointments get the default status when none is chosen
before_validation :prepare_entry
# Prevent scheduling on non-working days or holidays
validate :validate_working_day
@@ -32,25 +37,32 @@ class CustomerAppointment < ActiveRecord::Base
# Capitalize the first letter only; titleize mangled vehicle names and acronyms (JL -> Jl, LED -> Led)
normalizes :subject, with: ->(subject) { subject.strip.sub(/\A[a-z]/, &:upcase) }
STATUSES = %w[
Scheduled
Checked_In
In_Progress
Ready
Completed
Cancelled
Call_Out
]
# An employee off work (sick, a day off): shown on the calendar, but not an appointment with a customer
CALL_OUT = 'call_out'
# Statuses whose time should not count toward scheduled hours totals
NON_SCHEDULED_STATUSES = %w[
Cancelled
Call_Out
]
# The statuses set up in the plugin settings (see RedmineQboCalendar::AppointmentStatus)
def self.statuses
RedmineQboCalendar::AppointmentStatus.all
end
# False for cancelled / call-out appointments, which stay on the calendar but free up their hours
# Status keys, for other plugins' settings (e.g. redmine_qbo_reminders' "statuses that get reminders")
def self.status_options
statuses.map { |s| [s.name, s.key] }
end
def call_out?
event_type == CALL_OUT
end
# This appointment's status from the settings; nil for call-outs, which have none
def status_record
return nil if call_out?
RedmineQboCalendar::AppointmentStatus.find(status) || RedmineQboCalendar::AppointmentStatus.default
end
# False for call-outs and statuses like Cancelled, which stay on the calendar but free up their hours
def counts_toward_hours?
!NON_SCHEDULED_STATUSES.include?(status.to_s)
!call_out? && status_record.counts_hours?
end
# Cast datetime to Date for Redmine Calendar compatibility
@@ -97,6 +109,21 @@ class CustomerAppointment < ActiveRecord::Base
Rails.logger.info "[CustomerAppointment] #{msg}"
end
def prepare_entry
if call_out?
self.customer = nil
self.vehicle = nil
self.estimate = nil
self.status = nil
self.estimated_hours = 0
self.subject = I18n.t(:label_call_out_subject, name: user.name) if subject.blank? && user
else
self.event_type = nil
self.status = RedmineQboCalendar::AppointmentStatus.default.key if status.blank?
end
self.description = description.to_s
end
# Checked when the estimate or the hours change, so changing just the status (the quick status buttons)
# never fails on an older appointment
def validate_hours_cover_estimate
+22 -16
View File
@@ -102,11 +102,8 @@
end
%>
<%# Calendar Status Legend Key %>
<%
# Collect status keys from CustomerAppointment enum
statuses = CustomerAppointment::STATUSES
%>
<%# Calendar Status Legend Key: the statuses set up in the plugin settings, then call-outs %>
<% statuses = CustomerAppointment.statuses %>
<% if statuses.any? %>
<div class="calendar-status-legend" style="display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 8px 0;">
@@ -114,12 +111,14 @@
<%= l(:label_legend, default: 'Legend') %>:
</span>
<% statuses.each do |status_key| %>
<span class="status-key-badge status-<%= status_key.parameterize %>"
style="padding: 2px 8px; border-radius: 4px; font-size: 0.8em; font-weight: 600; text-transform: capitalize;">
<%= status_key.humanize %>
<% statuses.each do |status| %>
<span class="status-key-badge" style="<%= status.style %>; padding: 2px 8px; border-radius: 4px; font-size: 0.8em; font-weight: 600;">
<%= status.name %>
</span>
<% end %>
<span class="status-key-badge status-call-out" style="padding: 2px 8px; border-radius: 4px; font-size: 0.8em; font-weight: 600;">
<i class="fa-solid fa-user-slash"></i> <%= l(:label_call_out) %>
</span>
</div>
<% end %>
@@ -189,14 +188,17 @@
standard_events = only_appointments ? [] : calendar.events_on(day).reject { |e| e.is_a?(CustomerAppointment) }
matching_appointments = all_appointments.select { |app| appointment_occurs_on.call(app, day) }
# Call-outs first (who's off that day), then everything else by time
day_events = (standard_events + matching_appointments).sort_by do |event|
if event.is_a?(CustomerAppointment) && event.date.present?
event.date.strftime('%H:%M:%S')
elsif event.respond_to?(:start_date) && event.start_date.present?
event.start_date.to_time.strftime('%H:%M:%S')
else
'00:00:00'
end
call_out = event.is_a?(CustomerAppointment) && event.call_out? ? 0 : 1
time = if event.is_a?(CustomerAppointment) && event.date.present?
event.date.strftime('%H:%M:%S')
elsif event.respond_to?(:start_date) && event.start_date.present?
event.start_date.to_time.strftime('%H:%M:%S')
else
'00:00:00'
end
[call_out, time]
end
day_total_hours = day_events.sum { |event| calculate_event_hours.call(event, day) }
@@ -258,6 +260,10 @@
new_customer_appointment_path(start_date: day),
class: 'icon-only icon-add',
title: l(:label_appointment_new, default: 'New Appointment') %>
<%= link_to new_customer_appointment_path(start_date: day, event_type: CustomerAppointment::CALL_OUT),
class: 'calendar-add-call-out', title: l(:label_new_call_out) do %>
<i class="fa-solid fa-user-slash"></i>
<% end %>
<% end %>
</span>
<% end %>
@@ -22,7 +22,11 @@
end
%>
<%= tag.div class: ['appointment-block', 'tooltip', appointment.status.to_s.parameterize] do %>
<%
status = appointment.status_record
css = ['appointment-block', 'tooltip', (appointment.call_out? ? 'call-out' : status.key.parameterize), ('no-hours' if status && !status.counts_hours?)]
%>
<%= tag.div class: css.compact, style: status&.style do %>
<div class="appointment-bar"></div>
@@ -51,9 +55,14 @@
<span class="appointment-days">(Day <%= day_number %> of <%= duration %>)</span>
<% end %>
<span class="appointment-hours">
<%= number_with_precision(appointment.estimated_hours.to_f, precision: 1) %>h
</span>
<%# Call-outs don't use hours %>
<% if appointment.call_out? %>
<span class="appointment-call-out-label" title="<%= l(:label_call_out) %>"><i class="fa-solid fa-user-slash"></i></span>
<% else %>
<span class="appointment-hours">
<%= number_with_precision(appointment.estimated_hours.to_f, precision: 1) %>h
</span>
<% end %>
</div>
<%# Row 3: Details %>
+20 -1
View File
@@ -4,6 +4,21 @@
<%= error_messages_for @appointment %>
<div class="box tabular">
<%= f.hidden_field :event_type %>
<% if @appointment.call_out? %>
<%# An employee off work: no customer, vehicle, estimate, hours or status %>
<p>
<label for="customer_appointment_user_id"><%= l(:label_employee) %> <span class="required">*</span></label>
<%= f.collection_select :user_id, User.active.sorted, :id, :name, { include_blank: true }, { label: false } %>
</p>
<p>
<%= f.text_field :subject, style: "width: 95%;", placeholder: l(:text_call_out_subject_placeholder) %>
</p>
<p><%= f.datetime_local_field :date %></p>
<p><%= f.number_field :duration, step: 1, size: 6, label: :label_duration %></p>
<% else %>
<p><%= f.text_field :subject, required: true, style: "width: 95%;" %></p>
<p>
@@ -33,7 +48,11 @@
<p><%= f.number_field :estimated_hours, step: 1, size: 6 %></p>
<p><%= f.number_field :duration, step: 1, size: 6, label: :label_duration %></p>
<p><%= f.select :status, CustomerAppointment::STATUSES, label: :field_status %></p>
<%# The statuses set up in the plugin settings, plus a removed one this appointment still has %>
<% status_options = CustomerAppointment.status_options %>
<% status_options += [[@appointment.status_record.name, @appointment.status]] if @appointment.status.present? && !@appointment.status_record.known? %>
<p><%= f.select :status, status_options, label: :field_status %></p>
<% end %>
<p>
<%= f.text_area :description,
@@ -60,7 +60,7 @@
<td>
<%= appointment.status %>
<%= appointment_status_badge(appointment) %>
</td>
@@ -19,11 +19,7 @@
<td><%= link_to appointment.user unless appointment.user.nil? %></td>
<td><%= format_date(appointment.date) %></td>
<td>
<% if appointment.status.present? %>
<span class="status-badge status-<%= appointment.status.to_s.parameterize %>">
<%= appointment.status.to_s.humanize %>
</span>
<% end %>
<%= appointment_status_badge(appointment) %>
</td>
</tr>
<% end %>
@@ -4,7 +4,7 @@
</h3>
<p class="subtitle">
<strong><%= appointment.status.to_s.humanize %></strong>
<strong><%= appointment.call_out? ? l(:label_call_out) : appointment.status_record.name %></strong>
</p>
<table class="attributes">
@@ -38,7 +38,7 @@
<% if appointment.user %>
<tr>
<th><%= l(:label_technician) %></th>
<th><%= l(appointment.call_out? ? :label_employee : :label_technician) %></th>
<td>
<%= avatar(appointment.user, size: 16) %>
<%= link_to appointment.user %>
@@ -74,10 +74,12 @@
</tr>
<% end %>
<% unless appointment.call_out? %>
<tr>
<th><%= l(:label_estimated_hours) %></th>
<td><%= number_with_precision(appointment.estimated_hours, precision: 1) %> hrs</td>
</tr>
<% end %>
<tr>
<th><%= l(:label_duration) %></th>
@@ -1,3 +1,3 @@
<h2><%=l(:label_edit_appointment)%></h2>
<h2><%= l(@appointment.call_out? ? :label_edit_call_out : :label_edit_appointment) %></h2>
<%= render "form" %>
<%= render "form" %>
@@ -35,7 +35,7 @@
<label for="status"><%= l(:field_status) %></label>
<%= select_tag :status,
options_for_select(CustomerAppointment::STATUSES.map { |s| [s.humanize, s] }, @status),
options_for_select(CustomerAppointment.status_options + [[l(:label_call_out_plural), CustomerAppointment::CALL_OUT]], @status),
include_blank: l(:label_all_statuses) %>
<label for="from"><%= l(:label_date_from) %></label>
@@ -80,11 +80,7 @@
<td><%= link_to_user(appointment.user) if appointment.user %></td>
<td><%= appointment.duration %> <%= l(:label_days) %></td>
<td>
<% if appointment.status.present? %>
<span class="status-badge status-<%= appointment.status.to_s.parameterize %>">
<%= appointment.status.to_s.humanize %>
</span>
<% end %>
<%= appointment_status_badge(appointment) %>
</td>
</tr>
<% end %>
+2 -2
View File
@@ -1,3 +1,3 @@
<h2><%=l(:label_new_appointment)%></h2>
<h2><%= l(@appointment.call_out? ? :label_new_call_out : :label_new_appointment) %></h2>
<%= render "form" %>
<%= render "form" %>
+20 -14
View File
@@ -8,7 +8,7 @@
<i class="fa-solid fa-clock-rotate-left"></i> <%= l(:label_appointment_history) %>
<% end %>
<% end %>
<% if User.current.allowed_to?(:add_customer_appointments, nil, global: true) %>
<% if !@appointment.call_out? && User.current.allowed_to?(:add_customer_appointments, nil, global: true) %>
<%= link_to new_issue_path(issue: { customer_id: @appointment.customer_id, vehicle_id: @appointment.vehicle_id, estimate_id: @appointment.estimate_id }), target: :_blank do %>
<i class="fa-solid fa-plus"></i> <%= l(:label_new_issue) %>
<% end %>
@@ -53,19 +53,15 @@
</span>
</h2>
<% if User.current.allowed_to?(:edit_customer_appointments, nil, global: true) %>
<%# Quick Status Action Buttons %>
<%
statuses = CustomerAppointment::STATUSES
current_status = @appointment.status.to_s.parameterize.underscore
%>
<%# Quick status buttons; call-outs have no status %>
<% if !@appointment.call_out? && User.current.allowed_to?(:edit_customer_appointments, nil, global: true) %>
<div class="appointment-quick-actions">
<span class="quick-action-label"><%= l(:label_change_status, default: 'Change Status:') %></span>
<% statuses.reject { |s| s == current_status }.each do |status_key| %>
<%= link_to status_key.humanize,
customer_appointment_path(@appointment, customer_appointment: { status: status_key }),
<% CustomerAppointment.statuses.reject { |s| s.key == @appointment.status_record.key }.each do |status| %>
<%= link_to status.name,
customer_appointment_path(@appointment, customer_appointment: { status: status.key }),
method: :patch,
class: "quick-status-btn status-#{status_key.parameterize}" %>
class: 'quick-status-btn', style: status.style %>
<% end %>
</div>
<% end %>
@@ -74,6 +70,13 @@
<div class="attributes">
<div class="splitcontent">
<div class="splitcontentleft">
<% if @appointment.call_out? %>
<%# A call-out is about the employee who's off, not a customer %>
<div class="attribute">
<div class="label"><%= l(:label_employee) %>:</div>
<div class="value"><%= link_to_user(@appointment.user) %></div>
</div>
<% else %>
<div class="attribute">
<div class="label"><%= l(:label_customer, default: 'Customer') %>:</div>
<div class="value"><%= link_to_if @appointment.customer, @appointment.customer&.name, @appointment.customer %></div>
@@ -92,15 +95,14 @@
<div class="value"><%= link_to @appointment.estimate, @appointment.estimate, target: "_blank" %></div>
<% end %>
</div>
<% end %>
</div>
<div class="splitcontentright">
<div class="attribute">
<div class="label"><%= l(:field_status) %>:</div>
<div class="value">
<span class="status-badge status-<%= @appointment.status.to_s.parameterize %>">
<%= @appointment.status.to_s.humanize %>
</span>
<%= appointment_status_badge(@appointment) %>
</div>
</div>
<div class="attribute">
@@ -109,18 +111,22 @@
<%= @appointment.date ? @appointment.date.strftime('%I:%M %p') : '-' %>
</div>
</div>
<% unless @appointment.call_out? %>
<div class="attribute">
<div class="label"><%= l(:field_user) %>:</div>
<div class="value"><%= link_to_user(@appointment.user) %></div>
</div>
<% end %>
<div class="attribute">
<div class="label"><%= l(:label_duration, default: 'Duration') %>:</div>
<div class="value"><%= @appointment.duration %> <%= l(:label_days) %></div>
</div>
<% unless @appointment.call_out? %>
<div class="attribute">
<div class="label"><%= l(:field_estimated_hours) %>:</div>
<div class="value"><%= @appointment.estimated_hours %> <%= l(:label_hours) %></div>
</div>
<% end %>
</div>
</div>
</div>
@@ -66,8 +66,7 @@
<tbody>
<% expanded_appointments.each do |item| %>
<% appt = item[:appointment] %>
<% status_slug = appt.status.to_s.parameterize %>
<tr class="<%= cycle('odd', 'even') %> appointment-row status-<%= status_slug %>">
<tr class="<%= cycle('odd', 'even') %> appointment-row">
<td class="subject">
<%= link_to appt.subject, customer_appointment_path(appt) %>
<% if item[:total_days] > 1 %>
@@ -84,11 +83,7 @@
<%= format_date(item[:date]) %>
</td>
<td class="status">
<% if appt.status.present? %>
<span class="badge status-badge status-<%= status_slug %>">
<%= appt.status.to_s.humanize %>
</span>
<% end %>
<%= appointment_status_badge(appt, css: 'badge status-badge') %>
</td>
</tr>
<% end %>
+128
View File
@@ -0,0 +1,128 @@
<%
statuses = RedmineQboCalendar::AppointmentStatus.all
# How many appointments use each status: removing one in use asks where to move them
in_use = CustomerAppointment.where(event_type: nil).group(:status).count
status_row = lambda do |index, status|
count = status ? in_use[status.key].to_i : 0
name = "settings[statuses][#{index}]"
content_tag(:tr, class: 'appointment-status-row', data: { in_use: count }) do
safe_join([
content_tag(:td, sprite_icon('reorder'), class: 'appointment-status-handle', title: l(:label_drag_to_reorder_status)),
content_tag(:td, safe_join([
hidden_field_tag("#{name}[key]", status&.key, id: nil),
text_field_tag("#{name}[name]", status&.name, id: nil, size: 24, class: 'appointment-status-name',
'aria-label': l(:field_name))
])),
content_tag(:td, color_field_tag("#{name}[color]", status&.color || '#1fc0ff', id: nil,
class: 'appointment-status-color', 'aria-label': l(:field_color))),
content_tag(:td, safe_join([
hidden_field_tag("#{name}[counts_hours]", '0', id: nil),
check_box_tag("#{name}[counts_hours]", '1', status.nil? || status.counts_hours?, id: nil,
'aria-label': l(:label_status_counts_hours))
]), class: 'appointment-status-hours'),
content_tag(:td, content_tag(:span, status&.name || l(:label_new_status), class: 'status-badge appointment-status-preview',
style: "--status-color: #{status&.color || '#1fc0ff'}")),
content_tag(:td, count.positive? ? l(:label_status_in_use, count: count) : '', class: 'appointment-status-count'),
content_tag(:td, link_to(sprite_icon('del', l(:button_delete)), '#', class: 'icon-only icon-del appointment-status-remove',
title: l(:button_delete)), class: 'buttons')
])
end
end
%>
<fieldset class="box">
<legend><%= l(:label_appointment_statuses) %></legend>
<p><em class="info"><%= l(:text_appointment_statuses) %></em></p>
<table class="list appointment-statuses">
<thead>
<tr>
<th></th>
<th><%= l(:field_name) %></th>
<th><%= l(:field_color) %></th>
<th><%= l(:label_status_counts_hours) %></th>
<th><%= l(:label_preview) %></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody id="appointment-statuses">
<% statuses.each_with_index do |status, index| %>
<%= status_row.call(index, status) %>
<% end %>
</tbody>
</table>
<template id="appointment-status-template">
<%= status_row.call('NEW_STATUS', nil) %>
</template>
<p>
<%= link_to sprite_icon('add', l(:button_add_status)), '#', id: 'appointment-status-add', class: 'icon icon-add' %>
</p>
<p><em class="info"><%= l(:text_call_outs_not_statuses) %></em></p>
</fieldset>
<%= javascript_tag do %>
$(function () {
var $body = $('#appointment-statuses');
$body.sortable({ handle: '.appointment-status-handle', axis: 'y' });
// The preview badge follows the name and colour
$body.on('input', '.appointment-status-name, .appointment-status-color', function () {
var $row = $(this).closest('tr');
var $preview = $row.find('.appointment-status-preview');
$preview.text($row.find('.appointment-status-name').val() || <%= raw l(:label_new_status).to_json %>);
$preview[0].style.setProperty('--status-color', $row.find('.appointment-status-color').val());
});
$('#appointment-status-add').on('click', function (e) {
e.preventDefault();
var html = $('#appointment-status-template').html().replace(/NEW_STATUS/g, 'new' + Date.now());
$body.append(html);
$body.find('tr:last .appointment-status-name').focus();
});
// Removing a status its appointments use: its row becomes a choice of status to move them to (done when
// the settings are saved), or keeping it (shown grey). Undo puts the row back.
$body.on('click', '.appointment-status-remove', function (e) {
e.preventDefault();
var $row = $(this).closest('tr');
var count = parseInt($row.data('in-use'), 10) || 0;
var key = $row.find('input[name$="[key]"]').val();
if (!count || !key) { $row.remove(); return; }
var $select = $('<select>').attr('name', 'settings[status_moves][' + key + ']');
$body.find('tr.appointment-status-row').not($row).each(function () {
var otherKey = $(this).find('input[name$="[key]"]').val();
if (otherKey) $select.append($('<option>').val(otherKey).text($(this).find('.appointment-status-name').val()));
});
$select.append($('<option>').val('').text(<%= raw l(:label_keep_removed_status).to_json %>));
var text = <%= raw l(:text_move_status_appointments).to_json %>
.replace('%{count}', count).replace('%{name}', $row.find('.appointment-status-name').val());
var $move = $('<tr class="appointment-status-move">').append(
$('<td>'),
$('<td colspan="5">').append($('<span>').text(text + ' '), $select),
$('<td class="buttons">').append(
$('<a href="#" class="appointment-status-undo">').text(<%= raw l(:button_undo_remove_status).to_json %>)));
$move.data('row', $row);
$row.after($move).detach();
});
$body.on('click', '.appointment-status-undo', function (e) {
e.preventDefault();
var $move = $(this).closest('tr');
$move.replaceWith($move.data('row'));
});
});
<% end %>
<style>
table.appointment-statuses { width: auto; min-width: 640px; }
table.appointment-statuses td { vertical-align: middle; }
table.appointment-statuses .appointment-status-handle { cursor: grab; color: #8d9096; width: 18px; }
table.appointment-statuses input[type=color] { width: 44px; height: 26px; padding: 0 2px; }
table.appointment-statuses .appointment-status-count { color: #6b6c72; font-size: .9em; }
table.appointment-statuses tr.appointment-status-move td { background: #fff8e1; text-align: left; }
</style>