Sha256: e4e769374271c6c584c6102603291140fafa0a7120bb4ae3165a7162126b05ec

Contents?: true

Size: 1.46 KB

Versions: 62

Compression:

Stored size: 1.46 KB

Contents

module Workarea
  class PriceAdjustmentSet < Array
    def select(*args)
      self.class.new(super)
    end

    def reject(*args)
      self.class.new(super)
    end

    def adjusting(type)
      select { |a| a.price == type }
    end

    def sum
      super(&:amount).to_m
    end

    def discounts
      select(&:discount?)
    end

    def +(val)
      self.class.new(to_a + val.to_a)
    end

    def reduce_by_description(type)
      amounts = adjusting(type).reduce({}) do |memo, adjustment|
        memo[adjustment.description] ||= 0.to_m
        memo[adjustment.description] += adjustment.amount
        memo
      end

      self.class.new(
        amounts.keys.map do |description|
          PriceAdjustment.new(
            description: description,
            amount: amounts[description]
          )
        end
      )
    end

    def taxable_share_for(adjustment)
      return 0.to_m if taxable_total.zero?

      discount_share = adjustment.amount / taxable_total
      discount_amount = discount_total * discount_share
      adjustment.amount - discount_amount
    end

    def grouped_by_parent
      each_with_object({}) do |adjustment, memo|
        memo[adjustment._parent] ||= PriceAdjustmentSet.new
        memo[adjustment._parent] << adjustment
      end
    end

    private

    def taxable_total
      reject { |a| a.discount? || a.data['tax_code'].blank? }.sum(&:amount).to_m
    end

    def discount_total
      discounts.sum(&:amount).to_m.abs
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

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