property_hire/app/helpers/admin/property_hires_helper.rb

178 lines
8.3 KiB
Ruby
Raw Normal View History

2017-01-20 09:02:50 +00:00
module Admin::PropertyHiresHelper
2021-09-06 16:45:14 +00:00
data = File.open(File.join(File.dirname(__FILE__), '../../../config', 'list.yml')).read
$property_list = YAML::load(ERB.new(data).result(binding)).symbolize_keys
2021-10-26 10:58:47 +00:00
include OrbitBackendHelper
2021-09-06 16:45:14 +00:00
def check_for_availability(stime, etime, pid, interval=nil, recurring_end_date=nil, time_setting_id=nil)
2017-01-20 09:02:50 +00:00
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?
timezone = (params[:timezone] rescue nil)
timezone = timezone ? timezone : Time.zone.to_s
2020-06-03 13:05:49 +00:00
if !stime.blank?
stime = DateTime.parse(stime + timezone) rescue nil
2020-06-03 13:05:49 +00:00
else
stime = nil
end
if !etime.blank?
etime = DateTime.parse(etime + timezone) rescue nil
2020-06-03 13:05:49 +00:00
else
etime = nil
end
if !recurring_end_date.blank?
recurring_end_date = DateTime.parse(recurring_end_date + timezone) rescue nil
begin
interval_time = 1.send(interval)
tmp_date = recurring_end_date - interval_time
if tmp_date < etime
I18n.with_locale(params[:locale]) do
interval_str= I18n.t("property_hire.1_#{interval}","1 #{interval}")
default_msg = "Recurring end date must exceed hire end time than #{interval_str}!"
msg = I18n.t("property_hire.recurring_end_date_must_exceed_time",{:time=>interval_str,:default=>default_msg})
return {"success" => false, "msg" => msg}
end
end
rescue
end
2020-06-03 13:05:49 +00:00
else
recurring_end_date = nil
end
2017-01-20 09:02:50 +00:00
data = {}
if stime > etime
I18n.with_locale(params[:locale]) do
return {"success" => false, "msg" => I18n.t("property_hire.starting_time_cannot_be_greater_than_ending_time")}
end
end
2021-09-06 16:45:14 +00:00
available_flag = property.is_available_for_hire?(stime, etime, interval, recurring_end_date, time_setting_id)
if available_flag == 1
2021-09-06 16:45:14 +00:00
if property.not_yet_hired?(stime, etime, interval, recurring_end_date,params[:phire_id], time_setting_id)
2017-01-20 09:02:50 +00:00
data = {"success" => true}
else
I18n.with_locale(params[:locale]) do
data = {"success" => false, "msg" => I18n.t("property_hire.property_is_already_hired_during_this_time")}
end
2017-01-20 09:02:50 +00:00
end
else
I18n.with_locale(params[:locale]) do
msg = I18n.t("property_hire.property_is_unavailable_during_this_time")
if available_flag == 2 ## need hire after
if recurring_end_date.present?
stime = [stime,recurring_end_date].max
end
can_hire_date = stime - (property.can_hire_before_months).month
2022-08-29 09:51:31 +00:00
msg += ("<br>" + I18n.t("property_hire.please_hire_after_date",{:date=>"{#{can_hire_date.new_offset(0).to_json.gsub('"','')}}"}))
elsif available_flag == 3 ## need hire before
default_msg = "This property must be reserved #{property.need_hire_before} #{property.need_hire_before_unit}s in advance."
msg += I18n.t("property_hire.unavailable_hint3",{:month=>property.need_hire_before,:unit=>I18n.t("property_hire._#{property.need_hire_before_unit}",:default=>property.need_hire_before_unit),:default=>default_msg})
elsif available_flag == 0
msg += ("<br>" + property.render_unavailable_message)
end
data = {"success" => false, "msg" => msg}
end
2017-01-20 09:02:50 +00:00
end
return data
end
2021-09-06 16:45:14 +00:00
def render_custom_text_field(f,field_name,type)
text = "<div>
<div class=\"input-append\">
<div class=\"tab-content\">"
@site_in_use_locales.each_with_index do |locale,i|
text += "<div class=\"tab-pane fade #{'active in' if i == 0}\" id=\"property_#{field_name}_#{type}_#{locale}\">
#{f.text_field_tag("#{f.object_name}[#{field_name}][#{type}][#{locale}]", f.object.custom_text(field_name,type,locale))}
</div>"
end
text += "<div class=\"btn-group\" data-toggle=\"buttons-radio\">"
@site_in_use_locales.each_with_index do |locale,i|
text += "<a class=\"btn #{'active' if i == 0}\" data-toggle=\"tab\" href=\"#property_#{field_name}_#{type}_#{locale}\" aria-expanded=\"true\">#{t(locale)}</a>"
end
text += "</div>
</div>
</div>
</div>"
text.html_safe
end
2020-03-23 17:26:52 +00:00
module HireMethod
extend ActionView::Helpers::UrlHelper
extend ActionView::Helpers::TagHelper
extend ActionView::Context
extend ActionView::Helpers::FormTagHelper
def self.set_input_name(input_name)
@input_name = input_name
end
def self.get_input_name
@input_name
end
def self.create_lang_panel(field)
tmp2 = content_tag(:div,:class => 'btn-group', :data=>{:toggle=>"buttons-radio"}) do
I18n.available_locales.collect do |key|
link_entry_ary = ["##{field}","_#{key}"]
link_entry = link_entry_ary.join
link_to(I18n.t(key),link_entry,:data=>{:toggle=>"tab"},:class=>"btn #{(key == I18n.locale ? "active" : nil)}",:for=>key)
end.join.html_safe
end
end
def self.multiple_lang_tag(index1,type_of_tag,field,value=nil,custom_options={},combine_element='',exteral_options={},panel_in_first=false)
content_tag(:div,{:class => "tab-panel"}.merge(exteral_options)) do
all_field = (get_input_name + "[#{index1}][#{field}][parant]").gsub(/\[/,'_').gsub(/\]/,'')
tmp = I18n.available_locales.collect do |locale|
active_flag = ((locale == I18n.locale) ? ' active' : '')
content_tag(:div,:class => "tab-content#{active_flag}",:id=>"#{all_field}_#{locale}") do
value_locale = ((value[locale] || value[locale.to_s]) rescue nil)
self.__send__("#{type_of_tag}_tag","#{get_input_name}[#{index1}][#{field}][#{locale}]",value_locale,custom_options)
end
end.join
if panel_in_first
tmp = create_lang_panel(all_field).html_safe + tmp.html_safe + combine_element
else
tmp = tmp.html_safe + create_lang_panel(all_field).html_safe + combine_element
end
tmp
end
end
def self.show_set_field(id,field_sets,key_field,key_index,field,markup='text_field',with_id=true)
end_block = with_id ? hidden_field_tag("property[#{key_field}][#{key_index}]"+"[id]",id) : ''
custom_options = markup == 'text_area' ? {:class => 'ckeditor'} : {}
a = multiple_lang_tag(key_field,markup,"#{key_index}][#{field}",field_sets,custom_options,end_block,{},markup=='text_area')
a.html_safe
end
2020-05-20 12:14:02 +00:00
def self.send_mail(field_name,email,property_id,send_date=nil,hire_id=nil,user_id=nil)
2024-08-24 03:00:08 +00:00
return if email.blank?
2020-03-23 17:26:52 +00:00
property = Property.where(id: property_id).first
if !property.nil?
email_set = property.hire_email_sets.select{|v| v.field_name == field_name}
2022-12-07 03:39:42 +00:00
title = property.title_translations.collect{|k,v| v}.select{|v| v.present?}.uniq.join('/')
note = property.note_translations.collect{|k,v| v}.select{|v| v.present?}.uniq.join('/')
2020-03-23 17:26:52 +00:00
content = "title:#{title}<br>note:#{note}"
mail_subject = I18n.t("property_hire.email_#{field_name}_success")
email_set_content = nil
enable = false
2020-03-23 17:26:52 +00:00
if email_set.length==0
enable = true
2020-03-23 17:26:52 +00:00
elsif !(email_set[0].disabled)
enable = true
mail_subject = email_set[0].title[I18n.locale]
email_set_content = email_set[0].content.to_yaml
end
if field_name == "p_hire" && (property.set_availability rescue false)
hire = PHire.find(hire_id) rescue nil
if hire
mail_subject += " (#{I18n.t("property_hire.hire_time")}: #{hire.date.to_s.gsub("-","/")} #{hire.time})"
end
end
if enable
2020-03-23 17:26:52 +00:00
mail = Email.create(mail_to: Array(email),
module_app_key:"property_hire",
template:"email/#{field_name}_email.html.erb",
mail_sentdate: send_date || Time.current,
mail_subject: mail_subject,
template_data:{'property_id'=>property_id,'email_set_content'=>email_set_content,'content'=>content,'locale'=>I18n.locale.to_s,'hire_id'=>hire_id,'user_id'=>user_id})
else
return false
2020-03-23 17:26:52 +00:00
end
begin
mail.deliver
rescue => e
puts ["email can't deliver",e]
end
end
end
end
2017-01-20 09:02:50 +00:00
end