Class: Cats::Core::Commodity

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

Constant Summary collapse

DRAFT =

Statuses

"Draft".freeze
APPROVED =
"Approved".freeze
ALLOCATED =
"Allocated".freeze
STATUSES =
[DRAFT, APPROVED, ALLOCATED].freeze
GRADE1 =

Grades

"Grade1".freeze
GRADE2 =
"Grade2".freeze
GRADE3 =
"Grade3".freeze
COMMODITY_GRADES =
[GRADE1, GRADE2, GRADE3].freeze
GOOD =

Commodity statuses

"Good".freeze
DAMAGED =
"Damaged".freeze
LEAKAGE =
"Leakage".freeze
WET =
"Wet".freeze
MOLDY =
"Moldy".freeze
INFESTED =
"Infested".freeze
INSECT_INFECTED =
"Insect Infected".freeze
FAECES_FROM_RODENTS =
"Faeces From Rodents".freeze
UNDERWEIGHT_PACKAGE =
"Underweight Package".freeze
COMMODITY_STATUSES =
[GOOD, DAMAGED, LEAKAGE, WET, MOLDY, INFESTED, INSECT_INFECTED, FAECES_FROM_RODENTS,
UNDERWEIGHT_PACKAGE].freeze
AT_SOURCE =

Arrival Statuses

"At Source".freeze
AT_ORIGIN_PORT =
"At Origin Port".freeze
IN_LOCAL_TRANSIT =
"In Local Transit".freeze
IN_INTERNATIONAL_TRANSIT =
"In International Transit".freeze
AT_DESTINATION_PORT =
"At Destination Port".freeze
ARRIVED =
"Arrived".freeze
ARRIVAL_STATUSES =
[
  AT_SOURCE,
  AT_ORIGIN_PORT,
  IN_LOCAL_TRANSIT,
  IN_INTERNATIONAL_TRANSIT,
  AT_DESTINATION_PORT,
  ARRIVED
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ransackable_associations(_auth_object = nil) ⇒ Object



126
127
128
# File 'app/models/cats/core/commodity.rb', line 126

def self.ransackable_associations(_auth_object = nil)
  %w[package_unit project unit_of_measure]
end

.ransackable_attributes(_auth_object = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/cats/core/commodity.rb', line 112

def self.ransackable_attributes(_auth_object = nil)
  %w[
    arrival_status
    batch_no
    best_use_before
    commodity_grade
    package_unit_id
    project_id
    quantity
    shipping_reference
    status
  ]
end

Instance Method Details

#approveObject

Raises:

  • (StandardError)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/cats/core/commodity.rb', line 78

def approve
  raise(StandardError, "Commodity already approved.") if status == APPROVED

  result = false
  Cats::Core::Commodity.transaction do
    self.status = APPROVED
    save!
    store = Cats::Core::Store.find_by(code: "SUP-STORE")
    raise(StandardError, "Supplier store does not exist.") unless store

    stack = Cats::Core::Stack.create!(
      code: batch_no,
      store: store,
      commodity: self,
      quantity: quantity,
      unit: unit,
      commodity_status: GOOD,
      stack_status: Stack::ALLOCATED,
      length: 100,
      width: 100,
      height: 10,
      start_x: 1,
      start_y: 1
    )
    result = stack.id.positive?
  end
  result
end

#nameObject



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

def name
  # For this to be efficient, our query should use includes like
  # Commodity.includes(project: { source: commodity_category })
  project.source.commodity_name
end

#requested_quantityObject



107
108
109
110
# File 'app/models/cats/core/commodity.rb', line 107

def requested_quantity
  requests = RhnRequest.where(commodity: self)
  UnitConversion.harmonized_total(requests, unit_of_measure)
end

#validate_updateabilityObject



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

def validate_updateability
  return unless status

  return if status_changed?

  errors.add(:base, "Commodity cannot be updated because it is in approved state.") if status == APPROVED
end