module Logistics module Core class ContractKilogramRatesController < ApplicationController before_action :set_contract_kilogram_rate, only: [:update] def index contract_kilogram_rates = ContractKilogramRate.where(contract_id: params[:client_contract_id]) contract_kilogram_rate_array = [] contract_kilogram_rates.each do |ckg_rate| cs_id = ckg_rate.chargeable_service_unit_of_charge&.chargeable_service_id cs_name = ckg_rate.chargeable_service_unit_of_charge&.chargeable_service&.name sdu_id = ckg_rate.chargeable_service_unit_of_charge&.service_delivery_unit_id sdu_name = ckg_rate.chargeable_service_unit_of_charge&.service_delivery_unit&.name transaction_name = ckg_rate.transaction_type&.name contract_kilogram_rate_array.push({id: ckg_rate.id, from: ckg_rate.from, to: ckg_rate.to, margin: ckg_rate.margin, rate: ckg_rate.rate, in_contract: ckg_rate.in_contract, contract_id: ckg_rate.contract_id, 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: ckg_rate.transaction_type_id, transaction_type_name: transaction_name}) end result = Mks::Common::MethodResponse.new(true, nil, contract_kilogram_rate_array, nil, nil) render json: result end def create cs_uoc = ChargeableServiceUnitOfCharge.where(service_delivery_unit_id: params[:contract_kilogram_rate][:service_delivery_unit_id], chargeable_service_id: params[:contract_kilogram_rate][:chargeable_service_id]) contract_kilogram_rate = ContractKilogramRate.new(contract_kilogram_rate_params) contract_kilogram_rate.chargeable_service_unit_of_charge_id = cs_uoc[0].id unless cs_uoc.empty? if contract_kilogram_rate.valid? contract_kilogram_rate.save result = Mks::Common::MethodResponse.new(true, 'Rate saved successfully !', nil, nil, nil) else errors = Mks::Common::Util.error_messages contract_kilogram_rate, 'Contract kilogram rate' result = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end render json: result end def update cs_uoc = ChargeableServiceUnitOfCharge.where(service_delivery_unit_id: params[:contract_kilogram_rate][:service_delivery_unit_id], chargeable_service_id: params[:contract_kilogram_rate][:chargeable_service_id]) @contract_kilogram_rate.chargeable_service_unit_of_charge_id = cs_uoc[0].id unless cs_uoc.empty? if @contract_kilogram_rate.update(contract_kilogram_rate_params) result = Mks::Common::MethodResponse.new(true, 'Rate updated successfully !', nil, nil, nil) else errors = Mks::Common::Util.error_messages @contract_kilogram_rate, 'Contract kilogram rate' result = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end render json: result end private def set_contract_kilogram_rate @contract_kilogram_rate = ContractKilogramRate.find (params[:id]) end def contract_kilogram_rate_params params.require('contract_kilogram_rate').permit(:contract_id, :from, :to, :rate, :in_contract, :margin, :transaction_type_id) end end end end