require_dependency "educode_sales/application_controller" module EducodeSales class BusinessesController < ApplicationController def file @root = Rails.root render layout: false end def upload_file render layout: false end def time_line @business = Business.find(params[:business_id]) respond_to do |format| format.html do @follow_ups = @business.follow_ups.order(created_at: :desc) # @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]) render layout: false end end end 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 gon.menus << { title: '跟进时间线', event: 'time_line' } gon.menus << { title: '附件管理', event: 'file' } 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 '自己' business_ids = Business.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).pluck(:id) @businesses = Business.where("educode_sales_businesses.staff_id = ? OR educode_sales_businesses.id in (?)", @current_admin.id, business_ids) when '区域' school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) business_ids = Business.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).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} OR educode_sales_businesses.id in (?)", school_ids, business_ids) else @businesses = Business end end if params[:q].present? && params[:q][:name].present? @year = params[:q][:name].split("-")[1].present? ? params[:q][:name].split("-")[1] : '' end if params[:q].present? && params[:q][:name].present? && params[:q][:name] == "(销售态势-现有商机)" a_id = Common.find_by(extras: EducodeSales::Common::ATYPE)&.id b_id = Common.find_by(extras: EducodeSales::Common::BTYPE)&.id c_id = Common.find_by(extras: EducodeSales::Common::CTYPE)&.id d_id = Common.find_by(extras: EducodeSales::Common::DTYPE)&.id ids = [a_id ,b_id ,c_id ,d_id] @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 in (?)",ids) end if params[:q].present? && params[:q][:name].present? && params[:q][:name] == "(销售态势-" + @year + "-已中标商机)" @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.bidded_date > ? AND educode_sales_follow_ups.bidded_date < ?", "#{@year}-01-01", "#{@year}-12-31") end if params[:q].present? && params[:q][:name].present? && params[:q][:name] == "(销售态势-" + @year + "-已签单商机)" @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.signed_date > ? AND educode_sales_follow_ups.signed_date < ?", "#{@year}-01-01", "#{@year}-12-31") end if params[:q].present? && params[:q][:name].present? && params[:q][:name] == "(销售态势-" + @year + "-已回款商机)" x_id = Common.find_by(extras: EducodeSales::Common::XTYPE)&.id # @businesses = @businesses.where("educode_sales_businesses.created_at > ? AND educode_sales_businesses.created_at < ? AND educode_sales_businesses.return_money > 0", "#{@year}-01-01", "#{@year}-12-31").joins(" # JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id # ").where.not("educode_sales_follow_ups.clazz_id = ?",x_id) year_time = "#{@year}-01-01 00:00:00" year_over_time = "#{@year}-12-31 23:59:59" @businesses = @businesses.joins(follow_ups: :money_plans).where("educode_sales_follow_ups.clazz_id != ?", x_id).where.not("educode_sales_money_plans.clazz!= ?", 1).where("educode_sales_money_plans.date_at >= ? AND educode_sales_money_plans.date_at <= ? ", year_time, year_over_time).distinct end if params[:q].present? && params[:q][:all].present? @businesses = @businesses end if params[:q].present? && params[:q][:mine].present? ids = AssignFollowUp.where(staff_id: @current_admin.id).pluck(:follow_up_id) ids1 = FollowUp.where(id: ids).pluck(:business_id) @businesses =@businesses.where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", ids1, @current_admin.id) end @businesses = @businesses if params[:q].present? && params[:q][:name].present? && params[:q][:name] != "(销售态势-" + @year + "-已中标商机)" && params[:q][:name] != "(销售态势-" + @year + "-已签单商机)" && params[:q][:name] != "(销售态势-" + @year + "-已回款商机)" && params[:q][:name] != "(销售态势-现有商机)" @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? part_a_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is null AND staff_id = ?",params[:q][:staff_id]).ids part_b_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is not null").joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.staff_id = ?", params[:q][:staff_id]).pluck(:business_id) business_ids = part_a_ids + part_b_ids @businesses = EducodeSales::Business.where(id:business_ids) 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 @o_name = EducodeSales::Common.find_by(extras: EducodeSales::Common::OTYPE)&.name @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 if @last_follow_up gon.sales = @last_follow_up.assign_follow_ups.map do |d| {value: d.staff_id, name: d.staff.user.full_name} end end 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_id = params[:follow_up_id] @follow_up = FollowUp.find(follow_up_id) id = @follow_up.id @follow_ups = Business.find(@follow_up.business_id).follow_ups ids = @follow_ups.ids size = ids.size ids = ids.reverse index = ids.index(id) @next_id = (index != size - 1 && size > 1) ? ids[index + 1] : 0 @pre_id = (index != 0 && size > 1) ? ids[index - 1] : 0 render layout: false end def edit_follow_record @o_name = EducodeSales::Common.find_by(extras: EducodeSales::Common::OTYPE)&.name @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_amount, :description, :advise, :place_id) end end end