Class: Logistics::Core::RouteRate

Inherits:
ApplicationRecord show all
Defined in:
app/models/logistics/core/route_rate.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

as_json

Class Method Details

.check_for_existing_transport_rate(route_rate_all, new_route_rate) ⇒ Object



175
176
177
178
179
180
181
182
183
# File 'app/models/logistics/core/route_rate.rb', line 175

def self.check_for_existing_transport_rate route_rate_all, new_route_rate
  route_rate = route_rate_all.where(new_route_rate)

  if route_rate.count > 0
    return true
  else
    return false
  end
end

.create_archiveObject



327
328
329
# File 'app/models/logistics/core/route_rate.rb', line 327

def self.create_archive
  self.connection.execute("select create_current_route_rate_archive()")
end

.fetch_all(route_rates_all, start, limit) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/logistics/core/route_rate.rb', line 154

def self.fetch_all route_rates_all, start, limit
  result = []
  route_rates = route_rates_all.offset(start).limit(limit).order("transporter_id, route_id, weight_range_id, container_size_id")
  route_rates.each do |route_rate|
    result.push({id: route_rate.id,
                 weight_from: route_rate.weight_from,
                 weight_to: route_rate.weight_to,
                 rate: route_rate.rate,
                 route_id: route_rate.route_id,
                 route_name: route_rate.route_name,
                 transporter_id: route_rate.transporter_id,
                 transporter_name: route_rate.transporter_name,
                 effective_date: route_rate.effective_date,
                 valid_until: route_rate.valid_until,
                 rate_type: route_rate.rate_type,
                 container_size_id: route_rate.container_size_id,
                 container_size: route_rate.container_size})
  end
  return result
end

.fetch_current_route_rate(transporter_id) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'app/models/logistics/core/route_rate.rb', line 309

def self.fetch_current_route_rate transporter_id
  result = []
  route_rates = RouteRate.where("transporter_id = ?", "#{transporter_id}")
  route_rates.each do |route_rate|
    if route_rate.route_type == "TransportationRouteRate"
      route_type_name = "Transport"
    elsif route_rate.route_type == "PositioningRouteRate"
      route_type_name = "Positioning"
    end
    result.push({id: route_rate.id, weight_from: route_rate.weight_from, weight_to: route_rate.weight_to,
                 rate: route_rate.rate, :container_return_fee => route_rate.container_return_fee, :route_id => route_rate.route_id,
                 route_name: route_rate.route_name, transporter_id: route_rate.transporter_id, transporter_name: route_rate.transporter_name,
                 effective_date: route_rate.effective_date, valid_until: route_rate.valid_until,
                 route_type: route_rate.route_type, route_type_name: route_type_name})
  end
  return result
end

.fetch_route(route_type) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'app/models/logistics/core/route_rate.rb', line 295

def self.fetch_route route_type
  result = []
  reference_transporter = Transporter.find_by(name: 'Reference Transporter')
  # reference_transporter = Transporter.where("name" => "Reference Transporter")
  ids = RouteRate.select(:route_id).where("rate_type = ? and transporter_id = ?", "#{route_type}","#{reference_transporter.id}")
  if ids.count > 0
    routes = Route.where("id" => ids)
    routes.each do |route|
      result.push({id: route.id, route_name: route.route_name})
    end
  end
  return result
end

.generate_transportation_rates(route_type, rate_type) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'app/models/logistics/core/route_rate.rb', line 185

def self.generate_transportation_rates route_type, rate_type
  route_rate_all = RouteRate.where(rate_type: route_type[0] + 'C' + rate_type)

  transporters = Transporter.where.not("name" => "Reference Transporter")
  routes = Route.where("route_type" => route_type)
  current_rate_period = TransportRatePeriod.where('current_period' => true)
  weight_ranges = WeightRange.all
  container_sizes = ContainerSize.all

  route_rate_arr = []
  message = ""
  count = 0

  if transporters.count > 0  && current_rate_period.count > 0
    transporters.each do |transporter|
      if routes.count > 0
        routes.each do |route|
          if rate_type == 'Transport'
            rate_type_type = route_type == 'Inter City'? 'ICTransport' : 'WCTransport'
            if weight_ranges.count > 0
              weight_ranges.each do |weight_range|
                check_existing_rate = false
                new_route_rate = { transporter_id: transporter.id,
                                   route_id: route.id,
                                   rate_period_id: current_rate_period[0].id,
                                   rate_type: rate_type_type,
                                   weight_range_id: weight_range.id,
                                   container_size_id: nil }
                if route_rate_all.count > 0
                  check_existing_rate = check_for_existing_transport_rate route_rate_all, new_route_rate
                end

                if !check_existing_rate
                  new_route_rate = { transporter_id: transporter.id,
                                     route_id: route.id,
                                     rate_period_id: current_rate_period[0].id,
                                     rate_type: rate_type_type,
                                     rate: 0,
                                     weight_range_id: weight_range.id,
                                     container_size_id: nil }
                  route_rate_arr << new_route_rate
                else
                  count += 1
                end
              end
            else
              message = "Cargo Weight Range!"
              break
            end
          else
            if rate_type == 'ReturnFee'
              rate_type_type = route_type == 'Inter City'? 'ICReturnFee' : 'WCReturnFee'
            else
              rate_type_type = route_type == 'Inter City'? 'ICPositioning' : 'WCPositioning'
            end
            if container_sizes.count > 0
              container_sizes.each do |container_size|
                check_existing_rate = false
                new_route_rate = { transporter_id: transporter.id,
                                   route_id: route.id,
                                   rate_period_id: current_rate_period[0].id,
                                   rate_type: rate_type_type,
                                   weight_range_id: nil,
                                   container_size_id: container_size.id }
                if route_rate_all.count > 0
                  check_existing_rate = check_for_existing_transport_rate route_rate_all, new_route_rate
                end

                if !check_existing_rate
                  new_route_rate = { transporter_id: transporter.id,
                                     route_id: route.id,
                                     rate_period_id: current_rate_period[0].id,
                                     rate_type: rate_type_type,
                                     rate: 0,
                                     weight_range_id: nil,
                                     container_size_id: container_size.id }
                  route_rate_arr << new_route_rate
                else
                  count += 1
                end
              end
            else
              message = "Cargo Container Size!"
              break
            end
          end
        end
      else
        message = route_type + " transport Route!"
        break
      end
    end
    if route_rate_arr.count > 0
      RouteRate.create(route_rate_arr)
      message = route_type + " " + rate_type + " Rate Generated Successfully!"
    end
  elsif current_rate_period.count == 0
    message = "Transport Rate Period!"
  else
    message = "Transporter!"
  end
  if message == route_type + " " + rate_type + " Rate Generated Successfully!" || count > 0
    message2 = ["A total of <b>" + count.to_s + "</b> Records have been skipped!"]
    return {ind: 1, value: "", message: message, errors: message2}
  else
    error = ["No Record Found for <b>" + message + "</b>."]
    return {ind: 0, value: "", message: nil, errors: error}
  end
end

.get_transport_rates(working_route_rates, start, limit) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/models/logistics/core/route_rate.rb', line 119

def self.get_transport_rates working_route_rates, start, limit
  transport_rates = working_route_rates.offset(start).limit(limit).order("route_id, weight_range_id, container_size_id")
  rate_type = working_route_rates[0].rate_type
  result = []
  if rate_type == "ICTransport"
    rate_type = 'IC Transport Rate'
  elsif rate_type == "ICReturnFee"
    rate_type = 'IC Container Return Rate'
  elsif rate_type == "ICPositioning"
    rate_type = 'IC Container Positioning Rate'
  elsif rate_type == "WCTransport"
    rate_type = 'WC Transport Rate'
  elsif rate_type == "WCReturnFee"
    rate_type = 'WC Container Return Rate'
  elsif rate_type == "WCPositioning"
    rate_type = 'WC Container Positioning Rate'
  end
  transport_rates.each do |transport_rate|
    result.push({id: transport_rate.id,
                 weight_from: transport_rate.weight_from,
                 weight_to: transport_rate.weight_to,
                 rate: transport_rate.rate,
                 route_id: transport_rate.route_id,
                 route_name: transport_rate.route_name,
                 transporter_id: transport_rate.selected_transporter_id,
                 transporter_name: transport_rate.selected_transporter_name,
                 effective_date: transport_rate.effective_date,
                 valid_until: transport_rate.valid_until,
                 rate_type: rate_type,
                 container_size_id: transport_rate.container_size_id,
                 container_size: transport_rate.container_size})
  end
  return result
end

.route_typeObject



16
17
18
# File 'app/models/logistics/core/route_rate.rb', line 16

def self.route_type
  %w(TransportationRouteRate, PositioningRouteRate)
end

Instance Method Details

#container_sizeObject



110
111
112
113
114
115
116
117
# File 'app/models/logistics/core/route_rate.rb', line 110

def container_size
  if self.container_size_id
    container_size = ContainerSize.find self.container_size_id
    container_size.name
  else
    nil
  end
end

#effective_dateObject



92
93
94
95
96
97
98
99
# File 'app/models/logistics/core/route_rate.rb', line 92

def effective_date
  if self.rate_period_id
    rate_period = TransportRatePeriod.find self.rate_period_id
    rate_period.effective_date
  else
    nil
  end
end

#fromObject



20
21
22
23
24
25
26
27
28
29
# File 'app/models/logistics/core/route_rate.rb', line 20

def from
  if self.route_id
    route = Route.where("id = ? and route_type = ?",
                        "#{self.route_id}", "Inter City")
    route_lookup = RouteLookup.find route.first.from_id
    route_lookup.name
  else
    nil
  end
end

#route_nameObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/logistics/core/route_rate.rb', line 42

def route_name
  route = Route.find self.route_id
  if route.route_type == 'Inter City'
    if from && to
      self.from + " - " + self.to
    else
      nil
    end
  elsif route.route_type == 'Within City'
    route_lookup = RouteLookup.find route.node_id
    route_lookup.name + " - " + route.zone
  end
end

#selected_transporter_nameObject



65
66
67
68
69
70
71
72
# File 'app/models/logistics/core/route_rate.rb', line 65

def selected_transporter_name
  if self.selected_transporter_id
    transporter = Transporter.find self.selected_transporter_id
    transporter.name
  else
    nil
  end
end

#toObject



31
32
33
34
35
36
37
38
39
40
# File 'app/models/logistics/core/route_rate.rb', line 31

def to
  if self.route_id
    route = Route.where("id = ? and route_type = ?",
                        "#{self.route_id}", "Inter City")
    route_lookup = RouteLookup.find route.first.to_id
    route_lookup.name
  else
    nil
  end
end

#transporter_nameObject



56
57
58
59
60
61
62
63
# File 'app/models/logistics/core/route_rate.rb', line 56

def transporter_name
  if self.transporter_id
    transporter = Transporter.find self.transporter_id
    transporter.name
  else
    nil
  end
end

#valid_untilObject



101
102
103
104
105
106
107
108
# File 'app/models/logistics/core/route_rate.rb', line 101

def valid_until
  if self.rate_period_id
    rate_period = TransportRatePeriod.find self.rate_period_id
    rate_period.valid_until
  else
    nil
  end
end

#weight_fromObject



74
75
76
77
78
79
80
81
# File 'app/models/logistics/core/route_rate.rb', line 74

def weight_from
  if self.weight_range_id
    weight_range = WeightRange.find self.weight_range_id
    weight_range.weight_from
  else
    nil
  end
end

#weight_toObject



83
84
85
86
87
88
89
90
# File 'app/models/logistics/core/route_rate.rb', line 83

def weight_to
  if self.weight_range_id
    weight_range = WeightRange.find self.weight_range_id
    weight_range.weight_to
  else
    nil
  end
end