require_dependency "educode_sales/application_controller" module EducodeSales class FollowUpsController < ApplicationController def index authorize! :read, Business respond_to do |format| format.html do end format.json do if @current_admin.is_admin? @follow_ups = FollowUp.all else permissions = @current_admin.permissions.pluck(:name) area_business_ids = [] if permissions.include?("专项管理商机") # 按客户类型查看商机下跟进记录 school_tag_ids = EducodeSales::StaffSchoolTag.where(staff_id: @current_admin.id).pluck :school_tag_id school_ids = SchoolTagMiddle.where(school_tag_id: school_tag_ids).pluck :school_id area_business_ids = Business.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id").where("departments.school_id in (?)", school_ids).pluck(:id) end level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level case level when '自己' if permissions.include?("区域管理商机") # 按负责区域查看商机下跟进记录 school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) area_business_ids += Business.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id").where("departments.school_id in (?)", school_ids).pluck(:id) end 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) business_ids = @businesses.pluck(:id) # 指定查看人 follow_up_ids = EducodeSales::WatchFollowUp.where(staff_id: @current_admin.id).pluck(:follow_up_id) @follow_ups = FollowUp.where(business_id: business_ids + area_business_ids).or(FollowUp.where(id: follow_up_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) business_ids = @businesses.pluck(:id) follow_up_ids = EducodeSales::WatchFollowUp.where(staff_id: @current_admin.id).pluck(:follow_up_id) @follow_ups = FollowUp.where(business_id: business_ids + area_business_ids).or(FollowUp.where(id: follow_up_ids)) else @follow_ups = FollowUp.all end end if params[:q].present? && params[:q][:name].present? @follow_ups = @follow_ups.joins(:business).where("educode_sales_businesses.name LIKE ?", "%#{params[:q][:name]}%") end if params[:q].present? && params[:q][:follows_date].present? date = params[:q][:follows_date].split(" - ") @follow_ups = @follow_ups.where("educode_sales_follow_ups.created_at > ? AND educode_sales_follow_ups.created_at < ?", date[0], date[1] + '23:59:59') end if params[:q].present? && params[:q][:property].present? # 客户类型 @follow_ups = @follow_ups.joins(business: [department: [school: :school_tags]]).where("school_tags.id = ?", params[:q][:property]) end if params[:q].present? && params[:q][:staff_id].present? @follow_ups = @follow_ups.where(staff_id: params[:q][:staff_id]) end if params[:q].present? && params[:q][:department].present? departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:q][:department]}%").pluck(:id) @follow_ups = @follow_ups.joins(business: :department).where("departments.id in (?)", departments_ids) end if params[:q].present? && params[:q][:area].present? p = EducodeSales::Common.find(params[:q][:area]).name @follow_ups = @follow_ups.joins(business: :department).joins(" JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) end if params[:q].present? && params[:q][:description].present? @follow_ups = @follow_ups.joins(:business).joins("JOIN educode_sales_businesses ON educode_sales_businesses.id = educode_sales_follow_ups.business_id ").where("educode_sales_follow_ups.description LIKE ?" ,"%#{params[:q][:description]}%") end @follow_ups = @follow_ups.includes(:staff, :clazz, :stage, key_person: :teacher, business: [department: :school]) if params[:sort].present? && params[:sort][:field] @follow_ups = @follow_ups.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @follow_ups = @follow_ups.order("educode_sales_follow_ups.created_at desc") end @follow_ups = @follow_ups.page(params[:page]).per(params[:limit]) end end end def create load_business if @business.last_follow_up follow_up = @business.last_follow_up.dup follow_up.assign_attributes(follow_up_params) else follow_up = @business.follow_ups.build(follow_up_params) end if params[:service_time].present? date = params[:service_time].split(" - ") follow_up.service_start_time = date[0] follow_up.service_end_time = date[1] follow_up.service_time_long = (date[1].to_date - date[0].to_date).to_i end follow_up.staff = @current_admin params[:assign_follow_up].each do |d| follow_up.assign_follow_ups.build(staff_id: d) end follow_up.profit_amount = follow_up.actual_amount - (follow_up.divide_amount.to_i) if follow_up.actual_amount last_follow_up = @business.last_follow_up if @business.clazz.present? follow_up.clazz_changed = @business.clazz_id != follow_up.clazz_id if follow_up.clazz_changed if @business.clazz.extras == 'a_class' @business.state_id = 3 end clazz_changes = "#{@business.clazz.extras.split('_')[0]}-#{follow_up.clazz.extras.split('_')[0]}" business_clazz_change = EducodeSales::BusinessClazzChange.find_or_initialize_by(business_id: @business.id, clazz_changed: clazz_changes) business_clazz_change.save unless business_clazz_change.persisted? end end # 计划投标时间有变化且是当前周的话,添加当周/当月的销售计划 current_week = Time.now.strftime('%W').to_i if follow_up.invitation_at&.strftime('%W').to_i == current_week if last_follow_up && last_follow_up.invitation_at&.strftime('%W').to_i == current_week current_week = nil end else current_week = nil end if follow_up.save if last_follow_up.present? last_follow_up.key_person.each do |d| key_person = d.dup key_person.follow_up_id = follow_up.id key_person.save end last_follow_up.money_plans.each do |d| money = d.dup money.staff = @current_admin money.follow_up_id = follow_up.id money.save end # 合同里签到日期全同步到最新跟进 last_follow_up.contract_date_lists.update_all(follow_up_id: follow_up.id) end @business.update(last_follow_up_id: follow_up.id, clazz_id: follow_up.clazz_id, p_deploy_time: params[:deploy_time]) if current_week.present? staff_manage_ids = @business&.last_follow_up&.assign_follow_ups.present? ? @business.last_follow_up.assign_follow_ups.pluck(:staff_id) : [@business.staff_id] common_id = EducodeSales::Common.find_by(clazz: '计划类型', name: '中标计划')&.id staff_manage_ids.each do |staff_id| EducodeSales::SalePlan.create(month: Time.now.beginning_of_month, weekly: current_week, content: "提醒:请补充中标计划内容!", business: @business, staff_id: staff_id, finish_rate: '0', common_id: common_id ) EducodeSales::SalePlan.create(month: Time.now.beginning_of_month, content: "提醒:请补充中标计划内容!", business: @business, staff_id: staff_id, finish_rate: '0', common_id: common_id ) end end # 更新对应的计划完成度 if follow_up.bidded_date.present? staff_manage_ids = @business&.last_follow_up&.assign_follow_ups.present? ? @business.last_follow_up.assign_follow_ups.pluck(:staff_id) : [@business.staff_id] common_id = EducodeSales::Common.find_by(clazz: '计划类型', name: '中标计划')&.id if follow_up.bidded_date.strftime('%W') == Time.now.strftime('%W') EducodeSales::SalePlan.where(month: Time.now.beginning_of_month, weekly: Time.now.strftime('%W').to_i, business: @business, staff_id: staff_manage_ids, finish_rate: 0, common_id: common_id).update_all(finish_rate: 100) end if follow_up.bidded_date.strftime('%Y-%m') == Time.now.strftime('%Y-%m') EducodeSales::SalePlan.where(month: Time.now.beginning_of_month, weekly: nil, business: @business, staff_id: staff_manage_ids, finish_rate: 0, common_id: common_id).update_all(finish_rate: 100) end end # 增加o商机编号 add_business_number update_department render_success else render_failure follow_up end end def destroy follow_up = FollowUp.find(params[:id]) business = follow_up.business if follow_up.soft_destroy(@current_admin.id) if follow_up.id == business.last_follow_up_id if business.follow_ups.last&.clazz_id business.clazz_id = business.follow_ups.last&.clazz_id end business.update(last_follow_up: business.follow_ups.last, return_money: MoneyPlan.where(clazz: '实际回款', follow_up_id: business.follow_ups.last&.id).sum(:amount)) # 合同里签到日期全同步到最新跟进 follow_up.contract_date_lists.update_all(follow_up_id: business.follow_ups.last.id) if business.follow_ups.last.present? end render_success else render_failure follow_up end end def update follow_up = FollowUp.find(params[:id]) follow_up.assign_attributes(follow_up_params) if params[:service_time].present? date = params[:service_time].split(" - ") follow_up.service_start_time = date[0] follow_up.service_end_time = date[1] follow_up.service_time_long = (date[1].to_date - date[0].to_date).to_i end assign_follow_ups = [] params[:assign_follow_up].each do |d| assign_follow_ups << follow_up.assign_follow_ups.find_or_initialize_by(staff_id: d) end follow_up.assign_follow_ups = assign_follow_ups follow_up.profit_amount = follow_up.actual_amount - (follow_up.divide_amount.to_i) * 0.01 if follow_up.actual_amount if follow_up.save # 增加o类商机 @business = follow_up.business @business.update(clazz_id: follow_up.clazz_id, p_deploy_time: params[:deploy_time]) add_business_number update_department render_success else render_failure follow_up end end def teachers follow_up = FollowUp.find_by(id: params[:id]) if follow_up.present? @teachers = follow_up.key_person.includes(:teacher).page(params[:page]).per(params[:limit]) else @teachers = FollowUp.none end end def money_plans follow_up = FollowUp.find_by(id: params[:id]) @money_plans = follow_up.money_plans.page(params[:page]).per(params[:limit]) end def add_money follow_up = FollowUp.find(params[:id]) money_plan = follow_up.money_plans.build(date_at: params[:date_at], amount: params[:amount], clazz: params[:clazz]) money_plan.staff = @current_admin if money_plan.save render_success else render_failure money_plan end end def update_money follow_up = FollowUp.find_by(id: params[:id]) money_plan = follow_up.money_plans.find(params[:plan_id]) if money_plan.update(date_at: params[:date_at], amount: params[:amount], clazz: params[:clazz]) render_success else render_failure money_plan end end def delete_money follow_up = FollowUp.find_by(id: params[:id]) money_plan = follow_up.money_plans.find(params[:plan_id]) if money_plan.destroy render_success else render_failure money_plan end end def add_keys follow_up = FollowUp.find(params[:id]) if params[:name].blank? return render_failure '请从平台里选择教师' end names = params[:name].split("-") if names[0] == 't' # 从教师列表里选择的用户 teacher = Teacher.find(names[1]) key_peprson = KeyPerson.new(teacher_params) key_peprson.name = teacher.name key_peprson.teacher = teacher elsif names[0] == 'u' # 从头歌平台选择的用户 user = User.find(names[1]) key_peprson = KeyPerson.new(teacher_params) teacher = EducodeSales::Teacher.find_or_initialize_by(name: user.real_name, department_id: user.department_id, user_id: user.id) teacher.staff = @current_admin teacher.professional_title = params[:professional_title] teacher.job = params[:job] teacher.is_key = true teacher.save key_peprson.name = teacher.name key_peprson.teacher_id = teacher.id else key_peprson = KeyPerson.new(teacher_params) end follow_up.key_person << key_peprson if follow_up.save! render_success else render_failure follow_up end rescue ActiveRecord::RecordInvalid => e if e.message.include?("Teacher已经被使用") || e.message.include?('Key person是无效的') render_failure '该老师已在关键人列表中' else render_failure e.message end end def update_advise followup = FollowUp.find(params[:id]) followup.update(advise: params[:content]) render_success end def comment followup = FollowUp.find(params[:id]) followup.update(comment: params[:comment]) render_success end def file gon.folder = edu_setting('attachment_folder') + '/' render layout: false end def upload_file render layout: false end def add_tag followup = FollowUp.find(params[:id]) gon.tags = [] tags = Tag.all.pluck(:name) selecte_tags = followup.follow_up_tags.includes(:tag).map { |d| d.tag.name } tags.each do |d| gon.tags << {name: d, value: d, selected: selecte_tags.include?(d)} end render layout: false end def update_tags follow_up = FollowUp.find(params[:id]) tags = [] tags_name = [] params[:tags].split(",").uniq.each do |d| tag = Tag.find_or_initialize_by(name: d) do |d| d.staff = @current_admin end tags_name << tag.name tag.save unless tag.persisted? t = FollowUpTag.find_or_initialize_by(tag_id: tag.id, follow_up_id: follow_up.id) t.staff = @current_admin tags << t end follow_up.follow_up_tags = tags follow_up.save render json: {success: true, tags: tags_name} end def add_assign follow_up = FollowUp.find(params[:id]) staff_ids = follow_up.watch_follow_ups.pluck(:staff_id) common = Common.find_by(clazz: 'staff_type', name: '销售') gon.assign_watch= Staff.joins(:user).where.not(job_type: common.id).where.not(role_id: 11).map { |d| {name: d.user.real_name, value: d.id, selected: staff_ids.include?(d.id)} } render layout: false end def update_assign follow_up = FollowUp.find(params[:id]) staffs = [] params[:staff_ids].split(",").each do |staff_id| staffs << WatchFollowUp.find_or_initialize_by(staff_id: staff_id, follow_up_id: follow_up.id) end follow_up.watch_follow_ups = staffs follow_up.save render json: {success: true, tags: staffs.map { |d| d.staff.user.real_name }} end private def edu_setting name EduSetting.get(name) end def load_business @business = Business.find(params[:business_id]) end # o类商机增加编号 def add_business_number # test数据库中 o类商机id=74 pre与线上o类商机=77 if ( @business.number.blank?) && params[:clazz_id].to_i == Common.find_by(extras: "o_class").id totual_count = EducodeSales::Business.unscoped.where("number like :data",data: "#{Time.now.year}%").count + 1 + Time.now.year*1000.to_i @business_number_record = BusinessNumberRecord.find_or_create_by(id: 1) while @business_number_record.value.include?(totual_count) || Business.pluck(:number).include?(totual_count.to_s) totual_count += 1 end @business_number_record.update(value: @business_number_record.value | [ totual_count ]) @business.update(number: totual_count.to_s) end end def follow_up_params params.permit(:clazz_id, :stage_id, :invitation_at, :reception_at, :total_amount, :actual_amount, :divide_amount, :divide_rate, :budget_amount, :description, :advise, :place_id, :bidded_date, :signed_date, :year, :o_business_deployment, :deploy_time, :rival, :plan_a_date, :plan_a_money, :invitation_money, :plan_return_date, :plan_return_money) end # 更新部门和部门下老师用户的状态 def update_department clazz = %w[已中标 已签单 已验收 回款中 服务中] name = @business.last_follow_up&.stage&.name Rails.logger.debug("调试======#{clazz.include?(name)}") if clazz.include?(name) department = @business.department if department department.level = 2 department.save! UserExtension.where(department_id:@business.department_id,identity:0).update_all(level:2) end end end def teacher_params params.permit(:professional_title, :job, :attitude_id, :sex, :birth_date, :remark, :tel, :name) end end end