require_dependency 'application_controller' module Logistics module Core class ServiceDeliveryUnitsController < ApplicationController before_action :set_service_delivery_unit, only: [:update] def index @sdus = ServiceDeliveryUnit.all service_delivery_units_array = [] @sdus.each { |sdu| service_delivery_units_array.push({:id => sdu.id, :code => sdu.code, :name => sdu.name, :address => sdu.address, :currency_id => sdu.currency_id, :currency_name => sdu.currency.name, :service_delivery_unit_type_id => sdu.service_delivery_unit_type_id, :type_name => sdu.service_delivery_unit_type.name }) } @response = {:success => true, :message => '', :data => service_delivery_units_array} render json: @response end def create @service_delivery_unit = ServiceDeliveryUnit.new(model_params) if @service_delivery_unit.valid? @service_delivery_unit.save @response = {:success => true, :message => 'Service delivery unit saved successfully!', :data => @service_delivery_unit} render json: @response else errors = Mks::Common::Util.error_messages(@service_delivery_unit, 'Service delivery unit') response = Mks::Common::MethodResponse.new(false,nil,nil,errors,nil) render json: response end end # PATCH/PUT /service_delivery_units/1 # PATCH/PUT /service_delivery_units/1.json def update if @service_delivery_unit.update_attributes(model_params) @service_delivery_unit.save @response = {:success => true, :message => 'Service delivery unit updated successfully!', :data => @service_delivery_unit} render json: @response else errors = Mks::Common::Util.error_messages(@service_delivery_unit, 'Service delivery unit') response = Mks::Common::MethodResponse.new(false,nil,nil,errors,nil) render json: response end end def get_chargeable_service_name(cs_id) cs = ChargeableService.find(cs_id) return cs.name end def chargeable_services cs_array = [] service_delivery_unit_id = params[:id] chargeable_services = ServiceDeliveryUnitChargeableService.where('service_delivery_unit_id' =>service_delivery_unit_id) chargeable_services.each { |cs| cs_array.push({:id =>cs.chargeable_service_id, :name => get_chargeable_service_name(cs.chargeable_service_id), :is_mandatory => cs.is_mandatory, sdu_cs_id: cs.id}) } @response = {:success => true, :message => '', :data => cs_array} render json: @response end def unassigned_services sdu = ServiceDeliveryUnit.find(params[:id]) ids = sdu.chargeable_services.map(&:id) unassigned_services = ChargeableService.where.not(id: ids) response = Mks::Common::MethodResponse.new(true, nil, unassigned_services, nil, nil) render json: response end def get_sdus_with_specific_services offer_request_id = params[:offer_request_id] type = params[:type] sdus = ServiceDeliveryUnit.get_sdus_with_specific_services(offer_request_id, type) response = Mks::Common::MethodResponse.new(true, nil, sdus, nil, nil) render json: response end private def set_service_delivery_unit @service_delivery_unit = ServiceDeliveryUnit.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def model_params params.require(:service_delivery_unit).permit(:code, :name, :address, :currency_id, :service_delivery_unit_type_id) end end end end