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 level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level case level when '自己' 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_ups = FollowUp.where(business_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) business_ids = @businesses.pluck(:id) @follow_ups = FollowUp.where(business_id: business_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][: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 follow_up = @business.follow_ups.build(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 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 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 end @business.update(last_follow_up_id: follow_up.id, clazz_id: follow_up.clazz_id) # 增加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)) 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) 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.teacher = teacher else # 从头歌平台选择的用户 user = User.find(names[1]) teacher = Teacher.new(staff: @current_admin, professional_title: params[:professional_title], job: params[:job], user_id: user.id, is_key: true, department_id: user.department_id, name: user.real_name ) teacher.save key_peprson = KeyPerson.new(teacher_params) teacher_id = EducodeSales::Teacher.find_by(name: user.real_name, department_id: user.department_id).id key_peprson.teacher_id = teacher_id 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 private 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) 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) end end end