require_dependency "educode_sales/application_controller" module EducodeSales class BusinessesController < ApplicationController skip_before_action :verify_authenticity_token,only: [:following] def index authorize! :read, Business respond_to do |format| format.html do common = Common.find_by(clazz: 'staff_type', name: '销售') @staffs = Staff.joins(:user).where(job_type: common.id).where.not(role_id: 11).map { |d| [d.user.real_name, d.id]} gon.staffs = Staff.joins(:user).where(job_type: common.id).where.not(role_id: 11).map { |d| {name: d.user.real_name, value: d.id } } # @more = can?(:create, EducodeSales::SalePlan) || can?(:update, EducodeSales::Business) || can?(:destroy, EducodeSales::Business) || can?(:show_file, EducodeSales::Business) @more = true gon.menus = [] gon.assign_menus = [] gon.export_menus = [] gon.place = params[:place_id].present? ? [{ value: params[:place_id], name: EducodeSales::Place.find(params[:place_id]).name }] : [] bussiness_type = [] if params[:type] # 项目类型 bussiness_type = EducodeSales::Business.include_types(params[:type]) || [] end business_step = [] if params[:step] # 项目阶段 business_step = EducodeSales::Business.include_steps(params[:step]) || [] end if params[:business_year].present? && params[:business_year] != '全部' gon.business_year = params[:business_year] end if params[:bidded_date_year].present? && params[:bidded_date_year] != '全部' if params[:step] == '应收款' gon.bidded_date = "2015-01-01" + " - " + Time.new(params[:bidded_date_year]).end_of_year.strftime("%Y-%m-%d") else gon.bidded_date = Time.new(params[:bidded_date_year]).strftime("%Y-%m-%d") + " - " + Time.new(params[:bidded_date_year]).end_of_year.strftime("%Y-%m-%d") end end if params[:signed_date_year].present? && params[:signed_date_year] != '全部' gon.signed_date = Time.new(params[:signed_date_year]).strftime("%Y-%m-%d") + " - " + Time.new(params[:signed_date_year]).end_of_year.strftime("%Y-%m-%d") end if params[:date_at].present? && params[:date_at] != '全部' # 回款时间 gon.date_at = Time.new(params[:date_at]).strftime("%Y-%m-%d") + " - " + Time.new(params[:date_at]).end_of_year.strftime("%Y-%m-%d") end gon.business_type = Common.where(clazz: 'business_type').map do |d| {value: d.id, name: d.name, selected: bussiness_type.include?(d.extras)} end # gon.business_step = Common.where(clazz: 'business_step', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).order("position").map do |d| # {value: d.id, name: d.name, selected: business_step.include?(d.name)} # end gon.business_step = Common.where(clazz: 'business_step').order("position").map do |d| {value: d.id, name: d.name, selected: business_step.include?(d.name)} end if can?(:create, EducodeSales::SalePlan) # gon.menus << { title: '添加团队建议', event: 'suggest' } gon.menus << { title: '添加周计划', event: 'week' } gon.menus << { title: '添加月计划', event: 'month' } gon.menus << { title: '添加年计划', event: 'year' } end if can?(:assign, EducodeSales::SalePlan) gon.assign_menus << { title: '指派周计划', event: 'week' } gon.assign_menus << { title: '指派月计划', event: 'month' } gon.assign_menus << { title: '指派年计划', event: 'year' } end gon.menus << { title: '添加回款计划', event: 'money_plan' } gon.assign_menus << { title: '添加回款计划', event: 'money_plan' } gon.menus << { title: '关注', event: 'following' } gon.menus << { title: '跟进时间线', event: 'time_line' } gon.assign_menus << { title: '关注', event: 'following' } gon.assign_menus << { title: '跟进时间线', event: 'time_line' } if can?(:show_file, EducodeSales::Business) gon.menus << { title: '附件管理', event: 'file' } gon.assign_menus << { title: '附件管理', event: 'file' } end if can?(:update, EducodeSales::Business) # gon.menus << { title: '编辑', event: 'edit' } end if can?(:destroy, EducodeSales::Business) gon.menus << { title: '删除', event: 'delete' } gon.assign_menus << { title: '删除', event: 'delete' } end if can?(:export_business, EducodeSales::Business) gon.export_menus << { title: '导出到Csv文件', event: 'export_csv' } gon.export_menus << { title: '导出到Excel文件', event: 'export_excel' } end gon.export_menus << { title: '导出记录', event: 'export_records' } filter = Filter.find_or_create_by(staff_id: @current_admin.id, clazz: "businesses_list") if filter.extras.present? if filter.extras['area'].nil? filter.extras['area'] = 1 filter.extras['property'] = 1 filter.extras['sale_way'] = 1 end filter.save gon.filter = filter.extras else gon.filter = {'area': 1, 'property': 1, 'sale_way': 1} end end format.json do if params[:q] && (params[:q][:clazz] == 'area' || params[:q][:clazz] == 'special' || params[:q][:clazz] == 'delete_list' || params[:q][:clazz] == 'plan_year') if params[:q][:clazz] == 'special' school_ids = [] # 专项商机 school_tag_ids = @current_admin.staff_school_tags.pluck(:school_tag_id) school_ids = SchoolTagMiddle.where(school_tag_id: school_tag_ids).pluck(:school_id) @businesses = Business.joins("JOIN departments AS dp ON educode_sales_businesses.department_id = dp.id").where("dp.school_id in (?)", school_ids) elsif params[:q][:clazz] == 'area' school_ids = [] # 区域管理商机 # 排查看区域商机,需要排除掉其它人员手上的监管学校 other_staff_school_id = EducodeSales::StaffSchool.where.not(staff_id: @current_admin.id).where("school_id IN (SELECT school_id FROM educode_sales_staff_schools WHERE staff_id = #{@current_admin.id}) IS NOT TRUE").distinct.pluck :school_id school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id) - other_staff_school_id @businesses = Business.joins("JOIN departments AS dp ON educode_sales_businesses.department_id = dp.id").where("dp.school_id in (?)", school_ids) elsif params[:q][:clazz] == 'delete_list' @businesses = Business.unscoped.where(state_id: [1, 3]) elsif params[:q][:clazz] == 'plan_year' @businesses = Business.joins(:last_follow_up).where("educode_sales_follow_ups.plan_a_date > :year OR educode_sales_follow_ups.plan_return_date > :year OR educode_sales_follow_ups.invitation_at > :year", year: Time.now.beginning_of_year) end else if @current_admin.is_admin? @businesses = Business 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) when '区域' # 查看区域商机,需要排除掉其它人员手上的监管学校 other_staff_school_id = EducodeSales::StaffSchool.where.not(staff_id: @current_admin.id).where("school_id IN (SELECT school_id FROM educode_sales_staff_schools WHERE staff_id = #{@current_admin.id}) IS NOT TRUE").distinct.pluck :school_id school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id) - other_staff_school_id if can?(:special, EducodeSales::Business) # 专项商机 school_tag_ids = @current_admin.staff_school_tags.pluck(:school_tag_id) tag_school_ids = SchoolTagMiddle.where(school_tag_id: school_tag_ids).pluck(:school_id) school_ids += tag_school_ids 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.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 end end end if params[:q].present? && params[:q][:name].present? @year = params[:q][:name].split("-")[1].present? ? params[:q][:name].split("-")[1] : '' end # @budget_stage_ids = Common.where(clazz: '商机阶段', name: ['初步接洽', '准备方案','已交方案', '已立项']).pluck(:id) if params[:q] && params[:q][:signed_date].present? date = params[:q][:signed_date].split(" - ") @businesses = @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.signed_date >= ? AND educode_sales_follow_ups.signed_date <= ?", date[0], date[1]) end if params[:q]&& params[:q][:date_at].present? date = params[:q][:date_at].split(" - ") x_id = Common.find_by(extras: EducodeSales::Common::XTYPE)&.id @businesses = @businesses.joins(follow_ups: :money_plans).where("educode_sales_follow_ups.clazz_id != ?", x_id).where.not("educode_sales_money_plans.clazz!= 1").where("educode_sales_money_plans.date_at >= ? AND educode_sales_money_plans.date_at <= ? ", date[0] + " 00:00:00", date[1] + " 23:59:59").distinct end if params[:q] && params[:q][:bidded_date].present? date = params[:q][:bidded_date].split(" - ") @businesses = @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.bidded_date >= ? AND educode_sales_follow_ups.bidded_date <= ?", date[0], date[1]) end if params[:q].present? && params[:q][:all].present? @businesses = @businesses end if params[:q].present? && params[:q][:name].present? @businesses = @businesses.where("educode_sales_businesses.name like ?", "%#{params[:q][:name]}%") end if params[:q].present? && params[:q][:department].present? if params[:q][:department].include?("---") # 从客户管理进来按部门显示商机 school_name, department = params[:q][:department].split("---") if department departments_ids = Department.joins(:school).where("schools.name = ? AND departments.name = ?", school_name, department).pluck(:id) @businesses = @businesses.joins(:department).where("departments.id in (?)", departments_ids) else school = School.find_by(name: school_name) @businesses = @businesses.where("educode_sales_businesses.school_id = ?", school&.id) end else departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:q][:department]}%").pluck(:id) @businesses = @businesses.joins(:department).where("departments.id in (?)", departments_ids) end end if params[:q].present? && params[:q][:staff_id].present? part_a_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is null AND educode_sales_businesses.staff_id = ?",params[:q][:staff_id]).ids part_b_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is not null").joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.staff_id = ?", params[:q][:staff_id]).ids business_ids = part_a_ids + part_b_ids if params[:q][:clazz] == 'delete_list' @businesses = EducodeSales::Business.unscoped.where(id:business_ids).where(state_id: [1,3]) else @businesses = EducodeSales::Business.where(id:business_ids) end end if params[:q].present? && params[:q][:business_type].present? @businesses = @businesses.where("educode_sales_businesses.clazz_id in (?)", params[:q][:business_type].split(",").map(&:to_i)) end if params[:q].present? && params[:q][:contract].present? contract_ids = Common.where(clazz: '商机阶段', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).pluck(:id) @businesses = @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.stage_id in (?)", contract_ids) end if params[:q].present? && params[:q][:business_step].present? @businesses = @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.stage_id in (?)", params[:q][:business_step].split(",").map(&:to_i)) end if params[:q].present? && params[:q][:place_id].present? @businesses = @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.place_id = ?", params[:q][:place_id]) end if params[:q].present? && params[:q][:business_year].present? @businesses = @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.year = ?", params[:q][:business_year]) end if params[:q].present? && params[:q][:year].present? @businesses = @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.year <= ?", params[:q][:year]) end if params[:q].present? && params[:q][:o_business_deployment].present? @businesses = @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.o_business_deployment = ?", params[:q][:o_business_deployment]) end if params[:q].present? && params[:q][:area].present? p = EducodeSales::Common.find(params[:q][:area]).name if @current_admin.is_admin? @businesses = @businesses.joins(" JOIN departments ON educode_sales_businesses.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) else level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level if level == "区域" @businesses = @businesses.joins(" JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) else @businesses = @businesses.joins(" JOIN departments ON educode_sales_businesses.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) end end end if params[:q].present? && params[:q][:date].present? date = params[:q][:date].split(" - ") @businesses = @businesses.where("educode_sales_businesses.created_at > ? AND educode_sales_businesses.created_at < ?", date[0], date[1] + '23:59:59') end if params[:q].present? && params[:q][:invitation_at].present? date = params[:q][:invitation_at].split(" - ") @businesses = @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.invitation_at > ? AND educode_sales_follow_ups.invitation_at < ?", date[0] + '00:00:00', date[1] + '23:59:59') end # 根据编号获取商机 if params[:q].present? && params[:q][:number].present? @businesses = @businesses.where("number like :data", data: "%#{params[:q][:number].strip}%") end if params[:q].present? && params[:q][:clazz].present? # 我的关注 if params[:q][:clazz] == 'followes' my_ids = BusinessRelationShip.where(user_id:@current_admin.user_id).pluck(:business_id) @businesses = @businesses.where(id: my_ids) elsif params[:q][:clazz] == 'me' ids = AssignFollowUp.joins("JOIN educode_sales_follow_ups ON educode_sales_follow_ups.id = educode_sales_assign_follow_ups.follow_up_id").where(staff_id: @current_admin.id).pluck(:business_id) @businesses = @businesses.where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", ids, @current_admin.id) end end #商机来源 if params[:q].present? && params[:q][:source_way].present? if params[:q].present? && params[:q][:source_name].present? if ["会议活动", "渠道代理", "客户"].include?(params[:q][:source_way]) if params[:q][:source_way] == '会议活动' sourcable_id = EducodeSales::Activity.where("name like ?", "%#{params[:q][:source_name]}%").limit(20).pluck(:id) elsif params[:q][:source_way] == '渠道代理' sourcable_id = EducodeSales::Place.where("name like ?", "%#{params[:q][:source_name]}%").limit(20).pluck(:id) elsif params[:q][:source_way] == '客户' sourcable_id = EducodeSales::Department.where("name like ?", "%#{params[:q][:source_name]}%").limit(20).pluck(:id) end if sourcable_id.present? @businesses = @businesses.where(source_way: params[:q][:source_way], sourcable_id: sourcable_id) else @businesses = @businesses.none end else @businesses = @businesses.where(source_way: params[:q][:source_way]).where("sourcable_type like ?", "%#{params[:q][:source_name]}%") end else @businesses = @businesses.where(source_way: params[:q][:source_way]) end end # 商机变化 if params[:q][:clazz_changes].present? clazz_changes = EducodeSales::BusinessClazzChange.clazz_changes_value[params[:q][:clazz_changes].to_s] @businesses = @businesses.joins("JOIN educode_sales_business_clazz_changes ON educode_sales_business_clazz_changes.business_id = educode_sales_businesses.id").where("educode_sales_business_clazz_changes.clazz_changed in (?)", clazz_changes) end if params[:q].present? && params[:q][:property].present? # 客户类型 @businesses = @businesses.joins(department: [school: :school_tags]).where("school_tags.id = ?", params[:q][:property]) # @businesses = @businesses.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id # JOIN schools ON departments.school_id = schools.id").where("schools.school_property_id = ?", params[:q][:property]) end if params[:q].present? && params[:q][:staff_manages].present? # 销售经理 @businesses = @businesses.joins("LEFT JOIN educode_sales_follow_ups AS last_follow_up ON educode_sales_businesses.last_follow_up_id = last_follow_up.id AND last_follow_up.deleted_at IS NULL LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = last_follow_up.id"). where("(educode_sales_assign_follow_ups.id IS NOT NULL AND educode_sales_assign_follow_ups.staff_id = ?) OR (educode_sales_assign_follow_ups.id IS NULL AND educode_sales_businesses.staff_id = ?)", params[:q][:staff_manages], params[:q][:staff_manages]) end @business_count = @businesses.distinct.count if params[:sort].present? && params[:sort][:field] if params[:sort][:field] == "service_end_time" @businesses = @businesses.order("service_time_long #{params[:sort][:order]}") else @businesses = @businesses.order("#{params[:sort][:field]} #{params[:sort][:order]}") end else @businesses = @businesses.order("educode_sales_businesses.created_at desc") end # mysql -select watch_sql = " " unless @current_admin.is_admin? # 保密和战略商机,只有自己,指定人员或管理员才有权限查看 watch_sql = " LEFT JOIN educode_sales_business_watches ON educode_sales_business_watches.business_id = educode_sales_businesses.id LEFT JOIN educode_sales_business_levels ON educode_sales_business_levels.business_id = educode_sales_businesses.id" @businesses = @businesses.where("educode_sales_business_levels.id IS NULL OR (educode_sales_business_levels.id IS NOT NULL AND educode_sales_businesses.staff_id = #{@current_admin.id}) OR educode_sales_business_levels.level_id NOT IN (1,2) OR (educode_sales_business_levels.level_id IN (1, 2) AND educode_sales_business_watches.staff_id = #{@current_admin.id})") end if params[:q][:level_id].present? if @current_admin.is_admin? watch_sql = " JOIN educode_sales_business_levels ON educode_sales_business_levels.business_id = educode_sales_businesses.id" @businesses = @businesses.where("educode_sales_business_levels.level_id = ?", params[:q][:level_id]) else watch_sql = " JOIN educode_sales_business_watches ON educode_sales_business_watches.business_id = educode_sales_businesses.id JOIN educode_sales_business_levels ON educode_sales_business_levels.business_id = educode_sales_businesses.id AND educode_sales_business_levels.level_id = #{params[:q][:level_id].to_i}" @businesses = @businesses.where("(educode_sales_businesses.staff_id = #{@current_admin.id}) OR educode_sales_business_levels.level_id NOT IN (1,2) OR (educode_sales_business_levels.level_id IN (1, 2) AND educode_sales_business_watches.staff_id = #{@current_admin.id})" ) end end @businesses = @businesses.select(" distinct educode_sales_businesses.*, last_follow.plan_a_date, last_follow.plan_a_money, last_follow.invitation_money, last_follow.plan_return_date, last_follow.plan_return_money, last_follow.invitation_at, last_follow.budget_amount, last_follow.o_business_deployment, last_follow.service_time_long, last_follow.service_end_time, last_follow.service_start_time, last_follow.reception_at, last_follow.bidded_date, last_follow.signed_date, last_follow.year, last_follow.created_at as latest_time, last_follow.actual_amount, last_follow.divide_rate, last_follow.divide_amount, (last_follow.total_amount - last_follow.actual_amount) as divide_money, (last_follow.actual_amount - educode_sales_businesses.return_money) as wait_return_money, last_follow.total_amount, total_follow_ups.follow_ups_counts ").joins(" LEFT JOIN educode_sales_follow_ups AS last_follow ON educode_sales_businesses.last_follow_up_id = last_follow.id AND last_follow.deleted_at IS NULL LEFT JOIN ( SELECT COUNT(*) AS follow_ups_counts, educode_sales_follow_ups.business_id FROM educode_sales_follow_ups WHERE educode_sales_follow_ups.deleted_at IS NULL GROUP BY educode_sales_follow_ups.business_id ) AS total_follow_ups ON educode_sales_businesses.id = total_follow_ups.business_id #{watch_sql} ").includes(:users, last_follow_up: [:clazz, :assign_follow_ups]).page(params[:page]).per(params[:limit]) end end end def daily_paper @followups = EducodeSales::FollowUp.where(staff_id: @current_admin.id).where("created_at > ?", Time.now.beginning_of_day).order("created_at") ab = EducodeSales::Common.where(extras: [EducodeSales::Common::ATYPE, EducodeSales::Common::BTYPE]).pluck(:id) business = EducodeSales::FollowUp.joins(" JOIN educode_sales_businesses ON educode_sales_businesses.id = educode_sales_follow_ups.business_id JOIN educode_sales_follow_ups AS last_up ON last_up.id = educode_sales_businesses.last_follow_up_id ").where("educode_sales_businesses.staff_id = ? AND educode_sales_follow_ups.staff_id = ? AND last_up.clazz_id IN (?) AND last_up.staff_id = #{@current_admin.id}", @current_admin.id, @current_admin.id, ab) @month = business.where("educode_sales_follow_ups.created_at >= ?", Time.now.beginning_of_month).count("DISTINCT(educode_sales_follow_ups.business_id)") @quarter = business.where("educode_sales_follow_ups.created_at >= ?", Time.now.beginning_of_quarter).count("DISTINCT(educode_sales_follow_ups.business_id)") @year = business.where("educode_sales_follow_ups.created_at >= ?", Time.now.beginning_of_year).count("DISTINCT(educode_sales_follow_ups.business_id)") @activities = EducodeSales::Activity.where(staff_id: @current_admin.id).where("created_at >= ?", Time.now.beginning_of_month).count @teachers = EducodeSales::Activity.joins(:teachers).where(staff_id: @current_admin.id).where("educode_sales_activities.created_at >= ?", Time.now.beginning_of_month).count @schools = EducodeSales::Activity.joins(teachers: :department).where(staff_id: @current_admin.id).where("educode_sales_activities.created_at >= ?", Time.now.beginning_of_month).count("DISTINCT(departments.school_id)") render layout: false end def create department = Department.find(params[:department_id]) if params[:sourcable_type].present? if params[:source_way] == '会议活动' params[:sourcable_type] = 'EducodeSales::Activity' elsif params[:source_way] == '渠道代理' params[:sourcable_type] = 'EducodeSales::Place' elsif params[:source_way] == '客户' params[:sourcable_type] = 'EducodeSales::Department' else params[:sourcable_id] = "" end end if ['会议活动', '渠道代理', '客户'].include?(params[:source_way] ) && params[:sourcable_id].blank? params[:sourcable_type] = "" end if params[:sourcable_type].blank? params[:sourcable_id] = "" end business = @current_admin.businesses.build(name: params[:name], department_id: department.id, source: params[:source], school_id: department.school_id, clazz_id: params[:clazz_id], sourcable_type: params[:sourcable_type], sourcable_id: params[:sourcable_id], source_way: params[:source_way]) if business.save render_success else render_failure business end end def new @clazz = Common.where(clazz: 'business_type').order("position").pluck(:name, :id) render layout: false end def edit @business = Business.find(params[:id]) levels = @business.business_levels.pluck(:level_id) staff_ids = @business.business_watches.pluck(:staff_id) gon.is_secret = levels.include?("保密型") || levels.include?("战略型") gon.levels = EducodeSales::BusinessLevel.level_ids.keys.map { |d| {name: d, value: d, selected: levels.include?(d)}} gon.watche_ids = Staff.all.map { |d| {name: d.user&.real_name, value: d.id, selected: staff_ids.include?(d.id)}} gon.department = {value: @business.department_id, name: "#{@business.department&.school&.name}-#{@business.department&.name}"} gon.value = @business.department_id if ['会议活动', '渠道代理', '客户'].include?(@business.source_way) && @business.sourcable.present? if @business.source_way == '客户' gon.sourcable = [{name: "#{@business.sourcable.school.name} #{@business.sourcable.name}", value: @business.sourcable_id}] else gon.sourcable = [{name: @business.sourcable.name, value: @business.sourcable_id}] end gon.sourcable_id = @business.sourcable_id elsif @business.sourcable_type.present? gon.sourcable = [{value: @business.sourcable_type, name: @business.sourcable_type}] gon.sourcable_id = @business.sourcable_type else gon.sourcable = [] gon.sourcable_id = '' end gon.sourcable_type = @business.sourcable_type gon.source_way = @business.source_way render layout: false end def update business = Business.find(params[:id]) department = Department.find(params[:department_id]) if @current_admin.is_admin? if params[:level_ids] levels = [] params[:level_ids].split(",").each do |d| levels << business.business_levels.find_or_initialize_by(level_id: d) end business.business_levels = levels if params[:watch_ids].blank? || !params[:level_ids].split(",").include?('保密型') && !params[:level_ids].split(",").include?('战略型') business.business_watches.destroy_all else staffs = [] params[:watch_ids].split(",").each do |d| staffs << business.business_watches.find_or_initialize_by(staff_id: d) end business.business_watches = staffs end end end if params[:sourcable_type].present? if params[:source_way] == '会议活动' params[:sourcable_type] = 'EducodeSales::Activity' elsif params[:source_way] == '渠道代理' params[:sourcable_type] = 'EducodeSales::Place' elsif params[:source_way] == '客户' params[:sourcable_type] = 'EducodeSales::Department' else params[:sourcable_id] = "" end end if ['会议活动', '渠道代理', '客户'].include?(params[:source_way] ) && params[:sourcable_id].blank? params[:sourcable_type] = "" end if params[:sourcable_type].blank? params[:sourcable_id] = "" end if business.update(name: params[:name], department_id: department.id, source: params[:source], school_id: department.school_id, sourcable_type: params[:sourcable_type], sourcable_id: params[:sourcable_id], source_way: params[:source_way] ) if params[:merge_business_ids].present? merge_business = Business.where(id: params[:merge_business_ids].split(",")) merge_business.each do |b| b.follow_ups.update_all(origin_business_id: b.id, business_id: business.id) b.update(deleted_at: Time.now) end business.update(follow_ups_count: EducodeSales::FollowUp.where(business_id: business.id).size) end render_success else render_failure business end end def updateMoney business = Business.find(params[:id]) if business.update(return_money: params[:return_money]) render_success else render_failure business end end def destroy business = Business.find(params[:id]) if ['a_class', 'b_class'].include?(business.clazz&.extras) if params[:content].present? business.update(deleted_at: Time.now, state_id: 1, delete_reason: params[:content]) business.sale_plans.update_all(deleted_at: Time.now) business.follow_ups.update_all(deleted_at: Time.now) else render_failure '请输入删除商机原因' end else business.soft_destroy(@current_admin.id) render_success end rescue ActiveRecord::DeleteRestrictionError => e render_failure '该商机已其它关联数据产生,暂不能删除' end def audit business = Business.unscoped.find(params[:id]) if params[:clazz] == 'destroy' if params[:op] == 'pass' business.update(state_id: 2) business.sale_plans.each do |d| EducodeSales::Recycle.create(source: d, deleter_id: @current_admin.id) end EducodeSales::Recycle.create(source: business, deleter_id: @current_admin.id) else business.update(state_id: 0, delete_reason: '', deleted_at: nil) SalePlan.unscoped.where(business_id: business.id).update_all(deleted_at: nil) FollowUp.unscoped.where(business_id: business.id).update_all(deleted_at: nil) end else if params[:op] == 'pass' business.update(state_id: 0) else business.clazz_id = EducodeSales::Common.find_by(extras: 'a_class').id business.update(state_id: 0) end end render_success end def show_keys render layout: false end def add_keys render layout: false end def show_follow load_business respond_to do |format| format.html do unless @current_admin.is_admin? levels = @business.business_levels.pluck(:level_id) staff_ids = @business.business_watches.pluck(:staff_id) if levels.include?("保密型") || levels.include?("战略型") if !staff_ids.include?(@current_admin.id) && @business.staff_id != @current_admin.id && @business.last_follow_up&.staff_id != @current_admin.id return render "no_permission", layout: false end end end render layout: false end format.json do @follow_ups = @business.follow_ups @latest = @follow_ups.order(created_at: :desc).first if params[:field] @follow_ups = @follow_ups.order("#{params[:field]} #{params[:order]}") else @follow_ups = @follow_ups.order("created_at desc") end @follow_ups = @follow_ups.page(params[:page]).per(params[:limit]) end end end def new_follow_record load_business @years = (2015..Time.now.year).to_a.reverse @o_name = EducodeSales::Common.find_by(extras: EducodeSales::Common::OTYPE)&.name @clazz = Common.where(clazz: 'business_type').order("position").pluck(:name, :id) @stages = Common.where(clazz: 'business_step').order("position").pluck(:name, :id) @deployment_type = EducodeSales::FollowUp::BUSINESS_DEPLOYMENT @places = Place.order("created_at desc").pluck(:name, :id) @last_follow_up = @business.last_follow_up if @last_follow_up gon.sales = @last_follow_up.assign_follow_ups.map do |d| {value: d.staff_id, name: d.staff.user.full_name} end gon.place = @last_follow_up.place.present? ? [{ value: @last_follow_up.place&.id, name: @last_follow_up.place&.name }] : [] end render layout: false end def unfinish_plans load_business @plans = @business.sale_plans.where.not(finish_rate: 100).page(params[:page]).per(params[:limit]) end def show_follow_record follow_up_id = params[:follow_up_id] @follow_up = FollowUp.find(follow_up_id) @business = Business.find(@follow_up.business_id) # 保密商机限制查看 unless @current_admin.is_admin? levels = @business.business_levels.pluck(:level_id) staff_ids = @business.business_watches.pluck(:staff_id) if levels.include?("保密型") || levels.include?("战略型") if !staff_ids.include?(@current_admin.id) && @business.staff_id != @current_admin.id && @business.last_follow_up&.staff_id != @current_admin.id return render "no_permission", layout: false end end end id = @follow_up.id @follow_ups = @business.follow_ups ids = @follow_ups.ids size = ids.size ids = ids.reverse index = ids.index(id) EducodeSales::FollowUp::BUSINESS_DEPLOYMENT[1..-1].each do |m| if @follow_up.o_business_deployment == m[1] @show_follow_recored_deployment = m[0] break end end @next_id = (index != size - 1 && size > 1) ? ids[index + 1] : 0 @pre_id = (index != 0 && size > 1) ? ids[index - 1] : 0 render layout: false end def edit_follow_record @years = (2015..Time.now.year).to_a.reverse @o_name = EducodeSales::Common.find_by(extras: EducodeSales::Common::OTYPE)&.name @deployment_type = EducodeSales::FollowUp::BUSINESS_DEPLOYMENT @follow_up = FollowUp.find(params[:follow_up_id]) @clazz = Common.where(clazz: 'business_type').order("position").pluck(:name, :id) @stages = Common.where(clazz: 'business_step').order("position").pluck(:name, :id) @places = Place.order("created_at desc").pluck(:name, :id) gon.sales = @follow_up.assign_follow_ups.map do |d| {value: d.staff_id, name: d.staff.user.full_name} end gon.place = @follow_up.place.present? ? [{ value: @follow_up.place&.id, name: @follow_up.place&.name }] : [value: '', name: ''] render layout: false end def edit_plan render layout: false end def file gon.folder = edu_setting('attachment_folder') + '/' render layout: false end def upload_file render layout: false end def time_line @business = Business.find(params[:business_id]) respond_to do |format| format.html do # 保密商机限制查看 unless @current_admin.is_admin? levels = @business.business_levels.pluck(:level_id) staff_ids = @business.business_watches.pluck(:staff_id) if levels.include?("保密型") || levels.include?("战略型") if !staff_ids.include?(@current_admin.id) && @business.staff_id != @current_admin.id && @business.last_follow_up&.staff_id != @current_admin.id return render "no_permission", layout: false end end end @follow_ups = @business.follow_ups.order(created_at: :desc) @latest = @follow_ups.present? ? @follow_ups[0] : nil render layout: false end end end def export_records respond_to do |format| format.html do render layout: false end format.json do @records = BusinessExportRecord.all.order(id: :desc).page(params[:page]).per(params[:limit]) end end end def add_export_records if params[:type] == 'csv' record = BusinessExportRecord.new(name: '商机数据表.csv', staff_id: @current_admin.id) else params[:type] == 'excel' record = BusinessExportRecord.new(name: '商机数据表.xls', staff_id: @current_admin.id) end if record.save render_success else render_failure business end end def get_export_data respond_to do |format| format.html do end format.json do if @current_admin.is_admin? @businesses = Business 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) when '区域' school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(: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 end end if params[:signed_date].present? date = params[:signed_date].split(" - ") @businesses = @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.signed_date >= ? AND educode_sales_follow_ups.signed_date <= ?", date[0], date[1]) end if params[:date_at].present? date = params[:date_at].split(" - ") x_id = Common.find_by(extras: EducodeSales::Common::XTYPE)&.id @businesses = @businesses.joins(follow_ups: :money_plans).where("educode_sales_follow_ups.clazz_id != ?", x_id).where.not("educode_sales_money_plans.clazz!= 1").where("educode_sales_money_plans.date_at >= ? AND educode_sales_money_plans.date_at <= ? ", date[0] + " 00:00:00", date[1] + " 23:59:59").distinct end if params[:bidded_date].present? date = params[:bidded_date].split(" - ") @businesses = @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.bidded_date >= ? AND educode_sales_follow_ups.bidded_date <= ?", date[0], date[1]) end if params[:business_year].present? @businesses = @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.year = ?", params[:business_year]) end if params[:year].present? @businesses = @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.year <= ?", params[:year]) end if params[:all].present? @businesses = @businesses end if params[:mine].present? ids = AssignFollowUp.where(staff_id: @current_admin.id).pluck(:follow_up_id) ids1 = FollowUp.where(id: ids).pluck(:business_id) @businesses =@businesses.where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", ids1, @current_admin.id) end if params[:is_follow].present? # 关注 my_ids = BusinessRelationShip.where(user_id:@current_admin.user_id).pluck(:business_id) @businesses = @businesses.where(id: my_ids) end @businesses = @businesses if params[:name].present? # && params[:name] != "(销售态势-" + @year + "-已中标商机)" && params[:name] != "(销售态势-" + @year + "-已签单商机)" && params[:name] != "(销售态势-" + @year + "-已回款商机)" && params[:name] != "(销售态势-" + @year + "-现有商机)"&& params[:name] != "(销售态势-" + @year + "-应收款商机)" @businesses = @businesses.where("educode_sales_businesses.name like ?", "%#{params[:name]}%") end if params[:department].present? departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:department]}%").pluck(:id) @businesses = @businesses.joins(:department).where("departments.id in (?)", departments_ids) end if params[:staff_id].present? part_a_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is null AND educode_sales_businesses.staff_id = ?",params[:staff_id]).ids part_b_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is not null").joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.staff_id = ?", params[:staff_id]).ids business_ids = part_a_ids + part_b_ids @businesses = EducodeSales::Business.where(id:business_ids) end if params[:business_type].present? @businesses = @businesses.where("educode_sales_businesses.clazz_id in (?)", params[:business_type].split(",").map(&:to_i)) end if params[:business_step].present? @businesses = @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.stage_id in (?)", params[:business_step].split(",").map(&:to_i)) end if params[:place_id].present? @businesses = @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.place_id = ?", params[:place_id]) end if params[:area].present? p = EducodeSales::Common.find(params[:area]).name if @current_admin.is_admin? @businesses = @businesses.joins(" JOIN departments ON educode_sales_businesses.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) else level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level if level == "区域" @businesses = @businesses.joins(" JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) else @businesses = @businesses.joins(" JOIN departments ON educode_sales_businesses.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) end end end if params[:date].present? date = params[:date].split(" - ") @businesses = @businesses.where("educode_sales_businesses.created_at > ? AND educode_sales_businesses.created_at < ?", date[0], date[1] + '23:59:59') end if params[:invitation_at].present? date = params[:invitation_at].split(" - ") @businesses = @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.invitation_at > ? AND educode_sales_follow_ups.invitation_at < ?", date[0] + '00:00:00', date[1] + '23:59:59') end if params[:sort].present? && params[:sort][:field] @businesses = @businesses.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @businesses = @businesses.order("educode_sales_businesses.created_at desc") end @business_count = @businesses.count @businesses = @businesses.select(" educode_sales_businesses.*, last_follow.invitation_at, last_follow.budget_amount, last_follow.reception_at, last_follow.service_time_long, last_follow.service_end_time, last_follow.service_start_time, last_follow.bidded_date, last_follow.signed_date, last_follow.created_at as latest_time, last_follow.actual_amount, last_follow.divide_rate, last_follow.divide_amount, (last_follow.total_amount - last_follow.actual_amount) as divide_money, (last_follow.actual_amount - educode_sales_businesses.return_money) as wait_return_money, last_follow.total_amount, total_follow_ups.follow_ups_count ").joins(" LEFT JOIN educode_sales_follow_ups AS last_follow ON educode_sales_businesses.last_follow_up_id = last_follow.id AND last_follow.deleted_at IS NULL LEFT JOIN ( SELECT COUNT(*) AS follow_ups_count, educode_sales_follow_ups.business_id FROM educode_sales_follow_ups WHERE educode_sales_follow_ups.deleted_at IS NULL GROUP BY educode_sales_follow_ups.business_id ) AS total_follow_ups ON educode_sales_businesses.id = total_follow_ups.business_id ").includes(last_follow_up: [:clazz, :assign_follow_ups]) end end end #关注接口 def following if brs=follow_business(@current_admin.user_id,params[:business_id]) if brs.delete render json: {code:200,msg:"已取消关注"} else render json: {code:300,msg:"操作失败"} end else brs=BusinessRelationShip.new(user_id:@current_admin.user_id, business_id:params[:business_id]) if brs.save render json: {code:200,msg:"关注成功"} else render json: {code:300,msg:"操作失败"} end end end #我已关注的商机 def following_businesses p=[] BusinessRelationShip.select(:business_id).where(user_id:@current_admin.user_id).to_a.each { |d| p << d.business_id} @businesses=Business.where(id:p) @businesses=@businesses.select(" educode_sales_businesses.*, last_follow.budget_amount, last_follow.invitation_at, last_follow.reception_at, last_follow.service_time_long, last_follow.service_end_time, last_follow.service_start_time, last_follow.bidded_date, last_follow.signed_date, last_follow.created_at as latest_time, last_follow.actual_amount, (last_follow.actual_amount - educode_sales_businesses.return_money) as wait_return_money, last_follow.total_amount").joins(" LEFT JOIN educode_sales_follow_ups AS last_follow ON educode_sales_businesses.last_follow_up_id = last_follow.id ").page(params[:page]).per(params[:limit]) end def followed if follow_business(@current_admin.user_id,params[:business_id]) render json: {msg:'确定取消关注?'} else render json: {msg:'确定关注?'} end end def search @business = Business.where.not(id: params[:id]).order("created_at desc") @business = @business.where("educode_sales_businesses.name like ?", "%#{params[:q]}%").limit(30) if params[:q].present? end def contract authorize! :contract_business, Business respond_to do |format| format.html do common = Common.find_by(clazz: 'staff_type', name: '销售') @staffs = Staff.joins(:user).where(job_type: common.id).where.not(role_id: 11).map { |d| [d.user.real_name, d.id]} gon.staffs = Staff.joins(:user).where(job_type: common.id).where.not(role_id: 11).map { |d| {name: d.user.real_name, value: d.id } } @more = true gon.menus = [] gon.export_menus = [] gon.place = params[:place_id].present? ? [{ value: params[:place_id], name: EducodeSales::Place.find(params[:place_id]).name }] : [] bussiness_type = [] if params[:type] # 项目类型 bussiness_type = EducodeSales::Business.include_types(params[:type]) || [] end business_step = [] if params[:step] # 项目阶段 business_step = EducodeSales::Business.include_steps(params[:step]) || [] end if params[:business_year].present? && params[:business_year] != '全部' gon.business_year = params[:business_year] end if params[:bidded_date_year].present? && params[:bidded_date_year] != '全部' if params[:step] == '应收款' gon.bidded_date = "2015-01-01" + " - " + Time.new(params[:bidded_date_year]).end_of_year.strftime("%Y-%m-%d") else gon.bidded_date = Time.new(params[:bidded_date_year]).strftime("%Y-%m-%d") + " - " + Time.new(params[:bidded_date_year]).end_of_year.strftime("%Y-%m-%d") end end if params[:signed_date_year].present? && params[:signed_date_year] != '全部' gon.signed_date = Time.new(params[:signed_date_year]).strftime("%Y-%m-%d") + " - " + Time.new(params[:signed_date_year]).end_of_year.strftime("%Y-%m-%d") end if params[:date_at].present? && params[:date_at] != '全部' # 回款时间 gon.date_at = Time.new(params[:date_at]).strftime("%Y-%m-%d") + " - " + Time.new(params[:date_at]).end_of_year.strftime("%Y-%m-%d") end gon.business_type = Common.where(clazz: 'business_type').map do |d| {value: d.id, name: d.name, selected: bussiness_type.include?(d.extras)} end gon.business_step = Common.where(clazz: 'business_step', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).order("position").map do |d| {value: d.id, name: d.name, selected: business_step.include?(d.name)} end filter = Filter.find_or_create_by(staff_id: @current_admin.id, clazz: "businesses_list") if filter.extras.present? if filter.extras['area'].nil? filter.extras['area'] = 1 filter.extras['property'] = 1 filter.extras['sale_way'] = 1 end filter.save gon.filter = filter.extras else gon.filter = {'area': 1, 'property': 1, 'sale_way': 1} end end format.json do if @current_admin.is_admin? @businesses = Business 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) when '区域' # 查看区域商机,需要排除掉其它人员手上的监管学校 other_staff_school_id = EducodeSales::StaffSchool.where.not(staff_id: @current_admin.id).where("school_id IN (SELECT school_id FROM educode_sales_staff_schools WHERE staff_id = #{@current_admin.id}) IS NOT TRUE").distinct.pluck :school_id school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id) - other_staff_school_id if can?(:special, EducodeSales::Business) # 专项商机 school_tag_ids = @current_admin.staff_school_tags.pluck(:school_tag_id) tag_school_ids = SchoolTagMiddle.where(school_tag_id: school_tag_ids).pluck(:school_id) school_ids += tag_school_ids 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.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 end end if params[:q].present? && params[:q][:name].present? @year = params[:q][:name].split("-")[1].present? ? params[:q][:name].split("-")[1] : '' end # @budget_stage_ids = Common.where(clazz: '商机阶段', name: ['初步接洽', '准备方案','已交方案', '已立项']).pluck(:id) if params[:q] && params[:q][:signed_date].present? date = params[:q][:signed_date].split(" - ") @businesses = @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.signed_date >= ? AND educode_sales_follow_ups.signed_date <= ?", date[0], date[1]) end if params[:q]&& params[:q][:date_at].present? date = params[:q][:date_at].split(" - ") x_id = Common.find_by(extras: EducodeSales::Common::XTYPE)&.id @businesses = @businesses.joins(follow_ups: :money_plans).where("educode_sales_follow_ups.clazz_id != ?", x_id).where.not("educode_sales_money_plans.clazz!= 1").where("educode_sales_money_plans.date_at >= ? AND educode_sales_money_plans.date_at <= ? ", date[0] + " 00:00:00", date[1] + " 23:59:59").distinct end if params[:q] && params[:q][:bidded_date].present? date = params[:q][:bidded_date].split(" - ") @businesses = @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.bidded_date >= ? AND educode_sales_follow_ups.bidded_date <= ?", date[0], date[1]) end if params[:q].present? && params[:q][:all].present? @businesses = @businesses end if params[:q].present? && params[:q][:name].present? @businesses = @businesses.where("educode_sales_businesses.name like ?", "%#{params[:q][:name]}%") end if params[:q].present? && params[:q][:department].present? departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:q][:department]}%").pluck(:id) @businesses = @businesses.joins(:department).where("departments.id in (?)", departments_ids) end if params[:q].present? && params[:q][:staff_id].present? part_a_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is null AND educode_sales_businesses.staff_id = ?",params[:q][:staff_id]).ids part_b_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is not null").joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.staff_id = ?", params[:q][:staff_id]).ids business_ids = part_a_ids + part_b_ids if params[:q][:clazz] == 'delete_list' @businesses = EducodeSales::Business.unscoped.where(id:business_ids) else @businesses = EducodeSales::Business.where(id:business_ids) end end if params[:q].present? && params[:q][:business_type].present? @businesses = @businesses.where("educode_sales_businesses.clazz_id in (?)", params[:q][:business_type].split(",").map(&:to_i)) end if params[:q].present? && params[:q][:contract].present? contract_ids = Common.where(clazz: '商机阶段', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).pluck(:id) @businesses = @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.stage_id in (?)", contract_ids) end if params[:q].present? && params[:q][:business_step].present? @businesses = @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.stage_id in (?)", params[:q][:business_step].split(",").map(&:to_i)) end if params[:q].present? && params[:q][:place_id].present? @businesses = @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.place_id = ?", params[:q][:place_id]) end if params[:q].present? && params[:q][:business_year].present? @businesses = @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.year = ?", params[:q][:business_year]) end if params[:q].present? && params[:q][:year].present? @businesses = @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.year <= ?", params[:q][:year]) end if params[:q].present? && params[:q][:o_business_deployment].present? @businesses = @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.o_business_deployment = ?", params[:q][:o_business_deployment]) end if params[:q].present? && params[:q][:area].present? p = EducodeSales::Common.find(params[:q][:area]).name if @current_admin.is_admin? @businesses = @businesses.joins(" JOIN departments ON educode_sales_businesses.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) else level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level if level == "区域" @businesses = @businesses.joins(" JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) else @businesses = @businesses.joins(" JOIN departments ON educode_sales_businesses.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) end end end if params[:q].present? && params[:q][:date].present? date = params[:q][:date].split(" - ") @businesses = @businesses.where("educode_sales_businesses.created_at > ? AND educode_sales_businesses.created_at < ?", date[0], date[1] + '23:59:59') end if params[:q].present? && params[:q][:invitation_at].present? date = params[:q][:invitation_at].split(" - ") @businesses = @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.invitation_at > ? AND educode_sales_follow_ups.invitation_at < ?", date[0] + '00:00:00', date[1] + '23:59:59') end # 根据编号获取商机 if params[:q].present? && params[:q][:number].present? @businesses = @businesses.where("number like :data", data: "%#{params[:q][:number].strip}%") end if params[:q].present? && params[:q][:clazz].present? # 我的关注 if params[:q][:clazz] == 'followes' my_ids = BusinessRelationShip.where(user_id:@current_admin.user_id).pluck(:business_id) @businesses = @businesses.where(id: my_ids) elsif params[:q][:clazz] == 'me' ids = AssignFollowUp.joins("JOIN educode_sales_follow_ups ON educode_sales_follow_ups.id = educode_sales_assign_follow_ups.follow_up_id").where(staff_id: @current_admin.id).pluck(:business_id) @businesses = @businesses.where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", ids, @current_admin.id) end end # 商机变化 if params[:q][:clazz_changes].present? clazz_changes = EducodeSales::BusinessClazzChange.clazz_changes_value[params[:q][:clazz_changes].to_s] @businesses = @businesses.joins("JOIN educode_sales_business_clazz_changes ON educode_sales_business_clazz_changes.business_id = educode_sales_businesses.id").where("educode_sales_business_clazz_changes.clazz_changed in (?)", clazz_changes) end if params[:sort].present? && params[:sort][:field] if params[:sort][:field] == "service_end_time" @businesses = @businesses.order("service_time_long #{params[:sort][:order]}") else @businesses = @businesses.order("#{params[:sort][:field]} #{params[:sort][:order]}") end else @businesses = @businesses.order("educode_sales_businesses.created_at desc") end @business_count = @businesses.distinct.count # mysql -select @businesses = @businesses.select(" distinct educode_sales_businesses.*, last_follow.invitation_at, last_follow.budget_amount, last_follow.o_business_deployment, last_follow.service_time_long, last_follow.service_end_time, last_follow.service_start_time, last_follow.reception_at, last_follow.bidded_date, last_follow.signed_date, last_follow.year, last_follow.created_at as latest_time, last_follow.actual_amount, last_follow.divide_rate, last_follow.divide_amount, (last_follow.total_amount - last_follow.actual_amount) as divide_money, (last_follow.actual_amount - educode_sales_businesses.return_money) as wait_return_money, last_follow.total_amount, total_follow_ups.follow_ups_counts ").joins(" LEFT JOIN educode_sales_follow_ups AS last_follow ON educode_sales_businesses.last_follow_up_id = last_follow.id AND last_follow.deleted_at IS NULL LEFT JOIN ( SELECT COUNT(*) AS follow_ups_counts, educode_sales_follow_ups.business_id FROM educode_sales_follow_ups WHERE educode_sales_follow_ups.deleted_at IS NULL GROUP BY educode_sales_follow_ups.business_id ) AS total_follow_ups ON educode_sales_businesses.id = total_follow_ups.business_id ").includes(:users, last_follow_up: [:clazz, :assign_follow_ups]).page(params[:page]).per(params[:limit]) end end end def update_advise business = Business.find(params[:id]) if business.last_follow_up.present? business.last_follow_up.update(advise: params[:content]) render_success else render_failure '无跟进动态,暂不能添加建议' end end def key_person respond_to do |format| format.html do render layout: false end format.json do @teachers = EducodeSales::KeyPerson.where(follow_up_id: params[:follow_up_id]).page(params[:page]).per(params[:limit]) end end end private def load_business @business = Business.find(params[:id]) end def follow_up_params params.permit(:clazz_id, :stage_id, :invitation_at, :reception_at, :total_amount, :actual_amount, :divide_amount, :description, :advise, :place_id) end def edu_setting name EduSetting.get(name) end def follow_business(user_id,business_id) BusinessRelationShip.where(user_id:user_id,business_id:business_id).first end end end