Class: EducodeSales::BusinessesController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- EducodeSales::BusinessesController
- Defined in:
- app/controllers/educode_sales/businesses_controller.rb
Instance Method Summary collapse
- #add_export_records ⇒ Object
- #add_keys ⇒ Object
- #audit ⇒ Object
- #contract ⇒ Object
- #create ⇒ Object
- #daily_paper ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #edit_follow_record ⇒ Object
- #edit_plan ⇒ Object
- #export_records ⇒ Object
- #file ⇒ Object
- #followed ⇒ Object
-
#following ⇒ Object
关注接口.
-
#following_businesses ⇒ Object
我已关注的商机.
- #get_export_data ⇒ Object
- #index ⇒ Object
- #key_person ⇒ Object
- #new ⇒ Object
- #new_follow_record ⇒ Object
- #search ⇒ Object
- #show_follow ⇒ Object
- #show_follow_record ⇒ Object
- #show_keys ⇒ Object
- #time_line ⇒ Object
- #unfinish_plans ⇒ Object
- #update ⇒ Object
- #update_advise ⇒ Object
- #updateMoney ⇒ Object
- #upload_file ⇒ Object
Methods inherited from ApplicationController
#authenticate_admin, #authenticate_request, #current_user, #filter, #render_failure, #render_success
Instance Method Details
#add_export_records ⇒ Object
717 718 719 720 721 722 723 724 725 726 727 728 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 717 def add_export_records if params[:type] == 'csv' record = BusinessExportRecord.new(name: '商机数据表.csv', staff_id: @current_admin.id) else params[:type] == 'excel' record = BusinessExportRecord.new(name: '商机数据表.xls', staff_id: @current_admin.id) end if record.save render_success else render_failure business end end |
#add_keys ⇒ Object
568 569 570 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 568 def add_keys render layout: false end |
#audit ⇒ Object
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 539 def audit business = Business.unscoped.find(params[:id]) if params[:clazz] == 'destroy' if params[:op] == 'pass' business.update(state_id: 2) business.sale_plans.each do |d| EducodeSales::Recycle.create(source: d, deleter_id: @current_admin.id) end EducodeSales::Recycle.create(source: business, deleter_id: @current_admin.id) else business.update(state_id: 0, delete_reason: '', deleted_at: nil) SalePlan.unscoped.where(business_id: business.id).update_all(deleted_at: nil) FollowUp.unscoped.where(business_id: business.id).update_all(deleted_at: nil) end else if params[:op] == 'pass' business.update(state_id: 0) else business.clazz_id = EducodeSales::Common.find_by(extras: 'a_class').id business.update(state_id: 0) end end render_success end |
#contract ⇒ Object
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 956 def contract :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. = [] gon. = [] 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..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 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][: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 if params[:q].present? && params[:q][:contract].present? 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) 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? 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][: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 |
#create ⇒ Object
405 406 407 408 409 410 411 412 413 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 405 def create department = Department.find(params[:department_id]) business = @current_admin.businesses.build(name: params[:name], department_id: department.id, source: params[:source], school_id: department.school_id, clazz_id: params[:clazz_id]) if business.save render_success else render_failure business end end |
#daily_paper ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 386 def daily_paper @followups = EducodeSales::FollowUp.where(staff_id: @current_admin.id).where("updated_at > ?", Time.now.beginning_of_day).order("updated_at") ab = EducodeSales::Common.where(extras: [EducodeSales::Common::ATYPE, EducodeSales::Common::BTYPE]).pluck(:id) business = EducodeSales::FollowUp.joins(" JOIN educode_sales_businesses ON educode_sales_businesses.id = educode_sales_follow_ups.business_id JOIN educode_sales_follow_ups AS last_up ON last_up.id = educode_sales_businesses.last_follow_up_id ").where("educode_sales_businesses.staff_id = ? AND educode_sales_follow_ups.staff_id = ? AND last_up.clazz_id IN (?) AND last_up.staff_id = #{@current_admin.id}", @current_admin.id, @current_admin.id, ab) @month = business.where("educode_sales_follow_ups.created_at >= ?", Time.now.beginning_of_month).count("DISTINCT(educode_sales_follow_ups.business_id)") @quarter = business.where("educode_sales_follow_ups.created_at >= ?", Time.now.beginning_of_quarter).count("DISTINCT(educode_sales_follow_ups.business_id)") @year = business.where("educode_sales_follow_ups.created_at >= ?", Time.now.beginning_of_year).count("DISTINCT(educode_sales_follow_ups.business_id)") @activities = EducodeSales::Activity.where(staff_id: @current_admin.id).where("created_at >= ?", Time.now.beginning_of_month).count @teachers = EducodeSales::Activity.joins(:teachers).where(staff_id: @current_admin.id).where("educode_sales_activities.created_at >= ?", Time.now.beginning_of_month).count @schools = EducodeSales::Activity.joins(teachers: :department).where(staff_id: @current_admin.id).where("educode_sales_activities.created_at >= ?", Time.now.beginning_of_month).count("DISTINCT(departments.school_id)") render layout: false end |
#destroy ⇒ Object
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 520 def destroy business = Business.find(params[:id]) if ['a_class', 'b_class'].include?(business.clazz&.extras) if params[:content].present? business.update(deleted_at: Time.now, state_id: 1, delete_reason: params[:content]) business.sale_plans.update_all(deleted_at: Time.now) business.follow_ups.update_all(deleted_at: Time.now) else render_failure '请输入删除商机原因' end else business.soft_destroy(@current_admin.id) render_success end rescue ActiveRecord::DeleteRestrictionError => e render_failure '该商机已其它关联数据产生,暂不能删除' end |
#edit ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 420 def edit @business = Business.find(params[:id]) levels = @business.business_levels.pluck(:level_id) staff_ids = @business.business_watches.pluck(:staff_id) gon.is_secret = levels.include?("保密型") || levels.include?("战略型") gon.levels = EducodeSales::BusinessLevel.level_ids.keys.map { |d| {name: d, value: d, selected: levels.include?(d)}} gon.watche_ids = Staff.all.map { |d| {name: d.user&.real_name, value: d.id, selected: staff_ids.include?(d.id)}} gon.department = {value: @business.department_id, name: "#{@business.department&.school&.name}-#{@business.department&.name}"} gon.value = @business.department_id if ['会议活动', '渠道代理', '客户'].include?(@business.source_way) && @business.sourcable.present? if @business.source_way == '客户' gon.sourcable = [{name: "#{@business.sourcable.school.name} #{@business.sourcable.name}", value: @business.sourcable_id}] else gon.sourcable = [{name: @business.sourcable.name, value: @business.sourcable_id}] end gon.sourcable_id = @business.sourcable_id elsif @business.sourcable_type.present? gon.sourcable = [{value: @business.sourcable_type, name: @business.sourcable_type}] gon.sourcable_id = @business.sourcable_type else gon.sourcable = [] gon.sourcable_id = '' end gon.sourcable_type = @business.sourcable_type gon.source_way = @business.source_way render layout: false end |
#edit_follow_record ⇒ Object
659 660 661 662 663 664 665 666 667 668 669 670 671 672 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 659 def edit_follow_record @years = (2015..Time.now.year).to_a.reverse @o_name = EducodeSales::Common.find_by(extras: EducodeSales::Common::OTYPE)&.name @deployment_type = EducodeSales::FollowUp::BUSINESS_DEPLOYMENT @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 gon.place = @follow_up.place.present? ? [{ value: @follow_up.place&.id, name: @follow_up.place&.name }] : [value: '', name: ''] render layout: false end |
#edit_plan ⇒ Object
674 675 676 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 674 def edit_plan render layout: false end |
#export_records ⇒ Object
707 708 709 710 711 712 713 714 715 716 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 707 def export_records respond_to do |format| format.html do render layout: false end format.json do @records = BusinessExportRecord.all.order(id: :desc).page(params[:page]).per(params[:limit]) end end end |
#file ⇒ Object
678 679 680 681 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 678 def file gon.folder = edu_setting('attachment_folder') + '/' render layout: false end |
#followed ⇒ Object
943 944 945 946 947 948 949 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 943 def followed if follow_business(@current_admin.user_id,params[:business_id]) render json: {msg:'确定取消关注?'} else render json: {msg:'确定关注?'} end end |
#following ⇒ Object
关注接口
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 903 def following if brs=follow_business(@current_admin.user_id,params[:business_id]) if brs.delete render json: {code:200,msg:"已取消关注"} else render json: {code:300,msg:"操作失败"} end else brs=BusinessRelationShip.new(user_id:@current_admin.user_id, business_id:params[:business_id]) if brs.save render json: {code:200,msg:"关注成功"} else render json: {code:300,msg:"操作失败"} end end end |
#following_businesses ⇒ Object
我已关注的商机
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 921 def following_businesses p=[] BusinessRelationShip.select(:business_id).where(user_id:@current_admin.user_id).to_a.each { |d| p << d.business_id} @businesses=Business.where(id:p) @businesses=@businesses.select(" educode_sales_businesses.*, last_follow.budget_amount, last_follow.invitation_at, last_follow.reception_at, last_follow.service_time_long, last_follow.service_end_time, last_follow.service_start_time, last_follow.bidded_date, last_follow.signed_date, last_follow.created_at as latest_time, last_follow.actual_amount, (last_follow.actual_amount - educode_sales_businesses.return_money) as wait_return_money, 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 |
#get_export_data ⇒ Object
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 730 def get_export_data respond_to do |format| format.html do 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[:signed_date].present? date = params[: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[:date_at].present? date = params[: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[:bidded_date].present? date = params[: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[: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[:business_year]) end if params[: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[:year]) end if params[:all].present? @businesses = @businesses end if params[: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 if params[:is_follow].present? # 关注 my_ids = BusinessRelationShip.where(user_id:@current_admin.user_id).pluck(:business_id) @businesses = @businesses.where(id: my_ids) end @businesses = @businesses if params[:name].present? # && params[:name] != "(销售态势-" + @year + "-已中标商机)" && params[:name] != "(销售态势-" + @year + "-已签单商机)" && params[:name] != "(销售态势-" + @year + "-已回款商机)" && params[:name] != "(销售态势-" + @year + "-现有商机)"&& params[:name] != "(销售态势-" + @year + "-应收款商机)" @businesses = @businesses.where("educode_sales_businesses.name like ?", "%#{params[:name]}%") end if params[:department].present? departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:department]}%").pluck(:id) @businesses = @businesses.joins(:department).where("departments.id in (?)", departments_ids) end if params[:staff_id].present? part_a_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is null AND educode_sales_businesses.staff_id = ?",params[: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[:staff_id]).ids business_ids = part_a_ids + part_b_ids @businesses = EducodeSales::Business.where(id:business_ids) end if params[:business_type].present? @businesses = @businesses.where("educode_sales_businesses.clazz_id in (?)", params[:business_type].split(",").map(&:to_i)) end if params[: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[:business_step].split(",").map(&:to_i)) end if params[: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[:place_id]) end if params[:area].present? p = EducodeSales::Common.find(params[: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[:date].present? date = params[: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[:invitation_at].present? date = params[: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[: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.budget_amount, last_follow.reception_at, last_follow.service_time_long, last_follow.service_end_time, last_follow.service_start_time, last_follow.bidded_date, last_follow.signed_date, 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_count ").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_count, 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(last_follow_up: [:clazz, :assign_follow_ups]) end end end |
#index ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 8 def index :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).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 = can?(:create, EducodeSales::SalePlan) || can?(:update, EducodeSales::Business) || can?(:destroy, EducodeSales::Business) || can?(:show_file, EducodeSales::Business) @more = true gon. = [] gon. = [] 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 gon.business_step = Common.where(clazz: 'business_step').order("position").map do |d| {value: d.id, name: d.name, selected: business_step.include?(d.name)} end if can?(:create, EducodeSales::SalePlan) # gon.menus << { title: '添加团队建议', event: 'suggest' } gon. << { title: '添加周计划', event: 'week' } gon. << { title: '添加月计划', event: 'month' } gon. << { title: '添加年计划', event: 'year' } end gon. << { title: '关注', event: 'following' } gon. << { title: '跟进时间线', event: 'time_line' } if can?(:show_file, EducodeSales::Business) gon. << { title: '附件管理', event: 'file' } end if can?(:update, EducodeSales::Business) # gon.menus << { title: '编辑', event: 'edit' } end if can?(:destroy, EducodeSales::Business) gon. << { title: '删除', event: 'delete' } end if can?(:export_business, EducodeSales::Business) gon. << { title: '导出到Csv文件', event: 'export_csv' } gon. << { title: '导出到Excel文件', event: 'export_excel' } end gon. << { title: '导出记录', event: 'export_records' } 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 params[:q] && (params[:q][:clazz] == 'area' || params[:q][:clazz] == 'special' || params[:q][:clazz] == 'delete_list') if params[:q][:clazz] == 'special' school_ids = [] # 专项商机 school_tag_ids = @current_admin..pluck(:school_tag_id) school_ids = SchoolTagMiddle.where(school_tag_id: school_tag_ids).pluck(:school_id) @businesses = Business.joins("JOIN departments AS dp ON educode_sales_businesses.department_id = dp.id").where("dp.school_id in (?)", school_ids) elsif params[:q][:clazz] == 'area' school_ids = [] # 区域管理商机 # 排查看区域商机,需要排除掉其它人员手上的监管学校 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 @businesses = Business.joins("JOIN departments AS dp ON educode_sales_businesses.department_id = dp.id").where("dp.school_id in (?)", school_ids) elsif params[:q][:clazz] == 'delete_list' @businesses = Business.unscoped.where(state_id: [1, 3]) end else 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..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 end 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][:department].present? if params[:q][:department].include?("---") # 从客户管理进来按部门显示商机 school_name, department = params[:q][:department].split("---") if department departments_ids = Department.joins(:school).where("schools.name = ? AND departments.name = ?", school_name, department).pluck(:id) @businesses = @businesses.joins(:department).where("departments.id in (?)", departments_ids) else school = School.find_by(name: school_name) @businesses = @businesses.where("educode_sales_businesses.school_id = ?", school&.id) end else 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 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).where(state_id: [1,3]) 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 if params[:q].present? && params[:q][:contract].present? 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) 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? 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][: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[:q].present? && params[:q][:property].present? # 客户类型 @businesses = @businesses.joins(department: [school: :school_tags]).where("school_tags.id = ?", params[:q][:property]) # @businesses = @businesses.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id # JOIN schools ON departments.school_id = schools.id").where("schools.school_property_id = ?", params[:q][:property]) end if params[:q].present? && params[:q][:staff_manages].present? # 销售经理 @businesses = @businesses.joins("LEFT JOIN educode_sales_follow_ups AS last_follow_up ON educode_sales_businesses.last_follow_up_id = last_follow_up.id AND last_follow_up.deleted_at IS NULL LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = 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][:staff_manages], params[:q][:staff_manages]) end @business_count = @businesses.distinct.count 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 # mysql -select watch_sql = " " unless @current_admin.is_admin? # 保密和战略商机,只有自己,指定人员或管理员才有权限查看 watch_sql = " LEFT JOIN educode_sales_business_watches ON educode_sales_business_watches.business_id = educode_sales_businesses.id LEFT JOIN educode_sales_business_levels ON educode_sales_business_levels.business_id = educode_sales_businesses.id" @businesses = @businesses.where("educode_sales_business_levels.id IS NULL OR (educode_sales_business_levels.id IS NOT NULL AND educode_sales_businesses.staff_id = #{@current_admin.id}) OR educode_sales_business_levels.level_id NOT IN (1,2) OR (educode_sales_business_levels.level_id IN (1, 2) AND educode_sales_business_watches.staff_id = #{@current_admin.id})") end if params[:q][:level_id].present? if @current_admin.is_admin? watch_sql = " JOIN educode_sales_business_levels ON educode_sales_business_levels.business_id = educode_sales_businesses.id" @businesses = @businesses.where("educode_sales_business_levels.level_id = ?", params[:q][:level_id]) else watch_sql = " JOIN educode_sales_business_watches ON educode_sales_business_watches.business_id = educode_sales_businesses.id JOIN educode_sales_business_levels ON educode_sales_business_levels.business_id = educode_sales_businesses.id AND educode_sales_business_levels.level_id = #{params[:q][:level_id].to_i}" @businesses = @businesses.where("(educode_sales_businesses.staff_id = #{@current_admin.id}) OR educode_sales_business_levels.level_id NOT IN (1,2) OR (educode_sales_business_levels.level_id IN (1, 2) AND educode_sales_business_watches.staff_id = #{@current_admin.id})" ) end 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 #{watch_sql} ").includes(:users, last_follow_up: [:clazz, :assign_follow_ups]).page(params[:page]).per(params[:limit]) end end end |
#key_person ⇒ Object
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 1247 def key_person respond_to do |format| format.html do render layout: false end format.json do @teachers = EducodeSales::KeyPerson.where(follow_up_id: params[:follow_up_id]).page(params[:page]).per(params[:limit]) end end end |
#new ⇒ Object
415 416 417 418 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 415 def new @clazz = Common.where(clazz: 'business_type').order("position").pluck(:name, :id) render layout: false end |
#new_follow_record ⇒ Object
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 601 def new_follow_record load_business @years = (2015..Time.now.year).to_a.reverse @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) @deployment_type = EducodeSales::FollowUp::BUSINESS_DEPLOYMENT @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 gon.place = @last_follow_up.place.present? ? [{ value: @last_follow_up.place&.id, name: @last_follow_up.place&.name }] : [] end render layout: false end |
#search ⇒ Object
951 952 953 954 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 951 def search @business = Business.where.not(id: params[:id]).order("created_at desc") @business = @business.where("educode_sales_businesses.name like ?", "%#{params[:q]}%").limit(30) if params[:q].present? end |
#show_follow ⇒ Object
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 572 def show_follow load_business respond_to do |format| format.html do unless @current_admin.is_admin? levels = @business.business_levels.pluck(:level_id) staff_ids = @business.business_watches.pluck(:staff_id) if levels.include?("保密型") || levels.include?("战略型") if !staff_ids.include?(@current_admin.id) && @business.staff_id != @current_admin.id && @business.last_follow_up&.staff_id != @current_admin.id return render "no_permission", layout: false end end end 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 |
#show_follow_record ⇒ Object
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 625 def show_follow_record follow_up_id = params[:follow_up_id] @follow_up = FollowUp.find(follow_up_id) @business = Business.find(@follow_up.business_id) # 保密商机限制查看 unless @current_admin.is_admin? levels = @business.business_levels.pluck(:level_id) staff_ids = @business.business_watches.pluck(:staff_id) if levels.include?("保密型") || levels.include?("战略型") if !staff_ids.include?(@current_admin.id) && @business.staff_id != @current_admin.id && @business.last_follow_up&.staff_id != @current_admin.id return render "no_permission", layout: false end end end id = @follow_up.id @follow_ups = @business.follow_ups ids = @follow_ups.ids size = ids.size ids = ids.reverse index = ids.index(id) EducodeSales::FollowUp::BUSINESS_DEPLOYMENT[1..-1].each do |m| if @follow_up.o_business_deployment == m[1] @show_follow_recored_deployment = m[0] break end end @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 |
#show_keys ⇒ Object
564 565 566 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 564 def show_keys render layout: false end |
#time_line ⇒ Object
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 685 def time_line @business = Business.find(params[:business_id]) respond_to do |format| format.html do # 保密商机限制查看 unless @current_admin.is_admin? levels = @business.business_levels.pluck(:level_id) staff_ids = @business.business_watches.pluck(:staff_id) if levels.include?("保密型") || levels.include?("战略型") if !staff_ids.include?(@current_admin.id) && @business.staff_id != @current_admin.id && @business.last_follow_up&.staff_id != @current_admin.id return render "no_permission", layout: false end end end @follow_ups = @business.follow_ups.order(created_at: :desc) @latest = @follow_ups.present? ? @follow_ups[0] : nil render layout: false end end end |
#unfinish_plans ⇒ Object
620 621 622 623 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 620 def unfinish_plans load_business @plans = @business.sale_plans.where.not(finish_rate: 100).page(params[:page]).per(params[:limit]) end |
#update ⇒ Object
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 454 def update business = Business.find(params[:id]) department = Department.find(params[:department_id]) if @current_admin.is_admin? if params[:level_ids] levels = [] params[:level_ids].split(",").each do |d| levels << business.business_levels.find_or_initialize_by(level_id: d) end business.business_levels = levels if params[:watch_ids].blank? || !params[:level_ids].split(",").include?('保密型') && !params[:level_ids].split(",").include?('战略型') business.business_watches.destroy_all else staffs = [] params[:watch_ids].split(",").each do |d| staffs << business.business_watches.find_or_initialize_by(staff_id: d) end business.business_watches = staffs end end end if params[:sourcable_type].present? if params[:source_way] == '会议活动' params[:sourcable_type] = 'EducodeSales::Activity' elsif params[:source_way] == '渠道代理' params[:sourcable_type] = 'EducodeSales::Place' elsif params[:source_way] == '客户' params[:sourcable_type] = 'EducodeSales::Department' else params[:sourcable_id] = "" end end if ['会议活动', '渠道代理', '客户'].include?(params[:source_way] ) && params[:sourcable_id].blank? params[:sourcable_type] = "" end if params[:sourcable_type].blank? params[:sourcable_id] = "" end if business.update(name: params[:name], department_id: department.id, source: params[:source], school_id: department.school_id, sourcable_type: params[:sourcable_type], sourcable_id: params[:sourcable_id], source_way: params[:source_way] ) if params[:merge_business_ids].present? merge_business = Business.where(id: params[:merge_business_ids].split(",")) merge_business.each do |b| b.follow_ups.update_all(origin_business_id: b.id, business_id: business.id) b.update(deleted_at: Time.now) end business.update(follow_ups_count: EducodeSales::FollowUp.where(business_id: business.id).size) end render_success else render_failure business end end |
#update_advise ⇒ Object
1237 1238 1239 1240 1241 1242 1243 1244 1245 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 1237 def update_advise business = Business.find(params[:id]) if business.last_follow_up.present? business.last_follow_up.update(advise: params[:content]) render_success else render_failure '无跟进动态,暂不能添加建议' end end |
#updateMoney ⇒ Object
511 512 513 514 515 516 517 518 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 511 def updateMoney business = Business.find(params[:id]) if business.update(return_money: params[:return_money]) render_success else render_failure business end end |
#upload_file ⇒ Object
682 683 684 |
# File 'app/controllers/educode_sales/businesses_controller.rb', line 682 def upload_file render layout: false end |