module EducodeSales module ApplicationHelper def current?(css, path) if path.is_a?(Array) ? (current_page?(path.first) || request.path == path.last) : current_page?(path) css else ' ' end end def url_to_avatar(source) return "" if source.blank? if File.exist?(disk_filename(source&.class, source&.id)) && File.file?(disk_filename(source&.class, source&.id)) ctime = File.ctime(disk_filename(source&.class, source&.id)).to_i if source.class.to_s == 'User' File.join(relative_path, ["#{source&.class}", "#{source&.id}"]) + "?t=#{ctime}" else File.join("images/avatars", ["#{source&.class}", "#{source&.id}"]) + "?t=#{ctime}" end elsif source.class.to_s == 'User' str = source.user_extension.try(:gender).to_i == 0 ? "b" : "g" File.join(relative_path, "#{source.class}", str) elsif source.class.to_s == 'Subject' File.join("images","educoder", "index", "subject", "subject#{rand(19)}.jpg") elsif source.class.to_s == 'Shixun' File.join("images","educoder", "index", "shixun", "shixun#{rand(23)}.jpg") end end def disk_filename(source_type,source_id,image_file=nil) File.join(storage_path, "#{source_type}", "#{source_id}") end def storage_path File.join(Rails.root, "public", "images", relative_path) end def relative_path "avatars" end def base_url url = Rails.application.config_for(:configuration)['wechat_pay']['callback_url'] if url.include?("https://data.educoder") "https://www.educoder.net" elsif url.include?("'https://pre-data.educoder") "https://pre.educoder.net" else "https://test.educoder.net" end end # 完成率completion_rate def completion_rate(setting,progress) if setting.to_i == 0 || progress.to_i >= setting.to_i '100%' else (progress.to_f/setting.to_f*100).round(2).to_s + "%" end end # 签单金额 得分规则 def signed_amount_score(setting,progress) progress.to_f >= setting.to_f || setting.to_i == 0 ? 40:(progress.to_f/setting.to_f*40).round(2) end # 回款金额 得分规则 def collection_amount_score(setting,progress) (progress.to_i >= setting.to_i) || (setting.to_i == 0) ? 20:(progress.to_f/setting.to_f*20).round(2) end # 拜访量 得分规则 def visits_score(d) d.to_i > 30 ? 20:0 end #新增商机数 得分规则 def add_businesses_score(staff_id,start_time,end_time) if @current_admin.is_admin? @businesses = Business.all else level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level case level when '自己' # Business.joins(Business字段: :表的名称)----> Business表(belongs_to) Business字段关联的表(has_many) Business字段关联的表在关联的表(belongs_to) business_ids = Business.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).pluck(:id) @businesses = Business.where("educode_sales_businesses.staff_id = ? OR educode_sales_businesses.id in (?)", @current_admin.id, business_ids) when '区域' school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id) business_ids = Business.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).pluck(:id) @businesses = Business.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id").where("departments.school_id in (?) OR educode_sales_businesses.staff_id = #{@current_admin.id} OR educode_sales_businesses.id in (?)", school_ids, business_ids) else @businesses = Business.all end end ids_a_b = Common.where(extras: %w[a_class b_class ]).pluck(:id) ids_c_d = Common.where(extras: %w[c_class d_class]).pluck(:id) @businesses_a_b_count = @businesses.joins("JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id").where("educode_sales_follow_ups.clazz_id in (?)",ids_a_b) .where("educode_sales_businesses.staff_id = ?",staff_id) .where("educode_sales_businesses.created_at >= ? and educode_sales_businesses.created_at <= ?", "#{@assessment_year}-#{start_time} 00:00:00".to_date, "#{@assessment_year}-#{end_time} 23:59:00".to_date) @businesses_c_d_count = @businesses.joins("JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id").where("educode_sales_follow_ups.clazz_id in (?)",ids_c_d) .where("educode_sales_businesses.staff_id = ?",staff_id) .where("educode_sales_businesses.created_at >= ? and educode_sales_businesses.created_at <= ?", "#{@assessment_year}-#{start_time} 00:00:00".to_date, "#{@assessment_year}-#{end_time} 23:59:00".to_date) @businesses_a_b_count.count.to_i*10 + @businesses_c_d_count.count.to_i*5 end end end