diff --git a/app/controllers/customer_appointments_controller.rb b/app/controllers/customer_appointments_controller.rb index 4b83101..eb03965 100644 --- a/app/controllers/customer_appointments_controller.rb +++ b/app/controllers/customer_appointments_controller.rb @@ -95,6 +95,7 @@ class CustomerAppointmentsController < ApplicationController @appointment.project = @project @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 end def create @@ -129,6 +130,20 @@ class CustomerAppointmentsController < ApplicationController private + # Links from an estimate (redmine_qbo_billing) pass the estimate and vehicle; each is used only when it + # belongs to the appointment's customer + def prefill_estimate_and_vehicle + customer = @appointment.customer + return unless customer + + if params[:estimate_id].present? && customer.estimates.exists?(id: params[:estimate_id]) + @appointment.estimate_id = params[:estimate_id] + end + if params[:vehicle_id].present? && customer.respond_to?(:vehicles) && customer.vehicles.exists?(id: params[:vehicle_id]) + @appointment.vehicle_id = params[:vehicle_id] + end + end + def appointment_params params diff --git a/app/views/customer_appointments/_estimate_actions.html.erb b/app/views/customer_appointments/_estimate_actions.html.erb new file mode 100644 index 0000000..156b454 --- /dev/null +++ b/app/views/customer_appointments/_estimate_actions.html.erb @@ -0,0 +1,7 @@ +<%# Add appointment link on an estimate's page (redmine_qbo_billing) %> +<% if User.current.allowed_to?(:add_customer_appointments, nil, global: true) %> + <% vehicle = customer.vehicles.detect { |v| v.vin.to_s.casecmp?(vin.to_s.strip) } if vin.present? && customer.respond_to?(:vehicles) %> + <%= link_to sprite_icon('time-add', l(:label_add_appointment)), + new_customer_appointment_path(customer_id: customer.id, estimate_id: estimate_id, vehicle_id: vehicle&.id), + class: 'icon icon-time-add', target: :_blank %> +<% end %> diff --git a/lib/redmine_qbo_calendar/hooks/customer_actions_hook_listener.rb b/lib/redmine_qbo_calendar/hooks/customer_actions_hook_listener.rb index f3ab4a9..a04215d 100644 --- a/lib/redmine_qbo_calendar/hooks/customer_actions_hook_listener.rb +++ b/lib/redmine_qbo_calendar/hooks/customer_actions_hook_listener.rb @@ -19,6 +19,16 @@ module RedmineQboCalendar context[:hook_caller].render(partial: 'customer_appointments/customer_actions', locals: { customer: context[:customer] }) end + + # Adds "Add appointment" to an estimate's page in redmine_qbo_billing (:view_qbo_estimate_contextual), + # with the customer, the estimate and the vehicle matching its VIN filled in + def view_qbo_estimate_contextual(context = {}) + customer = Customer.find_by(id: context[:customer_id]) + return '' unless customer + + context[:hook_caller].render(partial: 'customer_appointments/estimate_actions', + locals: { customer: customer, estimate_id: context[:estimate_id], vin: context[:vin] }) + end end end