Sha256: 14e6c169955b8d35efa40dff1a2f474cdf07332fa1166823c6fb823951d0560a

Contents?: true

Size: 615 Bytes

Versions: 14

Compression:

Stored size: 615 Bytes

Contents

module Returnly
  module Discounts
    class LineItem
      attr_reader :adjustment, :order

      def initialize(order, adjustment)
        @adjustment = adjustment
        @order      = order
      end

      def discount_amount(line_item, units = 0)
        return 0.0 if units <= 0
        return 0.0 if adjustment.adjustable != line_item

        units = line_item.quantity if units > line_item.quantity
        adjustment.amount * weight_of(line_item, units.to_d)
      end

      private

      def weight_of(line_item, units)
        (units / line_item.quantity).round(2, :down)
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
spree-returnly-0.13.1 lib/returnly/discounts/line_item.rb
solidus-returnly-0.13.1 lib/returnly/discounts/line_item.rb
spree-returnly-0.13.0 lib/returnly/discounts/line_item.rb
solidus-returnly-0.13.0 lib/returnly/discounts/line_item.rb
solidus-returnly-0.12.0 lib/returnly/discounts/line_item.rb
solidus-returnly-0.11.0 lib/returnly/discounts/line_item.rb
solidus-returnly-0.10.0 lib/returnly/discounts/line_item.rb
solidus-returnly-0.9.0 lib/returnly/discounts/line_item.rb
solidus-returnly-0.8.1 lib/returnly/discounts/line_item.rb
solidus-returnly-0.8.0 lib/returnly/discounts/line_item.rb
solidus-returnly-0.7.1 lib/returnly/discounts/line_item.rb
solidus-returnly-0.7.0 lib/returnly/discounts/line_item.rb
solidus-returnly-0.6.2 lib/returnly/discounts/line_item.rb
solidus-returnly-0.6.1 lib/returnly/discounts/line_item.rb