Class: Gemgento::Adapter::Sellect::Order

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/gemgento/adapter/sellect/order.rb

Class Method Summary collapse

Class Method Details

.address(address_id) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'app/models/gemgento/adapter/sellect/order.rb', line 169

def self.address(address_id)
  self.table_name = 'sellect_addresses'
  address = self.find_by(id: address_id)

  if address.nil?
    return address_not_provided
  else
    return address
  end
end

.address_not_providedObject



180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/models/gemgento/adapter/sellect/order.rb', line 180

def self.address_not_provided
  self.table_name = 'sellect_addresses'
  address = self.new
  address.firstname = 'NOT PROVIDED'
  address.lastname = 'NOT PROVIDED'
  address.address1 = 'NOT PROVIDED'
  address.city = 'NOT PROVIDED'
  address.zipcode = 'NOT PROVIDED'
  address.phone = 'NOT PROVIDED'

  return address
end

.completed_ordersObject



40
41
42
43
44
# File 'app/models/gemgento/adapter/sellect/order.rb', line 40

def self.completed_orders
  self.table_name = 'sellect_orders'

  return self.where(state: %w[complete returned challenged challenged_canceled]).order('created_at ASC')
end

.country(address) ⇒ Object



314
315
316
317
318
319
320
321
322
# File 'app/models/gemgento/adapter/sellect/order.rb', line 314

def self.country(address)
  self.table_name = 'sellect_countries'

  if address.country_id.nil?
    return nil
  else
    return self.find(address.country_id).iso
  end
end

.email(email) ⇒ Object



278
279
280
281
282
283
284
# File 'app/models/gemgento/adapter/sellect/order.rb', line 278

def self.email(email)
  if email.nil? || email == ''
    return '[email protected]'
  else
    return email
  end
end

.export_to_csvObject



7
8
9
10
11
12
13
14
15
# File 'app/models/gemgento/adapter/sellect/order.rb', line 7

def self.export_to_csv
  CSV.open("orders.csv", "wb") do |csv|
    csv << order_export_headers
    completed_orders.each_with_index do |order, index|
      puts "Working on row #{index}"
      csv << order_row(order)
    end
  end
end

.line_items(order_id) ⇒ Object



272
273
274
275
276
# File 'app/models/gemgento/adapter/sellect/order.rb', line 272

def self.line_items(order_id)
  self.table_name = 'sellect_line_items'

  return self.where(order_id: order_id)
end

.order_export_headersObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/gemgento/adapter/sellect/order.rb', line 17

def self.order_export_headers
  %w[
      order_id website email password group_id store_id payment_method shipping_method 
      billing_prefix billing_firstname billing_middlename billing_lastname billing_suffix 
      billing_street_full billing_city billing_region billing_country billing_postcode 
      billing_telephone billing_company billing_fax shipping_prefix shipping_firstname 
      shipping_middlename shipping_lastname shipping_suffix shipping_street_full shipping_city 
      shipping_region shipping_country shipping_postcode shipping_telephone shipping_company 
      shipping_fax created_in is_subscribed customer_id created_at updated_at tax_amount 
      shipping_amount discount_amount subtotal grand_total total_paid total_refunded 
      total_qty_ordered total_canceled total_invoiced total_online_refunded total_offline_refunded 
      base_tax_amount base_shipping_amount base_discount_amount base_subtotal base_grand_total 
      base_total_paid base_total_refunded base_total_qty_ordered base_total_canceled base_total_invoiced 
      base_total_online_refunded base_total_offline_refunded subtotal_refunded subtotal_canceled 
      discount_refunded discount_invoiced tax_refunded tax_canceled shipping_refunded shipping_canceled 
      base_subtotal_refunded base_subtotal_canceled base_discount_refunded base_discount_canceled 
      base_discount_invoiced base_tax_refunded base_tax_canceled base_shipping_refunded 
      base_shipping_canceled subtotal_invoiced tax_invoiced shipping_invoiced base_subtotal_invoiced 
      base_tax_invoiced base_shipping_invoiced shipping_tax_amount base_shipping_tax_amount 
      shipping_tax_refunded base_shipping_tax_refunded products_ordered order_status
  ]
end

.order_row(order) ⇒ Object



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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/models/gemgento/adapter/sellect/order.rb', line 46

def self.order_row(order)
  user = Gemgento::User.find_by(email: order.email)
  store = store(order.zone_member_id)
  shipping_addres = address(order.ship_address_id)
  billing_address = address(order.bill_address_id)
  payment = payment(order.id)
  totals = totals(order, payment)
  line_items = line_items(order.id).reload

  [
    order.number.gsub('R', ''),   # order_id
    store.code,                   # website
    email(order.email),           # email
    '',                           # password
    user_group(user),             # group_id
    store.magento_id,             # store_id
    'globalcollect_cc_merchant',  # payment_method
    'flatrate_flatrate',          # shipping_method
    '',                           # billing_prefix
    billing_address.firstname,    # billing_firstname
    '',                           # billing_middlename
    billing_address.lastname,     # billing_lastname
    '',                           # billing_suffix
    street(billing_address),      # billing_street_full
    billing_address.city,         # billing_city
    state(billing_address),       # billing_region
    country(billing_address),     # billing_country
    billing_address.zipcode,      # billing_postcode
    billing_address.phone,        # billing_telephone
    '',                           # billing_company
    '',                           # billing_fax
    '',                           # shipping_prefix
    shipping_addres.firstname,    # shipping_firstname
    '',                           # shipping_middlename
    shipping_addres.lastname,     # shipping_lastname
    '',                           # shipping_suffix
    street(shipping_addres),      # shipping_street_full
    shipping_addres.city,         # shipping_city
    state(shipping_addres),       # shipping_region
    country(shipping_addres),     # shipping_country
    shipping_addres.zipcode,      # shipping_postcode
    shipping_addres.phone,        # shipping_telephone
    '',                           # shipping_company
    '',                           # shipping_fax
    store.name,                   # created_in
    0,                            # is_subscribed
    nil,                          # customer_id
    order.created_at,             # created_at
    order.updated_at,             # updated_at
    totals[:tax],                 # tax_amount
    totals[:shipping],            # shipping_amount
    0,                            # discount_amount
    totals[:subtotal],            # subtotal
    totals[:grand],               # grand_total
    totals[:paid],                # total_paid
    totals[:refunded],            # total_refunded
    0,                            # total_qty_ordered
    totals[:canceled],            # total_canceled
    totals[:grand],               # total_invoiced
    totals[:refunded],            # total_online_refunded
    0,                            # total_offline_refunded
    totals[:tax],                 # base_tax_amount
    totals[:shipping],            # base_shipping_amount
    0,                            # base_discount_amount
    totals[:subtotal],            # base_subtotal
    totals[:grand],               # base_grand_total
    totals[:paid],                # base_total_paid
    totals[:refunded],            # base_total_refunded
    0,                            # base_total_qty_ordered
    totals[:canceled],            # base_total_canceled
    totals[:grand],               # base_total_invoiced
    totals[:refunded],            # base_total_online_refunded
    0,                            # base_total_offline_refunded
    totals[:refunded],            # subtotal_refunded
    totals[:canceled],            # subtotal_canceled
    0,                            # discount_refunded
    0,                            # discount_invoiced
    0,                            # tax_refunded
    0,                            # tax_canceled
    0,                            # shipping_refunded
    0,                            # shipping_canceled
    totals[:refunded],            # base_subtotal_refunded
    totals[:canceled],            # base_subtotal_canceled
    0,                            # base_discount_refunded
    0,                            # base_discount_canceled
    0,                            # base_discount_invoiced
    0,                            # base_tax_refunded
    0,                            # base_tax_canceled
    0,                            # base_shipping_refunded
    0,                            # base_shipping_canceled
    totals[:subtotal],            # subtotal_invoiced
    totals[:tax],                 # tax_invoiced
    totals[:shipping],            # shipping_invoiced
    totals[:subtotal],            # base_subtotal_invoiced
    totals[:tax],                 # base_tax_invoiced
    totals[:shipping],            # base_shipping_invoiced
    0,                            # shipping_tax_amount
    0,                            # base_shipping_tax_amount
    0,                            # shipping_tax_refunded
    0,                            # base_shipping_tax_refunded
    products_ordered(line_items, store), # products_ordered
    order_status(order.state)     # order_status
  ]
end

.order_status(state) ⇒ Object



362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'app/models/gemgento/adapter/sellect/order.rb', line 362

def self.order_status(state)
  case state
    when 'complete'
      'complete'
    when 'returned'
      'complete'
    when 'challenged'
      'fraud'
    when 'challenged_canceled'
      'canceled'
    else
      'complete'
  end
end

.payment(order_id) ⇒ Object



193
194
195
196
197
# File 'app/models/gemgento/adapter/sellect/order.rb', line 193

def self.payment(order_id)
  self.table_name = 'sellect_payments'

  return self.find_by(order_id: order_id)
end

.product(variant_id, store) ⇒ Object



352
353
354
355
356
357
358
359
360
# File 'app/models/gemgento/adapter/sellect/order.rb', line 352

def self.product(variant_id, store)
  self.table_name = 'sellect_variants'
  variant = self.find_by(id: variant_id)
  return nil if variant.nil?

  upc = Gemgento::ProductAttribute.find_by(code: 'upc')

  return Gemgento::Product.where(magento_type: 'simple').filter({ attribute: upc, value: variant.upc }, store).first
end

.products_ordered(line_items, store) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
# File 'app/models/gemgento/adapter/sellect/order.rb', line 340

def self.products_ordered(line_items, store)
  ordered = []

  line_items.each do |li|
    p = product(li.variant_id, store)
    next if p.nil?
    ordered << "#{p.sku}:#{li.quantity}:#{p.price}"
  end

  return ordered.join('|')
end

.quantity(line_items) ⇒ Object



332
333
334
335
336
337
338
# File 'app/models/gemgento/adapter/sellect/order.rb', line 332

def self.quantity(line_items)
  quantity = 0

  line_items.each { |li| quantity+= li.quantity }

  return quantity
end

.shipment(order_id) ⇒ Object



199
200
201
202
203
# File 'app/models/gemgento/adapter/sellect/order.rb', line 199

def self.shipment(order_id)
  self.table_name = 'sellect_shipments'

  return self.find_by(order_id: order_id)
end

.shipping_cost(order) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'app/models/gemgento/adapter/sellect/order.rb', line 218

def self.shipping_cost(order)
  self.inheritance_column = :_type_disabled
  self.table_name = 'sellect_adjustments'
  cost = self.find_by(
      adjustable_type: 'Sellect::Order',
      adjustable_id: order.id,
      originator_type: 'Sellect::ShippingMethod'
  )

  if cost.nil?
    return 0
  else
    return cost.amount.to_f
  end
end

.state(address) ⇒ Object



304
305
306
307
308
309
310
311
312
# File 'app/models/gemgento/adapter/sellect/order.rb', line 304

def self.state(address)
  self.table_name = 'sellect_states'

  if address.state_id.nil?
    return nil
  else
    return self.find(address.state_id).name
  end
end

.store(zone_member_id) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/models/gemgento/adapter/sellect/order.rb', line 151

def self.store(zone_member_id)
  self.table_name = 'sellect_zone_members'
  zone_member = self.find(zone_member_id)

  case zone_member.currency
    when 'eur'
      store = Gemgento::Store.find_by(code: 'eu')
    when 'gbp'
      store = Gemgento::Store.find_by(code: 'uk')
    when 'usd'
      store = Gemgento::Store.find_by(code: 'us')
    else
      store = Gemgento::Store.current
  end

  return store
end

.street(address) ⇒ Object



294
295
296
297
298
299
300
301
302
# File 'app/models/gemgento/adapter/sellect/order.rb', line 294

def self.street(address)
  street = address.address1.nil? ? 'NOT PROVIDED' : address.address1

  if !address.address2.nil? && address.address2 != ''
    street+= "\n" + address.address2
  end

  return street
end

.tax(order) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'app/models/gemgento/adapter/sellect/order.rb', line 234

def self.tax(order)
  self.inheritance_column = :_type_disabled
  self.table_name = 'sellect_adjustments'
  taxes = self.where(
      adjustable_type: 'Sellect::Order',
      adjustable_id: order.id,
      originator_type: 'Sellect::TaxRate'
  )

  tax = 0
  taxes.each { |t| tax+= t.amount.to_f }

  return tax
end

.total_canceled(order) ⇒ Object



264
265
266
267
268
269
270
# File 'app/models/gemgento/adapter/sellect/order.rb', line 264

def self.total_canceled(order)
  if order.state == 'challenged_canceled'
    order.payment_total
  else
    0
  end
end

.total_refunded(order) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'app/models/gemgento/adapter/sellect/order.rb', line 249

def self.total_refunded(order)
  self.inheritance_column = :_type_disabled
  self.table_name = 'sellect_adjustments'
  refunds = self.where(
      adjustable_type: 'Sellect::Order',
      adjustable_id: order.id,
      source_type: 'Sellect::Refund'
  )

  refund = 0
  refunds.each { |r| refund+= r.amount.to_f.abs }

  return refund
end

.totals(order, payment) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
# File 'app/models/gemgento/adapter/sellect/order.rb', line 205

def self.totals(order, payment)
  totals = {}
  totals[:grand] = order.payment_total
  totals[:shipping] = shipping_cost(order)
  totals[:tax] = tax(order)
  totals[:subtotal] = order.item_total
  totals[:paid] = order.payment_total
  totals[:refunded] = total_refunded(order)
  totals[:canceled] = total_canceled(order)

  return totals
end

.user_group(user) ⇒ Object



286
287
288
289
290
291
292
# File 'app/models/gemgento/adapter/sellect/order.rb', line 286

def self.user_group(user)
  if user.nil? || user.id.nil?
    return Gemgento::UserGroup.find_by(magento_id: 0).code
  else
    return user.user_group.code
  end
end

.user_id(user) ⇒ Object



324
325
326
327
328
329
330
# File 'app/models/gemgento/adapter/sellect/order.rb', line 324

def self.user_id(user)
  if user.nil?
    return nil
  else
    return user.magento_id
  end
end