Class: Cats::Core::Dispatch

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

Constant Summary collapse

DRAFT =
"Draft".freeze
APPROVED =
"Approved".freeze
READY_TO_START =
"Ready to Start".freeze
STARTED =
"Started".freeze
RECEIVED =
"Received".freeze
STACKED =
"Stacked".freeze
DISPATCH_STATUSES =
[DRAFT, APPROVED, READY_TO_START, STARTED, RECEIVED, STACKED].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ransackable_associations(_auth_object = nil) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/cats/core/dispatch.rb', line 145

def self.ransackable_associations(_auth_object = nil)
  %w[
    prepared_by
    transporter
    dispatch_plan_item
    unit
    dispatch_authorizations
    receipt_authorizations
    dispatch_transactions
    receipt_transactions
  ]
end

.ransackable_attributes(_auth_object = nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/cats/core/dispatch.rb', line 130

def self.ransackable_attributes(_auth_object = nil)
  %w[
    commodity_status
    dispatch_plan_item_id
    dispatch_status
    driver_name
    driver_phone
    plate_no
    prepared_by_id
    quantity
    transporter_id
    unit_id
  ]
end

.search_commodity(batch_no) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/cats/core/dispatch.rb', line 110

def self.search_commodity(batch_no)
  commodity = Commodity.find_by(batch_no: batch_no)
  dispatches = Dispatch.includes(:dispatch_transactions)
                       .joins(:transporter)
                       .where(
                         dispatch_status: [APPROVED, STARTED]
                       )
  dispatches.map do |dispatch|
    {
      batch_no: batch_no,
      commodity_name: commodity.name,
      quantity: dispatch.quantity,
      unit: commodity.unit_abbreviation,
      location: dispatch.transporter.name,
      location_detail: "Plate No.: #{dispatch.plate_no}, Driver: #{dispatch.driver_name}",
      stack: ""
    }
  end
end

Instance Method Details

#all_authorizations_confirmed?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
# File 'app/models/cats/core/dispatch.rb', line 65

def all_authorizations_confirmed?
  statuses = dispatch_authorizations.map(&:status).uniq
  return true if statuses.length == 1 && statuses[0] == Authorization::CONFIRMED

  false
end

#approveObject

Raises:

  • (StandardError)


45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/cats/core/dispatch.rb', line 45

def approve
  raise(StandardError, "Dispatch has to be in draft state.") unless dispatch_status == Dispatch::DRAFT

  # Check if dispatch has authorizations
  raise(StandardError, "Dispatch has no authorizations.") unless dispatch_authorizations.count.positive?

  dispatch_total = UnitConversion.harmonized_total(dispatch_authorizations, unit)
  self.dispatch_status = APPROVED
  self.quantity = dispatch_total
  save!
end

#destinationObject



34
35
36
# File 'app/models/cats/core/dispatch.rb', line 34

def destination
  dispatch_plan_item.destination.name
end

#receiveObject

Raises:

  • (StandardError)


93
94
95
96
97
98
# File 'app/models/cats/core/dispatch.rb', line 93

def receive
  raise(StandardError, "Dispatch is not started.") unless dispatch_status == Dispatch::STARTED

  self.dispatch_status = RECEIVED
  save!
end

#revertObject

Raises:

  • (StandardError)


57
58
59
60
61
62
63
# File 'app/models/cats/core/dispatch.rb', line 57

def revert
  raise(StandardError, "Dispatch has to be in approved state.") unless dispatch_status == Dispatch::APPROVED

  self.dispatch_status = DRAFT
  self.quantity = 0
  save!
end

#stackObject



100
101
102
103
104
105
106
107
108
# File 'app/models/cats/core/dispatch.rb', line 100

def stack
  # check if all receipt transactions have been committed and change status
  # of dispatch to stacked.
  statuses = receipt_transactions.map(&:status).uniq
  return unless statuses.length == 1 && statuses[0] == Transaction::COMMITTED

  self.dispatch_status = STACKED
  save!
end

#startObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/cats/core/dispatch.rb', line 72

def start
  self.dispatch_status = STARTED
  save!

  # Check if destination is an FDP and create receipt authorization
  location = dispatch_plan_item.destination
  authorized_by = User.find_by(first_name: "SYSTEM-USER")
  return unless location.location_type == Location::FDP

  store = location.fdp_store
  ReceiptAuthorization.create!(
    dispatch: self,
    store: store,
    quantity: quantity,
    unit: unit,
    received_quantity: 0,
    status: Authorization::AUTHORIZED,
    authorized_by: authorized_by
  )
end

#validate_dispatch_plan_statusObject



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

def validate_dispatch_plan_status
  return unless dispatch_plan_item

  status = dispatch_plan_item.dispatch_plan.status
  errors.add(:dispatch_plan, "should be approved first.") unless status == DispatchPlan::APPROVED
end