Fix hire bug.

Add required fields check by ruby.
This commit is contained in:
BoHung Chiu 2022-06-20 10:01:36 +08:00
parent d1b61401cb
commit 92b3b91138
6 changed files with 65 additions and 13 deletions

View File

@ -221,6 +221,13 @@ class PropertyHiresController < ApplicationController
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)
if data["success"] == true
error_messages = property.check_require_fields(booking_p)
if error_messages.present?
data["msg"] = error_messages
data["success"] = false
end
end
if data["success"] == true
hire = nil
if params[:phire_id]
@ -240,6 +247,7 @@ class PropertyHiresController < ApplicationController
end
redirect_to index_page + "?method=hire_success"
else
session[:hire_data] = booking_p
session["hire-save-msg"] = data["msg"]
if property.nil?
redirect_to index_page

View File

@ -20,7 +20,7 @@ module Admin::PHireFieldHelper
@member = member
@attribute_value = @member.get_value_from_field_id(id,p_hire)
@attribute_type = attribute_type
@new_attribute = @attribute_value.nil?
@new_attribute = @attribute_value.nil? || @attribute_value.new_record?
@attribute_value = @attribute_value || p_hire.p_hire_field_values.build(p_hire_field_id: id)
@prefiled_value = value || @attribute_value.value rescue nil
return instance_eval("render_#{markup}") rescue ""

View File

@ -371,7 +371,8 @@ class Property
else
d_step = 0
end
bookings = bookings.any_of([{:start_time.gte => etime},{:end_time.gte => etime}])
not_in_ids = bookings.any_of([{:end_time.lt=>stime},{:start_time.gt=>etime}]).pluck(:id)
bookings = bookings.where(:id.nin=>not_in_ids)
if time_setting_id
recurring_bookings = recurring_bookings.where(:property_day_setting_id=>time_setting_id)
bookings = recurring_bookings.where(:property_day_setting_id=>time_setting_id)
@ -465,7 +466,45 @@ class Property
end
return available
end
def check_require_fields(booking_p)
fields_name = self.get_all_fields
has_p_hire_fields = self.p_hire_fields.count != 0
p_hire_fields = {}
if has_p_hire_fields
p_hire_fields = self.p_hire_fields.map{|rf| [rf.id.to_s,rf]}.to_h
end
error_messages = ""
form_index = 0
cross_lang_types = ['select','date','radio_button','checkbox']
unfilled_text = I18n.t('property_hire.unfilled')
available_locales = I18n.available_locales
available_locales_trans = available_locales.map{|l| [l, I18n.t(l)]}.to_h
fields_name.each do |field_name|
if has_p_hire_fields && field_name.include?("p_hire_fields")
rf = p_hire_fields[field_name.sub("p_hire_fields.",'')]
if rf.markup != 'hint_text' && rf.to_require
v = booking_p["p_hire_field_values_attributes"][form_index.to_s]['value']
if cross_lang_types.include?(rf.markup) || rf.get_data["cross_lang"] == "true"
if v.blank?
error_messages += "#{rf.title}: #{unfilled_text}\n"
end
else
available_locales.each do |l|
if (v[l].blank? rescue true)
error_messages += "#{rf.title}(#{available_locales_trans[l]}): #{unfilled_text}\n"
end
end
end
end
form_index = form_index +1
else
if self.send(field_name)["required"] == "true" && booking_p[field_name].blank?
error_messages += "#{self.custom_text(field_name,"name")}: #{unfilled_text}\n"
end
end
end
error_messages
end
def self.time_iterate(start_time, end_time, step, &block)
times = []
if block_given?

View File

@ -3,6 +3,7 @@
<script src="https://polyfill.io/v3/polyfill.min.js?features=Intl.DateTimeFormat,Intl.DateTimeFormat.~locale.en,Intl.NumberFormat.~locale.en"></script>
<script type="text/javascript" src="/assets/property_hire_fullcalendar.min.js"></script>
<script type="text/javascript" src="/assets/property_hire_calendar_frontend.js"></script>
<%= javascript_include_tag "basic/jquery.nanoscroller.js" %>
<%
data = action_data
hire = data["hire"]
@ -257,8 +258,8 @@
<% end %>
</ul>
<% if session["hire-save-msg"].present? %>
<div id="property-unavaialable-alert" class="alert alert-danger" role="alert"><b>Sorry! </b><span> <%= session["hire-save-msg"] %></span></div>
<script type="text/javascript">alert("<%= session["hire-save-msg"] %>")</script>
<div id="property-unavaialable-alert" class="alert alert-danger" role="alert"><b>Sorry! </b><br><span> <%= session["hire-save-msg"].gsub("\n",'<br>').html_safe %></span></div>
<script type="text/javascript">alert("<%= escape_javascript(session["hire-save-msg"]) %>")</script>
<% session.delete("hire-save-msg") %>
<% end %>
</article>
@ -304,7 +305,7 @@
<div style="display: grid;">
<span id="hidden_date" class="pull-left" style="margin-right: 1em;"></span>
<%= fields_for :timepicker do |f|%>
<%= f.time_picker :timepicker, :no_label => true, :new_record => hire.new_record? && !recover,:format=>"HH:mm", :class => "pull-left", :data=>{"picker-type" => "range", "range" => "end", "fv-validation" => "required;", "fv-messages" => "Cannot be empty;"} %>
<%= f.time_picker :timepicker, :no_label => true, :new_record => hire.new_record? && !recover,:format=>"HH:mm", :class => "pull-left", :data=>{"picker-type" => "range", "range" => "end"} %>
<% end %>
<div class="pull-left btn-group" style="margin-top: 0.5em;">
<button id="confirm_date" class="btn btn-primary btn-sm" style="margin-right: 0.5em;" onclick="set_datetimepicker()"><%=t("property_hire.confirm")%></button>
@ -474,7 +475,7 @@
<%= f.label :start_time, "*"+t("property_hire.start_time"), :class => "col-sm-#{label_col} control-label" %>
<% if property.calendar_type == 0 %>
<div class="col-sm-<%=input_col%>">
<%= f.datetime_picker :start_time, :no_label => true, :new_record => hire.new_record? && !recover,:class => "pull-left", :data=>{"picker-type" => "range", "range" => "start", "fv-validation" => "required;", "fv-messages" => "Cannot be empty;"} %>
<%= f.datetime_picker :start_time, :no_label => true, :new_record => hire.new_record? && !recover,:class => "pull-left", :data=>{"picker-type" => "range", "range" => "start","fv-validation" => "required;", "fv-messages" => "Cannot be empty;"} %>
<button type="button" id="pick_start_date" onclick="change_pick(this)" class="btn btn-primary btn-sm pull-left" style="margin-left: 1em;"><%=t("property_hire.pick_from_calendar")%></button>
</div>
<% end %>
@ -482,7 +483,7 @@
<div class="form-group">
<%= f.label :end_time, "*"+t("property_hire.end_time"), :class => "col-sm-#{label_col} control-label" %>
<div class="col-sm-<%=input_col%>">
<%= f.datetime_picker :end_time, :no_label => true, :new_record => hire.new_record? && !recover,:class => "pull-left", :data=>{"picker-type" => "range", "range" => "end", "fv-validation" => "required;", "fv-messages" => "Cannot be empty;"} %>
<%= f.datetime_picker :end_time, :no_label => true, :new_record => hire.new_record? && !recover,:class => "pull-left", :data=>{"picker-type" => "range", "range" => "end","fv-validation" => "required;", "fv-messages" => "Cannot be empty;"} %>
<button type="button" id="pick_end_date" onclick="change_pick(this)" class="btn btn-primary btn-sm pull-left" style="margin-left: 1em;"><%=t("property_hire.pick_from_calendar")%></button>
</div>
</div>
@ -498,7 +499,7 @@
<div class="form-group">
<%= f.label :recurring_interval, t("property_hire.recurring_interval"), :class => "col-sm-#{label_col} control-label" %>
<div class="col-sm-1">
<%= f.select :recurring_interval, PHire::INTERVALS.collect{|int| [t("property_hire.recurring_interval_types.#{int}"), int] }, {:prompt => t('property_hire.select_interval')}, {:data => {"fv-validation" => "requiredifrecurring;" , "fv-messages" => "Cannot be empty;"}} %>
<%= f.select :recurring_interval, PHire::INTERVALS.collect{|int| [t("property_hire.recurring_interval_types.#{int}"), int] }, {:prompt => t('property_hire.select_interval'),:data=>{"fv-validation"=> "requiredifrecurring;", "fv-messages"=> "Cannot be empty;"}} %>
</div>
</div>
@ -583,9 +584,9 @@
<div class="col-sm-<%=input_col%>">
<% placeholder = property.custom_text(field_name,"placeholder") %>
<% if custom_field_type[field_name] %>
<%= f.send(custom_field_type[field_name], field_name , {:class => "form-control", :placeholder => placeholder, :value => default_values[field_name], :data => (required ? {"fv-validation" => "required;", "fv-messages" => "Cannot be empty;"} : nil)}) %>
<%= f.send(custom_field_type[field_name], field_name , {:class => "form-control", :placeholder => placeholder, :value => default_values[field_name], :required=>(required ? 'required' : nil)}) %>
<% else %>
<%= f.text_field field_name, :class => "form-control", :placeholder => placeholder, :value => default_values[field_name], :data => (required ? {"fv-validation" => "required;", "fv-messages" => "Cannot be empty;"} : nil) %>
<%= f.text_field field_name, :class => "form-control", :placeholder => placeholder, :value => default_values[field_name], :required=>(required ? 'required' : nil) %>
<% end %>
</div>
</div>
@ -608,6 +609,7 @@
<% if f.object.id.present? %>
<%= f.hidden_field :id %>
<% end %>
<div class="nano"><div class="content"></div></div>
<%= f.submit t("property_hire.save"), :class => "btn btn-primary" %>
<%= f.hidden_field :property_id, :value => property.id %>
<input type="hidden" name="url" value="<%= url %>" />
@ -755,7 +757,9 @@
$("#unavailable-schedule").on("click",function(){
})
if( document.createElement('canvas').getContext == undefined ){ //html5 not supported
$('[required]:not([readonly])').attr({"data-fv-validation": "required;", "data-fv-messages": "Cannot be empty;"});
}
var hireForm = new FormValidator($("#hire_form"));
hireForm.validate_functions.checkForDates = function(value,element){
return value == "1";
@ -767,7 +771,6 @@
return true;
}
}
$("#p_hire_recurring").on("click",function(){
$("#dates_validated").val("0");
$("#property-avaialable-alert").hide();

View File

@ -5,6 +5,7 @@ en:
restful_actions:
fields_display_order: "Fields display order"
property_hire:
unfilled: Unfilled
hire_time: Reservation time
back_to_hire_page: "Back to Reserve Page"
apply_default_time_settings: Apply default time settings

View File

@ -5,6 +5,7 @@ zh_tw:
restful_actions:
fields_display_order: "欄位顯示順序"
property_hire:
unfilled: 未填寫
hire_time: 預約時間
back_to_hire_page: "回到預約頁面"
apply_default_time_settings: 套用預設時段設定