require_dependency "educode_sales/application_controller" module EducodeSales class BusinessesController < ApplicationController 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).map { |d| [d.user.real_name, d.id]} @more = can?(:create, EducodeSales::SalePlan) || can?(:update, EducodeSales::Business) || can?(:destroy, EducodeSales::Business) gon.menus = [] if can?(:create, EducodeSales::SalePlan) gon.menus << { title: '添加周计划', event: 'week' } gon.menus << { title: '添加月计划', event: 'month' } end if can?(:update, EducodeSales::Business) gon.menus << { title: '编辑', event: 'edit' } end if can?(:destroy, EducodeSales::Business) gon.menus << { title: '删除', event: 'delete' } 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 '自己' @businesses = Business.where(staff_id: @current_admin.id) when '区域' school_ids = School.where(province: @current_admin.areas.pluck(:name)).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}", school_ids) else @businesses = Business end end @businesses = @businesses 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? @businesses = @businesses.where(staff_id: params[:q][:staff_id]) end if params[:q].present? && params[:q][:business_type].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.clazz_id = ?", params[:q][:business_type]) 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 = ?", params[:q][:business_step]) 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][:area].present? p = EducodeSales::Common.find(params[:q][:area]).name @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 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]) 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.reception_at, 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 end end def create department = Department.find(params[:department_id]) business = @current_admin.businesses.build(name: params[:name], department_id: department.id, source: params[:source]) if business.save render_success else render_failure business end end def new render layout: false end def edit @business = Business.find(params[:id]) gon.department = {value: @business.department_id, name: "#{@business.department.school.name}-#{@business.department.name}"} gon.value = @business.department_id render layout: false end def update business = Business.find(params[:id]) department = Department.find(params[:department_id]) if business.update(name: params[:name], department_id: department.id, source: params[:source]) 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]) business.destroy render_success rescue ActiveRecord::DeleteRestrictionError => e render_failure '该商机已其它关联数据产生,暂不能删除' 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 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 @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) @last_follow_up = @business.last_follow_up 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 = FollowUp.find(params[:follow_up_id]) render layout: false end def edit_follow_record @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 render layout: false end def edit_plan render layout: false 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_rate, :description, :advise, :place_id) end end end