require_dependency 'application_controller' module Logistics module Core class OutOfGaugeBbRatesController < ApplicationController protect_from_forgery with: :null_session before_action :set_out_of_gauge_bb_rate, only: [:update] # GET /out_of_gauge_bb_rates # GET /out_of_gauge_bb_rates.json def index out_of_gauge_bb_rates = OutOfGaugeBbRate.fetch_all response = Mks::Common::MethodResponse.new(true, nil, out_of_gauge_bb_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_bb_rates # POST /out_of_gauge_bb_rates.json def create out_of_gauge_bb_rate = OutOfGaugeBbRate.new(out_of_gauge_bb_rate_params) margin = out_of_gauge_bb_rate_params[:margin] rate = out_of_gauge_bb_rate_params[:rate] new_rate = rate + (rate * margin/100) out_of_gauge_bb_rate.rate = new_rate if out_of_gauge_bb_rate.save response = Mks::Common::MethodResponse.new(true, "Break Bulk Out of Gauge rate information saved successfully!", out_of_gauge_bb_rate, nil, nil) else errors = Mks::Common::Util.error_messages out_of_gauge_bb_rate,"Break Bulk Out of Gauge Rate" response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end render json: response end # PATCH/PUT /out_of_gauge_bb_rates/1 # PATCH/PUT /out_of_gauge_bb_rates/1.json def update out_of_gauge_bb_rate = OutOfGaugeBbRate.find(params[:id]) margin = out_of_gauge_bb_rate_params[:margin] if margin > out_of_gauge_bb_rate.margin rate = out_of_gauge_bb_rate_params[:rate] margin = margin - out_of_gauge_bb_rate.margin new_rate = rate + (rate * margin/100) params[:out_of_gauge_bb_rate][:rate] = new_rate end if out_of_gauge_bb_rate.update(out_of_gauge_bb_rate_params) response = Mks::Common::MethodResponse.new(true, "Break Bulk Out of Gauge rate information updated successfully!", out_of_gauge_bb_rate, nil, nil) else errors = Mks::Common::Util.error_messages out_of_gauge_bb_rate,"Break Bulk 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_bb_rate @out_of_gauge_bb_rate = OutOfGaugeBbRate.find(params[:id]) end def out_of_gauge_bb_rate_params params.require(:out_of_gauge_bb_rate).permit(:route_id, :break_bulk_unit_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