require_dependency 'application_controller'
module Logistics
module Core
class CarCarrierRatesController < ApplicationController
protect_from_forgery with: :null_session
before_action :set_car_carrier_rate, only: [:update]
# GET /car_carrier_rates
# GET /car_carrier_rates.json
def index
start = params[:start]
limit = params[:limit]
route_type = params[:route_type]
transporter_id = Transporter.select("id").where("name" => "Reference Transporter").ids.first
current_period_id = TransportRatePeriod.where(current_period: true).ids.first
routes = Route.where(route_type: route_type)
car_carrier_rates_all = CarCarrierRate.where(route_id: routes.ids,
transport_rate_period_id: current_period_id).where.not("transporter_id" => transporter_id)
count = car_carrier_rates_all.count
car_carrier_rates = CarCarrierRate.fetch_all car_carrier_rates_all, start, limit
response = Mks::Common::MethodResponse.new(true, nil, car_carrier_rates, nil, count)
render json: response
end
def get_all_fields car_carrier_rates
result = []
car_carrier_rates.all.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
return result
end
def get_car_carrier_routes
rate_type = params[:rate_type]
car_carrier_rates = CarCarrierRate.where(rate_type: rate_type).select("DISTINCT ON (route_id) *")
result = get_all_fields car_carrier_rates
response = Mks::Common::MethodResponse.new(true, nil, result, nil, nil)
render json: response
end
def get_car_carrier_rates
start = params[:start]
limit = params[:limit]
rate_type = params[:rate_type]
result = []
transporter = Transporter.where(name: "Reference Transporter")
transport_rate_period = TransportRatePeriod.where(current_period: true)
if transporter.length > 0 && transport_rate_period.length > 0
transporter_id = transporter[0].id
transport_rate_period_id = transport_rate_period[0].id
car_carrier_rates_all = CarCarrierRate.where(rate_type: rate_type,
transporter_id: transporter_id,
transport_rate_period_id: transport_rate_period_id)
count = car_carrier_rates_all.count
car_carrier_rates = car_carrier_rates_all.offset(start).limit(limit).order("transporter_id, route_id, break_bulk_unit_id")
if rate_type == "ICCarrier"
rate_type_type = "IC Carrier"
else
rate_type_type = "WC Carrier"
end
car_carrier_rates.each do |car_carrier_rate|
selected_transporter = Transporter.find(car_carrier_rate.selected_transporter_id)
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.selected_transporter_id,
transporter_name: selected_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,
rate_type: rate_type_type})
end
end
response = Mks::Common::MethodResponse.new(true, nil, result, nil, count)
render json: response
end
def get_min_max_transporter_rates
route_id = params[:route_id]
rate_type = params[:rate_type]
data_min = []
data_max = []
result = []
default_transporter = Transporter.where(name: "Reference Transporter")
rate_period = TransportRatePeriod.where(current_period: true)
if default_transporter.length > 0 && rate_period.length > 0
default_transporter_id = default_transporter[0].id
rate_period_id = rate_period[0].id
car_carrier_rates = CarCarrierRate.where("route_id = ? and rate_type = ? and transport_rate_period_id = ? and rate > 0",
"#{route_id}",
"#{rate_type}",
"#{rate_period_id}")
current_car_carrier_rates = car_carrier_rates.where(transporter_id: default_transporter_id)
break_bulk_unit_ids = []
current_car_carrier_rates.each do |bb_rate|
break_bulk_unit_ids.push(bb_rate[:break_bulk_unit_id])
end
car_carrier_rates = car_carrier_rates.where.not(transporter_id: default_transporter_id,
break_bulk_unit_id: break_bulk_unit_ids)
car_carrier_rates_uniques = car_carrier_rates.select(:break_bulk_unit_id).distinct
if car_carrier_rates_uniques.length > 0
car_carrier_rates_uniques.each do |car_carrier_rate_unique|
break_bulk_unit_id = car_carrier_rate_unique.break_bulk_unit_id
rate = car_carrier_rates.where ({ break_bulk_unit_id: break_bulk_unit_id })
rate = rate.order("rate ASC")
if rate.length > 0
data_min.push(create_min_max_data rate.first)
data_max.push(create_min_max_data rate.last)
end
end
end
result.push({min_rate: data_min, max_rate: data_max})
end
response = Mks::Common::MethodResponse.new(true, nil, result, nil, nil)
render json: response
end
def reload_min_max_transporter_rates
reload_rate = params[:reload_rate]
route_id = reload_rate[:route_id]
rate_type = reload_rate[:rate_type]
items = reload_rate[:items]
data_min = []
data_max = []
result = []
default_transporter = Transporter.where(name: "Reference Transporter")
rate_period = TransportRatePeriod.where(current_period: true)
break_bulk_unit_ids = []
items.each do |break_bulk_unit|
break_bulk_unit_ids << break_bulk_unit[:id]
end
if default_transporter.length > 0 && rate_period.length > 0
default_transporter_id = default_transporter[0].id
rate_period_id = rate_period[0].id
car_carrier_rates = CarCarrierRate.where("route_id = ? and rate_type = ? and transport_rate_period_id = ? and rate > 0",
"#{route_id}",
"#{rate_type}",
"#{rate_period_id}")
current_car_carrier_rates = car_carrier_rates.where(transporter_id: default_transporter_id)
current_car_carrier_rates.each do |bb_rate|
break_bulk_unit_ids.push(bb_rate[:break_bulk_unit_id])
end
car_carrier_rates = car_carrier_rates.where.not(transporter_id: default_transporter_id,
break_bulk_unit_id: break_bulk_unit_ids)
car_carrier_rates_uniques = car_carrier_rates.select(:break_bulk_unit_id).distinct
if car_carrier_rates.length > 0
car_carrier_rates_uniques.each do |car_carrier_rate|
break_bulk_unit_id = car_carrier_rate.break_bulk_unit_id
rate = car_carrier_rates.where ({ break_bulk_unit_id: break_bulk_unit_id })
rate = rate.order("rate ASC")
if rate.length > 0
data_min.push(create_min_max_data rate.first)
data_max.push(create_min_max_data rate.last)
end
end
end
result.push({min_rate: data_min, max_rate: data_max})
end
response = Mks::Common::MethodResponse.new(true, nil, result, nil, nil)
render json: response
end
def create_min_max_data min_max_rate
data = {
route_id: min_max_rate.route_id,
route_name: min_max_rate.route.route_name,
break_bulk_unit_id: min_max_rate.break_bulk_unit_id,
break_bulk_unit_type: min_max_rate.break_bulk_unit.name,
transporter_id: min_max_rate.transporter_id,
transporter_name: min_max_rate.transporter.name,
rate: min_max_rate.rate,
margin: min_max_rate.route.margin
}
return data
end
# POST /car_carrier_rates
# POST /car_carrier_rates.json
def create
route_type = params[:route_type]
generated = CarCarrierRate.generate_car_carrier_rate route_type
if generated[:ind] == 1
response = Mks::Common::MethodResponse.new(true, generated[:message], nil, generated[:errors], nil)
else
errors = generated[:message]
response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil)
end
render json: response
end
def create_working_rate
car_carrier_rates = params[:car_carrier_rate]
rate_type = params[:rate_type]
route_id = car_carrier_rates[0][:route_id]
new_car_carrier_rate_arr = []
errors = []
rate_period = TransportRatePeriod.where(current_period: true)
default_transporter = Transporter.where(name: "Reference Transporter")
if default_transporter.length > 0 && rate_period.length > 0
default_transporter_id = default_transporter[0].id
rate_period_id = rate_period[0].id
car_carrier_rates_all = CarCarrierRate.where( route_id: route_id,
rate_type: rate_type,
transport_rate_period_id: rate_period_id )
if car_carrier_rates_all.length > 0
car_carrier_rates.each do |car_carrier_rate|
route_id = car_carrier_rate[:route_id]
transporter_id = car_carrier_rate[:transporter_id]
rate_type = car_carrier_rate[:rate_type]
margin = car_carrier_rate[:margin]
break_bulk_unit_id = car_carrier_rate[:break_bulk_unit_id]
selected = car_carrier_rates_all.where({ route_id: route_id,
transporter_id: transporter_id,
break_bulk_unit_id: break_bulk_unit_id,
rate_type: rate_type })
new_car_carrier_rate = { route_id: route_id,
transporter_id: default_transporter_id,
break_bulk_unit_id: break_bulk_unit_id,
rate_type: rate_type,
selected_transporter_id: transporter_id }
check_existing = CarCarrierRate.check_for_existing_rate car_carrier_rates_all, new_car_carrier_rate
if !check_existing
rate = selected[0].rate + (selected[0].rate * margin/100)
new_car_carrier_rate = { route_id: route_id,
transporter_id: default_transporter_id,
break_bulk_unit_id: break_bulk_unit_id,
transport_rate_period_id: rate_period_id,
rate_type: rate_type,
selected_transporter_id: transporter_id,
rate: rate,
margin: margin }
new_car_carrier_rate_arr << new_car_carrier_rate
else
errors << ["Maccfa Rate for Break Bulk Unit type " + car_carrier_rate[:break_bulk_unit_type] + " already exist. So it is skipped!"]
end
end
if new_car_carrier_rate_arr.length > 0
CarCarrierRate.create! new_car_carrier_rate_arr
response = Mks::Common::MethodResponse.new(true, "Maccfa Car Carrier Rate information saved successfully!", nil, errors, nil)
else
response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil)
end
else
errors << ["Error on saving " + rate_type + " Car Carrier Rate. No Rate record found!"]
response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil)
end
else
errors << ["Error on saving " + rate_type + " Car Carrier Rate!"]
response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil)
end
render json: response
end
# PATCH/PUT /car_carrier_rates/1
# PATCH/PUT /car_carrier_rates/1.json
def update
route_type = params[:route_type]
count = 0
car_carrier_rates = params[:updated_car_carrier_rates]
car_carrier_rates.each do |car_carrier_rate|
old_driving_rate = CarCarrierRate.find(car_carrier_rate['id'])
old_driving_rate.rate = car_carrier_rate['rate']
if old_driving_rate.save
count += 1
end
end
message = "A total of " + count.to_s + " " + route_type + " Car Carrier Rate updated successfully!"
response = Mks::Common::MethodResponse.new(true, message, car_carrier_rates, nil, nil)
render json: response
end
private
def set_car_carrier_rate
@car_carrier_rate = CarCarrierRate.find(params[:id])
end
def car_carrier_rate_params
params[:car_carrier_rate]
end
end
end
end