From e190f1e23c8fc7e00c476a6c362402e8646afadc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Wed, 2 Oct 2024 09:38:50 +0800 Subject: [PATCH] Fix bug. --- app/controllers/property_hires_controller.rb | 22 ++++++++++-------- app/helpers/admin/property_hires_helper.rb | 16 ++++--------- app/models/property.rb | 24 +++++++++++--------- app/views/property_hires/hire.html.erb | 8 +++---- 4 files changed, 34 insertions(+), 36 deletions(-) diff --git a/app/controllers/property_hires_controller.rb b/app/controllers/property_hires_controller.rb index aa9ddc8..fbef542 100644 --- a/app/controllers/property_hires_controller.rb +++ b/app/controllers/property_hires_controller.rb @@ -34,7 +34,7 @@ class PropertyHiresController < ApplicationController def index_data(properties, url) ma = ModuleApp.find_by_key("property_hire") - is_user_manager = (OrbitHelper.current_user.is_admin? || OrbitHelper.current_user.is_manager?(ma) || OrbitHelper.current_user.is_sub_manager?(ma)) + is_user_manager = OrbitHelper.current_user && (OrbitHelper.current_user.is_admin? || OrbitHelper.current_user.is_manager?(ma) || OrbitHelper.current_user.is_sub_manager?(ma)) data = properties.collect do |property| actions = [] url_to_show = "#{url}/#{property.to_param}?method=hire" @@ -334,7 +334,7 @@ class PropertyHiresController < ApplicationController allow_no_logins_user = PropertyHireSetting.first.allow_no_logins_user all_day_settings = property.all_day_settings.map{|d,settings| [d,settings.map{|s| [s.id.to_s,s.title]}]}.to_h ma = ModuleApp.find_by_key("property_hire") - is_user_manager = (OrbitHelper.current_user.is_admin? || OrbitHelper.current_user.is_manager?(ma) || OrbitHelper.current_user.is_sub_manager?(ma)) + is_user_manager = OrbitHelper.current_user && (OrbitHelper.current_user.is_admin? || OrbitHelper.current_user.is_manager?(ma) || OrbitHelper.current_user.is_sub_manager?(ma)) { "hire" => hire, "property" => property, @@ -370,16 +370,16 @@ class PropertyHiresController < ApplicationController @need_check_events = allevents.map{|e| [e[:date],e[:s_id]]} @special_unavailable_dates = property.special_unavailable_dates.map{|dt| Date.parse(dt)} ma = ModuleApp.find_by_key("property_hire") - is_user_manager = (OrbitHelper.current_user.is_admin? || OrbitHelper.current_user.is_manager?(ma) || OrbitHelper.current_user.is_sub_manager?(ma)) + is_user_manager = OrbitHelper.current_user && (OrbitHelper.current_user.is_admin? || OrbitHelper.current_user.is_manager?(ma) || OrbitHelper.current_user.is_sub_manager?(ma)) if property.set_availability && params[:display_hire_event] == "true" check_setting = property.set_unavailibility && (property.property_day_settings.where(:enable=>false).count != 0) @check_start_time = property.start_time.blank? ? "00:00" : property.start_time @check_end_time = property.end_time.blank? ? "24:00" : property.end_time - @check_start_date = property.start_date.to_date rescue nil - @check_end_date = property.end_date.to_date rescue nil + @check_start_date = property.start_date ? property.start_date.to_date : nil + @check_end_date = property.end_date ? property.end_date.to_date : nil if check_setting - if (@check_start_date > edt rescue false) || (@check_end_date < sdt rescue false) + if (@check_start_date && @check_start_date > edt) || (@check_end_date && @check_end_date < sdt) check_setting = false end end @@ -429,7 +429,9 @@ class PropertyHiresController < ApplicationController available = false end - if DateTime.now >= @property.start_date && DateTime.now <= @property.end_date + need_check_unavailable = (@check_start_date.nil? || date >= @check_start_date) && (@check_end_date.nil? || date <= @check_end_date) + + if need_check_unavailable if @property.hours_restriction > 0 && @is_user_manager === false check = false sd = nil @@ -461,13 +463,13 @@ class PropertyHiresController < ApplicationController end if available - if DateTime.now >= @property.start_date && DateTime.now <= @property.end_date + if need_check_unavailable available = date > (DateTime.now + (@property.need_hire_before).send(@property.need_hire_before_unit)) end end if @check_setting && allow_times.select{|a| !a[5]}.count != 0 - available = (date > @check_end_date rescue false) || (date < @check_start_date rescue false) + available = !need_check_unavailable if available available = @property.weekdays.include?(date.wday.to_s) ? false : true end @@ -509,7 +511,7 @@ class PropertyHiresController < ApplicationController @weeknumber = 0 @monthnumber = 0 ma = ModuleApp.find_by_key("property_hire") - @is_user_manager = (OrbitHelper.current_user.is_admin? || OrbitHelper.current_user.is_manager?(ma) || OrbitHelper.current_user.is_sub_manager?(ma)) + @is_user_manager = OrbitHelper.current_user && (OrbitHelper.current_user.is_admin? || OrbitHelper.current_user.is_manager?(ma) || OrbitHelper.current_user.is_sub_manager?(ma)) def generate_events(start_wday,end_wday,type=0,start_validate=false) if type == 0 (start_wday..end_wday).each_with_index do |wday,i| diff --git a/app/helpers/admin/property_hires_helper.rb b/app/helpers/admin/property_hires_helper.rb index 77a5317..06a4a48 100644 --- a/app/helpers/admin/property_hires_helper.rb +++ b/app/helpers/admin/property_hires_helper.rb @@ -4,19 +4,13 @@ module Admin::PropertyHiresHelper include OrbitBackendHelper def check_for_availability(stime, etime, pid, interval=nil, recurring_end_date=nil, time_setting_id=nil) property = Property.find(pid) - return {"success" => false, "msg" => I18n.t("property_hire.values_are_not_ok",:default=>"Values are not ok.")} if property.nil? || stime.blank? || etime.blank? + values_not_ok = {"success" => false, "msg" => I18n.t("property_hire.values_are_not_ok",:default=>"Values are not ok.")} + return values_not_ok if property.nil? || stime.blank? || etime.blank? timezone = (params[:timezone] rescue nil) timezone = timezone ? timezone : Time.zone.to_s - if !stime.blank? - stime = DateTime.parse(stime + timezone) rescue nil - else - stime = nil - end - if !etime.blank? - etime = DateTime.parse(etime + timezone) rescue nil - else - etime = nil - end + stime = DateTime.parse(stime + timezone) rescue nil + etime = DateTime.parse(etime + timezone) rescue nil + return values_not_ok if stime.nil? || etime.nil? if !recurring_end_date.blank? recurring_end_date = DateTime.parse(recurring_end_date + timezone) rescue nil begin diff --git a/app/models/property.rb b/app/models/property.rb index 2ec2c0c..1b44fa8 100644 --- a/app/models/property.rb +++ b/app/models/property.rb @@ -46,8 +46,8 @@ class Property field :weekdays, type: Array, default: [] field :special_unavailable_dates, type: Array, default: [] field :special_unavailable_dates_title, type: Array, default: [] - field :start_date, type: DateTime - field :end_date, type: DateTime + field :start_date, type: DateTime # unavailable start date + field :end_date, type: DateTime # unavailable end date field :hours_restriction, type: Integer, default: 0 field :hours_restriction_duration, type: String field :description, :localize => true @@ -148,9 +148,10 @@ class Property end def can_reserve - start_time = self.p_hire_start_time || Time.now - end_time = self.p_hire_end_time || Time.now - return Time.now >= start_time && end_time >= Time.now + now = Time.now + start_time = self.p_hire_start_time || now + end_time = self.p_hire_end_time || now + return now >= start_time && end_time >= now end def p_hire_fields_enabled @@ -339,14 +340,17 @@ class Property end end - if DateTime.now > self.start_date && DateTime.now < self.end_date + startt = self.start_date.nil? ? stime : self.start_date + endt = self.end_date.nil? ? etime : self.end_date + + if (stime >= startt) && (etime <= endt) if self.hours_restriction > 0 && !user.nil? sd = nil edd = nil case self.hours_restriction_duration when "week" sd = stime - stime.wday - edd = stime + (6 - stime.wday) + edd = sd + 6 when "month" sd = Date.new(stime.year, stime.month, 1) edd = sd.next_month.prev_day @@ -356,10 +360,8 @@ class Property end end end - startt = self.start_date.nil? ? stime : self.start_date - endt = self.end_date.nil? ? etime : self.end_date - available = 1 if (startt > stime && endt > etime) - available = 1 if (endt < stime) + available = 1 if startt > etime + available = 1 if endt < stime weekdays = self.weekdays.collect{|w| w.to_i} if !startt.nil? if available == 0 diff --git a/app/views/property_hires/hire.html.erb b/app/views/property_hires/hire.html.erb index 660c503..52e3d85 100644 --- a/app/views/property_hires/hire.html.erb +++ b/app/views/property_hires/hire.html.erb @@ -265,7 +265,7 @@ <% session.delete("hire-save-msg") %> <% end %> -<% if property.calendar_type == 0 %> +<% if calendar_type == 0 %>