module Logistics module Core class ChargeableServiceUnitOfCharge < ApplicationRecord belongs_to :service_delivery_unit belongs_to :chargeable_service belongs_to :unit_of_charge has_many :chargeable_service_increments has_many :service_increments has_many :service_increments, :through => :chargeable_service_increments delegate :name, to: :chargeable_service, prefix: true, allow_nil: true delegate :name, to: :service_delivery_unit, prefix: true, allow_nil: true delegate :name, to: :unit_of_charge, prefix: true, allow_nil: true def self.get_chargeable_service_for_sdu(sdu_id) cs_ids = ServiceDeliveryUnitChargeableService.select(:chargeable_service_id).where('service_delivery_unit_id' => sdu_id) return ChargeableService.where('id' => cs_ids) end def self.generate_chargeable_service_units service_delivery_units = ServiceDeliveryUnit.all service_delivery_units.each { |sdu| chargeable_services = get_chargeable_service_for_sdu(sdu.id) chargeable_services.each { |cs| if ChargeableServiceUnitOfCharge.where('service_delivery_unit_id' => sdu.id, 'chargeable_service_id' => cs.id).count == 0 ChargeableServiceUnitOfCharge.create('service_delivery_unit_id' => sdu.id, 'chargeable_service_id' => cs.id, 'has_increment' => FALSE) end } } end def self.get_unit_of_charge(sdu_id, cs_id) csuoc = ChargeableServiceUnitOfCharge.where('service_delivery_unit_id' => sdu_id, 'chargeable_service_id' => cs_id) unit_of_charge = UnitOfCharge.find(csuoc[0].unit_of_charge_id) return unit_of_charge.code end def self.get_sdus_with_specific_unit_of_charge(code) unit_of_charge_id = UnitOfCharge.find_by(code: code).id sdu_ids = ChargeableServiceUnitOfCharge.select(:service_delivery_unit_id). where(unit_of_charge_id: unit_of_charge_id).distinct sdus = ServiceDeliveryUnit.where(id: sdu_ids) end def self.get_chargeable_services_with_specific_sdu_and_uoc(sdu_id, code) unit_of_charge_id = UnitOfCharge.find_by(code: code) cs_ids = ChargeableServiceUnitOfCharge.select(:chargeable_service_id). where(service_delivery_unit_id: sdu_id, unit_of_charge_id: unit_of_charge_id) chargeable_services = ChargeableService.where(id: cs_ids) end end end end