class HistoryEmail < ActiveRecord::Base # Email history records def self.history_email(user, type , seen, organization , affected_id) create_history(user, type , seen , organization , affected_id) end # Asset Create Email def self.asset_create_email(asset , organizatin , email, add_user) # Find users of organization and onwer and managers member = Member.where("organization_id = ? " , organizatin.id) member.each do |mem| org_user = User.where("id =?",mem.user_id) org_user.each do |user| email_date = date_time_setting(user) if email_date != "immediately" date_time = time_email(email_date) @date_now = Date.today setting_time = UserNotificationSetting.setting_date(user.id) dt = setting_time + date_time if dt == @date_now if user.has_role? 'manager' UserMailer.create_asset( asset, organizatin , email , user , add_user).deliver elsif user.has_role? 'owner' UserMailer.create_asset( asset, organizatin , email , user , add_user).deliver end end end end end end # Asset Update Email def self.asset_update_email(asset , organizatin , email, add_user) # Find users of organization and onwer and managers member = Member.where("organization_id = ? " , organizatin.id) member.each do |mem| org_user = User.where("id =?",mem.user_id) org_user.each do |user| email_date = date_time_setting(user) if email_date != "immediately" date_time = time_email(email_date) @date_now = Date.today setting_time = UserNotificationSetting.setting_date(user.id) dt = setting_time + date_time if dt == @date_now if user.has_role? 'manager' UserMailer.update_asset(asset, organizatin , email , user , add_user).deliver elsif user.has_role? 'owner' UserMailer.update_asset(asset, organizatin , email , user , add_user).deliver end end end end end end # Vendor create email def self.vendor_create_email(vendor , organizatin , email, add_user) # Find users of organization and onwer and managers member = Member.where("organization_id = ? " , organizatin.id) member.each do |mem| org_user = User.where("id =?",mem.user_id) org_user.each do |user| email_date = date_time_setting(user) if email_date != "immediately" date_time = time_email(email_date) @date_now = Date.today setting_time = UserNotificationSetting.setting_date(user.id) dt = setting_time + date_time if dt == @date_now if user.has_role? 'manager' UserMailer.ct_vendor(vendor, email ,organizatin , user , add_user).deliver elsif user.has_role? 'owner' UserMailer.ct_vendor(vendor, email ,organizatin , user , add_user).deliver end end end end end end # Create Ticket Email def self.create_ticket_email(asset, organization ,email , ticket , add_user) # Find users of organization and onwer and managers member = Member.where("organization_id = ? " , organization.id) member.each do |mem| org_user = User.where("id =?",mem.user_id) org_user.each do |user| email_date = date_time_setting(user) if email_date != "immediately" date_time = time_email(email_date) @date_now = Date.today setting_time = UserNotificationSetting.setting_date(user.id) dt = setting_time + date_time if dt == @date_now if user.has_role? 'manager' UserMailer.create_ticket(asset, organization , email , user , ticket, add_user).deliver elsif user.has_role? 'owner' UserMailer.create_ticket(asset, organization , email , user , ticket, add_user).deliver end end end end end end def self.collect_ticket_email(asset , organization ,email , collection , add_user) # Find users of organization and onwer and managers member = Member.where("organization_id = ? " , organization.id) member.each do |mem| org_user = User.where("id =?",mem.user_id) org_user.each do |user| email_date = date_time_setting(user) if email_date != "immediately" date_time = time_email(email_date) @date_now = Date.today setting_time = UserNotificationSetting.setting_date(user.id) dt = setting_time + date_time if dt == @date_now if user.has_role? 'manager' UserMailer.collect_ticket(asset, organization , email , user , collection, add_user).deliver elsif user.has_role? 'owner' UserMailer.collect_ticket(asset, organization , email , user , collection, add_user).deliver end end end end end end # Assignemt asset # (asset, organization , employee , email, user , assignment ,add_user) def self.asset_assignment_email(asset, organization , employee , email, assignment ,add_user) # Find users of organization and onwer and managers member = Member.where("organization_id = ? " , organization.id) member.each do |mem| org_user = User.where("id =?",mem.user_id) org_user.each do |user| email_date = date_time_setting(user) if email_date != "immediately" date_time = time_email(email_date) @date_now = Date.today setting_time = UserNotificationSetting.setting_date(user.id) dt = setting_time + date_time if dt == @date_now if user.has_role? 'manager' UserMailer.asset_assgiment(asset, organization ,employee, email , user , assignment, add_user).deliver elsif user.has_role? 'owner' UserMailer.asset_assgiment(asset, organization ,employee, email , user , assignment, add_user).deliver end end end end end end # Send For repair # #(asset, organizaion , email, user , repair, add_user) def self.asset_repair_email(asset , organization , email , repair, add_user) # Find users of organization and onwer and managers member = Member.where("organization_id = ? " , organization.id) member.each do |mem| org_user = User.where("id =?",mem.user_id) org_user.each do |user| email_date = date_time_setting(user) if email_date != "immediately" date_time = time_email(email_date) @date_now = Date.today setting_time = UserNotificationSetting.setting_date(user.id) dt = setting_time + date_time if dt == @date_now if user.has_role? 'manager' UserMailer.repair_request(asset, organization , email , user , repair, add_user).deliver elsif user.has_role? 'owner' UserMailer.repair_request(asset, organization , email , user , repair, add_user).deliver end end end end end end private def self.create_history(user_id, type , seen , organization , affected_id) HistoryEmail.create(:user_id=> user_id,:email_type=>type, :email_seen => seen , :organization_id =>organization ,:effected_id =>affected_id) end def self.date_time_setting(user) UserNotificationSetting.email_setting(user.id) end def self.time_email(email_date) if email_date == "day" @email_time = 1.day elsif email_date == "week" @email_time = 1.week elsif email_date =="month" @email_time = 1.month end return @email_time end end