venue_management/app/models/venue_management_reminder.rb

105 lines
4.9 KiB
Ruby
Raw Permalink Normal View History

2020-11-12 13:51:31 +00:00
# encoding: utf-8
class VenueManagementReminder
include Mongoid::Document
include Mongoid::Timestamps
field :description, localize: true
field :reminder_date, type: Date
field :reminder_date_reminder, type: Boolean,default: false
field :reminder_date_reminder_day, type: Integer
field :calendar_data,type: Hash,default: {type:{},key:[],page_id: {}}
field :calendar_dict,type: Hash,default: {}
has_many :venue_management_emails, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :venue_management_emails, :allow_destroy => true
def to_calendar_param
vm_contract = self.venue_management_contract rescue VenueManagementContract.all.select{|v| v.venue_management_reminder_ids.include? self.id}.first
venue_main = vm_contract.venue_management_main
self.description+'-'+vm_contract.id.to_s+'?method=show_contract'
end
def calendar_id(field)
self.calendar_dict[field]
end
before_save do
venue_main = self.venue_management_contract.venue_management_main rescue VenueManagementContract.all.select{|v| v.venue_management_reminder_ids.include? self.id}.first.venue_management_main
calendar_keys = Array(self.calendar_data['key'])
(self.calendar_dict.keys - calendar_keys).each do |key_deleted|
event = Event.where(:id => self.calendar_dict[key_deleted]).first
event.model_class = nil
event.destroy
self.calendar_dict.delete(key_deleted)
end
if !calendar_keys.blank?
calendar_keys.each do |key|
key_id = self.calendar_id(key)
current_user_id = OrbitHelper.current_user.id rescue nil
in_use_locales = Site.first.in_use_locales
title_tp = {}
note_tp = {}
titles = venue_main['title'].select{|k,v| v.present?}.to_h
notes = self.description_translations.select{|k,v| v.present?}.to_h
in_use_locales.each do |locale|
if titles[locale].nil?
title_tp[locale] = titles.values.first.to_s + " " + I18n.t("venue_management.reminder")
else
title_tp[locale] = venue_main['title'][locale].to_s + " " + I18n.t("venue_management.reminder")
end
note_tp[locale] = self.description_translations[locale].blank? ? I18n.t(:url_alt) +': ' + notes.values.first.to_s : I18n.t(:url_alt) +': ' + self.description_translations[locale]
end
update_dict = {key: key,model_class: self.class.to_s,model_id: self.id,update_user_id: current_user_id,calendar_type_id: calendar_data['type'][key],all_day: true,start: self.send(key),end: self.send(key),title_translations: title_tp,note_translations: note_tp,module_key: 'venue_management',model_page_id: calendar_data['page_id'][key]}
if key_id.nil?
self.calendar_dict[key] = Event.create(update_dict.merge(create_user_id: current_user_id)).id
else
calendar = Event.find(key_id) rescue nil
if calendar.nil?
self.calendar_dict[key] = Event.create(update_dict.merge(create_user_id: current_user_id)).id
else
calendar.update_attributes(update_dict)
end
end
end
end
self.venue_management_emails.each do |venue_mail|
email = venue_mail.email
if !email.nil?
email.destroy
end
end
self.venue_management_emails = []
manager_emails = venue_main.manager_emails
self.fields.values.each do|v|
if v.type==Date || v.type==DateTime
if self.send(v.name+'_reminder')
title = venue_main.title + ' ' + self.description.to_s + ' ' + get_trans(v.name)
send_time = self.send(v.name)-self.send(v.name+'_reminder_day').day rescue nil
if !send_time.nil?
new_email = Email.create(mail_to: manager_emails,
module_app_key:"venue_management",
template:"email/reminder_email.html.erb",
mail_sentdate: send_time,
mail_subject: title,
template_data:{'title'=>title,'send_time'=>self.send(v.name),'locale'=>I18n.locale})
self.venue_management_emails << VenueManagementEmail.new(email_id: new_email.id)
end
end
end
end
end
before_destroy do
calendar_keys = Array(self.calendar_data['key'])
calendar_keys.each do |key|
key_id = self.calendar_id(key)
event = Event.find(key_id) rescue nil
event.destroy
end
end
def get_trans(field)
class_name = self.class.to_s
if class_name == 'VenueManagementMain'
I18n.t("venue_management.#{field}")
else
class_name = class_name.underscore.gsub('venue_management','vm')
I18n.t("#{class_name}.#{field}")
end
end
end