Class: EducodeSales::SaleReportsController

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/controllers/educode_sales/sale_reports_controller.rb', line 143

def audit
  @sale_report = SaleReport.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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/educode_sales/sale_reports_controller.rb', line 6

def create
  if params[:month].present?
    sale_report = @current_admin.sale_reports.build(month: Date.today.beginning_of_month)
  else
    sale_report = @current_admin.sale_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]
  sale_report.amount = params[:amount]
  if sale_report.save
    render_success
  else
    render_failure sale_report
  end
end

#destroyObject



41
42
43
44
45
46
47
48
# File 'app/controllers/educode_sales/sale_reports_controller.rb', line 41

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

#editObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/educode_sales/sale_reports_controller.rb', line 96

def edit
  @sale_report = SaleReport.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



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

def index
  # authorize! :read, SalePlan
  respond_to do |format|
    format.html do
    end
    format.json do
      if @current_admin.is_admin?
        @sale_reports = SaleReport
      else
        level = @current_admin.role.role_areas.find_by(clazz: '销售计划').level
        case level
        when '自己'
          @sale_reports = SaleReport.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 = SaleReport.where("staff_id in (?) OR educode_sales_sale_reports.staff_id = ?", staff_ids, @current_admin.id)
        else
          @sale_reports = SaleReport
        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



160
161
162
163
# File 'app/controllers/educode_sales/sale_reports_controller.rb', line 160

def plans
  sale_report = SaleReport.find(params[:id])
  @sale_plans = SalePlan.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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/educode_sales/sale_reports_controller.rb', line 24

def show
  @sale_report = SaleReport.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



113
114
115
116
117
118
119
120
121
# File 'app/controllers/educode_sales/sale_reports_controller.rb', line 113

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

#update_auditObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/controllers/educode_sales/sale_reports_controller.rb', line 123

def update_audit
  authorize! :audit, EducodeSales::SaleReport
  sale_report = SaleReport.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