Class: EducodeSales::PlansController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin, #authenticate_request, #current_user, #filter, #render_failure, #render_success

Instance Method Details

#createObject



61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/educode_sales/plans_controller.rb', line 61

def create
  plan = @current_admin.sale_plans.build(plan_params)
  plan.month = params["month"] + "-1"
  plan.business = Business.find_by(id: params[:business_id])
  if plan.save
    render_success
  else
    render_failure plan
  end
end

#destroyObject



198
199
200
201
202
203
204
205
# File 'app/controllers/educode_sales/plans_controller.rb', line 198

def destroy
  sale_plan = SalePlan.find(params[:id])
  if sale_plan.soft_destroy(@current_admin.id)
    render_success
  else
    render_failure sale_plan
  end
end

#edit_monthObject



211
212
213
214
215
216
217
218
219
# File 'app/controllers/educode_sales/plans_controller.rb', line 211

def edit_month
  business_ids = EducodeSales::AssignFollowUp.joins(:follow_up).where(staff_id: @current_admin.id).pluck :business_id
  @businesses = Business.includes(:department, :last_follow_up).where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", business_ids, @current_admin.id).order(created_at: :desc)
  @businesses = @businesses.map { |b| [b.name + "(" + b.department.school.name + ")" + "---" + "最新跟进:" + (b.last_follow_up.present? ? b.last_follow_up.created_at.to_s : b.created_at.to_s), b.id] }
  @sale_plan = SalePlan.find(params[:id])
  @finish_rates = []
  11.times { |d| @finish_rates << ["#{d*10}%", d * 10]}
  render layout: false
end

#edit_monthlyObject



221
222
223
# File 'app/controllers/educode_sales/plans_controller.rb', line 221

def edit_monthly
  render layout: false
end

#edit_weekObject



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

def edit_week
  business_ids = EducodeSales::AssignFollowUp.joins(:follow_up).where(staff_id: @current_admin.id).pluck :business_id
  @businesses = Business.includes(:department, :last_follow_up).where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", business_ids, @current_admin.id).order(created_at: :desc)
  @businesses = @businesses.map { |b| [b.name + "(" + b.department.school.name + ")" + "---" + "最新跟进:" + (b.last_follow_up.present? ? b.last_follow_up.created_at.to_s : b.created_at.to_s), b.id] }
  @sale_plan = SalePlan.find(params[:id])
  if @sale_plan.weekly.present?
    week = Time.now.strftime('%W').to_i
    if week > 3
      @weeks = [week+2, week+1, week, week - 1, week - 2]
    elsif week == 2
      @weeks = [week, week - 1]
    else
      @weeks = [week]
    end
    @weeks << @sale_plan.weekly
    @weeks = @weeks.uniq.sort
    @weeks = @weeks.map do |d|
      if d == week
        ["#{d}  本周", d]
      else
        [d, d]
      end
    end
  end
  @months = [Time.now.strftime("%Y-%m"), Time.now.months_ago(1).strftime("%Y-%m")]
  @months << @sale_plan.month.strftime("%Y-%m")
  @months = @months.uniq.sort
  @finish_rates = []
  11.times { |d| @finish_rates << ["#{d * 10}%", d * 10] }
  render layout: false
end

#edit_weeklyObject



207
208
209
# File 'app/controllers/educode_sales/plans_controller.rb', line 207

def edit_weekly
  render layout: false
end

#indexObject



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

def index
  # authorize! :read, SalePlan
  respond_to do |format|
    format.html do
      common = Common.find_by(clazz: 'staff_type', name: '销售')
      @staffs = Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] }
      @years = (1..(Time.now.year - 2020)).map { |d| 2020 + d }
      @months = (1..12).map { |d| d }
    end
    format.json do
      if @current_admin.is_admin?
        @sale_plans = SalePlan
      else
        level = @current_admin.role.role_areas.find_by(clazz: '销售计划').level
        case level
        when '自己'
          @sale_plans = SalePlan.where(staff_id: @current_admin.id)
        when '区域'
          staff_ids = Staff.joins(user: [user_extension: [department: :school]]).where("schools.province in (?)", @current_admin.areas.pluck(:name)).pluck(:id)
          business_ids = Business.where(school_id: StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id)).pluck(:id)
          @sale_plans = SalePlan.where("staff_id in (?) OR educode_sales_sale_plans.staff_id = ? OR educode_sales_sale_plans.business_id in (?)", staff_ids, @current_admin.id,business_ids)
        else
          @sale_plans = SalePlan
        end
      end
      if params[:clazz] == 'week'
        @sale_plans = @sale_plans.where.not(weekly: nil)
      else
        @sale_plans = @sale_plans.where(weekly: nil)
      end
      if params[:q].present? && params[:q][:staff_id].present?
        @sale_plans = @sale_plans.where(staff_id: params[:q][:staff_id])
      end
      # p "#{params[:q][:year]}-01-01 00:00:00".to_date, "#{params[:q][:year]}-12-31 23:59:00".to_date
      if params[:q].present? && params[:q][:year].present?
        @sale_plans = @sale_plans.where("month >= ? and month <= ?", "#{params[:q][:year]}-01-01 00:00:00".to_date, "#{params[:q][:year]}-12-31 23:59:00".to_date)
      end
      if params[:q].present? && params[:q][:month].present?
        @sale_plans = @sale_plans.where("month = ?", "#{params[:q][:month]}-01 00:00:00".to_date)
      end
      if params[:sort].present? && params[:sort][:field]
        @sale_plans = @sale_plans.order("#{params[:sort][:field]} #{params[:sort][:order]}")
      else
        @sale_plans = @sale_plans.order("created_at desc")
      end

      @sale_plans = @sale_plans.page(params[:page]).per(params[:limit])
    end
  end
end

#my_monthObject



235
236
237
238
239
240
241
242
243
# File 'app/controllers/educode_sales/plans_controller.rb', line 235

def my_month
  @sale_plans = SalePlan.where(staff_id: @current_admin.id, month: Time.now.strftime("%Y-%m") + "-01 00:00:00", weekly: nil)
  if  params[:sort].present? && params[:sort][:field]
    @sale_plans = @sale_plans.order("#{params[:sort][:field]} #{params[:sort][:order]}")
  else
    @sale_plans = @sale_plans.order("updated_at desc")
  end
  @sale_plans =  @sale_plans.page(params[:page]).per(params[:limit])
end

#my_weekObject



225
226
227
228
229
230
231
232
233
# File 'app/controllers/educode_sales/plans_controller.rb', line 225

def my_week
  @sale_plans = SalePlan.where(staff_id: @current_admin.id, month: Time.now.strftime("%Y-%m") + "-01 00:00:00", weekly: Time.now.strftime('%W').to_i)
  if  params[:sort].present? && params[:sort][:field]
    @sale_plans = @sale_plans.order("#{params[:sort][:field]} #{params[:sort][:order]}")
  else
    @sale_plans = @sale_plans.order("updated_at desc")
  end
  @sale_plans =  @sale_plans.page(params[:page]).per(params[:limit])
end

#newObject



57
58
59
# File 'app/controllers/educode_sales/plans_controller.rb', line 57

def new
  render layout: false
end

#new_monthObject



110
111
112
113
114
115
116
# File 'app/controllers/educode_sales/plans_controller.rb', line 110

def new_month
  @business = Business.find_by(id: params[:business_id])
  business_ids = EducodeSales::AssignFollowUp.joins(:follow_up).where(staff_id: @current_admin.id).pluck :business_id
  @businesses =Business.includes(:department, :last_follow_up).where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", business_ids, @current_admin.id).order(created_at: :desc)
  @businesses = @businesses.map { |b| [b.name + "(" + b.department.school.name + ")" + "---" + "最新跟进:" + (b.last_follow_up.present? ? b.last_follow_up.created_at.to_s : b.created_at.to_s), b.id] }
  render layout: false
end

#new_monthlyObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/controllers/educode_sales/plans_controller.rb', line 118

def new_monthly
  d = @current_admin
  area_ids = EducodeSales::Common.where(clazz: 'area').ids.sort.to_s
  if d.areas.present?
    if d.areas.ids.sort.to_s == area_ids
      @area = '全国'
    else
      @area =  d.areas.pluck(:name).join("")
    end
  else
    @area =  ''
  end
  @name = @current_admin.user.real_name
  sale_plans = SalePlan.where(month: Time.now.strftime("%Y-%m") + "-01 00:00:00", staff_id: @current_admin.id, weekly: nil)
  @rate = sale_plans.present? ? (sale_plans.sum("finish_rate") / sale_plans.size) : 0
  @departments_count = sale_plans.joins(:business).count("distinct(department_id)")
  @total_amount = sale_plans.joins(business: :last_follow_up).distinct.sum("total_amount")
  render layout: false
end

#new_weekObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/educode_sales/plans_controller.rb', line 73

def new_week
  @business = Business.find_by(id: params[:business_id])
  @week = Time.now.strftime('%W').to_i
  week = Time.now.strftime('%W').to_i
  if week > 3
    @weeks = [[week+2,week+2], [week+1,week+1], [week.to_s + '  本周',week], [week - 1,week - 1], [week - 2,week - 2]]
  elsif week == 2
    @weeks = [[week.to_s + '  本周',week], [week - 1,week - 1]]
  else
    @weeks = [[week.to_s + '  本周',week]]
  end
  business_ids = EducodeSales::AssignFollowUp.joins(:follow_up).where(staff_id: @current_admin.id).pluck :business_id
  @businesses = Business.includes(:department, :last_follow_up).where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", business_ids, @current_admin.id).order(created_at: :desc)
  @businesses = @businesses.map { |b| [b.name + "(" + b.department.school.name + ")" + "---" + "最新跟进:" + (b.last_follow_up.present? ? b.last_follow_up.created_at.to_s : b.created_at.to_s), b.id] }
  render layout: false
end

#new_weeklyObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/educode_sales/plans_controller.rb', line 90

def new_weekly
  d = @current_admin
  area_ids = EducodeSales::Common.where(clazz: 'area').ids.sort.to_s
  if d.areas.present?
    if d.areas.ids.sort.to_s == area_ids
      @area = '全国'
    else
      @area =  d.areas.pluck(:name).join("")
    end
  else
    @area =  ''
  end
  @name = @current_admin.user.real_name
  sale_plans = SalePlan.where(month: Time.now.strftime("%Y-%m") + "-01 00:00:00", weekly: Time.now.strftime('%W').to_i, staff_id: @current_admin.id)
  @rate = sale_plans.present? ? (sale_plans.sum("finish_rate") / sale_plans.size) : 0
  @departments_count = sale_plans.joins(:business).count("distinct(department_id)")
  @total_amount = sale_plans.joins(business: :last_follow_up).distinct.sum("total_amount")
  render layout: false
end

#show_monthObject



147
148
149
150
# File 'app/controllers/educode_sales/plans_controller.rb', line 147

def show_month
  @sale_plan = SalePlan.find(params[:id])
  render layout: false
end

#show_monthlyObject



152
153
154
# File 'app/controllers/educode_sales/plans_controller.rb', line 152

def show_monthly
  render layout: false
end

#show_weekObject



138
139
140
141
# File 'app/controllers/educode_sales/plans_controller.rb', line 138

def show_week
  @sale_plan = SalePlan.find(params[:id])
  render layout: false
end

#show_weeklyObject



143
144
145
# File 'app/controllers/educode_sales/plans_controller.rb', line 143

def show_weekly
  render layout: false
end

#updateObject



188
189
190
191
192
193
194
195
196
# File 'app/controllers/educode_sales/plans_controller.rb', line 188

def update
  sale_plan = SalePlan.find(params[:id])
  params["month"] = params["month"] + "-1"
  if sale_plan.update(plan_params)
    render_success
  else
    render_failure sale_plan
  end
end