Class: EducodeSales::AssessmentsSetting

Inherits:
ApplicationRecord show all
Defined in:
app/models/educode_sales/assessments_setting.rb

Constant Summary collapse

CLAZZ =
{
 7 => '商机目标',  18 => '中标目标', 1 => '签单目标', 2 => '回款目标', 3 => '商机跟进', 4 => '商机挖掘-A类商机', 5 => '商机挖掘-B类商机', 6 => '客户拜访-线下拜访', 8 => '客户拜访-线上拜访',
 9 => '注册数据-学生', 10 => '注册数据-老师', 11 => '会议组织-区域性会议', 12 => '会议组织-单校会议', 13 => '生态合作-虚拟教研室', 14 => '生态合作-特软', 15 => '生态合作-大V合作',
 16 => '重要事项', 17 => '临时任务'
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.stats_by_business(year) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'app/models/educode_sales/assessments_setting.rb', line 88

def self.stats_by_business(year)
   # 商机目标完成情况
    ids = EducodeSales::Common.where(extras: %w[a_class b_class c_class d_class e_class]).pluck(:id)
    data = EducodeSales::Business.joins(:last_follow_up).where("educode_sales_follow_ups.clazz_id in (?) ",ids)
                                        .where("educode_sales_follow_ups.created_at >= ? and educode_sales_follow_ups.created_at <= ?", "#{year}-01-01 00:00:00".to_date, "#{year}-12-31 23:59:59".to_date)
                                        .group("educode_sales_follow_ups.staff_id, MONTH(educode_sales_follow_ups.created_at)").select("SUM(budget_amount) AS value, educode_sales_follow_ups.staff_id, MONTH(educode_sales_follow_ups.created_at) AS month")
                                        .group_by{|d| d.staff_id }
    self.update_assessment_value(7, year, data)
end

.stats_by_contracts(year) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'app/models/educode_sales/assessments_setting.rb', line 99

def self.stats_by_contracts(year)
  # 中标目标
    contract_ids = EducodeSales::Common.where(clazz: '商机阶段', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).pluck(:id)
    data = EducodeSales::Business.joins(:last_follow_up).where("educode_sales_follow_ups.stage_id in (?) ", contract_ids)
    .where("educode_sales_follow_ups.created_at >= ? and educode_sales_follow_ups.created_at <= ?", "#{year}-01-01 00:00:00".to_date, "#{year}-12-31 23:59:59".to_date)
    .group("educode_sales_follow_ups.staff_id, MONTH(educode_sales_follow_ups.created_at)").select("SUM(actual_amount) AS value, educode_sales_follow_ups.staff_id, MONTH(educode_sales_follow_ups.created_at) AS month")
    .group_by{|d| d.staff_id }
    self.update_assessment_value(18, year, data)
end

.stats_by_return_money(year) ⇒ Object



109
110
111
112
113
114
115
116
# File 'app/models/educode_sales/assessments_setting.rb', line 109

def self.stats_by_return_money(year)
  # 回款目标完成
    data = EducodeSales::MoneyPlanClaim.joins(:money_plan)
                                .where("educode_sales_money_plans.created_at >= ? and educode_sales_money_plans.created_at <= ?", "#{year}-01-01 00:00:00".to_date, "#{year}-12-31 23:59:59".to_date)
                                .group("educode_sales_money_plans.staff_id, MONTH(educode_sales_money_plans.created_at)").select("SUM(educode_sales_money_plan_claims.amount) AS value, educode_sales_money_plans.staff_id, MONTH(educode_sales_money_plans.created_at) AS month")
                                .group_by{|d| d.staff_id }
    self.update_assessment_value(2, year, data)
end

.update_assessment_value(assessment_id, year, data) ⇒ Object



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
# File 'app/models/educode_sales/assessments_setting.rb', line 44

def self.update_assessment_value(assessment_id, year, data)
  assessments = EducodeSales::AssessmentsSetting.where(assessment: assessment_id).where("assessment_year >= ? and assessment_year <= ?", "#{year}-01-01 00:00:00".to_date, "#{year}-12-31 23:59:00".to_date)
  assessments.each do |ass|
   if data[ass.staff_id].present?
    data_count = []
    data[ass.staff_id].each do |d|
      data_count[d['month']-1] = d['value']
    end

    data_count[12] = data_count[0].to_f  + data_count[1].to_f  + data_count[2].to_f
    data_count[13] = data_count[3].to_f  + data_count[4].to_f  + data_count[5].to_f
    data_count[14] = data_count[6].to_f  + data_count[7].to_f  + data_count[8].to_f
    data_count[15] = data_count[9].to_f  + data_count[10].to_f  + data_count[11].to_f
    data_count[16] = data_count[12].to_f + data_count[13].to_f + data_count[14].to_f + data_count[15].to_f
    ass.update(annual_progress: data_count[16],
      first_quarter_progress: data_count[12],
      second_quarter_progress: data_count[13],
      third_quarter_progress: data_count[14], 
      fourth_quarter_progress: data_count[15],
      january_progress: data_count[0],
      february_progress: data_count[1],
      march_progress: data_count[2], 
      april_progress: data_count[3], 
      may_progress: data_count[4],
      june_progress: data_count[5],
      july_progress: data_count[6],
      august_progress: data_count[7],
      september_progress: data_count[8],
      october_progress: data_count[9],
      november_progress: data_count[10],
      december_progress: data_count[11])
   end
  end
end

.update_stats(year, assessment_id) ⇒ Object



79
80
81
82
83
84
85
86
# File 'app/models/educode_sales/assessments_setting.rb', line 79

def self.update_stats(year, assessment_id)
  Rails.cache.fetch("stats_by_assessment_#{year}", :expires_in => 1.days) do
    self.stats_by_return_money(year)
    self.stats_by_business(year)
    self.stats_by_contracts(year)
    true
  end
end

Instance Method Details

#unit_nameObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/educode_sales/assessments_setting.rb', line 18

def unit_name
  case self.assessment
  when 1, 2, 7, 18
    '万元'
  when 3
    ''
  when 4, 5
    ''
  when 6, 8, 11, 12
    ''
  when 9, 10, 15
    ''
  when 16, 17
    ''
  when 13, 14
    '个人'
  end
end