require_dependency 'application_controller' module Logistics module Core class RouteRatesController < ApplicationController protect_from_forgery with: :null_session before_action :set_route_rate, only: [:update] # GET /route_rates # GET /route_rates.json def index start = params[:start] limit = params[:limit] rate_type = params['rate_type'] transporter_id = Transporter.select("id").where("name" => "Reference Transporter").ids.first current_period_id = TransportRatePeriod.where(current_period: true).ids.first route_rates_all = RouteRate.where("rate_type" => rate_type, "rate_period_id" => current_period_id).where.not("transporter_id" => transporter_id) count = route_rates_all.count route_rates = RouteRate.fetch_all route_rates_all, start, limit result = [] if route_rates.length > 0 result = route_rates end response = Mks::Common::MethodResponse.new(true, nil, result, nil, count) render json: response end def get_route route_type = params['route_type'] @route = RouteRate.fetch_route route_type response = Mks::Common::MethodResponse.new(true, nil, @route, nil, nil) render json: response end def get_current_route_rate transporter_id = params['transporter_id'] @route_rates = RouteRate.fetch_current_route_rate transporter_id response = Mks::Common::MethodResponse.new(true, nil, @route_rates, nil, nil) render json: response end def save_current_route_rate reference_transporter_id = Transporter.select("id").where("name" => "Reference Transporter").ids.first reference_route_rate = RouteRate.where("transporter_id" => reference_transporter_id) current_route_rates = params[:route_rates] if reference_route_rate.count > 0 RouteRate.create_archive RouteRate.delete(reference_route_rate) end current_route_rates.each do |current_route_rate| route_rate = RouteRate.create! weight_from: current_route_rate['weight_from'], weight_to: current_route_rate['weight_to'], rate: current_route_rate['rate'], container_return_fee: current_route_rate['container_return_fee'], route_id: current_route_rate['route_id'], effective_date: current_route_rate['effective_date'], valid_until: current_route_rate['valid_until'], transporter_id: reference_transporter_id, route_type: current_route_rate['route_type'] route_rate.save! end response = Mks::Common::MethodResponse.new(true, "Current Route Rate Successfully generated!", reference_transporter_id, nil, nil) render json: response end def create route_type = params[:route_type] rate_type = params[:rate_type] result = RouteRate.generate_transportation_rates route_type, rate_type if result[:ind] == 1 response = Mks::Common::MethodResponse.new(true, result[:message], nil, result[:errors], nil) else errors = result[:errors] response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end render json: response end def update route_type = params[:route_type] rate_type = params[:rate_type] count = 0 route_rates = params[:updated_route_rates] route_rates.each do |route_rate| old_route_rate = RouteRate.find(route_rate['id']) old_route_rate.rate = route_rate['rate'] if old_route_rate.save count += 1 end end message = "A total of " + count.to_s + " " + route_type + " " + rate_type + " Rate updated successfully!" response = Mks::Common::MethodResponse.new(true, message, route_rates, nil, nil) render json: response end private def set_route_rate @route_rate = RouteRate.find(params[:id]) end def route_rate_params params.require(:route_rate).permit(:weight_from, :weight_to, :rate, :container_return_fee, :route_id, :effective_date, :valid_until, :transporter_id, :route_type) end end end end