property_hire/app/controllers/property_hires_controller.rb

215 lines
6.0 KiB
Ruby
Raw 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,
"image-thumb" => property.image.thumb.url,
"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" => t("property_hire.image")
},
{
"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
data << {
"header" => t("property_hire.property_number"),
"value" => property.property_number
}
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>")
}
data << {
"header" => t("property_hire.purchase_date"),
"value" => property.purchase_date.strftime("%Y-%m-%d")
}
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
data << {
"header" => t("property_hire.owners"),
"value" => owners
}
data << {
"header" => t("property_hire.owner_email"),
"value" => property.owner_email
}
data << {
"header" => t("property_hire.owner_phone"),
"value" => property.owner_phone
}
data << {
"header" => t("property_hire.price"),
"value" => property.price
}
2017-01-20 09:02:50 +00:00
data << {
"header" => t("property_hire.location"),
"value" => property.get_location_name
}
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
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