Sha256: dcaf834a6c4444cde547cd863b9c15f497ac11b2c35aaea19b78abc60597b1e9

Contents?: true

Size: 1.16 KB

Versions: 15

Compression:

Stored size: 1.16 KB

Contents

require_dependency 'spree/shipping_calculator'

module Spree
  module Calculator::Shipping
    class FlexiRate < ShippingCalculator
      preference :first_item,      :decimal, :default => 0.0
      preference :additional_item, :decimal, :default => 0.0
      preference :max_items,       :integer, :default => 0
      preference :currency,        :string,  :default => Spree::Config[:currency]

      attr_accessible :preferred_first_item,
                      :preferred_additional_item,
                      :preferred_max_items,
                      :preferred_currency

      def self.description
        Spree.t(:shipping_flexible_rate)
      end

      def compute_package(package)
        content_items = package.contents
        sum = 0
        max = self.preferred_max_items.to_i
        items_count = content_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.to_f
          else
            sum += self.preferred_additional_item.to_f
          end
        end

        sum
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
spree_core-2.0.13 app/models/spree/calculator/shipping/flexi_rate.rb
spree_core-2.0.12 app/models/spree/calculator/shipping/flexi_rate.rb
spree_core-2.0.11 app/models/spree/calculator/shipping/flexi_rate.rb
spree_core-2.0.10 app/models/spree/calculator/shipping/flexi_rate.rb
spree_core-2.0.9 app/models/spree/calculator/shipping/flexi_rate.rb
spree_core-2.0.8 app/models/spree/calculator/shipping/flexi_rate.rb
spree_core-2.0.7 app/models/spree/calculator/shipping/flexi_rate.rb
spree_core-2.0.6 app/models/spree/calculator/shipping/flexi_rate.rb
spree_core-2.0.5 app/models/spree/calculator/shipping/flexi_rate.rb
spree_core-2.0.4 app/models/spree/calculator/shipping/flexi_rate.rb
spree_core-2.0.3 app/models/spree/calculator/shipping/flexi_rate.rb
spree_core-2.0.2 app/models/spree/calculator/shipping/flexi_rate.rb
spree_core-2.0.1 app/models/spree/calculator/shipping/flexi_rate.rb
spree_core-2.0.0 app/models/spree/calculator/shipping/flexi_rate.rb
spree_core-2.0.0.rc1 app/models/spree/calculator/shipping/flexi_rate.rb