Sha256: d7d25189378a6f49bedd6624720a317e7cdd2cd7adacb959845181318acb3b00

Contents?: true

Size: 1.53 KB

Versions: 62

Compression:

Stored size: 1.53 KB

Contents

module Workarea
  module Pricing
    # Build price adjustments and set order total prices.
    # Does nothing if the order isn't valid.
    #
    # @param [Workarea::Order] order
    # @return [self]
    #
    def self.perform(order, shippings = nil)
      shippings = Array(shippings)
      return self unless order.save && shippings.all?(&:save)

      request = Request.new(order, shippings)
      request.run
      request.save!

      self
    end

    # Find the price adjustments for shipping discounts that
    # would apply to a given order.
    #
    # Used in {Checkout::ShippingOptions} to determine
    # the prices post-discounting for available shipping
    # methods.
    #
    # @param [Workarea::Order]
    # @return [Array<PriceAdjustment>]
    #
    def self.find_shipping_discounts(order, shipping)
      request = Request.new(order, shipping)
      request.run
      request.shippings.first.price_adjustments.adjusting('shipping').discounts
    end

    # Find whether a promo code is valid to apply
    #
    # @param [String] promo code
    # @return [Boolean] whether it is valid
    #
    def self.valid_promo_code?(promo_code, email = nil)
      discounts = Discount
                    .where(:promo_codes.in => [promo_code.downcase])
                    .to_a

      return true if discounts.any? do |discount|
        discount.active? &&
          (email.blank? ||
           !discount.single_use? ||
           !discount.has_been_redeemed?(email))
      end

      Discount::GeneratedPromoCode.valid_code?(promo_code)
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/pricing.rb
workarea-core-3.5.26 app/models/workarea/pricing.rb
workarea-core-3.4.45 app/models/workarea/pricing.rb
workarea-core-3.5.25 app/models/workarea/pricing.rb
workarea-core-3.5.23 app/models/workarea/pricing.rb
workarea-core-3.4.44 app/models/workarea/pricing.rb
workarea-core-3.5.22 app/models/workarea/pricing.rb
workarea-core-3.4.43 app/models/workarea/pricing.rb
workarea-core-3.5.21 app/models/workarea/pricing.rb
workarea-core-3.4.42 app/models/workarea/pricing.rb
workarea-core-3.5.20 app/models/workarea/pricing.rb
workarea-core-3.4.41 app/models/workarea/pricing.rb
workarea-core-3.5.19 app/models/workarea/pricing.rb
workarea-core-3.4.40 app/models/workarea/pricing.rb
workarea-core-3.5.18 app/models/workarea/pricing.rb
workarea-core-3.4.39 app/models/workarea/pricing.rb
workarea-core-3.5.17 app/models/workarea/pricing.rb
workarea-core-3.4.38 app/models/workarea/pricing.rb
workarea-core-3.5.16 app/models/workarea/pricing.rb
workarea-core-3.4.37 app/models/workarea/pricing.rb