module Logistics module Core class TruckRatesController < ApplicationController before_action :set_truck_rate, only: [:update] # GET /truck_rates # GET /truck_rates.json def index @truck_rates = TruckRate.all truck_rates =[] @truck_rates.each { |truck_rate| truck_rates.push({:id => truck_rate.id, :service_delivery_unit_id => truck_rate.chargeable_service_unit_of_charge.service_delivery_unit_id, :service_delivery_unit_name => truck_rate.chargeable_service_unit_of_charge.service_delivery_unit.name, :chargeable_service_id => truck_rate.chargeable_service_unit_of_charge.chargeable_service_id, :chargeable_service_name => truck_rate.chargeable_service_unit_of_charge.chargeable_service.name, :transaction_type_id => truck_rate.transaction_type_id, :transaction_type_name => truck_rate.transaction_type.name, :low => truck_rate.low, :medium => truck_rate.medium, :high => truck_rate.high, :margin => truck_rate.margin,:effective_date => truck_rate.effective_date}) } @response = {:success => true, :message => '', :data =>truck_rates} render json: @response end # POST /truck_rates # POST /truck_rates.json def create TruckRate.generate_rate_for_truck_rate(params[:effective_date]) response = Mks::Common::MethodResponse.new(true,'Truck rate generated successfully!',nil,nil,nil) render json: response end # PATCH/PUT /truck_rates/1 # PATCH/PUT /truck_rates/1.json def update service_rates = params[:updated_service_rates] service_rates.each { |service_rate| old_service_rate =TruckRate.find(service_rate['id']) old_service_rate.low = service_rate['low'] old_service_rate.medium = service_rate['medium'] old_service_rate.high = service_rate['high'] old_service_rate.margin = service_rate['margin'] old_service_rate.effective_date = service_rate['effective_date'] old_service_rate.save } response = Mks::Common::MethodResponse.new(true,'Truck rate updated successfully!',nil,nil,nil) render json: response end private # Use callbacks to share common setup or constraints between actions. def set_truck_rate @truck_rate = TruckRate.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def truck_rate_params params.require(:truck_rate).permit(:service_delivery_unit_id, :rate, :effective_date) end end end end