Class: QuickEstimator

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/quick_estimator.rb

Overview

== Schema Information

Table name: quick_estimators
Database name: primary

id :integer not null, primary key
api_key :string(255)
domain_name :string(255)
logo_name :string
logo_uid :string
main_ui_color :string(255)
pricing_factor :float
pricing_factor_flat_amount :float
pricing_formula :string(255)
starting_application :string(255)
swf_asset :string(255)
third_party_name_locale :string(255)
title :string(255)
created_at :datetime
updated_at :datetime
catalog_id :integer
customer_id :integer

Indexes

index_quick_estimators_on_api_key (api_key)
index_quick_estimators_on_customer_id (customer_id)

Constant Summary collapse

STARTING_APPLICATION_OPTIONS =

, ["", "OW"]]

[["Floor Warming", "FW"], ["Snow Melting", "SM"]]
DEFAULT_MAIN_UI_COLOR =
"#4B2628"
DEFAULT_LOGO_URL =
"https://ik.warmlyyours.com/img/wy-logo-79b20c.png"
BUTTON_URL =
"https://ik.warmlyyours.com/img/quick-estimator-button-7b7d20.png"
PRICING_FORMULA_OPTIONS =
["msrp_price*pricing_factor+pricing_factor_flat_amount", "(msrp_price*pricing_factor).to_i+pricing_factor_flat_amount", "(msrp_price*pricing_factor).floor+pricing_factor_flat_amount", "(msrp_price*pricing_factor).ceil+pricing_factor_flat_amount"]
DEFAULT_SWF_ASSET =
"quick_estimator_etailer.swf"
SWF_ASSET_OPTIONS =
[[DEFAULT_SWF_ASSET, DEFAULT_SWF_ASSET], ["quick_estimator_hdca.swf", "quick_estimator_hdca.swf"], ["quick_estimator_hdca_fr_ca.swf", "quick_estimator_hdca_fr_ca.swf"], ["quick_estimator_hdca_v2.swf", "quick_estimator_hdca_v2.swf"], ["quick_estimator_hdca_fr_ca_v2.swf", "quick_estimator_hdca_fr_ca_v2.swf"]]
QE_HEIGHT =
{"quick_estimator_etailer.swf" => 1080, "quick_estimator_hdca.swf" => 900, "quick_estimator_hdca_fr_ca.swf" => 900, "quick_estimator_hdca_v2.swf" => 900, "quick_estimator_hdca_fr_ca_v2.swf" => 900}
QE_WIDTH =
{"quick_estimator_etailer.swf" => 670, "quick_estimator_hdca.swf" => 670, "quick_estimator_hdca_fr_ca.swf" => 670, "quick_estimator_hdca_v2.swf" => 670, "quick_estimator_hdca_fr_ca_v2.swf" => 670}
THIRD_PARTY_LOCAL_OPTIONS =
[["en_ca", "en_ca"], ["fr_ca", "fr_ca"]]

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.get_qe_options(params) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/models/quick_estimator.rb', line 180

def self.get_qe_options(params)
  zone_sqft = nil
  zone_length = nil
  zone_width = nil
  expansion_joint_spacing = nil
  #puts "params[:zones]: #{params[:zones].inspect}"
  unless params[:zones].nil? || params[:zones].empty?
    # need at least one zone to be valid
    params[:zones].each{|zone_ind, zone_data|
      zone = {}
      zone_sqft = zone_data[:sqft].to_f
      zone_length = zone_data[:length].to_f if zone_data[:length].to_f > 0.0
      zone_width = zone_data[:width].to_f if zone_data[:width].to_f > 0.0
    }
  else
    zone_sqft = params[:zone_sqft].to_f if params[:zone_length].to_f > 0.0
    zone_length = params[:zone_length].to_f if params[:zone_length].to_f > 0.0
    zone_width = params[:zone_width].to_f if params[:zone_width].to_f > 0.0
  end
  expansion_joint_spacing = params[:expansion_joint_spacing].to_f if params[:expansion_joint_spacing].to_f > 0.0
  return {:catalog_id => params[:catalog_id].to_i, :floor_type_id => params[:floor_type_id].to_i, :application_type => params[:application_code], :heating_system_type_code => params[:heating_system_type_code], :zone_sqft => zone_sqft, :zone_length => zone_length, :zone_width => zone_width, :coverage_state => params[:coverage_state] || 0, :expansion_joint_spacing => expansion_joint_spacing }
end

.pricing_formula_options_for_selectObject



176
177
178
# File 'app/models/quick_estimator.rb', line 176

def self.pricing_formula_options_for_select
  PRICING_FORMULA_OPTIONS.map{|o| [o, o]}
end

.starting_application_options_for_selectObject



97
98
99
# File 'app/models/quick_estimator.rb', line 97

def self.starting_application_options_for_select
  STARTING_APPLICATION_OPTIONS.map{|o| [o[0], o[1]]}
end

.swf_asset_options_for_selectObject



101
102
103
# File 'app/models/quick_estimator.rb', line 101

def self.swf_asset_options_for_select
  SWF_ASSET_OPTIONS.map{|o| [o[0], o[1]]}
end

.third_party_name_locale_options_for_selectObject



105
106
107
# File 'app/models/quick_estimator.rb', line 105

def self.third_party_name_locale_options_for_select
  THIRD_PARTY_LOCAL_OPTIONS.map{|o| [o[0], o[1]]}
end

.valid_domain_name?(api_key, domain_name) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
# File 'app/models/quick_estimator.rb', line 109

def self.valid_domain_name?(api_key, domain_name)
  qe = QuickEstimator.find_by(api_key: api_key)
  qe.domain_name.downcase == domain_name.downcase
end

Instance Method Details

#base_urlObject



153
154
155
# File 'app/models/quick_estimator.rb', line 153

def base_url
  "https://#{API_HOSTNAME}/v1/quick_estimators/#{self.api_key}"
end

#catalogCatalog

Returns:

See Also:



33
# File 'app/models/quick_estimator.rb', line 33

belongs_to :catalog, optional: true

#customerCustomer

Returns:

See Also:



32
# File 'app/models/quick_estimator.rb', line 32

belongs_to :customer, optional: true

#generate_api_keyObject



68
69
70
71
72
73
# File 'app/models/quick_estimator.rb', line 68

def generate_api_key
  self.update_attribute(:api_key, Encryption.encrypt_string("#{self.id}"))
  unless self.customer and self.customer.catalog and self.customer.catalog.bom_enabled
    self.customer.catalog.update(bom_enabled: true)
  end
end

#get_pricing_formula(hide_indicator = false) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/models/quick_estimator.rb', line 161

def get_pricing_formula(hide_indicator=false)
  if !self.pricing_factor.blank? && !self.pricing_formula.blank?
    # here we parse the formula and insert the values, order is important! default to 0.0 for pricing_factor_flat_amount blank
    res = self.pricing_formula.gsub('pricing_factor_flat_amount', self.pricing_factor_flat_amount.to_f.to_s).gsub('pricing_factor', self.pricing_factor.to_s)
  else
    # not set return msrp
    res = "msrp_price"
    if hide_indicator
      res = "HIDE"
    end
  end
  #puts "get_pricing_formula, res: #{res}"
  return res
end

#handle_caseObject



75
76
77
78
79
# File 'app/models/quick_estimator.rb', line 75

def handle_case
  self.update_attribute(:domain_name, self.domain_name.downcase)
  self.update_attribute(:title, self.title.titlecase) if self.title
  self.update_attribute(:main_ui_color, self.main_ui_color.upcase) if self.main_ui_color
end

#iframe_snippetObject



139
140
141
# File 'app/models/quick_estimator.rb', line 139

def iframe_snippet
  "<iframe id='#{self.api_key}' height='#{QE_HEIGHT[self.swf_asset]}' width='#{QE_WIDTH[self.swf_asset]}' allowtransparency='true' frameborder='0' scrolling='no' src='#{base_url}/display'></iframe>"
end

#is_authorized?(referer) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
# File 'app/models/quick_estimator.rb', line 119

def is_authorized?(referer)
  Rails.logger.debug("Quick estimator", customer_id: self.customer&.id, api_key: self.api_key)
  return true if (Rails.env.development? or Rails.env.staging?)
  referer.to_s.index(self.domain_name) ||
    referer.to_s == "/" ||
    referer.to_s.index("warmlyyours.com") ||
    referer.to_s.index("homedepot.ca") ||
    referer.inspect == "nil"
end


157
158
159
# File 'app/models/quick_estimator.rb', line 157

def link_popup_html_snippet
  "<a href='#{base_url}/display' target='_blank' rel='noopener noreferrer'><img src='#{BUTTON_URL}' alt='Instant Quote'/></a>"
end


143
144
145
146
147
148
149
150
151
# File 'app/models/quick_estimator.rb', line 143

def link_popup_js_snippet(use_local=false)
  script = <<-EOS
    <script src='#{base_url}/link_popup.js' type='text/javascript'></script>
    <a href='javascript:quick_estimator_popup_#{self.api_key}();'>
      <img src='#{BUTTON_URL}' alt='Instant Quote'/>
    </a>
  EOS
  script
end

#log_access(access_request_type, referer = nil) ⇒ Object



129
130
131
# File 'app/models/quick_estimator.rb', line 129

def log_access(access_request_type, referer=nil)
  QeAccessStatistic.create({:quick_estimator_id => self.id, :access_type => access_request_type, :referer => referer.to_s[0..254]})
end

#log_data(data_request_type, params) ⇒ Object



133
134
135
136
137
# File 'app/models/quick_estimator.rb', line 133

def log_data(data_request_type, params)
  data_options = QuickEstimator.get_qe_options(params)
  qe_data_stat = QeDataStatistic.create(data_options)
  QeAccessStatistic.create({:quick_estimator_id => self.id, :access_type => data_request_type, :qe_data_statistic_id => qe_data_stat.id})
end

#logo_url(return_missing_image = true) ⇒ Object



114
115
116
117
# File 'app/models/quick_estimator.rb', line 114

def logo_url(return_missing_image=true)
  url = .thumb('50x').url rescue nil
  url ||= DEFAULT_LOGO_URL if return_missing_image
end

#main_ui_color_defined?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/models/quick_estimator.rb', line 93

def main_ui_color_defined?
  self.main_ui_color?
end

#must_be_an_organizationObject (protected)



214
215
216
217
218
# File 'app/models/quick_estimator.rb', line 214

def must_be_an_organization
  if (self.customer && !self.customer.is_organization?)
    errors.add :base, "Instant Quote applies to organizations only"
  end
end

#pricing_factor_defined?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'app/models/quick_estimator.rb', line 85

def pricing_factor_defined?
  self.pricing_factor?
end

#pricing_factor_flat_amount_defined?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/models/quick_estimator.rb', line 89

def pricing_factor_flat_amount_defined?
  self.pricing_factor_flat_amount?
end

#pricing_formula_defined?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'app/models/quick_estimator.rb', line 81

def pricing_formula_defined?
  !self.pricing_formula.blank?
end

#qe_access_statisticsActiveRecord::Relation<QeAccessStatistic>

Returns:

See Also:



35
# File 'app/models/quick_estimator.rb', line 35

has_many :qe_access_statistics

#store_nameObject



203
204
205
206
207
208
209
210
# File 'app/models/quick_estimator.rb', line 203

def store_name
  case self.catalog.store_id
    when 2
      'Canada'
    else
      'USA'
  end
end