Class: Checkout::ServiceForm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::API, ActiveModel::Attributes, ActiveModel::Validations, ActiveModel::Validations::Callbacks, Models::ValidationErrorReporting, StripAttributes
Defined in:
app/forms/checkout/service_form.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_from_cart(cart) ⇒ Object



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
161
162
163
164
165
166
167
168
# File 'app/forms/checkout/service_form.rb', line 131

def self.new_from_cart(cart)
  #We get the first support case open linked to the order.
  support_case = cart.support_cases&.services&.pending_service_payment&.first
  cu = cart.customer
  if support_case.present?
    address = support_case&.service_address
  else
    address = cu.billing_address
  end
  cu.contacts.build if cu.customer.contacts.empty?
  cu.clear_names if cu.has_guest_name?
  cu.clear_individual_names if cu.has_guest_individual_name?
  cu.save
  service_form = new(
    customer_id: cu.id,
    order_id: cart.id,
    service_address: Checkout::StreetAddress.new(
      street1: address.try(:street1),
      street2: address.try(:street2),
      state_code: address.try(:state_code),
      postal_code: address.try(:zip),
      city: address.try(:city),
      country_iso: address.try(:country_iso3),
      residential_address: address.try(:is_residential),
      requires_liftgate: address.try(:requires_liftgate),
      has_loading_dock: address.try(:has_loading_dock),
      is_construction_site: address.try(:is_construction_site),
      limited_access: address.try(:limited_access),
      requires_appointment: address.try(:requires_appointment)
    )
  )
  if support_case.present?
    service_form.support_case_id = support_case.id
    service_form.service_date = support_case.service_date
    service_form.service_timeframe = support_case.preferred_service_timeframe
  end
  service_form
end

Instance Method Details

#build_service_address(address) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'app/forms/checkout/service_form.rb', line 119

def build_service_address(address)
  address.street1 = service_address.street1
  address.street2 = service_address.street2
  address.city = service_address.city
  address.person_name = contact_name if contact_name
  address.state_code = service_address.state_code
  address.zip = service_address.postal_code
  address.country_iso3 = Country.find_by(iso: service_address.country_iso).iso3
  address.disable_address_correction = true
  address
end

#build_service_description(service, service_timeframe) ⇒ Object



110
111
112
113
114
115
116
117
# File 'app/forms/checkout/service_form.rb', line 110

def build_service_description(service, service_timeframe)
  res = "<p>Customer requested: </p>"
  service.split(';').each do |ser|
    res += "<li>#{ser}</li>"
  end
  res += "<br><p>Preferred time frame is <b>#{service_timeframe}</b></p>"
  res
end

#create_ticket(service) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/forms/checkout/service_form.rb', line 84

def create_ticket(service)
  sc = SupportCase.new(case_type: 'SmartService')
  sc.send(:set_case_number)
  sc.state = 'pending_service_payment'
  sc.description = build_service_description(service, service_timeframe)
  sc.priority = 'Low'
  sc.service = service
  sc.service_address_id = @service_address.id if is_onsite_service?
  sc.service_date = service_date
  sc.preferred_service_timeframe = service_timeframe
  sc.build_participants_and_rooms(order_id: cart.id)
  sc.save
  sc
end

#is_onsite_service?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/forms/checkout/service_form.rb', line 33

def is_onsite_service?
  !is_remote_service?
end

#is_remote_service?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/forms/checkout/service_form.rb', line 37

def is_remote_service?
  Order.find(order_id).line_items.joins(:item).merge(Item.remote_services).any?
end

#persisted?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/forms/checkout/service_form.rb', line 29

def persisted?
  false
end

#saveObject



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
# File 'app/forms/checkout/service_form.rb', line 41

def save
  self.cart = Order.find(order_id)
  unless valid?
    errors.add(:base, 'Service form not valid')
    Rails.logger.debug("Saving Service form error")
    return false
  end

  if is_onsite_service?
    #First we save the address to the customer address list
    cu = cart.customer

    existing_address = cu.all_addresses.detect do |a|
      a.full_address(false).casecmp(service_address.full_address(false)).zero?
    end
    if existing_address.present?
      @service_address = existing_address
    elsif service_address.address_complete?
      @service_address = build_service_address(cu.addresses.new)
    else
      Rails.logger.error("Service address provided is not complete. Service schedule will fail. cart_id: #{self.cart.id}, address: #{service_address.full_address(true)}")
      service_address.errors.add(:base, "The address provided is missing some information. Please review all fields.")
    end

    if @service_address && !@service_address.is_a?(Checkout::StreetAddress)
      @service_address.save
    else
      errors.add(:base, 'Service address missing')
      return false
    end
  end

  #Then we create or update the support case
  @support_case = SupportCase.find(support_case_id) if support_case_id.present?
  if @support_case.present?
    @support_case = update_ticket(cart.line_items.smartservice_items.map{|a| a.item.name}.join('; '))
  else
    @support_case = create_ticket(cart.line_items.smartservice_items.map{|a| a.item.name}.join('; '))
  end

  @support_case.errors.blank?
end

#update_ticket(service) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'app/forms/checkout/service_form.rb', line 99

def update_ticket(service)
  sc = @support_case
  sc.description = build_service_description(service, service_timeframe)
  sc.service = service
  sc.service_address_id = @service_address.id if is_onsite_service?
  sc.service_date = service_date
  sc.preferred_service_timeframe = service_timeframe
  sc.save
  sc
end