module Logistics
module Core
class SelfDrivingRate < ApplicationRecord
belongs_to :break_bulk_unit
belongs_to :rate_period, class_name: 'Logistics::Core::TransportRatePeriod'
belongs_to :route
belongs_to :selected_transporter, class_name: 'Logistics::Core::Transporter'
belongs_to :transporter
validates :route, :break_bulk_unit, :rate, :rate_period, :transporter, presence: true
validates :route, uniqueness: {scope: [:break_bulk_unit, :rate_period, :transporter]}
def self.fetch_all self_driving_rates_all, start, limit
result = []
self_driving_rates = self_driving_rates_all.offset(start).limit(limit).order("transporter_id, route_id, break_bulk_unit_id")
if self_driving_rates.length > 0
self_driving_rates.each do |self_driving_rate|
result.push({id: self_driving_rate.id,
route_id: self_driving_rate.route_id,
route_name: self_driving_rate.route ? self_driving_rate.route.route_name : nil,
break_bulk_unit_id: self_driving_rate.break_bulk_unit_id,
break_bulk_unit_type: self_driving_rate.break_bulk_unit ? self_driving_rate.break_bulk_unit.name : nil,
rate: self_driving_rate.rate,
margin: self_driving_rate.margin,
transporter_id: self_driving_rate.transporter_id,
transporter_name: self_driving_rate.transporter ? self_driving_rate.transporter.name : nil,
rate_period_id: self_driving_rate.rate_period_id,
effective_date: self_driving_rate.rate_period.effective_date,
valid_until: self_driving_rate.rate_period.valid_until })
end
end
return result
end
def self.check_for_existing_rate current_self_driving_rate, new_self_driving_rate
self_driving_rate = current_self_driving_rate.where(new_self_driving_rate)
if self_driving_rate.count > 0
return true
else
return false
end
end
def self.generate_self_driving_rate route_type
message = ''
count = 0
routes = Route.where(route_type: route_type)
break_bulk_units = BreakBulkUnit.all
transporters = Transporter.where.not(name: 'Reference Transporter')
rate_period = TransportRatePeriod.where(current_period: true)
if route_type == 'Inter City'
rate_type = 'ICDriving'
else
rate_type = 'WCDriving'
end
current_self_driving_rate = SelfDrivingRate.where rate_type: rate_type
new_self_driving_rate_arr = []
if transporters.length > 0 && rate_period.length > 0
transporters.each do |transporter|
if routes.length > 0
routes.each do |route|
if break_bulk_units.length > 0
break_bulk_units.each do |break_bulk_unit|
new_self_driving_rate = { transporter_id: transporter.id,
route_id: route.id,
break_bulk_unit_id: break_bulk_unit.id,
rate_period_id: rate_period[0].id,
rate_type: rate_type }
check_existing_rate = check_for_existing_rate current_self_driving_rate, new_self_driving_rate
if !check_existing_rate
new_self_driving_rate = { transporter_id: transporter.id,
route_id: route.id,
break_bulk_unit_id: break_bulk_unit.id,
rate: 0,
margin: route.margin,
rate_period_id: rate_period[0].id,
rate_type: rate_type }
new_self_driving_rate_arr << new_self_driving_rate
else
count += 1
end
end
else
message = "Break Bulk Unit Types!"
break
end
end
else
message = route_type + " Transport Route!"
break
end
end
if new_self_driving_rate_arr.count > 0
SelfDrivingRate.create! new_self_driving_rate_arr
message = route_type + " Self Driving Rate Generated Successfully!"
end
elsif rate_period.length == 0
message = "Transport Rate Period!"
else
message = "Transporter!"
end
if message == route_type + " Self Driving Rate Generated Successfully!" || count > 0
message2 = ["A total of " + count.to_s + " Records have been skipped!"]
return {ind: 1, value: "", message: message, errors: message2}
else
error = ["No Record Found for " + message + "."]
return {ind: 0, value: "", message: error}
end
end
end
end
end