class Returnly::Refund::AmountCalculator

Attributes

refund[RW]

Public Class Methods

new(refund) click to toggle source
# File lib/returnly/refund/amount_calculator.rb, line 6
def initialize(refund)
  self.refund = refund
end

Public Instance Methods

return_item_refund_amount(return_item) click to toggle source
# File lib/returnly/refund/amount_calculator.rb, line 10
def return_item_refund_amount(return_item)
  (item_price(return_item) * item_price_percentage / 100).round(2, :down)
end

Protected Instance Methods

available_amount() click to toggle source
# File lib/returnly/refund/amount_calculator.rb, line 55
def available_amount
  refund.product_available_amount.round(2, :down)
end
discounter_calculator() click to toggle source
# File lib/returnly/refund/amount_calculator.rb, line 59
def discounter_calculator
  @discounter_calculator ||= Returnly::DiscountCalculator.new(refund.order)
end
item_price(return_item) click to toggle source
# File lib/returnly/refund/amount_calculator.rb, line 32
def item_price(return_item)
  line_item_price(return_item.inventory_unit.line_item)
end
item_price_percentage() click to toggle source
# File lib/returnly/refund/amount_calculator.rb, line 28
def item_price_percentage
  [(available_amount * 100 / total_single_items_price).round(2, :down), 100.0.to_d].min
end
line_item_discount_price(line_item) click to toggle source
# File lib/returnly/refund/amount_calculator.rb, line 40
def line_item_discount_price(line_item)
  discounter_calculator.calculate(
    [
      {
        order_line_item_id: line_item.id,
        units: 1
      }
    ]
  )
end
line_item_price(line_item) click to toggle source
# File lib/returnly/refund/amount_calculator.rb, line 24
def line_item_price(line_item)
  (line_item.price + line_item_tax_price(line_item) + line_item_discount_price(line_item)).round(2, :down)
end
line_item_tax_price(line_item) click to toggle source
# File lib/returnly/refund/amount_calculator.rb, line 36
def line_item_tax_price(line_item)
  (line_item.adjustments.tax.sum(&:amount).to_d.round(2, :down) / line_item.quantity).round(2, :down)
end
line_items_price() click to toggle source
# File lib/returnly/refund/amount_calculator.rb, line 51
def line_items_price
  refund.order.line_items.sum(&:price).round(2, :down)
end
total_single_items_price() click to toggle source
# File lib/returnly/refund/amount_calculator.rb, line 16
def total_single_items_price
  refund.line_items.inject(0.0.to_d) do |amount, line_item_hash|
    line_item = Spree::LineItem.find(line_item_hash['order_line_item_id'])
    amount += (line_item_price(line_item) * line_item_hash['units'].to_d).round(2, :down)
    amount
  end.round(2, :down)
end