Sha256: 310c335d290b75830d4417bf187d0218270f7855288a6477db6328aaffd78f09

Contents?: true

Size: 785 Bytes

Versions: 2

Compression:

Stored size: 785 Bytes

Contents

module Spree
  class Calculator::FlexiRate < Calculator
    preference :first_item,      :decimal, :default => 0.0
    preference :additional_item, :decimal, :default => 0.0
    preference :max_items,       :decimal, :default => 0.0

    def self.description
      I18n.t(:flexible_rate)
    end

    def self.available?(object)
      true
    end

    def compute(object)
      sum = 0
      max = self.preferred_max_items
      items_count = object.line_items.map(&:quantity).sum
      items_count.times do |i|
        # check max value to avoid divide by 0 errors
        if (max == 0 && i == 0) || (max > 0) && (i % max == 0)
          sum += self.preferred_first_item
        else
          sum += self.preferred_additional_item
        end
      end

      sum
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spree_core-1.0.0.rc2 app/models/spree/calculator/flexi_rate.rb
spree_core-1.0.0.rc1 app/models/spree/calculator/flexi_rate.rb