Class: Order2cb::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/order2cb/order.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Order

Returns a new instance of Order.



10
11
12
# File 'lib/order2cb/order.rb', line 10

def initialize(data)
  @data = data
end

Class Method Details

.find(order_number) ⇒ Object



4
5
6
7
8
# File 'lib/order2cb/order.rb', line 4

def self.find(order_number)
  sql = "SELECT * FROM exp_spree_orders where number = '#{order_number}';"
  data = $db_client.query(sql)
  return Order.new(data.first)
end

Instance Method Details

#adjustmentsObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/order2cb/order.rb', line 144

def adjustments
  line_items_results = []
  db_adjustments_by_order.each do |adj|
    line_items_results << {
      'qty' => 1,
      'sku' => adj['coupon_code'],
      'price' => exvat(adj['amount']),
      'size' => nil,
      'speed_rating' => nil,
      'load_index' => nil,
      'description' => 'Promotion'
    }
  end
  line_items_results
end

#current_stateObject



18
19
20
21
22
23
24
25
26
# File 'lib/order2cb/order.rb', line 18

def current_state
  if high_risk?
    return "Requiring action Risk"
  elsif fitting_centre['mobile_fitting']
    return "Ready for mobile fitting"
  else
    return "Ready for dataflex"
  end
end

#customerObject



32
33
34
# File 'lib/order2cb/order.rb', line 32

def customer
  [@data['title'], @data['first_name'], @data['surname']].reject{|s| s.strip.empty?}.join(" ")
end

#db_adjustments_by_orderObject



140
141
142
# File 'lib/order2cb/order.rb', line 140

def db_adjustments_by_order
  $db_client.query("SELECT * FROM exp_spree_adjustments WHERE source_id = '#{@data['id']}' ORDER BY id ASC")
end

#exvat(with_vat_amount) ⇒ Object



163
164
165
# File 'lib/order2cb/order.rb', line 163

def exvat(with_vat_amount)
  (with_vat_amount / 120) * 100
end

#fitting_centreObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/order2cb/order.rb', line 90

def fitting_centre
  channel_fields = $db_client.query("SELECT exp_channel_fields.* FROM exp_channel_fields JOIN exp_channels on exp_channels.channel_id = exp_channel_fields.group_id WHERE exp_channels.channel_name = 'branches'")
  data_field_id = $db_client.query("SELECT exp_channel_fields.field_id FROM exp_channel_fields JOIN exp_channels on exp_channels.channel_id = exp_channel_fields.group_id WHERE exp_channels.channel_name = 'branches' and exp_channel_fields.field_name = 'dataflex_id'").first["field_id"]
  fitting_centre_fields = $db_client.query("SELECT * FROM exp_channel_data WHERE field_id_#{data_field_id} = '#{@data['fitting_centre_id']}'")

  fitting_centre_hash = {}
  channel_fields.map{|f| fitting_centre_hash[f['field_name']] = fitting_centre_fields.first["field_id_#{f['field_id']}"]}

  is_mobile_fitting = fitting_centre_hash['is_mobile_fitting'].upcase == "YES"
  details = {
              'phone_number' => fitting_centre_hash['phone_number'],
              'branch_number' => fitting_centre_hash['dataflex_id'],
              'mobile_fitting' => is_mobile_fitting
  }
  if is_mobile_fitting

    address_components = [fitting_centre_hash['street'], fitting_centre_hash['street2'], fitting_centre_hash['town'], fitting_centre_hash['postcode']]
    address = address_components.reject{|comp| comp.strip.empty?}.join(", ")

    details['mobile_fitter'] = fitting_centre_hash['name']
    details['customer_contact_name'] = 'Unknown'
    details['fitting_address'] = address
  else
    details['name'] = fitting_centre_hash['name']
  end

  details
end

#get_line_itemsObject



160
161
162
# File 'lib/order2cb/order.rb', line 160

def get_line_items
  $db_client.query("SELECT * FROM exp_remote_line_items WHERE order_id = #{@data['id']} ORDER BY id ASC;")
end

#high_risk?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/order2cb/order.rb', line 28

def high_risk?
  (@data["risk_score"] && @data["risk_score"] > 22) || (@data["worldpay_risk_score"] && @data["worldpay_risk_score"] >= 40)
end

#line_item_price(line_item_row) ⇒ Object



167
168
169
# File 'lib/order2cb/order.rb', line 167

def line_item_price(line_item_row)
  line_item_row['prices'].to_s.split(",")[0].to_s.to_f
end

#line_itemsObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/order2cb/order.rb', line 119

def line_items
  line_items_results = []
  @operational_stock_order = true

  get_line_items.each do |line_item_row|
    if line_item_row['is_operational_stock']==0
      @operational_stock_order = false
    end
    line_items_results << {
      'qty' => line_item_row['quantity'],
      'sku' => line_item_row['sku'],
      'price' => exvat(line_item_price(line_item_row)),
      'size' => line_item_row['tyre_size'],
      'speed_rating' => line_item_row['speed_rating'],
      'load_index' => line_item_row['load_index'].to_i,
      'description' => line_item_row['sku_description']
    }
  end
  line_items_results
end

#market_opt_inObject



36
37
38
# File 'lib/order2cb/order.rb', line 36

def market_opt_in
  @data['accept_marketing'] == 1
end

#numberObject



14
15
16
# File 'lib/order2cb/order.rb', line 14

def number
  @data['number']
end

#order_detailsObject



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
# File 'lib/order2cb/order.rb', line 48

def order_details
  {
    'title' => @data['number'],
    'case_type_id' => ENV['CB_CASE_TYPE_ID'],
    'case_type' => 'tyre-shopper-order',
    'current_state' => current_state,
    'order_number' => @data['number'],
    'order_date' => DateTime.now,
    'customer' => customer,
    'vrn' => @data['vehicle_registration'],
    'address1' => @data['address1'],
    'address2' => @data['address2'],
    'address3' => @data['town'],
    'postcode' => @data['postcode'],
    'daytime_telephone' => @data['daytime_telephone'],
    'evening_telephone' => @data['evening_telephone'],
    'email' => @data['email'],
    'market_opt_in' => market_opt_in,
    'payment_type' => payment_type,
    'transaction_reference' => payment_reference,
    'card_number' => @data['card_last_digits'],
    'account_holder' => @data['card_holder_name'],
    'streamline_risk_assessment' => @data['worldpay_risk_score'],
    'tyre_shopper_risk_assessment' => @data['risk_score'],
    'tyre_shopper_risk_assessment_notes' => @data['risk_score_reasons'],
    'order_source' => 'TyreShopper',
    'is_van_tyre' => short_codes.include?("VN"),
    'is_winter_tyre' => short_codes.include?("WN"),
    'is_4x4_tyre' => short_codes.include?("4X"),
    'is_run_flat_tyre' => short_codes.include?("RF"),
     # set time to + 5 hours
    'fitting_date' => @data['fitting_date'] + 5.hours,
    'am_pm' => @data['fitting_day_part'],
    'stock_check' => true,
    'original_total' => @data['total']
  }
end

#payment_referenceObject



44
45
46
# File 'lib/order2cb/order.rb', line 44

def payment_reference
  @data['card_type'].to_s.strip == "" ? @data['pp_payment_id'] : @data['number']
end

#payment_typeObject



40
41
42
# File 'lib/order2cb/order.rb', line 40

def payment_type
  @data['card_type'].to_s.strip == "" ? "PayPal" : "Creditcard"
end

#short_codesObject



86
87
88
# File 'lib/order2cb/order.rb', line 86

def short_codes
  @data["short_code"].to_s.upcase.split("-")
end

#to_hashObject



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/order2cb/order.rb', line 171

def to_hash
  order = order_details.merge(fitting_centre)

  order['line_items'] = []
  order['line_items'] += line_items
  order['line_items'] += adjustments

  order['operational_stock'] = @operational_stock_order

  return {'case' => order}
end

#to_jsonObject



183
184
185
# File 'lib/order2cb/order.rb', line 183

def to_json
  self.to_hash.to_json
end