Class: Returnly::RefundCalculator

Inherits:
Object
  • Object
show all
Extended by:
RefundsConfiguration
Defined in:
lib/returnly/refund_calculator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RefundsConfiguration

gift_card_class, gift_card_estimate_class, refund_calculator_class, refund_presenter_class, refunder_class, return_item_amount_calculator_class, return_item_builder_class, return_item_restock_policy_class

Constructor Details

#initialize(order, line_items) ⇒ RefundCalculator

Returns a new instance of RefundCalculator.



10
11
12
13
# File 'lib/returnly/refund_calculator.rb', line 10

def initialize(order, line_items)
  self.line_items = line_items
  self.order = order
end

Instance Attribute Details

#line_itemsObject

Returns the value of attribute line_items.



3
4
5
# File 'lib/returnly/refund_calculator.rb', line 3

def line_items
  @line_items
end

#orderObject

Returns the value of attribute order.



3
4
5
# File 'lib/returnly/refund_calculator.rb', line 3

def order
  @order
end

Class Method Details

.process(order, line_items) ⇒ Object



6
7
8
# File 'lib/returnly/refund_calculator.rb', line 6

def self.process(order, line_items)
  refund_presenter_class.present_estimate new(order, line_items)
end

Instance Method Details

#discountObject



15
16
17
# File 'lib/returnly/refund_calculator.rb', line 15

def discount
  Returnly::DiscountCalculator.new(order).calculate(line_items)
end

#gift_cardObject



36
37
38
# File 'lib/returnly/refund_calculator.rb', line 36

def gift_card
  @gift_card ||= self.class.gift_card_estimate_class.new(order)
end

#line_item_tax(line_item) ⇒ Object



19
20
21
# File 'lib/returnly/refund_calculator.rb', line 19

def line_item_tax(line_item)
  (line_item.adjustments.tax.sum(&:amount).to_d / line_item.quantity).round(2, :down)
end

#line_items_return_itemsObject



23
24
25
26
27
28
29
30
# File 'lib/returnly/refund_calculator.rb', line 23

def line_items_return_items
  available_return_items.each_with_object({}) do |return_item, return_items|
    line_item_id = return_item.inventory_unit.line_item_id
    return_items[line_item_id] ||= []
    return_items[line_item_id] << set_tax(return_item)
    return_items
  end
end

#shipping_taxObject



32
33
34
# File 'lib/returnly/refund_calculator.rb', line 32

def shipping_tax
  order.all_adjustments.where(source_type: 'Spree::TaxRate', adjustable_type: 'Spree::Shipment').sum(&:amount).to_d.round(2, :down)
end