class Patrimonio::DadosDoDestinoDaTransferencia < ApplicationRecord
  has_paper_trail

  attr_accessor :nome_responsavel

  belongs_to :transferencia, class_name: 'Patrimonio::Transferencia'
  belongs_to :unidade_gestora_destino, class_name: 'Loa::UnidadeGestora', foreign_key: :unidade_gestora_destino_id
  belongs_to :centro_de_custo_destino, class_name: 'Controladoria::CentroDeCusto', foreign_key: :centro_de_custo_destino_id
  belongs_to :responsavel_destino, class_name: 'Controladoria::ResponsavelDoCentroDeCusto', foreign_key: :responsavel_destino_id

  validates_presence_of :unidade_gestora_destino_id

  after_destroy :remove_itens

  def contem_itens_neste_destino?(destino = nil)
    destino = dados_dos_destinos_das_transferencias.first if destino.nil?

    self.transferencia.itens_da_transferencia.where(
      unidade_gestora: destino.unidade_gestora_destino,
      centro_de_custo: destino.centro_de_custo_destino
    ).any?
  end

  def remove_itens
    self.transferencia.itens_da_transferencia.where(transferencia: self.transferencia, unidade_gestora: self.unidade_gestora_destino, centro_de_custo: self.centro_de_custo_destino).destroy_all
  end

end
