module Logistics
module Core
class RouteRate < ApplicationRecord
validates :rate, presence: true, allow_blank: false
validates :route_id, presence: true, allow_blank: false
validates :transporter_id, presence: true, allow_blank: false
validates :rate_period_id, presence: true, allow_blank: false
belongs_to :route
belongs_to :transporter
belongs_to :weight_range, optional: true
belongs_to :rate_period, class_name: 'Logistics::Core::TransportRatePeriod'
belongs_to :container_size, optional: true
belongs_to :selected_transporter, class_name: 'Logistics::Core::Transporter', optional: true
def self.route_type
%w(TransportationRouteRate, PositioningRouteRate)
end
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
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
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
def transporter_name
if self.transporter_id
transporter = Transporter.find self.transporter_id
transporter.name
else
nil
end
end
def selected_transporter_name
if self.selected_transporter_id
transporter = Transporter.find self.selected_transporter_id
transporter.name
else
nil
end
end
def weight_from
if self.weight_range_id
weight_range = WeightRange.find self.weight_range_id
weight_range.weight_from
else
nil
end
end
def weight_to
if self.weight_range_id
weight_range = WeightRange.find self.weight_range_id
weight_range.weight_to
else
nil
end
end
def effective_date
if self.rate_period_id
rate_period = TransportRatePeriod.find self.rate_period_id
rate_period.effective_date
else
nil
end
end
def valid_until
if self.rate_period_id
rate_period = TransportRatePeriod.find self.rate_period_id
rate_period.valid_until
else
nil
end
end
def container_size
if self.container_size_id
container_size = ContainerSize.find self.container_size_id
container_size.name
else
nil
end
end
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
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
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
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 " + 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: nil, errors: error}
end
end
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
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
def self.create_archive
self.connection.execute("select create_current_route_rate_archive()")
end
end
end
end