Sha256: 661969a3b521bc99457a50345d40ce91f229de855f3eba3a3a9cd54f05b2b726

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

module Spree
  class ShippingRate < Spree::Base
    belongs_to :shipment, class_name: 'Spree::Shipment'
    belongs_to :tax_rate, -> { with_deleted }, class_name: 'Spree::TaxRate'
    belongs_to :shipping_method, -> { with_deleted }, class_name: 'Spree::ShippingMethod', inverse_of: :shipping_rates
    extend Spree::DisplayMoney

    money_methods :base_price, :final_price, :tax_amount

    delegate :order, :currency, :free?, to: :shipment
    delegate :name,             to: :shipping_method
    delegate :code,             to: :shipping_method, prefix: true

    def display_price
      price = display_base_price.to_s

      return price if tax_rate.nil? || tax_amount.zero? || !tax_rate.show_rate_in_label

      Spree.t(
        tax_rate.included_in_price? ? :including_tax : :excluding_tax,
        scope: 'shipping_rates.display_price',
        price: price,
        tax_amount: display_tax_amount,
        tax_rate_name: tax_rate.name
      )
    end
    alias display_cost display_price
    alias_attribute :base_price, :cost

    def tax_amount
      @tax_amount ||= tax_rate&.calculator&.compute_shipping_rate(self) || BigDecimal(0)
    end

    # returns base price - any available discounts for this Shipment
    # useful when you want to present a list of available shipping rates
    def final_price
      if free? || cost < -discount_amount
        BigDecimal(0)
      else
        cost + discount_amount
      end
    end

    private

    def discount_amount
      shipment.adjustments.promotion.sum(:amount)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spree_core-4.10.1 app/models/spree/shipping_rate.rb
spree_core-4.10.0 app/models/spree/shipping_rate.rb
spree_core-4.9.0 app/models/spree/shipping_rate.rb