From 167e956457a2240a998dfb4de1dd53afd43abd17 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Sat, 5 Sep 2026 22:08:36 -0400 Subject: [PATCH] Add holiday management functionality with CRUD operations - Implement HolidaysController for managing holidays - Create Holiday model with validation for recurrence types - Add holiday form and views for creating, editing, and listing holidays - Integrate holiday checks into customer appointment scheduling - Update locale file for holiday-related labels and error messages - Add migration for holidays table - Update plugin version and add holidays to admin menu --- app/controllers/holidays_controller.rb | 81 +++++++++++++++++++++ app/models/customer_appointment.rb | 20 ++++++ app/models/holiday.rb | 40 +++++++++++ app/views/common/_calendar.html.erb | 36 ++++++++-- app/views/holidays/_form.html.erb | 97 ++++++++++++++++++++++++++ app/views/holidays/edit.html.erb | 7 ++ app/views/holidays/index.html.erb | 55 +++++++++++++++ app/views/holidays/new.html.erb | 7 ++ config/locales/en.yml | 34 +++++++-- config/routes.rb | 1 + db/migrate/002_create_holidays.rb | 33 +++++++++ init.rb | 5 +- 12 files changed, 407 insertions(+), 9 deletions(-) create mode 100644 app/controllers/holidays_controller.rb create mode 100644 app/models/holiday.rb create mode 100644 app/views/holidays/_form.html.erb create mode 100644 app/views/holidays/edit.html.erb create mode 100644 app/views/holidays/index.html.erb create mode 100644 app/views/holidays/new.html.erb create mode 100644 db/migrate/002_create_holidays.rb diff --git a/app/controllers/holidays_controller.rb b/app/controllers/holidays_controller.rb new file mode 100644 index 0000000..4a97271 --- /dev/null +++ b/app/controllers/holidays_controller.rb @@ -0,0 +1,81 @@ +#The MIT License (MIT) +# +#Copyright (c) 2026 rick barrette +# +#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +# +#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +# +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO CustomerAppointment SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +class HolidaysController < ApplicationController + layout 'admin' # Uses Redmine's admin layout to fit standard administration menus + + before_action :require_admin # Adjust permissions if regular project managers should access this + before_action :find_holiday, only: [:edit, :update, :destroy] + + def index + @holidays = Holiday.order(exact_date: :asc, month: :asc) + end + + def new + @holiday = Holiday.new + end + + def create + @holiday = Holiday.new(holiday_params) + + if @holiday.save + flash[:notice] = l(:notice_successful_create) + redirect_to holidays_path + else + respond_to do |format| + format.html { render action: 'new' } + end + end + end + + def edit + # @holiday is loaded via before_action + end + + def update + if @holiday.update(holiday_params) + flash[:notice] = l(:notice_successful_update) + redirect_to holidays_path + else + respond_to do |format| + format.html { render action: 'edit' } + end + end + end + + def destroy + @holiday.destroy + flash[:notice] = l(:notice_successful_delete) + redirect_to holidays_path + rescue ActiveRecord::DeleteRestrictionError + flash[:error] = l(:notice_unable_delete_holiday) + redirect_to holidays_path + end + + private + + def find_holiday + @holiday = Holiday.find(params[:id]) + rescue ActiveRecord::RecordNotFound + render_404 + end + + def holiday_params + params.require(:holiday).permit( + :name, + :exact_date, + :is_recurring, + :recurrence_type, + :month, + :day_of_week, + :week_of_month + ) + end +end \ No newline at end of file diff --git a/app/models/customer_appointment.rb b/app/models/customer_appointment.rb index 5a0aa39..34cf3ed 100644 --- a/app/models/customer_appointment.rb +++ b/app/models/customer_appointment.rb @@ -18,6 +18,9 @@ class CustomerAppointment < ActiveRecord::Base validates :subject, presence: true validates :date, presence: true + + # Prevent scheduling on non-working days or holidays + validate :validate_working_day normalizes :subject, with: ->(subject) { subject.strip.titleize } @@ -115,6 +118,23 @@ class CustomerAppointment < ActiveRecord::Base Rails.logger.info "[CustomerAppointment] #{msg}" end + # Validates that the appointment date does not land on a weekend or holiday + def validate_working_day + return if date.blank? + + target_date = date.to_date + non_working_days = Setting.non_working_week_days || [] + + if non_working_days.include?(target_date.cwday.to_s) + errors.add(:date, :invalid, message: I18n.t(:error_non_working_day)) + elsif defined?(Holiday) + matched_holiday = Holiday.all.find { |h| h.occurs_on?(target_date) } + if matched_holiday.present? + errors.add(:date, :invalid, message: I18n.t(:error_holiday, name: matched_holiday.name)) + end + end + end + # Helper to parse item name across Quickbooks::Model::Line and Hash structures def extract_item_name(line) if line.respond_to?(:sales_line_item_detail) && line.sales_line_item_detail.present? diff --git a/app/models/holiday.rb b/app/models/holiday.rb new file mode 100644 index 0000000..0892844 --- /dev/null +++ b/app/models/holiday.rb @@ -0,0 +1,40 @@ +#The MIT License (MIT) +# +#Copyright (c) 2026 rick barrette +# +#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +# +#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +# +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +class Holiday < ApplicationRecord + + TYPES = %w[ + fixed + dynamic + ] + + validates :recurrence_type, inclusion: { in: TYPES }, allow_blank: true + + def occurs_on?(target_date) + if !is_recurring + return exact_date == target_date + end + + if recurrence_type == 'fixed' + return target_date.month == exact_date.month && target_date.day == exact_date.day + end + + if recurrence_type == 'dynamic' + # Check if month and day of week match + return false unless target_date.month == month && target_date.wday == day_of_week + + # Calculate if it's the correct week of the month (1st, 2nd, 3rd, etc.) + week_in_month = ((target_date.day - 1) / 7) + 1 + return week_in_month == week_of_month + end + + false + end +end \ No newline at end of file diff --git a/app/views/common/_calendar.html.erb b/app/views/common/_calendar.html.erb index c40bcc2..3fa900c 100644 --- a/app/views/common/_calendar.html.erb +++ b/app/views/common/_calendar.html.erb @@ -12,6 +12,8 @@ # Get Redmine's configured non-working week days (e.g., ["6", "7"] for Sat/Sun) non_working_days = Setting.non_working_week_days || [] + all_holidays = Holiday.all + # Safely collect all unique CustomerAppointments across the entire calendar date range all_appointments = (calendar.startdt..calendar.enddt).flat_map { |date| calendar.events_on(date) @@ -26,15 +28,23 @@ target_date = current_day.to_date return false if target_date < start_date - # Skip rendering on Redmine non-working days + + # Skip rendering on Redmine standard non-working days return false if non_working_days.include?(target_date.cwday.to_s) + + # Skip rendering on custom non-working days (Holidays) + return false if all_holidays.any? { |h| h.occurs_on?(target_date) } duration = [appointment.duration.to_i, 1].max date_cursor = start_date working_days_count = 0 while date_cursor <= target_date - unless non_working_days.include?(date_cursor.cwday.to_s) + is_standard_weekend = non_working_days.include?(date_cursor.cwday.to_s) + is_custom_holiday = all_holidays.any? { |h| h.occurs_on?(date_cursor) } + + # Only increment duration count if it's a standard working day AND not a holiday + unless is_standard_weekend || is_custom_holiday working_days_count += 1 end @@ -135,6 +145,11 @@ <% is_today = (day == User.current.today) + + # Find the specific holiday for this day (if any) + current_holiday = all_holidays.find { |h| h.occurs_on?(day) } + is_custom_holiday = current_holiday.present? + is_standard_weekend = non_working_days.include?(day.cwday.to_s) # Filter out standard events if "only appointments" toggle is active standard_events = only_appointments ? [] : calendar.events_on(day).reject { |e| e.is_a?(CustomerAppointment) } @@ -169,7 +184,8 @@ end %> -
  • > + <%# Added 'week-end' class and background color styling for custom holidays to mimic weekends %> +
  • >

    @@ -182,7 +198,8 @@ - <% if day >= User.current.today && !non_working_days.include?(day.cwday.to_s) %> + <%# Only show ADD button if it's in the future, NOT a weekend, and NOT a custom holiday %> + <% if day >= User.current.today && !is_standard_weekend && !is_custom_holiday %> <%= link_to sprite_icon('add'), new_customer_appointment_path(start_date: day), class: 'icon-only icon-add', @@ -200,6 +217,17 @@

    + <%# Render Holiday Name Badge if it's a holiday %> + <% if is_custom_holiday %> +
    + + + + + <%= current_holiday.name %> +
    + <% end %> + <% day_events.each do |event| %> <% if event.is_a?(Issue) %> diff --git a/app/views/holidays/_form.html.erb b/app/views/holidays/_form.html.erb new file mode 100644 index 0000000..9de5e56 --- /dev/null +++ b/app/views/holidays/_form.html.erb @@ -0,0 +1,97 @@ +<%= error_messages_for 'holiday' %> + +
    +

    + + <%= f.text_field :name, required: true, size: 40 %> +

    + +

    + + <%= f.check_box :is_recurring, id: 'holiday_is_recurring' %> +

    + + + + +

    + + <%= f.date_field :exact_date, id: 'holiday_exact_date' %> +

    + + + +
    + + \ No newline at end of file diff --git a/app/views/holidays/edit.html.erb b/app/views/holidays/edit.html.erb new file mode 100644 index 0000000..d64db8f --- /dev/null +++ b/app/views/holidays/edit.html.erb @@ -0,0 +1,7 @@ +

    <%= l(:label_holiday_edit, default: 'Edit Holiday') %>

    + +<%= form_with model: @holiday, url: holiday_path(@holiday), method: :patch, local: true do |f| %> + <%= render partial: 'form', locals: { f: f } %> + <%= submit_tag l(:button_save) %> + <%= link_to l(:button_cancel), holidays_path, class: 'cancel' %> +<% end %> \ No newline at end of file diff --git a/app/views/holidays/index.html.erb b/app/views/holidays/index.html.erb new file mode 100644 index 0000000..bef0d85 --- /dev/null +++ b/app/views/holidays/index.html.erb @@ -0,0 +1,55 @@ +
    + <%= link_to 'New Holiday', new_holiday_path, class: 'icon icon-add' %> +
    + +

    <%= l(:label_holiday_plural, default: 'Holidays') %>

    + +<% if @holidays.any? %> +
    + + + + + + + + + + + <% @holidays.each do |holiday| %> + + + + + + + + + + <% end %> + +
    <%= l(:field_name, default: 'Name') %><%= l(:field_type, default: 'Type') %><%= l(:field_date, default: 'Date / Rule') %>
    <%= holiday.name %> + <% if !holiday.is_recurring %> + One-Time + <% else %> + Recurring (<%= holiday.recurrence_type.capitalize %>) + <% end %> + + <% if !holiday.is_recurring || holiday.recurrence_type == 'fixed' %> + <%= holiday.exact_date&.strftime('%B %d, %Y') %> + <% elsif holiday.recurrence_type == 'dynamic' %> + <%= ['1st', '2nd', '3rd', '4th', 'Last'][holiday.week_of_month - 1] %> + <%= Date::DAYNAMES[holiday.day_of_week] %> of + <%= Date::MONTHNAMES[holiday.month] %> + <% end %> + + <%= link_to l(:button_edit), edit_holiday_path(holiday), class: 'icon icon-edit' %> + <%= link_to l(:button_delete), holiday_path(holiday), + method: :delete, + data: { confirm: l(:text_are_you_sure) }, + class: 'icon icon-del' %> +
    +
    +<% else %> +

    <%= l(:label_no_data) %>

    +<% end %> \ No newline at end of file diff --git a/app/views/holidays/new.html.erb b/app/views/holidays/new.html.erb new file mode 100644 index 0000000..87cfa3b --- /dev/null +++ b/app/views/holidays/new.html.erb @@ -0,0 +1,7 @@ +

    <%= l(:label_holiday_new, default: 'New Holiday') %>

    + +<%= form_with model: @holiday, url: holidays_path, local: true do |f| %> + <%= render partial: 'form', locals: { f: f } %> + <%= submit_tag l(:button_create) %> + <%= link_to l(:button_cancel), holidays_path, class: 'cancel' %> +<% end %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 431a0fd..ee48c96 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -14,9 +14,23 @@ en: are_you_sure: "Are you sure?" button_appointments: Appointments button_calendar: Calendar + day_name_friday: "Friday" + day_name_monday: "Monday" + day_name_saturday: "Saturday" + day_name_sunday: "Sunday" + day_name_thursday: "Thursday" + day_name_tuesday: "Tuesday" + day_name_wednesday: "Wednesday" + error_holiday: "Cannot be scheduled on holiday: %{name}" + error_non_working_day: "Cannot be scheduled on a non-working day" field_customer_name: Customer + field_date: Date + field_dynamic_rule: Rule + field_is_recurring: "Is this a recurring holiday?" field_license_plate: License Plate field_phone: Phone + field_recurrence_type: Recurrence Type + field_type: Type field_vehicle: Vehicle label_add_appointment: Add Appointment label_appointment: Appointment @@ -29,16 +43,28 @@ en: label_end_date: End Date label_estimated_hours: Estimated Time label_event: Event - label_open_appointment: Open Appointment - label_only_customer_appointments: Show Customer Appointments Only + label_holiday_edit: Edit Holiday + label_holiday_new: New Holiday + label_holiday_plural: Holidays label_new_appointment: New Appointment + label_of: "of" + label_only_customer_appointments: Show Customer Appointments Only + label_open_appointment: Open Appointment + label_recurrence_dynamic: "Dynamic Date (e.g., 1st Monday of Sept)" + label_recurrence_fixed: "Fixed Date (e.g., Dec 25)" label_start_date: Start Date label_status: Status label_technician: Technician - label_today: Today label_time: Time label_title: Title + label_today: Today label_upcoming_appointments: "My Upcoming Appointments" + label_week_1st: "1st" + label_week_2nd: "2nd" + label_week_3rd: "3rd" + label_week_4th: "4th" + label_week_last: "Last" + notice_unable_delete_holiday: "Unable to delete holiday" permission_manage_customer_appointments: Manage Customer Appointments permission_view_customer_appointments: View Customer Appointments - title_customer_appointments: Customer Appoitnments \ No newline at end of file + title_customer_appointments: Customer Appointments \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index c56aa3d..d715eac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,6 +10,7 @@ RedmineApp::Application.routes.draw do + resources :holidays, except: [:show] # Exclude :show since we don't need a separate page for individual holidays resources :customer_appointments do collection do get :customer_options diff --git a/db/migrate/002_create_holidays.rb b/db/migrate/002_create_holidays.rb new file mode 100644 index 0000000..21c3ea7 --- /dev/null +++ b/db/migrate/002_create_holidays.rb @@ -0,0 +1,33 @@ +#The MIT License (MIT) +# +#Copyright (c) 2026 rick barrette +# +#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +# +#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +# +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +class CreateHolidays < ActiveRecord::Migration[7.0] + def change + create_table :holidays do |t| + t.string :name, null: false + + # Use date instead of datetime since holidays are full days + t.date :exact_date + + # For recurring logic + t.boolean :is_recurring, default: false + + # "fixed" (same date every year) or "dynamic" (e.g., 1st Monday) + t.string :recurrence_type + + # Fields for dynamic recurring holidays (Labor Day) + t.integer :month # e.g., 9 (September) + t.integer :day_of_week # e.g., 1 (Monday) + t.integer :week_of_month # e.g., 1 (First week) + + t.timestamps + end + end +end \ No newline at end of file diff --git a/init.rb b/init.rb index 33f8f7e..07a4696 100644 --- a/init.rb +++ b/init.rb @@ -14,12 +14,15 @@ Redmine::Plugin.register :redmine_qbo_calendar do name 'Redmine QBO Calendar plugin' author 'Rick Barrette' description 'A plugin for Redmine to extend the capabilitys of the Redmine QuickBooks Online plugin to create customer appointments for the Redmine Calendar' - version '2026.8.1' + version '2026.9.0' url 'https://github.com/rickbarrette/redmine_qbo_calendar' author_url 'https://barrettefabrication.com' requires_redmine version_or_higher: '7.0.0' settings partial: 'calendar/settings' + # Add Holidays index to the Redmine Administration Menu + menu :admin_menu, :holidays, { controller: 'holidays', action: 'index' }, caption: :label_holiday_plural, html: { class: 'icon icon-list' } + # Ensure redmine_qbo is installed begin requires_redmine_plugin :redmine_qbo, version_or_higher: '2026.8.2'