require_dependency 'application_controller' module Logistics module Core class TransportRatePeriodsController < ApplicationController protect_from_forgery with: :null_session before_action :set_rate_period, only: [:update] # GET /rate_periods # GET /rate_periods.json def index rate_periods = TransportRatePeriod.all result = [] rate_periods.each do |rate_period| if rate_period.current_period current_period = 'Yes' else current_period = 'No' end result.push({id: rate_period.id, effective_date: rate_period.effective_date, valid_until: rate_period.valid_until, current_period: current_period }) end response = Mks::Common::MethodResponse.new(true, nil, result, nil, nil) render json: response end def get_current_rate_period rate_period = TransportRatePeriod.where("current_period" => true) result = [] success = false if rate_period.length > 0 result = rate_period success = true end response = Mks::Common::MethodResponse.new(success, nil, result, nil, nil) render json: response end # POST /rate_periods # POST /rate_periods.json def create rate_period = TransportRatePeriod.new(rate_period_params) result = TransportRatePeriod.create_transport_rate_period rate_period if result[0]['success'] response = Mks::Common::MethodResponse.new(result[0]['success'], result[0]['message'], result[0]['data'], nil, nil) else response = Mks::Common::MethodResponse.new(result[0]['success'], nil, nil, result[0]['errors'], nil) end render json: response end # PATCH/PUT /rate_periods/1 # PATCH/PUT /rate_periods/1.json def update rate_period = TransportRatePeriod.find(params[:id]) new_rate_period = TransportRatePeriod.new(rate_period_params) result = TransportRatePeriod.update_transport_rate_period rate_period, new_rate_period, rate_period_params if result[0]['success'] response = Mks::Common::MethodResponse.new(result[0]['success'], result[0]['message'], result[0]['data'], nil, nil) else response = Mks::Common::MethodResponse.new(result[0]['success'], nil, nil, result[0]['errors'], nil) end render json: response end private def set_rate_period @rate_period = TransportRatePeriod.find(params[:id]) end def rate_period_params params.require(:rate_period).permit(:effective_date, :valid_until) end end end end