require_dependency "educode_sales/application_controller" module EducodeSales class ContractsController < ApplicationController def index authorize! :read_contract, 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) @staffs = staffs.map { |d| [d.user.real_name, d.id]} gon.place = params[:place_id].present? ? [{ value: params[:place_id], name: EducodeSales::Place.find(params[:place_id]).name }] : [] gon.staffs = staffs.map { |d| {name: d.user.real_name, value: d.id } } 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 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 end format.json do @businesses = Business 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 @current_admin.is_admin? @businesses = @businesses else level = @current_admin.role.role_areas.find_by(clazz: '合同管理').level case level when '自己' assign_business_ids = EducodeSales::Business.joins(:assign_staffs).where("educode_sales_assign_staffs.staff_id = ?", @current_admin.id).pluck(:id) @businesses = @businesses.where("educode_sales_businesses.staff_id = ? OR educode_sales_businesses.id in (?)", @current_admin.id, assign_business_ids) when '区域' school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id) business_ids = @businesses.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).pluck(:id) assign_business_ids = EducodeSales::Business.joins(:assign_staffs).where("educode_sales_assign_staffs.staff_id = ?", @current_admin.id).pluck(:id) @businesses = @businesses.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 + assign_business_ids) end end 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][:name].present? @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 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 @businesses = EducodeSales::Business.where(id:business_ids) 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 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? area = 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 = ?", area) 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 = ?", area) else @businesses = @businesses.joins(" JOIN departments ON educode_sales_businesses.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", area) 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][:sales_staff_id].present? # 销售经理 @businesses = @businesses.joins("LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = educode_sales_businesses.last_follow_up_id"). where("(educode_sales_assign_follow_ups.id IS NOT NULL AND educode_sales_assign_follow_ups.staff_id = ?) OR (educode_sales_assign_follow_ups.id IS NULL AND educode_sales_businesses.staff_id = ?)", params[:q][:sales_staff_id], params[:q][:sales_staff_id]) end if params[:q].present? && params[:q][:after_sales_staff_id].present? # 售后工程师 @businesses = @businesses.joins("LEFT JOIN educode_sales_assign_staffs ON educode_sales_assign_staffs.sourcable_type = 'EducodeSales::Business' AND educode_sales_assign_staffs.sourcable_id = educode_sales_businesses.id"). where("educode_sales_assign_staffs.staff_id = ?", params[:q][:after_sales_staff_id]) 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 @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 list common = Common.find_by(clazz: 'staff_type', name: '销售') staffs = Staff.joins(:user).where(job_type: common.id).where.not(role_id: 11) @staffs = staffs.map { |d| [d.user.real_name, d.id]} role = Role.find_by(name: '售后工程师') @after_sales_staffs = Staff.joins(:user).where(role_id: role.id).map { |d| [d.user.real_name, d.id]} end def new_follow_up @business = Business.find(params[:id]) @last_follow_up = @business.last_follow_up @deployment_type = EducodeSales::FollowUp::BUSINESS_DEPLOYMENT @signed_clazz = EducodeSales::FollowUp.signed_clazzs.keys signed_department = @last_follow_up.signed_department if signed_department gon.department = [{value: signed_department.id, name: "#{signed_department&.school&.name}-#{signed_department&.name}"}] gon.value = signed_department.id else gon.department = [] gon.value = '' end @list = {} EducodeSales::ContractDateList.clazzs.each do |d, i| @list[d] = [EducodeSales::ContractDateList.new] end @last_follow_up.contract_date_lists.each do |d| if @list[d.clazz][0].id == nil @list[d.clazz][0] = d else @list[d.clazz] << d end end render layout: false end def follow_ups authorize! :read_contract, Business contract_ids = Common.where(clazz: '商机阶段', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).pluck(:id) @follow_ups = FollowUp.joins(" JOIN educode_sales_businesses ON educode_sales_follow_ups.business_id = educode_sales_businesses.id ").joins(:follow_up_trends).where("educode_sales_follow_ups.stage_id in (?)", contract_ids).select("educode_sales_follow_ups.*, educode_sales_follow_up_trends.clazz, educode_sales_follow_up_trends.content, educode_sales_follow_up_trends.id AS trend_id") if @current_admin.is_admin? @follow_ups = @follow_ups else level = @current_admin.role.role_areas.find_by(clazz: '合同管理').level case level when '自己' @follow_ups = @follow_ups.where("educode_sales_follow_ups.staff_id = ?", @current_admin.id) when '区域' school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_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) business_ids = @businesses.pluck(:id) @follow_ups = @follow_ups.where(business_id: business_ids) end end if params[:q].present? && params[:q][:name].present? @follow_ups = @follow_ups.joins(:business).where("educode_sales_businesses.name LIKE ?", "%#{params[:q][:name]}%") end if params[:q].present? && params[:q][:follows_date].present? date = params[:q][:follows_date].split(" - ") @follow_ups = @follow_ups.where("educode_sales_follow_ups.created_at > ? AND educode_sales_follow_ups.created_at < ?", date[0], date[1] + '23:59:59') end if params[:q].present? && params[:q][:staff_id].present? @follow_ups = @follow_ups.where(staff_id: params[:q][:staff_id]) end if params[:q].present? && params[:q][:department].present? departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:q][:department]}%").pluck(:id) @follow_ups = @follow_ups.joins(business: :department).where("departments.id in (?)", departments_ids) end if params[:q].present? && params[:q][:area].present? p = EducodeSales::Common.find(params[:q][:area]).name @follow_ups = @follow_ups.joins(business: :department).joins(" JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) end if params[:q].present? && params[:q][:description].present? @follow_ups = @follow_ups.joins(:business).joins("JOIN educode_sales_businesses ON educode_sales_businesses.id = educode_sales_follow_ups.business_id ").where("educode_sales_follow_ups.description LIKE ?" ,"%#{params[:q][:description]}%") end if params[:q].present? && params[:q][:info_clazz].present? @follow_ups = @follow_ups.where("educode_sales_follow_up_trends.clazz = ?", params[:q][:info_clazz]) end @follow_ups = @follow_ups.includes(:staff, :clazz, :stage, key_person: :teacher, business: [department: :school]) if params[:sort].present? && params[:sort][:field] @follow_ups = @follow_ups.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @follow_ups = @follow_ups.order("educode_sales_follow_ups.created_at desc") end @follow_ups = @follow_ups.page(params[:page]).per(params[:limit]) end def add_follow_ups @business = Business.find(params[:business_id]) last_follow_up = @business.last_follow_up data = [] 6.times do |i| data[i] = [] params["#{i+1}_promise_date"].each do |d, v| data[i] << [v] end params["#{i+1}_num"].each do |d, v| data[i][d.to_i] << v end params["#{i+1}_plan_date"].each do |d, v| data[i][d.to_i] << v end params["#{i+1}_actual_date"].each do |d, v| data[i][d.to_i] << v if i == 1 && last_follow_up # 验收时间 last_follow_up.reception_at = v end end end data.each do |d| d.select! { |d| d.delete_if{|f| f == ''}.present? } end follow_up = last_follow_up.dup follow_up.assign_attributes(follow_up_params) follow_up.description = params[:content] if params[:service_time].present? date = params[:service_time].split(" - ") follow_up.service_start_time = date[0] follow_up.service_end_time = date[1] follow_up.service_time_long = (date[1].to_date - date[0].to_date).to_i end follow_up.staff = @current_admin last_follow_up.assign_follow_ups.each do |d| follow_up.assign_follow_ups.build(staff_id: d.staff_id) end if follow_up.save! if last_follow_up.present? last_follow_up.key_person.each do |d| key_person = d.dup key_person.follow_up_id = follow_up.id key_person.save end last_follow_up.money_plans.each do |d| money = d.dup money.staff = @current_admin money.follow_up_id = follow_up.id money.save end end contract_lists = EducodeSales::ContractDateList.clazzs.invert data.each_with_index do |s, i| if s.present? s.each do |d| follow_up.contract_date_lists.create!({promise_date: d[0], num: d[1], plan_date: d[2], actual_date: d[3], staff_id: @current_admin.id, clazz: contract_lists[i]}) end end end follow_up.follow_up_trends.create(clazz: '跟进信息', content: params[:content], staff_id: @current_admin.id) @business.update(last_follow_up_id: follow_up.id) render_success else render_failure follow_up end end def advise followup = FollowUp.find(params[:id]) followup.follow_up_trends.create(clazz: '建议信息', content: params[:content], staff_id: @current_admin.id) render_success end def assign role = Role.find_by( name: '售后工程师') staffs = Staff.joins(:user).where(role_id: role.id) business = Business.find(params[:id]) selected_staff_ids = business.assign_staffs.pluck(:staff_id) gon.sales_staffs = staffs.map { |d| {name: d.user.real_name, value: d.id, selected: selected_staff_ids.include?(d.id) } } render layout: false end def product_list 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) @staffs = staffs.map { |d| [d.user.real_name, d.id]} gon.staffs = staffs.map { |d| {name: d.user.real_name, value: d.id } } gon.business_step = Common.where(clazz: 'business_step', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).order("position").map do |d| {value: d.id, name: d.name, selected: false} end end format.json do @product_catalogs = ProductCatalog @product_catalogs = @product_catalogs.where("name like ?", "%#{params[:q][:name]}%") if params[:q] && params[:q][:name].present? @product_catalogs = @product_catalogs.where(item_clazz: params[:q][:item_clazz]) if params[:q] && params[:q][:item_clazz].present? @product_catalogs = @product_catalogs.page(params[:page]).per(params[:limit]) end end end def assign_staff @business = Business.find(params[:id]) assign_staffs = [] params[:to_id].split(",").each do |i| assign_staffs << AssignStaff.find_or_initialize_by(sourcable_id: @business.id, staff_id: i, sourcable_type: "EducodeSales::Business") end @business.assign_staffs = assign_staffs @business.save render_success end def new_product render layout: false end def product_catalog product_catalog = ProductCatalog.new(product_up_params) if product_catalog.save render_success else render_failure product_catalog end end def update_product_catalog product_catalog = ProductCatalog.find(params[:id]) if product_catalog.update(product_up_params) render_success else render_failure product_catalog end end def edit_product @product_catalog = ProductCatalog.find(params[:id]) render layout: false end def show_follow_record @follow_up = FollowUp.find(params[:follow_up_id]) @business = Business.find(@follow_up.business_id) @show_follow_recored_deployment = EducodeSales::FollowUp::BUSINESS_DEPLOYMENT.to_h.invert[@follow_up.o_business_deployment] @trend = FollowUpTrend.find(params[:trend_id]) id = @follow_up.id @follow_ups = @business.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 new_sales_detail render layout: false end def select_product render layout: false end def sales_detail sales_detail = SalesDetail.new(sales_detail_params) sales_detail.staff = @current_admin if sales_detail.save render_success else render_failure sales_detail end end def upload_products xlsx = Roo::Spreadsheet.open(params[:file]) ods = xlsx.sheet(0) rows = ods.last_row - 1 rows.times do |r| #行数 next unless ods.row(r+2)[0] EducodeSales::ProductCatalog.create(name: ods.row(r+2)[0].to_s.strip, item_clazz: ods.row(r+2)[1].to_s.strip, brand: ods.row(r+2)[2].to_s.strip, specification: ods.row(r+2)[3].to_s.strip, unit: ods.row(r+2)[4].to_s.strip, param: ods.row(r+2)[5].to_s.strip, source_method: ods.row(r+2)[6].to_s.strip, supplier: ods.row(r+2)[7].to_s.strip) end render json: { succcess: true} end private def follow_up_params params.permit(:o_business_deployment, :plan_signed_date, :bidded_days, :signed_date, :signed_department_id, :service_years, :service_start_time, :service_end_time, :funding_source, :signed_clazz) end def product_up_params params.permit(:name, :item_clazz, :brand, :specification, :unit, :param, :source_method, :supplier) end def sales_detail_params params.permit(:amount, :price, :total_price, :custom_clazz, :delivery_date, :product_catalog_id, :business_id, :proprietorship) end end end