Class: Workarea::PriceAdjustmentSet

Inherits:
Array
  • Object
show all
Defined in:
app/models/workarea/price_adjustment_set.rb

Instance Method Summary collapse

Instance Method Details

#+(val) ⇒ Object



23
24
25
# File 'app/models/workarea/price_adjustment_set.rb', line 23

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

#adjusting(type) ⇒ Object



11
12
13
# File 'app/models/workarea/price_adjustment_set.rb', line 11

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

#discountsObject



19
20
21
# File 'app/models/workarea/price_adjustment_set.rb', line 19

def discounts
  select(&:discount?)
end

#grouped_by_parentObject



52
53
54
55
56
57
# File 'app/models/workarea/price_adjustment_set.rb', line 52

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

#reduce_by_description(type) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/workarea/price_adjustment_set.rb', line 27

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

#reject(*args) ⇒ Object



7
8
9
# File 'app/models/workarea/price_adjustment_set.rb', line 7

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

#select(*args) ⇒ Object



3
4
5
# File 'app/models/workarea/price_adjustment_set.rb', line 3

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

#sumObject



15
16
17
# File 'app/models/workarea/price_adjustment_set.rb', line 15

def sum
  super(&:amount).to_m
end

#taxable_share_for(adjustment) ⇒ Object



44
45
46
47
48
49
50
# File 'app/models/workarea/price_adjustment_set.rb', line 44

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