Sha256: 3dc4cdf9f8579bb5386917120926c83e99ca674d80dbc2425e6264a66465d1dc

Contents?: true

Size: 1007 Bytes

Versions: 61

Compression:

Stored size: 1007 Bytes

Contents

module Workarea
  module Pricing
    class Override
      include ApplicationDocument

      # This will be the order id
      field :_id, type: String, default: -> { BSON::ObjectId.new.to_s }
      field :created_by_id, type: String
      field :subtotal_adjustment, type: Money, default: 0
      field :shipping_adjustment, type: Money, default: 0
      field :item_prices, type: Hash, default: {}

      def has_adjustments?
        adjusts_shipping? || adjusts_subtotal? || adjusts_items?
      end

      def adjusts_shipping?
        shipping_adjustment.present? && !shipping_adjustment.zero?
      end

      def adjusts_subtotal?
        subtotal_adjustment.present? && !subtotal_adjustment.zero?
      end

      def adjusts_items?
        item_prices.present? &&
          item_prices.values.reject(&:blank?).any?(&:present?)
      end

      def item_price_for_id(item_id)
        price = item_prices.fetch(item_id.to_s, nil)
        price.present? ? price.to_m : nil
      end
    end
  end
end

Version data entries

61 entries across 61 versions & 1 rubygems

Version Path
workarea-core-3.4.13 app/models/workarea/pricing/override.rb