Class: AdditionalCalculator::WeightAndQuantity

Inherits:
Base
  • Object
show all
Defined in:
app/models/additional_calculator/weight_and_quantity.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descriptionObject

The description of the calculator



6
7
8
# File 'app/models/additional_calculator/weight_and_quantity.rb', line 6

def self.description
  I18n.t('calculator_names.weight_and_quantity')
end

Instance Method Details

#available?(object) ⇒ Boolean

check if this calculator is available for the Order

Returns:

  • (Boolean)


44
45
46
# File 'app/models/additional_calculator/weight_and_quantity.rb', line 44

def available?(object)
  !self.compute(object).nil?
end

#compute(object) ⇒ Object

object can be [Order, Shipment] and maybe something else …



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/additional_calculator/weight_and_quantity.rb', line 11

def compute(object)
  line_items = object_to_line_items(object)
  return nil if line_items.nil?

  total_qnty = get_total_qnty(line_items)
  total_weight = get_total_weight(line_items)
  weight_rate = get_rate(total_weight, AdditionalCalculatorRate::WEIGHT)

  # sometimes the compute method is called without checking if the calculator is available
  if weight_rate.nil?
    logger.warn("The calculator's #{name} weight_rate is nil - returning. Availability is not checked!")
    return nil
  end

  # quantity rate might be nil if the specified range is not available
  qnty_rate = get_rate(total_qnty, AdditionalCalculatorRate::QNTY)
  # find the previous qnty rate or set it to 0 if not found
  previous_qnty_rate = get_previous_rate(total_qnty, AdditionalCalculatorRate::QNTY)

  # the total rate is sum of weight and quantity rates
  # the rate is available if
  # 1) qnty_rate is not nil
  # 2) qnty_rate is nil AND previous_qnty_rate is nil (quantity configuration not defined at all)
  if !qnty_rate.nil?
    weight_rate + qnty_rate
  elsif previous_qnty_rate.nil?
    weight_rate
  else
    nil
  end
end