Class: Spree::UnreturnedItemCharger

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/core/unreturned_item_charger.rb

Defined Under Namespace

Classes: ChargeFailure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shipment, return_items) ⇒ UnreturnedItemCharger

Returns a new instance of UnreturnedItemCharger.



15
16
17
18
19
# File 'lib/spree/core/unreturned_item_charger.rb', line 15

def initialize(shipment, return_items)
  @shipment = shipment
  @original_order = @shipment.order
  @return_items = return_items
end

Instance Attribute Details

#original_orderObject (readonly)

Returns the value of attribute original_order.



13
14
15
# File 'lib/spree/core/unreturned_item_charger.rb', line 13

def original_order
  @original_order
end

Instance Method Details

#charge_for_itemsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/spree/core/unreturned_item_charger.rb', line 21

def charge_for_items
  new_order.associate_user!(@original_order.user) if @original_order.user

  add_exchange_variants_to_order
  set_shipment_for_new_order

  new_order.update!
  set_order_payment

  # There are several checks in the order state machine to skip
  # certain transitions when an order is an unreturned exchange
  if !new_order.unreturned_exchange?
    raise ChargeFailure.new('order is not an unreturned exchange', new_order)
  end

  # Transitions will call update_totals on the order
  until new_order.can_complete?
    new_order.next!
  end

  new_order.contents.approve(name: self.class.name)
  new_order.complete!
  Spree::OrderCapturing.new(new_order).capture_payments if (Spree::Config[:auto_capture_exchanges] && !Spree::Config[:auto_capture])

  @return_items.each(&:expired!)
  create_new_rma if Spree::Config[:create_rma_for_unreturned_exchange]

  if !new_order.completed?
    raise ChargeFailure.new('order not complete', new_order)
  elsif !new_order.valid?
    raise ChargeFailure.new('order not valid', new_order)
  end

end