Class: InterventionParameter::AmountComputation

Inherits:
Object
  • Object
show all
Defined in:
app/models/intervention_parameter/amount_computation.rb

Overview

Amount computation represents how a amount is computed for a cost or an earn in an intervention

Constant Summary collapse

NATURES =
[:failed, :none, :quantity].freeze
ORIGINS =
[:catalog, :purchase, :sale].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nature, origin = nil, options = {}) ⇒ AmountComputation

Returns a new instance of AmountComputation.


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/intervention_parameter/amount_computation.rb', line 22

def initialize(nature, origin = nil, options = {})
  unless NATURES.include?(nature)
    raise ArgumentError, "Invalid nature. Got: #{nature.inspect}"
  end
  @nature = nature
  if quantity?
    unless ORIGINS.include?(origin)
      raise ArgumentError, "Invalid origin. Got: #{origin.inspect}"
    end
    @origin = origin
  end
  @options = options
  @options[:quantity] ||= 0
  check_option_presence!(:quantity, :unit_name) if quantity?
  check_option_presence!(:catalog_usage) if catalog?
  check_option_presence!(:purchase_item) if purchase?
  check_option_presence!(:sale_item) if sale?
end

Class Method Details

.failedObject


9
10
11
# File 'app/models/intervention_parameter/amount_computation.rb', line 9

def failed
  new(:failed)
end

.noneObject


13
14
15
# File 'app/models/intervention_parameter/amount_computation.rb', line 13

def none
  new(:none)
end

.quantity(origin, options = {}) ⇒ Object


17
18
19
# File 'app/models/intervention_parameter/amount_computation.rb', line 17

def quantity(origin, options = {})
  new(:quantity, origin, options)
end

Instance Method Details

#amountObject


84
85
86
# File 'app/models/intervention_parameter/amount_computation.rb', line 84

def amount
  (unit_amount * quantity).to_i
end

#amount?Boolean

Returns:

  • (Boolean)

80
81
82
# File 'app/models/intervention_parameter/amount_computation.rb', line 80

def amount?
  unit_amount?
end

#currencyObject

FIXME: Not suitable more multi-money support


60
61
62
# File 'app/models/intervention_parameter/amount_computation.rb', line 60

def currency
  Nomen::Currency.find(Preference[:currency])
end

#human_amountObject


88
89
90
# File 'app/models/intervention_parameter/amount_computation.rb', line 88

def human_amount
  amount.round(0).l(currency: currency.name, precision: 0)
end

#human_unit_amount(_options = {}) ⇒ Object


64
65
66
# File 'app/models/intervention_parameter/amount_computation.rb', line 64

def human_unit_amount(_options = {})
  unit_amount.round(currency.precision).l(currency: currency.name, precision: currency.precision)
end

#itemObject


68
69
70
# File 'app/models/intervention_parameter/amount_computation.rb', line 68

def item
  @origin ? send("#{@origin}_item") : nil
end

#unit_amountObject


76
77
78
# File 'app/models/intervention_parameter/amount_computation.rb', line 76

def unit_amount
  item ? item.unit_pretax_amount : 0.0
end

#unit_amount?Boolean

Returns:

  • (Boolean)

72
73
74
# File 'app/models/intervention_parameter/amount_computation.rb', line 72

def unit_amount?
  item.present?
end