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"]].freeze
DEFAULT_MAIN_UI_COLOR =

Default main ui color.

"#4B2628".freeze
DEFAULT_LOGO_URL =

Default logo url.

"https://ik.warmlyyours.com/img/wy-logo-79b20c.png".freeze
BUTTON_URL =

URL for button.

"https://ik.warmlyyours.com/img/quick-estimator-button-7b7d20.png".freeze
PRICING_FORMULA_OPTIONS =

Available 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"].freeze
DEFAULT_SWF_ASSET =

Default swf asset.

"quick_estimator_etailer.swf".freeze
SWF_ASSET_OPTIONS =

Available 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"]].freeze
QE_HEIGHT =

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 }.freeze
QE_WIDTH =

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 }.freeze
THIRD_PARTY_LOCAL_OPTIONS =

Available third party local options.

[%w[en_ca en_ca], %w[fr_ca fr_ca]].freeze

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

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 Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#customer_idObject (readonly)



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

validates :starting_application, :domain_name, :customer_id, presence: true

#domain_nameObject (readonly)



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

validates :starting_application, :domain_name, :customer_id, presence: true

#logoObject (readonly)



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

validates :logo, length: { maximum: 300.kilobytes }

#main_ui_colorObject (readonly)



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

validates :main_ui_color, format: { with: /#\b[A-Fa-f0-9]{6}\b/, if: :main_ui_color_defined? }

#pricing_factorObject (readonly)



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

validates :pricing_factor, numericality: { greater_than_or_equal_to: 0.0, if: :pricing_factor_defined? }

#pricing_factor_flat_amountObject (readonly)



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

validates :pricing_factor_flat_amount, numericality: { if: :pricing_factor_flat_amount_defined? }

#pricing_formulaObject (readonly)



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

validates :pricing_formula, format: { with: /\bmsrp_price+\b/, if: :pricing_formula_defined? }

#starting_applicationObject (readonly)



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

validates :starting_application, :domain_name, :customer_id, presence: true

#titleObject (readonly)



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

validates :title, length: { maximum: 40 }

Class Method Details

.get_qe_options(params) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/models/quick_estimator.rb', line 192

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}"
  if params[:zones].blank?
    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
  else
    # need at least one zone to be valid
    params[:zones].each do |_zone_ind, zone_data|
      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
    end
  end
  expansion_joint_spacing = params[:expansion_joint_spacing].to_f if params[:expansion_joint_spacing].to_f > 0.0
  { 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



188
189
190
# File 'app/models/quick_estimator.rb', line 188

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

.starting_application_options_for_selectObject



107
108
109
# File 'app/models/quick_estimator.rb', line 107

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

.swf_asset_options_for_selectObject



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

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

.third_party_name_locale_options_for_selectObject



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

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)


119
120
121
122
# File 'app/models/quick_estimator.rb', line 119

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



167
168
169
# File 'app/models/quick_estimator.rb', line 167

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

#catalogCatalog

Returns:

See Also:



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

belongs_to :catalog, optional: true

#customerCustomer

Returns:

See Also:



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

belongs_to :customer, optional: true

#generate_api_keyObject



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

def generate_api_key
  update_attribute(:api_key, Encryption.encrypt_string(id.to_s))
  customer.catalog.update(bom_enabled: true) unless customer&.catalog&.bom_enabled
end

#get_pricing_formula(hide_indicator = false) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/models/quick_estimator.rb', line 175

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

#handle_caseObject



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

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

#iframe_snippetObject



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

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

#is_authorized?(referer) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
136
137
138
139
140
141
142
# File 'app/models/quick_estimator.rb', line 133

def is_authorized?(referer)
  Rails.logger.debug("Quick estimator", customer_id: customer&.id, api_key: api_key)
  return true if Rails.env.development? || Rails.env.staging?

  referer.to_s.index(domain_name) ||
    referer.to_s == "/" ||
    referer.to_s.index("warmlyyours.com") ||
    referer.to_s.index("homedepot.ca") ||
    referer.inspect == "nil"
end


171
172
173
# File 'app/models/quick_estimator.rb', line 171

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


158
159
160
161
162
163
164
165
# File 'app/models/quick_estimator.rb', line 158

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

#log_access(access_request_type, referer = nil) ⇒ Object



144
145
146
# File 'app/models/quick_estimator.rb', line 144

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

#log_data(data_request_type, params) ⇒ Object



148
149
150
151
152
# File 'app/models/quick_estimator.rb', line 148

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: id, access_type: data_request_type, qe_data_statistic_id: qe_data_stat.id })
end

#logo_url(return_missing_image = true) ⇒ Object



124
125
126
127
128
129
130
131
# File 'app/models/quick_estimator.rb', line 124

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

#main_ui_color_defined?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/quick_estimator.rb', line 103

def main_ui_color_defined?
  main_ui_color?
end

#must_be_an_organizationObject (protected)



226
227
228
# File 'app/models/quick_estimator.rb', line 226

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

#pricing_factor_defined?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'app/models/quick_estimator.rb', line 95

def pricing_factor_defined?
  pricing_factor?
end

#pricing_factor_flat_amount_defined?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'app/models/quick_estimator.rb', line 99

def pricing_factor_flat_amount_defined?
  pricing_factor_flat_amount?
end

#pricing_formula_defined?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/models/quick_estimator.rb', line 91

def pricing_formula_defined?
  pricing_formula.present?
end

#qe_access_statisticsActiveRecord::Relation<QeAccessStatistic>

Returns:

See Also:



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

has_many :qe_access_statistics

#store_nameObject



215
216
217
218
219
220
221
222
# File 'app/models/quick_estimator.rb', line 215

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