Method: Logistics::Core::RouteRate.generate_transportation_rates

Defined in:
app/models/logistics/core/route_rate.rb

.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