Class: Logistics::Core::ContractKilogramRatesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/logistics/core/contract_kilogram_rates_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/logistics/core/contract_kilogram_rates_controller.rb', line 25

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

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/logistics/core/contract_kilogram_rates_controller.rb', line 6

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

#updateObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/logistics/core/contract_kilogram_rates_controller.rb', line 40

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