Class: EducodeSales::BusinessesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/educode_sales/businesses_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin, #authenticate_request, #current_user, #filter, #is_commissioner_above?, #paginate, #render_failure, #render_success, #subject_members, #subject_staffs, #subject_url

Methods included from ApplicationHelper

#add_businesses_score, #base_url, #collection_amount_score, #completion_rate, #current?, #disk_filename, #get_businesses_chart, #handled_data, #handled_time_data, #handled_time_data_accurate, #relative_path, #signed_amount_score, #storage_path, #url_to_avatar, #visits_score

Instance Method Details

#add_export_recordsObject



810
811
812
813
814
815
816
817
818
819
820
821
# File 'app/controllers/educode_sales/businesses_controller.rb', line 810

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_keysObject



661
662
663
# File 'app/controllers/educode_sales/businesses_controller.rb', line 661

def add_keys
  render layout: false
end

#auditObject



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'app/controllers/educode_sales/businesses_controller.rb', line 632

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

#contractObject



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
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
# File 'app/controllers/educode_sales/businesses_controller.rb', line 1085

def contract
  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
      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

#createObject



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
# File 'app/controllers/educode_sales/businesses_controller.rb', line 479

def create
  department = Department.find(params[:department_id])
  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] = '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
  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],
    sourcable_type: params[:sourcable_type], sourcable_id: params[:sourcable_id], source_way: params[:source_way])

  if business.save
    render_success
  else
    render_failure business
  end
end

#daily_paperObject



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'app/controllers/educode_sales/businesses_controller.rb', line 460

def daily_paper
  @followups = EducodeSales::FollowUp.where(staff_id: @current_admin.id).where("created_at > ?", Time.now.beginning_of_day).order("created_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

#destroyObject



613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'app/controllers/educode_sales/businesses_controller.rb', line 613

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

#editObject



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'app/controllers/educode_sales/businesses_controller.rb', line 513

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_recordObject



752
753
754
755
756
757
758
759
760
761
762
763
764
765
# File 'app/controllers/educode_sales/businesses_controller.rb', line 752

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_planObject



767
768
769
# File 'app/controllers/educode_sales/businesses_controller.rb', line 767

def edit_plan
  render layout: false
end

#export_recordsObject



800
801
802
803
804
805
806
807
808
809
# File 'app/controllers/educode_sales/businesses_controller.rb', line 800

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

#fileObject



771
772
773
774
# File 'app/controllers/educode_sales/businesses_controller.rb', line 771

def file
  gon.folder = edu_setting('attachment_folder') + '/'
  render layout: false
end

#followedObject



1072
1073
1074
1075
1076
1077
1078
# File 'app/controllers/educode_sales/businesses_controller.rb', line 1072

def followed
  if follow_business(@current_admin.user_id,params[:business_id])
    render json: {msg:'确定取消关注?'}
  else
    render json: {msg:'确定关注?'}
  end
end

#followingObject

关注接口



1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
# File 'app/controllers/educode_sales/businesses_controller.rb', line 1032

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_businessesObject

我已关注的商机



1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'app/controllers/educode_sales/businesses_controller.rb', line 1050

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_dataObject



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
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
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
# File 'app/controllers/educode_sales/businesses_controller.rb', line 823

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

      if params[:is_area].present?
        # 区域管理商机
        # 排查看区域商机,需要排除掉其它人员手上的监管学校
        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)
      end

      if params[:special].present?
        # 专项商机
        school_tag_ids = @current_admin.staff_school_tags.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)
      end

      if params[:delete_list].present?
        @businesses = Business.unscoped.where(state_id: [1, 3])
      end

      if params[:plan_year].present?
        if @current_admin.is_admin?
          @businesses = Business
        else
          # 年度计划,按负责区域和专项客户类型查看对应的年度商机
          school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id)
          if @current_admin.staff_school_tags.present?
            # 如果设置专项客户类型,则视为专项经理,根据专项客户查看对应商机
            school_ids += School.joins(:school_tags).where("school_tags.id in (?)", @current_admin.staff_school_tags.pluck(:school_tag_id)).pluck(:id)
          end
          @businesses = Business.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id").where("departments.school_id in (?) ", school_ids.uniq)
        end

        @businesses = @businesses.joins(:last_follow_up).where("educode_sales_follow_ups.plan_a_date > :year OR educode_sales_follow_ups.plan_return_date > :year OR educode_sales_follow_ups.invitation_at > :year", year: Time.now.beginning_of_year)
      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

#indexObject



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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
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
453
454
455
456
457
458
# File 'app/controllers/educode_sales/businesses_controller.rb', line 8

def index
  authorize! :read, Business
  respond_to do |format|
    format.html do
      common = Common.find_by(clazz: 'staff_type', name: '销售')
      @staffs = Staff.joins(:user).where(job_type: common.id).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.menus = []
      gon.assign_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

      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.menus << { title: '添加周计划', event: 'week' }
        gon.menus << { title: '添加月计划', event: 'month' }
        gon.menus << { title: '添加年计划', event: 'year' }
      end

      if can?(:assign, EducodeSales::SalePlan)
        gon.assign_menus << { title: '指派周计划', event: 'week' }
        gon.assign_menus << { title: '指派月计划', event: 'month' }
        gon.assign_menus << { title: '指派年计划', event: 'year' }
      end

      gon.menus << { title: '添加回款计划', event: 'money_plan' }
      gon.assign_menus << { title: '添加回款计划', event: 'money_plan' }

      gon.menus << { title: '关注', event: 'following' }
      gon.menus << { title: '跟进时间线', event: 'time_line' }

      gon.assign_menus << { title: '关注', event: 'following' }
      gon.assign_menus << { title: '跟进时间线', event: 'time_line' }
      if can?(:show_file, EducodeSales::Business)
        gon.menus << { title: '附件管理', event: 'file' }
        gon.assign_menus << { title: '附件管理', event: 'file' }
      end
      if can?(:update, EducodeSales::Business)
        # gon.menus << { title: '编辑', event: 'edit' }
      end
      if can?(:destroy, EducodeSales::Business)
        gon.menus << { title: '删除', event: 'delete' }
        gon.assign_menus << { title: '删除', event: 'delete' }
      end
      if can?(:export_business, EducodeSales::Business)
        gon.export_menus << { title: '导出到Csv文件', event: 'export_csv' }
        gon.export_menus << { title: '导出到Excel文件', event: 'export_excel' }
      end
      gon.export_menus << { 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' || params[:q][:clazz] == 'plan_year')
        if params[:q][:clazz] == 'special'
          school_ids = []
          # 专项商机
          school_tag_ids = @current_admin.staff_school_tags.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])
        elsif params[:q][:clazz] == 'plan_year'
          if @current_admin.is_admin?
            @businesses = Business
          else
            # 年度计划,按负责区域和专项客户类型查看对应的年度商机
            school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id)
            if @current_admin.staff_school_tags.present?
              # 如果设置专项客户类型,则视为专项经理,根据专项客户查看对应商机
              school_ids += School.joins(:school_tags).where("school_tags.id in (?)", @current_admin.staff_school_tags.pluck(:school_tag_id)).pluck(:id)
            end
            @businesses = Business.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id").where("departments.school_id in (?) ", school_ids.uniq)
          end
          
          @businesses = @businesses.joins(:last_follow_up).where("educode_sales_follow_ups.plan_a_date > :year OR educode_sales_follow_ups.plan_return_date > :year OR educode_sales_follow_ups.invitation_at > :year", year: Time.now.beginning_of_year)
        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.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
      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] + ' 00:00:00', 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][:plan_return_date].present?
        date = params[:q][:plan_return_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.plan_return_date >= ? AND educode_sales_follow_ups.plan_return_date <= ?", date[0] + ' 00:00:00', date[1] + ' 23:59:59')
      end
    
      if params[:q].present? && params[:q][:plan_a_date].present?
        date = params[:q][:plan_a_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.plan_a_date >= ? AND educode_sales_follow_ups.plan_a_date <= ?", 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].present? && params[:q][:source_way].present?
        if params[:q].present? && params[:q][:source_name].present?
          if ["会议活动", "渠道代理", "客户"].include?(params[:q][:source_way])
            if params[:q][:source_way] == '会议活动'
              sourcable_id = EducodeSales::Activity.where("name like ?", "%#{params[:q][:source_name]}%").limit(20).pluck(:id)
            elsif params[:q][:source_way]  == '渠道代理'
              sourcable_id = EducodeSales::Place.where("name like ?", "%#{params[:q][:source_name]}%").limit(20).pluck(:id)
            elsif params[:q][:source_way]  == '客户'
              sourcable_id = Department.where("name like ?", "%#{params[:q][:source_name]}%").limit(20).pluck(:id)
            end

            if sourcable_id.present?
              @businesses = @businesses.where(source_way: params[:q][:source_way], sourcable_id: sourcable_id)
            else
              @businesses = @businesses.none
            end
          else
            @businesses = @businesses.where(source_way: params[:q][:source_way]).where("sourcable_type like ?", "%#{params[:q][:source_name]}%")
          end
        else
          @businesses = @businesses.where(source_way: params[:q][:source_way])
        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.plan_a_date,
        last_follow.plan_a_money,
        last_follow.invitation_money,
        last_follow.plan_return_date,
        last_follow.plan_return_money,
        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_personObject



1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
# File 'app/controllers/educode_sales/businesses_controller.rb', line 1376

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

#newObject



508
509
510
511
# File 'app/controllers/educode_sales/businesses_controller.rb', line 508

def new
  @clazz = Common.where(clazz: 'business_type').order("position").pluck(:name, :id)
  render layout: false
end

#new_follow_recordObject



694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'app/controllers/educode_sales/businesses_controller.rb', line 694

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

#searchObject



1080
1081
1082
1083
# File 'app/controllers/educode_sales/businesses_controller.rb', line 1080

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_followObject



665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'app/controllers/educode_sales/businesses_controller.rb', line 665

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_recordObject



718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'app/controllers/educode_sales/businesses_controller.rb', line 718

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_keysObject



657
658
659
# File 'app/controllers/educode_sales/businesses_controller.rb', line 657

def show_keys
  render layout: false
end

#time_lineObject



778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
# File 'app/controllers/educode_sales/businesses_controller.rb', line 778

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_plansObject



713
714
715
716
# File 'app/controllers/educode_sales/businesses_controller.rb', line 713

def unfinish_plans
  load_business
  @plans = @business.sale_plans.where.not(finish_rate: 100).page(params[:page]).per(params[:limit])
end

#updateObject



547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
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
600
601
602
# File 'app/controllers/educode_sales/businesses_controller.rb', line 547

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] = '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_adviseObject



1366
1367
1368
1369
1370
1371
1372
1373
1374
# File 'app/controllers/educode_sales/businesses_controller.rb', line 1366

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

#updateMoneyObject



604
605
606
607
608
609
610
611
# File 'app/controllers/educode_sales/businesses_controller.rb', line 604

def updateMoney
  business = Business.find(params[:id])
  if business.update(return_money: params[:return_money])
    render_success
  else
    render_failure business
  end
end

#upload_fileObject



775
776
777
# File 'app/controllers/educode_sales/businesses_controller.rb', line 775

def upload_file
  render layout: false
end