Sha256: a99ded569b6a34202b17fe996ce91cec162c9208f01fcfa04a96cad7551b417f

Contents?: true

Size: 1.88 KB

Versions: 4

Compression:

Stored size: 1.88 KB

Contents

module Returnly
  class RefundCalculator
    attr_accessor :line_items, :order
    extend ::Returnly::RefundsConfiguration

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

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

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

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

    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

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

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

    protected

    def available_return_items
      self.class.return_item_builder_class.new(order).build_by_requested_line_items(line_items)
    end

    def set_tax(return_item)
      percent_of_tax = if return_item.amount <= 0
                         0
                       else
                         return_item.amount / Spree::ReturnItem.refund_amount_calculator.new.compute(return_item)
                       end

      additional_tax_total = percent_of_tax * return_item.inventory_unit.additional_tax_total
      included_tax_total = percent_of_tax * return_item.inventory_unit.included_tax_total

      return_item.additional_tax_total = additional_tax_total
      return_item.included_tax_total = included_tax_total

      return_item
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
spree-returnly-0.13.1 lib/returnly/refund_calculator.rb
solidus-returnly-0.13.1 lib/returnly/refund_calculator.rb
spree-returnly-0.13.0 lib/returnly/refund_calculator.rb
solidus-returnly-0.13.0 lib/returnly/refund_calculator.rb