Class: EducodeSales::SalesDetailsController

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

#destroyObject



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

def destroy
  sales_detail = SalesDetail.find(params[:id])
  if sales_detail.destroy
    render_success
  else
    render_failure sales_detail
  end
end

#editObject



119
120
121
122
123
# File 'app/controllers/educode_sales/sales_details_controller.rb', line 119

def edit
  @sale_detail = SalesDetail.find(params[:id])
  @name = [@sale_detail.business.name, @sale_detail.product_catalog.item_clazz, @sale_detail.product_catalog.brand, @sale_detail.product_catalog.specification, @sale_detail.product_catalog.unit, @sale_detail.product_catalog.source_method, @sale_detail.product_catalog.param, @sale_detail.product_catalog.supplier].join(" ")
  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
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.extras)}
      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

#updateObject



125
126
127
128
129
130
131
132
# File 'app/controllers/educode_sales/sales_details_controller.rb', line 125

def update
  sale_detail = SalesDetail.find(params[:id])
  if sale_detail.update(sales_detail_params)
    render_success
  else
    render_failure sale_detail
  end
end

#uploadObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/controllers/educode_sales/sales_details_controller.rb', line 134

def upload
  xlsx = Roo::Spreadsheet.open(params[:file])
  ods = xlsx.sheet(0)
  rows = ods.last_row - 1
  rows.times do |r|  #行数
    next unless ods.row(r+2)[0]
    business = EducodeSales::Business.find_by(number: ods.row(r+2)[0].to_s.strip)
    product_catalog = EducodeSales::ProductCatalog.find_by(name: ods.row(r+2)[5].to_s.strip,
    item_clazz: ods.row(r+2)[6].to_s.strip, 
    brand: ods.row(r+2)[7].to_s.strip, 
    specification: ods.row(r+2)[8].to_s.strip, 
    unit: ods.row(r+2)[9].to_s.strip,
    source_method: ods.row(r+2)[13].to_s.strip,
    supplier:  ods.row(r+2)[15].to_s.strip)
    if business.present? && product_catalog.present?
      SalesDetail.create(staff_id: @current_admin.id, business_id: business.id, product_catalog_id: product_catalog.id, price: ods.row(r+2)[11].to_s.strip, amount: ods.row(r+2)[10].to_s.strip, total_price: ods.row(r+2)[12].to_s.strip, custom_clazz: ods.row(r+2)[14].to_s.strip, delivery_date: ods.row(r+2)[16].to_s.strip, proprietorship: ods.row(r+2)[17].to_s.strip)
    end
  end
  
  render json: { succcess: true}
end