require_dependency "educode_sales/application_controller" module EducodeSales class OperationReportsController < ApplicationController def index # authorize! :read, SalePlan respond_to do |format| format.html do end format.json do if @current_admin.is_admin? @sale_reports = OperationReport else level = @current_admin.role.role_areas.find_by(clazz: '销售计划').level case level when '自己' @sale_reports = OperationReport.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) @sale_reports = OperationReport.where("staff_id in (?) OR educode_sales_operation_reports.staff_id = ?", staff_ids, @current_admin.id) else @sale_reports = OperationReport end end if params[:clazz] == 'week' @sale_reports = @sale_reports.where.not(weekly: nil) else @sale_reports = @sale_reports.where(weekly: nil) end if params[:q].present? && params[:q][:staff_id].present? @sale_reports = @sale_reports.where(staff_id: params[:q][:staff_id]) end if params[:q].present? && params[:q][:year].present? @sale_reports = @sale_reports.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_reports = @sale_reports.where("month = ?", "#{params[:q][:month]}-01 00:00:00".to_date) end if params[:sort].present? && params[:sort][:field] @sale_reports = @sale_reports.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @sale_reports = @sale_reports.order("created_at desc") end @sale_reports = @sale_reports.page(params[:page]).per(params[:limit]) end end end def create if params[:month].present? sale_report = @current_admin.operation_reports.build(month: Date.today.beginning_of_month) else sale_report = @current_admin.operation_reports.build(month: Date.today.beginning_of_month, weekly: Time.now.strftime('%W').to_i) end sale_report.finish_rate = params[:finish_rate] sale_report.client = params[:client] sale_report.content = params[:content] if sale_report.save render_success else render_failure sale_report end end def show @sale_report = OperationReport.find(params[:id]) @name = @sale_report.staff.user.real_name @area = @sale_report.staff.areas.present? ? @sale_report.staff.areas.pluck(:name).join("、") : '' render layout: false end def destroy sale_report = OperationReport.find(params[:id]) sale_report.destroy render_success end def edit @sale_report = OperationReport.find(params[:id]) @name = @sale_report.staff.user.real_name @area = @sale_report.staff.areas.present? ? @sale_report.staff.areas.pluck(:name).join("、") : '' render layout: false end def update sale_report = OperationReport.find(params[:id]) sale_report.content = params[:content] if sale_report.save render_success else render_failure sale_report end end def update_audit authorize! :audit, EducodeSales::OperationReport sale_report = OperationReport.find(params[:id]) sale_report.assign_attributes(audit_params) if sale_report.level1_opinion_changed? || sale_report.level1_score_changed? sale_report.audit1_user_id = @current_admin.id end if sale_report.level2_opinion_changed? || sale_report.level2_score_changed? sale_report.audit2_user_id = @current_admin.id end if sale_report.level3_opinion_changed? || sale_report.level3_score_changed? sale_report.audit3_user_id = @current_admin.id end if sale_report.save render_success else render_failure sale_report end end def audit @sale_report = OperationReport.find(params[:id]) @name = @sale_report.staff.user.real_name @area = @sale_report.staff.areas.present? ? @sale_report.staff.areas.pluck(:name).join("、") : '' render layout: false end def plans sale_report = OperationReport.find(params[:id]) @sale_plans = OperationPlan.where(staff_id: sale_report.staff_id, month: sale_report.month, weekly: sale_report.weekly).order("updated_at desc").page(params[:page]).per(params[:limit]) end private def audit_params params.permit(:level1_opinion, :level2_opinion, :level3_opinion, :level1_score, :level2_score, :level3_score) end end end