Class: Workarea::Pricing::Discount::ReconcileTotal

Inherits:
Object
  • Object
show all
Defined in:
app/models/workarea/pricing/discount/reconcile_total.rb

Instance Method Summary collapse

Constructor Details

#initialize(price_adjustments) ⇒ ReconcileTotal

Returns a new instance of ReconcileTotal.



5
6
7
# File 'app/models/workarea/pricing/discount/reconcile_total.rb', line 5

def initialize(price_adjustments)
  @price_adjustments = price_adjustments
end

Instance Method Details

#over_discounted?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'app/models/workarea/pricing/discount/reconcile_total.rb', line 9

def over_discounted?
  over_discounted_amount > 0
end

#over_discounted_amountObject



13
14
15
# File 'app/models/workarea/pricing/discount/reconcile_total.rb', line 13

def over_discounted_amount
  0.to_m - @price_adjustments.sum
end

#performObject



17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/workarea/pricing/discount/reconcile_total.rb', line 17

def perform
  units = discount_adjustments.map do |adjustment|
    { id: adjustment.id, price: adjustment.amount.abs }
  end

  distributor = PriceDistributor.new(over_discounted_amount, units)
  distributor.results.each do |id, value|
    adjustment = discount_adjustments.detect { |a| a.id == id }
    adjustment.amount += value
  end
end