diff --git a/app/controllers/admin/property_hires_controller.rb b/app/controllers/admin/property_hires_controller.rb index b10232b..da5e453 100644 --- a/app/controllers/admin/property_hires_controller.rb +++ b/app/controllers/admin/property_hires_controller.rb @@ -114,6 +114,9 @@ class Admin::PropertyHiresController < OrbitAdminController flash.now[:notice] = "Updated Fields" property.p_hire_fields.each{|t| t.destroy if t["to_delete"] == true} else + if @property_params["special_unavailable_dates"].nil? + @property_params["special_unavailable_dates"] = [] + end property.update_attributes(@property_params) email = property.get_owner_emails Admin::PropertyHiresHelper::HireMethod.send_mail('edit',email,property.id,nil,nil,current_user.id) diff --git a/app/controllers/property_hires_controller.rb b/app/controllers/property_hires_controller.rb index 33cdc82..aa9ddc8 100644 --- a/app/controllers/property_hires_controller.rb +++ b/app/controllers/property_hires_controller.rb @@ -2,7 +2,7 @@ class PropertyHiresController < ApplicationController include Admin::PropertyHiresHelper FrontendMethods = ["hire", "view_calendar", "hire_success"] def index - properties = Property.filter_by_categories.sort_order + properties = Property.can_display.filter_by_categories.sort_order url = "/#{I18n.locale}#{OrbitHelper.page.get_url}" data = index_data(properties, url) headers = [ @@ -33,11 +33,13 @@ class PropertyHiresController < ApplicationController end 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)) data = properties.collect do |property| actions = [] url_to_show = "#{url}/#{property.to_param}?method=hire" hire_url = nil - if property.can_be_hired_frontend && property.can_reserve + if (property.can_be_hired_frontend && property.can_reserve) || is_user_manager hire_url = url_to_show actions << { "text" => t("property_hire.hire"), @@ -45,7 +47,7 @@ class PropertyHiresController < ApplicationController "link" => hire_url } end - if !property.disable_view_calendar_page && property.can_be_show_frontend && property.can_reserve + if (!property.disable_view_calendar_page && property.can_be_show_frontend && property.can_reserve) || is_user_manager actions << { "text" => t("property_hire.view_calendar"), "btn-class" => "btn-info", @@ -331,10 +333,13 @@ class PropertyHiresController < ApplicationController end 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)) { "hire" => hire, "property" => property, "page" => page.url, + "is_user_manager" => is_user_manager, "current_user" => OrbitHelper.current_user, "allow_no_logins_user" => allow_no_logins_user, "carousel_display_style" => "width: #{property.carousel_image_width};", @@ -352,7 +357,7 @@ class PropertyHiresController < ApplicationController def get_bookings events =[] allevents = [] - property = Property.find(params[:property_id]) rescue nil + property = @property = Property.find(params[:property_id]) rescue nil unless property.nil? if params[:start].present? && params[:end].present? sdt = Time.at(params[:start].to_i) @@ -362,9 +367,10 @@ class PropertyHiresController < ApplicationController events = events.map{|e| e.as_json} allevents = events.inject(re, :<<) allevents = allevents.sort_by{|e| e[:start]} - Rails.logger.info allevents @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)) if property.set_availability && params[:display_hire_event] == "true" check_setting = property.set_unavailibility && (property.property_day_settings.where(:enable=>false).count != 0) @@ -380,14 +386,19 @@ class PropertyHiresController < ApplicationController all_day_settings = property.all_day_settings.map{|d,settings| [d,settings.map{|s| [s.start_time,s.end_time,s.id.to_s,s.title,s.reservation_limit,s.enable]}]}.to_h if all_day_settings.count != 0 time_now = Time.zone.now - get_start_time = [sdt,time_now].max - get_end_time = edt + if is_user_manager + get_start_time = property.p_open_start_time + get_end_time = property.p_open_end_time + else + get_start_time = [sdt,time_now].max + get_end_time = edt + end if property.set_unavailibility if property.can_hire_before_months != 0 get_end_time = [time_now + (property.can_hire_before_months).send("month"),edt].min end end - if property.can_reserve + if property.can_reserve || is_user_manager allevents += generate_all_reserve_buttons(get_start_time,get_end_time,all_day_settings,check_setting) end @@ -417,15 +428,57 @@ class PropertyHiresController < ApplicationController if @special_unavailable_dates.include?(date) available = false end + + if DateTime.now >= @property.start_date && DateTime.now <= @property.end_date + if @property.hours_restriction > 0 && @is_user_manager === false + check = false + sd = nil + ed = nil + case @property.hours_restriction_duration + when "week" + weeknumber = date.strftime("%U").to_i + if @weeknumber != weeknumber + sd = date - date.wday + ed = date + (6 - date.wday) + @weeknumber = weeknumber + check = true + end + when "month" + if @monthnumber != date.month + sd = Date.new(date.year, date.month, 1) + ed = sd.next_month.prev_day + @monthnumber = date.month + check = true + end + end + if check === true + @user_total_hired_hours = @property.get_user_total_user_hired_hours(OrbitHelper.current_user.id.to_s, sd, ed) + end + if @user_total_hired_hours >= @property.hours_restriction + available = false + end + end + end + + if available + if DateTime.now >= @property.start_date && DateTime.now <= @property.end_date + 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) + if available + available = @property.weekdays.include?(date.wday.to_s) ? false : true + end + unless available allow_times = allow_times.select do |allow_time| if allow_time[5] true elsif (allow_time[0] > @check_end_time || allow_time[1] < @check_start_time) true - else + elsif false end end @@ -453,6 +506,10 @@ class PropertyHiresController < ApplicationController @all_day_settings = all_day_settings @startt = startt @endt = endt + @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)) 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 23df175..77a5317 100644 --- a/app/helpers/admin/property_hires_helper.rb +++ b/app/helpers/admin/property_hires_helper.rb @@ -41,7 +41,7 @@ module Admin::PropertyHiresHelper return {"success" => false, "msg" => I18n.t("property_hire.starting_time_cannot_be_greater_than_ending_time")} end end - available_flag = property.is_available_for_hire?(stime, etime, interval, recurring_end_date, time_setting_id) + available_flag = property.is_available_for_hire?(stime, etime, interval, recurring_end_date, time_setting_id, OrbitHelper.current_user.member_profile) if available_flag == 1 if property.not_yet_hired?(stime, etime, interval, recurring_end_date,params[:phire_id], time_setting_id) data = {"success" => true} @@ -172,7 +172,7 @@ module Admin::PropertyHiresHelper rescue => e puts ["email can't deliver",e] end - end + end end end -end \ No newline at end of file +end diff --git a/app/models/property.rb b/app/models/property.rb index 9fd410e..2ec2c0c 100644 --- a/app/models/property.rb +++ b/app/models/property.rb @@ -31,6 +31,8 @@ class Property field :p_hire_end_time, type: DateTime field :p_open_start_time, type: DateTime field :p_open_end_time, type: DateTime + field :p_display_start_time, type: DateTime + field :p_display_end_time, type: DateTime field :recurring_enable, type: Boolean, :default => false mount_uploader :image, ImageUploader @@ -46,6 +48,8 @@ class Property field :special_unavailable_dates_title, type: Array, default: [] field :start_date, type: DateTime field :end_date, type: DateTime + field :hours_restriction, type: Integer, default: 0 + field :hours_restriction_duration, type: String field :description, :localize => true field :unavailibility_note, :localize => true field :hiring_person_email, type: Hash, default: {"enable"=>"1","required"=>"true"} @@ -86,7 +90,7 @@ class Property has_many :property_links, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :property_links, :allow_destroy => true scope :sort_order, ->{order_by([:created_at,:desc],[:order_position,:asc])} - scope :can_display, ->{any_of({:p_hire_start_time.lt=>Time.now, :p_hire_end_time.gt=>Time.now},{:p_hire_start_time.lt=>Time.now, :p_hire_end_time=>nil},{:p_hire_start_time=>nil, :p_hire_end_time=>nil})} + scope :can_display, ->{any_of({:p_display_start_time.lt=>Time.now, :p_display_end_time.gt=>Time.now},{:p_display_start_time.lt=>Time.now, :p_display_end_time=>nil},{:p_display_start_time=>nil, :p_display_end_time=>nil})} WEEKDAYS = [ "Sunday", "Monday", @@ -288,6 +292,9 @@ class Property default_msg = "This property is unavaliable to reserved before #{property.can_hire_before_months} month ago." message += I18n.t("property_hire.unavailable_hint2",{:month=>property.can_hire_before_months,:default=>default_msg}) end + if property.hours_restriction > 0 + message += "
" + I18n.t("property_hire.hours_restriction_message", {:no_of_hours => property.hours_restriction, :duration => I18n.t("property_hire._#{property.hours_restriction_duration}") }) + end end return message.html_safe end @@ -299,8 +306,22 @@ class Property MemberProfile.find(self.owners) rescue [] end - def is_available_for_hire?(stime, etime, interval = nil, recurring_end_date = nil, time_setting_id = nil) + def get_user_total_user_hired_hours(member_profile_id, start_time, end_time) + hires = self.p_hires.where(:hiring_person_id => member_profile_id, :start_time.gte => start_time, :end_time.lte => end_time) + total_hours = 0.0 + hires.each do |hire| + diff = hire.end_time - hire.start_time + total_hours += diff * 24.0 + end + return total_hours + end + + def is_available_for_hire?(stime, etime, interval = nil, recurring_end_date = nil, time_setting_id = nil, member_profile) available = 0 + ma = ModuleApp.find_by_key("property_hire") + user = member_profile.user + is_user_manager = (user.is_admin? || user.is_manager?(ma) || user.is_sub_manager?(ma)) + return 1 if is_user_manager == true return 1 if self.set_unavailibility == false return 1 if self.weekdays.empty? && self.can_hire_before_months == 0 time_now = Time.now.to_datetime @@ -317,6 +338,24 @@ class Property return 0 end end + + if DateTime.now > self.start_date && DateTime.now < self.end_date + 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) + when "month" + sd = Date.new(stime.year, stime.month, 1) + edd = sd.next_month.prev_day + end + if self.get_user_total_user_hired_hours(member_profile.id.to_s, sd, edd) >= self.hours_restriction + return 0 + 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) diff --git a/app/views/admin/property_hires/_form.html.erb b/app/views/admin/property_hires/_form.html.erb index 166f506..4b5c132 100644 --- a/app/views/admin/property_hires/_form.html.erb +++ b/app/views/admin/property_hires/_form.html.erb @@ -125,15 +125,33 @@ <%= f.text_field :other_location %> -
+ <% +=begin%> +
<%= f.label :property_number, t("property_hire.property_number"), :class => "control-label muted" %>
<%= f.text_field :property_number %>
+
+<% +=end%> +
+ <%= f.label :p_display_start_time, t("property_hire.p_display_start_time"), :class => "control-label muted" %> +
+ <%= f.datetime_picker :p_display_start_time, :picker_type => "date", :no_label => true, :new_record => @property.new_record?, :data=>{"picker-type" => "range", "range" => "start"}, :format => "yyyy/MM/dd hh:mm" %> +
+
+ <%= f.label :p_display_end_time, t("property_hire.p_display_end_time"), :class => "control-label muted" %> +
+ <%= f.datetime_picker :p_display_end_time, :picker_type => "date", :no_label => true, :new_record => @property.new_record?, :data=>{"picker-type" => "range", "range" => "end"}, :format => "yyyy/MM/dd hh:mm" %> +
+
+
<%= f.label :p_hire_start_time, t("property_hire.p_hire_start_time"), :class => "control-label muted" %>
+
<%= t("property_hire.time_period_note") %>
<%= f.datetime_picker :p_hire_start_time, :picker_type => "date", :no_label => true, :new_record => @property.new_record?, :data=>{"picker-type" => "range", "range" => "start"}, :format => "yyyy/MM/dd hh:mm" %>
@@ -153,6 +171,7 @@
<%= f.label :p_open_start_time, t("property_hire.p_open_start_time"), :class => "control-label muted" %>
+
<%= t("property_hire.available_time_note") %>
<%= f.datetime_picker :p_open_start_time, :picker_type => "date", :no_label => true, :new_record => @property.new_record?, :data=>{"picker-type" => "range", "range" => "start"}, :format => "yyyy/MM/dd" %>
@@ -163,12 +182,16 @@
-
+ <% +=begin%> +
<%= f.label :purchase_date, t("property_hire.purchase_date"), :class => "control-label muted" %>
<%= f.datetime_picker :purchase_date, :no_label => true, :new_record => @property.new_record?, :picker_type => "date", :format => "yyyy/MM/dd" %>
-
+
+<% +=end%>
<%= f.label :owners, t("property_hire.owners"), :class => "control-label muted" %>
@@ -199,12 +222,16 @@ <%= f.text_field :owner_phone %>
-
+ <% +=begin%> +
<%= f.label :price, t("property_hire.price"), :class => "control-label muted" %>
<%= f.text_field :price %>
-
+
+<% +=end%>
@@ -378,20 +405,13 @@ <% else %>