require_dependency 'application_controller' module Logistics module Core class OutOfGaugeCntRatesController < ApplicationController protect_from_forgery with: :null_session before_action :set_out_of_gauge_cnt_rate, only: [:update] # GET /out_of_gauge_cnt_rates # GET /out_of_gauge_cnt_rates.json def index out_of_gauge_cnt_rates = OutOfGaugeCntRate.fetch_all response = Mks::Common::MethodResponse.new(true, nil, out_of_gauge_cnt_rates, nil, nil) render json: response end def search_route route_id = params[:route][:id] route = Route.find(route_id) response = Mks::Common::MethodResponse.new(true, nil, route.margin, nil, nil) render json: response end # POST /out_of_gauge_cnt_rates # POST /out_of_gauge_cnt_rates.json def create out_of_gauge_cnt_rate = OutOfGaugeCntRate.new(out_of_gauge_cnt_rate_params) margin = out_of_gauge_cnt_rate_params[:margin] rate = out_of_gauge_cnt_rate_params[:rate] new_rate = rate + (rate * margin/100) out_of_gauge_cnt_rate.rate = new_rate if out_of_gauge_cnt_rate.save response = Mks::Common::MethodResponse.new(true, "Containerized Out of Gauge rate information saved successfully!", out_of_gauge_cnt_rate, nil, nil) else errors = Mks::Common::Util.error_messages out_of_gauge_cnt_rate,"Containerized Out of Gauge Rate" response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end render json: response end # PATCH/PUT /out_of_gauge_cnt_rates/1 # PATCH/PUT /out_of_gauge_cnt_rates/1.json def update out_of_gauge_cnt_rate = OutOfGaugeCntRate.find(params[:id]) margin = out_of_gauge_cnt_rate_params[:margin] if margin > out_of_gauge_cnt_rate.margin rate = out_of_gauge_cnt_rate_params[:rate] margin = margin - out_of_gauge_cnt_rate.margin new_rate = rate + (rate * margin/100) params[:out_of_gauge_cnt_rate][:rate] = new_rate end if out_of_gauge_cnt_rate.update(out_of_gauge_cnt_rate_params) response = Mks::Common::MethodResponse.new(true, "Containerized Out of Gauge rate information updated successfully!", out_of_gauge_cnt_rate, nil, nil) else errors = Mks::Common::Util.error_messages out_of_gauge_cnt_rate,"Containerized Out of Gauge Rate" response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end render json: response end private def set_out_of_gauge_cnt_rate @out_of_gauge_cnt_rate = OutOfGaugeCntRate.find(params[:id]) end def out_of_gauge_cnt_rate_params params.require(:out_of_gauge_cnt_rate).permit(:route_id, :container_size_id, :length_from, :length_to, :width_from, :width_to, :height_from, :height_to, :weight_from, :weight_to, :rate, :margin, :effective_date, :valid_until) end end end end