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

#calculatorObject

Returns the value of attribute calculator.



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

def calculator
  @calculator
end

#line_itemsObject

Returns the value of attribute line_items.



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

def line_items
  @line_items
end

#orderObject

Returns the value of attribute order.



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

def order
  @order
end

#restockObject

Returns the value of attribute restock.



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

def restock
  @restock
end

#transactionsObject

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_paramsObject



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

def customer_return_params
  {
      stock_location_id: stock_location_id,
  }
end

#perform_reimbursement(customer_return) ⇒ Object



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

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
# 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
    return_item.resellable = self.restock
  end

  if customer_return.save!
    reimbursement = perform_reimbursement(customer_return)
    processed_customer_return(customer_return, reimbursement)
  end
end

#processed_customer_return(customer_return, reimbursement) ⇒ Object



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

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_itemObject



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

def refund_amount_per_item
  refund_available_amount / line_items.size
end

#refund_available_amountObject



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

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_idObject



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

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