Class: Spree::Order
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Spree::Order
show all
- Includes:
- Checkout, CurrencyUpdater
- Defined in:
- app/models/spree/order.rb,
app/models/spree/order/checkout.rb,
app/models/spree/order/currency_updater.rb
Defined Under Namespace
Modules: Checkout, CurrencyUpdater
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#price_from_line_item, #update_line_item_currencies!, #update_line_item_price!
Methods included from Checkout
included
Instance Attribute Details
#coupon_code ⇒ Object
Returns the value of attribute coupon_code.
23
24
25
|
# File 'app/models/spree/order.rb', line 23
def coupon_code
@coupon_code
end
|
#use_billing ⇒ Object
Returns the value of attribute use_billing.
72
73
74
|
# File 'app/models/spree/order.rb', line 72
def use_billing
@use_billing
end
|
Class Method Details
.between(start_date, end_date) ⇒ Object
95
96
97
98
|
# File 'app/models/spree/order.rb', line 95
def self.between(start_date, end_date)
ActiveSupport::Deprecation.warn("Order#between will be deprecated in Spree 2.3, please use either Order#created_between or Order#completed_between instead.")
self.created_between(start_date, end_date)
end
|
.by_customer(customer) ⇒ Object
100
101
102
|
# File 'app/models/spree/order.rb', line 100
def self.by_customer(customer)
joins(:user).where("#{Spree.user_class.table_name}.email" => customer)
end
|
.by_number(number) ⇒ Object
88
89
90
|
# File 'app/models/spree/order.rb', line 88
def self.by_number(number)
where(number: number)
end
|
.by_state(state) ⇒ Object
104
105
106
|
# File 'app/models/spree/order.rb', line 104
def self.by_state(state)
where(state: state)
end
|
.complete ⇒ Object
108
109
110
|
# File 'app/models/spree/order.rb', line 108
def self.complete
where.not(completed_at: nil)
end
|
.incomplete ⇒ Object
112
113
114
|
# File 'app/models/spree/order.rb', line 112
def self.incomplete
where(completed_at: nil)
end
|
.register_update_hook(hook) ⇒ Object
Use this method in other gems that wish to register their own custom logic that should be called after Order#update
118
119
120
|
# File 'app/models/spree/order.rb', line 118
def self.register_update_hook(hook)
self.update_hooks.add(hook)
end
|
Instance Method Details
#all_adjustments ⇒ Object
122
123
124
125
|
# File 'app/models/spree/order.rb', line 122
def all_adjustments
Adjustment.where("order_id = :order_id OR (adjustable_id = :order_id AND adjustable_type = 'Spree::Order')",
:order_id => self.id)
end
|
#allow_cancel? ⇒ Boolean
244
245
246
247
|
# File 'app/models/spree/order.rb', line 244
def allow_cancel?
return false unless completed? and state != 'canceled'
shipment_state.nil? || %w{ready backorder pending}.include?(shipment_state)
end
|
#amount ⇒ Object
For compatiblity with Calculator::PriceSack
128
129
130
|
# File 'app/models/spree/order.rb', line 128
def amount
line_items.inject(0.0) { |sum, li| sum + li.amount }
end
|
#approve! ⇒ Object
572
573
574
|
# File 'app/models/spree/order.rb', line 572
def approve!
update_column(:considered_risky, false)
end
|
#approved? ⇒ Boolean
554
555
556
|
# File 'app/models/spree/order.rb', line 554
def approved?
!!self.approved_at
end
|
#approved_by(user) ⇒ Object
543
544
545
546
547
548
549
550
551
552
|
# File 'app/models/spree/order.rb', line 543
def approved_by(user)
self.transaction do
approve!
self.update_columns(
approver_id: user.id,
approved_at: Time.now,
considered_risky: false,
)
end
end
|
#associate_user!(user, override_email = true) ⇒ Object
Associates the specified user with the order.
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'app/models/spree/order.rb', line 258
def associate_user!(user, override_email = true)
self.user = user
attrs_to_set = { user_id: user.id }
attrs_to_set[:email] = user.email if override_email
attrs_to_set[:created_by_id] = user.id if self.created_by.blank?
assign_attributes(attrs_to_set)
if persisted?
self.class.unscoped.where(id: id).update_all(attrs_to_set)
end
end
|
#available_payment_methods ⇒ Object
#awaiting_returns? ⇒ Boolean
249
250
251
|
# File 'app/models/spree/order.rb', line 249
def awaiting_returns?
return_authorizations.any? { |return_authorization| return_authorization.authorized? }
end
|
#backordered? ⇒ Boolean
204
205
206
|
# File 'app/models/spree/order.rb', line 204
def backordered?
shipments.any?(&:backordered?)
end
|
#billing_firstname ⇒ Object
403
404
405
|
# File 'app/models/spree/order.rb', line 403
def billing_firstname
bill_address.try(:firstname)
end
|
#billing_lastname ⇒ Object
407
408
409
|
# File 'app/models/spree/order.rb', line 407
def billing_lastname
bill_address.try(:lastname)
end
|
#can_add_coupon? ⇒ Boolean
#can_approve? ⇒ Boolean
558
559
560
|
# File 'app/models/spree/order.rb', line 558
def can_approve?
!approved?
end
|
#can_ship? ⇒ Boolean
320
321
322
|
# File 'app/models/spree/order.rb', line 320
def can_ship?
self.complete? || self.resumed? || self.awaiting_return? || self.returned?
end
|
#checkout_allowed? ⇒ Boolean
Indicates whether or not the user is allowed to proceed to checkout. Currently this is implemented as a check for whether or not there is at least one LineItem in the Order. Feel free to override this logic in your own application if you require additional steps before allowing a checkout.
185
186
187
|
# File 'app/models/spree/order.rb', line 185
def checkout_allowed?
line_items.count > 0
end
|
#clone_billing_address ⇒ Object
235
236
237
238
239
240
241
242
|
# File 'app/models/spree/order.rb', line 235
def clone_billing_address
if bill_address and self.ship_address.nil?
self.ship_address = bill_address.clone
else
self.ship_address.attributes = bill_address.attributes.except('id', 'updated_at', 'created_at')
end
true
end
|
#completed? ⇒ Boolean
177
178
179
|
# File 'app/models/spree/order.rb', line 177
def completed?
completed_at.present?
end
|
#confirmation_required? ⇒ Boolean
If true, causes the confirmation step to happen during the checkout process
195
196
197
198
199
200
201
202
|
# File 'app/models/spree/order.rb', line 195
def confirmation_required?
Spree::Config[:always_include_confirm_step] ||
payments.valid.map(&:payment_method).compact.any?(&:payment_profiles_supported?) ||
state == 'confirm'
end
|
#consider_risk ⇒ Object
562
563
564
565
566
|
# File 'app/models/spree/order.rb', line 562
def consider_risk
if is_risky? && !approved?
considered_risky!
end
end
|
#considered_risky! ⇒ Object
568
569
570
|
# File 'app/models/spree/order.rb', line 568
def considered_risky!
update_column(:considered_risky, true)
end
|
#contains?(variant) ⇒ Boolean
286
287
288
|
# File 'app/models/spree/order.rb', line 286
def contains?(variant)
find_line_item_by_variant(variant).present?
end
|
#contents ⇒ Object
253
254
255
|
# File 'app/models/spree/order.rb', line 253
def contents
@contents ||= Spree::OrderContents.new(self)
end
|
#create_proposed_shipments ⇒ Object
481
482
483
484
485
486
487
488
489
490
491
|
# File 'app/models/spree/order.rb', line 481
def create_proposed_shipments
adjustments.shipping.delete_all
shipments.destroy_all
packages = Spree::Stock::Coordinator.new(self).packages
packages.each do |package|
shipments << package.to_shipment
end
shipments
end
|
#create_tax_charge! ⇒ Object
Creates new tax charges if there are any applicable rates. If prices already include taxes then price adjustments are created instead.
301
302
303
304
|
# File 'app/models/spree/order.rb', line 301
def create_tax_charge!
Spree::TaxRate.adjust(self, line_items)
Spree::TaxRate.adjust(self, shipments) if shipments.any?
end
|
#credit_cards ⇒ Object
324
325
326
327
|
# File 'app/models/spree/order.rb', line 324
def credit_cards
credit_card_ids = payments.from_credit_card.pluck(:source_id).uniq
CreditCard.where(id: credit_card_ids)
end
|
#currency ⇒ Object
132
133
134
|
# File 'app/models/spree/order.rb', line 132
def currency
self[:currency] || Spree::Config[:currency]
end
|
#deliver_order_confirmation_email ⇒ Object
353
354
355
356
|
# File 'app/models/spree/order.rb', line 353
def deliver_order_confirmation_email
OrderMailer.confirm_email(self.id).deliver
update_column(:confirmation_delivered, true)
end
|
#display_additional_tax_total ⇒ Object
152
153
154
|
# File 'app/models/spree/order.rb', line 152
def display_additional_tax_total
Spree::Money.new(additional_tax_total, { currency: currency })
end
|
#display_adjustment_total ⇒ Object
144
145
146
|
# File 'app/models/spree/order.rb', line 144
def display_adjustment_total
Spree::Money.new(adjustment_total, { currency: currency })
end
|
#display_included_tax_total ⇒ Object
148
149
150
|
# File 'app/models/spree/order.rb', line 148
def display_included_tax_total
Spree::Money.new(included_tax_total, { currency: currency })
end
|
#display_item_total ⇒ Object
140
141
142
|
# File 'app/models/spree/order.rb', line 140
def display_item_total
Spree::Money.new(item_total, { currency: currency })
end
|
#display_outstanding_balance ⇒ Object
136
137
138
|
# File 'app/models/spree/order.rb', line 136
def display_outstanding_balance
Spree::Money.new(outstanding_balance, { currency: currency })
end
|
#display_shipment_total ⇒ Object
Also known as:
display_ship_total
160
161
162
|
# File 'app/models/spree/order.rb', line 160
def display_shipment_total
Spree::Money.new(shipment_total, { currency: currency })
end
|
#display_tax_total ⇒ Object
156
157
158
|
# File 'app/models/spree/order.rb', line 156
def display_tax_total
Spree::Money.new(included_tax_total + additional_tax_total, { currency: currency })
end
|
#display_total ⇒ Object
165
166
167
|
# File 'app/models/spree/order.rb', line 165
def display_total
Spree::Money.new(total, { currency: currency })
end
|
#empty! ⇒ Object
439
440
441
442
443
444
445
446
|
# File 'app/models/spree/order.rb', line 439
def empty!
line_items.destroy_all
updater.update_item_count
adjustments.destroy_all
update_totals
persist_totals
end
|
#ensure_updated_shipments ⇒ Object
Clean shipments and make order back to address state
At some point the might need to force the order to transition from address to delivery again so that proper updated shipments are created. e.g. customer goes back from payment step and changes order items
505
506
507
508
509
510
511
|
# File 'app/models/spree/order.rb', line 505
def ensure_updated_shipments
if shipments.any? && !self.completed?
self.shipments.destroy_all
self.update_column(:shipment_total, 0)
restart_checkout_flow
end
end
|
#exclude_tax? ⇒ Boolean
Indicates whether tax should be backed out of the price calcualtions in cases where prices include tax but the customer is not required to pay taxes in that case.
217
218
219
220
|
# File 'app/models/spree/order.rb', line 217
def exclude_tax?
return false unless Spree::Config[:prices_inc_tax]
tax_zone != Zone.default_tax
end
|
#finalize! ⇒ Object
Finalizes an in progress order after checkout is complete. Called after transition to complete state when payments will have been processed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
# File 'app/models/spree/order.rb', line 331
def finalize!
all_adjustments.each{|a| a.close}
updater.update_payment_state
shipments.each do |shipment|
shipment.update!(self)
shipment.finalize!
end
updater.update_shipment_state
save
updater.run_hooks
touch :completed_at
deliver_order_confirmation_email unless confirmation_delivered?
consider_risk
end
|
#find_line_item_by_variant(variant) ⇒ Object
295
296
297
|
# File 'app/models/spree/order.rb', line 295
def find_line_item_by_variant(variant)
line_items.detect { |line_item| line_item.variant_id == variant.id }
end
|
#generate_order_number ⇒ Object
FIXME refactor this method and implement validation using validates_* utilities
272
273
274
275
276
277
278
279
280
|
# File 'app/models/spree/order.rb', line 272
def generate_order_number
record = true
while record
random = "R#{Array.new(9){rand(9)}.join}"
record = self.class.where(number: random).first
end
self.number = random if self.number.blank?
self.number
end
|
#has_step?(step) ⇒ Boolean
448
449
450
|
# File 'app/models/spree/order.rb', line 448
def has_step?(step)
checkout_steps.include?(step)
end
|
#insufficient_stock_lines ⇒ Object
411
412
413
|
# File 'app/models/spree/order.rb', line 411
def insufficient_stock_lines
line_items.select(&:insufficient_stock?)
end
|
#is_risky? ⇒ Boolean
534
535
536
537
538
539
540
541
|
# File 'app/models/spree/order.rb', line 534
def is_risky?
self.payments.where(%{
(avs_response IS NOT NULL and avs_response != '' and avs_response != 'D' and avs_response != 'M') or
(cvv_response_code IS NOT NULL and cvv_response_code != 'M') or
cvv_response_message IS NOT NULL and cvv_response_message != '' or
state = 'failed'
}.squish!).uniq.count > 0
end
|
#merge!(order, user = nil) ⇒ Object
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
# File 'app/models/spree/order.rb', line 415
def merge!(order, user = nil)
order.line_items.each do |line_item|
next unless line_item.currency == currency
current_line_item = self.line_items.find_by(variant: line_item.variant)
if current_line_item
current_line_item.quantity += line_item.quantity
current_line_item.save
else
line_item.order_id = self.id
line_item.save
end
end
self.associate_user!(user) if !self.user && !user.blank?
updater.update_item_count
updater.update_item_total
updater.persist_totals
order.line_items.reload
order.destroy
end
|
#name ⇒ Object
314
315
316
317
318
|
# File 'app/models/spree/order.rb', line 314
def name
if (address = bill_address || ship_address)
"#{address.firstname} #{address.lastname}"
end
end
|
#outstanding_balance ⇒ Object
306
307
308
|
# File 'app/models/spree/order.rb', line 306
def outstanding_balance
total - payment_total
end
|
#outstanding_balance? ⇒ Boolean
310
311
312
|
# File 'app/models/spree/order.rb', line 310
def outstanding_balance?
self.outstanding_balance != 0
end
|
#paid? ⇒ Boolean
Helper methods for checkout steps
359
360
361
|
# File 'app/models/spree/order.rb', line 359
def paid?
payment_state == 'paid' || payment_state == 'credit_owed'
end
|
#payment_required? ⇒ Boolean
Is this a free order in which case the payment step should be skipped
190
191
192
|
# File 'app/models/spree/order.rb', line 190
def payment_required?
total.to_f > 0.0
end
|
#pending_payments ⇒ Object
367
368
369
|
# File 'app/models/spree/order.rb', line 367
def pending_payments
payments.select { |payment| payment.checkout? || payment.pending? }
end
|
#process_payments! ⇒ Object
processes any pending payments and must return a boolean as it’s return value is used by the checkout state_machine to determine success or failure of the ‘complete’ event for the order
Returns:
-
true if all pending_payments processed successfully
-
true if a payment failed, ie. raised a GatewayError which gets rescued and converted to TRUE when :allow_checkout_gateway_error is set to true
-
false if a payment failed, ie. raised a GatewayError which gets rescued and converted to FALSE when :allow_checkout_on_gateway_error is set to false
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
# File 'app/models/spree/order.rb', line 384
def process_payments!
if pending_payments.empty?
raise Core::GatewayError.new Spree.t(:no_pending_payments)
else
pending_payments.each do |payment|
break if payment_total >= total
payment.process!
if payment.completed?
self.payment_total += payment.amount
end
end
end
rescue Core::GatewayError => e
result = !!Spree::Config[:allow_checkout_on_gateway_error]
errors.add(:base, e.message) and return result
end
|
#quantity_of(variant) ⇒ Object
290
291
292
293
|
# File 'app/models/spree/order.rb', line 290
def quantity_of(variant)
line_item = find_line_item_by_variant(variant)
line_item ? line_item.quantity : 0
end
|
#refresh_shipment_rates ⇒ Object
520
521
522
|
# File 'app/models/spree/order.rb', line 520
def refresh_shipment_rates
shipments.map &:refresh_rates
end
|
#reload ⇒ Object
589
590
591
592
|
# File 'app/models/spree/order.rb', line 589
def reload
remove_instance_variable(:@tax_zone) if defined?(@tax_zone)
super
end
|
#restart_checkout_flow ⇒ Object
513
514
515
516
517
518
|
# File 'app/models/spree/order.rb', line 513
def restart_checkout_flow
self.update_columns(
state: checkout_steps.first,
updated_at: Time.now,
)
end
|
#set_shipments_cost ⇒ Object
528
529
530
531
532
|
# File 'app/models/spree/order.rb', line 528
def set_shipments_cost
shipments.each(&:update_amounts)
updater.update_shipment_total
persist_totals
end
|
#shipped? ⇒ Boolean
477
478
479
|
# File 'app/models/spree/order.rb', line 477
def shipped?
%w(partial shipped).include?(shipment_state)
end
|
#shipped_shipments ⇒ Object
282
283
284
|
# File 'app/models/spree/order.rb', line 282
def shipped_shipments
shipments.shipped
end
|
#shipping_discount ⇒ Object
169
170
171
|
# File 'app/models/spree/order.rb', line 169
def shipping_discount
shipment_adjustments.eligible.sum(:amount) * - 1
end
|
#shipping_eq_billing_address? ⇒ Boolean
524
525
526
|
# File 'app/models/spree/order.rb', line 524
def shipping_eq_billing_address?
(bill_address.empty? && ship_address.empty?) || bill_address.same_as?(ship_address)
end
|
#state_changed(name) ⇒ Object
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
# File 'app/models/spree/order.rb', line 452
def state_changed(name)
state = "#{name}_state"
if persisted?
old_state = self.send("#{state}_was")
new_state = self.send(state)
unless old_state == new_state
self.state_changes.create(
previous_state: old_state,
next_state: new_state,
name: name,
user_id: self.user_id
)
end
end
end
|
#tax_address ⇒ Object
Returns the address for taxation based on configuration
223
224
225
|
# File 'app/models/spree/order.rb', line 223
def tax_address
Spree::Config[:tax_using_ship_address] ? ship_address : bill_address
end
|
#tax_zone ⇒ Object
Returns the relevant zone (if any) to be used for taxation purposes. Uses default tax zone unless there is a specific match
210
211
212
|
# File 'app/models/spree/order.rb', line 210
def tax_zone
@tax_zone ||= Zone.match(tax_address) || Zone.default_tax
end
|
#to_param ⇒ Object
173
174
175
|
# File 'app/models/spree/order.rb', line 173
def to_param
number.to_s.to_url.upcase
end
|
#update! ⇒ Object
231
232
233
|
# File 'app/models/spree/order.rb', line 231
def update!
updater.update
end
|
#update_line_items(line_item_params) ⇒ Object
moved from api order_decorator. This is a better place for it.
577
578
579
580
581
582
583
584
585
586
587
|
# File 'app/models/spree/order.rb', line 577
def update_line_items(line_item_params)
return if line_item_params.blank?
line_item_params.each_value do |attributes|
if attributes[:id].present?
self.line_items.find(attributes[:id]).update_attributes!(attributes)
else
self.line_items.create!(attributes)
end
end
self.ensure_updated_shipments
end
|
#updater ⇒ Object
227
228
229
|
# File 'app/models/spree/order.rb', line 227
def updater
@updater ||= OrderUpdater.new(self)
end
|