Class: ErpApp::Organizer::Crm::BaseController

Inherits:
BaseController show all
Defined in:
app/controllers/erp_app/organizer/crm/base_controller.rb

Constant Summary collapse

@@date_format =
"%m/%d/%Y"
@@datetime_format =
"%m/%d/%Y %l:%M%P"

Instance Method Summary collapse

Methods inherited from BaseController

#get_preferences, #index, #setup_preferences, #update_preferences

Instance Method Details

#activateObject



189
190
191
192
193
194
195
196
197
198
199
200
# File 'app/controllers/erp_app/organizer/crm/base_controller.rb', line 189

def activate
  if @user = User.load_from_activation_token(params[:activation_token])
    @user.activate!
    success = true
    message = 'User was successfully activated.'
  else
    success = false
    message = "Invalid activation token."
  end

  render :json => {:success => success, :message => message} and return
end

#contact_mechanismObject



31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/erp_app/organizer/crm/base_controller.rb', line 31

def contact_mechanism
  render :inline => if request.post?
                      create_contact_mechanism
                    elsif request.put?
                      update_contact_mechanism
                    elsif request.get?
                      get_contact_mechanisms
                    elsif request.delete?
                      delete_contact_mechanism
                    end
end

#contact_purposesObject



17
18
19
# File 'app/controllers/erp_app/organizer/crm/base_controller.rb', line 17

def contact_purposes
  render :inline => "{\"types\":#{ContactPurpose.all.to_json(:only => [:id, :description])}}"
end

#create_partyObject



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
# File 'app/controllers/erp_app/organizer/crm/base_controller.rb', line 43

def create_party
  result = {}
  begin
    enterprise_identifier = params[:enterprise_identifier]
    party_type = params[:party_type]
    klass = party_type.constantize
    params.delete(:enterprise_identifier)
    params.delete(:action)
    params.delete(:controller)
    params.delete(:party_type)
    params.delete(:authenticity_token)

    params[:birth_date] = Date.strptime(params[:birth_date], @@date_format) unless params[:birth_date].blank?
    params[:current_passport_expire_date] = Date.strptime(params[:current_passport_expire_date], @@date_format) unless params[:current_passport_expire_date].blank?

    business_party = klass.create(params)
    business_party.party.enterprise_identifier = enterprise_identifier
    business_party.party.save

    result = {:success => true, :message => "#{party_type} Added", :name => business_party.party.description}
  rescue Exception => ex
    result = {:success => false, :message => "Error adding #{party_type}"}
  end

  render :json => result
end

#get_party_detailsObject



115
116
117
# File 'app/controllers/erp_app/organizer/crm/base_controller.rb', line 115

def get_party_details
  @party = Party.find(params[:id]) rescue nil
end

#get_userObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/controllers/erp_app/organizer/crm/base_controller.rb', line 119

def get_user
  party = Party.find(params[:party_id]) rescue nil

  if params[:party_id] and !party.nil?
    user = party.user
    if user.nil?
      success = true
      message = "User Does Not Exist"
      u = {
          :username => '',
          :email => '',
          :activation_state => '',
          :last_login_at => '',
          :failed_logins_count => '',
          :activation_token => ''
      }
    else
      success = true
      message = "User Found"

       = user..blank? ? '' : user..getlocal.strftime(@@datetime_format)

      u = {
          :username => user.username,
          :email => user.email,
          :activation_state => user.activation_state,
          :last_login_at => ,
          :failed_logins_count => user.failed_logins_count,
          :activation_token => user.activation_token
      }
    end
  else
    success = false
    message = "Party Not Found"
    u = {}
  end

  render :json => {:success => success,
                   :message => message,
                   :data => [u]
  }
end


8
9
10
11
12
13
14
15
# File 'app/controllers/erp_app/organizer/crm/base_controller.rb', line 8

def menu
  menu = []

  menu << {:text => 'Individuals', :businessPartType => 'individual', :leaf => true, :iconCls => 'icon-user', :applicationCardId => "individuals_search_grid"}
  menu << {:text => 'Organizations', :businessPartType => 'organization', :leaf => true, :iconCls => 'icon-user', :applicationCardId => "organizations_search_grid"}

  render :json => menu
end

#partiesObject



21
22
23
24
25
26
27
28
29
# File 'app/controllers/erp_app/organizer/crm/base_controller.rb', line 21

def parties
  render :inline => if request.put?
                      update_party
                    elsif request.get?
                      get_parties
                    elsif request.delete?
                      delete_party
                    end
end

#send_emailObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/controllers/erp_app/organizer/crm/base_controller.rb', line 202

def send_email
  send_to = params[:send_to].strip
  subject = params[:subject].strip
  message = params[:message].strip
  cc_email = params[:cc_email].blank? ? nil : params[:cc_email].strip

  CrmMailer.send_message(send_to, subject, message).deliver

  if cc_email
    CrmMailer.send_message(cc_email, subject, message).deliver
  end

  render :json => {:success => true}
end

#update_partyObject



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
109
110
111
112
113
# File 'app/controllers/erp_app/organizer/crm/base_controller.rb', line 70

def update_party
  enterprise_identifier = params[:enterprise_identifier]
  party_type = params[:party_type]
  business_party_id = params[:business_party_id]
  business_party_data = params
  business_party_data.delete(:id)
  business_party_data.delete(:business_party_id)
  business_party_data.delete(:enterprise_identifier)
  business_party_data.delete(:authenticity_token)
  business_party_data.delete(:party_type)
  business_party_data.delete(:controller)
  business_party_data.delete(:action)
  puts business_party_data[:birth_date]
  business_party_data[:birth_date] = Date.strptime(business_party_data[:birth_date], @@date_format) unless business_party_data[:birth_date].blank?
  business_party_data[:current_passport_expire_date] = Date.strptime(business_party_data[:current_passport_expire_date], @@date_format) unless business_party_data[:current_passport_expire_date].blank?

  klass = party_type.constantize
  business_party = klass.find(business_party_id)

  business_party_data.each do |key, value|
    key = key.gsub("business_party.", "")
    method = key + '='
    business_party.send method.to_sym, value
  end

  business_party.save
  party = business_party.party

  begin
    party.enterprise_identifier = enterprise_identifier
    party.save
  end unless enterprise_identifier.blank?

  render :json => {:success => true,
                   :message => "#{party_type} updated",
                   :data => [{
                                 :id => party.id,
                                 :enterprise_identifier => party.enterprise_identifier,
                                 :created_at => party.created_at,
                                 :updated_at => party.updated_at,
                                 :business_party => party.business_party
                             }]
  }
end

#update_userObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/controllers/erp_app/organizer/crm/base_controller.rb', line 162

def update_user
  party = Party.find(params[:party_id]) rescue nil

  if params[:party_id] and !party.nil?
    user = party.user
    if user.nil?
      success = true
      message = "User Does Not Exist"
    else
      user.username = params[:username]
      user.email = params[:email]
      if user.save
        success = true
        message = "User Successfully Updated"
      else
        success = false
        message = 'Update Failed'
      end
    end
  else
    success = false
    message = "Party Not Found"
  end

  render :json => {:success => success, :message => message} and return
end