Class: CustomersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/customers_controller.rb

Instance Method Summary collapse

Methods included from SessionsHelper

#preferences_customer_type=, #preferences_customer_type?, #signed_in?, #signed_in_user, #store_location

Instance Method Details

#createObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/customers_controller.rb', line 47

def create
  if (params[:customer]==nil)
    return
  end
  
  @customer = Customer.new(params[:customer])
  @person = CustomerPj.new(params[:customer_pj])

  @customer.person = @person
  
  update_advanced_fields
  
  if @customer.save
    flash[:success] = t("helpers.forms.new_sucess")
    redirect_to customer_path(@customer)
  else
    authorize! :new, @customer, 'new.'+preferences_customer_type?.to_s
    render 'new.'+preferences_customer_type?.to_s
  end
end

#custom_load_creatorObject



109
110
111
112
# File 'app/controllers/customers_controller.rb', line 109

def custom_load_creator
  params[:customer_pj] = params[:customer][:customer_pj]
  params[:customer].delete :customer_pj
end

#disableObject



29
30
31
# File 'app/controllers/customers_controller.rb', line 29

def disable
  
end

#editObject



86
87
88
89
90
91
# File 'app/controllers/customers_controller.rb', line 86

def edit
  @customer = Customer.find params[:id]
  @person = @customer.person
  
  @customer.emails.build
end

#filter_before_changesObject



93
94
95
96
97
98
# File 'app/controllers/customers_controller.rb', line 93

def filter_before_changes
  authorize! params[:action].to_sym, @customer
  
  params[:customer][:doc].gsub! /[\.\/-]/, "" if params[:customer][:doc]
  params[:customer][:doc_rg].gsub! /[\.\/-]/, "" if params[:customer][:doc_rg] 
end

#indexObject



10
11
12
13
14
15
16
# File 'app/controllers/customers_controller.rb', line 10

def index
  @sels = params["sels"] || []
  @search = Customer.search(params[:search])
  #@customers = Customer.search_by_name(@customers, params[:name]).paginate(page: params[:page], :per_page => 5)
  @customers = @search.paginate(page: params[:page], :per_page => 10)
  params[:search] = {} if params[:search].nil?
end

#multiselect_business_activitiesObject



105
106
107
# File 'app/controllers/customers_controller.rb', line 105

def multiselect_business_activities
  render :json => BusinessActivity.where(["name ilike ?", "%"+params[:tag]+"%"] ).collect { |c| { :key => c.id.to_s, :value => c.name } }
end

#multiselect_business_segmentsObject



101
102
103
# File 'app/controllers/customers_controller.rb', line 101

def multiselect_business_segments
  render :json => BusinessSegment.where(["name ilike ?", "%"+params[:tag]+"%"] ).collect { |c| { :key => c.id.to_s, :value => c.name } }
end

#multiselect_customers_pjObject



114
115
116
# File 'app/controllers/customers_controller.rb', line 114

def multiselect_customers_pj
  render :json => CustomerPj.includes(:customer).where(["(customers.name ilike ?  or customers.name_sec ilike ?)", params[:tag]+"%", params[:tag]+"%"] ).collect { |c| { :key => c.id.to_s, :value => c.customer.name } }
end

#newObject



33
34
35
36
37
38
# File 'app/controllers/customers_controller.rb', line 33

def new
  @person = CustomerPj.new
  @customer.person = @person
  @segments = BusinessSegment.all
  render "new."+preferences_customer_type?.to_s
end

#showObject



18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/customers_controller.rb', line 18

def show
  @task = @customer.tasks.build
  @tasks = @customer.tasks.paginate(page:params[:task_page] || 1, per_page: 4)
  
  @selected_department = params[:department]
  @contacts = @customer.contacts
  @contacts = Contact.search_by_params @contacts, department_id: @selected_department if @selected_department
  
  render "show."+@customer.person.prefix
end

#updateObject



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

def update
  
  params_pj = params[:customer][:customer_pj]
  params[:customer].delete :customer_pj
  
  #@customer = Customer.find params[:id]
  @person = @customer.person
  
  update_advanced_fields
  
  if @person.save && @customer.update_attributes(params[:customer]) && @person.update_attributes(params_pj)
    flash[:success] = t("helpers.forms.new_sucess")
    redirect_to customer_path(@customer)
  else
    render 'new.'+preferences_customer_type?.to_s
  end
end

#update_advanced_fieldsObject



40
41
42
43
44
45
# File 'app/controllers/customers_controller.rb', line 40

def update_advanced_fields    
  @person.segments = params[:segments_select] ? params[:segments_select].collect { |bsid| BusinessSegment.find bsid }.uniq : []
  @person.activities = params[:activities_select] ? params[:activities_select].collect { |baid| BusinessActivity.find baid }.uniq : []
  
  @person.associateds = params[:associateds_select] ? params[:associateds_select].collect { |assoc| CustomerPj.find assoc }.uniq : []    
end