require_dependency 'application_controller' module Logistics module Core class SelfDrivingRatesController < ApplicationController protect_from_forgery with: :null_session before_action :set_self_driving_rate, only: [:update] # GET /self_driving_rates # GET /self_driving_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) self_driving_rates_all = SelfDrivingRate.where(route_id: routes.ids, rate_period_id: current_period_id).where.not("transporter_id" => transporter_id) count = self_driving_rates_all.count self_driving_rates = SelfDrivingRate.fetch_all self_driving_rates_all, start, limit response = Mks::Common::MethodResponse.new(true, nil, self_driving_rates, nil, count) render json: response end def get_all_fields self_driving_rates result = [] self_driving_rates.all.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.route_name, break_bulk_unit_id: self_driving_rate.break_bulk_unit_id, break_bulk_unit_type: self_driving_rate.break_bulk_unit.name, rate: self_driving_rate.rate, margin: self_driving_rate.margin, transporter_id: self_driving_rate.transporter_id, transporter_name: self_driving_rate.transporter.name, 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 result end def get_self_driving_routes rate_type = params[:rate_type] self_driving_rates = SelfDrivingRate.where(rate_type: rate_type).select("DISTINCT ON (route_id) *") result = get_all_fields self_driving_rates response = Mks::Common::MethodResponse.new(true, nil, result, nil, nil) render json: response end def get_self_driving_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 self_driving_rates_all = SelfDrivingRate.where(rate_type: rate_type, transporter_id: transporter_id, rate_period_id: transport_rate_period_id) count = self_driving_rates_all.count self_driving_rates = self_driving_rates_all.offset(start).limit(limit).order("transporter_id, route_id, break_bulk_unit_id") if rate_type == "ICDriving" rate_type_type = "IC Driving" else rate_type_type = "WC Driving" end self_driving_rates.each do |self_driving_rate| selected_transporter = Transporter.find(self_driving_rate.selected_transporter_id) result.push({id: self_driving_rate.id, route_id: self_driving_rate.route_id, route_name: self_driving_rate.route.route_name, break_bulk_unit_id: self_driving_rate.break_bulk_unit_id, break_bulk_unit_type: self_driving_rate.break_bulk_unit.name, rate: self_driving_rate.rate, margin: self_driving_rate.margin, transporter_id: self_driving_rate.selected_transporter_id, transporter_name: selected_transporter.name, 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, 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 self_driving_rates = SelfDrivingRate.where("route_id = ? and rate_type = ? and rate_period_id = ? and rate > 0", "#{route_id}", "#{rate_type}", "#{rate_period_id}") current_self_driving_rates = self_driving_rates.where(transporter_id: default_transporter_id) break_bulk_unit_ids = [] current_self_driving_rates.each do |bb_rate| break_bulk_unit_ids.push(bb_rate[:break_bulk_unit_id]) end self_driving_rates = self_driving_rates.where.not(transporter_id: default_transporter_id, break_bulk_unit_id: break_bulk_unit_ids) self_driving_rate_uniques = self_driving_rates.select(:break_bulk_unit_id).distinct if self_driving_rate_uniques.length > 0 self_driving_rate_uniques.each do |self_driving_rate| break_bulk_unit_id = self_driving_rate.break_bulk_unit_id rate = self_driving_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 self_driving_rates = SelfDrivingRate.where("route_id = ? and rate_type = ? and rate_period_id = ? and rate > 0", "#{route_id}", "#{rate_type}", "#{rate_period_id}") current_self_driving_rates = self_driving_rates.where(transporter_id: default_transporter_id) current_self_driving_rates.each do |bb_rate| break_bulk_unit_ids << bb_rate[:break_bulk_unit_id] end self_driving_rates = self_driving_rates.where.not(transporter_id: default_transporter_id, break_bulk_unit_id: break_bulk_unit_ids) self_driving_rate_uniques = self_driving_rates.select(:break_bulk_unit_id).distinct if self_driving_rates.length > 0 self_driving_rate_uniques.each do |self_driving_rate| break_bulk_unit_id = self_driving_rate.break_bulk_unit_id rate = self_driving_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 /self_driving_rates # POST /self_driving_rates.json def create route_type = params[:route_type] generated = SelfDrivingRate.generate_self_driving_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 self_driving_rates = params[:self_driving_rate] rate_type = params[:rate_type] route_id = self_driving_rates[0][:route_id] new_self_driving_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 self_driving_rates_all = SelfDrivingRate.where( route_id: route_id, rate_type: rate_type, rate_period_id: rate_period_id) if self_driving_rates_all.length > 0 self_driving_rates.each do |self_driving_rate| route_id = self_driving_rate[:route_id] transporter_id = self_driving_rate[:transporter_id] rate_type = self_driving_rate[:rate_type] margin = self_driving_rate[:margin] break_bulk_unit_id = self_driving_rate[:break_bulk_unit_id] selected = self_driving_rates_all.where({ transporter_id: transporter_id, break_bulk_unit_id: break_bulk_unit_id }) new_self_driving_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 = SelfDrivingRate.check_for_existing_rate self_driving_rates_all, new_self_driving_rate if !check_existing rate = selected[0].rate + (selected[0].rate * margin/100) new_self_driving_rate = { route_id: route_id, transporter_id: default_transporter_id, break_bulk_unit_id: break_bulk_unit_id, rate_period_id: rate_period_id, rate_type: rate_type, selected_transporter_id: transporter_id, rate: rate, margin: margin } new_self_driving_rate_arr << new_self_driving_rate else errors << "Maccfa Rate for Break Bulk Unit type " + self_driving_rate[:break_bulk_unit_type] + " already exist. So it is skipped!" end end if new_self_driving_rate_arr.length > 0 SelfDrivingRate.create! new_self_driving_rate_arr response = Mks::Common::MethodResponse.new(true, "Maccfa Self Driving 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 + " Self Driving Working Rate. No Rate record found!"] response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end else errors << ["Error on saving " + rate_type + " Self Driving Working Rate!"] response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end render json: response end # PATCH/PUT /self_driving_rates/1 # PATCH/PUT /self_driving_rates/1.json def update route_type = params[:route_type] count = 0 self_driving_rates = params[:updated_self_driving_rates] self_driving_rates.each do |self_driving_rate| old_driving_rate = SelfDrivingRate.find(self_driving_rate['id']) rate = self_driving_rate['rate'] old_driving_rate.rate = rate if old_driving_rate.save count += 1 end end message = "A total of " + count.to_s + " " + route_type + " Self Driving Rate updated successfully!" response = Mks::Common::MethodResponse.new(true, message, self_driving_rates, nil, nil) render json: response end private def set_self_driving_rate @self_driving_rate = SelfDrivingRate.find(params[:id]) end def self_driving_rate_params params[:self_driving_rate] end end end end