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
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
|
# File 'app/controllers/educode_sales/sales_details_controller.rb', line 6
def index
respond_to do |format|
format.js do
common = Common.find_by(clazz: 'staff_type', name: '销售')
staffs = Staff.joins(:user).where(job_type: common.id).where.not(role_id: 11)
@staffs = staffs.map { |d| [d.user.real_name, d.id]}
gon.place = params[:place_id].present? ? [{ value: params[:place_id], name: EducodeSales::Place.find(params[:place_id]).name }] : []
gon.staffs = staffs.map { |d| {name: d.user.real_name, value: d.id } }
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
gon.business_type = Common.where(clazz: 'business_type').map do |d|
{value: d.id, name: d.name, selected: bussiness_type.include?(d.)}
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
end
format.json do
@sales_details = SalesDetail
@sales_details = @sales_details.joins(:business).includes(:business, :product_catalog)
if params[:q] && params[:q][:signed_date].present?
date = params[:q][:signed_date].split(" - ")
@sales_details = @sales_details.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][:bidded_date].present?
date = params[:q][:bidded_date].split(" - ")
@sales_details = @sales_details.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][:name].present?
@sales_details = @sales_details.where("educode_sales_businesses.name like ?", "%#{params[:q][:name]}%")
end
if params[:sort].present? && params[:sort][:field]
@sales_details = @sales_details.order("#{params[:sort][:field]} #{params[:sort][:order]}")
else
@sales_details = @sales_details.order("educode_sales_sales_details.created_at desc")
end
if params[:q].present? && params[:q][:business_step].present?
@sales_details = @sales_details.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][:sales_staff_id].present?
@sales_details = @sales_details.joins("LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = educode_sales_businesses.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][:sales_staff_id], params[:q][:sales_staff_id])
end
if params[:q].present? && params[:q][:department].present?
departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:q][:department]}%").pluck(:id)
@sales_details = @sales_details.joins(business: :department).where("departments.id in (?)", departments_ids)
end
if params[:q].present? && params[:q][:area].present?
area = EducodeSales::Common.find(params[:q][:area]).name
if @current_admin.is_admin?
@sales_details = @sales_details.joins("
JOIN departments ON educode_sales_businesses.department_id = departments.id
JOIN schools ON departments.school_id = schools.id
").where("province = ?", area)
else
level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level
if level == "区域"
@sales_details = @sales_details.joins("
JOIN schools ON departments.school_id = schools.id
").where("province = ?", area)
else
@sales_details = @sales_details.joins("
JOIN departments ON educode_sales_businesses.department_id = departments.id
JOIN schools ON departments.school_id = schools.id
").where("province = ?", area)
end
end
end
@sales_details = @sales_details.page(params[:page]).per(params[:limit])
end
end
end
|