Module: SmartServicesHelper

Included in:
SmartServicesController, SupportCasesController
Defined in:
app/helpers/smart_services_helper.rb

Instance Method Summary collapse

Instance Method Details

#build_address(address, address_hash) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/helpers/smart_services_helper.rb', line 113

def build_address(address, address_hash)
  address.street1 = address_hash[:street1]
  address.street2 = address_hash[:street2]
  address.city = address_hash[:city]
  address.person_name = address_hash[:recipient] if address_hash[:recipient]
  address.state_code = address_hash[:state_code]
  address.zip = address_hash[:postal_code]
  address.country_iso3 = Country.find_by(iso: address_hash[:country_iso]).iso3
  address.is_residential = address_hash[:residential_address].nil? ? false : address_hash[:residential_address]
  # address.lat = lat
  # address.lng = lng
  address.disable_address_correction = true # disable_address_correction.nil? ? false : disable_address_correction
  address
end

#calculate_distance_from_lz(zip_code) ⇒ Object



105
106
107
108
109
110
111
# File 'app/helpers/smart_services_helper.rb', line 105

def calculate_distance_from_lz(zip_code)
  distance = nil
  base_address = Geocoder.search('60047', params: { countrycodes: 'us' }).first
  service_address = Geocoder.search(zip_code, params: { countrycodes: 'us' }).first
  distance = Geocoder::Calculations.distance_between(base_address.coordinates, service_address.coordinates) if base_address.present? && service_address.present?
  distance&.round
end

#check_postal_code(zip_code) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/smart_services_helper.rb', line 91

def check_postal_code(zip_code)
  services = []
  distance = calculate_distance_from_lz(zip_code)
  unless distance.nil?
    services << 'smartfit_residential' if distance < 51
    services << 'smartfit_commercial' if distance < 51
    services << 'smartfit_remote' if distance > 51
    # services << 'smartfix local | SmartFix Local - $29' if distance < 51
    # services << 'smartinstall | SmartInstall - $150' if distance < 101
    # services << 'smartfix national | SmartFix National - $200' if distance >= 101
  end
  services
end

#create_customer(customer, name, phone, email) ⇒ Object

visits = Visit.where(gclid:nil).where('landing_page LIKE ?', '%gclid=%')
visits.each do |visit|
if visit.landing_page.present? && (uri = Addressable::URI.parse(visit.landing_page) rescue nil)
end
end

if data[:landing_page].present? && (uri = Addressable::URI.parse(data[:landing_page]) rescue nil)
merged_params = Rack::Utils.parse_nested_query uri.query
merged_params = (merged_params || {}).with_indifferent_access
data[:referral_code] = merged_params[:referral_code].presence
data[:gclid] ||= merged_params[:gclid].presence
data[:referring_domain] ||= merged_params[:_vsrefdom].presence
data[:search_keyword] ||= merged_params[:keyword].presence
end



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/smart_services_helper.rb', line 18

def create_customer(customer, name, phone, email)
  cu = customer
  cu.profile_id = customer.profile_id || ProfileConstants::HOMEOWNER # If nil then it's a homeowner
  cu.source_info = customer.source_info
  # Split the person name, we'll reuse that later
  PersonNameParser.new(name).to_party(cu)
  # Build the contact points into the contact if present otherwise customer
  cu.phone = phone
  cu.email = email
  cu.state = 'lead_qualify' if cu.guest?
  cu.save
  cu
end

#create_order(service, quantity, customer, service_address = nil) ⇒ Object



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/helpers/smart_services_helper.rb', line 32

def create_order(service, quantity, customer, service_address = nil)
  # First make sure the customer has a billing address
  if service_address.present?
    s_address = format_service_address(service_address)
    existing_address = customer.all_addresses.detect do |a|
      a.full_address(false, '.', nil, true).casecmp(s_address).zero?
    end
    address = existing_address.presence || build_address(customer.addresses.new, service_address)
    customer.billing_address = address unless customer.billing_address
  end
  customer.clear_names if customer.has_guest_name?
  customer.save

  # now we create the order and add line items
  qty = quantity.to_i
  order = Order.new(order_type: Order::SALES_ORDER)
  order.customer = customer
  order.non_commissionable = true
  # Main service SKU
  main_catalog_item = Item.find_by(sku: service).catalog_items.where(catalog_id: 1).first
  li = LineItem.new(catalog_item_id: main_catalog_item.id,
                    quantity: 1,
                    price: main_catalog_item.amount,
                    discounted_price: main_catalog_item.amount)
  order.line_items << li
  # Extra rooms/areas for the main service
  case service
  when 'SMARTFIT_REMOTE_FIXRATE'
    compl_item_sku = 'SMARTFIT_REMOTE_ADD_PER_ROOM'
    effective_qty = qty - 3
  when 'SMARTFIT_RES_INDOOR_FIXRATE'
    compl_item_sku = 'SMARTFIT_RES_INDOOR_PER_ROOM'
    effective_qty = qty - 3
  when 'SMARTFIT_RES_OUTDOOR_FIXRATE'
    compl_item_sku = 'SMARTFIT_RES_OUTDOOR_PER_AREA'
    effective_qty = qty - 3
  when 'SMARTFIT_COM_INDOOR_FIXRATE1'
    compl_item_sku = 'SMARTFIT_COM_INDOOR_FIXRATE2'
    effective_qty = (qty - 20).positive? ? 1 : -1
  when 'SMARTFIT_COM_OUTDOOR_FIXRATE1'
    compl_item_sku = 'SMARTFIT_COM_OUTDOOR_FIXRATE2'
    effective_qty = (qty - 20).positive? ? 1 : -1
  end

  if effective_qty > 0
    extra_catalog_item = Item.find_by(sku: compl_item_sku).catalog_items.where(catalog_id: 1).first
    li = LineItem.new(catalog_item_id: extra_catalog_item.id,
                      quantity: effective_qty,
                      price: extra_catalog_item.amount,
                      discounted_price: extra_catalog_item.amount)
    order.line_items << li
  end
  order.pending_payment
  order.shipping_address = address if order.shipping_address.nil? and service_address.present?
  order.save!
  order.reload
  order
end

#format_service_address(address_hash) ⇒ Object



128
129
130
# File 'app/helpers/smart_services_helper.rb', line 128

def format_service_address(address_hash)
  "#{address_hash[:street1].upcase}.#{address_hash[:city].upcase}, #{address_hash[:state_code].upcase} #{address_hash[:postal_code].upcase}"
end