property_hire/app/controllers/property_hires_controller.rb

492 lines
17 KiB
Ruby
Raw Normal View History

2017-01-20 09:02:50 +00:00
class PropertyHiresController < ApplicationController
include Admin::PropertyHiresHelper
def index
2021-09-06 16:45:14 +00:00
properties = Property.filter_by_categories.filter_by_tags.sort_order
2017-01-20 09:02:50 +00:00
data = properties.collect do |property|
actions = []
2021-09-06 16:45:14 +00:00
url_to_show = OrbitHelper.url_to_show(property.to_param)
2017-01-20 09:02:50 +00:00
if property.can_be_hired
2021-09-06 16:45:14 +00:00
hire_url = url_to_show + "?method=hire"
2017-01-20 09:02:50 +00:00
actions << {
"text" => t("property_hire.hire"),
"btn-class" => "btn-primary",
2021-09-06 16:45:14 +00:00
"link" => hire_url
2017-01-20 09:02:50 +00:00
}
2021-09-06 16:45:14 +00:00
unless property.disable_view_calendar_page
actions << {
"text" => t("property_hire.view_calendar"),
"btn-class" => "btn-info",
"link" => url_to_show + "?method=view_calendar"
}
end
if property.disable_content_page
url_to_show = hire_url
end
2017-01-20 09:02:50 +00:00
end
{
"title" => property.title,
"image" => (property.image.url.blank? ? '" style="display: none;' : property.image.url),
"image-thumb" => (property.image.thumb.url.blank? ? '" style="display: none;' : property.image.thumb.url),
2021-09-06 16:45:14 +00:00
"url_to_show" => url_to_show,
2017-01-20 09:02:50 +00:00
"location" => property.get_location_name,
"actions" => actions
}
end
headers = [
{
"column" => t("property_hire.title")
},
{
"column" => ""
2017-01-20 09:02:50 +00:00
},
{
"column" => t("property_hire.location")
},
{
"column" => t("property_hire.actions")
}
]
{
"properties" => data,
"headers" => headers,
"total_pages" => properties.total_pages
}
end
def show
params = OrbitHelper.params
property = Property.where(:uid => params[:uid]).first rescue nil
page = Page.where(:page_id => params[:page_id]).first
return {} if property.nil?
data = []
data << {
"header" => t("property_hire.title"),
"value" => property.title
}
if !property.property_usage.blank?
data << {
"header" => t("property_hire.property_usage"),
"value" => property.property_usage
}
end
if !property.note.blank?
data << {
"header" => t("property_hire.note"),
"value" => property.note
}
end
2019-06-13 17:33:44 +00:00
if !property.property_number.blank?
data << {
"header" => t("property_hire.property_number"),
"value" => property.property_number
}
end
2017-01-20 09:02:50 +00:00
data << {
"header" => t("property_hire.can_be_hired"),
"value" => (property.can_be_hired ? "<span class='label label-success'>Yes</span>" : "<span class='label label-danger'>No</span>")
}
2019-06-13 17:33:44 +00:00
if !property.purchase_date.to_s.blank?
data << {
"header" => t("property_hire.purchase_date"),
"value" => property.purchase_date.strftime("%Y-%m-%d")
}
end
2017-01-20 09:02:50 +00:00
if property.owners.empty?
owners = property.other_owner
else
owners = property.owner_profiles.collect{|mp| mp.name}.join(",")
end
if !owners.blank?
data << {
"header" => t("property_hire.owners"),
"value" => owners
}
end
if !property.owner_email.blank?
data << {
"header" => t("property_hire.owner_email"),
"value" => property.owner_email
}
end
if !property.owner_phone.blank?
data << {
"header" => t("property_hire.owner_phone"),
"value" => property.owner_phone
}
end
if !property.price.blank?
data << {
"header" => t("property_hire.price"),
"value" => property.price
}
end
if !property.get_location_name.blank?
data << {
"header" => t("property_hire.location"),
"value" => property.get_location_name
}
end
2017-01-20 09:02:50 +00:00
actions = []
if property.can_be_hired
actions << {
"text" => t("property_hire.hire"),
"btn-class" => "btn-primary",
"link" => OrbitHelper.url_to_show(property.to_param) + "?method=hire"
}
actions << {
"text" => t("property_hire.view_calendar"),
"btn-class" => "btn-info",
"link" => OrbitHelper.url_to_show(property.to_param) + "?method=view_calendar"
}
end
{
"data" => data,
"actions" => actions,
"extras" => {
"image" => property.image.url,
"image-thumb" => property.image.thumb.url,
"back_url" => page.url
}
}
end
def view_calendar
params = OrbitHelper.params
property = Property.where(:uid => params[:uid]).first rescue nil
page = Page.where(:page_id => params[:page_id]).first
return {} if property.nil?
{
"property" => property,
"url" => page.url,
"current_user" => OrbitHelper.current_user
}
end
def make_booking
booking_p = booking_params
2021-09-06 16:45:14 +00:00
time_setting_id = booking_p[:time]
start_time = booking_p[:start_time]
end_time = booking_p[:end_time]
time_setting = nil
is_admin_page = (params[:url] == "admin")
2021-09-06 16:45:14 +00:00
if time_setting_id.present?
start_time = booking_p[:date]
end_time = start_time
time_setting = PropertyDaySetting.find(time_setting_id)
2021-09-07 05:07:09 +00:00
if booking_p[:recurring_end_date].present?
booking_p[:recurring_end_date] = DateTime.parse(booking_p[:recurring_end_date].split(" ")[0] + ' ' + time_setting.end_time + Time.zone.to_s)
2021-09-07 05:07:09 +00:00
end
booking_p[:start_time] = booking_p[:date] + " " + time_setting.start_time
booking_p[:end_time] = booking_p[:date] + " " + time_setting.end_time
booking_p[:property_day_setting_id] = booking_p[:time]
booking_p.delete(:time)
2021-09-06 16:45:14 +00:00
end
2017-01-20 09:02:50 +00:00
property = Property.find(booking_p[:property_id]) rescue nil
begin
index_page = is_admin_page ? "/#{I18n.locale}/admin/property_hires/#{property.to_param}" : "/#{I18n.locale}#{params[:url]}/#{property.to_param}"
rescue
index_page = is_admin_page ? "/#{I18n.locale}/admin/property_hires" : "/#{I18n.locale}#{params[:url]}"
end
hire_page = is_admin_page ? request.referer : "#{index_page}?method=hire"
need_validate = (PropertyHireSetting.last.allow_no_logins_user rescue false) && current_user.nil?
session.delete(:hire_data)
if need_validate
unless gotcha_valid?
session[:hire_data] = booking_p
session["hire-save-msg"] = I18n.t("property_hire.recaptcha.errors.verification_failed")
if property.nil?
redirect_to index_page and return
else
redirect_to hire_page and return
end
end
end
if params[:url] == "admin"
params[:phire_id] = booking_p[:id]
end
data = check_for_availability(start_time,end_time,booking_p[:property_id], booking_p[:recurring_interval], booking_p[:recurring_end_date], time_setting_id)
2017-01-20 09:02:50 +00:00
if data["success"] == true
hire = nil
if params[:phire_id]
hire = PHire.find(params[:phire_id])
hire.update_attributes(booking_p)
else
hire = PHire.new(booking_p)
2021-09-06 16:45:14 +00:00
end
hire.passed = true if PropertyHireSetting.auto_approve_enabled?
2017-01-20 09:02:50 +00:00
hire.save
if !property.nil? && !is_admin_page
2020-03-23 17:26:52 +00:00
email = Array(MemberProfile.find(property.owners)).collect{|v| v.email} rescue []
email = User.all.select{|v| v.is_admin? && v.user_name != 'rulingcom'}.collect{|v| v.member_profile.email} if email.length == 0
email << hire.hiring_person_email
2020-05-20 08:55:19 +00:00
Admin::PropertyHiresHelper::HireMethod.send_mail('p_hire',email,property.id,nil,hire.id)
2020-03-23 17:26:52 +00:00
end
redirect_to index_page
2017-01-20 09:02:50 +00:00
else
session["hire-save-msg"] = data["msg"]
if property.nil?
redirect_to index_page
2017-01-20 09:02:50 +00:00
else
redirect_to hire_page
2017-01-20 09:02:50 +00:00
end
end
end
def hire
params = OrbitHelper.params
property = Property.where(:uid => params[:uid]).first rescue nil
page = Page.where(:page_id => params[:page_id]).first
return {} if property.nil?
hire = nil
session = OrbitHelper.request.session rescue {}
recover = false
if (session[:hire_data] rescue false)
begin
hire = PHire.new({:id=>nil}.merge(session[:hire_data]))
recover = true
rescue => e
puts e.to_s
hire = PHire.new(:id=>nil)
end
else
hire = PHire.new(:id=>nil)
end
allow_no_logins_user = PropertyHireSetting.last.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
2017-01-20 09:02:50 +00:00
{
"hire" => hire,
"property" => property,
"page" => page.url,
"current_user" => OrbitHelper.current_user,
"allow_no_logins_user" => allow_no_logins_user,
"carousel_display_style" => "width: #{property.carousel_image_width};",
"all_day_settings" => all_day_settings,
"recover" => recover
2017-01-20 09:02:50 +00:00
}
end
def check_availability
2021-09-06 16:45:14 +00:00
time_setting_id = params[:time_setting_id]
data = check_for_availability params[:stime], params[:etime], params[:property_id], params[:interval], params[:recurring_end_date], time_setting_id
2017-01-20 09:02:50 +00:00
render :json => data.to_json
end
def get_bookings
events =[]
property = Property.find(params[:property_id]) rescue nil
if !property.nil?
if params[:start].present? && params[:end].present?
sdt = Time.at(params[:start].to_i)
edt = Time.at(params[:end].to_i)
2021-09-07 05:07:09 +00:00
events = PHire.monthly_event(sdt,edt,params[:property_id],property.set_availability)
re = PHire.recurring_event(sdt,edt,params[:property_id],property.set_availability)
2021-09-06 16:45:14 +00:00
events = events.map{|e| e.as_json}
2018-01-24 08:29:28 +00:00
allevents = events.inject(re, :<<)
2021-09-07 05:07:09 +00:00
allevents = allevents.sort_by{|e| e[:start]}
@need_check_events = allevents.map{|e| [e[:date],e[:s_id]]}
if property.set_availability && params[:display_hire_event]
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
2021-09-09 02:14:57 +00:00
@check_start_date = property.start_date.to_date rescue nil
@check_end_date = property.end_date.to_date rescue nil
if check_setting
2021-09-09 02:14:57 +00:00
if (@check_start_date > edt rescue false) || (@check_end_date < sdt rescue false)
check_setting = false
end
end
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
2021-09-06 16:45:14 +00:00
if all_day_settings.count != 0
2021-09-07 05:07:09 +00:00
time_now = Time.zone.now
2021-09-06 16:45:14 +00:00
get_start_time = [sdt,time_now].max
get_end_time = edt
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
allevents += generate_all_reserve_buttons(get_start_time,get_end_time,all_day_settings,check_setting)
2021-09-06 16:45:14 +00:00
end
end
end
2017-01-20 09:02:50 +00:00
end
end
respond_to do |format|
format.html { render json: allevents.to_json }# index.html.erb
2018-01-24 08:29:28 +00:00
format.json { render json: allevents.to_json }
2017-01-20 09:02:50 +00:00
end
end
2021-09-06 16:45:14 +00:00
def reserve_calendar_event(date,title,allow_times)
available = true
2021-09-09 02:14:57 +00:00
if @check_setting && allow_times.select{|a| !a[5]}.count != 0
available = (date > @check_end_date rescue false) || (date < @check_start_date rescue false)
unless available
allow_times = allow_times.select do |allow_time|
2021-09-09 02:14:57 +00:00
if allow_time[5]
true
elsif (allow_time[0] > @check_end_time || allow_time[1] < @check_start_time)
true
else
false
end
end
available = (allow_times.count != 0)
end
end
if available
{:title=>title, :start=>date.to_s, :end => (date + 1.day).to_s, :allDay => true, :color => "#3788d8",:allow_times=>allow_times,:classNames=>["reserve_btn"]}
else
{}
end
2021-09-06 16:45:14 +00:00
end
def generate_all_reserve_buttons(startt,endt,all_day_settings,check_setting=false)
@check_setting = check_setting
2021-09-06 16:45:14 +00:00
@display_title = I18n.t("property_hire.reserve")
@allevents = []
startt = startt.in_time_zone(Time.zone)
endt = endt.in_time_zone(Time.zone)
2021-09-06 16:45:14 +00:00
start_wday = startt.wday
start_date = startt.to_date
@start_date = start_date
end_date = endt.to_date
@end_date = end_date
@first_check_time = startt.strftime("%H:%M")
@all_day_settings = all_day_settings
@startt = startt
@endt = endt
2021-09-07 05:07:09 +00:00
def generate_events(start_wday,end_wday,type=0,start_validate=false)
2021-09-06 16:45:14 +00:00
if type == 0
2021-09-07 05:07:09 +00:00
(start_wday..end_wday).each_with_index do |wday,i|
2021-09-06 16:45:14 +00:00
wday_str = wday.to_s
2021-09-07 05:07:09 +00:00
is_start_day = (i == 0 && start_validate)
2021-09-06 16:45:14 +00:00
if @all_day_settings.has_key?(wday_str)
check_time = nil
check_time = @first_check_time if is_start_day
i = @need_check_events.index{|e| e[0] > @start_date}
if i.nil?
if @need_check_events.length != 0 && @need_check_events[-1][0] <= @start_date
i = @need_check_events.length - 1
else
2021-09-07 05:07:09 +00:00
i = -1
end
elsif i != -1
i -= 1
2021-09-06 16:45:14 +00:00
end
check_events = []
if i != -1
check_events = @need_check_events[0..i]
end
@need_check_events = @need_check_events[(i+1)..-1]
settings = @all_day_settings[wday_str]
if check_events.count != 0
settings = settings.select do |s|
flag = true
if check_time
if check_time > s[1]
flag = false
end
end
if flag
2021-09-07 05:07:09 +00:00
if s[4] == 0 #reservation_limit == 0 => no limit
2021-09-06 16:45:14 +00:00
true
else
2021-09-07 05:07:09 +00:00
check_events.select{|e| e[1] == s[2] }.count < s[4]
2021-09-06 16:45:14 +00:00
end
else
false
end
end
end
if settings.count != 0
@allevents << reserve_calendar_event(@start_date,@display_title,settings)
end
end
@start_date += 1.day
if @start_date > @end_date
break
end
end
else
@all_day_settings.each do |wday_str,settings|
2021-09-07 05:07:09 +00:00
tmp_date = @start_date + wday_str.to_i.send("day")
i = @need_check_events.index{|e| e[0] > tmp_date}
if i.nil?
if @need_check_events.length != 0 && @need_check_events[-1][0] <= tmp_date
i = @need_check_events.length - 1
else
2021-09-07 05:07:09 +00:00
i = -1
end
elsif i != -1
i -= 1
2021-09-06 16:45:14 +00:00
end
check_events = []
if i != -1
check_events = @need_check_events[0..i]
end
@need_check_events = @need_check_events[(i+1)..-1]
if check_events.count != 0
settings = settings.select do |s|
2021-09-07 05:07:09 +00:00
if s[4] == 0 #reservation_limit == 0 => no limit
true
else
check_events.select{|e| e[1] == s[2] }.count < s[4]
end
2021-09-06 16:45:14 +00:00
end
end
if settings.count != 0
@allevents << reserve_calendar_event(tmp_date,@display_title,settings)
end
end
@start_date += 7.day
end
end
all_days = (end_date-start_date).to_i
end_wday = 6
end_date_wday = end_date.wday
only_first_week = false
if (start_wday + all_days <= 6)
end_wday = end_date_wday
only_first_week = true
end
2021-09-07 05:07:09 +00:00
generate_events(start_wday,end_wday,0,true)
2021-09-06 16:45:14 +00:00
unless only_first_week
all_days = all_days - (end_wday - start_wday)
while all_days > 6 do
2021-09-07 05:07:09 +00:00
generate_events(0,6,1)
2021-09-06 16:45:14 +00:00
all_days -= 7
end
generate_events(0,all_days)
end
@allevents
end
2017-01-20 09:02:50 +00:00
private
def booking_params
2020-07-22 10:13:50 +00:00
p_hire_params = params.require(:p_hire).permit!
2021-09-06 16:45:14 +00:00
property = Property.find(params[:p_hire][:property_id]) rescue Property.last
2020-07-22 10:13:50 +00:00
if(property.enable_notes_selector rescue false)
note_texts = ""
property.notes_selector.each do |index,sub_hash|
name = sub_hash["name"][I18n.locale.to_s]
name = sub_hash["name"].values.select{|v| v.present?}.first.to_s if name.blank?
values = sub_hash["value"][I18n.locale.to_s]
values = sub_hash["value"].values.select{|v| v.present?}.first.to_s if values.blank?
2021-09-06 16:45:14 +00:00
value_text = p_hire_params["notes_selector"][index.to_s].to_a.map{|i| values[i.to_i]}.join(",") rescue ""
2020-07-22 10:13:50 +00:00
value_text = I18n.t("property_hire.none") if value_text.blank?
note_texts += (name + ":"+value_text)
note_texts += "<br>".html_safe
end
p_hire_params["note_for_hire"] = note_texts
p_hire_params.delete("notes_selector")
end
return p_hire_params
2017-01-20 09:02:50 +00:00
end
end