Sha256: ebfb7f9978535a3f9b06b861c84c311aa7cfcb5d14e0fec7d776beab9d92ffef

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

require_dependency "spree/calculator"

module SolidusFriendlyPromotions
  module Calculators
    class TieredFlatRate < Spree::Calculator
      include PromotionCalculator

      preference :base_amount, :decimal, default: 0
      preference :tiers, :hash, default: {10 => 10}
      preference :currency, :string, default: -> { Spree::Config[:currency] }

      before_validation do
        # Convert tier values to decimals. Strings don't do us much good.
        if preferred_tiers.is_a?(Hash)
          self.preferred_tiers = preferred_tiers.map do |key, value|
            [cast_to_d(key.to_s), cast_to_d(value.to_s)]
          end.to_h
        end
      end

      validate :preferred_tiers_content

      def compute_item(object)
        _base, amount = preferred_tiers.sort.reverse.detect do |value, _|
          object.discountable_amount >= value
        end

        if preferred_currency.casecmp(object.currency).zero?
          amount || preferred_base_amount
        else
          0
        end
      end
      alias_method :compute_shipment, :compute_item
      alias_method :compute_line_item, :compute_item

      private

      def cast_to_d(value)
        value.to_s.to_d
      rescue ArgumentError
        BigDecimal("0")
      end

      def preferred_tiers_content
        if preferred_tiers.is_a? Hash
          unless preferred_tiers.keys.all? { |key| key.is_a?(Numeric) && key > 0 }
            errors.add(:base, :keys_should_be_positive_number)
          end
        else
          errors.add(:preferred_tiers, :should_be_hash)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solidus_friendly_promotions-1.9.3 app/models/solidus_friendly_promotions/calculators/tiered_flat_rate.rb
solidus_friendly_promotions-1.9.2 app/models/solidus_friendly_promotions/calculators/tiered_flat_rate.rb
solidus_friendly_promotions-1.9.1 app/models/solidus_friendly_promotions/calculators/tiered_flat_rate.rb
solidus_friendly_promotions-1.9.0 app/models/solidus_friendly_promotions/calculators/tiered_flat_rate.rb