From 946a9666c6b38ea35d574cbd568ef753dc8da5b2 Mon Sep 17 00:00:00 2001 From: rulingcom Date: Sat, 21 Dec 2024 17:26:27 +0800 Subject: [PATCH] added reminder, new translation and sorting fix --- .../admin/property_hires_controller.rb | 6 +- app/controllers/property_hires_controller.rb | 5 + app/helpers/admin/property_hires_helper.rb | 11 ++- app/models/hire_email_set.rb | 2 + app/views/admin/property_hires/_form.html.erb | 28 +++++- .../admin/property_hires/my_bookings.html.erb | 4 +- app/views/email/reminder_email.html.erb | 95 +++++++++++++++++++ config/locales/en.yml | 4 + config/locales/zh_tw.yml | 4 + 9 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 app/views/email/reminder_email.html.erb diff --git a/app/controllers/admin/property_hires_controller.rb b/app/controllers/admin/property_hires_controller.rb index d215212..1ee9d24 100644 --- a/app/controllers/admin/property_hires_controller.rb +++ b/app/controllers/admin/property_hires_controller.rb @@ -41,7 +41,7 @@ class Admin::PropertyHiresController < OrbitAdminController render :json => {"success" => true}.to_json end def my_bookings - @table_fields = ["property_hire.title","property_hire.hiring_person_name", "property_hire.reason_for_hire", "property_hire.hiring_person_number", "property_hire.period", "property_hire.passed", :actions] + @table_fields = ["property_hire.title","property_hire.hiring_person_name", "property_hire.reason_for_hire", "property_hire.hiring_person_number", "property_hire.period", "property_hire.passed", "property_hire.actions"] @bookings = PHire.where(:hiring_person_id => current_user.member_profile.id.to_s).desc(:created_at).page(params[:page]).per(10) end @@ -75,7 +75,7 @@ class Admin::PropertyHiresController < OrbitAdminController def show @table_fields = ["property_hire.hiring_person_name", "property_hire.reason_for_hire", "property_hire.hiring_person_number", "property_hire.period", "property_hire.passed", "property_hire.actions"] @property = Property.where(:uid => params[:id].split("-").last).first rescue nil - @bookings = @property.p_hires.desc(:created_at).page(params[:page]).per(10) + @bookings = @property.p_hires.desc(:start_time).page(params[:page]).per(10) end def destroy @@ -318,7 +318,7 @@ class Admin::PropertyHiresController < OrbitAdminController end def create_set (save_flag) @email_set = [] - ['p_hire','edit','delete'].each do |field_name| + ['p_hire','edit','delete', 'reminder'].each do |field_name| email_set = @property.hire_email_sets.select{|v| v.field_name==field_name} if email_set.length==0 title = Hash.new diff --git a/app/controllers/property_hires_controller.rb b/app/controllers/property_hires_controller.rb index fcc699f..757d3f4 100644 --- a/app/controllers/property_hires_controller.rb +++ b/app/controllers/property_hires_controller.rb @@ -300,7 +300,12 @@ class PropertyHiresController < ApplicationController email = property.get_owner_emails email << hire.hiring_person_email email = email.select{|e| e.present?} + # start_date = hire.start_date - hire.reminder_value.send(hire.reminder_unit) rescue nil Admin::PropertyHiresHelper::HireMethod.send_mail('p_hire',email,property.id,nil,hire.id,(current_user.id rescue nil)) + reminder_set = property.hire_email_sets.select{|v| v.field_name == "reminder"}[0] + if reminder_set.disabled == false && (reminder_set.reminder_value != nil || reminder_set.reminder_value > 0) + Admin::PropertyHiresHelper::HireMethod.send_mail('reminder',email,property.id,nil,hire.id,(current_user.id rescue nil)) + end end redirect_to index_page + "?method=hire_success" else diff --git a/app/helpers/admin/property_hires_helper.rb b/app/helpers/admin/property_hires_helper.rb index 92c7e9c..2b2c365 100644 --- a/app/helpers/admin/property_hires_helper.rb +++ b/app/helpers/admin/property_hires_helper.rb @@ -159,20 +159,27 @@ module Admin::PropertyHiresHelper hire = PHire.find(hire_id) rescue nil if hire mail_subject += " (#{I18n.t("property_hire.hire_time")}: #{hire.date.to_s.gsub("-","/")} #{hire.time})" + send_date = Time.current end end + if field_name == "reminder" + hire = PHire.find(hire_id) rescue nil + send_date = hire.start_time - email_set[0].reminder_value.send(email_set[0].reminder_unit) rescue nil + end if enable 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_sentdate: send_date, 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 end begin - mail.deliver + if field_name != "reminder" && send_date != nil + mail.deliver + end rescue => e puts ["email can't deliver",e] end diff --git a/app/models/hire_email_set.rb b/app/models/hire_email_set.rb index 7f3417e..95a3bc6 100644 --- a/app/models/hire_email_set.rb +++ b/app/models/hire_email_set.rb @@ -5,6 +5,8 @@ class HireEmailSet field :field_name, type: String field :title field :content + field :reminder_value, type: Integer, default: 0 + field :reminder_unit, type: String, default: 'month' field :disabled, type: Boolean, default: false belongs_to :property end diff --git a/app/views/admin/property_hires/_form.html.erb b/app/views/admin/property_hires/_form.html.erb index 4b5c132..97ef17e 100644 --- a/app/views/admin/property_hires/_form.html.erb +++ b/app/views/admin/property_hires/_form.html.erb @@ -348,9 +348,14 @@ <%= t('property_hire.delete') %> +
  • + + <%= t('property_hire.reminder') %> + +
  • - <% (0..2).each do |index1| %> + <% (0..3).each do |index1| %> <% active_email_set = index1==0 ? ' active' : '' %>
    @@ -364,6 +369,27 @@ <%= check_box_tag("property[hire_email_sets][#{index1}][disabled]", true ,@email_set[index1].disabled) %> + <% if index1 == 3 %> + + + + + <% end %> <% end %> diff --git a/app/views/email/reminder_email.html.erb b/app/views/email/reminder_email.html.erb new file mode 100644 index 0000000..fd4c800 --- /dev/null +++ b/app/views/email/reminder_email.html.erb @@ -0,0 +1,95 @@ +<% property = Property.where(id: @data['property_id']).first%> +<% email_set = property.hire_email_sets.select{|v| v.field_name == 'reminder'} %> +<% if email_set.length != 0 %> + <% if !(email_set[0].content.nil?) %> + <%= email_set[0].content[@data['locale']].html_safe %> + <% end %> +<% else %> + <%= t('property_hire.email_p_hire_content') %> +<% end %> +
    + +<% I18n.with_locale(@data['locale']) do %> +

    <%= property.title rescue nil %>

    + <% if !@data['hire_id'].nil? %> + <% hire = PHire.where(id: @data['hire_id']).first %> +
    + <%= t('property_hire.reminder_setting') %> + + <% units = ['months', 'day', 'hour', 'minute'] %> + + + <%#= hire_method.show_set_field(@email_set[index1].id,@email_set[index1]['reminder_value'],'hire_email_sets',index1,'reminder_value','number_field',with_id) %> + <%#= f.number_field :reminder_setting, :min=>0 %> + <%#= f.select :reminder_setting_unit, options_for_select(units.map{|unit| [t("property_hire._#{unit}"), unit]},f.object.need_hire_before_unit) %> +
    <%= t('property_hire.email_title') %> diff --git a/app/views/admin/property_hires/my_bookings.html.erb b/app/views/admin/property_hires/my_bookings.html.erb index 40b76da..397f2d4 100644 --- a/app/views/admin/property_hires/my_bookings.html.erb +++ b/app/views/admin/property_hires/my_bookings.html.erb @@ -35,10 +35,10 @@ - View + <%= t("property_hire.view") %> - Delete + <%= t("property_hire.delete") %>
    + + + + + + + + <% + p_hire_fields = property.p_hire_fields_enabled.map{|rf| [rf.id.to_s,rf]}.to_h + fields_name = property.get_all_fields + basic_keys = [] + if (property.set_availability rescue false) + basic_keys = ['date','time','hiring_person_name','hiring_person_email','recurring','recurring_interval','recurring_end_date','passed'] + else + basic_keys = ['hiring_person_name','hiring_person_email','start_time','end_time','recurring','recurring_interval','recurring_end_date','passed'] + end + fields_name = fields_name - basic_keys + keys = basic_keys + fields_name + keys = keys.select do |field_name| + return true if field_name.include?('p_hire_fields.') + tmp = property[field_name] + tmp.is_a?(Hash) ? (tmp["enable"] == "1") : true + end + p_hire_fields = property.p_hire_fields_enabled.map{|rf| [rf.id.to_s,rf]}.to_h + has_p_hire_fields = p_hire_fields.count != 0 + tmp_keys = [] + all_trans = keys.map do |k| + if has_p_hire_fields && k.include?("p_hire_fields") + tmp_keys << k.sub("p_hire_fields.",'') + nil + else + tmp_keys << k + property.custom_text(k,"name") + end + end + %> + <% p_hire_field_values = hire.p_hire_field_values.to_a %> + <% keys.each_with_index do |k,i| %> + <% if has_p_hire_fields && k.include?("p_hire_fields") + p_hire_field_id = tmp_keys[i] + v = p_hire_field_values.select{|v| v.p_hire_field_id.to_s == p_hire_field_id}.first + field_info = v.get_field_value rescue {} + if field_info["title"].present? && !field_info["value"].nil? + all_trans[i] = field_info["title"] + v = field_info["value"] + end + else + v = hire.send(k) + end + %> + <% if !v.nil? && !(k.include?('recurring') && hire.recurring != true)%> + + + + + <% end %> + <% end %> + +
    + <%= all_trans[i] %>:  + + <% if ['start_time','end_time','recurring','recurring_interval','recurring_end_date','passed'].exclude?(k) %> + <%= v.to_s.html_safe %> + <% elsif ['start_time','end_time','recurring_end_date'].include?(k) %> + <%= v.strftime('%Y/%m/%d %H:%M') rescue nil %> + <% elsif k == 'recurring_interval' %> + <%= t("property_hire.recurring_interval_types.#{v}") %> + <% elsif 'recurring' == k %> + <%= v ? t('property_hire.yes') : t('property_hire.no') %> + <% else %> + <%= v ? t('property_hire.yes') : t('property_hire.wait_for_permit') %> + <% end %> +
    + <% end %> +<% end %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 901d051..c7c4add 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -95,13 +95,17 @@ en: enable_notes_selector: Enable Note field selector editor: Editor email_p_hire_success: Reserve Success + email_reminder_success: Reminder Success email_edit_success: 'Property Hire Module:Edit Success' email_delete_success: 'Property Hire Module:Delete Success' email_p_hire_content: Reserve Success email_edit_content: 'Property Hire Module:Edit Success' email_delete_content: 'Property Hire Module:Delete Success' + email_reminder_content: 'Property Hire Module:Email Reminder Content' edit: Edit delete: Delete + reminder: Reminder + reminder_setting: Reminder Setting auto_send_email_set: Auto Send Email Setting email_title: Email Title email_content: Email Content diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 555f20b..51dbfbf 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -119,13 +119,17 @@ zh_tw: enable_notes_selector: 啟用備註選項 editor: 編輯者 email_p_hire_success: 預約成功 + email_reminder_success: Reminder Success email_edit_success: '租借模組:編輯成功' email_delete_success: '租借模組:刪除成功' email_p_hire_content: 預約成功 email_edit_content: '租借模組:編輯成功' email_delete_content: '租借模組:刪除成功' + email_reminder_content: 'Property Hire Module:Email Reminder Content' edit: 編輯 delete: 刪除 + reminder: Reminder + reminder_setting: Reminder Setting auto_send_email_set: 自動發信設定 email_title: 主旨 email_content: 內文