Class: Cats::Core::TransportOrder

Inherits:
ApplicationRecord show all
Defined in:
app/models/cats/core/transport_order.rb

Constant Summary collapse

DRAFT =
"Draft".freeze
APPROVED =
"Approved".freeze
STATUSES =
[DRAFT, APPROVED].freeze
ACTIVE =
"Active".freeze
CANCELLED =
"Cancelled".freeze
EXPIRED =
"Expired".freeze
ORDER_STATUSES =
[ACTIVE, CANCELLED, EXPIRED].freeze

Instance Method Summary collapse

Instance Method Details

#approve(user) ⇒ Object

Raises:

  • (StandardError)


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

def approve(user)
  raise(StandardError, "Transport order is already approved.") if status == APPROVED

  begin
    self.status = APPROVED
    self.approved_by = user
    save!
    self
  rescue ActiveRecord::RecordInvalid => e
    error = e.record.errors.full_messages_for(:status)[0]
    raise(StandardError, error)
  end
end

#validate_against_requisitionObject



28
29
30
31
32
33
34
# File 'app/models/cats/core/transport_order.rb', line 28

def validate_against_requisition
  return unless transport_requisition

  return if transport_requisition.approved?

  errors.add(:transport_requisition, "is not approved.")
end

#validate_statusObject



36
37
38
39
40
41
42
43
44
# File 'app/models/cats/core/transport_order.rb', line 36

def validate_status
  return unless status

  return if status == DRAFT

  return if transport_order_items.count.positive?

  errors.add(:status, 'cannot be set to "APPROVED" because the order has no items.') if status == APPROVED
end