module Logistics
module Core
class CarCarrierRate < ApplicationRecord
validates :route, :break_bulk_unit, :rate, :transport_rate_period, :transporter, presence: true
validates :route, uniqueness: {scope: [:break_bulk_unit_id, :transport_rate_period_id, :transporter_id]}
belongs_to :route
belongs_to :break_bulk_unit
belongs_to :transporter
belongs_to :transport_rate_period
belongs_to :selected_transporter, optional: true
def self.fetch_all car_carrier_rates_all, start, limit
result = []
car_carrier_rates = car_carrier_rates_all.offset(start).limit(limit).order("transporter_id, route_id, break_bulk_unit_id")
if car_carrier_rates.length > 0
car_carrier_rates.each do |car_carrier_rate|
result.push({id: car_carrier_rate.id,
route_id: car_carrier_rate.route_id,
route_name: car_carrier_rate.route.route_name,
break_bulk_unit_id: car_carrier_rate.break_bulk_unit_id,
break_bulk_unit_type: car_carrier_rate.break_bulk_unit.name,
rate: car_carrier_rate.rate,
margin: car_carrier_rate.margin,
transporter_id: car_carrier_rate.transporter_id,
transporter_name: car_carrier_rate.transporter.name,
transport_rate_period_id: car_carrier_rate.transport_rate_period_id,
effective_date: car_carrier_rate.transport_rate_period.effective_date,
valid_until: car_carrier_rate.transport_rate_period.valid_until })
end
end
return result
end
def self.check_for_existing_rate current_car_carrier_rate, new_car_carrier_rate
car_carrier_rate = current_car_carrier_rate.where(new_car_carrier_rate)
if car_carrier_rate.count > 0
return true
else
return false
end
end
def self.generate_car_carrier_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 = 'ICCarrier'
else
rate_type = 'WCCarrier'
end
new_car_carrier_rate_arr = []
current_car_carrier_rate = CarCarrierRate.where rate_type: rate_type
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_car_carrier_rate = { transporter_id: transporter.id, route_id: route.id,
break_bulk_unit_id: break_bulk_unit.id, transport_rate_period_id: rate_period[0].id,
rate_type: rate_type }
check_existing_rate = check_for_existing_rate current_car_carrier_rate, new_car_carrier_rate
if !check_existing_rate
new_car_carrier_rate = { transporter_id: transporter.id, route_id: route.id,
break_bulk_unit_id: break_bulk_unit.id, transport_rate_period_id: rate_period[0].id,
rate_type: rate_type, rate: 0, margin: route.margin }
new_car_carrier_rate_arr << new_car_carrier_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_car_carrier_rate_arr.count > 0
CarCarrierRate.create(new_car_carrier_rate_arr)
message = route_type + " Car Carrier Rate Generated Successfully!"
end
elsif rate_period.length == 0
message = "Transport Rate Period"
else
message = "Transporter"
end
if message == route_type + " Car Carrier 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