diff --git a/app/controllers/customer_appointments_controller.rb b/app/controllers/customer_appointments_controller.rb
index cb9c064..1f32056 100644
--- a/app/controllers/customer_appointments_controller.rb
+++ b/app/controllers/customer_appointments_controller.rb
@@ -48,6 +48,29 @@ class CustomerAppointmentsController < ApplicationController
end
end
+ # Lists past appointments, optionally filtered by customer, status and date range.
+ def history
+ @customer = Customer.find_by(id: params[:customer_id]) if params[:customer_id].present?
+ @status = params[:status].presence
+ @from = parse_date(params[:from])
+ @to = parse_date(params[:to])
+
+ scope = CustomerAppointment
+ .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
+ 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
+
+ @appointment_count = scope.count
+ @appointment_pages = Paginator.new(@appointment_count, per_page_option, params[:page])
+ @appointments = scope
+ .order(date: :desc)
+ .offset(@appointment_pages.offset)
+ .limit(@appointment_pages.per_page)
+ end
+
def show
@appointment = CustomerAppointment.find(params[:id])
end
@@ -111,6 +134,12 @@ class CustomerAppointmentsController < ApplicationController
end
+ def parse_date(value)
+ Date.parse(value) if value.present?
+ rescue ArgumentError
+ nil
+ end
+
def find_project
@project = Project.find(params[:project_id]) if params[:project_id].present?
end
diff --git a/app/views/customer_appointments/_customer_actions.html.erb b/app/views/customer_appointments/_customer_actions.html.erb
index f5c2756..6f36d0a 100644
--- a/app/views/customer_appointments/_customer_actions.html.erb
+++ b/app/views/customer_appointments/_customer_actions.html.erb
@@ -1,6 +1,11 @@
+<% customer = context[:customer] %>
<% if User.current.allowed_to?(:add_customer_appointments, nil, global: true) %>
- <% customer = context[:customer] %>
<%= link_to l(:label_add_appointment), new_customer_appointment_path(customer_id: customer.id), target: :_blank, id: :appointment_link %>
+<% end %>
+<% if User.current.allowed_to?(:view_customer_appointments, nil, global: true) %>
+
+ <%= link_to l(:label_appointment_history), history_customer_appointments_path(customer_id: customer.id), id: :appointment_history_link %>
+
<% end %>
\ No newline at end of file
diff --git a/app/views/customer_appointments/history.html.erb b/app/views/customer_appointments/history.html.erb
new file mode 100644
index 0000000..6b67e71
--- /dev/null
+++ b/app/views/customer_appointments/history.html.erb
@@ -0,0 +1,106 @@
+
+ <%= link_to issues_calendar_path do %>
+ <%= l(:button_calendar) %>
+ <% end %>
+ <%= link_to customer_appointments_path do %>
+ <%= l(:label_appointments) %>
+ <% end %>
+ <% if @customer %>
+ <%= link_to @customer do %>
+ <%= l(:label_customer) %>
+ <% end %>
+ <% end %>
+
+
+
+ <% if @customer %>
+ <%= l(:label_appointment_history_for, name: @customer.name) %>
+ <% else %>
+ <%= l(:label_appointment_history) %>
+ <% end %>
+
+
+<%= form_tag history_customer_appointments_path, method: :get, id: 'appointment-history-filters' do %>
+
+ <%= l(:label_filter_plural) %>
+
+ <%= l(:label_customer) %>
+ <%= text_field_tag :customer_name, @customer&.name,
+ id: 'history_customer_name',
+ class: 'customer-name',
+ autocomplete: 'off',
+ size: 30,
+ data: { autocomplete_url: '/customers/autocomplete' } %>
+ <%= hidden_field_tag :customer_id, @customer&.id, id: 'customer_id' %>
+
+ <%= l(:field_status) %>
+ <%= select_tag :status,
+ options_for_select(CustomerAppointment::STATUSES.map { |s| [s.humanize, s] }, @status),
+ include_blank: l(:label_all_statuses) %>
+
+ <%= l(:label_date_from) %>
+ <%= date_field_tag :from, @from %>
+
+ <%= l(:label_date_to) %>
+ <%= date_field_tag :to, @to %>
+
+
+ <%= submit_tag l(:button_apply), name: nil, class: 'button-small' %>
+ <%= link_to l(:button_clear), history_customer_appointments_path(customer_id: @customer&.id), class: 'icon icon-reload' %>
+
+
+<% end %>
+
+<% if @appointments.empty? %>
+ <%= l(:label_no_past_appointments) %>
+<% else %>
+
+
+
+ <%= l(:label_date) %>
+ <% unless @customer %>
+ <%= l(:label_customer) %>
+ <% end %>
+ <%= l(:label_vehicle) %>
+ <%= l(:label_appointment) %>
+ <%= l(:label_technician) %>
+ <%= l(:label_duration) %>
+ <%= l(:label_status) %>
+
+
+
+ <% @appointments.each do |appointment| %>
+
+ <%= format_date(appointment.date) %>
+ <% unless @customer %>
+ <%= link_to appointment.customer, history_customer_appointments_path(customer_id: appointment.customer_id) if appointment.customer %>
+ <% end %>
+ <%= link_to appointment.vehicle if appointment.vehicle %>
+ <%= link_to appointment.subject, customer_appointment_path(appointment) %>
+ <%= link_to_user(appointment.user) if appointment.user %>
+ <%= appointment.duration %> <%= l(:label_days) %>
+
+ <% if appointment.status.present? %>
+
+ <%= appointment.status.to_s.humanize %>
+
+ <% end %>
+
+
+ <% end %>
+
+
+
+
+<% end %>
+
+<%= javascript_tag do %>
+ // Clear the hidden customer id if the name field is emptied, so "all customers" works.
+ document.addEventListener('DOMContentLoaded', function () {
+ var name = document.getElementById('history_customer_name');
+ var id = document.getElementById('customer_id');
+ if (name && id) {
+ name.addEventListener('input', function () { if (!name.value.trim()) id.value = ''; });
+ }
+ });
+<% end %>
diff --git a/app/views/customer_appointments/index.html.erb b/app/views/customer_appointments/index.html.erb
index a6eebde..1c1c18e 100644
--- a/app/views/customer_appointments/index.html.erb
+++ b/app/views/customer_appointments/index.html.erb
@@ -5,6 +5,10 @@
<%= l(:button_calendar) %>
<% end %>
+ <%= link_to history_customer_appointments_path do %>
+ <%= l(:label_appointment_history) %>
+ <% end %>
+
<% if User.current.allowed_to?(:add_customer_appointments, nil, global: true) %>
<%= link_to new_customer_appointment_path do %>
<%= l(:button_add) %>
diff --git a/app/views/customer_appointments/show.html.erb b/app/views/customer_appointments/show.html.erb
index 258a388..e84d1c7 100644
--- a/app/views/customer_appointments/show.html.erb
+++ b/app/views/customer_appointments/show.html.erb
@@ -3,6 +3,11 @@
<%= l(:button_calendar) %>
<% end %>
+ <% if @appointment.customer %>
+ <%= link_to history_customer_appointments_path(customer_id: @appointment.customer_id) do %>
+ <%= l(:label_appointment_history) %>
+ <% end %>
+ <% end %>
<% if 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 %>
<%= l(:label_new_issue) %>
diff --git a/assets/stylesheets/customer_appointments.css b/assets/stylesheets/customer_appointments.css
index 0850501..ee280e6 100644
--- a/assets/stylesheets/customer_appointments.css
+++ b/assets/stylesheets/customer_appointments.css
@@ -596,4 +596,16 @@ table.cal td.today {
left: 50% !important;
right: auto !important;
}
-}
\ No newline at end of file
+}
+
+/* Appointment history page */
+#appointment-history-filters fieldset p label {
+ margin: 0 4px 0 12px;
+ font-weight: bold;
+}
+#appointment-history-filters fieldset p label:first-child {
+ margin-left: 0;
+}
+table.appointment-history td.subject {
+ text-align: left;
+}
diff --git a/config/locales/en.yml b/config/locales/en.yml
index d7153ed..8bb4ba8 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -36,6 +36,12 @@ en:
label_appointment: Appointment
label_appointment_new: "New Appointment"
label_appointments: Appointments
+ label_appointment_history: Appointment History
+ label_appointment_history_for: "Appointment History: %{name}"
+ label_all_statuses: "All statuses"
+ label_date_from: From
+ label_date_to: To
+ label_no_past_appointments: "No past appointments found"
label_customer_appointment: Customer Appointment
label_customer_calendar: Customer Calendar
label_days: Days
diff --git a/config/routes.rb b/config/routes.rb
index d715eac..5100282 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -15,6 +15,7 @@ RedmineApp::Application.routes.draw do
collection do
get :customer_options
get :create_issues
+ get :history
end
end
diff --git a/init.rb b/init.rb
index 816bae3..5a37760 100644
--- a/init.rb
+++ b/init.rb
@@ -30,7 +30,7 @@ Redmine::Plugin.register :redmine_qbo_calendar do
end
# Global Permissions
- permission :view_customer_appointments, { customer_appointments: [:index, :show] }, global: true
+ permission :view_customer_appointments, { customer_appointments: [:index, :show, :history] }, global: true
permission :add_customer_appointments, { customer_appointments: [:new, :create] }, global: true
permission :edit_customer_appointments, { customer_appointments: [:edit, :update] }, global: true
permission :delete_customer_appointments, { customer_appointments: [:destroy] }, global: true