require_dependency "educode_sales/application_controller" module EducodeSales class PlansController < 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 = SalePlan else level = @current_admin.role.role_areas.find_by(clazz: '销售计划').level case level when '自己' @sale_plans = SalePlan.where(staff_id: @current_admin.id) when '区域' staff_ids = Staff.joins(department: :school).where("school.province in ?", staff.areas.pluck(:name)).pluck(:id) @sale_plans = SalePlan.where("staff_id in ?", staff_ids) else @sale_plans = SalePlan 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("updated_at desc") end @sale_plans = @sale_plans.page(params[:page]).per(params[:per_page]) end end end def new render layout: false end def create plan = @current_admin.sale_plans.build(plan_params) plan.month = params["month"] + "-1" plan.business_id = @current_admin.businesses.find(params[:business_id]).id if plan.save render_success else render_failure plan end end def new_week @business = Business.find_by(id: params[:business_id]) week = Time.now.strftime('%W').to_i if week > 3 @weeks = [week, week - 1, week - 2] elsif week == 2 @weeks = [week, week - 1] else @weeks = [week] end @businesses = @current_admin.businesses.pluck(:name, :id) render layout: false end def new_weekly sale_plans = SalePlan.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 ? (sale_plans.sum("finish_rate") / sale_plans.size) : 0 @departments_count = sale_plans.joins(:business).count("distinct(department_id)") @total_amount = sale_plans.joins(business: :last_follow_up).sum("total_amount") render layout: false end def new_month @business = Business.find_by(id: params[:business_id]) @businesses = @current_admin.businesses.pluck(:name, :id) render layout: false end def new_monthly sale_plans = SalePlan.where(month: Time.now.strftime("%Y-%m") + "-01 00:00:00", staff_id: @current_admin.id, weekly: nil) @rate = sale_plans ? (sale_plans.sum("finish_rate") / sale_plans.size) : 0 @departments_count = sale_plans.joins(:business).count("distinct(department_id)") @total_amount = sale_plans.joins(business: :last_follow_up).sum("total_amount") render layout: false end def show_week @sale_plan = SalePlan.find(params[:id]) render layout: false end def show_weekly render layout: false end def show_month @sale_plan = SalePlan.find(params[:id]) render layout: false end def show_monthly render layout: false end def edit_week @businesses = @current_admin.businesses.pluck(:name, :id) @sale_plan = @current_admin.sale_plans.find(params[:id]) if @sale_plan.weekly.present? week = Time.now.strftime('%W').to_i if week > 3 @weeks = [week, week - 1, week - 2] elsif week == 2 @weeks = [week, week - 1] else @weeks = [week] end @weeks << @sale_plan.weekly @weeks = @weeks.uniq.sort 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 = [] 10.times { |d| @finish_rates << ["#{d * 10}%", d * 10] } render layout: false end def update sale_plan = @current_admin.sale_plans.find(params[:id]) params["month"] = params["month"] + "-1" if sale_plan.update(plan_params) render_success else render_failure sale_plan end end def destroy sale_plan = @current_admin.sale_plans.find(params[:id]) if sale_plan.destroy render_success else render_failure sale_plan end end def edit_weekly render layout: false end def edit_month @businesses = @current_admin.businesses.pluck(:name, :id) @sale_plan = @current_admin.sale_plans.find(params[:id]) @finish_rates = [] 10.times { |d| @finish_rates << ["#{d*10}%", d * 10]} render layout: false end def edit_monthly render layout: false end def my_week @sale_plans = SalePlan.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[:per_page]) end def my_month @sale_plans = SalePlan.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[:per_page]) end private def plan_params params.permit(:month, :weekly, :content, :finish_rate) end end end