Class: Cats::Core::RhnRequest

Inherits:
ApplicationRecord show all
Includes:
Dispatchable
Defined in:
app/models/cats/core/rhn_request.rb

Constant Summary collapse

ALLOCATED =
"Allocated".freeze

Constants included from Dispatchable

Dispatchable::APPROVED, Dispatchable::COMPLETED, Dispatchable::DRAFT, Dispatchable::STATUSES

Instance Method Summary collapse

Instance Method Details

#allocateObject

Raises:

  • (StandardError)


77
78
79
80
81
82
# File 'app/models/cats/core/rhn_request.rb', line 77

def allocate
  raise(StandardError, "Request is not completed.") unless status == COMPLETED

  self.status = ALLOCATED
  save!
end

#approveObject

Raises:

  • (StandardError)


63
64
65
66
67
68
# File 'app/models/cats/core/rhn_request.rb', line 63

def approve
  raise(StandardError, "Request is not in draft state.") unless status == DRAFT

  self.status = APPROVED
  save!
end

#completeObject

Raises:

  • (StandardError)


70
71
72
73
74
75
# File 'app/models/cats/core/rhn_request.rb', line 70

def complete
  raise(StandardError, "Request is not approved.") unless status == APPROVED

  self.status = COMPLETED
  save!
end

#request_quantityObject



35
36
37
# File 'app/models/cats/core/rhn_request.rb', line 35

def request_quantity
  quantity
end

#request_referenceObject



31
32
33
# File 'app/models/cats/core/rhn_request.rb', line 31

def request_reference
  reference_no
end

#set_unitObject



23
24
25
26
27
28
29
# File 'app/models/cats/core/rhn_request.rb', line 23

def set_unit
  return if unit

  return unless commodity

  self.unit = commodity.unit
end

#validate_commodity_statusObject



39
40
41
42
43
# File 'app/models/cats/core/rhn_request.rb', line 39

def validate_commodity_status
  return unless commodity

  errors.add(:commodity, "should be approved first.") unless commodity.status == Commodity::APPROVED
end

#validate_quantityObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/cats/core/rhn_request.rb', line 45

def validate_quantity
  return unless commodity && quantity && unit

  requested = commodity.requested_quantity

  remaining = commodity.quantity - requested
  if quantity_was
    additional = UnitConversion.convert(unit, commodity.unit_of_measure, quantity_was)
    remaining += additional
  end
  new_quantity = UnitConversion.convert(unit, commodity.unit_of_measure, quantity)
  return unless new_quantity > remaining

  remaining = UnitConversion.convert(commodity.unit_of_measure, unit, remaining)
  message = "exceeds commodity quantity. Maximum allowed is #{remaining} #{unit.abbreviation}"
  errors.add(:quantity, message)
end