Class: Solidus::Returnly::Refunds

Inherits:
Object
  • Object
show all
Defined in:
lib/solidus/returnly/refunds.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order:, line_items:, restock: true, transactions:) ⇒ Refunds

Returns a new instance of Refunds.



6
7
8
9
10
11
12
# File 'lib/solidus/returnly/refunds.rb', line 6

def initialize(order:, line_items:, restock: true, transactions:)
  self.order = order
  self.line_items = line_items
  self.calculator = Calculator.new order, line_items
  self.restock = restock
  self.transactions = transactions
end

Instance Attribute Details

#calculator ⇒ Object

Returns the value of attribute calculator.



4
5
6
# File 'lib/solidus/returnly/refunds.rb', line 4

def calculator
  @calculator
end

#line_items ⇒ Object

Returns the value of attribute line_items.



4
5
6
# File 'lib/solidus/returnly/refunds.rb', line 4

def line_items
  @line_items
end

#order ⇒ Object

Returns the value of attribute order.



4
5
6
# File 'lib/solidus/returnly/refunds.rb', line 4

def order
  @order
end

#restock ⇒ Object

Returns the value of attribute restock.



4
5
6
# File 'lib/solidus/returnly/refunds.rb', line 4

def restock
  @restock
end

#transactions ⇒ Object

Returns the value of attribute transactions.



4
5
6
# File 'lib/solidus/returnly/refunds.rb', line 4

def transactions
  @transactions
end

Instance Method Details

#customer_return_params ⇒ Object



64
65
66
67
68
# File 'lib/solidus/returnly/refunds.rb', line 64

def customer_return_params
  {
      stock_location_id: stock_location_id,
  }
end

#perform_reimbursement(customer_return) ⇒ Object



57
58
59
60
61
62
# File 'lib/solidus/returnly/refunds.rb', line 57

def perform_reimbursement(customer_return)
  reimbursement = Spree::Reimbursement.build_from_customer_return(customer_return)
  reimbursement.save!
  reimbursement.perform!
  reimbursement
end

#proceed! ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/solidus/returnly/refunds.rb', line 20

def proceed!
  customer_return = Spree::CustomerReturn.new customer_return_params
  customer_return.return_items = calculator.line_items_return_items.values.flatten

  customer_return.return_items.each do |return_item|
    return_item.amount = refund_amount_per_item
  end

  if customer_return.save!
    reimbursement = perform_reimbursement(customer_return)
    restock_return(customer_return) if self.restock

    processed_customer_return(customer_return, reimbursement)
  end
end

#processed_customer_return(customer_return, reimbursement) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/solidus/returnly/refunds.rb', line 36

def processed_customer_return(customer_return, reimbursement)
  {
      refund_id: customer_return.number,
      created_at: customer_return.created_at,
      updated_at: customer_return.updated_at,
      line_items: customer_return.return_items.map {|ri|
        {
            refund_line_item_id: ri.id,
            order_line_item_id: ri.inventory_unit.line_item.id,
            quantity: 1
        }
      },
      transactions: [
          id: reimbursement.id,
          amount: reimbursement.total
      ],
      note: "comment text",
      restock: true
  }
end

#refund_amount_per_item ⇒ Object



78
79
80
# File 'lib/solidus/returnly/refunds.rb', line 78

def refund_amount_per_item
  refund_available_amount / line_items.size
end

#refund_available_amount ⇒ Object



74
75
76
# File 'lib/solidus/returnly/refunds.rb', line 74

def refund_available_amount
  @available_amount ||= self.transactions.sum {|t| Money.from_amount(t[:amount].to_f)}
end

#restock_return(customer_return) ⇒ Object



14
15
16
17
18
# File 'lib/solidus/returnly/refunds.rb', line 14

def restock_return(customer_return)
  customer_return.return_items.each do |item|
    customer_return.stock_location.restock(item.inventory_unit.variant, 1, customer_return)
  end
end

#stock_location_id ⇒ Object



70
71
72
# File 'lib/solidus/returnly/refunds.rb', line 70

def stock_location_id
  order.shipments.last.stock_location.id
end