Sha256: 981064fe607d8350ecb80315d21de8433d2813edcb9873cdcb6cd3cbeea318a0

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

module Workarea
  decorate Pricing::Discount, with: :global_e do
    # Fixed price orders can only handle percentage discounts or free gifts
    # a future improvement would be for FlatOrPercentOff to localize the amount field
    # to allow for fixed price orders to handle flat off amounts
    #
    def qualifies?(order)
      return super unless order.fixed_pricing?
      return super if is_percentage_discount? || is_free_gift?

      false
    end

    # Build the data {Hash} used to create the price
    # adjustment.
    #
    # This is the same method from Core except that the 0.to_m uses the same
    # currency as `value`
    #
    # @param [Money] value
    # @param [Integer] quantity
    # @return [Hash]
    #
    def adjustment_data(value, quantity)
      value = value.abs

      {
        price: self.class.price_level,
        description: name,
        calculator: self.class.name,
        amount: 0.to_m(value.currency) - value,
        quantity: quantity,
        data: {
          'discount_id' => id.to_s,
          'discount_value' => value.to_f
        }
      }
    end

    def remove_from_items(items)
      items.each do |item|
        keepers = item.price_adjustments.reject do |adjustment|
          adjustment.data['discount_id'] == id.to_s
        end

        international_keepers = item.international_price_adjustments.reject do |adjustment|
          adjustment.data['discount_id'] == id.to_s
        end

        item.price_adjustments = keepers
        item.international_price_adjustments = international_keepers
      end
    end

    private

      def is_percentage_discount?
        self.is_a?(Workarea::Pricing::Discount::FlatOrPercentOff) && amount_type == :percent
      end

      def is_free_gift?
        self.is_a?(Workarea::Pricing::Discount::FreeGift)
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workarea-global_e-1.3.0 app/models/workarea/pricing/discount.decorator
workarea-global_e-1.2.1 app/models/workarea/pricing/discount.decorator