require_dependency "educode_sales/application_controller" module EducodeSales class ProjectsController < ApplicationController before_action :find_project, only: [:edit, :destroy, :detail, :history, :update, :recycle] def index authorize! :contract_business, 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).where.not(role_id: 11).map { |d| [d.user.real_name, d.id] } gon.staffs = Staff.joins(:user).where(job_type: common.id).where.not(role_id: 11).map { |d| { name: d.user.real_name, value: d.id } } @more = true gon.menus = [] gon.export_menus = [] gon.place = params[:place_id].present? ? [{ value: params[:place_id], name: EducodeSales::Place.find(params[:place_id]).name }] : [] bussiness_type = [] if params[:type] # 项目类型 bussiness_type = EducodeSales::Business.include_types(params[:type]) || [] end business_step = [] if params[:step] # 项目阶段 business_step = EducodeSales::Business.include_steps(params[:step]) || [] end if params[:business_year].present? && params[:business_year] != '全部' gon.business_year = params[:business_year] end if params[:bidded_date_year].present? && params[:bidded_date_year] != '全部' if params[:step] == '应收款' gon.bidded_date = "2015-01-01" + " - " + Time.new(params[:bidded_date_year]).end_of_year.strftime("%Y-%m-%d") else gon.bidded_date = Time.new(params[:bidded_date_year]).strftime("%Y-%m-%d") + " - " + Time.new(params[:bidded_date_year]).end_of_year.strftime("%Y-%m-%d") end end if params[:signed_date_year].present? && params[:signed_date_year] != '全部' gon.signed_date = Time.new(params[:signed_date_year]).strftime("%Y-%m-%d") + " - " + Time.new(params[:signed_date_year]).end_of_year.strftime("%Y-%m-%d") end if params[:date_at].present? && params[:date_at] != '全部' # 回款时间 gon.date_at = Time.new(params[:date_at]).strftime("%Y-%m-%d") + " - " + Time.new(params[:date_at]).end_of_year.strftime("%Y-%m-%d") end gon.business_type = Common.where(clazz: 'business_type').map do |d| { value: d.id, name: d.name, selected: bussiness_type.include?(d.extras) } end gon.business_step = Common.where(clazz: 'business_step', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).order("position").map do |d| { value: d.id, name: d.name, selected: business_step.include?(d.name) } end filter = Filter.find_or_create_by(staff_id: @current_admin.id, clazz: "businesses_list") if filter.extras.present? if filter.extras['area'].nil? filter.extras['area'] = 1 filter.extras['property'] = 1 filter.extras['sale_way'] = 1 end filter.save gon.filter = filter.extras else gon.filter = { 'area': 1, 'property': 1, 'sale_way': 1 } 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 '区域' # 查看区域商机,需要排除掉其它人员手上的监管学校 other_staff_school_id = EducodeSales::StaffSchool.where.not(staff_id: @current_admin.id).where("school_id IN (SELECT school_id FROM educode_sales_staff_schools WHERE staff_id = #{@current_admin.id}) IS NOT TRUE").distinct.pluck :school_id school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id) - other_staff_school_id if can?(:special, EducodeSales::Business) # 专项商机 school_tag_ids = @current_admin.staff_school_tags.pluck(:school_tag_id) tag_school_ids = SchoolTagMiddle.where(school_tag_id: school_tag_ids).pluck(:school_id) school_ids += tag_school_ids end 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 @businesses = if params[:is_deleted].to_s.present? @businesses.where.not(p_deleted_at: nil) else @businesses.where(p_deleted_at: nil) end @is_deleted = params[:is_deleted].to_s.present? gon.is_deleted = @is_deleted ? 1 : 0 if params[:q].present? && params[:q][:name].present? @year = params[:q][:name].split("-")[1].present? ? params[:q][:name].split("-")[1] : '' end # @budget_stage_ids = Common.where(clazz: '商机阶段', name: ['初步接洽', '准备方案','已交方案', '已立项']).pluck(:id) if params[:q] && params[:q][:signed_date].present? date = params[:q][:signed_date].split(" - ") @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 <= ?", date[0], date[1]) end if params[:q] && params[:q][:date_at].present? date = params[:q][:date_at].split(" - ") x_id = Common.find_by(extras: EducodeSales::Common::XTYPE)&.id @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 <= ? ", date[0] + " 00:00:00", date[1] + " 23:59:59").distinct end if params[:q] && params[:q][:bidded_date].present? date = params[:q][:bidded_date].split(" - ") @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 <= ?", date[0], date[1]) end if params[:q].present? && params[:q][:all].present? @businesses = @businesses end if params[:q].present? && params[:q][:name].present? @businesses = @businesses.where("educode_sales_businesses.name like ?", "%#{params[:q][:name]}%") end if params[:q].present? && params[:q][:p_stage].present? @businesses = @businesses.where(p_stage: "#{params[:q][:p_stage].to_s.strip}") end if params[:q].present? && params[:q][:p_staff_id].present? @businesses = @businesses.where(p_staff_id: params[:q][:p_staff_id]) end if params[:q].present? && params[:q][:p_sale_staff_id].present? @businesses = @businesses.where(p_sale_staff_id: params[:q][:p_sale_staff_id]) end if params[:q].present? && params[:q][:p_special].present? @businesses = @businesses.where(p_special: "#{params[:q][:p_special]}") end if params[:q].present? && params[:q][:p_difficulty].present? @businesses = @businesses.where(p_difficulty: "#{params[:q][:p_difficulty]}") end # bidded_date = d.last_follow_up&.bidded_date # stage = d.last_follow_up&.stage&.name # (d.p_pre_money_time.present? && Time.now > d.p_pre_money_time && d.p_actual_money_time.blank?) || # (d.p_pre_accept_time.present? && Time.now > d.p_pre_accept_time && d.p_accept_time.blank?) ? "逾期" : "正常" # SELECT * FROM users WHERE NOW() > DATE_ADD(bidded_date, INTERVAL 1 MONTH) if params[:q].present? && params[:q][:p_state].present? p_state = params[:q][:p_state].to_i # 逾期id late_ids = Business.joins("JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id JOIN educode_sales_commons on educode_sales_commons.id = educode_sales_follow_ups.stage_id") .where("educode_sales_follow_ups.bidded_date is not null") .where("NOW() > DATE_ADD(educode_sales_follow_ups.bidded_date, INTERVAL 1 MONTH)") .where("educode_sales_businesses.p_stage is null") .where("educode_sales_commons.name = '已中标'") .or(Business.joins("JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id JOIN educode_sales_commons on educode_sales_commons.id = educode_sales_follow_ups.stage_id") .where("NOW() > educode_sales_businesses.p_pre_money_time") .where("educode_sales_businesses.p_pre_money_time is not null") .where("educode_sales_businesses.p_actual_money_time is null") ).or(Business.joins("JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id JOIN educode_sales_commons on educode_sales_commons.id = educode_sales_follow_ups.stage_id") .where("NOW() > educode_sales_businesses.p_pre_accept_time") .where("educode_sales_businesses.p_pre_accept_time is not null") .where("educode_sales_businesses.p_accept_time is null")).ids if p_state == 0 # 正常 @businesses = @businesses.where.not(id: late_ids) else # 逾期 @businesses = @businesses.where(id: late_ids) end 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 educode_sales_businesses.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]).ids business_ids = part_a_ids + part_b_ids if params[:q][:clazz] == 'delete_list' @businesses = EducodeSales::Business.unscoped.where(id: business_ids) else @businesses = EducodeSales::Business.where(id: business_ids) end end if params[:q].present? && params[:q][:business_type].present? @businesses = @businesses.where("educode_sales_businesses.clazz_id in (?)", params[:q][:business_type].split(",").map(&:to_i)) end contract_ids = Common.where(clazz: '商机阶段', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).pluck(: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.stage_id in (?)", contract_ids) 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 in (?)", params[:q][:business_step].split(",").map(&:to_i)) 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][:business_year].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.year = ?", params[:q][:business_year]) end if params[:q].present? && params[:q][:year].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.year <= ?", params[:q][:year]) end if params[:q].present? && params[:q][:o_business_deployment].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.o_business_deployment = ?", params[:q][:o_business_deployment]) end if params[:q].present? && params[:q][:area].present? p = EducodeSales::Common.find(params[:q][:area]).name if @current_admin.is_admin? @businesses = @businesses.joins(" JOIN departments ON educode_sales_businesses.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) else level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level if level == "区域" @businesses = @businesses.joins(" JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) else @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 end 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] + '23:59:59') end if params[:q].present? && params[:q][:search_p_actual_money_time].present? date = params[:q][:search_p_actual_money_time].split(" - ") @businesses = @businesses.where("educode_sales_businesses.p_actual_money_time > ? AND educode_sales_businesses.p_actual_money_time < ?", date[0], date[1] + '23:59:59') end if params[:q].present? && params[:q][:invitation_at].present? date = params[:q][:invitation_at].split(" - ") @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.invitation_at > ? AND educode_sales_follow_ups.invitation_at < ?", date[0] + '00:00:00', date[1] + '23:59:59') end # 根据编号获取商机 if params[:q].present? && params[:q][:number].present? @businesses = @businesses.where("number like :data", data: "%#{params[:q][:number].strip}%") end if params[:q].present? && params[:q][:clazz].present? # 我的关注 if params[:q][:clazz] == 'followes' my_ids = BusinessRelationShip.where(user_id: @current_admin.user_id).pluck(:business_id) @businesses = @businesses.where(id: my_ids) elsif params[:q][:clazz] == 'me' ids = AssignFollowUp.joins("JOIN educode_sales_follow_ups ON educode_sales_follow_ups.id = educode_sales_assign_follow_ups.follow_up_id").where(staff_id: @current_admin.id).pluck(:business_id) @businesses = @businesses.where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", ids, @current_admin.id) end end # 商机变化 if params[:q][:clazz_changes].present? clazz_changes = EducodeSales::BusinessClazzChange.clazz_changes_value[params[:q][:clazz_changes].to_s] @businesses = @businesses.joins("JOIN educode_sales_business_clazz_changes ON educode_sales_business_clazz_changes.business_id = educode_sales_businesses.id").where("educode_sales_business_clazz_changes.clazz_changed in (?)", clazz_changes) end if params[:sort].present? && params[:sort][:field] if params[:sort][:field] == "service_end_time" @businesses = @businesses.order("service_time_long #{params[:sort][:order]}") else @businesses = @businesses.order("#{params[:sort][:field]} #{params[:sort][:order]}") end else @businesses = @businesses.order("educode_sales_businesses.created_at desc") end @business_count = @businesses.distinct.count # mysql -select @businesses = @businesses.select(" distinct educode_sales_businesses.*, last_follow.invitation_at, last_follow.budget_amount, last_follow.o_business_deployment, last_follow.service_time_long, last_follow.service_end_time, last_follow.service_start_time, last_follow.reception_at, last_follow.bidded_date, last_follow.signed_date, last_follow.year, last_follow.created_at as latest_time, last_follow.actual_amount, last_follow.divide_rate, last_follow.divide_amount, (last_follow.total_amount - last_follow.actual_amount) as divide_money, (last_follow.actual_amount - educode_sales_businesses.return_money) as wait_return_money, last_follow.total_amount, total_follow_ups.follow_ups_counts ").joins(" LEFT JOIN educode_sales_follow_ups AS last_follow ON educode_sales_businesses.last_follow_up_id = last_follow.id AND last_follow.deleted_at IS NULL LEFT JOIN ( SELECT COUNT(*) AS follow_ups_counts, educode_sales_follow_ups.business_id FROM educode_sales_follow_ups WHERE educode_sales_follow_ups.deleted_at IS NULL GROUP BY educode_sales_follow_ups.business_id ) AS total_follow_ups ON educode_sales_businesses.id = total_follow_ups.business_id ").includes(:users, last_follow_up: [:clazz, :assign_follow_ups]).page(params[:page]).per(params[:limit]) end end end def detail staffs = Staff.where.not(role_id: 11).includes(:user) gon.staffs = staffs.map { |d| { name: d.user.real_name, value: d.id } } gon.staff_value = @project.p_staff_id ? [{ name: @project.p_staff&.user&.real_name, value: @project.p_staff_id }] : [] gon.sale_staff_value = @project.p_sale_staff_id ? [{ name: @project.p_sale_staff&.user&.real_name, value: @project.p_sale_staff_id }] : [] render layout: false end def history render layout: false end def update @project.assign_attributes(project_params) check_changes @project.save render_success end def destroy @project.p_soft_destroy(current_user.id) render_success end def edit staffs = Staff.where.not(role_id: 11).includes(:user) gon.staffs = staffs.map { |d| { name: d.user.real_name, value: d.id } } gon.staff_value = @project.p_staff_id ? [{ name: @project.p_staff&.user&.real_name, value: @project.p_staff_id }] : [] gon.sale_staff_value = @project.p_sale_staff_id ? [{ name: @project.p_sale_staff&.user&.real_name, value: @project.p_sale_staff_id }] : [] render layout: false end def recycle @project.p_recycle render_success end private def project_params permit = %i[p_stage p_difficulty p_special p_status p_staff_id p_sale_staff_id p_course_time p_platform_time p_deploy_time p_accept_time p_pre_money_time p_actual_money_time p_money p_content p_pre_accept_time] params.permit(permit) end def find_project @project = Business.find(params[:id]) end def check_changes unless @project.new_record? history = [] except_attr = %i[id staff_id department_id name created_at updated_at follow_ups_count last_follow_up_id return_money source deleted_at school_id number clazz_id delete_reason auth_desc state_id p_deleted_at p_deleter_id] arr = @project.attributes.except(except_attr).keys arr.each do |attr| if @project.send("#{attr}_changed?") old_value, new_value = @project.send("#{attr}_was"), @project.send(attr) res = Project.save_history(attr, old_value, new_value) history << res if res end end @project.business_histories.create(content: history, staff: current_user) if history.present? end end def edu_setting name EduSetting.get(name) end def follow_business(user_id, business_id) BusinessRelationShip.where(user_id: user_id, business_id: business_id).first end end end