Class: EducodeSales::MoneyPlansController

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

#addObject



91
92
93
94
95
96
97
98
99
# File 'app/controllers/educode_sales/money_plans_controller.rb', line 91

def add
  if params[:business_id].present?
    @business = Business.find(params[:business_id])
    gon.business_id_source = @business.id
  else
    gon.business_id_source = ""
  end
  render layout: false
end

#createObject



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

def create
  business = Business.find(params[:business_id])
  data = []
  week = Time.now.strftime('%W').to_i
  current_week = ""
  
  (params[:num].to_i+1).times do |i|
    _week = params["date_at[#{i}]"].to_date.strftime('%W').to_i
    current_week = _week if _week == week
    business.money_plans.create(category: params["category[#{i}]"], amount: params["amount[#{i}]"], payment_clause: params["payment_clause[#{i}]"], date_at: params["date_at[#{i}]"], follow_up_id: business.last_follow_up_id, staff_id: @current_admin.id )
  end
  if current_week.present?
    staff_manage_ids = business&.last_follow_up&.assign_follow_ups.present? ? business.last_follow_up.assign_follow_ups.pluck(:staff_id) : [business.staff_id]
    common_id = EducodeSales::Common.find_by(clazz: '计划类型', name: '回款计划')&.id
    staff_manage_ids.each do |staff_id|
      EducodeSales::SalePlan.create(month: Time.now.beginning_of_month, weekly: week, content: "提醒:请补充回款计划内容!", business: business, staff_id: staff_id, finish_rate: '0', common_id: common_id )
      EducodeSales::SalePlan.create(month: Time.now.beginning_of_month, content: "提醒:请补充回款计划内容!", business: business, staff_id: staff_id, finish_rate: '0', common_id: common_id )
    end
  end
  render_success
end

#destroyObject



143
144
145
146
147
# File 'app/controllers/educode_sales/money_plans_controller.rb', line 143

def destroy
  money_plan = MoneyPlan.find(params[:id])
  money_plan.destroy
  render_success
end

#editObject



101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/educode_sales/money_plans_controller.rb', line 101

def edit
  @money_plan = MoneyPlan.find(params[:id])
  gon.business_id = @money_plan.business_id
  gon.business = [{value: @money_plan.business_id.to_s, name: @money_plan.business&.name}]

  gon.edit_money_plan = can?(:update, EducodeSales::MoneyPlan)
  gon.edit_money_plan_self = can?(:update_self, EducodeSales::MoneyPlan)
  gon.staff_id = @current_admin.id
  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
# File 'app/controllers/educode_sales/money_plans_controller.rb', line 6

def index
  authorize! :read, MoneyPlan
  respond_to do |format|
    format.html do
     
    end
    format.json do
      last_follow_up_ids = Business.all.pluck(:last_follow_up_id)
      @money_plans = MoneyPlan.where(follow_up_id: last_follow_up_ids).joins(:business).left_joins(:money_plan_claims).group("educode_sales_money_plans.id").select("educode_sales_money_plans.*, SUM(educode_sales_money_plan_claims.amount) AS return_money, COUNT(educode_sales_money_plan_claims.id) AS claim_num")
      if @current_admin.is_admin?
      else
        level = @current_admin.role.role_areas.find_by(clazz: '回款管理').level
        case level
        when '自己'
          @money_plans = @money_plans.where(staff_id: @current_admin.id)
        when '区域'
          school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id)
          if @current_admin.staff_school_tags.present?
            # 如果设置专项客户类型,则视为专项经理,根据专项客户查看对应商机
            school_ids += School.joins(:school_tags).where("school_tags.id in (?)", @current_admin.staff_school_tags.pluck(:school_tag_id)).pluck(:id)
          end
          b_ids = Business.where(school_id: school_ids.uniq).pluck(:id)
          @money_plans = @money_plans.where("educode_sales_businesses.id in (?) OR educode_sales_money_plans.staff_id = ?", b_ids, @current_admin.id)
        end
      end

      if params[:q].present? && params[:q][:staff_manages].present?
        # 销售经理
        @money_plans = @money_plans.joins(:business).joins("
          LEFT JOIN educode_sales_follow_ups AS last_follow_up ON educode_sales_businesses.last_follow_up_id = last_follow_up.id AND last_follow_up.deleted_at IS NULL
          LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = 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][:staff_manages], params[:q][:staff_manages])
      end

      if params[:q].present? && params[:q][:business].present?
        @money_plans = @money_plans.where("educode_sales_businesses.name like ?", "%#{params[:q][:business]}%")
      end
      if params[:q].present? && params[:q][:school].present?
        @money_plans = @money_plans.joins(business: :school).where("schools.name like ?", "%#{params[:q][:school]}%")
      end

      if params[:q].present? && params[:q][:property].present?
        # 客户类型
        @money_plans = @money_plans.joins(business: [department: [school: :school_tags]]).where("school_tags.id = ?", params[:q][:property])
      end

      if params[:q] && params[:q][:date_at].present?
        date = params[:q][:date_at].split(" - ")
        @money_plans = @money_plans.where("date_at >= ? AND date_at <= ?", date[0], date[1])
      end

      if params[:sort].present? && params[:sort][:field]
        @money_plans = @money_plans.order("#{params[:sort][:field]} #{params[:sort][:order]}")
      else
        @money_plans = @money_plans.order(date_at: :desc)
      end

      if params[:page].present?
        @money_plans = @money_plans.page(params[:page]).per(params[:limit])
      end
      @money_plans = @money_plans
    end
  end
end

#listObject



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

def list
  # follow_up_ids = Business.pluck(:last_follow_up_id)
  authorize! :read, MoneyPlan
  respond_to do |format|
    format.html do
      common = Common.find_by(clazz: 'staff_type', name: '销售')
      @staffs = Staff.joins(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id]}

      gon.staffs = Staff.joins(:user).where(job_type: common.id).map { |d| {name: d.user.real_name, value: d.id } }
      gon.edit_money_plan = can?(:update, EducodeSales::MoneyPlan)
      gon.edit_money_plan_self = can?(:update_self, EducodeSales::MoneyPlan)
      gon.staff_id = @current_admin.id
      if params[:date_at].present? && params[:date_at] != '全部'
        gon.date = Time.new(params[:date_at]).beginning_of_year.strftime("%Y-%m-%d") + " - " + Time.new(params[:date_at]).end_of_year.strftime("%Y-%m-%d")
      end
    end
  
  end
end

#updateObject



112
113
114
115
116
117
118
119
# File 'app/controllers/educode_sales/money_plans_controller.rb', line 112

def update
  @money_plan = MoneyPlan.find(params[:id])
  if @money_plan.update(money_plan_params)
    render_success
  else
    render_failure @money_plan
  end
end