require_dependency "educode_sales/application_controller" module EducodeSales class OperationPlansController < ApplicationController def index # authorize! :read, SalePlan respond_to do |format| format.html do common = Common.find_by(clazz: 'staff_type', name: '运营') @staffs = Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] } @years = (1..(Time.now.year - 2020)).map { |d| 2020 + d } @months = (1..12).map { |d| d } end format.json do if @current_admin.is_admin? @sale_plans = OperationPlan else level = @current_admin.role.role_areas.find_by(clazz: '销售计划').level case level when '自己' @sale_plans = OperationPlan.where(staff_id: @current_admin.id) when '区域' staff_ids = Staff.joins(user: [user_extension: [department: :school]]).where("schools.province in (?)", @current_admin.areas.pluck(:name)).pluck(:id) school_ids = StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id) department_ids = Department.where(school_id: school_ids).pluck(:id) teacher_ids = Teacher.where(department_id: department_ids).pluck(:id) @sale_plans = OperationPlan.where("staff_id in (?) OR educode_sales_operation_plans.staff_id = ? OR educode_sales_operation_plans.teacher_id in (?)", staff_ids, @current_admin.id,teacher_ids) else @sale_plans = OperationPlan end end if params[:clazz] == 'week' @sale_plans = @sale_plans.where.not(weekly: nil) else @sale_plans = @sale_plans.where(weekly: nil) end if params[:q].present? && params[:q][:staff_id].present? @sale_plans = @sale_plans.where(staff_id: params[:q][:staff_id]) end if params[:q].present? && params[:q][:year].present? @sale_plans = @sale_plans.where("month >= ? and month <= ?", "#{params[:q][:year]}-01-01 00:00:00".to_date, "#{params[:q][:year]}-12-31 23:59:00".to_date) end if params[:q].present? && params[:q][:month].present? @sale_plans = @sale_plans.where("month = ?", "#{params[:q][:month]}-01 00:00:00".to_date) end if params[:sort].present? && params[:sort][:field] @sale_plans = @sale_plans.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @sale_plans = @sale_plans.order("created_at desc") end @sale_plans = @sale_plans.page(params[:page]).per(params[:limit]) end end end def new_week @teacher = Teacher.find_by(id: params[:teacher_id]) @week = Time.now.strftime('%W').to_i week = Time.now.strftime('%W').to_i if week > 3 @weeks = [[week+2,week+2], [week+1,week+1], [week.to_s + ' 本周',week], [week - 1,week - 1], [week - 2,week - 2]] elsif week == 2 @weeks = [[week.to_s + ' 本周',week], [week - 1,week - 1]] else @weeks = [[week.to_s + ' 本周',week]] end @teachers = @current_admin.teachers.where(is_key: 0).pluck(:name, :id) render layout: false end def create plan = @current_admin.operation_plans.build(plan_params) plan.month = params["month"] + "-1" if (Integer(params[:teacher_id]) rescue nil) plan.teacher_id = @current_admin.teachers.find(params[:teacher_id]).id if plan.save render_success else render_failure plan end else render_failure '不存在该教师' end end def new_weekly d = @current_admin area_ids = EducodeSales::Common.where(clazz: 'area').ids.sort.to_s if d.areas.present? if d.areas.ids.sort.to_s == area_ids @area = '全国' else @area = d.areas.pluck(:name).join("、") end else @area = '' end @name = @current_admin.user.real_name sale_plans = OperationPlan.where(month: Time.now.strftime("%Y-%m") + "-01 00:00:00", weekly: Time.now.strftime('%W').to_i, staff_id: @current_admin.id) @rate = sale_plans.present? ? (sale_plans.sum("finish_rate") / sale_plans.size) : 0 @departments_count = sale_plans.joins(:teacher).count("distinct(department_id)") # @departments_count = 1 render layout: false end def new_month @teacher = Teacher.find_by(id: params[:teacher_id]) @teachers = @current_admin.teachers.where(is_key: 0).pluck(:name, :id) render layout: false end def new_monthly d = @current_admin area_ids = EducodeSales::Common.where(clazz: 'area').ids.sort.to_s if d.areas.present? if d.areas.ids.sort.to_s == area_ids @area = '全国' else @area = d.areas.pluck(:name).join("、") end else @area = '' end @name = @current_admin.user.real_name sale_plans = OperationPlan.where(month: Time.now.strftime("%Y-%m") + "-01 00:00:00", staff_id: @current_admin.id, weekly: nil) @rate = sale_plans.present? ? (sale_plans.sum("finish_rate") / sale_plans.size) : 0 @departments_count = sale_plans.joins(:teacher).count("distinct(department_id)") render layout: false end def show_week @sale_plan = OperationPlan.find(params[:id]) render layout: false end def show_weekly render layout: false end def show_month render layout: false end def show_monthly render layout: false end def edit_week @teacher = Teacher.find_by(id: params[:teacher_id]) level = @current_admin.is_admin? ? '全部' : @current_admin.role.role_areas.find_by(clazz: '运营计划').level if level == '自己' @teachers = @current_admin.teachers.where(is_key: 0).pluck(:name, :id) elsif level == '区域' school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) @teachers = Teacher.joins("JOIN departments ON educode_sales_teachers.department_id = departments.id").where(is_key: false).where("departments.school_id in (?) OR educode_sales_teachers.staff_id = #{@current_admin.id}", school_ids) else @teachers = Teacher.where(is_key: 0).pluck(:name, :id) end @sale_plan = OperationPlan.find(params[:id]) if @sale_plan.weekly.present? week = Time.now.strftime('%W').to_i if week > 3 @weeks = [week+2, week+1, week, week - 1, week - 2] elsif week == 2 @weeks = [week, week - 1] else @weeks = [week] end @weeks << @sale_plan.weekly @weeks = @weeks.uniq.sort @weeks = @weeks.map do |d| if d == week ["#{d} 本周", d] else [d, d] end end end @months = [Time.now.strftime("%Y-%m"), Time.now.months_ago(1).strftime("%Y-%m")] @months << @sale_plan.month.strftime("%Y-%m") @months = @months.uniq.sort @finish_rates = [] 11.times { |d| @finish_rates << ["#{d * 10}%", d * 10] } render layout: false end def edit_weekly render layout: false end def edit_month @teacher = Teacher.find_by(id: params[:teacher_id]) level = @current_admin.is_admin? ? '全部' : @current_admin.role.role_areas.find_by(clazz: '运营计划').level if level == '自己' @teachers = @current_admin.teachers.where(is_key: 0).pluck(:name, :id) elsif level == '区域' school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) @teachers = Teacher.joins("JOIN departments ON educode_sales_teachers.department_id = departments.id").where(is_key: false).where("departments.school_id in (?) OR educode_sales_teachers.staff_id = #{@current_admin.id}", school_ids) else @teachers = Teacher.where(is_key: 0).pluck(:name, :id) end @sale_plan = OperationPlan.find(params[:id]) @months = [Time.now.strftime("%Y-%m"), Time.now.months_ago(1).strftime("%Y-%m")] @months << @sale_plan.month.strftime("%Y-%m") @months = @months.uniq.sort @finish_rates = [] 11.times { |d| @finish_rates << ["#{d * 10}%", d * 10] } render layout: false end def edit_monthly render layout: false end def my_week @sale_plans = OperationPlan.where(staff_id: @current_admin.id, month: Time.now.strftime("%Y-%m") + "-01 00:00:00", weekly: Time.now.strftime('%W').to_i) if params[:sort].present? && params[:sort][:field] @sale_plans = @sale_plans.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @sale_plans = @sale_plans.order("updated_at desc") end @sale_plans = @sale_plans.page(params[:page]).per(params[:limit]) end def my_month @sale_plans = OperationPlan.where(staff_id: @current_admin.id, month: Time.now.strftime("%Y-%m") + "-01 00:00:00", weekly: nil) if params[:sort].present? && params[:sort][:field] @sale_plans = @sale_plans.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @sale_plans = @sale_plans.order("updated_at desc") end @sale_plans = @sale_plans.page(params[:page]).per(params[:limit]) end def update operation_plan = OperationPlan.find(params[:id]) params["month"] = params["month"] + "-1" if operation_plan.update(params.permit(:month, :weekly, :content, :finish_rate, :teacher_id)) render_success else render_failure operation_plan end end def destroy operation_plan = OperationPlan.find(params[:id]) operation_plan.destroy render_success end private def plan_params params.permit(:month, :weekly, :content, :finish_rate) end end end