Sha256: e2cb7dc1d4031a270e701c630293fd4a1715740cef8373838570f6332899cfeb

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

require_dependency "spree/calculator"

# This is a calculator for line item adjustment actions. It accepts a line item
# and calculates its weighted adjustment amount based on the value of the
# preferred amount and the price of the other line items. More expensive line
# items will receive a greater share of the preferred amount.
module SolidusFriendlyPromotions
  module Calculators
    class DistributedAmount < Spree::Calculator
      include PromotionCalculator

      preference :amount, :decimal, default: 0
      preference :currency, :string, default: -> { Spree::Config[:currency] }

      def compute_line_item(line_item)
        return 0 unless line_item
        return 0 unless preferred_currency.casecmp(line_item.currency).zero?

        distributable_line_items = calculable.promotion.applicable_line_items(line_item.order)
        return 0 unless line_item.in?(distributable_line_items)

        DistributedAmountsHandler.new(
          distributable_line_items,
          preferred_amount
        ).amount(line_item)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solidus_friendly_promotions-1.0.0 app/models/solidus_friendly_promotions/calculators/distributed_amount.rb
solidus_friendly_promotions-1.0.0.rc.3 app/models/solidus_friendly_promotions/calculators/distributed_amount.rb
solidus_friendly_promotions-1.0.0.rc.2 app/models/solidus_friendly_promotions/calculators/distributed_amount.rb