Class: EducodeSales::OperationReportsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/educode_sales/operation_reports_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

#auditObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/controllers/educode_sales/operation_reports_controller.rb', line 138

def audit
  @sale_report = OperationReport.find(params[:id])
  @name = @sale_report.staff.user.real_name
  d = @sale_report.staff
  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
  render layout: false
end

#createObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/educode_sales/operation_reports_controller.rb', line 51

def create
  if params[:month].present?
    sale_report = @current_admin.operation_reports.build(month: Date.today.beginning_of_month)
  else
    sale_report = @current_admin.operation_reports.build(month: Date.today.beginning_of_month, weekly: Time.now.strftime('%W').to_i)
  end

  sale_report.finish_rate = params[:finish_rate]
  sale_report.client = params[:client]
  sale_report.content = params[:content]
  if sale_report.save
    render_success
  else
    render_failure sale_report
  end
end

#destroyObject



85
86
87
88
89
# File 'app/controllers/educode_sales/operation_reports_controller.rb', line 85

def destroy
  sale_report = OperationReport.find(params[:id])
  sale_report.destroy
    render_success
end

#editObject



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

def edit
  @sale_report = OperationReport.find(params[:id])
  @name = @sale_report.staff.user.real_name
  d = @sale_report.staff
  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
  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
# File 'app/controllers/educode_sales/operation_reports_controller.rb', line 6

def index
  # authorize! :read, SalePlan
  respond_to do |format|
    format.html do
    end
    format.json do
      if @current_admin.is_admin?
        @sale_reports = OperationReport
      else
        level = @current_admin.role.role_areas.find_by(clazz: '销售计划').level
        case level
        when '自己'
          @sale_reports = OperationReport.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)
          @sale_reports = OperationReport.where("staff_id in (?) OR educode_sales_operation_reports.staff_id = ?", staff_ids, @current_admin.id)
        else
          @sale_reports = OperationReport
        end
      end
      if params[:clazz] == 'week'
        @sale_reports = @sale_reports.where.not(weekly: nil)
      else
        @sale_reports = @sale_reports.where(weekly: nil)
      end
      if params[:q].present? && params[:q][:staff_id].present?
        @sale_reports = @sale_reports.where(staff_id: params[:q][:staff_id])
      end
      if params[:q].present? && params[:q][:year].present?
        @sale_reports = @sale_reports.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_reports = @sale_reports.where("month = ?", "#{params[:q][:month]}-01 00:00:00".to_date)
      end
      if params[:sort].present? && params[:sort][:field]
        @sale_reports = @sale_reports.order("#{params[:sort][:field]} #{params[:sort][:order]}")
      else
        @sale_reports = @sale_reports.order("created_at desc")
      end
      @sale_reports = @sale_reports.page(params[:page]).per(params[:limit])
    end
  end
end

#plansObject



155
156
157
158
# File 'app/controllers/educode_sales/operation_reports_controller.rb', line 155

def plans
  sale_report = OperationReport.find(params[:id])
  @sale_plans = OperationPlan.where(staff_id: sale_report.staff_id, month: sale_report.month, weekly: sale_report.weekly).order("updated_at desc").page(params[:page]).per(params[:limit])
end

#showObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/educode_sales/operation_reports_controller.rb', line 68

def show
  @sale_report = OperationReport.find(params[:id])
  @name = @sale_report.staff.user.real_name
  d = @sale_report.staff
  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
  render layout: false
end

#updateObject



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

def update
  sale_report = OperationReport.find(params[:id])
  sale_report.content = params[:content]
  if sale_report.save
    render_success
  else
    render_failure sale_report
  end
end

#update_auditObject



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

def update_audit
  authorize! :audit, EducodeSales::OperationReport
  sale_report = OperationReport.find(params[:id])
  sale_report.assign_attributes(audit_params)
  if sale_report.level1_opinion_changed? || sale_report.level1_score_changed?
    sale_report.audit1_user_id = @current_admin.id
  end
  if sale_report.level2_opinion_changed? || sale_report.level2_score_changed?
    sale_report.audit2_user_id = @current_admin.id
  end
  if sale_report.level3_opinion_changed? || sale_report.level3_score_changed?
    sale_report.audit3_user_id = @current_admin.id
  end
  if sale_report.save
    render_success
  else
    render_failure sale_report
  end
end