Sha256: 2a4630e5302fa388a17b08ef326c8f237765004e6020576cf82fee98d1d40cd4

Contents?: true

Size: 1.81 KB

Versions: 62

Compression:

Stored size: 1.81 KB

Contents

module Workarea
  module Pricing
    class Discount
      # This class does the calculation of how much each item should
      # receive off for a {QuantityFixedPrice}.
      #
      # It acts like a hash, where the key is the item id and the value
      # is the amount that item should receive off.
      #
      class QuantityFixedPrice::ItemShares
        delegate :[], :each, to: :hash
        delegate :price, :quantity, :product_ids, :category_ids, to: :@discount

        def initialize(discount, order, applications)
          @discount = discount
          @order = order
          @applications = applications
        end

        private

        def hash
          @hash ||=
            begin
              results = Hash.new(0.to_m)

              @applications.times do |i|
                current_units = qualified_units.slice!(0, quantity)
                total_price = current_units.sum { |u| u[:price] }
                total_value = total_price - price

                distribution = PriceDistributor.new(total_value, current_units)
                distribution.each do |id, value|
                  results[id] += value
                end
              end

              results
            end
        end

        def qualified_units
          @qualified_units ||=
            begin
              result = []

              @order.items.each do |item|
                next unless item.matches_products?(product_ids) ||
                  item.matches_categories?(category_ids)

                item.quantity.times do
                  result << { id: item.id, price: item.current_unit_price }
                end
              end

              result.sort! { |a, b| b[:price] <=> a[:price] }
              result.pop until result.length % quantity == 0
              result
            end
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

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