property_hire/app/controllers/property_hires_controller.rb

234 lines
6.8 KiB
Ruby
Raw Permalink Normal View History

2017-01-20 09:02:50 +00:00
class PropertyHiresController < ApplicationController
include Admin::PropertyHiresHelper
def index
properties = Property.filter_by_categories.filter_by_tags.desc(:created_at)
data = properties.collect do |property|
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
{
"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),
2017-01-20 09:02:50 +00:00
"url_to_show" => OrbitHelper.url_to_show(property.to_param),
"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
2018-01-24 08:29:28 +00:00
data = check_for_availability(booking_p[:start_time],booking_p[:end_time],booking_p[:property_id], booking_p[:recurring_interval], booking_p[:recurring_end_date])
2017-01-20 09:02:50 +00:00
property = Property.find(booking_p[:property_id]) rescue nil
if data["success"] == true
hire = PHire.new(booking_p)
hire.passed = true if PropertyHireSetting.auto_approve_enabled?
2017-01-20 09:02:50 +00:00
hire.save
2020-03-23 17:26:52 +00:00
if !property.nil?
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
2017-01-20 09:02:50 +00:00
redirect_to params[:url]
else
session["hire-save-msg"] = data["msg"]
if property.nil?
redirect_to "/" + I18n.locale.to_s + params[:url]
else
redirect_to "/" + I18n.locale.to_s + params[:url] + "/#{property.to_param}?method=hire"
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 = PHire.new
{
"hire" => hire,
"property" => property,
"page" => page.url,
"current_user" => OrbitHelper.current_user
}
end
def check_availability
2018-01-24 08:29:28 +00:00
data = check_for_availability params[:stime], params[:etime], params[:property_id], params[:interval], params[:recurring_end_date]
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)
events = PHire.monthly_event(sdt,edt,params[:property_id])
re = PHire.recurring_event(sdt,edt,params[:property_id])
2018-01-24 08:29:28 +00:00
allevents = events.inject(re, :<<)
2017-01-20 09:02:50 +00:00
end
end
respond_to do |format|
format.html # 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
private
def booking_params
params.require(:p_hire).permit!
end
end