require_dependency "educode_sales/application_controller" module EducodeSales class MoneyPlansController < ApplicationController def index follow_up_ids = Business.pluck(:last_follow_up_id) authorize! :read, MoneyPlan 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]} end format.json do if @current_admin.is_admin? @money_plans = MoneyPlan.where(follow_up_id: follow_up_ids) else level = @current_admin.role.role_areas.find_by(clazz: '回款管理').level case level when '自己' @money_plans = MoneyPlan.where(follow_up_id: follow_up_ids).where(staff_id: @current_admin.staff_id) when '区域' a_ids = MoneyPlan.where(follow_up_id: follow_up_ids).where(staff_id: @current_admin.staff_id).ids school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) b_ids = Business.where(school_id: school_ids).pluck(:last_follow_up_id) ids = a_ids + b_ids @money_plans = MoneyPlan.where(id: ids) else @money_plans = MoneyPlan.where(follow_up_id: follow_up_ids) end end if params[:q].present? && params[:q][:business].present? follow_up_ids = Business.where("name like ?", "%#{params[:q][:business]}%").pluck(:last_follow_up_id) @money_plans = @money_plans.where(follow_up_id: follow_up_ids) end if params[:q].present? && params[:q][:school].present? school_ids = School.where("name like ?", "%#{params[:q][:school]}%").pluck(:id) follow_up_ids = Business.where(school_id: school_ids).pluck(:last_follow_up_id) @money_plans = @money_plans.where(follow_up_id: follow_up_ids) end if params[:q].present? && params[:q][:staff_id].present? @money_plans = @money_plans.where(staff_id: params[:q][:staff_id]) end if params[:q].present? && params[:q][:clazz].present? @money_plans = @money_plans.where(clazz: params[:q][:clazz]) end if params[:q].present? && params[:q][:date].present? date = params[:q][:date].split(" - ") @money_plans = @money_plans.where("educode_sales_money_plans.created_at > ? AND educode_sales_money_plans.created_at < ?", date[0], date[1] + '23:59:59') end if params[:sort].present? && params[:sort][:field] @money_plans = @money_plans.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @money_plans = @money_plans.order(created_at: :desc) end @total_amount = @money_plans.sum(:amount) @money_plans = @money_plans.page(params[:page]).per(params[:limit]) end end end end end