module Logistics module Core class KilogramRatesController < ApplicationController before_action :set_kilogram_rate, only: [:update] def index @kilogram_rates = KilogramRate.all kilogram_rates_array =[] @kilogram_rates.each do |kg| cs_id = kg.chargeable_service_unit_of_charge&.chargeable_service_id cs_name = kg.chargeable_service_unit_of_charge&.chargeable_service&.name sdu_id = kg.chargeable_service_unit_of_charge&.service_delivery_unit_id sdu_name = kg.chargeable_service_unit_of_charge&.service_delivery_unit&.name transaction_name = kg.transaction_type&.name kilogram_rates_array.push({id: kg.id, high: kg.high, low: kg.low, medium: kg.medium, effective_date: kg.effective_date, from: kg.from, to: kg.to, margin: kg.margin, chargeable_service_id: cs_id, chargeable_service_name: cs_name, service_delivery_unit_id: sdu_id, service_delivery_unit_name: sdu_name, transaction_type_id: kg.transaction_type_id, transaction_type_name: transaction_name }) end @response = {:success => true, :message => '', :data => kilogram_rates_array} render json: @response end # POST /air_weigh_bills # POST /air_weigh_bills.json def create cs_uoc = ChargeableServiceUnitOfCharge.where(service_delivery_unit_id: params[:kilogram_rate][:service_delivery_unit_id], chargeable_service_id: params[:kilogram_rate][:chargeable_service_id]) @kilogram_rate = KilogramRate.new(kilogram_rate_params) @kilogram_rate.chargeable_service_unit_of_charge_id = cs_uoc[0].id unless cs_uoc.empty? if @kilogram_rate.valid? @kilogram_rate.save @response = Mks::Common::MethodResponse.new(true, 'Kilogram rate saved successfully !',nil,nil,nil); render json: @response else @response = {:success => false, :message => 'Error saving kilogram rate', :data => []} render json: @response end end # PATCH/PUT /air_weigh_bills/1 # PATCH/PUT /air_weigh_bills/1.json def update cs_uoc = ChargeableServiceUnitOfCharge.where(service_delivery_unit_id: params[:kilogram_rate][:service_delivery_unit_id], chargeable_service_id: params[:kilogram_rate][:chargeable_service_id]) @kilogram_rate.chargeable_service_unit_of_charge_id = cs_uoc[0].id unless cs_uoc.empty? if @kilogram_rate.update_attributes(kilogram_rate_params) @kilogram_rate.save @response = {:success => true, :message => 'Kilogram rate updated successfully', :data => []} render json: @response else @response = {:success => false, :message => 'Error updating kilogram rate', :data => []} render json: @response end end private # Use callbacks to share common setup or constraints between actions. def set_kilogram_rate @kilogram_rate = KilogramRate.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def kilogram_rate_params params.require(:kilogram_rate).permit(:from, :to, :low, :medium, :high, :margin, :transaction_type_id) end end end end