module Logistics module Core class ContractContainerRate < ApplicationRecord belongs_to :chargeable_service_unit_of_charge belongs_to :container_size belongs_to :contract belongs_to :transaction_type def self.get_service_delivery_units transport_id = ServiceDeliveryUnitType.select(:id).where('lower(name) = ?', 'Transport'.downcase) return ServiceDeliveryUnit.where.not('service_delivery_unit_type_id' => transport_id) end def self.get_chargeable_services(service_delivery_unit_id) unit_of_charge_id = UnitOfCharge.select(:id).where('code' => 'CNT') acsuocs = ChargeableServiceUnitOfCharge.where('service_delivery_unit_id' => service_delivery_unit_id, 'unit_of_charge_id' => unit_of_charge_id ) return acsuocs end def self.generate_rate_for_containerized_chargeable_service(client_contract_id) service_delivery_units = get_service_delivery_units transaction_types = TransactionType.all container_sizes = ContainerSize.all service_delivery_units.each{ |sdu| transaction_types.each { |transaction_type| container_sizes.each {|container_size| chargeable_services = get_chargeable_services(sdu.id) chargeable_services.each { |cs| service_rate = ContractContainerRate.where('chargeable_service_unit_of_charge_id' => cs.id, 'transaction_type_id' => transaction_type.id, 'container_size_id' => container_size.id, 'contract_id' => client_contract_id) if service_rate.count == 0 ContractContainerRate.create('chargeable_service_unit_of_charge_id' => cs.id, 'transaction_type_id' => transaction_type.id, 'container_size_id' => container_size.id, 'rate' => 0, 'contract_id' => client_contract_id, 'margin' => 0) end } } } } end end end end