module Admin::PropertyHiresHelper def check_for_availability(stime, etime, pid, interval=nil, recurring_end_date=nil) property = Property.find(pid) return {"success" => false, "msg" => "Values are not ok."} if property.nil? || stime.blank? || etime.blank? if !stime.blank? stime = DateTime.parse(stime + Time.zone.to_s) rescue nil else stime = nil end if !etime.blank? etime = DateTime.parse(etime + Time.zone.to_s) rescue nil else etime = nil end if !recurring_end_date.blank? recurring_end_date = DateTime.parse(recurring_end_date + Time.zone.to_s) rescue nil else recurring_end_date = nil end data = {} return {"success" => false, "msg" => "Starting time cannot be greater than ending time."} if stime > etime if property.is_available_for_hire?(stime, etime) if property.is_already_hired?(stime, etime, interval, recurring_end_date) data = {"success" => true} else data = {"success" => false, "msg" => "Property is already hired during this time."} end else data = {"success" => false, "msg" => "Property is unavailable during this time."} end return data end 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 def self.send_mail(field_name,email,property_id,send_date=nil,hire_id=nil,user_id=nil) property = Property.where(id: property_id).first if !property.nil? email_set = property.hire_email_sets.select{|v| v.field_name == field_name} title = property['title'].collect{|k,v| v}.select{|v| !v.to_s.empty?}.join('/') note = property['note'].collect{|k,v| v}.select{|v| !v.to_s.empty?}.join('/') content = "title:#{title}
note:#{note}" if email_set.length==0 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: I18n.t("property_hire.email_#{field_name}_success"), template_data:{'property_id'=>property_id,'content'=>content,'locale'=>I18n.locale.to_s,'hire_id'=>hire_id,'user_id'=>user_id}) elsif !(email_set[0].disabled) 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: email_set[0].title[I18n.locale], template_data:{'property_id'=>property_id,'email_set_content'=>email_set[0].content.to_yaml,'content'=>content,'locale'=>I18n.locale.to_s,'hire_id'=>hire_id,'user_id'=>user_id}) end begin mail.deliver rescue => e puts ["email can't deliver",e] end end end end end